blob: 23bc5ebdd5ee5fe280a4414e74150724bdfc4f02 [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.
Tim Northover042a6c12016-01-27 19:32:29 +0000243 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000244 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() ||
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000257 Subtarget->isTargetMuslAEABI() || 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 },
393 };
394
395 for (const auto &LC : LibraryCalls) {
396 setLibcallName(LC.Op, LC.Name);
397 setLibcallCallingConv(LC.Op, LC.CC);
398 }
399 }
400
Bob Wilsonbc158992011-10-07 16:59:21 +0000401 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000402 if (Subtarget->isTargetWatchOS() ||
403 (Subtarget->isTargetIOS() &&
404 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000405 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
406 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
407 }
408
Ahmed Bougachaf0b46ee2016-06-24 00:08:01 +0000409 // The half <-> float conversion functions are always soft-float on
410 // non-watchos platforms, but are needed for some targets which use a
411 // hard-float calling convention by default.
412 if (!Subtarget->isTargetWatchABI()) {
413 if (Subtarget->isAAPCS_ABI()) {
414 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
415 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
416 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
417 } else {
418 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
419 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
420 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
421 }
Oliver Stannard11790b22014-08-11 09:12:32 +0000422 }
423
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000424 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
425 // a __gnu_ prefix (which is the default).
426 if (Subtarget->isTargetAEABI()) {
427 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
428 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
429 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
430 }
431
David Goodwin22c2fba2009-07-08 23:10:31 +0000432 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000433 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000434 else
Craig Topperc7242e02012-04-20 07:30:17 +0000435 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000436 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000437 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000438 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000439 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000440 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000441
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000442 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000443 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000444 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000445 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
446 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
447 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
448 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000449
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000450 setOperationAction(ISD::MULHS, VT, Expand);
451 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
452 setOperationAction(ISD::MULHU, VT, Expand);
453 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000454
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000455 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000456 }
457
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000458 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000459 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000460
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000461 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
462 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
463
Bob Wilson2e076c42009-06-22 23:27:02 +0000464 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000465 addDRTypeForNEON(MVT::v2f32);
466 addDRTypeForNEON(MVT::v8i8);
467 addDRTypeForNEON(MVT::v4i16);
468 addDRTypeForNEON(MVT::v2i32);
469 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000470
Owen Anderson9f944592009-08-11 20:47:22 +0000471 addQRTypeForNEON(MVT::v4f32);
472 addQRTypeForNEON(MVT::v2f64);
473 addQRTypeForNEON(MVT::v16i8);
474 addQRTypeForNEON(MVT::v8i16);
475 addQRTypeForNEON(MVT::v4i32);
476 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000477
Bob Wilson194a2512009-09-15 23:55:57 +0000478 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
479 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000480 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
481 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000482 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
483 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
484 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000485 // FIXME: Code duplication: FDIV and FREM are expanded always, see
486 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000487 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
488 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000489 // FIXME: Create unittest.
490 // In another words, find a way when "copysign" appears in DAG with vector
491 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000492 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000493 // FIXME: Code duplication: SETCC has custom operation action, see
494 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000495 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000496 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000497 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
498 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
499 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
500 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
501 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
502 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
503 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
504 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
505 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
506 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
507 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
508 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000509 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000510 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
511 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
512 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
513 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
514 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000515 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000516
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000517 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
518 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
519 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
520 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
521 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
522 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
523 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
524 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
525 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
526 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000527 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
528 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
529 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
530 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000531 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000532
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000533 // Mark v2f32 intrinsics.
534 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
535 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
536 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
537 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
538 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
539 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
540 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
541 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
542 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
543 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
544 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
545 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
546 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
547 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
548 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
549
Bob Wilson6cc46572009-09-16 00:32:15 +0000550 // Neon does not support some operations on v1i64 and v2i64 types.
551 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000552 // Custom handling for some quad-vector types to detect VMULL.
553 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
554 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
555 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000556 // Custom handling for some vector types to avoid expensive expansions
557 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
558 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
559 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
560 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000561 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
562 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000563 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000564 // a destination type that is wider than the source, and nor does
565 // it have a FP_TO_[SU]INT instruction with a narrower destination than
566 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000567 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
568 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000569 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
570 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000571
Eli Friedmane6385e62012-11-15 22:44:27 +0000572 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000573 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000574
Evan Chengb4eae132012-12-04 22:41:50 +0000575 // NEON does not have single instruction CTPOP for vectors with element
576 // types wider than 8-bits. However, custom lowering can leverage the
577 // v8i8/v16i8 vcnt instruction.
578 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
579 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
580 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
581 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
Benjamin Kramer569efd22016-03-31 19:42:04 +0000582 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand);
583 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand);
Evan Chengb4eae132012-12-04 22:41:50 +0000584
Craig Topperedb4a6b2016-04-26 05:04:33 +0000585 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand);
586 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand);
587
Logan Chien0a43abc2015-07-13 15:37:30 +0000588 // NEON does not have single instruction CTTZ for vectors.
589 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
590 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
591 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
592 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
593
594 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
595 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
596 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
597 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
598
599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
600 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
602 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
603
604 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
605 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
606 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
607 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
608
Jim Grosbach5f215872013-02-27 21:31:12 +0000609 // NEON only has FMA instructions as of VFP4.
610 if (!Subtarget->hasVFP4()) {
611 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
612 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
613 }
614
Bob Wilson06fce872011-02-07 17:43:21 +0000615 setTargetDAGCombine(ISD::INTRINSIC_VOID);
616 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000617 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
618 setTargetDAGCombine(ISD::SHL);
619 setTargetDAGCombine(ISD::SRL);
620 setTargetDAGCombine(ISD::SRA);
621 setTargetDAGCombine(ISD::SIGN_EXTEND);
622 setTargetDAGCombine(ISD::ZERO_EXTEND);
623 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000624 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000625 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000626 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
627 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000628 setTargetDAGCombine(ISD::FP_TO_SINT);
629 setTargetDAGCombine(ISD::FP_TO_UINT);
630 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000631 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000632
James Molloy547d4c02012-02-20 09:24:05 +0000633 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000634 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
635 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000636 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000637 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
638 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
639 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000640 }
James Molloy547d4c02012-02-20 09:24:05 +0000641 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000642 }
643
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000644 // ARM and Thumb2 support UMLAL/SMLAL.
645 if (!Subtarget->isThumb1Only())
646 setTargetDAGCombine(ISD::ADDC);
647
Oliver Stannard51b1d462014-08-21 12:50:31 +0000648 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000649 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000650 // operations, f64 is legal for the few double-precision instructions which
651 // are present However, no double-precision operations other than moves,
652 // loads and stores are provided by the hardware.
653 setOperationAction(ISD::FADD, MVT::f64, Expand);
654 setOperationAction(ISD::FSUB, MVT::f64, Expand);
655 setOperationAction(ISD::FMUL, MVT::f64, Expand);
656 setOperationAction(ISD::FMA, MVT::f64, Expand);
657 setOperationAction(ISD::FDIV, MVT::f64, Expand);
658 setOperationAction(ISD::FREM, MVT::f64, Expand);
659 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
660 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
661 setOperationAction(ISD::FNEG, MVT::f64, Expand);
662 setOperationAction(ISD::FABS, MVT::f64, Expand);
663 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
664 setOperationAction(ISD::FSIN, MVT::f64, Expand);
665 setOperationAction(ISD::FCOS, MVT::f64, Expand);
666 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
667 setOperationAction(ISD::FPOW, MVT::f64, Expand);
668 setOperationAction(ISD::FLOG, MVT::f64, Expand);
669 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
670 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
671 setOperationAction(ISD::FEXP, MVT::f64, Expand);
672 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
673 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
674 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
675 setOperationAction(ISD::FRINT, MVT::f64, Expand);
676 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
677 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000678 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
679 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
680 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
681 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
682 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
683 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000684 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
685 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
686 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000687
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000688 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000689
Tim Northover4e80b582014-07-18 13:01:19 +0000690 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000691 for (MVT VT : MVT::fp_valuetypes()) {
692 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
693 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
694 }
Tim Northover4e80b582014-07-18 13:01:19 +0000695
696 // ... or truncating stores
697 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
698 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
699 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000700
Duncan Sands95d46ef2008-01-23 20:39:46 +0000701 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000702 for (MVT VT : MVT::integer_valuetypes())
703 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000704
Evan Cheng10043e22007-01-19 07:51:42 +0000705 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000706 if (!Subtarget->isThumb1Only()) {
707 for (unsigned im = (unsigned)ISD::PRE_INC;
708 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000709 setIndexedLoadAction(im, MVT::i1, Legal);
710 setIndexedLoadAction(im, MVT::i8, Legal);
711 setIndexedLoadAction(im, MVT::i16, Legal);
712 setIndexedLoadAction(im, MVT::i32, Legal);
713 setIndexedStoreAction(im, MVT::i1, Legal);
714 setIndexedStoreAction(im, MVT::i8, Legal);
715 setIndexedStoreAction(im, MVT::i16, Legal);
716 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000717 }
James Molloyb3326df2016-07-15 08:03:56 +0000718 } else {
719 // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}.
720 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
721 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
Evan Cheng10043e22007-01-19 07:51:42 +0000722 }
723
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000724 setOperationAction(ISD::SADDO, MVT::i32, Custom);
725 setOperationAction(ISD::UADDO, MVT::i32, Custom);
726 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
727 setOperationAction(ISD::USUBO, MVT::i32, Custom);
728
Evan Cheng10043e22007-01-19 07:51:42 +0000729 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000730 setOperationAction(ISD::MUL, MVT::i64, Expand);
731 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000732 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000733 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
734 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000735 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000736 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000737 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000738 setOperationAction(ISD::MULHS, MVT::i32, Expand);
739
Jim Grosbach5d994042009-10-31 19:38:01 +0000740 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000741 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000742 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000743 setOperationAction(ISD::SRL, MVT::i64, Custom);
744 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000745
Evan Chenge8916542011-08-30 01:34:54 +0000746 if (!Subtarget->isThumb1Only()) {
747 // FIXME: We should do this for Thumb1 as well.
748 setOperationAction(ISD::ADDC, MVT::i32, Custom);
749 setOperationAction(ISD::ADDE, MVT::i32, Custom);
750 setOperationAction(ISD::SUBC, MVT::i32, Custom);
751 setOperationAction(ISD::SUBE, MVT::i32, Custom);
752 }
753
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000754 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000755 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
756
Evan Cheng10043e22007-01-19 07:51:42 +0000757 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000758 setOperationAction(ISD::ROTL, MVT::i32, Expand);
759 for (MVT VT : MVT::vector_valuetypes()) {
760 setOperationAction(ISD::ROTL, VT, Expand);
761 setOperationAction(ISD::ROTR, VT, Expand);
762 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000763 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000764 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000765 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000766 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000767
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000768 // @llvm.readcyclecounter requires the Performance Monitors extension.
769 // Default to the 0 expansion on unsupported platforms.
770 // FIXME: Technically there are older ARM CPUs that have
771 // implementation-specific ways of obtaining this information.
772 if (Subtarget->hasPerfMon())
773 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000774
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000775 // Only ARMv6 has BSWAP.
776 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000777 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000778
Bradley Smith519563e2016-01-15 10:25:35 +0000779 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide()
780 : Subtarget->hasDivideInARMMode();
781 if (!hasDivide) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000782 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000783 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
784 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000785 }
Renato Golin87610692013-07-16 09:32:17 +0000786
Saleem Abdulrasool071a0992016-03-17 14:10:49 +0000787 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
788 setOperationAction(ISD::SDIV, MVT::i32, Custom);
789 setOperationAction(ISD::UDIV, MVT::i32, Custom);
790
791 setOperationAction(ISD::SDIV, MVT::i64, Custom);
792 setOperationAction(ISD::UDIV, MVT::i64, Custom);
793 }
794
Chad Rosierad7c9102014-08-23 18:29:43 +0000795 setOperationAction(ISD::SREM, MVT::i32, Expand);
796 setOperationAction(ISD::UREM, MVT::i32, Expand);
797 // Register based DivRem for AEABI (RTABI 4.2)
Renato Golin6027dd38e2016-02-03 16:10:54 +0000798 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000799 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000800 setOperationAction(ISD::SREM, MVT::i64, Custom);
801 setOperationAction(ISD::UREM, MVT::i64, Custom);
Diana Picus774d1572016-07-18 06:48:25 +0000802 HasStandaloneRem = false;
Scott Douglassbdef6042015-08-24 09:17:18 +0000803
Chad Rosierad7c9102014-08-23 18:29:43 +0000804 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
805 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
806 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
807 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
808 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
809 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
810 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
811 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
812
813 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
814 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
815 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
816 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
817 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
818 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
819 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
820 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
821
822 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
823 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Renato Golin175c6d62016-03-04 19:19:36 +0000824 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
825 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Chad Rosierad7c9102014-08-23 18:29:43 +0000826 } else {
Renato Golin87610692013-07-16 09:32:17 +0000827 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
828 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
829 }
Bob Wilson7117a912009-03-20 22:42:55 +0000830
Owen Anderson9f944592009-08-11 20:47:22 +0000831 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
832 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000833 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000834 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000835
Evan Cheng74d92c12011-04-08 21:37:21 +0000836 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000837
Evan Cheng10043e22007-01-19 07:51:42 +0000838 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000839 setOperationAction(ISD::VASTART, MVT::Other, Custom);
840 setOperationAction(ISD::VAARG, MVT::Other, Expand);
841 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
842 setOperationAction(ISD::VAEND, MVT::Other, Expand);
843 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
844 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000845
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000846 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
847 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
848 else
849 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
850
Evan Cheng6e809de2010-08-11 06:22:01 +0000851 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
James Y Knighte6a46462016-04-01 19:33:19 +0000852 // the default expansion.
James Y Knightf44fc522016-03-16 22:12:04 +0000853 InsertFencesForAtomic = false;
James Y Knighte6a46462016-04-01 19:33:19 +0000854 if (Subtarget->hasAnyDataBarrier() &&
855 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000856 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
857 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000858 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverb629c772016-04-18 21:48:55 +0000859 if (!Subtarget->isThumb() || !Subtarget->isMClass())
860 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000861
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000862 // On v8, we have particularly efficient implementations of atomic fences
863 // if they can be combined with nearby atomic loads and stores.
Tim Northoverb629c772016-04-18 21:48:55 +0000864 if (!Subtarget->hasV8Ops() || getTargetMachine().getOptLevel() == 0) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000865 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
James Y Knightf44fc522016-03-16 22:12:04 +0000866 InsertFencesForAtomic = true;
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000867 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000868 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000869 // If there's anything we can use as a barrier, go through custom lowering
870 // for ATOMIC_FENCE.
871 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
872 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
873
Jim Grosbach6860bb72010-06-18 22:35:32 +0000874 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000875 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000876 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000877 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000878 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000879 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000880 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000881 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000882 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000883 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000884 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000885 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000886 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000887 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
888 // Unordered/Monotonic case.
889 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
890 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000891 }
Evan Cheng10043e22007-01-19 07:51:42 +0000892
Evan Cheng21acf9f2010-11-04 05:19:35 +0000893 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000894
Eli Friedman8cfa7712010-06-26 04:36:50 +0000895 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
896 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000897 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
898 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000899 }
Owen Anderson9f944592009-08-11 20:47:22 +0000900 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000901
Eric Christopher824f42f2015-05-12 01:26:05 +0000902 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000903 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000904 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000905 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000906 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000907 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
908 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000909
910 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000911 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000912 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
913 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
914 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +0000915 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +0000916 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000917
Owen Anderson9f944592009-08-11 20:47:22 +0000918 setOperationAction(ISD::SETCC, MVT::i32, Expand);
919 setOperationAction(ISD::SETCC, MVT::f32, Expand);
920 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000921 setOperationAction(ISD::SELECT, MVT::i32, Custom);
922 setOperationAction(ISD::SELECT, MVT::f32, Custom);
923 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000924 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
925 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
926 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000927
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +0000928 // Thumb-1 cannot currently select ARMISD::SUBE.
929 if (!Subtarget->isThumb1Only())
930 setOperationAction(ISD::SETCCE, MVT::i32, Custom);
931
Owen Anderson9f944592009-08-11 20:47:22 +0000932 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
933 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
934 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
935 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
936 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000937
Dan Gohman482732a2007-10-11 23:21:31 +0000938 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000939 setOperationAction(ISD::FSIN, MVT::f64, Expand);
940 setOperationAction(ISD::FSIN, MVT::f32, Expand);
941 setOperationAction(ISD::FCOS, MVT::f32, Expand);
942 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000943 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
944 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000945 setOperationAction(ISD::FREM, MVT::f64, Expand);
946 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000947 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000948 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000949 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
950 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000951 }
Owen Anderson9f944592009-08-11 20:47:22 +0000952 setOperationAction(ISD::FPOW, MVT::f64, Expand);
953 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000954
Evan Chengd0007f32012-04-10 21:40:28 +0000955 if (!Subtarget->hasVFP4()) {
956 setOperationAction(ISD::FMA, MVT::f64, Expand);
957 setOperationAction(ISD::FMA, MVT::f32, Expand);
958 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000959
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000960 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000961 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000962 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
963 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000964 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
965 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
966 }
967
968 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000969 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000970 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
971 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000972 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000973 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000974
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000975 // Combine sin / cos into one node or libcall if possible.
976 if (Subtarget->hasSinCos()) {
977 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
978 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover042a6c12016-01-27 19:32:29 +0000979 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000980 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
981 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
982 }
983 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000984 // For iOS, we don't want to the normal expansion of a libcall to
985 // sincos. We want to issue a libcall to __sincos_stret.
986 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
987 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
988 }
989 }
Evan Cheng10043e22007-01-19 07:51:42 +0000990
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000991 // FP-ARMv8 implements a lot of rounding-like FP operations.
992 if (Subtarget->hasFPARMv8()) {
993 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
994 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
995 setOperationAction(ISD::FROUND, MVT::f32, Legal);
996 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
997 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
998 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000999 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1000 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +00001001 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
1002 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
1003 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
1004 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
1005
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001006 if (!Subtarget->isFPOnlySP()) {
1007 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1008 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1009 setOperationAction(ISD::FROUND, MVT::f64, Legal);
1010 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1011 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1012 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001013 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1014 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001015 }
1016 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001017
James Molloy974838f2015-08-17 19:37:12 +00001018 if (Subtarget->hasNEON()) {
1019 // vmin and vmax aren't available in a scalar form, so we use
1020 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001021 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1022 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001023 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1024 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1025 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1026 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1027 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001028
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001029 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001030 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001031 setTargetDAGCombine(ISD::ADD);
1032 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001033 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001034 setTargetDAGCombine(ISD::AND);
1035 setTargetDAGCombine(ISD::OR);
1036 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001037
Evan Chengf258a152012-02-23 02:58:19 +00001038 if (Subtarget->hasV6Ops())
1039 setTargetDAGCombine(ISD::SRL);
1040
Evan Cheng10043e22007-01-19 07:51:42 +00001041 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001042
Eric Christopher824f42f2015-05-12 01:26:05 +00001043 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001044 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001045 setSchedulingPreference(Sched::RegPressure);
1046 else
1047 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001048
Evan Cheng3ae2b792011-01-06 06:52:41 +00001049 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001050 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001051 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001052 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001053 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001054 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001055 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001056
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001057 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1058 // are at least 4 bytes aligned.
1059 setMinStackArgumentAlignment(4);
1060
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001061 // Prefer likely predicted branches to selects on out-of-order cores.
Junmo Park453f4aa2016-02-23 09:56:58 +00001062 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001063
Eli Friedman2518f832011-05-06 20:34:06 +00001064 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001065}
1066
Eric Christopher824f42f2015-05-12 01:26:05 +00001067bool ARMTargetLowering::useSoftFloat() const {
1068 return Subtarget->useSoftFloat();
1069}
1070
Andrew Trick43f25632011-01-19 02:35:27 +00001071// FIXME: It might make sense to define the representative register class as the
1072// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1073// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1074// SPR's representative would be DPR_VFP2. This should work well if register
1075// pressure tracking were modified such that a register use would increment the
1076// pressure of the register class's representative and all of it's super
1077// classes' representatives transitively. We have not implemented this because
1078// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001079// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001080// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001081std::pair<const TargetRegisterClass *, uint8_t>
1082ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1083 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001084 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001085 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001086 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001087 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001088 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001089 // Use DPR as representative register class for all floating point
1090 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1091 // the cost is 1 for both f32 and f64.
1092 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001093 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001094 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001095 // When NEON is used for SP, only half of the register file is available
1096 // because operations that define both SP and DP results will be constrained
1097 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1098 // coalescing by double-counting the SP regs. See the FIXME above.
1099 if (Subtarget->useNEONForSinglePrecisionFP())
1100 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001101 break;
1102 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1103 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001104 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001105 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001106 break;
1107 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001108 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001109 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001110 break;
1111 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001112 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001113 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001114 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001115 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001116 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001117}
1118
Evan Cheng10043e22007-01-19 07:51:42 +00001119const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001120 switch ((ARMISD::NodeType)Opcode) {
1121 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001122 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001123 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001124 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001125 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001126 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001127 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001128 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
Evan Cheng10043e22007-01-19 07:51:42 +00001129 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1130 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001131 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001132 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001133 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001134 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1135 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001136 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001137 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001138 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1139 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001140 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001141 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001142
Evan Cheng10043e22007-01-19 07:51:42 +00001143 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001144
Pablo Barrio7a643462016-06-23 16:53:49 +00001145 case ARMISD::SSAT: return "ARMISD::SSAT";
1146
Evan Cheng10043e22007-01-19 07:51:42 +00001147 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1148 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1149 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001150
Evan Chenge8916542011-08-30 01:34:54 +00001151 case ARMISD::ADDC: return "ARMISD::ADDC";
1152 case ARMISD::ADDE: return "ARMISD::ADDE";
1153 case ARMISD::SUBC: return "ARMISD::SUBC";
1154 case ARMISD::SUBE: return "ARMISD::SUBE";
1155
Bob Wilson22806742010-09-22 22:09:21 +00001156 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1157 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001158
Evan Chengec6d7c92009-10-28 06:55:03 +00001159 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001160 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1161 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001162
Dale Johannesend679ff72010-06-03 21:09:53 +00001163 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001164
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001165 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001166
Evan Chengb972e562009-08-07 00:34:42 +00001167 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1168
Bob Wilson7ed59712010-10-30 00:54:37 +00001169 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001170
Evan Cheng8740ee32010-11-03 06:34:55 +00001171 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1172
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001173 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001174 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001175
Bob Wilson2e076c42009-06-22 23:27:02 +00001176 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001177 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001178 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001179 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1180 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001181 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1182 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001183 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1184 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001185 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1186 case ARMISD::VTST: return "ARMISD::VTST";
1187
1188 case ARMISD::VSHL: return "ARMISD::VSHL";
1189 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1190 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001191 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1192 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1193 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1194 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1195 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1196 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1197 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1198 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1199 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1200 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1201 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1202 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001203 case ARMISD::VSLI: return "ARMISD::VSLI";
1204 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001205 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1206 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001207 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001208 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001209 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001210 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001211 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001212 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001213 case ARMISD::VREV64: return "ARMISD::VREV64";
1214 case ARMISD::VREV32: return "ARMISD::VREV32";
1215 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001216 case ARMISD::VZIP: return "ARMISD::VZIP";
1217 case ARMISD::VUZP: return "ARMISD::VUZP";
1218 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001219 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1220 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001221 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1222 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Sam Parkerd616cf02016-06-20 16:47:09 +00001223 case ARMISD::UMAAL: return "ARMISD::UMAAL";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001224 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1225 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001226 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001227 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001228 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1229 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001230 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001231 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001232 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1233 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1234 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001235 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1236 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1237 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1238 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1239 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1240 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1241 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1242 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1243 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1244 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1245 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1246 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1247 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1248 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1249 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1250 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1251 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001252 }
Matthias Braund04893f2015-05-07 21:33:59 +00001253 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001254}
1255
Mehdi Amini44ede332015-07-09 02:09:04 +00001256EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1257 EVT VT) const {
1258 if (!VT.isVector())
1259 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001260 return VT.changeVectorElementTypeToInteger();
1261}
1262
Evan Cheng4cad68e2010-05-15 02:18:07 +00001263/// getRegClassFor - Return the register class that should be used for the
1264/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001265const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001266 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1267 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1268 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001269 if (Subtarget->hasNEON()) {
1270 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001271 return &ARM::QQPRRegClass;
1272 if (VT == MVT::v8i64)
1273 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001274 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001275 return TargetLowering::getRegClassFor(VT);
1276}
1277
John Brawn0dbcd652015-03-18 12:01:59 +00001278// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1279// source/dest is aligned and the copy size is large enough. We therefore want
1280// to align such objects passed to memory intrinsics.
1281bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1282 unsigned &PrefAlign) const {
1283 if (!isa<MemIntrinsic>(CI))
1284 return false;
1285 MinSize = 8;
1286 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1287 // cycle faster than 4-byte aligned LDM.
1288 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1289 return true;
1290}
1291
Eric Christopher84bdfd82010-07-21 22:26:11 +00001292// Create a fast isel object.
1293FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001294ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1295 const TargetLibraryInfo *libInfo) const {
1296 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001297}
1298
Evan Cheng4401f882010-05-20 23:26:43 +00001299Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001300 unsigned NumVals = N->getNumValues();
1301 if (!NumVals)
1302 return Sched::RegPressure;
1303
1304 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001305 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001306 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001307 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001308 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001309 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001310 }
Evan Chengbf914992010-05-28 23:25:23 +00001311
1312 if (!N->isMachineOpcode())
1313 return Sched::RegPressure;
1314
1315 // Load are scheduled for latency even if there instruction itinerary
1316 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001317 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001318 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001319
Evan Cheng6cc775f2011-06-28 19:10:37 +00001320 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001321 return Sched::RegPressure;
1322 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001323 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001324 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001325
Evan Cheng4401f882010-05-20 23:26:43 +00001326 return Sched::RegPressure;
1327}
1328
Evan Cheng10043e22007-01-19 07:51:42 +00001329//===----------------------------------------------------------------------===//
1330// Lowering Code
1331//===----------------------------------------------------------------------===//
1332
Evan Cheng10043e22007-01-19 07:51:42 +00001333/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1334static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1335 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001336 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001337 case ISD::SETNE: return ARMCC::NE;
1338 case ISD::SETEQ: return ARMCC::EQ;
1339 case ISD::SETGT: return ARMCC::GT;
1340 case ISD::SETGE: return ARMCC::GE;
1341 case ISD::SETLT: return ARMCC::LT;
1342 case ISD::SETLE: return ARMCC::LE;
1343 case ISD::SETUGT: return ARMCC::HI;
1344 case ISD::SETUGE: return ARMCC::HS;
1345 case ISD::SETULT: return ARMCC::LO;
1346 case ISD::SETULE: return ARMCC::LS;
1347 }
1348}
1349
Bob Wilsona2e83332009-09-09 23:14:54 +00001350/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1351static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001352 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001353 CondCode2 = ARMCC::AL;
1354 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001355 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001356 case ISD::SETEQ:
1357 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1358 case ISD::SETGT:
1359 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1360 case ISD::SETGE:
1361 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1362 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001363 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001364 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1365 case ISD::SETO: CondCode = ARMCC::VC; break;
1366 case ISD::SETUO: CondCode = ARMCC::VS; break;
1367 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1368 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1369 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1370 case ISD::SETLT:
1371 case ISD::SETULT: CondCode = ARMCC::LT; break;
1372 case ISD::SETLE:
1373 case ISD::SETULE: CondCode = ARMCC::LE; break;
1374 case ISD::SETNE:
1375 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1376 }
Evan Cheng10043e22007-01-19 07:51:42 +00001377}
1378
Bob Wilsona4c22902009-04-17 19:07:39 +00001379//===----------------------------------------------------------------------===//
1380// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001381//===----------------------------------------------------------------------===//
1382
1383#include "ARMGenCallingConv.inc"
1384
Oliver Stannardc24f2172014-05-09 14:01:47 +00001385/// getEffectiveCallingConv - Get the effective calling convention, taking into
1386/// account presence of floating point hardware and calling convention
1387/// limitations, such as support for variadic functions.
1388CallingConv::ID
1389ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1390 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001391 switch (CC) {
1392 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001393 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001394 case CallingConv::ARM_AAPCS:
1395 case CallingConv::ARM_APCS:
1396 case CallingConv::GHC:
1397 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001398 case CallingConv::PreserveMost:
1399 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001400 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001401 case CallingConv::Swift:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001402 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1403 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001404 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001405 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001406 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001407 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1408 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001409 return CallingConv::ARM_AAPCS_VFP;
1410 else
1411 return CallingConv::ARM_AAPCS;
1412 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001413 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001414 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001415 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001416 return CallingConv::Fast;
1417 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001418 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001419 return CallingConv::ARM_AAPCS_VFP;
1420 else
1421 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001422 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001423}
1424
1425/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1426/// CallingConvention.
1427CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1428 bool Return,
1429 bool isVarArg) const {
1430 switch (getEffectiveCallingConv(CC, isVarArg)) {
1431 default:
1432 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001433 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001434 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001435 case CallingConv::ARM_AAPCS:
1436 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1437 case CallingConv::ARM_AAPCS_VFP:
1438 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1439 case CallingConv::Fast:
1440 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001441 case CallingConv::GHC:
1442 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001443 case CallingConv::PreserveMost:
1444 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001445 }
1446}
1447
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001448/// LowerCallResult - Lower the result values of a call into the
1449/// appropriate copies out of appropriate physical registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001450SDValue ARMTargetLowering::LowerCallResult(
1451 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1452 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1453 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
1454 SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001455
Bob Wilsona4c22902009-04-17 19:07:39 +00001456 // Assign locations to each value returned by this call.
1457 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001458 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1459 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001460 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001461 CCAssignFnForNode(CallConv, /* Return*/ true,
1462 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001463
1464 // Copy all of the result registers out of their specified physreg.
1465 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1466 CCValAssign VA = RVLocs[i];
1467
Stephen Linb8bd2322013-04-20 05:14:40 +00001468 // Pass 'this' value directly from the argument to return value, to avoid
1469 // reg unit interference
David Majnemer5d261272016-07-20 04:13:01 +00001470 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001471 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1472 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001473 InVals.push_back(ThisVal);
1474 continue;
1475 }
1476
Bob Wilson0041bd32009-04-25 00:33:20 +00001477 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001478 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001479 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001480 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001481 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001482 Chain = Lo.getValue(1);
1483 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001484 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001485 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001486 InFlag);
1487 Chain = Hi.getValue(1);
1488 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001489 if (!Subtarget->isLittle())
1490 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001491 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001492
Owen Anderson9f944592009-08-11 20:47:22 +00001493 if (VA.getLocVT() == MVT::v2f64) {
1494 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1495 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001496 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001497
1498 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001499 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001500 Chain = Lo.getValue(1);
1501 InFlag = Lo.getValue(2);
1502 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001503 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001504 Chain = Hi.getValue(1);
1505 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001506 if (!Subtarget->isLittle())
1507 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001508 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001509 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001510 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001511 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001512 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001513 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1514 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001515 Chain = Val.getValue(1);
1516 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001517 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001518
1519 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001520 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001521 case CCValAssign::Full: break;
1522 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001523 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001524 break;
1525 }
1526
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001527 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001528 }
1529
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001530 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001531}
1532
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001533/// LowerMemOpCallTo - Store the argument to the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001534SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
1535 SDValue Arg, const SDLoc &dl,
1536 SelectionDAG &DAG,
1537 const CCValAssign &VA,
1538 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001539 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001540 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001541 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1542 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001543 return DAG.getStore(
1544 Chain, dl, Arg, PtrOff,
Justin Lebar9c375812016-07-15 18:27:10 +00001545 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset));
Evan Cheng10043e22007-01-19 07:51:42 +00001546}
1547
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001548void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001549 SDValue Chain, SDValue &Arg,
1550 RegsToPassVector &RegsToPass,
1551 CCValAssign &VA, CCValAssign &NextVA,
1552 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001553 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001554 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001555
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001556 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001557 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001558 unsigned id = Subtarget->isLittle() ? 0 : 1;
1559 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001560
1561 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001562 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001563 else {
1564 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001565 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001566 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1567 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001568
Christian Pirkerb5728192014-05-08 14:06:24 +00001569 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001570 dl, DAG, NextVA,
1571 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001572 }
1573}
1574
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001575/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001576/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1577/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001578SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001579ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001580 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001581 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001582 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001583 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1584 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1585 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001586 SDValue Chain = CLI.Chain;
1587 SDValue Callee = CLI.Callee;
1588 bool &isTailCall = CLI.IsTailCall;
1589 CallingConv::ID CallConv = CLI.CallConv;
1590 bool doesNotRet = CLI.DoesNotReturn;
1591 bool isVarArg = CLI.IsVarArg;
1592
Dale Johannesend679ff72010-06-03 21:09:53 +00001593 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001594 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1595 bool isThisReturn = false;
1596 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001597 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001598
Bob Wilson8decdc42011-10-07 17:17:49 +00001599 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001600 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001601 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001602
Dale Johannesend679ff72010-06-03 21:09:53 +00001603 if (isTailCall) {
1604 // Check if it's really possible to do a tail call.
1605 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001606 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001607 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001608 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1609 report_fatal_error("failed to perform tail call elimination on a call "
1610 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001611 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1612 // detected sibcalls.
1613 if (isTailCall) {
1614 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001615 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001616 }
1617 }
Evan Cheng10043e22007-01-19 07:51:42 +00001618
Bob Wilsona4c22902009-04-17 19:07:39 +00001619 // Analyze operands of the call, assigning locations to each operand.
1620 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001621 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1622 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001623 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001624 CCAssignFnForNode(CallConv, /* Return*/ false,
1625 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001626
Bob Wilsona4c22902009-04-17 19:07:39 +00001627 // Get a count of how many bytes are to be pushed on the stack.
1628 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001629
Dale Johannesend679ff72010-06-03 21:09:53 +00001630 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001631 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001632 NumBytes = 0;
1633
Evan Cheng10043e22007-01-19 07:51:42 +00001634 // Adjust the stack pointer for the new arguments...
1635 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001636 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001637 Chain = DAG.getCALLSEQ_START(Chain,
1638 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001639
Mehdi Amini44ede332015-07-09 02:09:04 +00001640 SDValue StackPtr =
1641 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001642
Bob Wilson2e076c42009-06-22 23:27:02 +00001643 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001644 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001645
Bob Wilsona4c22902009-04-17 19:07:39 +00001646 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001647 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001648 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1649 i != e;
1650 ++i, ++realArgIdx) {
1651 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001652 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001653 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001654 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001655
Bob Wilsona4c22902009-04-17 19:07:39 +00001656 // Promote the value if needed.
1657 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001658 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001659 case CCValAssign::Full: break;
1660 case CCValAssign::SExt:
1661 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1662 break;
1663 case CCValAssign::ZExt:
1664 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1665 break;
1666 case CCValAssign::AExt:
1667 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1668 break;
1669 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001670 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001671 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001672 }
1673
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001674 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001675 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001676 if (VA.getLocVT() == MVT::v2f64) {
1677 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001678 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001679 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001680 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001681
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001682 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001683 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1684
1685 VA = ArgLocs[++i]; // skip ahead to next loc
1686 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001687 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001688 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1689 } else {
1690 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001691
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001692 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1693 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001694 }
1695 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001696 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001697 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001698 }
1699 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001700 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1701 assert(VA.getLocVT() == MVT::i32 &&
1702 "unexpected calling convention register assignment");
1703 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001704 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001705 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001706 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001707 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001708 } else if (isByVal) {
1709 assert(VA.isMemLoc());
1710 unsigned offset = 0;
1711
1712 // True if this byval aggregate will be split between registers
1713 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001714 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001715 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001716
1717 if (CurByValIdx < ByValArgsCount) {
1718
1719 unsigned RegBegin, RegEnd;
1720 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1721
Mehdi Amini44ede332015-07-09 02:09:04 +00001722 EVT PtrVT =
1723 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001724 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001725 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001726 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001727 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1728 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1729 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001730 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001731 MemOpChains.push_back(Load.getValue(1));
1732 RegsToPass.push_back(std::make_pair(j, Load));
1733 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001734
1735 // If parameter size outsides register area, "offset" value
1736 // helps us to calculate stack slot for remained part properly.
1737 offset = RegEnd - RegBegin;
1738
1739 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001740 }
1741
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001742 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001743 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001744 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001745 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001746 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001747 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001748 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001749 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001750 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001751 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1752 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001753
Manman Ren9f911162012-06-01 02:44:42 +00001754 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001755 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001756 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001757 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001758 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001759 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001760 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001761
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001762 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1763 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001764 }
Evan Cheng10043e22007-01-19 07:51:42 +00001765 }
1766
1767 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001768 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001769
1770 // Build a sequence of copy-to-reg nodes chained together with token chain
1771 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001772 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001773 // Tail call byval lowering might overwrite argument registers so in case of
1774 // tail call optimization the copies to registers are lowered later.
1775 if (!isTailCall)
1776 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1777 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1778 RegsToPass[i].second, InFlag);
1779 InFlag = Chain.getValue(1);
1780 }
Evan Cheng10043e22007-01-19 07:51:42 +00001781
Dale Johannesend679ff72010-06-03 21:09:53 +00001782 // For tail calls lower the arguments to the 'real' stack slot.
1783 if (isTailCall) {
1784 // Force all the incoming stack arguments to be loaded from the stack
1785 // before any new outgoing arguments are stored to the stack, because the
1786 // outgoing stack slots may alias the incoming argument stack slots, and
1787 // the alias isn't otherwise explicit. This is slightly more conservative
1788 // than necessary, because it means that each store effectively depends
1789 // on every argument instead of just those arguments it would clobber.
1790
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001791 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001792 InFlag = SDValue();
1793 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1794 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1795 RegsToPass[i].second, InFlag);
1796 InFlag = Chain.getValue(1);
1797 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001798 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001799 }
1800
Bill Wendling24c79f22008-09-16 21:48:12 +00001801 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1802 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1803 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001804 bool isDirect = false;
Rafael Espindola3888bdb2016-06-16 15:22:01 +00001805
1806 const TargetMachine &TM = getTargetMachine();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00001807 const Module *Mod = MF.getFunction()->getParent();
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001808 const GlobalValue *GV = nullptr;
1809 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1810 GV = G->getGlobal();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00001811 bool isStub =
Rafael Espindola3beef8d2016-06-27 23:15:57 +00001812 !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO();
Rafael Espindola3888bdb2016-06-16 15:22:01 +00001813
Rafael Espindola9ba9c5b2016-06-16 15:44:06 +00001814 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001815 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001816 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001817 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001818
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001819 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool4d950ef2016-07-05 18:30:52 +00001820 assert((!isPositionIndependent() || Subtarget->isTargetWindows()) &&
Rafael Espindola0f898332016-06-20 16:43:17 +00001821 "long-calls codegen is not position independent!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001822 // Handle a global address or an external symbol. If it's not one of
1823 // those, the target's already in a register, so we don't need to do
1824 // anything extra.
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001825 if (isa<GlobalAddressSDNode>(Callee)) {
Jim Grosbach32bb3622010-04-14 22:28:31 +00001826 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001827 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001828 ARMConstantPoolValue *CPV =
1829 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1830
Jim Grosbach32bb3622010-04-14 22:28:31 +00001831 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001832 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001833 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001834 Callee = DAG.getLoad(
1835 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001836 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach32bb3622010-04-14 22:28:31 +00001837 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1838 const char *Sym = S->getSymbol();
1839
1840 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001841 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001842 ARMConstantPoolValue *CPV =
1843 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1844 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001845 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001846 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001847 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001848 Callee = DAG.getLoad(
1849 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001850 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach32bb3622010-04-14 22:28:31 +00001851 }
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001852 } else if (isa<GlobalAddressSDNode>(Callee)) {
James Molloy2af08fa2016-07-15 07:57:35 +00001853 // If we're optimizing for minimum size and the function is called three or
1854 // more times in this block, we can improve codesize by calling indirectly
James Molloya454a112016-07-15 07:55:21 +00001855 // as BLXr has a 16-bit encoding.
1856 auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
1857 auto *BB = CLI.CS->getParent();
1858 bool PreferIndirect =
1859 Subtarget->isThumb() && MF.getFunction()->optForMinSize() &&
1860 std::count_if(GV->user_begin(), GV->user_end(), [&BB](const User *U) {
1861 return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB;
1862 }) > 2;
Rafael Espindola41410cc2016-06-01 21:57:11 +00001863
James Molloya454a112016-07-15 07:55:21 +00001864 if (!PreferIndirect) {
1865 isDirect = true;
1866 bool isDef = GV->isStrongDefinitionForLinker();
1867
1868 // ARM call to a local ARM function is predicable.
1869 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
1870 // tBX takes a register source operand.
1871 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
1872 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
1873 Callee = DAG.getNode(
1874 ARMISD::WrapperPIC, dl, PtrVt,
1875 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
Mehdi Amini44ede332015-07-09 02:09:04 +00001876 Callee =
James Molloya454a112016-07-15 07:55:21 +00001877 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001878 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Justin Lebar9c375812016-07-15 18:27:10 +00001879 /* Alignment = */ 0, MachineMemOperand::MOInvariant);
James Molloya454a112016-07-15 07:55:21 +00001880 } else if (Subtarget->isTargetCOFF()) {
1881 assert(Subtarget->isTargetWindows() &&
1882 "Windows is the only supported COFF target");
1883 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1884 ? ARMII::MO_DLLIMPORT
1885 : ARMII::MO_NO_FLAG;
1886 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0,
1887 TargetFlags);
1888 if (GV->hasDLLImportStorageClass())
1889 Callee =
1890 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1891 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Justin Lebar9c375812016-07-15 18:27:10 +00001892 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
James Molloya454a112016-07-15 07:55:21 +00001893 } else {
1894 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0);
1895 }
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001896 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001897 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001898 isDirect = true;
Evan Cheng83f35172007-01-30 20:37:08 +00001899 // tBX takes a register source operand.
1900 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001901 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001902 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001903 ARMConstantPoolValue *CPV =
1904 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1905 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001906 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001907 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001908 Callee = DAG.getLoad(
1909 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00001910 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001911 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001912 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001913 } else {
Rafael Espindolaafade352016-06-16 16:09:53 +00001914 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001915 }
Evan Cheng10043e22007-01-19 07:51:42 +00001916 }
1917
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001918 // FIXME: handle tail calls differently.
1919 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001920 if (Subtarget->isThumb()) {
1921 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001922 CallOpc = ARMISD::CALL_NOLINK;
1923 else
Tim Northoverb5ece522016-05-10 19:17:47 +00001924 CallOpc = ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001925 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001926 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001927 CallOpc = ARMISD::CALL_NOLINK;
Sjoerd Meijerd906bf12016-06-03 14:03:27 +00001928 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001929 // Emit regular call when code size is the priority
1930 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001931 // "mov lr, pc; b _foo" to avoid confusing the RSP
1932 CallOpc = ARMISD::CALL_NOLINK;
1933 else
1934 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001935 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001936
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001937 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001938 Ops.push_back(Chain);
1939 Ops.push_back(Callee);
1940
1941 // Add argument registers to the end of the list so that they are known live
1942 // into the call.
1943 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1944 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1945 RegsToPass[i].second.getValueType()));
1946
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001947 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001948 if (!isTailCall) {
1949 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001950 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001951 if (isThisReturn) {
1952 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001953 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001954 if (!Mask) {
1955 // Set isThisReturn to false if the calling convention is not one that
1956 // allows 'returned' to be modeled in this way, so LowerCallResult does
1957 // not try to pass 'this' straight through
1958 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001959 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001960 }
1961 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001962 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001963
Matthias Braunc22630e2013-10-04 16:52:54 +00001964 assert(Mask && "Missing call preserved mask for calling convention");
1965 Ops.push_back(DAG.getRegisterMask(Mask));
1966 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001967
Gabor Greiff304a7a2008-08-28 21:40:38 +00001968 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001969 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001970
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001971 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001972 if (isTailCall) {
1973 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001974 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001975 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001976
Duncan Sands739a0542008-07-02 17:40:58 +00001977 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001978 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001979 InFlag = Chain.getValue(1);
1980
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001981 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1982 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001983 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001984 InFlag = Chain.getValue(1);
1985
Bob Wilsona4c22902009-04-17 19:07:39 +00001986 // Handle result values, copying them out of physregs into vregs that we
1987 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001988 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001989 InVals, isThisReturn,
1990 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001991}
1992
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001993/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001994/// on the stack. Remember the next parameter register to allocate,
1995/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001996/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001997void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1998 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001999 assert((State->getCallOrPrologue() == Prologue ||
2000 State->getCallOrPrologue() == Call) &&
2001 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002002
Tim Northover8cda34f2015-03-11 18:54:22 +00002003 // Byval (as with any stack) slots are always at least 4 byte aligned.
2004 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002005
Tim Northover8cda34f2015-03-11 18:54:22 +00002006 unsigned Reg = State->AllocateReg(GPRArgRegs);
2007 if (!Reg)
2008 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002009
Tim Northover8cda34f2015-03-11 18:54:22 +00002010 unsigned AlignInRegs = Align / 4;
2011 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2012 for (unsigned i = 0; i < Waste; ++i)
2013 Reg = State->AllocateReg(GPRArgRegs);
2014
2015 if (!Reg)
2016 return;
2017
2018 unsigned Excess = 4 * (ARM::R4 - Reg);
2019
2020 // Special case when NSAA != SP and parameter size greater than size of
2021 // all remained GPR regs. In that case we can't split parameter, we must
2022 // send it to stack. We also must set NCRN to R4, so waste all
2023 // remained registers.
2024 const unsigned NSAAOffset = State->getNextStackOffset();
2025 if (NSAAOffset != 0 && Size > Excess) {
2026 while (State->AllocateReg(GPRArgRegs))
2027 ;
2028 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002029 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002030
2031 // First register for byval parameter is the first register that wasn't
2032 // allocated before this method call, so it would be "reg".
2033 // If parameter is small enough to be saved in range [reg, r4), then
2034 // the end (first after last) register would be reg + param-size-in-regs,
2035 // else parameter would be splitted between registers and stack,
2036 // end register would be r4 in this case.
2037 unsigned ByValRegBegin = Reg;
2038 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2039 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2040 // Note, first register is allocated in the beginning of function already,
2041 // allocate remained amount of registers we need.
2042 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2043 State->AllocateReg(GPRArgRegs);
2044 // A byval parameter that is split between registers and memory needs its
2045 // size truncated here.
2046 // In the case where the entire structure fits in registers, we set the
2047 // size in memory to zero.
2048 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002049}
2050
Dale Johannesend679ff72010-06-03 21:09:53 +00002051/// MatchingStackOffset - Return true if the given stack call argument is
2052/// already available in the same position (relatively) of the caller's
2053/// incoming argument stack.
2054static
2055bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2056 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002057 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002058 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2059 int FI = INT_MAX;
2060 if (Arg.getOpcode() == ISD::CopyFromReg) {
2061 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002062 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002063 return false;
2064 MachineInstr *Def = MRI->getVRegDef(VR);
2065 if (!Def)
2066 return false;
2067 if (!Flags.isByVal()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002068 if (!TII->isLoadFromStackSlot(*Def, FI))
Dale Johannesend679ff72010-06-03 21:09:53 +00002069 return false;
2070 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002071 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002072 }
2073 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2074 if (Flags.isByVal())
2075 // ByVal argument is passed in as a pointer but it's now being
2076 // dereferenced. e.g.
2077 // define @foo(%struct.X* %A) {
2078 // tail call @bar(%struct.X* byval %A)
2079 // }
2080 return false;
2081 SDValue Ptr = Ld->getBasePtr();
2082 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2083 if (!FINode)
2084 return false;
2085 FI = FINode->getIndex();
2086 } else
2087 return false;
2088
2089 assert(FI != INT_MAX);
2090 if (!MFI->isFixedObjectIndex(FI))
2091 return false;
2092 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2093}
2094
2095/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2096/// for tail call optimization. Targets which want to do tail call
2097/// optimization should implement this function.
2098bool
2099ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2100 CallingConv::ID CalleeCC,
2101 bool isVarArg,
2102 bool isCalleeStructRet,
2103 bool isCallerStructRet,
2104 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002105 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002106 const SmallVectorImpl<ISD::InputArg> &Ins,
2107 SelectionDAG& DAG) const {
Matthias Braun8d414362016-03-30 22:46:04 +00002108 MachineFunction &MF = DAG.getMachineFunction();
2109 const Function *CallerF = MF.getFunction();
Dale Johannesend679ff72010-06-03 21:09:53 +00002110 CallingConv::ID CallerCC = CallerF->getCallingConv();
Manman Ren4865d892016-03-18 23:41:51 +00002111
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002112 assert(Subtarget->supportsTailCall());
2113
Dale Johannesend679ff72010-06-03 21:09:53 +00002114 // Look for obvious safe cases to perform tail call optimization that do not
2115 // require ABI changes. This is what gcc calls sibcall.
2116
Jim Grosbache3864cc2010-06-16 23:45:49 +00002117 // Do not sibcall optimize vararg calls unless the call site is not passing
2118 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002119 if (isVarArg && !Outs.empty())
2120 return false;
2121
Tim Northoverd8407452013-10-01 14:33:28 +00002122 // Exception-handling functions need a special set of instructions to indicate
2123 // a return to the hardware. Tail-calling another function would probably
2124 // break this.
2125 if (CallerF->hasFnAttribute("interrupt"))
2126 return false;
2127
Dale Johannesend679ff72010-06-03 21:09:53 +00002128 // Also avoid sibcall optimization if either caller or callee uses struct
2129 // return semantics.
2130 if (isCalleeStructRet || isCallerStructRet)
2131 return false;
2132
Oliver Stannard12993dd2014-08-18 12:42:15 +00002133 // Externally-defined functions with weak linkage should not be
2134 // tail-called on ARM when the OS does not support dynamic
2135 // pre-emption of symbols, as the AAELF spec requires normal calls
2136 // to undefined weak functions to be replaced with a NOP or jump to the
2137 // next instruction. The behaviour of branch instructions in this
2138 // situation (as used for tail calls) is implementation-defined, so we
2139 // cannot rely on the linker replacing the tail call with a return.
2140 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2141 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002142 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002143 if (GV->hasExternalWeakLinkage() &&
2144 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002145 return false;
2146 }
2147
Matthias Braun8d414362016-03-30 22:46:04 +00002148 // Check that the call results are passed in the same way.
2149 LLVMContext &C = *DAG.getContext();
2150 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2151 CCAssignFnForNode(CalleeCC, true, isVarArg),
2152 CCAssignFnForNode(CallerCC, true, isVarArg)))
2153 return false;
Matthias Braun870c34f2016-04-04 18:56:13 +00002154 // The callee has to preserve all registers the caller needs to preserve.
Matthias Braun707e02c2016-04-13 21:43:25 +00002155 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2156 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
Matthias Braun870c34f2016-04-04 18:56:13 +00002157 if (CalleeCC != CallerCC) {
Matthias Braun707e02c2016-04-13 21:43:25 +00002158 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2159 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
Matthias Braun870c34f2016-04-04 18:56:13 +00002160 return false;
2161 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002162
Manman Ren7e48b252012-10-12 23:39:43 +00002163 // If Caller's vararg or byval argument has been split between registers and
2164 // stack, do not perform tail call, since part of the argument is in caller's
2165 // local frame.
Matthias Braun8d414362016-03-30 22:46:04 +00002166 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002167 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002168 return false;
2169
Dale Johannesend679ff72010-06-03 21:09:53 +00002170 // If the callee takes no arguments then go on to check the results of the
2171 // call.
2172 if (!Outs.empty()) {
2173 // Check if stack adjustment is needed. For now, do not do this if any
2174 // argument is passed on the stack.
2175 SmallVector<CCValAssign, 16> ArgLocs;
Matthias Braun8d414362016-03-30 22:46:04 +00002176 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002177 CCInfo.AnalyzeCallOperands(Outs,
2178 CCAssignFnForNode(CalleeCC, false, isVarArg));
2179 if (CCInfo.getNextStackOffset()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002180 // Check if the arguments are already laid out in the right way as
2181 // the caller's fixed stack objects.
2182 MachineFrameInfo *MFI = MF.getFrameInfo();
2183 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002184 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002185 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2186 i != e;
2187 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002188 CCValAssign &VA = ArgLocs[i];
2189 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002190 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002191 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002192 if (VA.getLocInfo() == CCValAssign::Indirect)
2193 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002194 if (VA.needsCustom()) {
2195 // f64 and vector types are split into multiple registers or
2196 // register/stack-slot combinations. The types will not match
2197 // the registers; give up on memory f64 refs until we figure
2198 // out what to do about this.
2199 if (!VA.isRegLoc())
2200 return false;
2201 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002202 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002203 if (RegVT == MVT::v2f64) {
2204 if (!ArgLocs[++i].isRegLoc())
2205 return false;
2206 if (!ArgLocs[++i].isRegLoc())
2207 return false;
2208 }
2209 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002210 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2211 MFI, MRI, TII))
2212 return false;
2213 }
2214 }
2215 }
Matthias Braun707e02c2016-04-13 21:43:25 +00002216
Matthias Braun46b0f032016-04-14 01:10:42 +00002217 const MachineRegisterInfo &MRI = MF.getRegInfo();
2218 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
2219 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002220 }
2221
2222 return true;
2223}
2224
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002225bool
2226ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2227 MachineFunction &MF, bool isVarArg,
2228 const SmallVectorImpl<ISD::OutputArg> &Outs,
2229 LLVMContext &Context) const {
2230 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002231 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002232 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2233 isVarArg));
2234}
2235
Tim Northoverd8407452013-10-01 14:33:28 +00002236static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002237 const SDLoc &DL, SelectionDAG &DAG) {
Tim Northoverd8407452013-10-01 14:33:28 +00002238 const MachineFunction &MF = DAG.getMachineFunction();
2239 const Function *F = MF.getFunction();
2240
2241 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2242
2243 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2244 // version of the "preferred return address". These offsets affect the return
2245 // instruction if this is a return from PL1 without hypervisor extensions.
2246 // IRQ/FIQ: +4 "subs pc, lr, #4"
2247 // SWI: 0 "subs pc, lr, #0"
2248 // ABORT: +4 "subs pc, lr, #4"
2249 // UNDEF: +4/+2 "subs pc, lr, #0"
2250 // UNDEF varies depending on where the exception came from ARM or Thumb
2251 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2252
2253 int64_t LROffset;
2254 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2255 IntKind == "ABORT")
2256 LROffset = 4;
2257 else if (IntKind == "SWI" || IntKind == "UNDEF")
2258 LROffset = 0;
2259 else
2260 report_fatal_error("Unsupported interrupt attribute. If present, value "
2261 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2262
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002263 RetOps.insert(RetOps.begin() + 1,
2264 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002265
Craig Topper48d114b2014-04-26 18:35:24 +00002266 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002267}
2268
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002269SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002270ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2271 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002272 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002273 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002274 const SDLoc &dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002275
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002276 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002277 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002278
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002279 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002280 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2281 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002282
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002283 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002284 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2285 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002286
Bob Wilsona4c22902009-04-17 19:07:39 +00002287 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002288 SmallVector<SDValue, 4> RetOps;
2289 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002290 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002291
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002292 MachineFunction &MF = DAG.getMachineFunction();
2293 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2294 AFI->setReturnRegsCount(RVLocs.size());
2295
Bob Wilsona4c22902009-04-17 19:07:39 +00002296 // Copy the result values into the output registers.
2297 for (unsigned i = 0, realRVLocIdx = 0;
2298 i != RVLocs.size();
2299 ++i, ++realRVLocIdx) {
2300 CCValAssign &VA = RVLocs[i];
2301 assert(VA.isRegLoc() && "Can only return in registers!");
2302
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002303 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002304
2305 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002306 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002307 case CCValAssign::Full: break;
2308 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002309 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002310 break;
2311 }
2312
Bob Wilsona4c22902009-04-17 19:07:39 +00002313 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002314 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002315 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002316 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002317 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002318 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002319 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002320
Christian Pirkerb5728192014-05-08 14:06:24 +00002321 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2322 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2323 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002324 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002325 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002326 VA = RVLocs[++i]; // skip ahead to next loc
2327 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002328 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2329 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002330 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002331 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002332 VA = RVLocs[++i]; // skip ahead to next loc
2333
2334 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002335 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002336 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002337 }
2338 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2339 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002340 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002341 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002342 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2343 fmrrd.getValue(isLittleEndian ? 0 : 1),
2344 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002345 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002346 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002347 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002348 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2349 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002350 Flag);
2351 } else
2352 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2353
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002354 // Guarantee that all emitted copies are
2355 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002356 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002357 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002358 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002359 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2360 const MCPhysReg *I =
2361 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2362 if (I) {
2363 for (; *I; ++I) {
2364 if (ARM::GPRRegClass.contains(*I))
2365 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2366 else if (ARM::DPRRegClass.contains(*I))
2367 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2368 else
2369 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2370 }
2371 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002372
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002373 // Update chain and glue.
2374 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002375 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002376 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002377
Tim Northoverd8407452013-10-01 14:33:28 +00002378 // CPUs which aren't M-class use a special sequence to return from
2379 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2380 // though we use "subs pc, lr, #N").
2381 //
2382 // M-class CPUs actually use a normal return sequence with a special
2383 // (hardware-provided) value in LR, so the normal code path works.
2384 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2385 !Subtarget->isMClass()) {
2386 if (Subtarget->isThumb1Only())
2387 report_fatal_error("interrupt attribute is not supported in Thumb1");
2388 return LowerInterruptReturn(RetOps, dl, DAG);
2389 }
2390
Craig Topper48d114b2014-04-26 18:35:24 +00002391 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002392}
2393
Evan Chengf8bad082012-04-10 01:51:00 +00002394bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002395 if (N->getNumValues() != 1)
2396 return false;
2397 if (!N->hasNUsesOfValue(1, 0))
2398 return false;
2399
Evan Chengf8bad082012-04-10 01:51:00 +00002400 SDValue TCChain = Chain;
2401 SDNode *Copy = *N->use_begin();
2402 if (Copy->getOpcode() == ISD::CopyToReg) {
2403 // If the copy has a glue operand, we conservatively assume it isn't safe to
2404 // perform a tail call.
2405 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2406 return false;
2407 TCChain = Copy->getOperand(0);
2408 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2409 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002410 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002411 SmallPtrSet<SDNode*, 2> Copies;
2412 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002413 UI != UE; ++UI) {
2414 if (UI->getOpcode() != ISD::CopyToReg)
2415 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002416 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002417 }
Evan Chengf8bad082012-04-10 01:51:00 +00002418 if (Copies.size() > 2)
2419 return false;
2420
2421 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2422 UI != UE; ++UI) {
2423 SDValue UseChain = UI->getOperand(0);
2424 if (Copies.count(UseChain.getNode()))
2425 // Second CopyToReg
2426 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002427 else {
2428 // We are at the top of this chain.
2429 // If the copy has a glue operand, we conservatively assume it
2430 // isn't safe to perform a tail call.
2431 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2432 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002433 // First CopyToReg
2434 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002435 }
Evan Chengf8bad082012-04-10 01:51:00 +00002436 }
2437 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002438 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002439 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002440 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002441 Copy = *Copy->use_begin();
2442 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002443 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002444 // If the copy has a glue operand, we conservatively assume it isn't safe to
2445 // perform a tail call.
2446 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2447 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002448 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002449 } else {
2450 return false;
2451 }
2452
Evan Cheng419ea282010-12-01 22:59:46 +00002453 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002454 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2455 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002456 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2457 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002458 return false;
2459 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002460 }
2461
Evan Chengf8bad082012-04-10 01:51:00 +00002462 if (!HasRet)
2463 return false;
2464
2465 Chain = TCChain;
2466 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002467}
2468
Evan Cheng0663f232011-03-21 01:19:09 +00002469bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002470 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002471 return false;
2472
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002473 auto Attr =
2474 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2475 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002476 return false;
2477
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002478 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002479}
2480
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002481// Trying to write a 64 bit value so need to split into two 32 bit values first,
2482// and pass the lower and high parts through.
2483static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2484 SDLoc DL(Op);
2485 SDValue WriteValue = Op->getOperand(2);
2486
2487 // This function is only supposed to be called for i64 type argument.
2488 assert(WriteValue.getValueType() == MVT::i64
2489 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2490
2491 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2492 DAG.getConstant(0, DL, MVT::i32));
2493 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2494 DAG.getConstant(1, DL, MVT::i32));
2495 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2496 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2497}
2498
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002499// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2500// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2501// one of the above mentioned nodes. It has to be wrapped because otherwise
2502// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2503// be used to form addressing mode. These wrapped nodes will be selected
2504// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002505static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002506 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002507 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002509 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002510 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002511 if (CP->isMachineConstantPoolEntry())
2512 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2513 CP->getAlignment());
2514 else
2515 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2516 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002517 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002518}
2519
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002520unsigned ARMTargetLowering::getJumpTableEncoding() const {
2521 return MachineJumpTableInfo::EK_Inline;
2522}
2523
Dan Gohman21cea8a2010-04-17 15:26:15 +00002524SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2525 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002526 MachineFunction &MF = DAG.getMachineFunction();
2527 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2528 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002529 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002530 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002531 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002532 SDValue CPAddr;
Rafael Espindola0f898332016-06-20 16:43:17 +00002533 bool IsPositionIndependent = isPositionIndependent();
2534 if (!IsPositionIndependent) {
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002535 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2536 } else {
2537 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002538 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002539 ARMConstantPoolValue *CPV =
2540 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2541 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002542 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2543 }
2544 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Justin Lebar9c375812016-07-15 18:27:10 +00002545 SDValue Result = DAG.getLoad(
2546 PtrVT, DL, DAG.getEntryNode(), CPAddr,
2547 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Rafael Espindola0f898332016-06-20 16:43:17 +00002548 if (!IsPositionIndependent)
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002549 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002550 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002551 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002552}
2553
Tim Northoverbd41cf82016-01-07 09:03:03 +00002554/// \brief Convert a TLS address reference into the correct sequence of loads
2555/// and calls to compute the variable's address for Darwin, and return an
2556/// SDValue containing the final node.
2557
2558/// Darwin only has one TLS scheme which must be capable of dealing with the
2559/// fully general situation, in the worst case. This means:
2560/// + "extern __thread" declaration.
2561/// + Defined in a possibly unknown dynamic library.
2562///
2563/// The general system is that each __thread variable has a [3 x i32] descriptor
2564/// which contains information used by the runtime to calculate the address. The
2565/// only part of this the compiler needs to know about is the first word, which
2566/// contains a function pointer that must be called with the address of the
2567/// entire descriptor in "r0".
2568///
2569/// Since this descriptor may be in a different unit, in general access must
2570/// proceed along the usual ARM rules. A common sequence to produce is:
2571///
2572/// movw rT1, :lower16:_var$non_lazy_ptr
2573/// movt rT1, :upper16:_var$non_lazy_ptr
2574/// ldr r0, [rT1]
2575/// ldr rT2, [r0]
2576/// blx rT2
2577/// [...address now in r0...]
2578SDValue
2579ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2580 SelectionDAG &DAG) const {
2581 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2582 SDLoc DL(Op);
2583
2584 // First step is to get the address of the actua global symbol. This is where
2585 // the TLS descriptor lives.
2586 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2587
2588 // The first entry in the descriptor is a function pointer that we must call
2589 // to obtain the address of the variable.
2590 SDValue Chain = DAG.getEntryNode();
2591 SDValue FuncTLVGet =
2592 DAG.getLoad(MVT::i32, DL, Chain, DescAddr,
2593 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Justin Lebar9c375812016-07-15 18:27:10 +00002594 /* Alignment = */ 4, MachineMemOperand::MONonTemporal |
2595 MachineMemOperand::MOInvariant);
Tim Northoverbd41cf82016-01-07 09:03:03 +00002596 Chain = FuncTLVGet.getValue(1);
2597
2598 MachineFunction &F = DAG.getMachineFunction();
2599 MachineFrameInfo *MFI = F.getFrameInfo();
2600 MFI->setAdjustsStack(true);
2601
2602 // TLS calls preserve all registers except those that absolutely must be
2603 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2604 // silly).
2605 auto TRI =
2606 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2607 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2608 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2609
2610 // Finally, we can make the call. This is just a degenerate version of a
2611 // normal AArch64 call node: r0 takes the address of the descriptor, and
2612 // returns the address of the variable in this thread.
2613 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2614 Chain =
2615 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2616 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2617 DAG.getRegisterMask(Mask), Chain.getValue(1));
2618 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2619}
2620
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002621SDValue
2622ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2623 SelectionDAG &DAG) const {
2624 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002625
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002626 SDValue Chain = DAG.getEntryNode();
2627 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2628 SDLoc DL(Op);
2629
2630 // Load the current TEB (thread environment block)
2631 SDValue Ops[] = {Chain,
2632 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2633 DAG.getConstant(15, DL, MVT::i32),
2634 DAG.getConstant(0, DL, MVT::i32),
2635 DAG.getConstant(13, DL, MVT::i32),
2636 DAG.getConstant(0, DL, MVT::i32),
2637 DAG.getConstant(2, DL, MVT::i32)};
2638 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2639 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2640
2641 SDValue TEB = CurrentTEB.getValue(0);
2642 Chain = CurrentTEB.getValue(1);
2643
2644 // Load the ThreadLocalStoragePointer from the TEB
2645 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2646 SDValue TLSArray =
2647 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002648 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002649
2650 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2651 // offset into the TLSArray.
2652
2653 // Load the TLS index from the C runtime
2654 SDValue TLSIndex =
2655 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2656 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
Justin Lebar9c375812016-07-15 18:27:10 +00002657 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002658
2659 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2660 DAG.getConstant(2, DL, MVT::i32));
2661 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2662 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
Justin Lebar9c375812016-07-15 18:27:10 +00002663 MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002664
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002665 // Get the offset of the start of the .tls section (section base)
2666 const auto *GA = cast<GlobalAddressSDNode>(Op);
2667 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL);
Justin Lebar9c375812016-07-15 18:27:10 +00002668 SDValue Offset = DAG.getLoad(
2669 PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32,
2670 DAG.getTargetConstantPool(CPV, PtrVT, 4)),
2671 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002672
2673 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset);
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002674}
2675
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002676// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002677SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002678ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002679 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002680 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002681 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002682 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002683 MachineFunction &MF = DAG.getMachineFunction();
2684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002685 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002686 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002687 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2688 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002689 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002690 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Justin Lebar9c375812016-07-15 18:27:10 +00002691 Argument = DAG.getLoad(
2692 PtrVT, dl, DAG.getEntryNode(), Argument,
2693 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002694 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002695
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002696 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002697 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002698
2699 // call __tls_get_addr.
2700 ArgListTy Args;
2701 ArgListEntry Entry;
2702 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002703 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002704 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002705
Dale Johannesen555a3752009-01-30 23:10:59 +00002706 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002707 TargetLowering::CallLoweringInfo CLI(DAG);
2708 CLI.setDebugLoc(dl).setChain(Chain)
2709 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002710 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002711
Justin Holewinskiaa583972012-05-25 16:35:28 +00002712 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002713 return CallResult.first;
2714}
2715
2716// Lower ISD::GlobalTLSAddress using the "initial exec" or
2717// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002718SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002719ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002720 SelectionDAG &DAG,
2721 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002722 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002723 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002724 SDValue Offset;
2725 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002726 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002727 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002728 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002729
Hans Wennborgaea41202012-05-04 09:40:39 +00002730 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002731 MachineFunction &MF = DAG.getMachineFunction();
2732 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002733 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002734 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002735 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2736 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002737 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2738 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2739 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002740 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002741 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002742 Offset = DAG.getLoad(
2743 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002744 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002745 Chain = Offset.getValue(1);
2746
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002747 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002748 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002749
Alex Lorenze40c8a22015-08-11 23:09:45 +00002750 Offset = DAG.getLoad(
2751 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002752 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002753 } else {
2754 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002755 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002756 ARMConstantPoolValue *CPV =
2757 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002758 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002759 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002760 Offset = DAG.getLoad(
2761 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002762 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002763 }
2764
2765 // The address of the thread local variable is the add of the thread
2766 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002767 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002768}
2769
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002770SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002771ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002772 if (Subtarget->isTargetDarwin())
2773 return LowerGlobalTLSAddressDarwin(Op, DAG);
2774
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002775 if (Subtarget->isTargetWindows())
2776 return LowerGlobalTLSAddressWindows(Op, DAG);
2777
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002778 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002779 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002780 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002781 if (DAG.getTarget().Options.EmulatedTLS)
2782 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002783
2784 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2785
2786 switch (model) {
2787 case TLSModel::GeneralDynamic:
2788 case TLSModel::LocalDynamic:
2789 return LowerToTLSGeneralDynamicModel(GA, DAG);
2790 case TLSModel::InitialExec:
2791 case TLSModel::LocalExec:
2792 return LowerToTLSExecModels(GA, DAG, model);
2793 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002794 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002795}
2796
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002797SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002798 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002799 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002800 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002801 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola7ad97b22016-05-31 15:31:55 +00002802 const TargetMachine &TM = getTargetMachine();
Rafael Espindola0f898332016-06-20 16:43:17 +00002803 if (isPositionIndependent()) {
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002804 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002805
2806 MachineFunction &MF = DAG.getMachineFunction();
2807 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2808 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2809 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2810 SDLoc dl(Op);
2811 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2812 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2813 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2814 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2815 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002816 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002817 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002818 SDValue Result = DAG.getLoad(
2819 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002820 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002821 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002822 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2823 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2824 if (UseGOT_PREL)
Justin Lebar9c375812016-07-15 18:27:10 +00002825 Result =
2826 DAG.getLoad(PtrVT, dl, Chain, Result,
2827 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002828 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002829 }
2830
2831 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002832 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002833 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002834 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002835 // FIXME: Once remat is capable of dealing with instructions with register
2836 // operands, expand this into two nodes.
2837 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2838 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002839 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002840 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2841 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002842 return DAG.getLoad(
2843 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002844 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002845 }
2846}
2847
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002848SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002849 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002850 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002851 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002852 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengdfce83c2011-01-17 08:03:18 +00002853
Eric Christopherc1058df2014-07-04 01:55:26 +00002854 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002855 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002856
Tim Northover72360d22013-12-02 10:35:41 +00002857 // FIXME: Once remat is capable of dealing with instructions with register
2858 // operands, expand this into multiple nodes
2859 unsigned Wrapper =
Rafael Espindola0f898332016-06-20 16:43:17 +00002860 isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002861
Tim Northover72360d22013-12-02 10:35:41 +00002862 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2863 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002864
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00002865 if (Subtarget->isGVIndirectSymbol(GV))
Tim Northover72360d22013-12-02 10:35:41 +00002866 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002867 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Evan Cheng10043e22007-01-19 07:51:42 +00002868 return Result;
2869}
2870
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002871SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2872 SelectionDAG &DAG) const {
2873 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002874 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2875 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002876
2877 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002878 const ARMII::TOF TargetFlags =
2879 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002880 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002881 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002882 SDLoc DL(Op);
2883
2884 ++NumMovwMovt;
2885
2886 // FIXME: Once remat is capable of dealing with instructions with register
2887 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002888 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2889 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002890 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002891 if (GV->hasDLLImportStorageClass())
2892 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002893 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002894 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002895}
2896
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002897SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002898ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002899 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002900 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002901 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2902 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002903 Op.getOperand(1), Val);
2904}
2905
2906SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002907ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002908 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002909 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002910 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002911}
2912
Matthias Braun3cd00c12015-07-16 22:34:16 +00002913SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2914 SelectionDAG &DAG) const {
2915 SDLoc dl(Op);
2916 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2917 Op.getOperand(0));
2918}
2919
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002920SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002921ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002922 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002923 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002924 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002925 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002926 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002927 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002928 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002929 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002930 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002931 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00002932 case Intrinsic::thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002933 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002934 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2935 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002936 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002937 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002938 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002939 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002940 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002941 SDValue CPAddr;
Rafael Espindola0f898332016-06-20 16:43:17 +00002942 bool IsPositionIndependent = isPositionIndependent();
2943 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
Jim Grosbach693e36a2009-08-11 00:09:57 +00002944 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002945 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2946 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002947 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002948 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002949 SDValue Result = DAG.getLoad(
2950 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002951 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach693e36a2009-08-11 00:09:57 +00002952
Rafael Espindola0f898332016-06-20 16:43:17 +00002953 if (IsPositionIndependent) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002954 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002955 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2956 }
2957 return Result;
2958 }
Evan Cheng18381b42011-03-29 23:06:19 +00002959 case Intrinsic::arm_neon_vmulls:
2960 case Intrinsic::arm_neon_vmullu: {
2961 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2962 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002963 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002964 Op.getOperand(1), Op.getOperand(2));
2965 }
James Molloyee868b22015-08-11 12:06:25 +00002966 case Intrinsic::arm_neon_vminnm:
2967 case Intrinsic::arm_neon_vmaxnm: {
2968 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2969 ? ISD::FMINNUM : ISD::FMAXNUM;
2970 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2971 Op.getOperand(1), Op.getOperand(2));
2972 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002973 case Intrinsic::arm_neon_vminu:
2974 case Intrinsic::arm_neon_vmaxu: {
2975 if (Op.getValueType().isFloatingPoint())
2976 return SDValue();
2977 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2978 ? ISD::UMIN : ISD::UMAX;
2979 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2980 Op.getOperand(1), Op.getOperand(2));
2981 }
James Molloyd616c642015-08-11 12:06:28 +00002982 case Intrinsic::arm_neon_vmins:
2983 case Intrinsic::arm_neon_vmaxs: {
2984 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002985 if (!Op.getValueType().isFloatingPoint()) {
2986 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2987 ? ISD::SMIN : ISD::SMAX;
2988 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2989 Op.getOperand(1), Op.getOperand(2));
2990 }
James Molloyd616c642015-08-11 12:06:28 +00002991 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2992 ? ISD::FMINNAN : ISD::FMAXNAN;
2993 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2994 Op.getOperand(1), Op.getOperand(2));
2995 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002996 }
2997}
2998
Eli Friedman30a49e92011-08-03 21:06:02 +00002999static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3000 const ARMSubtarget *Subtarget) {
3001 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003002 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003003 if (!Subtarget->hasDataBarrier()) {
3004 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3005 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3006 // here.
3007 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003008 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003009 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003010 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003011 }
3012
Tim Northover36b24172013-07-03 09:20:36 +00003013 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3014 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003015 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003016 if (Subtarget->isMClass()) {
3017 // Only a full system barrier exists in the M-class architectures.
3018 Domain = ARM_MB::SY;
Diana Picusc5baa432016-06-23 07:47:35 +00003019 } else if (Subtarget->preferISHSTBarriers() &&
3020 Ord == AtomicOrdering::Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003021 // Swift happens to implement ISHST barriers in a way that's compatible with
3022 // Release semantics but weaker than ISH so we'd be fools not to use
3023 // it. Beware: other processors probably don't!
3024 Domain = ARM_MB::ISHST;
3025 }
3026
Joey Gouly926d3f52013-09-05 15:35:24 +00003027 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003028 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3029 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003030}
3031
Evan Cheng8740ee32010-11-03 06:34:55 +00003032static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3033 const ARMSubtarget *Subtarget) {
3034 // ARM pre v5TE and Thumb1 does not have preload instructions.
3035 if (!(Subtarget->isThumb2() ||
3036 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3037 // Just preserve the chain.
3038 return Op.getOperand(0);
3039
Andrew Trickef9de2a2013-05-25 02:42:55 +00003040 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003041 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3042 if (!isRead &&
3043 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3044 // ARMv7 with MP extension has PLDW.
3045 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003046
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003047 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3048 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003049 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003050 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003051 isData = ~isData & 1;
3052 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003053
3054 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003055 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3056 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003057}
3058
Dan Gohman31ae5862010-04-17 14:41:14 +00003059static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3060 MachineFunction &MF = DAG.getMachineFunction();
3061 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3062
Evan Cheng10043e22007-01-19 07:51:42 +00003063 // vastart just stores the address of the VarArgsFrameIndex slot into the
3064 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003065 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003066 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003067 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003068 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003069 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00003070 MachinePointerInfo(SV));
Evan Cheng10043e22007-01-19 07:51:42 +00003071}
3072
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003073SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA,
3074 CCValAssign &NextVA,
3075 SDValue &Root,
3076 SelectionDAG &DAG,
3077 const SDLoc &dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003078 MachineFunction &MF = DAG.getMachineFunction();
3079 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3080
Craig Topper760b1342012-02-22 05:59:10 +00003081 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003082 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003083 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003084 else
Craig Topperc7242e02012-04-20 07:30:17 +00003085 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003086
3087 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003088 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003089 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003090
3091 SDValue ArgValue2;
3092 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003093 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00003094 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003095
3096 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003097 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003098 ArgValue2 = DAG.getLoad(
3099 MVT::i32, dl, Root, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003100 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Bob Wilson2e076c42009-06-22 23:27:02 +00003101 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003102 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003103 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003104 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003105 if (!Subtarget->isLittle())
3106 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003107 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003108}
3109
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003110// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003111// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003112// byval). Either way, we allocate stack slots adjacent to the data
3113// provided by our caller, and store the unallocated registers there.
3114// If this is a variadic function, the va_list pointer will begin with
3115// these values; otherwise, this reassembles a (byval) structure that
3116// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003117// Return: The frame index registers were stored into.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003118int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
3119 const SDLoc &dl, SDValue &Chain,
3120 const Value *OrigArg,
3121 unsigned InRegsParamRecordIdx,
3122 int ArgOffset, unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003123 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003124 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003125 // Setup first unallocated register as first byval register;
3126 // eat all remained registers
3127 // (these two actions are performed by HandleByVal method).
3128 // Then, here, we initialize stack frame with
3129 // "store-reg" instructions.
3130 // Case #2. Var-args function, that doesn't contain byval parameters.
3131 // The same: eat all remained unallocated registers,
3132 // initialize stack frame.
3133
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003134 MachineFunction &MF = DAG.getMachineFunction();
3135 MachineFrameInfo *MFI = MF.getFrameInfo();
3136 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003137 unsigned RBegin, REnd;
3138 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3139 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003140 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003141 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003142 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003143 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003144 }
3145
Tim Northover8cda34f2015-03-11 18:54:22 +00003146 if (REnd != RBegin)
3147 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003148
Mehdi Amini44ede332015-07-09 02:09:04 +00003149 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003150 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003151 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003152
Tim Northover8cda34f2015-03-11 18:54:22 +00003153 SmallVector<SDValue, 4> MemOps;
3154 const TargetRegisterClass *RC =
3155 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003156
Tim Northover8cda34f2015-03-11 18:54:22 +00003157 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3158 unsigned VReg = MF.addLiveIn(Reg, RC);
3159 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +00003160 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3161 MachinePointerInfo(OrigArg, 4 * i));
Tim Northover8cda34f2015-03-11 18:54:22 +00003162 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003163 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003164 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003165
3166 if (!MemOps.empty())
3167 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3168 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003169}
3170
3171// Setup stack frame, the va_list pointer will start from.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003172void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
3173 const SDLoc &dl, SDValue &Chain,
3174 unsigned ArgOffset,
3175 unsigned TotalArgRegsSaveSize,
3176 bool ForceMutable) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003177 MachineFunction &MF = DAG.getMachineFunction();
3178 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3179
3180 // Try to store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003181 // to their spots on the stack so that they may be loaded by dereferencing
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003182 // the result of va_next.
3183 // If there is no regs to be stored, just point address after last
3184 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003185 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3186 CCInfo.getInRegsParamsCount(),
3187 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003188 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003189}
3190
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003191SDValue ARMTargetLowering::LowerFormalArguments(
3192 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3193 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3194 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003195 MachineFunction &MF = DAG.getMachineFunction();
3196 MachineFrameInfo *MFI = MF.getFrameInfo();
3197
Bob Wilsona4c22902009-04-17 19:07:39 +00003198 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3199
3200 // Assign locations to all of the incoming arguments.
3201 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003202 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3203 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003204 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003205 CCAssignFnForNode(CallConv, /* Return*/ false,
3206 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003207
Bob Wilsona4c22902009-04-17 19:07:39 +00003208 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003209 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003210 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3211 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003212
3213 // Initially ArgRegsSaveSize is zero.
3214 // Then we increase this value each time we meet byval parameter.
3215 // We also increase this value in case of varargs function.
3216 AFI->setArgRegsSaveSize(0);
3217
Oliver Stannardd55e1152014-03-05 15:25:27 +00003218 // Calculate the amount of stack space that we need to allocate to store
3219 // byval and variadic arguments that are passed in registers.
3220 // We need to know this before we allocate the first byval or variadic
3221 // argument, as they will be allocated a stack slot below the CFA (Canonical
3222 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003223 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003224 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003225 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3226 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003227
Tim Northover8cda34f2015-03-11 18:54:22 +00003228 CCValAssign &VA = ArgLocs[i];
3229 unsigned Index = VA.getValNo();
3230 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3231 if (!Flags.isByVal())
3232 continue;
3233
3234 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3235 unsigned RBegin, REnd;
3236 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3237 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3238
3239 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003240 }
3241 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003242
3243 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003244 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003245 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3246 if (RegIdx != array_lengthof(GPRArgRegs))
3247 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003248 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003249
3250 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3251 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003252 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003253
Bob Wilsona4c22902009-04-17 19:07:39 +00003254 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3255 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003256 if (Ins[VA.getValNo()].isOrigArg()) {
3257 std::advance(CurOrigArg,
3258 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3259 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3260 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003261 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003262 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003263 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003264
Bob Wilsona4c22902009-04-17 19:07:39 +00003265 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003266 // f64 and vector types are split up into multiple registers or
3267 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003268 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003269 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003270 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003271 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003272 SDValue ArgValue2;
3273 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003274 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003275 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003276 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
3277 MachinePointerInfo::getFixedStack(
3278 DAG.getMachineFunction(), FI));
Bob Wilson699bdf72010-04-13 22:03:22 +00003279 } else {
3280 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3281 Chain, DAG, dl);
3282 }
Owen Anderson9f944592009-08-11 20:47:22 +00003283 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3284 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003285 ArgValue, ArgValue1,
3286 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003287 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003288 ArgValue, ArgValue2,
3289 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003290 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003291 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003292
Bob Wilson2e076c42009-06-22 23:27:02 +00003293 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003294 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003295
Owen Anderson9f944592009-08-11 20:47:22 +00003296 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003297 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003298 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003299 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003300 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003301 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003302 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003303 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3304 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003305 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003306 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003307
3308 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003309 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003310 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003311 }
3312
3313 // If this is an 8 or 16-bit value, it is really passed promoted
3314 // to 32 bits. Insert an assert[sz]ext to capture this, then
3315 // truncate to the right size.
3316 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003317 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003318 case CCValAssign::Full: break;
3319 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003320 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003321 break;
3322 case CCValAssign::SExt:
3323 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3324 DAG.getValueType(VA.getValVT()));
3325 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3326 break;
3327 case CCValAssign::ZExt:
3328 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3329 DAG.getValueType(VA.getValVT()));
3330 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3331 break;
3332 }
3333
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003334 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003335
3336 } else { // VA.isRegLoc()
3337
3338 // sanity check
3339 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003340 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003341
Andrew Trick05938a52015-02-16 18:10:47 +00003342 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003343
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003344 // Some Ins[] entries become multiple ArgLoc[] entries.
3345 // Process them only once.
3346 if (index != lastInsIndex)
3347 {
3348 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003349 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003350 // This can be changed with more analysis.
3351 // In case of tail call optimization mark all arguments mutable.
3352 // Since they could be overwritten by lowering of arguments in case of
3353 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003354 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003355 assert(Ins[index].isOrigArg() &&
3356 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003357 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003358
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003359 int FrameIndex = StoreByValRegs(
3360 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3361 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003362 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003363 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003364 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003365 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003366 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003367 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003368
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003369 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003370 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003371 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3372 MachinePointerInfo::getFixedStack(
3373 DAG.getMachineFunction(), FI)));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003374 }
3375 lastInsIndex = index;
3376 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003377 }
3378 }
3379
3380 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003381 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003382 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003383 CCInfo.getNextStackOffset(),
3384 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003385
Oliver Stannardb14c6252014-04-02 16:10:33 +00003386 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3387
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003388 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003389}
3390
3391/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003392static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003393 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003394 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003395 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003396 // Maybe this has already been legalized into the constant pool?
3397 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003398 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003399 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003400 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003401 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003402 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003403 } else if (Op->getOpcode() == ISD::BITCAST &&
3404 Op->getValueType(0) == MVT::f64) {
3405 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3406 // created by LowerConstantFP().
3407 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003408 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3409 isNullConstant(BitcastOp->getOperand(0)))
3410 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003411 }
3412 return false;
3413}
3414
Evan Cheng10043e22007-01-19 07:51:42 +00003415/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3416/// the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003417SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3418 SDValue &ARMcc, SelectionDAG &DAG,
3419 const SDLoc &dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003420 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003421 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003422 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003423 // Constant does not fit, try adjusting it by one?
3424 switch (CC) {
3425 default: break;
3426 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003427 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003428 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003429 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003430 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003431 }
3432 break;
3433 case ISD::SETULT:
3434 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003435 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003436 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003437 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003438 }
3439 break;
3440 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003441 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003442 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003443 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003444 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003445 }
3446 break;
3447 case ISD::SETULE:
3448 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003449 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003450 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003451 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003452 }
3453 break;
3454 }
3455 }
3456 }
3457
3458 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003459 ARMISD::NodeType CompareType;
3460 switch (CondCode) {
3461 default:
3462 CompareType = ARMISD::CMP;
3463 break;
3464 case ARMCC::EQ:
3465 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003466 // Uses only Z Flag
3467 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003468 break;
3469 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003470 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003471 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003472}
3473
3474/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003475SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS,
3476 SelectionDAG &DAG, const SDLoc &dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003477 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003478 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003479 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003480 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003481 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003482 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3483 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003484}
3485
Bob Wilson45acbd02011-03-08 01:17:20 +00003486/// duplicateCmp - Glue values can have only one use, so this function
3487/// duplicates a comparison node.
3488SDValue
3489ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3490 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003491 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003492 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3493 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3494
3495 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3496 Cmp = Cmp.getOperand(0);
3497 Opc = Cmp.getOpcode();
3498 if (Opc == ARMISD::CMPFP)
3499 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3500 else {
3501 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3502 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3503 }
3504 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3505}
3506
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003507std::pair<SDValue, SDValue>
3508ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3509 SDValue &ARMcc) const {
3510 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3511
3512 SDValue Value, OverflowCmp;
3513 SDValue LHS = Op.getOperand(0);
3514 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003515 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003516
3517 // FIXME: We are currently always generating CMPs because we don't support
3518 // generating CMN through the backend. This is not as good as the natural
3519 // CMP case because it causes a register dependency and cannot be folded
3520 // later.
3521
3522 switch (Op.getOpcode()) {
3523 default:
3524 llvm_unreachable("Unknown overflow instruction!");
3525 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003526 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3527 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3528 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003529 break;
3530 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003531 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3532 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3533 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003534 break;
3535 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003536 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3537 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3538 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003539 break;
3540 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003541 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3542 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3543 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003544 break;
3545 } // switch (...)
3546
3547 return std::make_pair(Value, OverflowCmp);
3548}
3549
3550
3551SDValue
3552ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3553 // Let legalize expand this if it isn't a legal type yet.
3554 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3555 return SDValue();
3556
3557 SDValue Value, OverflowCmp;
3558 SDValue ARMcc;
3559 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3560 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003561 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003562 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003563 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3564 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003565 EVT VT = Op.getValueType();
3566
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003567 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003568 ARMcc, CCR, OverflowCmp);
3569
3570 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003571 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003572}
3573
3574
Bill Wendling6a981312010-08-11 08:43:16 +00003575SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3576 SDValue Cond = Op.getOperand(0);
3577 SDValue SelectTrue = Op.getOperand(1);
3578 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003579 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003580 unsigned Opc = Cond.getOpcode();
3581
3582 if (Cond.getResNo() == 1 &&
3583 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3584 Opc == ISD::USUBO)) {
3585 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3586 return SDValue();
3587
3588 SDValue Value, OverflowCmp;
3589 SDValue ARMcc;
3590 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3591 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3592 EVT VT = Op.getValueType();
3593
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003594 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003595 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003596 }
Bill Wendling6a981312010-08-11 08:43:16 +00003597
3598 // Convert:
3599 //
3600 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3601 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3602 //
3603 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3604 const ConstantSDNode *CMOVTrue =
3605 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3606 const ConstantSDNode *CMOVFalse =
3607 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3608
3609 if (CMOVTrue && CMOVFalse) {
3610 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3611 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3612
3613 SDValue True;
3614 SDValue False;
3615 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3616 True = SelectTrue;
3617 False = SelectFalse;
3618 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3619 True = SelectFalse;
3620 False = SelectTrue;
3621 }
3622
3623 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003624 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003625 SDValue ARMcc = Cond.getOperand(2);
3626 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003627 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003628 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003629 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003630 }
3631 }
3632 }
3633
Dan Gohmand4a77c42012-02-24 00:09:36 +00003634 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3635 // undefined bits before doing a full-word comparison with zero.
3636 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003637 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003638
Bill Wendling6a981312010-08-11 08:43:16 +00003639 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003640 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003641 SelectTrue, SelectFalse, ISD::SETNE);
3642}
3643
Joey Gouly881eab52013-08-22 15:29:11 +00003644static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3645 bool &swpCmpOps, bool &swpVselOps) {
3646 // Start by selecting the GE condition code for opcodes that return true for
3647 // 'equality'
3648 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3649 CC == ISD::SETULE)
3650 CondCode = ARMCC::GE;
3651
3652 // and GT for opcodes that return false for 'equality'.
3653 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3654 CC == ISD::SETULT)
3655 CondCode = ARMCC::GT;
3656
3657 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3658 // to swap the compare operands.
3659 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3660 CC == ISD::SETULT)
3661 swpCmpOps = true;
3662
3663 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3664 // If we have an unordered opcode, we need to swap the operands to the VSEL
3665 // instruction (effectively negating the condition).
3666 //
3667 // This also has the effect of swapping which one of 'less' or 'greater'
3668 // returns true, so we also swap the compare operands. It also switches
3669 // whether we return true for 'equality', so we compensate by picking the
3670 // opposite condition code to our original choice.
3671 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3672 CC == ISD::SETUGT) {
3673 swpCmpOps = !swpCmpOps;
3674 swpVselOps = !swpVselOps;
3675 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3676 }
3677
3678 // 'ordered' is 'anything but unordered', so use the VS condition code and
3679 // swap the VSEL operands.
3680 if (CC == ISD::SETO) {
3681 CondCode = ARMCC::VS;
3682 swpVselOps = true;
3683 }
3684
3685 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3686 // code and swap the VSEL operands.
3687 if (CC == ISD::SETUNE) {
3688 CondCode = ARMCC::EQ;
3689 swpVselOps = true;
3690 }
3691}
3692
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003693SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003694 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3695 SDValue Cmp, SelectionDAG &DAG) const {
3696 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3697 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3698 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3699 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3700 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3701
3702 SDValue TrueLow = TrueVal.getValue(0);
3703 SDValue TrueHigh = TrueVal.getValue(1);
3704 SDValue FalseLow = FalseVal.getValue(0);
3705 SDValue FalseHigh = FalseVal.getValue(1);
3706
3707 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3708 ARMcc, CCR, Cmp);
3709 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3710 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3711
3712 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3713 } else {
3714 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3715 Cmp);
3716 }
3717}
3718
Benjamin Kramer4d098922016-07-10 11:28:51 +00003719static bool isGTorGE(ISD::CondCode CC) {
3720 return CC == ISD::SETGT || CC == ISD::SETGE;
3721}
Pablo Barrio7a643462016-06-23 16:53:49 +00003722
Benjamin Kramer4d098922016-07-10 11:28:51 +00003723static bool isLTorLE(ISD::CondCode CC) {
3724 return CC == ISD::SETLT || CC == ISD::SETLE;
3725}
Pablo Barrio7a643462016-06-23 16:53:49 +00003726
3727// See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating.
3728// All of these conditions (and their <= and >= counterparts) will do:
3729// x < k ? k : x
3730// x > k ? x : k
3731// k < x ? x : k
3732// k > x ? k : x
Benjamin Kramer4d098922016-07-10 11:28:51 +00003733static bool isLowerSaturate(const SDValue LHS, const SDValue RHS,
3734 const SDValue TrueVal, const SDValue FalseVal,
3735 const ISD::CondCode CC, const SDValue K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00003736 return (isGTorGE(CC) &&
3737 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) ||
3738 (isLTorLE(CC) &&
3739 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal)));
3740}
3741
3742// Similar to isLowerSaturate(), but checks for upper-saturating conditions.
Benjamin Kramer4d098922016-07-10 11:28:51 +00003743static bool isUpperSaturate(const SDValue LHS, const SDValue RHS,
3744 const SDValue TrueVal, const SDValue FalseVal,
3745 const ISD::CondCode CC, const SDValue K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00003746 return (isGTorGE(CC) &&
3747 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) ||
3748 (isLTorLE(CC) &&
3749 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal)));
3750}
3751
3752// Check if two chained conditionals could be converted into SSAT.
3753//
3754// SSAT can replace a set of two conditional selectors that bound a number to an
3755// interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples:
3756//
3757// x < -k ? -k : (x > k ? k : x)
3758// x < -k ? -k : (x < k ? x : k)
3759// x > -k ? (x > k ? k : x) : -k
3760// x < k ? (x < -k ? -k : x) : k
3761// etc.
3762//
3763// It returns true if the conversion can be done, false otherwise.
3764// Additionally, the variable is returned in parameter V and the constant in K.
Benjamin Kramer4d098922016-07-10 11:28:51 +00003765static bool isSaturatingConditional(const SDValue &Op, SDValue &V,
3766 uint64_t &K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00003767
3768 SDValue LHS1 = Op.getOperand(0);
3769 SDValue RHS1 = Op.getOperand(1);
3770 SDValue TrueVal1 = Op.getOperand(2);
3771 SDValue FalseVal1 = Op.getOperand(3);
3772 ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get();
3773
3774 const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1;
3775 if (Op2.getOpcode() != ISD::SELECT_CC)
3776 return false;
3777
3778 SDValue LHS2 = Op2.getOperand(0);
3779 SDValue RHS2 = Op2.getOperand(1);
3780 SDValue TrueVal2 = Op2.getOperand(2);
3781 SDValue FalseVal2 = Op2.getOperand(3);
3782 ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get();
3783
3784 // Find out which are the constants and which are the variables
3785 // in each conditional
3786 SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1)
3787 ? &RHS1
3788 : NULL;
3789 SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2)
3790 ? &RHS2
3791 : NULL;
3792 SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2;
3793 SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1;
3794 SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2;
3795 SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2;
3796
3797 // We must detect cases where the original operations worked with 16- or
3798 // 8-bit values. In such case, V2Tmp != V2 because the comparison operations
3799 // must work with sign-extended values but the select operations return
3800 // the original non-extended value.
3801 SDValue V2TmpReg = V2Tmp;
3802 if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG)
3803 V2TmpReg = V2Tmp->getOperand(0);
3804
3805 // Check that the registers and the constants have the correct values
3806 // in both conditionals
3807 if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp ||
3808 V2TmpReg != V2)
3809 return false;
3810
3811 // Figure out which conditional is saturating the lower/upper bound.
3812 const SDValue *LowerCheckOp =
3813 isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
3814 ? &Op
3815 : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) ? &Op2
3816 : NULL;
3817 const SDValue *UpperCheckOp =
3818 isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
3819 ? &Op
3820 : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) ? &Op2
3821 : NULL;
3822
3823 if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp)
3824 return false;
3825
3826 // Check that the constant in the lower-bound check is
3827 // the opposite of the constant in the upper-bound check
3828 // in 1's complement.
3829 int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue();
3830 int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue();
3831 int64_t PosVal = std::max(Val1, Val2);
3832
3833 if (((Val1 > Val2 && UpperCheckOp == &Op) ||
3834 (Val1 < Val2 && UpperCheckOp == &Op2)) &&
3835 Val1 == ~Val2 && isPowerOf2_64(PosVal + 1)) {
3836
3837 V = V2;
3838 K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive
3839 return true;
3840 }
3841
3842 return false;
3843}
3844
Dan Gohman21cea8a2010-04-17 15:26:15 +00003845SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Pablo Barrio7a643462016-06-23 16:53:49 +00003846
Owen Anderson53aa7a92009-08-10 22:56:29 +00003847 EVT VT = Op.getValueType();
Pablo Barrio7a643462016-06-23 16:53:49 +00003848 SDLoc dl(Op);
3849
3850 // Try to convert two saturating conditional selects into a single SSAT
3851 SDValue SatValue;
3852 uint64_t SatConstant;
Renato Golin32b165f2016-07-25 22:25:25 +00003853 if (Subtarget->hasDSP() &&
3854 isSaturatingConditional(Op, SatValue, SatConstant))
Pablo Barrio7a643462016-06-23 16:53:49 +00003855 return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue,
3856 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
3857
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003858 SDValue LHS = Op.getOperand(0);
3859 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003860 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003861 SDValue TrueVal = Op.getOperand(2);
3862 SDValue FalseVal = Op.getOperand(3);
Evan Cheng10043e22007-01-19 07:51:42 +00003863
Oliver Stannard51b1d462014-08-21 12:50:31 +00003864 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3865 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3866 dl);
3867
3868 // If softenSetCCOperands only returned one value, we should compare it to
3869 // zero.
3870 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003871 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003872 CC = ISD::SETNE;
3873 }
3874 }
3875
Owen Anderson9f944592009-08-11 20:47:22 +00003876 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003877 // Try to generate VSEL on ARMv8.
3878 // The VSEL instruction can't use all the usual ARM condition
3879 // codes: it only has two bits to select the condition code, so it's
3880 // constrained to use only GE, GT, VS and EQ.
3881 //
3882 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3883 // swap the operands of the previous compare instruction (effectively
3884 // inverting the compare condition, swapping 'less' and 'greater') and
3885 // sometimes need to swap the operands to the VSEL (which inverts the
3886 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003887 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3888 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003889 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3890 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3891 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003892 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003893 std::swap(TrueVal, FalseVal);
3894 }
3895 }
3896
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003897 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003898 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003899 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003900 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003901 }
3902
3903 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003904 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003905
Scott Douglass7ad77922015-04-08 17:18:28 +00003906 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003907 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3908 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003909 bool swpCmpOps = false;
3910 bool swpVselOps = false;
3911 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3912
3913 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3914 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3915 if (swpCmpOps)
3916 std::swap(LHS, RHS);
3917 if (swpVselOps)
3918 std::swap(TrueVal, FalseVal);
3919 }
3920 }
3921
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003922 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003923 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003924 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003925 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003926 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003927 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003928 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003929 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003930 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003931 }
3932 return Result;
3933}
3934
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003935/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3936/// to morph to an integer compare sequence.
3937static bool canChangeToInt(SDValue Op, bool &SeenZero,
3938 const ARMSubtarget *Subtarget) {
3939 SDNode *N = Op.getNode();
3940 if (!N->hasOneUse())
3941 // Otherwise it requires moving the value from fp to integer registers.
3942 return false;
3943 if (!N->getNumValues())
3944 return false;
3945 EVT VT = Op.getValueType();
3946 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3947 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3948 // vmrs are very slow, e.g. cortex-a8.
3949 return false;
3950
3951 if (isFloatingPointZero(Op)) {
3952 SeenZero = true;
3953 return true;
3954 }
3955 return ISD::isNormalLoad(N);
3956}
3957
3958static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3959 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003960 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003961
3962 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Justin Lebar9c375812016-07-15 18:27:10 +00003963 return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(),
3964 Ld->getPointerInfo(), Ld->getAlignment(),
3965 Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003966
3967 llvm_unreachable("Unknown VFP cmp argument!");
3968}
3969
3970static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3971 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003972 SDLoc dl(Op);
3973
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003974 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003975 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3976 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003977 return;
3978 }
3979
3980 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3981 SDValue Ptr = Ld->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +00003982 RetVal1 =
3983 DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(),
3984 Ld->getAlignment(), Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003985
3986 EVT PtrType = Ptr.getValueType();
3987 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003988 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3989 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
Justin Lebar9c375812016-07-15 18:27:10 +00003990 RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr,
3991 Ld->getPointerInfo().getWithOffset(4), NewAlign,
3992 Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003993 return;
3994 }
3995
3996 llvm_unreachable("Unknown VFP cmp argument!");
3997}
3998
3999/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
4000/// f32 and even f64 comparisons to integer ones.
4001SDValue
4002ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
4003 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00004004 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004005 SDValue LHS = Op.getOperand(2);
4006 SDValue RHS = Op.getOperand(3);
4007 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004008 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004009
Evan Chengd12af5d2012-03-01 23:27:13 +00004010 bool LHSSeenZero = false;
4011 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
4012 bool RHSSeenZero = false;
4013 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
4014 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00004015 // If unsafe fp math optimization is enabled and there are no other uses of
4016 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004017 // to an integer comparison.
4018 if (CC == ISD::SETOEQ)
4019 CC = ISD::SETEQ;
4020 else if (CC == ISD::SETUNE)
4021 CC = ISD::SETNE;
4022
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004023 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004024 SDValue ARMcc;
4025 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00004026 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4027 bitcastf32Toi32(LHS, DAG), Mask);
4028 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4029 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004030 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
4031 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4032 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
4033 Chain, Dest, ARMcc, CCR, Cmp);
4034 }
4035
4036 SDValue LHS1, LHS2;
4037 SDValue RHS1, RHS2;
4038 expandf64Toi32(LHS, DAG, LHS1, LHS2);
4039 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00004040 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
4041 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004042 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004043 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004044 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004045 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00004046 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004047 }
4048
4049 return SDValue();
4050}
4051
4052SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
4053 SDValue Chain = Op.getOperand(0);
4054 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
4055 SDValue LHS = Op.getOperand(2);
4056 SDValue RHS = Op.getOperand(3);
4057 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004058 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004059
Oliver Stannard51b1d462014-08-21 12:50:31 +00004060 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4061 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4062 dl);
4063
4064 // If softenSetCCOperands only returned one value, we should compare it to
4065 // zero.
4066 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004067 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00004068 CC = ISD::SETNE;
4069 }
4070 }
4071
Owen Anderson9f944592009-08-11 20:47:22 +00004072 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004073 SDValue ARMcc;
4074 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004075 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00004076 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004077 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00004078 }
4079
Owen Anderson9f944592009-08-11 20:47:22 +00004080 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004081
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004082 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004083 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
4084 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004085 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004086 return Result;
4087 }
4088
Evan Cheng10043e22007-01-19 07:51:42 +00004089 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00004090 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00004091
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004092 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004093 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004094 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004095 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004096 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00004097 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00004098 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004099 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004100 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00004101 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00004102 }
4103 return Res;
4104}
4105
Dan Gohman21cea8a2010-04-17 15:26:15 +00004106SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004107 SDValue Chain = Op.getOperand(0);
4108 SDValue Table = Op.getOperand(1);
4109 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004110 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004111
Mehdi Amini44ede332015-07-09 02:09:04 +00004112 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00004113 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004114 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00004115 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004116 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00004117 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004118 if (Subtarget->isThumb2()) {
4119 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4120 // which does another jump to the destination. This also makes it easier
4121 // to translate it to TBB / TBH later.
4122 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004123 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004124 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004125 }
Rafael Espindola0f898332016-06-20 16:43:17 +00004126 if (isPositionIndependent()) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004127 Addr =
4128 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00004129 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
Evan Chengf3a1fce2009-07-25 00:33:29 +00004130 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004131 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004132 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004133 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004134 Addr =
4135 DAG.getLoad(PTy, dl, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00004136 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
Evan Chengf3a1fce2009-07-25 00:33:29 +00004137 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004138 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004139 }
Evan Cheng10043e22007-01-19 07:51:42 +00004140}
4141
Eli Friedman2d4055b2011-11-09 23:36:02 +00004142static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004143 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004144 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004145
James Molloy547d4c02012-02-20 09:24:05 +00004146 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4147 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4148 return Op;
4149 return DAG.UnrollVectorOp(Op.getNode());
4150 }
4151
4152 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4153 "Invalid type for custom lowering!");
4154 if (VT != MVT::v4i16)
4155 return DAG.UnrollVectorOp(Op.getNode());
4156
4157 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4158 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004159}
4160
Oliver Stannard51b1d462014-08-21 12:50:31 +00004161SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004162 EVT VT = Op.getValueType();
4163 if (VT.isVector())
4164 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004165 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4166 RTLIB::Libcall LC;
4167 if (Op.getOpcode() == ISD::FP_TO_SINT)
4168 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4169 Op.getValueType());
4170 else
4171 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4172 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004173 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004174 /*isSigned*/ false, SDLoc(Op)).first;
4175 }
4176
James Molloyfa041152015-03-23 16:15:16 +00004177 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004178}
4179
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004180static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4181 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004182 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004183
Eli Friedman2d4055b2011-11-09 23:36:02 +00004184 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4185 if (VT.getVectorElementType() == MVT::f32)
4186 return Op;
4187 return DAG.UnrollVectorOp(Op.getNode());
4188 }
4189
Duncan Sandsa41634e2011-08-12 14:54:45 +00004190 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4191 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004192 if (VT != MVT::v4f32)
4193 return DAG.UnrollVectorOp(Op.getNode());
4194
4195 unsigned CastOpc;
4196 unsigned Opc;
4197 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004198 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004199 case ISD::SINT_TO_FP:
4200 CastOpc = ISD::SIGN_EXTEND;
4201 Opc = ISD::SINT_TO_FP;
4202 break;
4203 case ISD::UINT_TO_FP:
4204 CastOpc = ISD::ZERO_EXTEND;
4205 Opc = ISD::UINT_TO_FP;
4206 break;
4207 }
4208
4209 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4210 return DAG.getNode(Opc, dl, VT, Op);
4211}
4212
Oliver Stannard51b1d462014-08-21 12:50:31 +00004213SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004214 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004215 if (VT.isVector())
4216 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004217 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4218 RTLIB::Libcall LC;
4219 if (Op.getOpcode() == ISD::SINT_TO_FP)
4220 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4221 Op.getValueType());
4222 else
4223 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4224 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004225 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004226 /*isSigned*/ false, SDLoc(Op)).first;
4227 }
4228
James Molloyfa041152015-03-23 16:15:16 +00004229 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004230}
4231
Evan Cheng25f93642010-07-08 02:08:50 +00004232SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004233 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004234 SDValue Tmp0 = Op.getOperand(0);
4235 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004237 EVT VT = Op.getValueType();
4238 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004239 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4240 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4241 bool UseNEON = !InGPR && Subtarget->hasNEON();
4242
4243 if (UseNEON) {
4244 // Use VBSL to copy the sign bit.
4245 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4246 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004247 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004248 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4249 if (VT == MVT::f64)
4250 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4251 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004252 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004253 else /*if (VT == MVT::f32)*/
4254 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4255 if (SrcVT == MVT::f32) {
4256 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4257 if (VT == MVT::f64)
4258 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4259 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004260 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004261 } else if (VT == MVT::f32)
4262 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4263 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004264 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004265 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4266 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4267
4268 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004269 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004270 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4271 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4272 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004273
Evan Chengd6b641e2011-02-23 02:24:55 +00004274 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4275 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4276 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004277 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004278 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4279 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004280 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004281 } else {
4282 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4283 }
4284
4285 return Res;
4286 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004287
4288 // Bitcast operand 1 to i32.
4289 if (SrcVT == MVT::f64)
4290 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004291 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004292 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4293
Evan Chengd6b641e2011-02-23 02:24:55 +00004294 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004295 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4296 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004297 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4298 if (VT == MVT::f32) {
4299 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4300 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4301 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4302 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004303 }
4304
Evan Chengd6b641e2011-02-23 02:24:55 +00004305 // f64: Or the high part with signbit and then combine two parts.
4306 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004307 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004308 SDValue Lo = Tmp0.getValue(0);
4309 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4310 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4311 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004312}
4313
Evan Cheng168ced92010-05-22 01:47:14 +00004314SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4315 MachineFunction &MF = DAG.getMachineFunction();
4316 MachineFrameInfo *MFI = MF.getFrameInfo();
4317 MFI->setReturnAddressIsTaken(true);
4318
Bill Wendling908bf812014-01-06 00:43:20 +00004319 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004320 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004321
Evan Cheng168ced92010-05-22 01:47:14 +00004322 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004323 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004324 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4325 if (Depth) {
4326 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004327 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004328 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4329 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00004330 MachinePointerInfo());
Evan Cheng168ced92010-05-22 01:47:14 +00004331 }
4332
4333 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004334 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004335 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4336}
4337
Dan Gohman21cea8a2010-04-17 15:26:15 +00004338SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004339 const ARMBaseRegisterInfo &ARI =
4340 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4341 MachineFunction &MF = DAG.getMachineFunction();
4342 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004343 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004344
Owen Anderson53aa7a92009-08-10 22:56:29 +00004345 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004346 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004347 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004348 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004349 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4350 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004351 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00004352 MachinePointerInfo());
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004353 return FrameAddr;
4354}
4355
Renato Golinc7aea402014-05-06 16:51:25 +00004356// FIXME? Maybe this could be a TableGen attribute on some registers and
4357// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004358unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4359 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004360 unsigned Reg = StringSwitch<unsigned>(RegName)
4361 .Case("sp", ARM::SP)
4362 .Default(0);
4363 if (Reg)
4364 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004365 report_fatal_error(Twine("Invalid register name \""
4366 + StringRef(RegName) + "\"."));
4367}
4368
4369// Result is 64 bit value so split into two 32 bit values and return as a
4370// pair of values.
4371static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4372 SelectionDAG &DAG) {
4373 SDLoc DL(N);
4374
4375 // This function is only supposed to be called for i64 type destination.
4376 assert(N->getValueType(0) == MVT::i64
4377 && "ExpandREAD_REGISTER called for non-i64 type result.");
4378
4379 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4380 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4381 N->getOperand(0),
4382 N->getOperand(1));
4383
4384 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4385 Read.getValue(1)));
4386 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004387}
4388
Quentin Colombet901f0362015-12-04 01:53:14 +00004389/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4390/// When \p DstVT, the destination type of \p BC, is on the vector
4391/// register bank and the source of bitcast, \p Op, operates on the same bank,
4392/// it might be possible to combine them, such that everything stays on the
4393/// vector register bank.
4394/// \p return The node that would replace \p BT, if the combine
4395/// is possible.
4396static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4397 SelectionDAG &DAG) {
4398 SDValue Op = BC->getOperand(0);
4399 EVT DstVT = BC->getValueType(0);
4400
4401 // The only vector instruction that can produce a scalar (remember,
4402 // since the bitcast was about to be turned into VMOVDRR, the source
4403 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4404 // Moreover, we can do this combine only if there is one use.
4405 // Finally, if the destination type is not a vector, there is not
4406 // much point on forcing everything on the vector bank.
4407 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4408 !Op.hasOneUse())
4409 return SDValue();
4410
4411 // If the index is not constant, we will introduce an additional
4412 // multiply that will stick.
4413 // Give up in that case.
4414 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4415 if (!Index)
4416 return SDValue();
4417 unsigned DstNumElt = DstVT.getVectorNumElements();
4418
4419 // Compute the new index.
4420 const APInt &APIntIndex = Index->getAPIntValue();
4421 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4422 NewIndex *= APIntIndex;
4423 // Check if the new constant index fits into i32.
4424 if (NewIndex.getBitWidth() > 32)
4425 return SDValue();
4426
4427 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4428 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4429 SDLoc dl(Op);
4430 SDValue ExtractSrc = Op.getOperand(0);
4431 EVT VecVT = EVT::getVectorVT(
4432 *DAG.getContext(), DstVT.getScalarType(),
4433 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4434 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4435 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4436 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4437}
4438
Wesley Peck527da1b2010-11-23 03:31:01 +00004439/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004440/// expand a bit convert where either the source or destination type is i64 to
4441/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4442/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4443/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004444static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004445 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004446 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004447 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004448
Bob Wilson59b70ea2010-04-17 05:30:19 +00004449 // This function is only supposed to be called for i64 types, either as the
4450 // source or destination of the bit convert.
4451 EVT SrcVT = Op.getValueType();
4452 EVT DstVT = N->getValueType(0);
4453 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004454 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004455
Bob Wilson59b70ea2010-04-17 05:30:19 +00004456 // Turn i64->f64 into VMOVDRR.
4457 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004458 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4459 // if we can combine the bitcast with its source.
4460 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4461 return Val;
4462
Owen Anderson9f944592009-08-11 20:47:22 +00004463 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004464 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004465 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004466 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004467 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004468 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004469 }
Bob Wilson7117a912009-03-20 22:42:55 +00004470
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004471 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004472 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004473 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004474 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004475 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004476 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4477 DAG.getVTList(MVT::i32, MVT::i32),
4478 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4479 else
4480 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4481 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004482 // Merge the pieces into a single i64 value.
4483 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4484 }
Bob Wilson7117a912009-03-20 22:42:55 +00004485
Bob Wilson59b70ea2010-04-17 05:30:19 +00004486 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004487}
4488
Bob Wilson2e076c42009-06-22 23:27:02 +00004489/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004490/// Zero vectors are used to represent vector negation and in those cases
4491/// will be implemented with the NEON VNEG instruction. However, VNEG does
4492/// not support i64 elements, so sometimes the zero vectors will need to be
4493/// explicitly constructed. Regardless, use a canonical VMOV to create the
4494/// zero vector.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004495static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004496 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004497 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004498 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004499 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4500 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004501 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004502}
4503
Jim Grosbach624fcb22009-10-31 21:00:56 +00004504/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4505/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004506SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4507 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004508 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4509 EVT VT = Op.getValueType();
4510 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004511 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004512 SDValue ShOpLo = Op.getOperand(0);
4513 SDValue ShOpHi = Op.getOperand(1);
4514 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004515 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004516 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004517
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004518 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4519
Jim Grosbach624fcb22009-10-31 21:00:56 +00004520 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004521 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004522 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4523 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004524 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004525 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4526 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004527 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004528
4529 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004530 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4531 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004532 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004533 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004534 CCR, Cmp);
4535
4536 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004537 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004538}
4539
Jim Grosbach5d994042009-10-31 19:38:01 +00004540/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4541/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004542SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4543 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004544 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4545 EVT VT = Op.getValueType();
4546 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004547 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004548 SDValue ShOpLo = Op.getOperand(0);
4549 SDValue ShOpHi = Op.getOperand(1);
4550 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004551 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004552
4553 assert(Op.getOpcode() == ISD::SHL_PARTS);
4554 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004555 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004556 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4557 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004558 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004559 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4560 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4561
4562 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4563 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004564 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4565 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004566 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004567 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004568 CCR, Cmp);
4569
4570 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004571 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004572}
4573
Jim Grosbach535d3b42010-09-08 03:54:02 +00004574SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004575 SelectionDAG &DAG) const {
4576 // The rounding mode is in bits 23:22 of the FPSCR.
4577 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4578 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4579 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004580 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004581 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004582 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004583 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004584 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004585 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004586 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004587 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004588 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004589 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004590}
4591
Jim Grosbach8546ec92010-01-18 19:58:49 +00004592static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4593 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004594 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004595 EVT VT = N->getValueType(0);
4596 if (VT.isVector()) {
4597 assert(ST->hasNEON());
4598
4599 // Compute the least significant set bit: LSB = X & -X
4600 SDValue X = N->getOperand(0);
4601 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4602 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4603
4604 EVT ElemTy = VT.getVectorElementType();
4605
4606 if (ElemTy == MVT::i8) {
4607 // Compute with: cttz(x) = ctpop(lsb - 1)
4608 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4609 DAG.getTargetConstant(1, dl, ElemTy));
4610 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4611 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4612 }
4613
4614 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4615 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4616 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4617 unsigned NumBits = ElemTy.getSizeInBits();
4618 SDValue WidthMinus1 =
4619 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4620 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4621 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4622 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4623 }
4624
4625 // Compute with: cttz(x) = ctpop(lsb - 1)
4626
4627 // Since we can only compute the number of bits in a byte with vcnt.8, we
4628 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4629 // and i64.
4630
4631 // Compute LSB - 1.
4632 SDValue Bits;
4633 if (ElemTy == MVT::i64) {
4634 // Load constant 0xffff'ffff'ffff'ffff to register.
4635 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4636 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4637 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4638 } else {
4639 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4640 DAG.getTargetConstant(1, dl, ElemTy));
4641 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4642 }
4643
4644 // Count #bits with vcnt.8.
4645 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4646 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4647 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4648
4649 // Gather the #bits with vpaddl (pairwise add.)
4650 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4651 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4652 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4653 Cnt8);
4654 if (ElemTy == MVT::i16)
4655 return Cnt16;
4656
4657 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4658 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4659 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4660 Cnt16);
4661 if (ElemTy == MVT::i32)
4662 return Cnt32;
4663
4664 assert(ElemTy == MVT::i64);
4665 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4666 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4667 Cnt32);
4668 return Cnt64;
4669 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004670
4671 if (!ST->hasV6T2Ops())
4672 return SDValue();
4673
James Molloyb5640982015-11-13 16:05:22 +00004674 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004675 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4676}
4677
Evan Chengb4eae132012-12-04 22:41:50 +00004678/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4679/// for each 16-bit element from operand, repeated. The basic idea is to
4680/// leverage vcnt to get the 8-bit counts, gather and add the results.
4681///
4682/// Trace for v4i16:
4683/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4684/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4685/// 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 +00004686/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004687/// [b0 b1 b2 b3 b4 b5 b6 b7]
4688/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4689/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4690/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4691static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4692 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004693 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004694
4695 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4696 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4697 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4698 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4699 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4700 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4701}
4702
4703/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4704/// bit-count for each 16-bit element from the operand. We need slightly
4705/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4706/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004707///
Evan Chengb4eae132012-12-04 22:41:50 +00004708/// Trace for v4i16:
4709/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4710/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4711/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4712/// v4i16:Extracted = [k0 k1 k2 k3 ]
4713static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4714 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004715 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004716
4717 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4718 if (VT.is64BitVector()) {
4719 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4720 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004721 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004722 } else {
4723 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004724 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004725 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4726 }
4727}
4728
4729/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4730/// bit-count for each 32-bit element from the operand. The idea here is
4731/// to split the vector into 16-bit elements, leverage the 16-bit count
4732/// routine, and then combine the results.
4733///
4734/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4735/// input = [v0 v1 ] (vi: 32-bit elements)
4736/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4737/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004738/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004739/// [k0 k1 k2 k3 ]
4740/// N1 =+[k1 k0 k3 k2 ]
4741/// [k0 k2 k1 k3 ]
4742/// N2 =+[k1 k3 k0 k2 ]
4743/// [k0 k2 k1 k3 ]
4744/// Extended =+[k1 k3 k0 k2 ]
4745/// [k0 k2 ]
4746/// Extracted=+[k1 k3 ]
4747///
4748static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4749 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004750 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004751
4752 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4753
4754 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4755 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4756 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4757 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4758 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4759
4760 if (VT.is64BitVector()) {
4761 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4762 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004763 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004764 } else {
4765 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004766 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004767 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4768 }
4769}
4770
4771static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4772 const ARMSubtarget *ST) {
4773 EVT VT = N->getValueType(0);
4774
4775 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004776 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4777 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004778 "Unexpected type for custom ctpop lowering");
4779
4780 if (VT.getVectorElementType() == MVT::i32)
4781 return lowerCTPOP32BitElements(N, DAG);
4782 else
4783 return lowerCTPOP16BitElements(N, DAG);
4784}
4785
Bob Wilson2e076c42009-06-22 23:27:02 +00004786static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4787 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004788 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004789 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004790
Bob Wilson7d471332010-11-18 21:16:28 +00004791 if (!VT.isVector())
4792 return SDValue();
4793
Bob Wilson2e076c42009-06-22 23:27:02 +00004794 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004795 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004796
Bob Wilson7d471332010-11-18 21:16:28 +00004797 // Left shifts translate directly to the vshiftu intrinsic.
4798 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004799 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004800 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4801 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004802 N->getOperand(0), N->getOperand(1));
4803
4804 assert((N->getOpcode() == ISD::SRA ||
4805 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4806
4807 // NEON uses the same intrinsics for both left and right shifts. For
4808 // right shifts, the shift amounts are negative, so negate the vector of
4809 // shift amounts.
4810 EVT ShiftVT = N->getOperand(1).getValueType();
4811 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4812 getZeroVector(ShiftVT, DAG, dl),
4813 N->getOperand(1));
4814 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4815 Intrinsic::arm_neon_vshifts :
4816 Intrinsic::arm_neon_vshiftu);
4817 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004818 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004819 N->getOperand(0), NegatedCount);
4820}
4821
4822static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4823 const ARMSubtarget *ST) {
4824 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004825 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004826
Eli Friedman682d8c12009-08-22 03:13:10 +00004827 // We can get here for a node like i32 = ISD::SHL i32, i64
4828 if (VT != MVT::i64)
4829 return SDValue();
4830
4831 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004832 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004833
Chris Lattnerf81d5882007-11-24 07:07:01 +00004834 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004835 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004836 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004837
Chris Lattnerf81d5882007-11-24 07:07:01 +00004838 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004839 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004840
Chris Lattnerf81d5882007-11-24 07:07:01 +00004841 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004842 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004843 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004844 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004845 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004846
Chris Lattnerf81d5882007-11-24 07:07:01 +00004847 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4848 // captures the result into a carry flag.
4849 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004850 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004851
Chris Lattnerf81d5882007-11-24 07:07:01 +00004852 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004853 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004854
Chris Lattnerf81d5882007-11-24 07:07:01 +00004855 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004856 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004857}
4858
Bob Wilson2e076c42009-06-22 23:27:02 +00004859static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4860 SDValue TmpOp0, TmpOp1;
4861 bool Invert = false;
4862 bool Swap = false;
4863 unsigned Opc = 0;
4864
4865 SDValue Op0 = Op.getOperand(0);
4866 SDValue Op1 = Op.getOperand(1);
4867 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004868 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004869 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004870 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004871 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004872
James Molloybf170092015-08-20 16:33:44 +00004873 if (CmpVT.getVectorElementType() == MVT::i64)
4874 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4875 // but it's possible that our operands are 64-bit but our result is 32-bit.
4876 // Bail in this case.
4877 return SDValue();
4878
Oliver Stannard51b1d462014-08-21 12:50:31 +00004879 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004880 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004881 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004882 case ISD::SETUNE:
4883 case ISD::SETNE: Invert = true; // Fallthrough
4884 case ISD::SETOEQ:
4885 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4886 case ISD::SETOLT:
4887 case ISD::SETLT: Swap = true; // Fallthrough
4888 case ISD::SETOGT:
4889 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4890 case ISD::SETOLE:
4891 case ISD::SETLE: Swap = true; // Fallthrough
4892 case ISD::SETOGE:
4893 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4894 case ISD::SETUGE: Swap = true; // Fallthrough
4895 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4896 case ISD::SETUGT: Swap = true; // Fallthrough
4897 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4898 case ISD::SETUEQ: Invert = true; // Fallthrough
4899 case ISD::SETONE:
4900 // Expand this to (OLT | OGT).
4901 TmpOp0 = Op0;
4902 TmpOp1 = Op1;
4903 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004904 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4905 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004906 break;
4907 case ISD::SETUO: Invert = true; // Fallthrough
4908 case ISD::SETO:
4909 // Expand this to (OLT | OGE).
4910 TmpOp0 = Op0;
4911 TmpOp1 = Op1;
4912 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004913 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4914 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004915 break;
4916 }
4917 } else {
4918 // Integer comparisons.
4919 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004920 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004921 case ISD::SETNE: Invert = true;
4922 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4923 case ISD::SETLT: Swap = true;
4924 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4925 case ISD::SETLE: Swap = true;
4926 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4927 case ISD::SETULT: Swap = true;
4928 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4929 case ISD::SETULE: Swap = true;
4930 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4931 }
4932
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004933 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004934 if (Opc == ARMISD::VCEQ) {
4935
4936 SDValue AndOp;
4937 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4938 AndOp = Op0;
4939 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4940 AndOp = Op1;
4941
4942 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004943 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004944 AndOp = AndOp.getOperand(0);
4945
4946 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4947 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004948 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4949 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004950 Invert = !Invert;
4951 }
4952 }
4953 }
4954
4955 if (Swap)
4956 std::swap(Op0, Op1);
4957
Owen Andersonc7baee32010-11-08 23:21:22 +00004958 // If one of the operands is a constant vector zero, attempt to fold the
4959 // comparison to a specialized compare-against-zero form.
4960 SDValue SingleOp;
4961 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4962 SingleOp = Op0;
4963 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4964 if (Opc == ARMISD::VCGE)
4965 Opc = ARMISD::VCLEZ;
4966 else if (Opc == ARMISD::VCGT)
4967 Opc = ARMISD::VCLTZ;
4968 SingleOp = Op1;
4969 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004970
Owen Andersonc7baee32010-11-08 23:21:22 +00004971 SDValue Result;
4972 if (SingleOp.getNode()) {
4973 switch (Opc) {
4974 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004975 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004976 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004977 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004978 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004979 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004980 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004981 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004982 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004983 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004984 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004985 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004986 }
4987 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004988 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004989 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004990
Tim Northover45aa89c2015-02-08 00:50:47 +00004991 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4992
Bob Wilson2e076c42009-06-22 23:27:02 +00004993 if (Invert)
4994 Result = DAG.getNOT(dl, Result, VT);
4995
4996 return Result;
4997}
4998
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00004999static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) {
5000 SDValue LHS = Op.getOperand(0);
5001 SDValue RHS = Op.getOperand(1);
5002 SDValue Carry = Op.getOperand(2);
5003 SDValue Cond = Op.getOperand(3);
5004 SDLoc DL(Op);
5005
5006 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only.");
5007
5008 assert(Carry.getOpcode() != ISD::CARRY_FALSE);
5009 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
5010 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
5011
5012 SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
5013 SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
5014 SDValue ARMcc = DAG.getConstant(
5015 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
5016 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
5017 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
5018 Cmp.getValue(1), SDValue());
5019 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
5020 CCR, Chain.getValue(1));
5021}
5022
Bob Wilson5b2b5042010-06-14 22:19:57 +00005023/// isNEONModifiedImm - Check if the specified splat value corresponds to a
5024/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00005025/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00005026static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
5027 unsigned SplatBitSize, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005028 const SDLoc &dl, EVT &VT, bool is128Bits,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005029 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005030 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00005031
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005032 // SplatBitSize is set to the smallest size that splats the vector, so a
5033 // zero vector will always have SplatBitSize == 8. However, NEON modified
5034 // immediate instructions others than VMOV do not support the 8-bit encoding
5035 // of a zero vector, and the default encoding of zero is supposed to be the
5036 // 32-bit version.
5037 if (SplatBits == 0)
5038 SplatBitSize = 32;
5039
Bob Wilson2e076c42009-06-22 23:27:02 +00005040 switch (SplatBitSize) {
5041 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00005042 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00005043 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005044 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00005045 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005046 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00005047 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00005048 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005049 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00005050
5051 case 16:
5052 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00005053 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005054 if ((SplatBits & ~0xff) == 0) {
5055 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005056 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005057 Imm = SplatBits;
5058 break;
5059 }
5060 if ((SplatBits & ~0xff00) == 0) {
5061 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005062 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00005063 Imm = SplatBits >> 8;
5064 break;
5065 }
5066 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005067
5068 case 32:
5069 // NEON's 32-bit VMOV supports splat values where:
5070 // * only one byte is nonzero, or
5071 // * the least significant byte is 0xff and the second byte is nonzero, or
5072 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00005073 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00005074 if ((SplatBits & ~0xff) == 0) {
5075 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005076 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005077 Imm = SplatBits;
5078 break;
5079 }
5080 if ((SplatBits & ~0xff00) == 0) {
5081 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005082 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00005083 Imm = SplatBits >> 8;
5084 break;
5085 }
5086 if ((SplatBits & ~0xff0000) == 0) {
5087 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005088 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00005089 Imm = SplatBits >> 16;
5090 break;
5091 }
5092 if ((SplatBits & ~0xff000000) == 0) {
5093 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005094 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00005095 Imm = SplatBits >> 24;
5096 break;
5097 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005098
Owen Andersona4076922010-11-05 21:57:54 +00005099 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
5100 if (type == OtherModImm) return SDValue();
5101
Bob Wilson2e076c42009-06-22 23:27:02 +00005102 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005103 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
5104 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005105 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00005106 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005107 break;
5108 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005109
5110 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005111 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
5112 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005113 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00005114 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005115 break;
5116 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005117
5118 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
5119 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
5120 // VMOV.I32. A (very) minor optimization would be to replicate the value
5121 // and fall through here to test for a valid 64-bit splat. But, then the
5122 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00005123 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005124
5125 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00005126 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005127 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005128 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00005129 uint64_t BitMask = 0xff;
5130 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005131 unsigned ImmMask = 1;
5132 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005133 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005134 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005135 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005136 Imm |= ImmMask;
5137 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005138 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005139 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005140 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005141 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005142 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005143
Mehdi Aminiffc14022015-07-08 01:00:38 +00005144 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005145 // swap higher and lower 32 bit word
5146 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5147
Bob Wilson6eae5202010-06-11 21:34:50 +00005148 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005149 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005150 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005151 break;
5152 }
5153
Bob Wilson6eae5202010-06-11 21:34:50 +00005154 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005155 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005156 }
5157
Bob Wilsona3f19012010-07-13 21:16:48 +00005158 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005159 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005160}
5161
Lang Hames591cdaf2012-03-29 21:56:11 +00005162SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5163 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005164 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005165 return SDValue();
5166
Tim Northoverf79c3a52013-08-20 08:57:11 +00005167 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005168 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005169
Oliver Stannard51b1d462014-08-21 12:50:31 +00005170 // Use the default (constant pool) lowering for double constants when we have
5171 // an SP-only FPU
5172 if (IsDouble && Subtarget->isFPOnlySP())
5173 return SDValue();
5174
Lang Hames591cdaf2012-03-29 21:56:11 +00005175 // Try splatting with a VMOV.f32...
Benjamin Kramer46e38f32016-06-08 10:01:20 +00005176 const APFloat &FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005177 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5178
Lang Hames591cdaf2012-03-29 21:56:11 +00005179 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005180 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5181 // We have code in place to select a valid ConstantFP already, no need to
5182 // do any mangling.
5183 return Op;
5184 }
5185
5186 // It's a float and we are trying to use NEON operations where
5187 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005188 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005189 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005190 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5191 NewVal);
5192 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005193 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005194 }
5195
Tim Northoverf79c3a52013-08-20 08:57:11 +00005196 // The rest of our options are NEON only, make sure that's allowed before
5197 // proceeding..
5198 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5199 return SDValue();
5200
Lang Hames591cdaf2012-03-29 21:56:11 +00005201 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005202 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5203
5204 // It wouldn't really be worth bothering for doubles except for one very
5205 // important value, which does happen to match: 0.0. So make sure we don't do
5206 // anything stupid.
5207 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5208 return SDValue();
5209
5210 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005211 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5212 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005213 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005214 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005215 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5216 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005217 if (IsDouble)
5218 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5219
5220 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005221 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5222 VecConstant);
5223 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005224 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005225 }
5226
5227 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005228 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005229 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005230 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005231 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005232 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005233
5234 if (IsDouble)
5235 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5236
5237 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005238 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5239 VecConstant);
5240 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005241 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005242 }
5243
5244 return SDValue();
5245}
5246
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005247// check if an VEXT instruction can handle the shuffle mask when the
5248// vector sources of the shuffle are the same.
5249static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5250 unsigned NumElts = VT.getVectorNumElements();
5251
5252 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5253 if (M[0] < 0)
5254 return false;
5255
5256 Imm = M[0];
5257
5258 // If this is a VEXT shuffle, the immediate value is the index of the first
5259 // element. The other shuffle indices must be the successive elements after
5260 // the first one.
5261 unsigned ExpectedElt = Imm;
5262 for (unsigned i = 1; i < NumElts; ++i) {
5263 // Increment the expected index. If it wraps around, just follow it
5264 // back to index zero and keep going.
5265 ++ExpectedElt;
5266 if (ExpectedElt == NumElts)
5267 ExpectedElt = 0;
5268
5269 if (M[i] < 0) continue; // ignore UNDEF indices
5270 if (ExpectedElt != static_cast<unsigned>(M[i]))
5271 return false;
5272 }
5273
5274 return true;
5275}
5276
Lang Hames591cdaf2012-03-29 21:56:11 +00005277
Benjamin Kramer339ced42012-01-15 13:16:05 +00005278static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005279 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005280 unsigned NumElts = VT.getVectorNumElements();
5281 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005282
5283 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5284 if (M[0] < 0)
5285 return false;
5286
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005287 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005288
5289 // If this is a VEXT shuffle, the immediate value is the index of the first
5290 // element. The other shuffle indices must be the successive elements after
5291 // the first one.
5292 unsigned ExpectedElt = Imm;
5293 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005294 // Increment the expected index. If it wraps around, it may still be
5295 // a VEXT but the source vectors must be swapped.
5296 ExpectedElt += 1;
5297 if (ExpectedElt == NumElts * 2) {
5298 ExpectedElt = 0;
5299 ReverseVEXT = true;
5300 }
5301
Bob Wilson411dfad2010-08-17 05:54:34 +00005302 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005303 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005304 return false;
5305 }
5306
5307 // Adjust the index value if the source operands will be swapped.
5308 if (ReverseVEXT)
5309 Imm -= NumElts;
5310
Bob Wilson32cd8552009-08-19 17:03:43 +00005311 return true;
5312}
5313
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005314/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5315/// instruction with the specified blocksize. (The order of the elements
5316/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005317static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005318 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5319 "Only possible block sizes for VREV are: 16, 32, 64");
5320
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005321 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005322 if (EltSz == 64)
5323 return false;
5324
5325 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005326 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005327 // If the first shuffle index is UNDEF, be optimistic.
5328 if (M[0] < 0)
5329 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005330
5331 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5332 return false;
5333
5334 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005335 if (M[i] < 0) continue; // ignore UNDEF indices
5336 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005337 return false;
5338 }
5339
5340 return true;
5341}
5342
Benjamin Kramer339ced42012-01-15 13:16:05 +00005343static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005344 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5345 // range, then 0 is placed into the resulting vector. So pretty much any mask
5346 // of 8 elements can work here.
5347 return VT == MVT::v8i8 && M.size() == 8;
5348}
5349
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005350// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5351// checking that pairs of elements in the shuffle mask represent the same index
5352// in each vector, incrementing the expected index by 2 at each step.
5353// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5354// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5355// v2={e,f,g,h}
5356// WhichResult gives the offset for each element in the mask based on which
5357// of the two results it belongs to.
5358//
5359// The transpose can be represented either as:
5360// result1 = shufflevector v1, v2, result1_shuffle_mask
5361// result2 = shufflevector v1, v2, result2_shuffle_mask
5362// where v1/v2 and the shuffle masks have the same number of elements
5363// (here WhichResult (see below) indicates which result is being checked)
5364//
5365// or as:
5366// results = shufflevector v1, v2, shuffle_mask
5367// where both results are returned in one vector and the shuffle mask has twice
5368// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5369// want to check the low half and high half of the shuffle mask as if it were
5370// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005371static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005372 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5373 if (EltSz == 64)
5374 return false;
5375
Bob Wilsona7062312009-08-21 20:54:19 +00005376 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005377 if (M.size() != NumElts && M.size() != NumElts*2)
5378 return false;
5379
James Molloy8c995a92015-09-10 08:42:28 +00005380 // If the mask is twice as long as the input vector then we need to check the
5381 // upper and lower parts of the mask with a matching value for WhichResult
5382 // FIXME: A mask with only even values will be rejected in case the first
5383 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5384 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005385 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005386 if (M.size() == NumElts * 2)
5387 WhichResult = i / NumElts;
5388 else
5389 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005390 for (unsigned j = 0; j < NumElts; j += 2) {
5391 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5392 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5393 return false;
5394 }
Bob Wilsona7062312009-08-21 20:54:19 +00005395 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005396
5397 if (M.size() == NumElts*2)
5398 WhichResult = 0;
5399
Bob Wilsona7062312009-08-21 20:54:19 +00005400 return true;
5401}
5402
Bob Wilson0bbd3072009-12-03 06:40:55 +00005403/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5404/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5405/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005406static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005407 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5408 if (EltSz == 64)
5409 return false;
5410
5411 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005412 if (M.size() != NumElts && M.size() != NumElts*2)
5413 return false;
5414
5415 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005416 if (M.size() == NumElts * 2)
5417 WhichResult = i / NumElts;
5418 else
5419 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005420 for (unsigned j = 0; j < NumElts; j += 2) {
5421 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5422 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5423 return false;
5424 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005425 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005426
5427 if (M.size() == NumElts*2)
5428 WhichResult = 0;
5429
Bob Wilson0bbd3072009-12-03 06:40:55 +00005430 return true;
5431}
5432
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005433// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5434// that the mask elements are either all even and in steps of size 2 or all odd
5435// and in steps of size 2.
5436// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5437// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5438// v2={e,f,g,h}
5439// Requires similar checks to that of isVTRNMask with
5440// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005441static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005442 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5443 if (EltSz == 64)
5444 return false;
5445
Bob Wilsona7062312009-08-21 20:54:19 +00005446 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005447 if (M.size() != NumElts && M.size() != NumElts*2)
5448 return false;
5449
5450 for (unsigned i = 0; i < M.size(); i += NumElts) {
5451 WhichResult = M[i] == 0 ? 0 : 1;
5452 for (unsigned j = 0; j < NumElts; ++j) {
5453 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5454 return false;
5455 }
Bob Wilsona7062312009-08-21 20:54:19 +00005456 }
5457
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005458 if (M.size() == NumElts*2)
5459 WhichResult = 0;
5460
Bob Wilsona7062312009-08-21 20:54:19 +00005461 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005462 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005463 return false;
5464
5465 return true;
5466}
5467
Bob Wilson0bbd3072009-12-03 06:40:55 +00005468/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5469/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5470/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005471static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005472 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5473 if (EltSz == 64)
5474 return false;
5475
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005476 unsigned NumElts = VT.getVectorNumElements();
5477 if (M.size() != NumElts && M.size() != NumElts*2)
5478 return false;
5479
5480 unsigned Half = NumElts / 2;
5481 for (unsigned i = 0; i < M.size(); i += NumElts) {
5482 WhichResult = M[i] == 0 ? 0 : 1;
5483 for (unsigned j = 0; j < NumElts; j += Half) {
5484 unsigned Idx = WhichResult;
5485 for (unsigned k = 0; k < Half; ++k) {
5486 int MIdx = M[i + j + k];
5487 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5488 return false;
5489 Idx += 2;
5490 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005491 }
5492 }
5493
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005494 if (M.size() == NumElts*2)
5495 WhichResult = 0;
5496
Bob Wilson0bbd3072009-12-03 06:40:55 +00005497 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5498 if (VT.is64BitVector() && EltSz == 32)
5499 return false;
5500
5501 return true;
5502}
5503
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005504// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5505// that pairs of elements of the shufflemask represent the same index in each
5506// vector incrementing sequentially through the vectors.
5507// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5508// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5509// v2={e,f,g,h}
5510// Requires similar checks to that of isVTRNMask with respect the how results
5511// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005512static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005513 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5514 if (EltSz == 64)
5515 return false;
5516
Bob Wilsona7062312009-08-21 20:54:19 +00005517 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005518 if (M.size() != NumElts && M.size() != NumElts*2)
5519 return false;
5520
5521 for (unsigned i = 0; i < M.size(); i += NumElts) {
5522 WhichResult = M[i] == 0 ? 0 : 1;
5523 unsigned Idx = WhichResult * NumElts / 2;
5524 for (unsigned j = 0; j < NumElts; j += 2) {
5525 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5526 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5527 return false;
5528 Idx += 1;
5529 }
Bob Wilsona7062312009-08-21 20:54:19 +00005530 }
5531
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005532 if (M.size() == NumElts*2)
5533 WhichResult = 0;
5534
Bob Wilsona7062312009-08-21 20:54:19 +00005535 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005536 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005537 return false;
5538
5539 return true;
5540}
5541
Bob Wilson0bbd3072009-12-03 06:40:55 +00005542/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5543/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5544/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005545static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005546 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5547 if (EltSz == 64)
5548 return false;
5549
5550 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005551 if (M.size() != NumElts && M.size() != NumElts*2)
5552 return false;
5553
5554 for (unsigned i = 0; i < M.size(); i += NumElts) {
5555 WhichResult = M[i] == 0 ? 0 : 1;
5556 unsigned Idx = WhichResult * NumElts / 2;
5557 for (unsigned j = 0; j < NumElts; j += 2) {
5558 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5559 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5560 return false;
5561 Idx += 1;
5562 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005563 }
5564
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005565 if (M.size() == NumElts*2)
5566 WhichResult = 0;
5567
Bob Wilson0bbd3072009-12-03 06:40:55 +00005568 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5569 if (VT.is64BitVector() && EltSz == 32)
5570 return false;
5571
5572 return true;
5573}
5574
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005575/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5576/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5577static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5578 unsigned &WhichResult,
5579 bool &isV_UNDEF) {
5580 isV_UNDEF = false;
5581 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5582 return ARMISD::VTRN;
5583 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5584 return ARMISD::VUZP;
5585 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5586 return ARMISD::VZIP;
5587
5588 isV_UNDEF = true;
5589 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5590 return ARMISD::VTRN;
5591 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5592 return ARMISD::VUZP;
5593 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5594 return ARMISD::VZIP;
5595
5596 return 0;
5597}
5598
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005599/// \return true if this is a reverse operation on an vector.
5600static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5601 unsigned NumElts = VT.getVectorNumElements();
5602 // Make sure the mask has the right size.
5603 if (NumElts != M.size())
5604 return false;
5605
5606 // Look for <15, ..., 3, -1, 1, 0>.
5607 for (unsigned i = 0; i != NumElts; ++i)
5608 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5609 return false;
5610
5611 return true;
5612}
5613
Dale Johannesen2bff5052010-07-29 20:10:08 +00005614// If N is an integer constant that can be moved into a register in one
5615// instruction, return an SDValue of such a constant (will become a MOV
5616// instruction). Otherwise return null.
5617static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005618 const ARMSubtarget *ST, const SDLoc &dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005619 uint64_t Val;
5620 if (!isa<ConstantSDNode>(N))
5621 return SDValue();
5622 Val = cast<ConstantSDNode>(N)->getZExtValue();
5623
5624 if (ST->isThumb1Only()) {
5625 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005626 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005627 } else {
5628 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005629 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005630 }
5631 return SDValue();
5632}
5633
Bob Wilson2e076c42009-06-22 23:27:02 +00005634// If this is a case we can't handle, return null and let the default
5635// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005636SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5637 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005638 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005639 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005640 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005641
5642 APInt SplatBits, SplatUndef;
5643 unsigned SplatBitSize;
5644 bool HasAnyUndefs;
5645 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005646 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005647 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005648 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005649 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005650 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005651 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005652 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005653 if (Val.getNode()) {
5654 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005655 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005656 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005657
5658 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005659 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005660 Val = isNEONModifiedImm(NegatedImm,
5661 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005662 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005663 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005664 if (Val.getNode()) {
5665 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005666 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005667 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005668
5669 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005670 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005671 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005672 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005673 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005674 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5675 }
5676 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005677 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005678 }
5679
Bob Wilson91fdf682010-05-22 00:23:12 +00005680 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005681 //
5682 // As an optimisation, even if more than one value is used it may be more
5683 // profitable to splat with one value then change some lanes.
5684 //
5685 // Heuristically we decide to do this if the vector has a "dominant" value,
5686 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005687 unsigned NumElts = VT.getVectorNumElements();
5688 bool isOnlyLowElement = true;
5689 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005690 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005691 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005692
5693 // Map of the number of times a particular SDValue appears in the
5694 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005695 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005696 SDValue Value;
5697 for (unsigned i = 0; i < NumElts; ++i) {
5698 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005699 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00005700 continue;
5701 if (i > 0)
5702 isOnlyLowElement = false;
5703 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5704 isConstant = false;
5705
James Molloy49bdbce2012-09-06 09:55:02 +00005706 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005707 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005708
James Molloy49bdbce2012-09-06 09:55:02 +00005709 // Is this value dominant? (takes up more than half of the lanes)
5710 if (++Count > (NumElts / 2)) {
5711 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005712 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005713 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005714 }
James Molloy49bdbce2012-09-06 09:55:02 +00005715 if (ValueCounts.size() != 1)
5716 usesOnlyOneValue = false;
5717 if (!Value.getNode() && ValueCounts.size() > 0)
5718 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005719
James Molloy49bdbce2012-09-06 09:55:02 +00005720 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005721 return DAG.getUNDEF(VT);
5722
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005723 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5724 // Keep going if we are hitting this case.
5725 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005726 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5727
Dale Johannesen2bff5052010-07-29 20:10:08 +00005728 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5729
Dale Johannesen710a2d92010-10-19 20:00:17 +00005730 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5731 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005732 if (hasDominantValue && EltSize <= 32) {
5733 if (!isConstant) {
5734 SDValue N;
5735
5736 // If we are VDUPing a value that comes directly from a vector, that will
5737 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005738 // just use VDUPLANE. We can only do this if the lane being extracted
5739 // is at a constant index, as the VDUP from lane instructions only have
5740 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005741 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005742 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005743 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005744 // We need to create a new undef vector to use for the VDUPLANE if the
5745 // size of the vector from which we get the value is different than the
5746 // size of the vector that we need to create. We will insert the element
5747 // such that the register coalescer will remove unnecessary copies.
5748 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005749 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5750 VT.getVectorNumElements();
5751 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5752 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005753 Value, DAG.getConstant(index, dl, MVT::i32)),
5754 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005755 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005756 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005757 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005758 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005759 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5760
5761 if (!usesOnlyOneValue) {
5762 // The dominant value was splatted as 'N', but we now have to insert
5763 // all differing elements.
5764 for (unsigned I = 0; I < NumElts; ++I) {
5765 if (Op.getOperand(I) == Value)
5766 continue;
5767 SmallVector<SDValue, 3> Ops;
5768 Ops.push_back(N);
5769 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005770 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005771 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005772 }
5773 }
5774 return N;
5775 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005776 if (VT.getVectorElementType().isFloatingPoint()) {
5777 SmallVector<SDValue, 8> Ops;
5778 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005779 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005780 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005781 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00005782 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005783 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5784 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005785 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005786 }
James Molloy49bdbce2012-09-06 09:55:02 +00005787 if (usesOnlyOneValue) {
5788 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5789 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005790 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005791 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005792 }
5793
5794 // If all elements are constants and the case above didn't get hit, fall back
5795 // to the default expansion, which will generate a load from the constant
5796 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005797 if (isConstant)
5798 return SDValue();
5799
Bob Wilson6f2b8962011-01-07 21:37:30 +00005800 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5801 if (NumElts >= 4) {
5802 SDValue shuffle = ReconstructShuffle(Op, DAG);
5803 if (shuffle != SDValue())
5804 return shuffle;
5805 }
5806
Bob Wilson91fdf682010-05-22 00:23:12 +00005807 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005808 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5809 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005810 if (EltSize >= 32) {
5811 // Do the expansion with floating-point types, since that is what the VFP
5812 // registers are defined to use, and since i64 is not legal.
5813 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5814 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005815 SmallVector<SDValue, 8> Ops;
5816 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005817 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005818 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005819 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005820 }
5821
Jim Grosbach24e102a2013-07-08 18:18:52 +00005822 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5823 // know the default expansion would otherwise fall back on something even
5824 // worse. For a vector with one or two non-undef values, that's
5825 // scalar_to_vector for the elements followed by a shuffle (provided the
5826 // shuffle is valid for the target) and materialization element by element
5827 // on the stack followed by a load for everything else.
5828 if (!isConstant && !usesOnlyOneValue) {
5829 SDValue Vec = DAG.getUNDEF(VT);
5830 for (unsigned i = 0 ; i < NumElts; ++i) {
5831 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005832 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00005833 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005834 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005835 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5836 }
5837 return Vec;
5838 }
5839
Bob Wilson2e076c42009-06-22 23:27:02 +00005840 return SDValue();
5841}
5842
Bob Wilson6f2b8962011-01-07 21:37:30 +00005843// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005844// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005845SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5846 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005847 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005848 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005849 EVT VT = Op.getValueType();
5850 unsigned NumElts = VT.getVectorNumElements();
5851
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005852 struct ShuffleSourceInfo {
5853 SDValue Vec;
5854 unsigned MinElt;
5855 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005856
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005857 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5858 // be compatible with the shuffle we intend to construct. As a result
5859 // ShuffleVec will be some sliding window into the original Vec.
5860 SDValue ShuffleVec;
5861
5862 // Code should guarantee that element i in Vec starts at element "WindowBase
5863 // + i * WindowScale in ShuffleVec".
5864 int WindowBase;
5865 int WindowScale;
5866
5867 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5868 ShuffleSourceInfo(SDValue Vec)
5869 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5870 WindowScale(1) {}
5871 };
5872
5873 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5874 // node.
5875 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005876 for (unsigned i = 0; i < NumElts; ++i) {
5877 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005878 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005879 continue;
5880 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5881 // A shuffle can only come from building a vector from various
5882 // elements of other vectors.
5883 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005884 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5885 // Furthermore, shuffles require a constant mask, whereas extractelts
5886 // accept variable indices.
5887 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005888 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005889
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005890 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005891 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005892 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5893 if (Source == Sources.end())
5894 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005895
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005896 // Update the minimum and maximum lane number seen.
5897 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5898 Source->MinElt = std::min(Source->MinElt, EltNo);
5899 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005900 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005901
Bob Wilson6f2b8962011-01-07 21:37:30 +00005902 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005903 // are involved.
5904 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005905 return SDValue();
5906
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005907 // Find out the smallest element size among result and two sources, and use
5908 // it as element size to build the shuffle_vector.
5909 EVT SmallestEltTy = VT.getVectorElementType();
5910 for (auto &Source : Sources) {
5911 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5912 if (SrcEltTy.bitsLT(SmallestEltTy))
5913 SmallestEltTy = SrcEltTy;
5914 }
5915 unsigned ResMultiplier =
5916 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5917 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5918 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005919
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005920 // If the source vector is too wide or too narrow, we may nevertheless be able
5921 // to construct a compatible shuffle either by concatenating it with UNDEF or
5922 // extracting a suitable range of elements.
5923 for (auto &Src : Sources) {
5924 EVT SrcVT = Src.ShuffleVec.getValueType();
5925
5926 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005927 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005928
5929 // This stage of the search produces a source with the same element type as
5930 // the original, but with a total width matching the BUILD_VECTOR output.
5931 EVT EltVT = SrcVT.getVectorElementType();
5932 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5933 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5934
5935 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5936 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5937 return SDValue();
5938 // We can pad out the smaller vector for free, so if it's part of a
5939 // shuffle...
5940 Src.ShuffleVec =
5941 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5942 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5943 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005944 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005945
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005946 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5947 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005948
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005949 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005950 // Span too large for a VEXT to cope
5951 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005952 }
5953
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005954 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005955 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005956 Src.ShuffleVec =
5957 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5958 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5959 Src.WindowBase = -NumSrcElts;
5960 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005961 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005962 Src.ShuffleVec =
5963 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5964 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005965 } else {
5966 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005967 SDValue VEXTSrc1 =
5968 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5969 DAG.getConstant(0, dl, MVT::i32));
5970 SDValue VEXTSrc2 =
5971 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5972 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005973
5974 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5975 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005976 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005977 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005978 }
5979 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005980
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005981 // Another possible incompatibility occurs from the vector element types. We
5982 // can fix this by bitcasting the source vectors to the same type we intend
5983 // for the shuffle.
5984 for (auto &Src : Sources) {
5985 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5986 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005987 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005988 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5989 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5990 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5991 Src.WindowBase *= Src.WindowScale;
5992 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005993
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005994 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005995 DEBUG(
5996 for (auto Src : Sources)
5997 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5998 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005999
6000 // The stars all align, our next step is to produce the mask for the shuffle.
6001 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
6002 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
6003 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
6004 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006005 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006006 continue;
6007
6008 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
6009 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
6010
6011 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
6012 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
6013 // segment.
6014 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
6015 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
6016 VT.getVectorElementType().getSizeInBits());
6017 int LanesDefined = BitsDefined / BitsPerShuffleLane;
6018
6019 // This source is expected to fill ResMultiplier lanes of the final shuffle,
6020 // starting at the appropriate offset.
6021 int *LaneMask = &Mask[i * ResMultiplier];
6022
6023 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
6024 ExtractBase += NumElts * (Src - Sources.begin());
6025 for (int j = 0; j < LanesDefined; ++j)
6026 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006027 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006028
Bob Wilson6f2b8962011-01-07 21:37:30 +00006029 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006030 if (!isShuffleMaskLegal(Mask, ShuffleVT))
6031 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006032
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006033 // We can't handle more than two sources. This should have already
6034 // been checked before this point.
6035 assert(Sources.size() <= 2 && "Too many sources!");
6036
6037 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
6038 for (unsigned i = 0; i < Sources.size(); ++i)
6039 ShuffleOps[i] = Sources[i].ShuffleVec;
6040
6041 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
Craig Topper2bd8b4b2016-07-01 06:54:47 +00006042 ShuffleOps[1], Mask);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006043 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006044}
6045
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006046/// isShuffleMaskLegal - Targets can use this to indicate that they only
6047/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6048/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6049/// are assumed to be legal.
6050bool
6051ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6052 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006053 if (VT.getVectorNumElements() == 4 &&
6054 (VT.is128BitVector() || VT.is64BitVector())) {
6055 unsigned PFIndexes[4];
6056 for (unsigned i = 0; i != 4; ++i) {
6057 if (M[i] < 0)
6058 PFIndexes[i] = 8;
6059 else
6060 PFIndexes[i] = M[i];
6061 }
6062
6063 // Compute the index in the perfect shuffle table.
6064 unsigned PFTableIndex =
6065 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
6066 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6067 unsigned Cost = (PFEntry >> 30);
6068
6069 if (Cost <= 4)
6070 return true;
6071 }
6072
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006073 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00006074 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006075
Bob Wilson846bd792010-06-07 23:53:38 +00006076 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6077 return (EltSize >= 32 ||
6078 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006079 isVREVMask(M, VT, 64) ||
6080 isVREVMask(M, VT, 32) ||
6081 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00006082 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00006083 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006084 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006085 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006086}
6087
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006088/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6089/// the specified operations to build the shuffle.
6090static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
6091 SDValue RHS, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006092 const SDLoc &dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006093 unsigned OpNum = (PFEntry >> 26) & 0x0F;
6094 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
6095 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
6096
6097 enum {
6098 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
6099 OP_VREV,
6100 OP_VDUP0,
6101 OP_VDUP1,
6102 OP_VDUP2,
6103 OP_VDUP3,
6104 OP_VEXT1,
6105 OP_VEXT2,
6106 OP_VEXT3,
6107 OP_VUZPL, // VUZP, left result
6108 OP_VUZPR, // VUZP, right result
6109 OP_VZIPL, // VZIP, left result
6110 OP_VZIPR, // VZIP, right result
6111 OP_VTRNL, // VTRN, left result
6112 OP_VTRNR // VTRN, right result
6113 };
6114
6115 if (OpNum == OP_COPY) {
6116 if (LHSID == (1*9+2)*9+3) return LHS;
6117 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6118 return RHS;
6119 }
6120
6121 SDValue OpLHS, OpRHS;
6122 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6123 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6124 EVT VT = OpLHS.getValueType();
6125
6126 switch (OpNum) {
6127 default: llvm_unreachable("Unknown shuffle opcode!");
6128 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00006129 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00006130 if (VT.getVectorElementType() == MVT::i32 ||
6131 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00006132 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6133 // vrev <4 x i16> -> VREV32
6134 if (VT.getVectorElementType() == MVT::i16)
6135 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6136 // vrev <4 x i8> -> VREV16
6137 assert(VT.getVectorElementType() == MVT::i8);
6138 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006139 case OP_VDUP0:
6140 case OP_VDUP1:
6141 case OP_VDUP2:
6142 case OP_VDUP3:
6143 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006144 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006145 case OP_VEXT1:
6146 case OP_VEXT2:
6147 case OP_VEXT3:
6148 return DAG.getNode(ARMISD::VEXT, dl, VT,
6149 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006150 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006151 case OP_VUZPL:
6152 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006153 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006154 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6155 case OP_VZIPL:
6156 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006157 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006158 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6159 case OP_VTRNL:
6160 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006161 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6162 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006163 }
6164}
6165
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006166static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006167 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006168 SelectionDAG &DAG) {
6169 // Check to see if we can use the VTBL instruction.
6170 SDValue V1 = Op.getOperand(0);
6171 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006172 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006173
6174 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006175 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006176 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006177 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006178
Sanjay Patel57195842016-03-14 17:28:46 +00006179 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006180 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006181 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006182
Owen Anderson77aa2662011-04-05 21:48:57 +00006183 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006184 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006185}
6186
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006187static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6188 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006189 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006190 SDValue OpLHS = Op.getOperand(0);
6191 EVT VT = OpLHS.getValueType();
6192
6193 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6194 "Expect an v8i16/v16i8 type");
6195 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6196 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6197 // extract the first 8 bytes into the top double word and the last 8 bytes
6198 // into the bottom double word. The v8i16 case is similar.
6199 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6200 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006201 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006202}
6203
Bob Wilson2e076c42009-06-22 23:27:02 +00006204static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006205 SDValue V1 = Op.getOperand(0);
6206 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006207 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006208 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006209 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006210
Bob Wilsonc6800b52009-08-13 02:13:04 +00006211 // Convert shuffles that are directly supported on NEON to target-specific
6212 // DAG nodes, instead of keeping them as shuffles and matching them again
6213 // during code selection. This is more efficient and avoids the possibility
6214 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006215 // FIXME: floating-point vectors should be canonicalized to integer vectors
6216 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006217 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006218
Bob Wilson846bd792010-06-07 23:53:38 +00006219 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6220 if (EltSize <= 32) {
Craig Topperbc56e3b2016-06-30 04:38:51 +00006221 if (SVN->isSplat()) {
Bob Wilson846bd792010-06-07 23:53:38 +00006222 int Lane = SVN->getSplatIndex();
6223 // If this is undef splat, generate it via "just" vdup, if possible.
6224 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006225
Dan Gohman198b7ff2011-11-03 21:49:52 +00006226 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006227 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6228 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6229 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006230 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6231 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6232 // reaches it).
6233 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6234 !isa<ConstantSDNode>(V1.getOperand(0))) {
6235 bool IsScalarToVector = true;
6236 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006237 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006238 IsScalarToVector = false;
6239 break;
6240 }
6241 if (IsScalarToVector)
6242 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6243 }
Bob Wilson846bd792010-06-07 23:53:38 +00006244 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006245 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006246 }
Bob Wilson846bd792010-06-07 23:53:38 +00006247
6248 bool ReverseVEXT;
6249 unsigned Imm;
6250 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6251 if (ReverseVEXT)
6252 std::swap(V1, V2);
6253 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006254 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006255 }
6256
6257 if (isVREVMask(ShuffleMask, VT, 64))
6258 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6259 if (isVREVMask(ShuffleMask, VT, 32))
6260 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6261 if (isVREVMask(ShuffleMask, VT, 16))
6262 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6263
Sanjay Patel57195842016-03-14 17:28:46 +00006264 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006265 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006266 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006267 }
6268
Bob Wilson846bd792010-06-07 23:53:38 +00006269 // Check for Neon shuffles that modify both input vectors in place.
6270 // If both results are used, i.e., if there are two shuffles with the same
6271 // source operands and with masks corresponding to both results of one of
6272 // these operations, DAG memoization will ensure that a single node is
6273 // used for both shuffles.
6274 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006275 bool isV_UNDEF;
6276 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6277 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6278 if (isV_UNDEF)
6279 V2 = V1;
6280 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6281 .getValue(WhichResult);
6282 }
Bob Wilson846bd792010-06-07 23:53:38 +00006283
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006284 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6285 // shuffles that produce a result larger than their operands with:
6286 // shuffle(concat(v1, undef), concat(v2, undef))
6287 // ->
6288 // shuffle(concat(v1, v2), undef)
6289 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6290 //
6291 // This is useful in the general case, but there are special cases where
6292 // native shuffles produce larger results: the two-result ops.
6293 //
6294 // Look through the concat when lowering them:
6295 // shuffle(concat(v1, v2), undef)
6296 // ->
6297 // concat(VZIP(v1, v2):0, :1)
6298 //
Sanjay Patel57195842016-03-14 17:28:46 +00006299 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006300 SDValue SubV1 = V1->getOperand(0);
6301 SDValue SubV2 = V1->getOperand(1);
6302 EVT SubVT = SubV1.getValueType();
6303
6304 // We expect these to have been canonicalized to -1.
6305 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
6306 return i < (int)VT.getVectorNumElements();
6307 }) && "Unexpected shuffle index into UNDEF operand!");
6308
6309 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6310 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6311 if (isV_UNDEF)
6312 SubV2 = SubV1;
6313 assert((WhichResult == 0) &&
6314 "In-place shuffle of concat can only have one result!");
6315 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6316 SubV1, SubV2);
6317 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6318 Res.getValue(1));
6319 }
6320 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006321 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006322
Bob Wilsona7062312009-08-21 20:54:19 +00006323 // If the shuffle is not directly supported and it has 4 elements, use
6324 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006325 unsigned NumElts = VT.getVectorNumElements();
6326 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006327 unsigned PFIndexes[4];
6328 for (unsigned i = 0; i != 4; ++i) {
6329 if (ShuffleMask[i] < 0)
6330 PFIndexes[i] = 8;
6331 else
6332 PFIndexes[i] = ShuffleMask[i];
6333 }
6334
6335 // Compute the index in the perfect shuffle table.
6336 unsigned PFTableIndex =
6337 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006338 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6339 unsigned Cost = (PFEntry >> 30);
6340
6341 if (Cost <= 4)
6342 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6343 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006344
Bob Wilsond8a9a042010-06-04 00:04:02 +00006345 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006346 if (EltSize >= 32) {
6347 // Do the expansion with floating-point types, since that is what the VFP
6348 // registers are defined to use, and since i64 is not legal.
6349 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6350 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006351 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6352 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006353 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006354 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006355 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006356 Ops.push_back(DAG.getUNDEF(EltVT));
6357 else
6358 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6359 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6360 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006361 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006362 }
Craig Topper48d114b2014-04-26 18:35:24 +00006363 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006364 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006365 }
6366
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006367 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6368 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6369
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006370 if (VT == MVT::v8i8)
6371 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006372 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006373
Bob Wilson6f34e272009-08-14 05:16:33 +00006374 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006375}
6376
Eli Friedmana5e244c2011-10-24 23:08:52 +00006377static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6378 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6379 SDValue Lane = Op.getOperand(2);
6380 if (!isa<ConstantSDNode>(Lane))
6381 return SDValue();
6382
6383 return Op;
6384}
6385
Bob Wilson2e076c42009-06-22 23:27:02 +00006386static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006387 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006388 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006389 if (!isa<ConstantSDNode>(Lane))
6390 return SDValue();
6391
6392 SDValue Vec = Op.getOperand(0);
6393 if (Op.getValueType() == MVT::i32 &&
6394 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006395 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006396 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6397 }
6398
6399 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006400}
6401
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006402static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6403 // The only time a CONCAT_VECTORS operation can have legal types is when
6404 // two 64-bit vectors are concatenated to a 128-bit vector.
6405 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6406 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006407 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006408 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006409 SDValue Op0 = Op.getOperand(0);
6410 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006411 if (!Op0.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006412 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006413 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006414 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006415 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006416 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006417 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006418 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006419 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006420}
6421
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006422/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6423/// element has been zero/sign-extended, depending on the isSigned parameter,
6424/// from an integer type half its size.
6425static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6426 bool isSigned) {
6427 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6428 EVT VT = N->getValueType(0);
6429 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6430 SDNode *BVN = N->getOperand(0).getNode();
6431 if (BVN->getValueType(0) != MVT::v4i32 ||
6432 BVN->getOpcode() != ISD::BUILD_VECTOR)
6433 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006434 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006435 unsigned HiElt = 1 - LoElt;
6436 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6437 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6438 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6439 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6440 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6441 return false;
6442 if (isSigned) {
6443 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6444 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6445 return true;
6446 } else {
6447 if (Hi0->isNullValue() && Hi1->isNullValue())
6448 return true;
6449 }
6450 return false;
6451 }
6452
6453 if (N->getOpcode() != ISD::BUILD_VECTOR)
6454 return false;
6455
6456 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6457 SDNode *Elt = N->getOperand(i).getNode();
6458 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6459 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6460 unsigned HalfSize = EltSize / 2;
6461 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006462 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006463 return false;
6464 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006465 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006466 return false;
6467 }
6468 continue;
6469 }
6470 return false;
6471 }
6472
6473 return true;
6474}
6475
6476/// isSignExtended - Check if a node is a vector value that is sign-extended
6477/// or a constant BUILD_VECTOR with sign-extended elements.
6478static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6479 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6480 return true;
6481 if (isExtendedBUILD_VECTOR(N, DAG, true))
6482 return true;
6483 return false;
6484}
6485
6486/// isZeroExtended - Check if a node is a vector value that is zero-extended
6487/// or a constant BUILD_VECTOR with zero-extended elements.
6488static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6489 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6490 return true;
6491 if (isExtendedBUILD_VECTOR(N, DAG, false))
6492 return true;
6493 return false;
6494}
6495
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006496static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6497 if (OrigVT.getSizeInBits() >= 64)
6498 return OrigVT;
6499
6500 assert(OrigVT.isSimple() && "Expecting a simple value type");
6501
6502 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6503 switch (OrigSimpleTy) {
6504 default: llvm_unreachable("Unexpected Vector Type");
6505 case MVT::v2i8:
6506 case MVT::v2i16:
6507 return MVT::v2i32;
6508 case MVT::v4i8:
6509 return MVT::v4i16;
6510 }
6511}
6512
Sebastian Popa204f722012-11-30 19:08:04 +00006513/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6514/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6515/// We insert the required extension here to get the vector to fill a D register.
6516static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6517 const EVT &OrigTy,
6518 const EVT &ExtTy,
6519 unsigned ExtOpcode) {
6520 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6521 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6522 // 64-bits we need to insert a new extension so that it will be 64-bits.
6523 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6524 if (OrigTy.getSizeInBits() >= 64)
6525 return N;
6526
6527 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006528 EVT NewVT = getExtensionTo64Bits(OrigTy);
6529
Andrew Trickef9de2a2013-05-25 02:42:55 +00006530 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006531}
6532
6533/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6534/// does not do any sign/zero extension. If the original vector is less
6535/// than 64 bits, an appropriate extension will be added after the load to
6536/// reach a total size of 64 bits. We have to add the extension separately
6537/// because ARM does not have a sign/zero extending load for vectors.
6538static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006539 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6540
6541 // The load already has the right type.
6542 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006543 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Justin Lebar9c375812016-07-15 18:27:10 +00006544 LD->getBasePtr(), LD->getPointerInfo(),
6545 LD->getAlignment(), LD->getMemOperand()->getFlags());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006546
6547 // We need to create a zextload/sextload. We cannot just create a load
6548 // followed by a zext/zext node because LowerMUL is also run during normal
6549 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006550 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006551 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00006552 LD->getMemoryVT(), LD->getAlignment(),
6553 LD->getMemOperand()->getFlags());
Sebastian Popa204f722012-11-30 19:08:04 +00006554}
6555
6556/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6557/// extending load, or BUILD_VECTOR with extended elements, return the
6558/// unextended value. The unextended vector should be 64 bits so that it can
6559/// be used as an operand to a VMULL instruction. If the original vector size
6560/// before extension is less than 64 bits we add a an extension to resize
6561/// the vector to 64 bits.
6562static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006563 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006564 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6565 N->getOperand(0)->getValueType(0),
6566 N->getValueType(0),
6567 N->getOpcode());
6568
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006569 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006570 return SkipLoadExtensionForVMULL(LD, DAG);
6571
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006572 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6573 // have been legalized as a BITCAST from v4i32.
6574 if (N->getOpcode() == ISD::BITCAST) {
6575 SDNode *BVN = N->getOperand(0).getNode();
6576 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6577 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006578 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006579 return DAG.getBuildVector(
6580 MVT::v2i32, SDLoc(N),
6581 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)});
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006582 }
6583 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6584 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6585 EVT VT = N->getValueType(0);
6586 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6587 unsigned NumElts = VT.getVectorNumElements();
6588 MVT TruncVT = MVT::getIntegerVT(EltSize);
6589 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006590 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006591 for (unsigned i = 0; i != NumElts; ++i) {
6592 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6593 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006594 // Element types smaller than 32 bits are not legal, so use i32 elements.
6595 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006596 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006597 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006598 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006599}
6600
Evan Chenge2086e72011-03-29 01:56:09 +00006601static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6602 unsigned Opcode = N->getOpcode();
6603 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6604 SDNode *N0 = N->getOperand(0).getNode();
6605 SDNode *N1 = N->getOperand(1).getNode();
6606 return N0->hasOneUse() && N1->hasOneUse() &&
6607 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6608 }
6609 return false;
6610}
6611
6612static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6613 unsigned Opcode = N->getOpcode();
6614 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6615 SDNode *N0 = N->getOperand(0).getNode();
6616 SDNode *N1 = N->getOperand(1).getNode();
6617 return N0->hasOneUse() && N1->hasOneUse() &&
6618 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6619 }
6620 return false;
6621}
6622
Bob Wilson38ab35a2010-09-01 23:50:19 +00006623static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6624 // Multiplications are only custom-lowered for 128-bit vectors so that
6625 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6626 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006627 assert(VT.is128BitVector() && VT.isInteger() &&
6628 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006629 SDNode *N0 = Op.getOperand(0).getNode();
6630 SDNode *N1 = Op.getOperand(1).getNode();
6631 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006632 bool isMLA = false;
6633 bool isN0SExt = isSignExtended(N0, DAG);
6634 bool isN1SExt = isSignExtended(N1, DAG);
6635 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006636 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006637 else {
6638 bool isN0ZExt = isZeroExtended(N0, DAG);
6639 bool isN1ZExt = isZeroExtended(N1, DAG);
6640 if (isN0ZExt && isN1ZExt)
6641 NewOpc = ARMISD::VMULLu;
6642 else if (isN1SExt || isN1ZExt) {
6643 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6644 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6645 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6646 NewOpc = ARMISD::VMULLs;
6647 isMLA = true;
6648 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6649 NewOpc = ARMISD::VMULLu;
6650 isMLA = true;
6651 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6652 std::swap(N0, N1);
6653 NewOpc = ARMISD::VMULLu;
6654 isMLA = true;
6655 }
6656 }
6657
6658 if (!NewOpc) {
6659 if (VT == MVT::v2i64)
6660 // Fall through to expand this. It is not legal.
6661 return SDValue();
6662 else
6663 // Other vector multiplications are legal.
6664 return Op;
6665 }
6666 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006667
6668 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006669 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006670 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006671 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006672 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006673 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006674 assert(Op0.getValueType().is64BitVector() &&
6675 Op1.getValueType().is64BitVector() &&
6676 "unexpected types for extended operands to VMULL");
6677 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6678 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006679
Evan Chenge2086e72011-03-29 01:56:09 +00006680 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6681 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6682 // vmull q0, d4, d6
6683 // vmlal q0, d5, d6
6684 // is faster than
6685 // vaddl q0, d4, d5
6686 // vmovl q1, d6
6687 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006688 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6689 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006690 EVT Op1VT = Op1.getValueType();
6691 return DAG.getNode(N0->getOpcode(), DL, VT,
6692 DAG.getNode(NewOpc, DL, VT,
6693 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6694 DAG.getNode(NewOpc, DL, VT,
6695 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006696}
6697
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006698static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl,
6699 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006700 // TODO: Should this propagate fast-math-flags?
6701
Nate Begemanfa62d502011-02-11 20:53:29 +00006702 // Convert to float
6703 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6704 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6705 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6706 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6707 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6708 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6709 // Get reciprocal estimate.
6710 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006711 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006712 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6713 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006714 // Because char has a smaller range than uchar, we can actually get away
6715 // without any newton steps. This requires that we use a weird bias
6716 // of 0xb000, however (again, this has been exhaustively tested).
6717 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6718 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6719 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006720 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006721 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6722 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6723 // Convert back to short.
6724 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6725 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6726 return X;
6727}
6728
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006729static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl,
6730 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006731 // TODO: Should this propagate fast-math-flags?
6732
Nate Begemanfa62d502011-02-11 20:53:29 +00006733 SDValue N2;
6734 // Convert to float.
6735 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6736 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6737 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6738 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6739 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6740 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006741
Nate Begemanfa62d502011-02-11 20:53:29 +00006742 // Use reciprocal estimate and one refinement step.
6743 // float4 recip = vrecpeq_f32(yf);
6744 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006745 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006746 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6747 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006748 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006749 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006750 N1, N2);
6751 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6752 // Because short has a smaller range than ushort, we can actually get away
6753 // with only a single newton step. This requires that we use a weird bias
6754 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006755 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006756 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6757 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006758 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006759 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6760 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6761 // Convert back to integer and return.
6762 // return vmovn_s32(vcvt_s32_f32(result));
6763 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6764 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6765 return N0;
6766}
6767
6768static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6769 EVT VT = Op.getValueType();
6770 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6771 "unexpected type for custom-lowering ISD::SDIV");
6772
Andrew Trickef9de2a2013-05-25 02:42:55 +00006773 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006774 SDValue N0 = Op.getOperand(0);
6775 SDValue N1 = Op.getOperand(1);
6776 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006777
Nate Begemanfa62d502011-02-11 20:53:29 +00006778 if (VT == MVT::v8i8) {
6779 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6780 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006781
Nate Begemanfa62d502011-02-11 20:53:29 +00006782 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006783 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006784 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006785 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006786 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006787 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006788 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006789 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006790
6791 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6792 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6793
6794 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6795 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006796
Nate Begemanfa62d502011-02-11 20:53:29 +00006797 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6798 return N0;
6799 }
6800 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6801}
6802
6803static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006804 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006805 EVT VT = Op.getValueType();
6806 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6807 "unexpected type for custom-lowering ISD::UDIV");
6808
Andrew Trickef9de2a2013-05-25 02:42:55 +00006809 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006810 SDValue N0 = Op.getOperand(0);
6811 SDValue N1 = Op.getOperand(1);
6812 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006813
Nate Begemanfa62d502011-02-11 20:53:29 +00006814 if (VT == MVT::v8i8) {
6815 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6816 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006817
Nate Begemanfa62d502011-02-11 20:53:29 +00006818 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006819 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006820 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006821 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006822 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006823 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006824 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006825 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006826
Nate Begemanfa62d502011-02-11 20:53:29 +00006827 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6828 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006829
Nate Begemanfa62d502011-02-11 20:53:29 +00006830 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6831 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006832
6833 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006834 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6835 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006836 N0);
6837 return N0;
6838 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006839
Nate Begemanfa62d502011-02-11 20:53:29 +00006840 // v4i16 sdiv ... Convert to float.
6841 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6842 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6843 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6844 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6845 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006846 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006847
6848 // Use reciprocal estimate and two refinement steps.
6849 // float4 recip = vrecpeq_f32(yf);
6850 // recip *= vrecpsq_f32(yf, recip);
6851 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006852 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006853 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6854 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006855 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006856 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006857 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006858 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006859 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006860 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006861 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006862 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6863 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6864 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6865 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006866 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006867 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6868 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006869 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006870 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6871 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6872 // Convert back to integer and return.
6873 // return vmovn_u32(vcvt_s32_f32(result));
6874 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6875 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6876 return N0;
6877}
6878
Evan Chenge8916542011-08-30 01:34:54 +00006879static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6880 EVT VT = Op.getNode()->getValueType(0);
6881 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6882
6883 unsigned Opc;
6884 bool ExtraOp = false;
6885 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006886 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006887 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6888 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6889 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6890 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6891 }
6892
6893 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006894 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006895 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006896 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006897 Op.getOperand(1), Op.getOperand(2));
6898}
6899
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006900SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6901 assert(Subtarget->isTargetDarwin());
6902
6903 // For iOS, we want to call an alternative entry point: __sincos_stret,
6904 // return values are passed via sret.
6905 SDLoc dl(Op);
6906 SDValue Arg = Op.getOperand(0);
6907 EVT ArgVT = Arg.getValueType();
6908 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006909 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006910
6911 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006912 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006913
6914 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006915 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006916 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006917
6918 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006919 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6920 SDValue SRet;
6921 if (ShouldUseSRet) {
6922 // Create stack object for sret.
6923 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6924 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6925 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6926 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6927
6928 ArgListEntry Entry;
6929 Entry.Node = SRet;
6930 Entry.Ty = RetTy->getPointerTo();
6931 Entry.isSExt = false;
6932 Entry.isZExt = false;
6933 Entry.isSRet = true;
6934 Args.push_back(Entry);
6935 RetTy = Type::getVoidTy(*DAG.getContext());
6936 }
6937
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006938 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006939 Entry.Node = Arg;
6940 Entry.Ty = ArgTy;
6941 Entry.isSExt = false;
6942 Entry.isZExt = false;
6943 Args.push_back(Entry);
6944
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006945 const char *LibcallName =
6946 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006947 RTLIB::Libcall LC =
6948 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6949 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006950 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006951
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006952 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006953 CLI.setDebugLoc(dl)
6954 .setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00006955 .setCallee(CC, RetTy, Callee, std::move(Args))
Tim Northover8b403662015-10-28 22:51:16 +00006956 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006957 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6958
Tim Northover8b403662015-10-28 22:51:16 +00006959 if (!ShouldUseSRet)
6960 return CallResult.first;
6961
Justin Lebar9c375812016-07-15 18:27:10 +00006962 SDValue LoadSin =
6963 DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006964
6965 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006966 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006967 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Justin Lebar9c375812016-07-15 18:27:10 +00006968 SDValue LoadCos =
6969 DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006970
6971 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6972 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6973 LoadSin.getValue(0), LoadCos.getValue(0));
6974}
6975
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006976SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006977 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006978 SDValue &Chain) const {
6979 EVT VT = Op.getValueType();
6980 assert((VT == MVT::i32 || VT == MVT::i64) &&
6981 "unexpected type for custom lowering DIV");
6982 SDLoc dl(Op);
6983
6984 const auto &DL = DAG.getDataLayout();
6985 const auto &TLI = DAG.getTargetLoweringInfo();
6986
6987 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006988 if (Signed)
6989 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6990 else
6991 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006992
6993 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6994
6995 ARMTargetLowering::ArgListTy Args;
6996
6997 for (auto AI : {1, 0}) {
6998 ArgListEntry Arg;
6999 Arg.Node = Op.getOperand(AI);
7000 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
7001 Args.push_back(Arg);
7002 }
7003
7004 CallLoweringInfo CLI(DAG);
7005 CLI.setDebugLoc(dl)
7006 .setChain(Chain)
7007 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00007008 ES, std::move(Args));
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007009
7010 return LowerCallTo(CLI).first;
7011}
7012
Martell Maloned1229242015-11-26 15:34:03 +00007013SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
7014 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00007015 assert(Op.getValueType() == MVT::i32 &&
7016 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007017 SDLoc dl(Op);
7018
7019 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
7020 DAG.getEntryNode(), Op.getOperand(1));
7021
Martell Maloned1229242015-11-26 15:34:03 +00007022 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007023}
7024
7025void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00007026 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007027 SmallVectorImpl<SDValue> &Results) const {
7028 const auto &DL = DAG.getDataLayout();
7029 const auto &TLI = DAG.getTargetLoweringInfo();
7030
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00007031 assert(Op.getValueType() == MVT::i64 &&
7032 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007033 SDLoc dl(Op);
7034
7035 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
7036 DAG.getConstant(0, dl, MVT::i32));
7037 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
7038 DAG.getConstant(1, dl, MVT::i32));
7039 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
7040
7041 SDValue DBZCHK =
7042 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
7043
Martell Maloned1229242015-11-26 15:34:03 +00007044 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007045
7046 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
7047 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
7048 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
7049 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
7050
7051 Results.push_back(Lower);
7052 Results.push_back(Upper);
7053}
7054
Eli Friedman10f9ce22011-09-15 22:26:18 +00007055static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00007056 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
7057 // Acquire/Release load/store is not legal for targets without a dmb or
7058 // equivalent available.
7059 return SDValue();
Eli Friedmanba912e02011-09-15 22:18:49 +00007060
JF Bastien800f87a2016-04-06 21:19:33 +00007061 // Monotonic load/store is legal for all targets.
7062 return Op;
Eli Friedmanba912e02011-09-15 22:18:49 +00007063}
7064
Tim Northoverbc933082013-05-23 19:11:20 +00007065static void ReplaceREADCYCLECOUNTER(SDNode *N,
7066 SmallVectorImpl<SDValue> &Results,
7067 SelectionDAG &DAG,
7068 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007069 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00007070 // Under Power Management extensions, the cycle-count is:
7071 // mrc p15, #0, <Rt>, c9, c13, #0
7072 SDValue Ops[] = { N->getOperand(0), // Chain
7073 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
7074 DAG.getConstant(15, DL, MVT::i32),
7075 DAG.getConstant(0, DL, MVT::i32),
7076 DAG.getConstant(9, DL, MVT::i32),
7077 DAG.getConstant(13, DL, MVT::i32),
7078 DAG.getConstant(0, DL, MVT::i32)
7079 };
Tim Northoverbc933082013-05-23 19:11:20 +00007080
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00007081 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
7082 DAG.getVTList(MVT::i32, MVT::Other), Ops);
7083 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
7084 DAG.getConstant(0, DL, MVT::i32)));
7085 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00007086}
7087
Tim Northover1ee27c72016-04-19 22:25:02 +00007088static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) {
7089 SDLoc dl(V.getNode());
7090 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32);
7091 SDValue VHi = DAG.getAnyExtOrTrunc(
7092 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)),
7093 dl, MVT::i32);
Tim Northoverb629c772016-04-18 21:48:55 +00007094 SDValue RegClass =
7095 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
7096 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
7097 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
Tim Northover1ee27c72016-04-19 22:25:02 +00007098 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
Tim Northoverb629c772016-04-18 21:48:55 +00007099 return SDValue(
7100 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
7101}
7102
7103static void ReplaceCMP_SWAP_64Results(SDNode *N,
7104 SmallVectorImpl<SDValue> & Results,
7105 SelectionDAG &DAG) {
7106 assert(N->getValueType(0) == MVT::i64 &&
7107 "AtomicCmpSwap on types less than 64 should be legal");
7108 SDValue Ops[] = {N->getOperand(1),
Tim Northover1ee27c72016-04-19 22:25:02 +00007109 createGPRPairNode(DAG, N->getOperand(2)),
7110 createGPRPairNode(DAG, N->getOperand(3)),
Tim Northoverb629c772016-04-18 21:48:55 +00007111 N->getOperand(0)};
7112 SDNode *CmpSwap = DAG.getMachineNode(
7113 ARM::CMP_SWAP_64, SDLoc(N),
7114 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
7115
7116 MachineFunction &MF = DAG.getMachineFunction();
7117 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
7118 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
7119 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
7120
7121 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_0, SDLoc(N), MVT::i32,
7122 SDValue(CmpSwap, 0)));
7123 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_1, SDLoc(N), MVT::i32,
7124 SDValue(CmpSwap, 0)));
7125 Results.push_back(SDValue(CmpSwap, 2));
7126}
7127
Dan Gohman21cea8a2010-04-17 15:26:15 +00007128SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007129 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007130 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007131 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007132 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00007133 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00007134 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00007135 switch (Subtarget->getTargetTriple().getObjectFormat()) {
7136 default: llvm_unreachable("unknown object format");
7137 case Triple::COFF:
7138 return LowerGlobalAddressWindows(Op, DAG);
7139 case Triple::ELF:
7140 return LowerGlobalAddressELF(Op, DAG);
7141 case Triple::MachO:
7142 return LowerGlobalAddressDarwin(Op, DAG);
7143 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00007144 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00007145 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007146 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
7147 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007148 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00007149 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00007150 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00007151 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00007152 case ISD::SINT_TO_FP:
7153 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
7154 case ISD::FP_TO_SINT:
7155 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007156 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00007157 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00007158 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00007159 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00007160 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00007161 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00007162 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
7163 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00007164 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007165 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00007166 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00007167 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00007168 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
7169 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007170 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00007171 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007172 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007173 case ISD::CTTZ:
7174 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007175 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007176 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00007177 case ISD::SETCCE: return LowerSETCCE(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007178 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007179 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007180 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007181 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007182 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007183 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007184 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007185 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00007186 case ISD::SDIV:
7187 if (Subtarget->isTargetWindows())
7188 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
7189 return LowerSDIV(Op, DAG);
7190 case ISD::UDIV:
7191 if (Subtarget->isTargetWindows())
7192 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
7193 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007194 case ISD::ADDC:
7195 case ISD::ADDE:
7196 case ISD::SUBC:
7197 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007198 case ISD::SADDO:
7199 case ISD::UADDO:
7200 case ISD::SSUBO:
7201 case ISD::USUBO:
7202 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007203 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007204 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007205 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007206 case ISD::SDIVREM:
7207 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007208 case ISD::DYNAMIC_STACKALLOC:
7209 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7210 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7211 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007212 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7213 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007214 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007215 }
Evan Cheng10043e22007-01-19 07:51:42 +00007216}
7217
Duncan Sands6ed40142008-12-01 11:39:25 +00007218/// ReplaceNodeResults - Replace the results of node with an illegal result
7219/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007220void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007221 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007222 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007223 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007224 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007225 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007226 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007227 case ISD::READ_REGISTER:
7228 ExpandREAD_REGISTER(N, Results, DAG);
7229 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007230 case ISD::BITCAST:
7231 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007232 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007233 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007234 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007235 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007236 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007237 case ISD::SREM:
7238 case ISD::UREM:
7239 Res = LowerREM(N, DAG);
7240 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007241 case ISD::SDIVREM:
7242 case ISD::UDIVREM:
7243 Res = LowerDivRem(SDValue(N, 0), DAG);
7244 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7245 Results.push_back(Res.getValue(0));
7246 Results.push_back(Res.getValue(1));
7247 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007248 case ISD::READCYCLECOUNTER:
7249 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7250 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007251 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007252 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007253 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007254 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7255 Results);
Tim Northoverb629c772016-04-18 21:48:55 +00007256 case ISD::ATOMIC_CMP_SWAP:
7257 ReplaceCMP_SWAP_64Results(N, Results, DAG);
7258 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00007259 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007260 if (Res.getNode())
7261 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007262}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007263
Evan Cheng10043e22007-01-19 07:51:42 +00007264//===----------------------------------------------------------------------===//
7265// ARM Scheduler Hooks
7266//===----------------------------------------------------------------------===//
7267
Bill Wendling030b58e2011-10-06 22:18:16 +00007268/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7269/// registers the function context.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007270void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI,
7271 MachineBasicBlock *MBB,
7272 MachineBasicBlock *DispatchBB,
7273 int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007274 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007275 DebugLoc dl = MI.getDebugLoc();
Bill Wendling374ee192011-10-03 21:25:38 +00007276 MachineFunction *MF = MBB->getParent();
7277 MachineRegisterInfo *MRI = &MF->getRegInfo();
7278 MachineConstantPool *MCP = MF->getConstantPool();
7279 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7280 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007281
Bill Wendling374ee192011-10-03 21:25:38 +00007282 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007283 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007284
Bill Wendling374ee192011-10-03 21:25:38 +00007285 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007286 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007287 ARMConstantPoolValue *CPV =
7288 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7289 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7290
Craig Topper61e88f42014-11-21 05:58:21 +00007291 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7292 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007293
Bill Wendling030b58e2011-10-06 22:18:16 +00007294 // Grab constant pool and fixed stack memory operands.
7295 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007296 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7297 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007298
7299 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007300 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7301 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007302
7303 // Load the address of the dispatch MBB into the jump buffer.
7304 if (isThumb2) {
7305 // Incoming value: jbuf
7306 // ldr.n r5, LCPI1_1
7307 // orr r5, r5, #1
7308 // add r5, pc
7309 // str r5, [$jbuf, #+4] ; &jbuf[1]
7310 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7311 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7312 .addConstantPoolIndex(CPI)
7313 .addMemOperand(CPMMO));
7314 // Set the low bit because of thumb mode.
7315 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7316 AddDefaultCC(
7317 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7318 .addReg(NewVReg1, RegState::Kill)
7319 .addImm(0x01)));
7320 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7321 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7322 .addReg(NewVReg2, RegState::Kill)
7323 .addImm(PCLabelId);
7324 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7325 .addReg(NewVReg3, RegState::Kill)
7326 .addFrameIndex(FI)
7327 .addImm(36) // &jbuf[1] :: pc
7328 .addMemOperand(FIMMOSt));
7329 } else if (isThumb) {
7330 // Incoming value: jbuf
7331 // ldr.n r1, LCPI1_4
7332 // add r1, pc
7333 // mov r2, #1
7334 // orrs r1, r2
7335 // add r2, $jbuf, #+4 ; &jbuf[1]
7336 // str r1, [r2]
7337 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7338 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7339 .addConstantPoolIndex(CPI)
7340 .addMemOperand(CPMMO));
7341 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7342 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7343 .addReg(NewVReg1, RegState::Kill)
7344 .addImm(PCLabelId);
7345 // Set the low bit because of thumb mode.
7346 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7347 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7348 .addReg(ARM::CPSR, RegState::Define)
7349 .addImm(1));
7350 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7351 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7352 .addReg(ARM::CPSR, RegState::Define)
7353 .addReg(NewVReg2, RegState::Kill)
7354 .addReg(NewVReg3, RegState::Kill));
7355 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007356 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7357 .addFrameIndex(FI)
7358 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007359 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7360 .addReg(NewVReg4, RegState::Kill)
7361 .addReg(NewVReg5, RegState::Kill)
7362 .addImm(0)
7363 .addMemOperand(FIMMOSt));
7364 } else {
7365 // Incoming value: jbuf
7366 // ldr r1, LCPI1_1
7367 // add r1, pc, r1
7368 // str r1, [$jbuf, #+4] ; &jbuf[1]
7369 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7370 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7371 .addConstantPoolIndex(CPI)
7372 .addImm(0)
7373 .addMemOperand(CPMMO));
7374 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7375 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7376 .addReg(NewVReg1, RegState::Kill)
7377 .addImm(PCLabelId));
7378 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7379 .addReg(NewVReg2, RegState::Kill)
7380 .addFrameIndex(FI)
7381 .addImm(36) // &jbuf[1] :: pc
7382 .addMemOperand(FIMMOSt));
7383 }
7384}
7385
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007386void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI,
Matthias Brauneec4efc2015-04-28 00:37:05 +00007387 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007388 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007389 DebugLoc dl = MI.getDebugLoc();
Bill Wendling030b58e2011-10-06 22:18:16 +00007390 MachineFunction *MF = MBB->getParent();
7391 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007392 MachineFrameInfo *MFI = MF->getFrameInfo();
7393 int FI = MFI->getFunctionContextIndex();
7394
Craig Topper61e88f42014-11-21 05:58:21 +00007395 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7396 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007397
Bill Wendling362c1b02011-10-06 21:29:56 +00007398 // Get a mapping of the call site numbers to all of the landing pads they're
7399 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007400 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7401 unsigned MaxCSNum = 0;
7402 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007403 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7404 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007405 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007406
7407 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7408 // pad.
7409 for (MachineBasicBlock::iterator
7410 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7411 if (!II->isEHLabel()) continue;
7412
7413 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007414 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007415
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007416 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7417 for (SmallVectorImpl<unsigned>::iterator
7418 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7419 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007420 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007421 MaxCSNum = std::max(MaxCSNum, *CSI);
7422 }
Bill Wendling202803e2011-10-05 00:02:33 +00007423 break;
7424 }
7425 }
7426
7427 // Get an ordered list of the machine basic blocks for the jump table.
7428 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007429 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007430 LPadList.reserve(CallSiteNumToLPad.size());
7431 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7432 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7433 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007434 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007435 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007436 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7437 }
Bill Wendling202803e2011-10-05 00:02:33 +00007438 }
7439
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007440 assert(!LPadList.empty() &&
7441 "No landing pad destinations for the dispatch jump table!");
7442
Bill Wendling362c1b02011-10-06 21:29:56 +00007443 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007444 MachineJumpTableInfo *JTI =
7445 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7446 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Bill Wendling202803e2011-10-05 00:02:33 +00007447
Bill Wendling362c1b02011-10-06 21:29:56 +00007448 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007449
7450 // Shove the dispatch's address into the return slot in the function context.
7451 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007452 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007453
Bill Wendling324be982011-10-05 00:39:32 +00007454 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007455 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007456 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007457 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007458 else
7459 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7460
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007461 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007462 DispatchBB->addSuccessor(TrapBB);
7463
7464 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7465 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007466
Bill Wendling510fbcd2011-10-17 21:32:56 +00007467 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007468 MF->insert(MF->end(), DispatchBB);
7469 MF->insert(MF->end(), DispContBB);
7470 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007471
Bill Wendling030b58e2011-10-06 22:18:16 +00007472 // Insert code into the entry block that creates and registers the function
7473 // context.
7474 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7475
Alex Lorenze40c8a22015-08-11 23:09:45 +00007476 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7477 MachinePointerInfo::getFixedStack(*MF, FI),
7478 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007479
Chad Rosier1ec8e402012-11-06 23:05:24 +00007480 MachineInstrBuilder MIB;
7481 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7482
7483 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7484 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7485
7486 // Add a register mask with no preserved registers. This results in all
7487 // registers being marked as clobbered.
7488 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007489
Rafael Espindola0f898332016-06-20 16:43:17 +00007490 bool IsPositionIndependent = isPositionIndependent();
Bill Wendling85833f72011-10-18 22:49:07 +00007491 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007492 if (Subtarget->isThumb2()) {
7493 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7494 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7495 .addFrameIndex(FI)
7496 .addImm(4)
7497 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007498
Bill Wendling85833f72011-10-18 22:49:07 +00007499 if (NumLPads < 256) {
7500 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7501 .addReg(NewVReg1)
7502 .addImm(LPadList.size()));
7503 } else {
7504 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7505 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007506 .addImm(NumLPads & 0xFFFF));
7507
7508 unsigned VReg2 = VReg1;
7509 if ((NumLPads & 0xFFFF0000) != 0) {
7510 VReg2 = MRI->createVirtualRegister(TRC);
7511 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7512 .addReg(VReg1)
7513 .addImm(NumLPads >> 16));
7514 }
7515
Bill Wendling85833f72011-10-18 22:49:07 +00007516 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7517 .addReg(NewVReg1)
7518 .addReg(VReg2));
7519 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007520
Bill Wendling5626c662011-10-06 22:53:00 +00007521 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7522 .addMBB(TrapBB)
7523 .addImm(ARMCC::HI)
7524 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007525
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007526 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7527 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007528 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007529
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007530 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007531 AddDefaultCC(
7532 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007533 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7534 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007535 .addReg(NewVReg1)
7536 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7537
7538 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007539 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007540 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007541 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007542 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007543 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7544 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7545 .addFrameIndex(FI)
7546 .addImm(1)
7547 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007548
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007549 if (NumLPads < 256) {
7550 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7551 .addReg(NewVReg1)
7552 .addImm(NumLPads));
7553 } else {
7554 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007555 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7556 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7557
7558 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007559 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007560 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007561 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007562 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007563
7564 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7565 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7566 .addReg(VReg1, RegState::Define)
7567 .addConstantPoolIndex(Idx));
7568 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7569 .addReg(NewVReg1)
7570 .addReg(VReg1));
7571 }
7572
Bill Wendlingb3d46782011-10-06 23:37:36 +00007573 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7574 .addMBB(TrapBB)
7575 .addImm(ARMCC::HI)
7576 .addReg(ARM::CPSR);
7577
7578 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7579 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7580 .addReg(ARM::CPSR, RegState::Define)
7581 .addReg(NewVReg1)
7582 .addImm(2));
7583
7584 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007585 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007586 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007587
7588 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7589 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7590 .addReg(ARM::CPSR, RegState::Define)
7591 .addReg(NewVReg2, RegState::Kill)
7592 .addReg(NewVReg3));
7593
Alex Lorenze40c8a22015-08-11 23:09:45 +00007594 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7595 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007596
7597 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7598 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7599 .addReg(NewVReg4, RegState::Kill)
7600 .addImm(0)
7601 .addMemOperand(JTMMOLd));
7602
Chad Rosier96603432013-03-01 18:30:38 +00007603 unsigned NewVReg6 = NewVReg5;
Rafael Espindola0f898332016-06-20 16:43:17 +00007604 if (IsPositionIndependent) {
Chad Rosier96603432013-03-01 18:30:38 +00007605 NewVReg6 = MRI->createVirtualRegister(TRC);
7606 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7607 .addReg(ARM::CPSR, RegState::Define)
7608 .addReg(NewVReg5, RegState::Kill)
7609 .addReg(NewVReg3));
7610 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007611
7612 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7613 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007614 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007615 } else {
7616 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7617 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7618 .addFrameIndex(FI)
7619 .addImm(4)
7620 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007621
Bill Wendling4969dcd2011-10-18 22:52:20 +00007622 if (NumLPads < 256) {
7623 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7624 .addReg(NewVReg1)
7625 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007626 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007627 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7628 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007629 .addImm(NumLPads & 0xFFFF));
7630
7631 unsigned VReg2 = VReg1;
7632 if ((NumLPads & 0xFFFF0000) != 0) {
7633 VReg2 = MRI->createVirtualRegister(TRC);
7634 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7635 .addReg(VReg1)
7636 .addImm(NumLPads >> 16));
7637 }
7638
Bill Wendling4969dcd2011-10-18 22:52:20 +00007639 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7640 .addReg(NewVReg1)
7641 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007642 } else {
7643 MachineConstantPool *ConstantPool = MF->getConstantPool();
7644 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7645 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7646
7647 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007648 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007649 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007650 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007651 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7652
7653 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7654 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7655 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007656 .addConstantPoolIndex(Idx)
7657 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007658 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7659 .addReg(NewVReg1)
7660 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007661 }
7662
Bill Wendling5626c662011-10-06 22:53:00 +00007663 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7664 .addMBB(TrapBB)
7665 .addImm(ARMCC::HI)
7666 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007667
Bill Wendling973c8172011-10-18 22:11:18 +00007668 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007669 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007670 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007671 .addReg(NewVReg1)
7672 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007673 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7674 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007675 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007676
Alex Lorenze40c8a22015-08-11 23:09:45 +00007677 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7678 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007679 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007680 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007681 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7682 .addReg(NewVReg3, RegState::Kill)
7683 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007684 .addImm(0)
7685 .addMemOperand(JTMMOLd));
7686
Rafael Espindola0f898332016-06-20 16:43:17 +00007687 if (IsPositionIndependent) {
Chad Rosier96603432013-03-01 18:30:38 +00007688 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7689 .addReg(NewVReg5, RegState::Kill)
7690 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007691 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007692 } else {
7693 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7694 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007695 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007696 }
Bill Wendling5626c662011-10-06 22:53:00 +00007697 }
Bill Wendling202803e2011-10-05 00:02:33 +00007698
Bill Wendling324be982011-10-05 00:39:32 +00007699 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007700 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007701 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007702 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7703 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007704 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007705 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007706 }
7707
Bill Wendling26d27802011-10-17 05:25:09 +00007708 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007709 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007710 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007711 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007712
7713 // Remove the landing pad successor from the invoke block and replace it
7714 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007715 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7716 BB->succ_end());
7717 while (!Successors.empty()) {
7718 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007719 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007720 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007721 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007722 }
7723 }
7724
Cong Houd97c1002015-12-01 05:29:22 +00007725 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00007726 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007727
7728 // Find the invoke call and mark all of the callee-saved registers as
7729 // 'implicit defined' so that they're spilled. This prevents code from
7730 // moving instructions to before the EH block, where they will never be
7731 // executed.
7732 for (MachineBasicBlock::reverse_iterator
7733 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007734 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007735
7736 DenseMap<unsigned, bool> DefRegs;
7737 for (MachineInstr::mop_iterator
7738 OI = II->operands_begin(), OE = II->operands_end();
7739 OI != OE; ++OI) {
7740 if (!OI->isReg()) continue;
7741 DefRegs[OI->getReg()] = true;
7742 }
7743
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007744 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007745
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007746 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007747 unsigned Reg = SavedRegs[i];
7748 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007749 !ARM::tGPRRegClass.contains(Reg) &&
7750 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007751 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007752 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007753 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007754 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007755 continue;
7756 if (!DefRegs[Reg])
7757 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007758 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007759
7760 break;
7761 }
Bill Wendling883ec972011-10-07 23:18:02 +00007762 }
Bill Wendling324be982011-10-05 00:39:32 +00007763
Bill Wendling617075f2011-10-18 18:30:49 +00007764 // Mark all former landing pads as non-landing pads. The dispatch is the only
7765 // landing pad now.
7766 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7767 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007768 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007769
Bill Wendling324be982011-10-05 00:39:32 +00007770 // The instruction is gone now.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007771 MI.eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007772}
7773
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007774static
7775MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7776 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7777 E = MBB->succ_end(); I != E; ++I)
7778 if (*I != Succ)
7779 return *I;
7780 llvm_unreachable("Expecting a BB with two successors!");
7781}
7782
Manman Renb504f492013-10-29 22:27:32 +00007783/// Return the load opcode for a given load size. If load size >= 8,
7784/// neon opcode will be returned.
7785static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7786 if (LdSize >= 8)
7787 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7788 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7789 if (IsThumb1)
7790 return LdSize == 4 ? ARM::tLDRi
7791 : LdSize == 2 ? ARM::tLDRHi
7792 : LdSize == 1 ? ARM::tLDRBi : 0;
7793 if (IsThumb2)
7794 return LdSize == 4 ? ARM::t2LDR_POST
7795 : LdSize == 2 ? ARM::t2LDRH_POST
7796 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7797 return LdSize == 4 ? ARM::LDR_POST_IMM
7798 : LdSize == 2 ? ARM::LDRH_POST
7799 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7800}
7801
7802/// Return the store opcode for a given store size. If store size >= 8,
7803/// neon opcode will be returned.
7804static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7805 if (StSize >= 8)
7806 return StSize == 16 ? ARM::VST1q32wb_fixed
7807 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7808 if (IsThumb1)
7809 return StSize == 4 ? ARM::tSTRi
7810 : StSize == 2 ? ARM::tSTRHi
7811 : StSize == 1 ? ARM::tSTRBi : 0;
7812 if (IsThumb2)
7813 return StSize == 4 ? ARM::t2STR_POST
7814 : StSize == 2 ? ARM::t2STRH_POST
7815 : StSize == 1 ? ARM::t2STRB_POST : 0;
7816 return StSize == 4 ? ARM::STR_POST_IMM
7817 : StSize == 2 ? ARM::STRH_POST
7818 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7819}
7820
7821/// Emit a post-increment load operation with given size. The instructions
7822/// will be added to BB at Pos.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007823static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007824 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00007825 unsigned LdSize, unsigned Data, unsigned AddrIn,
7826 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7827 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7828 assert(LdOpc != 0 && "Should have a load opcode");
7829 if (LdSize >= 8) {
7830 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7831 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7832 .addImm(0));
7833 } else if (IsThumb1) {
7834 // load + update AddrIn
7835 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7836 .addReg(AddrIn).addImm(0));
7837 MachineInstrBuilder MIB =
7838 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7839 MIB = AddDefaultT1CC(MIB);
7840 MIB.addReg(AddrIn).addImm(LdSize);
7841 AddDefaultPred(MIB);
7842 } else if (IsThumb2) {
7843 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7844 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7845 .addImm(LdSize));
7846 } else { // arm
7847 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7848 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7849 .addReg(0).addImm(LdSize));
7850 }
7851}
7852
7853/// Emit a post-increment store operation with given size. The instructions
7854/// will be added to BB at Pos.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007855static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007856 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00007857 unsigned StSize, unsigned Data, unsigned AddrIn,
7858 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7859 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7860 assert(StOpc != 0 && "Should have a store opcode");
7861 if (StSize >= 8) {
7862 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7863 .addReg(AddrIn).addImm(0).addReg(Data));
7864 } else if (IsThumb1) {
7865 // store + update AddrIn
7866 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7867 .addReg(AddrIn).addImm(0));
7868 MachineInstrBuilder MIB =
7869 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7870 MIB = AddDefaultT1CC(MIB);
7871 MIB.addReg(AddrIn).addImm(StSize);
7872 AddDefaultPred(MIB);
7873 } else if (IsThumb2) {
7874 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7875 .addReg(Data).addReg(AddrIn).addImm(StSize));
7876 } else { // arm
7877 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7878 .addReg(Data).addReg(AddrIn).addReg(0)
7879 .addImm(StSize));
7880 }
7881}
7882
David Peixottoc32e24a2013-10-17 19:49:22 +00007883MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007884ARMTargetLowering::EmitStructByval(MachineInstr &MI,
David Peixottoc32e24a2013-10-17 19:49:22 +00007885 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007886 // This pseudo instruction has 3 operands: dst, src, size
7887 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7888 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007889 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007890 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007891 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007892
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007893 unsigned dest = MI.getOperand(0).getReg();
7894 unsigned src = MI.getOperand(1).getReg();
7895 unsigned SizeVal = MI.getOperand(2).getImm();
7896 unsigned Align = MI.getOperand(3).getImm();
7897 DebugLoc dl = MI.getDebugLoc();
Manman Rene8735522012-06-01 19:33:18 +00007898
Manman Rene8735522012-06-01 19:33:18 +00007899 MachineFunction *MF = BB->getParent();
7900 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007901 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007902 const TargetRegisterClass *TRC = nullptr;
7903 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007904
7905 bool IsThumb1 = Subtarget->isThumb1Only();
7906 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007907
7908 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007909 UnitSize = 1;
7910 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007911 UnitSize = 2;
7912 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007913 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007914 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007915 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007916 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007917 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007918 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007919 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007920 }
7921 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007922 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007923 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007924 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007925
David Peixottob0653e532013-10-24 16:39:36 +00007926 // Select the correct opcode and register class for unit size load/store
7927 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007928 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007929 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007930 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7931 : UnitSize == 8 ? &ARM::DPRRegClass
7932 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007933
Manman Rene8735522012-06-01 19:33:18 +00007934 unsigned BytesLeft = SizeVal % UnitSize;
7935 unsigned LoopSize = SizeVal - BytesLeft;
7936
7937 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7938 // Use LDR and STR to copy.
7939 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7940 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7941 unsigned srcIn = src;
7942 unsigned destIn = dest;
7943 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007944 unsigned srcOut = MRI.createVirtualRegister(TRC);
7945 unsigned destOut = MRI.createVirtualRegister(TRC);
7946 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007947 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7948 IsThumb1, IsThumb2);
7949 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7950 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007951 srcIn = srcOut;
7952 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007953 }
7954
7955 // Handle the leftover bytes with LDRB and STRB.
7956 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7957 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007958 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007959 unsigned srcOut = MRI.createVirtualRegister(TRC);
7960 unsigned destOut = MRI.createVirtualRegister(TRC);
7961 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007962 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7963 IsThumb1, IsThumb2);
7964 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7965 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007966 srcIn = srcOut;
7967 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007968 }
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007969 MI.eraseFromParent(); // The instruction is gone now.
Manman Rene8735522012-06-01 19:33:18 +00007970 return BB;
7971 }
7972
7973 // Expand the pseudo op to a loop.
7974 // thisMBB:
7975 // ...
7976 // movw varEnd, # --> with thumb2
7977 // movt varEnd, #
7978 // ldrcp varEnd, idx --> without thumb2
7979 // fallthrough --> loopMBB
7980 // loopMBB:
7981 // PHI varPhi, varEnd, varLoop
7982 // PHI srcPhi, src, srcLoop
7983 // PHI destPhi, dst, destLoop
7984 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7985 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7986 // subs varLoop, varPhi, #UnitSize
7987 // bne loopMBB
7988 // fallthrough --> exitMBB
7989 // exitMBB:
7990 // epilogue to handle left-over bytes
7991 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7992 // [destOut] = STRB_POST(scratch, destLoop, 1)
7993 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7994 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7995 MF->insert(It, loopMBB);
7996 MF->insert(It, exitMBB);
7997
7998 // Transfer the remainder of BB and its successor edges to exitMBB.
7999 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008000 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00008001 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
8002
8003 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00008004 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00008005 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00008006 unsigned Vtmp = varEnd;
8007 if ((LoopSize & 0xFFFF0000) != 0)
8008 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00008009 AddDefaultPred(BuildMI(BB, dl,
8010 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
8011 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00008012
8013 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00008014 AddDefaultPred(BuildMI(BB, dl,
8015 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
8016 varEnd)
8017 .addReg(Vtmp)
8018 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00008019 } else {
8020 MachineConstantPool *ConstantPool = MF->getConstantPool();
8021 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
8022 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
8023
8024 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008025 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00008026 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008027 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00008028 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8029
8030 if (IsThumb1)
8031 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
8032 varEnd, RegState::Define).addConstantPoolIndex(Idx));
8033 else
8034 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
8035 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
8036 }
Manman Rene8735522012-06-01 19:33:18 +00008037 BB->addSuccessor(loopMBB);
8038
8039 // Generate the loop body:
8040 // varPhi = PHI(varLoop, varEnd)
8041 // srcPhi = PHI(srcLoop, src)
8042 // destPhi = PHI(destLoop, dst)
8043 MachineBasicBlock *entryBB = BB;
8044 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00008045 unsigned varLoop = MRI.createVirtualRegister(TRC);
8046 unsigned varPhi = MRI.createVirtualRegister(TRC);
8047 unsigned srcLoop = MRI.createVirtualRegister(TRC);
8048 unsigned srcPhi = MRI.createVirtualRegister(TRC);
8049 unsigned destLoop = MRI.createVirtualRegister(TRC);
8050 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00008051
8052 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
8053 .addReg(varLoop).addMBB(loopMBB)
8054 .addReg(varEnd).addMBB(entryBB);
8055 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
8056 .addReg(srcLoop).addMBB(loopMBB)
8057 .addReg(src).addMBB(entryBB);
8058 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
8059 .addReg(destLoop).addMBB(loopMBB)
8060 .addReg(dest).addMBB(entryBB);
8061
8062 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
8063 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00008064 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00008065 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
8066 IsThumb1, IsThumb2);
8067 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
8068 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00008069
8070 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00008071 if (IsThumb1) {
8072 MachineInstrBuilder MIB =
8073 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
8074 MIB = AddDefaultT1CC(MIB);
8075 MIB.addReg(varPhi).addImm(UnitSize);
8076 AddDefaultPred(MIB);
8077 } else {
8078 MachineInstrBuilder MIB =
8079 BuildMI(*BB, BB->end(), dl,
8080 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
8081 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
8082 MIB->getOperand(5).setReg(ARM::CPSR);
8083 MIB->getOperand(5).setIsDef(true);
8084 }
8085 BuildMI(*BB, BB->end(), dl,
8086 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
8087 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00008088
8089 // loopMBB can loop back to loopMBB or fall through to exitMBB.
8090 BB->addSuccessor(loopMBB);
8091 BB->addSuccessor(exitMBB);
8092
8093 // Add epilogue to handle BytesLeft.
8094 BB = exitMBB;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00008095 auto StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00008096
8097 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
8098 // [destOut] = STRB_POST(scratch, destLoop, 1)
8099 unsigned srcIn = srcLoop;
8100 unsigned destIn = destLoop;
8101 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00008102 unsigned srcOut = MRI.createVirtualRegister(TRC);
8103 unsigned destOut = MRI.createVirtualRegister(TRC);
8104 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00008105 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
8106 IsThumb1, IsThumb2);
8107 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
8108 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008109 srcIn = srcOut;
8110 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008111 }
8112
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008113 MI.eraseFromParent(); // The instruction is gone now.
Manman Rene8735522012-06-01 19:33:18 +00008114 return BB;
8115}
8116
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00008117MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008118ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008119 MachineBasicBlock *MBB) const {
8120 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00008121 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008122 DebugLoc DL = MI.getDebugLoc();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008123
8124 assert(Subtarget->isTargetWindows() &&
8125 "__chkstk is only supported on Windows");
8126 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
8127
8128 // __chkstk takes the number of words to allocate on the stack in R4, and
8129 // returns the stack adjustment in number of bytes in R4. This will not
8130 // clober any other registers (other than the obvious lr).
8131 //
8132 // Although, technically, IP should be considered a register which may be
8133 // clobbered, the call itself will not touch it. Windows on ARM is a pure
8134 // thumb-2 environment, so there is no interworking required. As a result, we
8135 // do not expect a veneer to be emitted by the linker, clobbering IP.
8136 //
Alp Toker1d099d92014-06-19 19:41:26 +00008137 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008138 // required, again, ensuring that IP is not clobbered.
8139 //
8140 // Finally, although some linkers may theoretically provide a trampoline for
8141 // out of range calls (which is quite common due to a 32M range limitation of
8142 // branches for Thumb), we can generate the long-call version via
8143 // -mcmodel=large, alleviating the need for the trampoline which may clobber
8144 // IP.
8145
8146 switch (TM.getCodeModel()) {
8147 case CodeModel::Small:
8148 case CodeModel::Medium:
8149 case CodeModel::Default:
8150 case CodeModel::Kernel:
8151 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
8152 .addImm((unsigned)ARMCC::AL).addReg(0)
8153 .addExternalSymbol("__chkstk")
8154 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8155 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8156 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8157 break;
8158 case CodeModel::Large:
8159 case CodeModel::JITDefault: {
8160 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
8161 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
8162
8163 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
8164 .addExternalSymbol("__chkstk");
8165 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
8166 .addImm((unsigned)ARMCC::AL).addReg(0)
8167 .addReg(Reg, RegState::Kill)
8168 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8169 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8170 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8171 break;
8172 }
8173 }
8174
8175 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
8176 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +00008177 .addReg(ARM::SP, RegState::Kill)
8178 .addReg(ARM::R4, RegState::Kill)
8179 .setMIFlags(MachineInstr::FrameSetup)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008180
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008181 MI.eraseFromParent();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008182 return MBB;
8183}
8184
8185MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008186ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008187 MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008188 DebugLoc DL = MI.getDebugLoc();
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008189 MachineFunction *MF = MBB->getParent();
8190 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8191
8192 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
Saleem Abdulrasool750a90d2016-03-25 19:48:06 +00008193 MF->insert(++MBB->getIterator(), ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008194 ContBB->splice(ContBB->begin(), MBB,
8195 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008196 ContBB->transferSuccessorsAndUpdatePHIs(MBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008197
8198 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8199 MF->push_back(TrapBB);
8200 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
8201 MBB->addSuccessor(TrapBB);
8202
8203 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008204 .addReg(MI.getOperand(0).getReg())
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008205 .addMBB(TrapBB);
Saleem Abdulrasool0dab98d2016-03-25 00:34:11 +00008206 AddDefaultPred(BuildMI(*MBB, MI, DL, TII->get(ARM::t2B)).addMBB(ContBB));
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008207 MBB->addSuccessor(ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008208
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008209 MI.eraseFromParent();
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008210 return ContBB;
8211}
8212
8213MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008214ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008215 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008216 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008217 DebugLoc dl = MI.getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008218 bool isThumb2 = Subtarget->isThumb2();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008219 switch (MI.getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008220 default: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008221 MI.dump();
Evan Chengb972e562009-08-07 00:34:42 +00008222 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008223 }
James Molloyb3326df2016-07-15 08:03:56 +00008224
8225 // Thumb1 post-indexed loads are really just single-register LDMs.
8226 case ARM::tLDR_postidx: {
8227 BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD))
James Molloy8f16dff2016-07-15 08:12:44 +00008228 .addOperand(MI.getOperand(1)) // Rn_wb
8229 .addOperand(MI.getOperand(2)) // Rn
8230 .addOperand(MI.getOperand(3)) // PredImm
8231 .addOperand(MI.getOperand(4)) // PredReg
8232 .addOperand(MI.getOperand(0)); // Rt
8233 MI.eraseFromParent();
James Molloyb3326df2016-07-15 08:03:56 +00008234 return BB;
8235 }
8236
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008237 // The Thumb2 pre-indexed stores have the same MI operands, they just
8238 // define them differently in the .td files from the isel patterns, so
8239 // they need pseudos.
8240 case ARM::t2STR_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008241 MI.setDesc(TII->get(ARM::t2STR_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008242 return BB;
8243 case ARM::t2STRB_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008244 MI.setDesc(TII->get(ARM::t2STRB_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008245 return BB;
8246 case ARM::t2STRH_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008247 MI.setDesc(TII->get(ARM::t2STRH_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008248 return BB;
8249
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008250 case ARM::STRi_preidx:
8251 case ARM::STRBi_preidx: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008252 unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM
8253 : ARM::STRB_PRE_IMM;
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008254 // Decode the offset.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008255 unsigned Offset = MI.getOperand(4).getImm();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008256 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8257 Offset = ARM_AM::getAM2Offset(Offset);
8258 if (isSub)
8259 Offset = -Offset;
8260
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008261 MachineMemOperand *MMO = *MI.memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008262 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008263 .addOperand(MI.getOperand(0)) // Rn_wb
8264 .addOperand(MI.getOperand(1)) // Rt
8265 .addOperand(MI.getOperand(2)) // Rn
8266 .addImm(Offset) // offset (skip GPR==zero_reg)
8267 .addOperand(MI.getOperand(5)) // pred
8268 .addOperand(MI.getOperand(6))
8269 .addMemOperand(MMO);
8270 MI.eraseFromParent();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008271 return BB;
8272 }
8273 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008274 case ARM::STRBr_preidx:
8275 case ARM::STRH_preidx: {
8276 unsigned NewOpc;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008277 switch (MI.getOpcode()) {
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008278 default: llvm_unreachable("unexpected opcode!");
8279 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8280 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8281 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8282 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008283 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008284 for (unsigned i = 0; i < MI.getNumOperands(); ++i)
8285 MIB.addOperand(MI.getOperand(i));
8286 MI.eraseFromParent();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008287 return BB;
8288 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008289
Evan Chengbb2af352009-08-12 05:17:19 +00008290 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008291 // To "insert" a SELECT_CC instruction, we actually have to insert the
8292 // diamond control-flow pattern. The incoming instruction knows the
8293 // destination vreg to set, the condition code register to branch on, the
8294 // true/false values to select between, and a branch opcode to use.
8295 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008296 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008297
8298 // thisMBB:
8299 // ...
8300 // TrueVal = ...
8301 // cmpTY ccX, r1, r2
8302 // bCC copy1MBB
8303 // fallthrough --> copy0MBB
8304 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008305 MachineFunction *F = BB->getParent();
8306 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8307 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008308 F->insert(It, copy0MBB);
8309 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008310
8311 // Transfer the remainder of BB and its successor edges to sinkMBB.
8312 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008313 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008314 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8315
Dan Gohmanf4f04102010-07-06 15:49:48 +00008316 BB->addSuccessor(copy0MBB);
8317 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008318
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008319 BuildMI(BB, dl, TII->get(ARM::tBcc))
8320 .addMBB(sinkMBB)
8321 .addImm(MI.getOperand(3).getImm())
8322 .addReg(MI.getOperand(4).getReg());
Dan Gohman34396292010-07-06 20:24:04 +00008323
Evan Cheng10043e22007-01-19 07:51:42 +00008324 // copy0MBB:
8325 // %FalseValue = ...
8326 // # fallthrough to sinkMBB
8327 BB = copy0MBB;
8328
8329 // Update machine-CFG edges
8330 BB->addSuccessor(sinkMBB);
8331
8332 // sinkMBB:
8333 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8334 // ...
8335 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008336 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg())
8337 .addReg(MI.getOperand(1).getReg())
8338 .addMBB(copy0MBB)
8339 .addReg(MI.getOperand(2).getReg())
8340 .addMBB(thisMBB);
Evan Cheng10043e22007-01-19 07:51:42 +00008341
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008342 MI.eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008343 return BB;
8344 }
Evan Chengb972e562009-08-07 00:34:42 +00008345
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008346 case ARM::BCCi64:
8347 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008348 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008349 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008350
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008351 // Compare both parts that make up the double comparison separately for
8352 // equality.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008353 bool RHSisZero = MI.getOpcode() == ARM::BCCZi64;
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008354
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008355 unsigned LHS1 = MI.getOperand(1).getReg();
8356 unsigned LHS2 = MI.getOperand(2).getReg();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008357 if (RHSisZero) {
8358 AddDefaultPred(BuildMI(BB, dl,
8359 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8360 .addReg(LHS1).addImm(0));
8361 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8362 .addReg(LHS2).addImm(0)
8363 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8364 } else {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008365 unsigned RHS1 = MI.getOperand(3).getReg();
8366 unsigned RHS2 = MI.getOperand(4).getReg();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008367 AddDefaultPred(BuildMI(BB, dl,
8368 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8369 .addReg(LHS1).addReg(RHS1));
8370 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8371 .addReg(LHS2).addReg(RHS2)
8372 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8373 }
8374
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008375 MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008376 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008377 if (MI.getOperand(0).getImm() == ARMCC::NE)
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008378 std::swap(destMBB, exitMBB);
8379
8380 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8381 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008382 if (isThumb2)
8383 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8384 else
8385 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008386
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008387 MI.eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008388 return BB;
8389 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008390
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008391 case ARM::Int_eh_sjlj_setjmp:
8392 case ARM::Int_eh_sjlj_setjmp_nofp:
8393 case ARM::tInt_eh_sjlj_setjmp:
8394 case ARM::t2Int_eh_sjlj_setjmp:
8395 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008396 return BB;
8397
8398 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008399 EmitSjLjDispatchBlock(MI, BB);
8400 return BB;
8401
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008402 case ARM::ABS:
8403 case ARM::t2ABS: {
8404 // To insert an ABS instruction, we have to insert the
8405 // diamond control-flow pattern. The incoming instruction knows the
8406 // source vreg to test against 0, the destination vreg to set,
8407 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008408 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008409 // It transforms
8410 // V1 = ABS V0
8411 // into
8412 // V2 = MOVS V0
8413 // BCC (branch to SinkBB if V0 >= 0)
8414 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008415 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008416 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008417 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008418 MachineFunction *Fn = BB->getParent();
8419 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8420 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8421 Fn->insert(BBI, RSBBB);
8422 Fn->insert(BBI, SinkBB);
8423
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008424 unsigned int ABSSrcReg = MI.getOperand(1).getReg();
8425 unsigned int ABSDstReg = MI.getOperand(0).getReg();
8426 bool ABSSrcKIll = MI.getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008427 bool isThumb2 = Subtarget->isThumb2();
8428 MachineRegisterInfo &MRI = Fn->getRegInfo();
8429 // In Thumb mode S must not be specified if source register is the SP or
8430 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008431 unsigned NewRsbDstReg =
8432 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008433
8434 // Transfer the remainder of BB and its successor edges to sinkMBB.
8435 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008436 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008437 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8438
8439 BB->addSuccessor(RSBBB);
8440 BB->addSuccessor(SinkBB);
8441
8442 // fall through to SinkMBB
8443 RSBBB->addSuccessor(SinkBB);
8444
Manman Rene0763c72012-06-15 21:32:12 +00008445 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008446 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008447 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8448 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008449
8450 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008451 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008452 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8453 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8454
8455 // insert rsbri in RSBBB
8456 // Note: BCC and rsbri will be converted into predicated rsbmi
8457 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008458 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008459 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008460 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008461 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8462
Andrew Trick3f07c422011-10-18 18:40:53 +00008463 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008464 // reuse ABSDstReg to not change uses of ABS instruction
8465 BuildMI(*SinkBB, SinkBB->begin(), dl,
8466 TII->get(ARM::PHI), ABSDstReg)
8467 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008468 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008469
8470 // remove ABS instruction
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008471 MI.eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008472
8473 // return last added BB
8474 return SinkBB;
8475 }
Manman Rene8735522012-06-01 19:33:18 +00008476 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008477 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008478 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008479 case ARM::WIN__CHKSTK:
8480 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008481 case ARM::WIN__DBZCHK:
8482 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008483 }
8484}
8485
Scott Douglass953f9082015-10-05 14:49:54 +00008486/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8487/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8488/// instead of as a custom inserter because we need the use list from the SDNode.
8489static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008490 MachineInstr &MI, const SDNode *Node) {
Scott Douglass953f9082015-10-05 14:49:54 +00008491 bool isThumb1 = Subtarget->isThumb1Only();
8492
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008493 DebugLoc DL = MI.getDebugLoc();
8494 MachineFunction *MF = MI.getParent()->getParent();
Scott Douglass953f9082015-10-05 14:49:54 +00008495 MachineRegisterInfo &MRI = MF->getRegInfo();
8496 MachineInstrBuilder MIB(*MF, MI);
8497
8498 // If the new dst/src is unused mark it as dead.
8499 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008500 MI.getOperand(0).setIsDead(true);
Scott Douglass953f9082015-10-05 14:49:54 +00008501 }
8502 if (!Node->hasAnyUseOfValue(1)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008503 MI.getOperand(1).setIsDead(true);
Scott Douglass953f9082015-10-05 14:49:54 +00008504 }
8505
8506 // The MEMCPY both defines and kills the scratch registers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008507 for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) {
Scott Douglass953f9082015-10-05 14:49:54 +00008508 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8509 : &ARM::GPRRegClass);
8510 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8511 }
8512}
8513
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008514void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Evan Chenge6fba772011-08-30 19:09:48 +00008515 SDNode *Node) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008516 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00008517 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8518 return;
8519 }
8520
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008521 const MCInstrDesc *MCID = &MI.getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008522 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8523 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8524 // operand is still set to noreg. If needed, set the optional operand's
8525 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008526 //
Andrew Trick88b24502011-10-18 19:18:52 +00008527 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008528
Andrew Trick924123a2011-09-21 02:20:46 +00008529 // Rename pseudo opcodes.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008530 unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode());
Andrew Trick924123a2011-09-21 02:20:46 +00008531 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008532 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008533 MCID = &TII->get(NewOpc);
8534
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008535 assert(MCID->getNumOperands() == MI.getDesc().getNumOperands() + 1 &&
Andrew Trick88b24502011-10-18 19:18:52 +00008536 "converted opcode should be the same except for cc_out");
8537
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008538 MI.setDesc(*MCID);
Andrew Trick88b24502011-10-18 19:18:52 +00008539
8540 // Add the optional cc_out operand
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008541 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008542 }
Andrew Trick88b24502011-10-18 19:18:52 +00008543 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008544
8545 // Any ARM instruction that sets the 's' bit should specify an optional
8546 // "cc_out" operand in the last operand position.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008547 if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008548 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008549 return;
8550 }
Andrew Trick924123a2011-09-21 02:20:46 +00008551 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8552 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008553 bool definesCPSR = false;
8554 bool deadCPSR = false;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008555 for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e;
8556 ++i) {
8557 const MachineOperand &MO = MI.getOperand(i);
Andrew Trick8586e622011-09-20 03:17:40 +00008558 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8559 definesCPSR = true;
8560 if (MO.isDead())
8561 deadCPSR = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008562 MI.RemoveOperand(i);
Andrew Trick8586e622011-09-20 03:17:40 +00008563 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008564 }
8565 }
Andrew Trick8586e622011-09-20 03:17:40 +00008566 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008567 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008568 return;
8569 }
8570 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008571 if (deadCPSR) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008572 assert(!MI.getOperand(ccOutIdx).getReg() &&
Andrew Trick924123a2011-09-21 02:20:46 +00008573 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008574 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008575 }
Andrew Trick8586e622011-09-20 03:17:40 +00008576
Andrew Trick924123a2011-09-21 02:20:46 +00008577 // If this instruction was defined with an optional CPSR def and its dag node
8578 // had a live implicit CPSR def, then activate the optional CPSR def.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008579 MachineOperand &MO = MI.getOperand(ccOutIdx);
Andrew Trick8586e622011-09-20 03:17:40 +00008580 MO.setReg(ARM::CPSR);
8581 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008582}
8583
Evan Cheng10043e22007-01-19 07:51:42 +00008584//===----------------------------------------------------------------------===//
8585// ARM Optimization Hooks
8586//===----------------------------------------------------------------------===//
8587
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008588// Helper function that checks if N is a null or all ones constant.
8589static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008590 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008591}
8592
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008593// Return true if N is conditionally 0 or all ones.
8594// Detects these expressions where cc is an i1 value:
8595//
8596// (select cc 0, y) [AllOnes=0]
8597// (select cc y, 0) [AllOnes=0]
8598// (zext cc) [AllOnes=0]
8599// (sext cc) [AllOnes=0/1]
8600// (select cc -1, y) [AllOnes=1]
8601// (select cc y, -1) [AllOnes=1]
8602//
8603// Invert is set when N is the null/all ones constant when CC is false.
8604// OtherOp is set to the alternative value of N.
8605static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8606 SDValue &CC, bool &Invert,
8607 SDValue &OtherOp,
8608 SelectionDAG &DAG) {
8609 switch (N->getOpcode()) {
8610 default: return false;
8611 case ISD::SELECT: {
8612 CC = N->getOperand(0);
8613 SDValue N1 = N->getOperand(1);
8614 SDValue N2 = N->getOperand(2);
8615 if (isZeroOrAllOnes(N1, AllOnes)) {
8616 Invert = false;
8617 OtherOp = N2;
8618 return true;
8619 }
8620 if (isZeroOrAllOnes(N2, AllOnes)) {
8621 Invert = true;
8622 OtherOp = N1;
8623 return true;
8624 }
8625 return false;
8626 }
8627 case ISD::ZERO_EXTEND:
8628 // (zext cc) can never be the all ones value.
8629 if (AllOnes)
8630 return false;
8631 // Fall through.
8632 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008633 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008634 EVT VT = N->getValueType(0);
8635 CC = N->getOperand(0);
8636 if (CC.getValueType() != MVT::i1)
8637 return false;
8638 Invert = !AllOnes;
8639 if (AllOnes)
8640 // When looking for an AllOnes constant, N is an sext, and the 'other'
8641 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008642 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008643 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8644 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008645 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008646 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008647 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8648 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008649 return true;
8650 }
8651 }
8652}
8653
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008654// Combine a constant select operand into its use:
8655//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008656// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8657// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8658// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8659// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8660// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008661//
8662// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008663// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008664//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008665// Also recognize sext/zext from i1:
8666//
8667// (add (zext cc), x) -> (select cc (add x, 1), x)
8668// (add (sext cc), x) -> (select cc (add x, -1), x)
8669//
8670// These transformations eventually create predicated instructions.
8671//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008672// @param N The node to transform.
8673// @param Slct The N operand that is a select.
8674// @param OtherOp The other N operand (x above).
8675// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008676// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008677// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008678static
8679SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008680 TargetLowering::DAGCombinerInfo &DCI,
8681 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008682 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008683 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008684 SDValue NonConstantVal;
8685 SDValue CCOp;
8686 bool SwapSelectOps;
8687 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8688 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008689 return SDValue();
8690
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008691 // Slct is now know to be the desired identity constant when CC is true.
8692 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008693 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008694 OtherOp, NonConstantVal);
8695 // Unless SwapSelectOps says CC should be false.
8696 if (SwapSelectOps)
8697 std::swap(TrueVal, FalseVal);
8698
Andrew Trickef9de2a2013-05-25 02:42:55 +00008699 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008700 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008701}
8702
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008703// Attempt combineSelectAndUse on each operand of a commutative operator N.
8704static
8705SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8706 TargetLowering::DAGCombinerInfo &DCI) {
8707 SDValue N0 = N->getOperand(0);
8708 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008709 if (N0.getNode()->hasOneUse())
8710 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008711 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008712 if (N1.getNode()->hasOneUse())
8713 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008714 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008715 return SDValue();
8716}
8717
Eric Christopher1b8b94192011-06-29 21:10:36 +00008718// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008719// (only after legalization).
8720static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8721 TargetLowering::DAGCombinerInfo &DCI,
8722 const ARMSubtarget *Subtarget) {
8723
8724 // Only perform optimization if after legalize, and if NEON is available. We
8725 // also expected both operands to be BUILD_VECTORs.
8726 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8727 || N0.getOpcode() != ISD::BUILD_VECTOR
8728 || N1.getOpcode() != ISD::BUILD_VECTOR)
8729 return SDValue();
8730
8731 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8732 EVT VT = N->getValueType(0);
8733 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8734 return SDValue();
8735
8736 // Check that the vector operands are of the right form.
8737 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8738 // operands, where N is the size of the formed vector.
8739 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8740 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008741
8742 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008743 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008744 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008745 SDValue Vec = N0->getOperand(0)->getOperand(0);
8746 SDNode *V = Vec.getNode();
8747 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008748
Eric Christopher1b8b94192011-06-29 21:10:36 +00008749 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008750 // check to see if each of their operands are an EXTRACT_VECTOR with
8751 // the same vector and appropriate index.
8752 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8753 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8754 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008755
Tanya Lattnere9e67052011-06-14 23:48:48 +00008756 SDValue ExtVec0 = N0->getOperand(i);
8757 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008758
Tanya Lattnere9e67052011-06-14 23:48:48 +00008759 // First operand is the vector, verify its the same.
8760 if (V != ExtVec0->getOperand(0).getNode() ||
8761 V != ExtVec1->getOperand(0).getNode())
8762 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008763
Tanya Lattnere9e67052011-06-14 23:48:48 +00008764 // Second is the constant, verify its correct.
8765 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8766 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008767
Tanya Lattnere9e67052011-06-14 23:48:48 +00008768 // For the constant, we want to see all the even or all the odd.
8769 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8770 || C1->getZExtValue() != nextIndex+1)
8771 return SDValue();
8772
8773 // Increment index.
8774 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008775 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008776 return SDValue();
8777 }
8778
8779 // Create VPADDL node.
8780 SelectionDAG &DAG = DCI.DAG;
8781 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008782
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008783 SDLoc dl(N);
8784
Tanya Lattnere9e67052011-06-14 23:48:48 +00008785 // Build operand list.
8786 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008787 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008788 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008789
8790 // Input is the vector.
8791 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008792
Tanya Lattnere9e67052011-06-14 23:48:48 +00008793 // Get widened type and narrowed type.
8794 MVT widenType;
8795 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00008796
Silviu Barangaa3106e62014-04-03 10:44:27 +00008797 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8798 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008799 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8800 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8801 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8802 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008803 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008804 }
8805
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008806 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008807 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008808 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008809}
8810
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008811static SDValue findMUL_LOHI(SDValue V) {
8812 if (V->getOpcode() == ISD::UMUL_LOHI ||
8813 V->getOpcode() == ISD::SMUL_LOHI)
8814 return V;
8815 return SDValue();
8816}
8817
8818static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8819 TargetLowering::DAGCombinerInfo &DCI,
8820 const ARMSubtarget *Subtarget) {
8821
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008822 // Look for multiply add opportunities.
8823 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8824 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8825 // a glue link from the first add to the second add.
8826 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8827 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008828 // UMUL_LOHI
8829 // / :lo \ :hi
8830 // / \ [no multiline comment]
8831 // loAdd -> ADDE |
8832 // \ :glue /
8833 // \ /
8834 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008835 //
8836 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8837 SDValue AddcOp0 = AddcNode->getOperand(0);
8838 SDValue AddcOp1 = AddcNode->getOperand(1);
8839
8840 // Check if the two operands are from the same mul_lohi node.
8841 if (AddcOp0.getNode() == AddcOp1.getNode())
8842 return SDValue();
8843
8844 assert(AddcNode->getNumValues() == 2 &&
8845 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008846 "Expect ADDC with two result values. First: i32");
8847
8848 // Check that we have a glued ADDC node.
8849 if (AddcNode->getValueType(1) != MVT::Glue)
8850 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008851
8852 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8853 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8854 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8855 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8856 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8857 return SDValue();
8858
8859 // Look for the glued ADDE.
8860 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008861 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008862 return SDValue();
8863
8864 // Make sure it is really an ADDE.
8865 if (AddeNode->getOpcode() != ISD::ADDE)
8866 return SDValue();
8867
8868 assert(AddeNode->getNumOperands() == 3 &&
8869 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8870 "ADDE node has the wrong inputs");
8871
8872 // Check for the triangle shape.
8873 SDValue AddeOp0 = AddeNode->getOperand(0);
8874 SDValue AddeOp1 = AddeNode->getOperand(1);
8875
8876 // Make sure that the ADDE operands are not coming from the same node.
8877 if (AddeOp0.getNode() == AddeOp1.getNode())
8878 return SDValue();
8879
8880 // Find the MUL_LOHI node walking up ADDE's operands.
8881 bool IsLeftOperandMUL = false;
8882 SDValue MULOp = findMUL_LOHI(AddeOp0);
8883 if (MULOp == SDValue())
8884 MULOp = findMUL_LOHI(AddeOp1);
8885 else
8886 IsLeftOperandMUL = true;
8887 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008888 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008889
8890 // Figure out the right opcode.
8891 unsigned Opc = MULOp->getOpcode();
8892 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8893
8894 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008895 SDValue* HiAdd = nullptr;
8896 SDValue* LoMul = nullptr;
8897 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008898
Jyoti Allurf1d70502015-01-23 09:10:03 +00008899 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8900 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8901 return SDValue();
8902
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008903 if (IsLeftOperandMUL)
8904 HiAdd = &AddeOp1;
8905 else
8906 HiAdd = &AddeOp0;
8907
8908
Jyoti Allurf1d70502015-01-23 09:10:03 +00008909 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8910 // whose low result is fed to the ADDC we are checking.
8911
8912 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008913 LoMul = &AddcOp0;
8914 LowAdd = &AddcOp1;
8915 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008916 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008917 LoMul = &AddcOp1;
8918 LowAdd = &AddcOp0;
8919 }
8920
Craig Topper062a2ba2014-04-25 05:30:21 +00008921 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008922 return SDValue();
8923
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008924 // Create the merged node.
8925 SelectionDAG &DAG = DCI.DAG;
8926
8927 // Build operand list.
8928 SmallVector<SDValue, 8> Ops;
8929 Ops.push_back(LoMul->getOperand(0));
8930 Ops.push_back(LoMul->getOperand(1));
8931 Ops.push_back(*LowAdd);
8932 Ops.push_back(*HiAdd);
8933
Andrew Trickef9de2a2013-05-25 02:42:55 +00008934 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008935 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008936
8937 // Replace the ADDs' nodes uses by the MLA node's values.
8938 SDValue HiMLALResult(MLALNode.getNode(), 1);
8939 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8940
8941 SDValue LoMLALResult(MLALNode.getNode(), 0);
8942 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8943
8944 // Return original node to notify the driver to stop replacing.
8945 SDValue resNode(AddcNode, 0);
8946 return resNode;
8947}
8948
Sam Parkerd616cf02016-06-20 16:47:09 +00008949static SDValue AddCombineTo64bitUMAAL(SDNode *AddcNode,
8950 TargetLowering::DAGCombinerInfo &DCI,
8951 const ARMSubtarget *Subtarget) {
8952 // UMAAL is similar to UMLAL except that it adds two unsigned values.
8953 // While trying to combine for the other MLAL nodes, first search for the
8954 // chance to use UMAAL. Check if Addc uses another addc node which can first
8955 // be combined into a UMLAL. The other pattern is AddcNode being combined
8956 // into an UMLAL and then using another addc is handled in ISelDAGToDAG.
8957
Sam Parkerd5ca0a62016-07-25 10:11:00 +00008958 if (!Subtarget->hasV6Ops() ||
8959 (Subtarget->isThumb() && !Subtarget->hasThumb2()))
Sam Parkerd616cf02016-06-20 16:47:09 +00008960 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
8961
8962 SDNode *PrevAddc = nullptr;
8963 if (AddcNode->getOperand(0).getOpcode() == ISD::ADDC)
8964 PrevAddc = AddcNode->getOperand(0).getNode();
8965 else if (AddcNode->getOperand(1).getOpcode() == ISD::ADDC)
8966 PrevAddc = AddcNode->getOperand(1).getNode();
8967
8968 // If there's no addc chains, just return a search for any MLAL.
8969 if (PrevAddc == nullptr)
8970 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
8971
8972 // Try to convert the addc operand to an MLAL and if that fails try to
8973 // combine AddcNode.
8974 SDValue MLAL = AddCombineTo64bitMLAL(PrevAddc, DCI, Subtarget);
8975 if (MLAL != SDValue(PrevAddc, 0))
8976 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
8977
8978 // Find the converted UMAAL or quit if it doesn't exist.
8979 SDNode *UmlalNode = nullptr;
8980 SDValue AddHi;
8981 if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) {
8982 UmlalNode = AddcNode->getOperand(0).getNode();
8983 AddHi = AddcNode->getOperand(1);
8984 } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) {
8985 UmlalNode = AddcNode->getOperand(1).getNode();
8986 AddHi = AddcNode->getOperand(0);
8987 } else {
8988 return SDValue();
8989 }
8990
8991 // The ADDC should be glued to an ADDE node, which uses the same UMLAL as
8992 // the ADDC as well as Zero.
8993 auto *Zero = dyn_cast<ConstantSDNode>(UmlalNode->getOperand(3));
8994
8995 if (!Zero || Zero->getZExtValue() != 0)
8996 return SDValue();
8997
8998 // Check that we have a glued ADDC node.
8999 if (AddcNode->getValueType(1) != MVT::Glue)
9000 return SDValue();
9001
9002 // Look for the glued ADDE.
9003 SDNode* AddeNode = AddcNode->getGluedUser();
9004 if (!AddeNode)
9005 return SDValue();
9006
9007 if ((AddeNode->getOperand(0).getNode() == Zero &&
9008 AddeNode->getOperand(1).getNode() == UmlalNode) ||
9009 (AddeNode->getOperand(0).getNode() == UmlalNode &&
9010 AddeNode->getOperand(1).getNode() == Zero)) {
9011
9012 SelectionDAG &DAG = DCI.DAG;
9013 SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1),
9014 UmlalNode->getOperand(2), AddHi };
9015 SDValue UMAAL = DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode),
9016 DAG.getVTList(MVT::i32, MVT::i32), Ops);
9017
9018 // Replace the ADDs' nodes uses by the UMAAL node's values.
9019 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1));
9020 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0));
9021
9022 // Return original node to notify the driver to stop replacing.
9023 return SDValue(AddcNode, 0);
9024 }
9025 return SDValue();
9026}
9027
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009028/// PerformADDCCombine - Target-specific dag combine transform from
Sam Parkerd616cf02016-06-20 16:47:09 +00009029/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL or
9030/// ISD::ADDC, ISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009031static SDValue PerformADDCCombine(SDNode *N,
9032 TargetLowering::DAGCombinerInfo &DCI,
9033 const ARMSubtarget *Subtarget) {
9034
Sam Parkerd616cf02016-06-20 16:47:09 +00009035 if (Subtarget->isThumb1Only()) return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009036
Sam Parkerd616cf02016-06-20 16:47:09 +00009037 // Only perform the checks after legalize when the pattern is available.
9038 if (DCI.isBeforeLegalize()) return SDValue();
9039
9040 return AddCombineTo64bitUMAAL(N, DCI, Subtarget);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009041}
9042
Bob Wilson728eb292010-07-29 20:34:14 +00009043/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
9044/// operands N0 and N1. This is a helper for PerformADDCombine that is
9045/// called with the default operands, and if that fails, with commuted
9046/// operands.
9047static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009048 TargetLowering::DAGCombinerInfo &DCI,
9049 const ARMSubtarget *Subtarget){
9050
9051 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009052 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00009053 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00009054
Chris Lattner4147f082009-03-12 06:52:53 +00009055 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009056 if (N0.getNode()->hasOneUse())
9057 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
9058 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00009059 return SDValue();
9060}
9061
Bob Wilson728eb292010-07-29 20:34:14 +00009062/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
9063///
9064static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009065 TargetLowering::DAGCombinerInfo &DCI,
9066 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00009067 SDValue N0 = N->getOperand(0);
9068 SDValue N1 = N->getOperand(1);
9069
9070 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009071 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00009072 return Result;
9073
9074 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00009075 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00009076}
9077
Chris Lattner4147f082009-03-12 06:52:53 +00009078/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00009079///
Chris Lattner4147f082009-03-12 06:52:53 +00009080static SDValue PerformSUBCombine(SDNode *N,
9081 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00009082 SDValue N0 = N->getOperand(0);
9083 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00009084
Chris Lattner4147f082009-03-12 06:52:53 +00009085 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009086 if (N1.getNode()->hasOneUse())
9087 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
9088 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00009089
Chris Lattner4147f082009-03-12 06:52:53 +00009090 return SDValue();
9091}
9092
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009093/// PerformVMULCombine
9094/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
9095/// special multiplier accumulator forwarding.
9096/// vmul d3, d0, d2
9097/// vmla d3, d1, d2
9098/// is faster than
9099/// vadd d3, d0, d1
9100/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00009101// However, for (A + B) * (A + B),
9102// vadd d2, d0, d1
9103// vmul d3, d0, d2
9104// vmla d3, d1, d2
9105// is slower than
9106// vadd d2, d0, d1
9107// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009108static SDValue PerformVMULCombine(SDNode *N,
9109 TargetLowering::DAGCombinerInfo &DCI,
9110 const ARMSubtarget *Subtarget) {
9111 if (!Subtarget->hasVMLxForwarding())
9112 return SDValue();
9113
9114 SelectionDAG &DAG = DCI.DAG;
9115 SDValue N0 = N->getOperand(0);
9116 SDValue N1 = N->getOperand(1);
9117 unsigned Opcode = N0.getOpcode();
9118 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
9119 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00009120 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009121 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
9122 Opcode != ISD::FADD && Opcode != ISD::FSUB)
9123 return SDValue();
9124 std::swap(N0, N1);
9125 }
9126
Weiming Zhao2052f482013-09-25 23:12:06 +00009127 if (N0 == N1)
9128 return SDValue();
9129
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009130 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009131 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009132 SDValue N00 = N0->getOperand(0);
9133 SDValue N01 = N0->getOperand(1);
9134 return DAG.getNode(Opcode, DL, VT,
9135 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
9136 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
9137}
9138
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009139static SDValue PerformMULCombine(SDNode *N,
9140 TargetLowering::DAGCombinerInfo &DCI,
9141 const ARMSubtarget *Subtarget) {
9142 SelectionDAG &DAG = DCI.DAG;
9143
9144 if (Subtarget->isThumb1Only())
9145 return SDValue();
9146
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009147 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9148 return SDValue();
9149
9150 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009151 if (VT.is64BitVector() || VT.is128BitVector())
9152 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009153 if (VT != MVT::i32)
9154 return SDValue();
9155
9156 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9157 if (!C)
9158 return SDValue();
9159
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009160 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009161 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009162
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009163 ShiftAmt = ShiftAmt & (32 - 1);
9164 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009165 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009166
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009167 SDValue Res;
9168 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009169
9170 if (MulAmt >= 0) {
9171 if (isPowerOf2_32(MulAmt - 1)) {
9172 // (mul x, 2^N + 1) => (add (shl x, N), x)
9173 Res = DAG.getNode(ISD::ADD, DL, VT,
9174 V,
9175 DAG.getNode(ISD::SHL, DL, VT,
9176 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009177 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009178 MVT::i32)));
9179 } else if (isPowerOf2_32(MulAmt + 1)) {
9180 // (mul x, 2^N - 1) => (sub (shl x, N), x)
9181 Res = DAG.getNode(ISD::SUB, DL, VT,
9182 DAG.getNode(ISD::SHL, DL, VT,
9183 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009184 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009185 MVT::i32)),
9186 V);
9187 } else
9188 return SDValue();
9189 } else {
9190 uint64_t MulAmtAbs = -MulAmt;
9191 if (isPowerOf2_32(MulAmtAbs + 1)) {
9192 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
9193 Res = DAG.getNode(ISD::SUB, DL, VT,
9194 V,
9195 DAG.getNode(ISD::SHL, DL, VT,
9196 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009197 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009198 MVT::i32)));
9199 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
9200 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
9201 Res = DAG.getNode(ISD::ADD, DL, VT,
9202 V,
9203 DAG.getNode(ISD::SHL, DL, VT,
9204 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009205 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009206 MVT::i32)));
9207 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009208 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009209
9210 } else
9211 return SDValue();
9212 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009213
9214 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009215 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009216 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009217
9218 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009219 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009220 return SDValue();
9221}
9222
Owen Anderson30c48922010-11-05 19:27:46 +00009223static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00009224 TargetLowering::DAGCombinerInfo &DCI,
9225 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00009226
Owen Anderson30c48922010-11-05 19:27:46 +00009227 // Attempt to use immediate-form VBIC
9228 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009229 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00009230 EVT VT = N->getValueType(0);
9231 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009232
Tanya Lattner266792a2011-04-07 15:24:20 +00009233 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9234 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009235
Owen Anderson30c48922010-11-05 19:27:46 +00009236 APInt SplatBits, SplatUndef;
9237 unsigned SplatBitSize;
9238 bool HasAnyUndefs;
9239 if (BVN &&
9240 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9241 if (SplatBitSize <= 64) {
9242 EVT VbicVT;
9243 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
9244 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009245 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009246 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00009247 if (Val.getNode()) {
9248 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009249 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00009250 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009251 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00009252 }
9253 }
9254 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009255
Evan Chenge87681c2012-02-23 01:19:06 +00009256 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009257 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009258 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009259 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009260 }
9261
Owen Anderson30c48922010-11-05 19:27:46 +00009262 return SDValue();
9263}
9264
Jim Grosbach11013ed2010-07-16 23:05:05 +00009265/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
9266static SDValue PerformORCombine(SDNode *N,
9267 TargetLowering::DAGCombinerInfo &DCI,
9268 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009269 // Attempt to use immediate-form VORR
9270 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009271 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009272 EVT VT = N->getValueType(0);
9273 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009274
Tanya Lattner266792a2011-04-07 15:24:20 +00009275 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9276 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009277
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009278 APInt SplatBits, SplatUndef;
9279 unsigned SplatBitSize;
9280 bool HasAnyUndefs;
9281 if (BVN && Subtarget->hasNEON() &&
9282 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9283 if (SplatBitSize <= 64) {
9284 EVT VorrVT;
9285 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9286 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009287 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009288 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009289 if (Val.getNode()) {
9290 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009291 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009292 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009293 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009294 }
9295 }
9296 }
9297
Evan Chenge87681c2012-02-23 01:19:06 +00009298 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009299 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009300 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009301 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009302 }
9303
Nadav Rotem3a94c542012-08-13 18:52:44 +00009304 // The code below optimizes (or (and X, Y), Z).
9305 // The AND operand needs to have a single user to make these optimizations
9306 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009307 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009308 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009309 return SDValue();
9310 SDValue N1 = N->getOperand(1);
9311
9312 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9313 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9314 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9315 APInt SplatUndef;
9316 unsigned SplatBitSize;
9317 bool HasAnyUndefs;
9318
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009319 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009320 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009321 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9322 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009323 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009324 HasAnyUndefs) && !HasAnyUndefs) {
9325 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9326 HasAnyUndefs) && !HasAnyUndefs) {
9327 // Ensure that the bit width of the constants are the same and that
9328 // the splat arguments are logical inverses as per the pattern we
9329 // are trying to simplify.
9330 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9331 SplatBits0 == ~SplatBits1) {
9332 // Canonicalize the vector type to make instruction selection
9333 // simpler.
9334 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9335 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9336 N0->getOperand(1),
9337 N0->getOperand(0),
9338 N1->getOperand(0));
9339 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9340 }
9341 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009342 }
9343 }
9344
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009345 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9346 // reasonable.
9347
Jim Grosbach11013ed2010-07-16 23:05:05 +00009348 // BFI is only available on V6T2+
9349 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9350 return SDValue();
9351
Andrew Trickef9de2a2013-05-25 02:42:55 +00009352 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009353 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009354 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009355 //
9356 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009357 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009358 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009359 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009360 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009361 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009362
Jim Grosbach11013ed2010-07-16 23:05:05 +00009363 if (VT != MVT::i32)
9364 return SDValue();
9365
Evan Cheng2e51bb42010-12-13 20:32:54 +00009366 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009367
Jim Grosbach11013ed2010-07-16 23:05:05 +00009368 // The value and the mask need to be constants so we can verify this is
9369 // actually a bitfield set. If the mask is 0xffff, we can do better
9370 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009371 SDValue MaskOp = N0.getOperand(1);
9372 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9373 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009374 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009375 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009376 if (Mask == 0xffff)
9377 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009378 SDValue Res;
9379 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009380 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9381 if (N1C) {
9382 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009383 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009384 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009385
Evan Cheng34345752010-12-11 04:11:38 +00009386 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009387 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009388
Evan Cheng2e51bb42010-12-13 20:32:54 +00009389 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009390 DAG.getConstant(Val, DL, MVT::i32),
9391 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009392
9393 // Do not add new nodes to DAG combiner worklist.
9394 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009395 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009396 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009397 } else if (N1.getOpcode() == ISD::AND) {
9398 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009399 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9400 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009401 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009402 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009403
Eric Christopherd5530962011-03-26 01:21:03 +00009404 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9405 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009406 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009407 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009408 // The pack halfword instruction works better for masks that fit it,
9409 // so use that when it's available.
9410 if (Subtarget->hasT2ExtractPack() &&
9411 (Mask == 0xffff || Mask == 0xffff0000))
9412 return SDValue();
9413 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009414 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009415 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009416 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009417 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009418 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009419 // Do not add new nodes to DAG combiner worklist.
9420 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009421 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009422 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009423 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009424 // The pack halfword instruction works better for masks that fit it,
9425 // so use that when it's available.
9426 if (Subtarget->hasT2ExtractPack() &&
9427 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9428 return SDValue();
9429 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009430 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009431 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009432 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009433 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009434 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009435 // Do not add new nodes to DAG combiner worklist.
9436 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009437 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009438 }
9439 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009440
Evan Cheng2e51bb42010-12-13 20:32:54 +00009441 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9442 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9443 ARM::isBitFieldInvertedMask(~Mask)) {
9444 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9445 // where lsb(mask) == #shamt and masked bits of B are known zero.
9446 SDValue ShAmt = N00.getOperand(1);
9447 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009448 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009449 if (ShAmtC != LSB)
9450 return SDValue();
9451
9452 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009453 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009454
9455 // Do not add new nodes to DAG combiner worklist.
9456 DCI.CombineTo(N, Res, false);
9457 }
9458
Jim Grosbach11013ed2010-07-16 23:05:05 +00009459 return SDValue();
9460}
9461
Evan Chenge87681c2012-02-23 01:19:06 +00009462static SDValue PerformXORCombine(SDNode *N,
9463 TargetLowering::DAGCombinerInfo &DCI,
9464 const ARMSubtarget *Subtarget) {
9465 EVT VT = N->getValueType(0);
9466 SelectionDAG &DAG = DCI.DAG;
9467
9468 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9469 return SDValue();
9470
9471 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009472 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009473 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009474 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009475 }
9476
9477 return SDValue();
9478}
9479
James Molloyce12c922015-11-11 15:40:40 +00009480// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9481// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9482// their position in "to" (Rd).
9483static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9484 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009485
James Molloyce12c922015-11-11 15:40:40 +00009486 SDValue From = N->getOperand(1);
9487 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9488 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9489
9490 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9491 // #C in the base of the SHR.
9492 if (From->getOpcode() == ISD::SRL &&
9493 isa<ConstantSDNode>(From->getOperand(1))) {
9494 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9495 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9496 FromMask <<= Shift.getLimitedValue(31);
9497 From = From->getOperand(0);
9498 }
9499
9500 return From;
9501}
9502
9503// If A and B contain one contiguous set of bits, does A | B == A . B?
9504//
9505// Neither A nor B must be zero.
9506static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9507 unsigned LastActiveBitInA = A.countTrailingZeros();
9508 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9509 return LastActiveBitInA - 1 == FirstActiveBitInB;
9510}
9511
9512static SDValue FindBFIToCombineWith(SDNode *N) {
9513 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9514 // if one exists.
9515 APInt ToMask, FromMask;
9516 SDValue From = ParseBFI(N, ToMask, FromMask);
9517 SDValue To = N->getOperand(0);
9518
9519 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9520 // aren't compatible, but not if they set the same bit in their destination as
9521 // we do (or that of any BFI we're going to combine with).
9522 SDValue V = To;
9523 APInt CombinedToMask = ToMask;
9524 while (V.getOpcode() == ARMISD::BFI) {
9525 APInt NewToMask, NewFromMask;
9526 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9527 if (NewFrom != From) {
9528 // This BFI has a different base. Keep going.
9529 CombinedToMask |= NewToMask;
9530 V = V.getOperand(0);
9531 continue;
9532 }
9533
9534 // Do the written bits conflict with any we've seen so far?
9535 if ((NewToMask & CombinedToMask).getBoolValue())
9536 // Conflicting bits - bail out because going further is unsafe.
9537 return SDValue();
9538
9539 // Are the new bits contiguous when combined with the old bits?
9540 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9541 BitsProperlyConcatenate(FromMask, NewFromMask))
9542 return V;
9543 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9544 BitsProperlyConcatenate(NewFromMask, FromMask))
9545 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009546
James Molloyce12c922015-11-11 15:40:40 +00009547 // We've seen a write to some bits, so track it.
9548 CombinedToMask |= NewToMask;
9549 // Keep going...
9550 V = V.getOperand(0);
9551 }
9552
9553 return SDValue();
9554}
9555
Evan Chengc1778132010-12-14 03:22:07 +00009556static SDValue PerformBFICombine(SDNode *N,
9557 TargetLowering::DAGCombinerInfo &DCI) {
9558 SDValue N1 = N->getOperand(1);
9559 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009560 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9561 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009562 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9563 if (!N11C)
9564 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009565 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009566 unsigned LSB = countTrailingZeros(~InvMask);
9567 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009568 assert(Width <
9569 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009570 "undefined behavior");
9571 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009572 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009573 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009574 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009575 N->getOperand(0), N1.getOperand(0),
9576 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009577 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9578 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9579 // Keep track of any consecutive bits set that all come from the same base
9580 // value. We can combine these together into a single BFI.
9581 SDValue CombineBFI = FindBFIToCombineWith(N);
9582 if (CombineBFI == SDValue())
9583 return SDValue();
9584
9585 // We've found a BFI.
9586 APInt ToMask1, FromMask1;
9587 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9588
9589 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009590 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9591 assert(From1 == From2);
9592 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009593
James Molloyce12c922015-11-11 15:40:40 +00009594 // First, unlink CombineBFI.
9595 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9596 // Then create a new BFI, combining the two together.
9597 APInt NewFromMask = FromMask1 | FromMask2;
9598 APInt NewToMask = ToMask1 | ToMask2;
9599
9600 EVT VT = N->getValueType(0);
9601 SDLoc dl(N);
9602
9603 if (NewFromMask[0] == 0)
9604 From1 = DCI.DAG.getNode(
9605 ISD::SRL, dl, VT, From1,
9606 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9607 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9608 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009609 }
9610 return SDValue();
9611}
9612
Bob Wilson22806742010-09-22 22:09:21 +00009613/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9614/// ARMISD::VMOVRRD.
9615static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009616 TargetLowering::DAGCombinerInfo &DCI,
9617 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009618 // vmovrrd(vmovdrr x, y) -> x,y
9619 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009620 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009621 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009622
9623 // vmovrrd(load f64) -> (load i32), (load i32)
9624 SDNode *InNode = InDouble.getNode();
9625 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9626 InNode->getValueType(0) == MVT::f64 &&
9627 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9628 !cast<LoadSDNode>(InNode)->isVolatile()) {
9629 // TODO: Should this be done for non-FrameIndex operands?
9630 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9631
9632 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009633 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009634 SDValue BasePtr = LD->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +00009635 SDValue NewLD1 =
9636 DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(),
9637 LD->getAlignment(), LD->getMemOperand()->getFlags());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009638
9639 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009640 DAG.getConstant(4, DL, MVT::i32));
Justin Lebar9c375812016-07-15 18:27:10 +00009641 SDValue NewLD2 = DAG.getLoad(
9642 MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, LD->getPointerInfo(),
9643 std::min(4U, LD->getAlignment() / 2), LD->getMemOperand()->getFlags());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009644
9645 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009646 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009647 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009648 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009649 return Result;
9650 }
9651
Bob Wilson22806742010-09-22 22:09:21 +00009652 return SDValue();
9653}
9654
9655/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9656/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9657static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9658 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9659 SDValue Op0 = N->getOperand(0);
9660 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009661 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009662 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009663 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009664 Op1 = Op1.getOperand(0);
9665 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9666 Op0.getNode() == Op1.getNode() &&
9667 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009668 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009669 N->getValueType(0), Op0.getOperand(0));
9670 return SDValue();
9671}
9672
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009673/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9674/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9675/// i64 vector to have f64 elements, since the value can then be loaded
9676/// directly into a VFP register.
9677static bool hasNormalLoadOperand(SDNode *N) {
9678 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9679 for (unsigned i = 0; i < NumElts; ++i) {
9680 SDNode *Elt = N->getOperand(i).getNode();
9681 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9682 return true;
9683 }
9684 return false;
9685}
9686
Bob Wilsoncb6db982010-09-17 22:59:05 +00009687/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9688/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009689static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009690 TargetLowering::DAGCombinerInfo &DCI,
9691 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009692 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9693 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9694 // into a pair of GPRs, which is fine when the value is used as a scalar,
9695 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009696 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009697 if (N->getNumOperands() == 2)
9698 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009699 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +00009700
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009701 // Load i64 elements as f64 values so that type legalization does not split
9702 // them up into i32 values.
9703 EVT VT = N->getValueType(0);
9704 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9705 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009706 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009707 SmallVector<SDValue, 8> Ops;
9708 unsigned NumElts = VT.getVectorNumElements();
9709 for (unsigned i = 0; i < NumElts; ++i) {
9710 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9711 Ops.push_back(V);
9712 // Make the DAGCombiner fold the bitcast.
9713 DCI.AddToWorklist(V.getNode());
9714 }
9715 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00009716 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009717 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9718}
9719
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009720/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9721static SDValue
9722PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9723 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9724 // At that time, we may have inserted bitcasts from integer to float.
9725 // If these bitcasts have survived DAGCombine, change the lowering of this
9726 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9727 // force to use floating point types.
9728
9729 // Make sure we can change the type of the vector.
9730 // This is possible iff:
9731 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9732 // 1.1. Vector is used only once.
9733 // 1.2. Use is a bit convert to an integer type.
9734 // 2. The size of its operands are 32-bits (64-bits are not legal).
9735 EVT VT = N->getValueType(0);
9736 EVT EltVT = VT.getVectorElementType();
9737
9738 // Check 1.1. and 2.
9739 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9740 return SDValue();
9741
9742 // By construction, the input type must be float.
9743 assert(EltVT == MVT::f32 && "Unexpected type!");
9744
9745 // Check 1.2.
9746 SDNode *Use = *N->use_begin();
9747 if (Use->getOpcode() != ISD::BITCAST ||
9748 Use->getValueType(0).isFloatingPoint())
9749 return SDValue();
9750
9751 // Check profitability.
9752 // Model is, if more than half of the relevant operands are bitcast from
9753 // i32, turn the build_vector into a sequence of insert_vector_elt.
9754 // Relevant operands are everything that is not statically
9755 // (i.e., at compile time) bitcasted.
9756 unsigned NumOfBitCastedElts = 0;
9757 unsigned NumElts = VT.getVectorNumElements();
9758 unsigned NumOfRelevantElts = NumElts;
9759 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9760 SDValue Elt = N->getOperand(Idx);
9761 if (Elt->getOpcode() == ISD::BITCAST) {
9762 // Assume only bit cast to i32 will go away.
9763 if (Elt->getOperand(0).getValueType() == MVT::i32)
9764 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +00009765 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009766 // Constants are statically casted, thus do not count them as
9767 // relevant operands.
9768 --NumOfRelevantElts;
9769 }
9770
9771 // Check if more than half of the elements require a non-free bitcast.
9772 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9773 return SDValue();
9774
9775 SelectionDAG &DAG = DCI.DAG;
9776 // Create the new vector type.
9777 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9778 // Check if the type is legal.
9779 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9780 if (!TLI.isTypeLegal(VecVT))
9781 return SDValue();
9782
9783 // Combine:
9784 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9785 // => BITCAST INSERT_VECTOR_ELT
9786 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9787 // (BITCAST EN), N.
9788 SDValue Vec = DAG.getUNDEF(VecVT);
9789 SDLoc dl(N);
9790 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9791 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +00009792 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009793 continue;
9794 if (V.getOpcode() == ISD::BITCAST &&
9795 V->getOperand(0).getValueType() == MVT::i32)
9796 // Fold obvious case.
9797 V = V.getOperand(0);
9798 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009799 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009800 // Make the DAGCombiner fold the bitcasts.
9801 DCI.AddToWorklist(V.getNode());
9802 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009803 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009804 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9805 }
9806 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9807 // Make the DAGCombiner fold the bitcasts.
9808 DCI.AddToWorklist(Vec.getNode());
9809 return Vec;
9810}
9811
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009812/// PerformInsertEltCombine - Target-specific dag combine xforms for
9813/// ISD::INSERT_VECTOR_ELT.
9814static SDValue PerformInsertEltCombine(SDNode *N,
9815 TargetLowering::DAGCombinerInfo &DCI) {
9816 // Bitcast an i64 load inserted into a vector to f64.
9817 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9818 EVT VT = N->getValueType(0);
9819 SDNode *Elt = N->getOperand(1).getNode();
9820 if (VT.getVectorElementType() != MVT::i64 ||
9821 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9822 return SDValue();
9823
9824 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009825 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009826 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9827 VT.getVectorNumElements());
9828 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9829 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9830 // Make the DAGCombiner fold the bitcasts.
9831 DCI.AddToWorklist(Vec.getNode());
9832 DCI.AddToWorklist(V.getNode());
9833 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9834 Vec, V, N->getOperand(2));
9835 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009836}
9837
Bob Wilsonc7334a12010-10-27 20:38:28 +00009838/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9839/// ISD::VECTOR_SHUFFLE.
9840static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9841 // The LLVM shufflevector instruction does not require the shuffle mask
9842 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9843 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9844 // operands do not match the mask length, they are extended by concatenating
9845 // them with undef vectors. That is probably the right thing for other
9846 // targets, but for NEON it is better to concatenate two double-register
9847 // size vector operands into a single quad-register size vector. Do that
9848 // transformation here:
9849 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9850 // shuffle(concat(v1, v2), undef)
9851 SDValue Op0 = N->getOperand(0);
9852 SDValue Op1 = N->getOperand(1);
9853 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9854 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9855 Op0.getNumOperands() != 2 ||
9856 Op1.getNumOperands() != 2)
9857 return SDValue();
9858 SDValue Concat0Op1 = Op0.getOperand(1);
9859 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00009860 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +00009861 return SDValue();
9862 // Skip the transformation if any of the types are illegal.
9863 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9864 EVT VT = N->getValueType(0);
9865 if (!TLI.isTypeLegal(VT) ||
9866 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9867 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9868 return SDValue();
9869
Andrew Trickef9de2a2013-05-25 02:42:55 +00009870 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009871 Op0.getOperand(0), Op1.getOperand(0));
9872 // Translate the shuffle mask.
9873 SmallVector<int, 16> NewMask;
9874 unsigned NumElts = VT.getVectorNumElements();
9875 unsigned HalfElts = NumElts/2;
9876 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9877 for (unsigned n = 0; n < NumElts; ++n) {
9878 int MaskElt = SVN->getMaskElt(n);
9879 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009880 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009881 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009882 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009883 NewElt = HalfElts + MaskElt - NumElts;
9884 NewMask.push_back(NewElt);
9885 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009886 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Craig Topper2bd8b4b2016-07-01 06:54:47 +00009887 DAG.getUNDEF(VT), NewMask);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009888}
9889
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009890/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9891/// NEON load/store intrinsics, and generic vector load/stores, to merge
9892/// base address updates.
9893/// For generic load/stores, the memory type is assumed to be a vector.
9894/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009895static SDValue CombineBaseUpdate(SDNode *N,
9896 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009897 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009898 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9899 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009900 const bool isStore = N->getOpcode() == ISD::STORE;
9901 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009902 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009903 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009904 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009905
9906 // Search for a use of the address operand that is an increment.
9907 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9908 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9909 SDNode *User = *UI;
9910 if (User->getOpcode() != ISD::ADD ||
9911 UI.getUse().getResNo() != Addr.getResNo())
9912 continue;
9913
9914 // Check that the add is independent of the load/store. Otherwise, folding
9915 // it would create a cycle.
9916 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9917 continue;
9918
9919 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009920 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009921 bool isLaneOp = false;
9922 unsigned NewOpc = 0;
9923 unsigned NumVecs = 0;
9924 if (isIntrinsic) {
9925 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9926 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009927 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009928 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9929 NumVecs = 1; break;
9930 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9931 NumVecs = 2; break;
9932 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9933 NumVecs = 3; break;
9934 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9935 NumVecs = 4; break;
9936 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9937 NumVecs = 2; isLaneOp = true; break;
9938 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9939 NumVecs = 3; isLaneOp = true; break;
9940 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9941 NumVecs = 4; isLaneOp = true; break;
9942 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009943 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009944 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009945 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009946 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009947 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009948 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009949 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009950 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009951 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009952 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009953 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009954 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009955 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009956 }
9957 } else {
9958 isLaneOp = true;
9959 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009960 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009961 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9962 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9963 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009964 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9965 NumVecs = 1; isLaneOp = false; break;
9966 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9967 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009968 }
9969 }
9970
9971 // Find the size of memory referenced by the load/store.
9972 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009973 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009974 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009975 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009976 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009977 } else {
9978 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9979 VecTy = N->getOperand(1).getValueType();
9980 }
9981
Bob Wilson06fce872011-02-07 17:43:21 +00009982 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9983 if (isLaneOp)
9984 NumBytes /= VecTy.getVectorNumElements();
9985
9986 // If the increment is a constant, it must match the memory ref size.
9987 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9988 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9989 uint64_t IncVal = CInc->getZExtValue();
9990 if (IncVal != NumBytes)
9991 continue;
9992 } else if (NumBytes >= 3 * 16) {
9993 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9994 // separate instructions that make it harder to use a non-constant update.
9995 continue;
9996 }
9997
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009998 // OK, we found an ADD we can fold into the base update.
9999 // Now, create a _UPD node, taking care of not breaking alignment.
10000
10001 EVT AlignedVecTy = VecTy;
10002 unsigned Alignment = MemN->getAlignment();
10003
10004 // If this is a less-than-standard-aligned load/store, change the type to
10005 // match the standard alignment.
10006 // The alignment is overlooked when selecting _UPD variants; and it's
10007 // easier to introduce bitcasts here than fix that.
10008 // There are 3 ways to get to this base-update combine:
10009 // - intrinsics: they are assumed to be properly aligned (to the standard
10010 // alignment of the memory type), so we don't need to do anything.
10011 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
10012 // intrinsics, so, likewise, there's nothing to do.
10013 // - generic load/store instructions: the alignment is specified as an
10014 // explicit operand, rather than implicitly as the standard alignment
10015 // of the memory type (like the intrisics). We need to change the
10016 // memory type to match the explicit alignment. That way, we don't
10017 // generate non-standard-aligned ARMISD::VLDx nodes.
10018 if (isa<LSBaseSDNode>(N)) {
10019 if (Alignment == 0)
10020 Alignment = 1;
10021 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
10022 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
10023 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
10024 assert(!isLaneOp && "Unexpected generic load/store lane.");
10025 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
10026 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
10027 }
10028 // Don't set an explicit alignment on regular load/stores that we want
10029 // to transform to VLD/VST 1_UPD nodes.
10030 // This matches the behavior of regular load/stores, which only get an
10031 // explicit alignment if the MMO alignment is larger than the standard
10032 // alignment of the memory type.
10033 // Intrinsics, however, always get an explicit alignment, set to the
10034 // alignment of the MMO.
10035 Alignment = 1;
10036 }
10037
Bob Wilson06fce872011-02-07 17:43:21 +000010038 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010039 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +000010040 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010041 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +000010042 unsigned n;
10043 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010044 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +000010045 Tys[n++] = MVT::i32;
10046 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +000010047 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010048
10049 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +000010050 SmallVector<SDValue, 8> Ops;
10051 Ops.push_back(N->getOperand(0)); // incoming chain
10052 Ops.push_back(N->getOperand(AddrOpIdx));
10053 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010054
10055 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
10056 // Try to match the intrinsic's signature
10057 Ops.push_back(StN->getValue());
10058 } else {
10059 // Loads (and of course intrinsics) match the intrinsics' signature,
10060 // so just add all but the alignment operand.
10061 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
10062 Ops.push_back(N->getOperand(i));
10063 }
10064
10065 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010066 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010067
10068 // If this is a non-standard-aligned STORE, the penultimate operand is the
10069 // stored value. Bitcast it to the aligned type.
10070 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
10071 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010072 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010073 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010074
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010075 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010076 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010077 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +000010078
10079 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +000010080 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010081 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +000010082 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010083
10084 // If this is an non-standard-aligned LOAD, the first result is the loaded
10085 // value. Bitcast it to the expected result type.
10086 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
10087 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010088 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010089 }
10090
Bob Wilson06fce872011-02-07 17:43:21 +000010091 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
10092 DCI.CombineTo(N, NewResults);
10093 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
10094
10095 break;
Owen Anderson77aa2662011-04-05 21:48:57 +000010096 }
Bob Wilson06fce872011-02-07 17:43:21 +000010097 return SDValue();
10098}
10099
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010100static SDValue PerformVLDCombine(SDNode *N,
10101 TargetLowering::DAGCombinerInfo &DCI) {
10102 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10103 return SDValue();
10104
10105 return CombineBaseUpdate(N, DCI);
10106}
10107
Bob Wilson2d790df2010-11-28 06:51:26 +000010108/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
10109/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
10110/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
10111/// return true.
10112static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
10113 SelectionDAG &DAG = DCI.DAG;
10114 EVT VT = N->getValueType(0);
10115 // vldN-dup instructions only support 64-bit vectors for N > 1.
10116 if (!VT.is64BitVector())
10117 return false;
10118
10119 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
10120 SDNode *VLD = N->getOperand(0).getNode();
10121 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
10122 return false;
10123 unsigned NumVecs = 0;
10124 unsigned NewOpc = 0;
10125 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
10126 if (IntNo == Intrinsic::arm_neon_vld2lane) {
10127 NumVecs = 2;
10128 NewOpc = ARMISD::VLD2DUP;
10129 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
10130 NumVecs = 3;
10131 NewOpc = ARMISD::VLD3DUP;
10132 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
10133 NumVecs = 4;
10134 NewOpc = ARMISD::VLD4DUP;
10135 } else {
10136 return false;
10137 }
10138
10139 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
10140 // numbers match the load.
10141 unsigned VLDLaneNo =
10142 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
10143 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
10144 UI != UE; ++UI) {
10145 // Ignore uses of the chain result.
10146 if (UI.getUse().getResNo() == NumVecs)
10147 continue;
10148 SDNode *User = *UI;
10149 if (User->getOpcode() != ARMISD::VDUPLANE ||
10150 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
10151 return false;
10152 }
10153
10154 // Create the vldN-dup node.
10155 EVT Tys[5];
10156 unsigned n;
10157 for (n = 0; n < NumVecs; ++n)
10158 Tys[n] = VT;
10159 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +000010160 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +000010161 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
10162 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010163 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +000010164 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +000010165 VLDMemInt->getMemOperand());
10166
10167 // Update the uses.
10168 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
10169 UI != UE; ++UI) {
10170 unsigned ResNo = UI.getUse().getResNo();
10171 // Ignore uses of the chain result.
10172 if (ResNo == NumVecs)
10173 continue;
10174 SDNode *User = *UI;
10175 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
10176 }
10177
10178 // Now the vldN-lane intrinsic is dead except for its chain result.
10179 // Update uses of the chain.
10180 std::vector<SDValue> VLDDupResults;
10181 for (unsigned n = 0; n < NumVecs; ++n)
10182 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
10183 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
10184 DCI.CombineTo(VLD, VLDDupResults);
10185
10186 return true;
10187}
10188
Bob Wilson103a0dc2010-07-14 01:22:12 +000010189/// PerformVDUPLANECombine - Target-specific dag combine xforms for
10190/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +000010191static SDValue PerformVDUPLANECombine(SDNode *N,
10192 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +000010193 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010194
Bob Wilson2d790df2010-11-28 06:51:26 +000010195 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
10196 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
10197 if (CombineVLDDUP(N, DCI))
10198 return SDValue(N, 0);
10199
10200 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
10201 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +000010202 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010203 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +000010204 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010205 return SDValue();
10206
10207 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
10208 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
10209 // The canonical VMOV for a zero vector uses a 32-bit element size.
10210 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10211 unsigned EltBits;
10212 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
10213 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +000010214 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010215 if (EltSize > VT.getVectorElementType().getSizeInBits())
10216 return SDValue();
10217
Andrew Trickef9de2a2013-05-25 02:42:55 +000010218 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010219}
10220
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010221static SDValue PerformLOADCombine(SDNode *N,
10222 TargetLowering::DAGCombinerInfo &DCI) {
10223 EVT VT = N->getValueType(0);
10224
10225 // If this is a legal vector load, try to combine it into a VLD1_UPD.
10226 if (ISD::isNormalLoad(N) && VT.isVector() &&
10227 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10228 return CombineBaseUpdate(N, DCI);
10229
10230 return SDValue();
10231}
10232
Ahmed Bougacha23167462014-12-09 21:26:53 +000010233/// PerformSTORECombine - Target-specific dag combine xforms for
10234/// ISD::STORE.
10235static SDValue PerformSTORECombine(SDNode *N,
10236 TargetLowering::DAGCombinerInfo &DCI) {
10237 StoreSDNode *St = cast<StoreSDNode>(N);
10238 if (St->isVolatile())
10239 return SDValue();
10240
10241 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
10242 // pack all of the elements in one place. Next, store to memory in fewer
10243 // chunks.
10244 SDValue StVal = St->getValue();
10245 EVT VT = StVal.getValueType();
10246 if (St->isTruncatingStore() && VT.isVector()) {
10247 SelectionDAG &DAG = DCI.DAG;
10248 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10249 EVT StVT = St->getMemoryVT();
10250 unsigned NumElems = VT.getVectorNumElements();
10251 assert(StVT != VT && "Cannot truncate to the same type");
10252 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
10253 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
10254
10255 // From, To sizes and ElemCount must be pow of two
10256 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
10257
10258 // We are going to use the original vector elt for storing.
10259 // Accumulated smaller vector elements must be a multiple of the store size.
10260 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
10261
10262 unsigned SizeRatio = FromEltSz / ToEltSz;
10263 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
10264
10265 // Create a type on which we perform the shuffle.
10266 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
10267 NumElems*SizeRatio);
10268 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
10269
10270 SDLoc DL(St);
10271 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
10272 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
10273 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +000010274 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
10275 ? (i + 1) * SizeRatio - 1
10276 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +000010277
10278 // Can't shuffle using an illegal type.
10279 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10280
10281 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10282 DAG.getUNDEF(WideVec.getValueType()),
Craig Topper2bd8b4b2016-07-01 06:54:47 +000010283 ShuffleVec);
Ahmed Bougacha23167462014-12-09 21:26:53 +000010284 // At this point all of the data is stored at the bottom of the
10285 // register. We now need to save it to mem.
10286
10287 // Find the largest store unit
10288 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010289 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010290 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10291 StoreType = Tp;
10292 }
10293 // Didn't find a legal store type.
10294 if (!TLI.isTypeLegal(StoreType))
10295 return SDValue();
10296
10297 // Bitcast the original vector into a vector of store-size units
10298 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10299 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10300 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10301 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10302 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010303 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10304 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010305 SDValue BasePtr = St->getBasePtr();
10306
10307 // Perform one or more big stores into memory.
10308 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10309 for (unsigned I = 0; I < E; I++) {
10310 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10311 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010312 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010313 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
Justin Lebar9c375812016-07-15 18:27:10 +000010314 St->getPointerInfo(), St->getAlignment(),
10315 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010316 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10317 Increment);
10318 Chains.push_back(Ch);
10319 }
10320 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10321 }
10322
10323 if (!ISD::isNormalStore(St))
10324 return SDValue();
10325
10326 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10327 // ARM stores of arguments in the same cache line.
10328 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10329 StVal.getNode()->hasOneUse()) {
10330 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010331 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010332 SDLoc DL(St);
10333 SDValue BasePtr = St->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +000010334 SDValue NewST1 = DAG.getStore(
10335 St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0),
10336 BasePtr, St->getPointerInfo(), St->getAlignment(),
10337 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010338
10339 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010340 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010341 return DAG.getStore(NewST1.getValue(0), DL,
10342 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Justin Lebar9c375812016-07-15 18:27:10 +000010343 OffsetPtr, St->getPointerInfo(),
10344 std::min(4U, St->getAlignment() / 2),
10345 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010346 }
10347
10348 if (StVal.getValueType() == MVT::i64 &&
10349 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10350
10351 // Bitcast an i64 store extracted from a vector to f64.
10352 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10353 SelectionDAG &DAG = DCI.DAG;
10354 SDLoc dl(StVal);
10355 SDValue IntVec = StVal.getOperand(0);
10356 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10357 IntVec.getValueType().getVectorNumElements());
10358 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10359 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10360 Vec, StVal.getOperand(1));
10361 dl = SDLoc(N);
10362 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10363 // Make the DAGCombiner fold the bitcasts.
10364 DCI.AddToWorklist(Vec.getNode());
10365 DCI.AddToWorklist(ExtElt.getNode());
10366 DCI.AddToWorklist(V.getNode());
10367 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +000010368 St->getPointerInfo(), St->getAlignment(),
10369 St->getMemOperand()->getFlags(), St->getAAInfo());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010370 }
10371
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010372 // If this is a legal vector store, try to combine it into a VST1_UPD.
10373 if (ISD::isNormalStore(N) && VT.isVector() &&
10374 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10375 return CombineBaseUpdate(N, DCI);
10376
Ahmed Bougacha23167462014-12-09 21:26:53 +000010377 return SDValue();
10378}
10379
Chad Rosierfa8d8932011-06-24 19:23:04 +000010380/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10381/// can replace combinations of VMUL and VCVT (floating-point to integer)
10382/// when the VMUL has a constant operand that is a power of 2.
10383///
10384/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10385/// vmul.f32 d16, d17, d16
10386/// vcvt.s32.f32 d16, d16
10387/// becomes:
10388/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010389static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010390 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010391 if (!Subtarget->hasNEON())
10392 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010393
Chad Rosiera087fd22015-10-06 20:23:42 +000010394 SDValue Op = N->getOperand(0);
Tim Northover498c56c2016-03-17 20:10:28 +000010395 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
10396 Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010397 return SDValue();
10398
Chad Rosierfa8d8932011-06-24 19:23:04 +000010399 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010400 if (!isa<BuildVectorSDNode>(ConstVec))
10401 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010402
Tim Northover7cbc2152013-06-28 15:29:25 +000010403 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010404 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010405 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010406 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010407 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010408 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010409 // These instructions only exist converting from f32 to i32. We can handle
10410 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010411 // be lossy. We also can't handle more then 4 lanes, since these intructions
10412 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010413 return SDValue();
10414 }
10415
Chad Rosier169865f2015-10-07 17:28:58 +000010416 BitVector UndefElements;
10417 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10418 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10419 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010420 return SDValue();
10421
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010422 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010423 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010424 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10425 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010426 SDValue FixConv = DAG.getNode(
10427 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10428 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010429 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010430
Chad Rosier9df4aff2015-10-06 20:45:45 +000010431 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010432 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010433
10434 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010435}
10436
10437/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10438/// can replace combinations of VCVT (integer to floating-point) and VDIV
10439/// when the VDIV has a constant operand that is a power of 2.
10440///
10441/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10442/// vcvt.f32.s32 d16, d16
10443/// vdiv.f32 d16, d17, d16
10444/// becomes:
10445/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010446static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010447 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010448 if (!Subtarget->hasNEON())
10449 return SDValue();
10450
Chad Rosierfa8d8932011-06-24 19:23:04 +000010451 SDValue Op = N->getOperand(0);
10452 unsigned OpOpcode = Op.getNode()->getOpcode();
Tim Northover498c56c2016-03-17 20:10:28 +000010453 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010454 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10455 return SDValue();
10456
Chad Rosierfa8d8932011-06-24 19:23:04 +000010457 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010458 if (!isa<BuildVectorSDNode>(ConstVec))
10459 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010460
Tim Northover7cbc2152013-06-28 15:29:25 +000010461 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010462 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010463 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010464 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010465 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10466 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010467 // These instructions only exist converting from i32 to f32. We can handle
10468 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010469 // be lossy. We also can't handle more then 4 lanes, since these intructions
10470 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010471 return SDValue();
10472 }
10473
Chad Rosier169865f2015-10-07 17:28:58 +000010474 BitVector UndefElements;
10475 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10476 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10477 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010478 return SDValue();
10479
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010480 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010481 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010482 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010483 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010484 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010485 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010486 ConvInput);
10487
Eric Christopher1b8b94192011-06-29 21:10:36 +000010488 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010489 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010490 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010491 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010492 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010493 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010494}
10495
10496/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010497/// operand of a vector shift operation, where all the elements of the
10498/// build_vector must have the same constant integer value.
10499static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10500 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010501 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010502 Op = Op.getOperand(0);
10503 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10504 APInt SplatBits, SplatUndef;
10505 unsigned SplatBitSize;
10506 bool HasAnyUndefs;
10507 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10508 HasAnyUndefs, ElementBits) ||
10509 SplatBitSize > ElementBits)
10510 return false;
10511 Cnt = SplatBits.getSExtValue();
10512 return true;
10513}
10514
10515/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10516/// operand of a vector shift left operation. That value must be in the range:
10517/// 0 <= Value < ElementBits for a left shift; or
10518/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010519static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010520 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010521 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010522 if (! getVShiftImm(Op, ElementBits, Cnt))
10523 return false;
10524 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10525}
10526
10527/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10528/// operand of a vector shift right operation. For a shift opcode, the value
10529/// is positive, but for an intrinsic the value count must be negative. The
10530/// absolute value must be in the range:
10531/// 1 <= |Value| <= ElementBits for a right shift; or
10532/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010533static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010534 int64_t &Cnt) {
10535 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010536 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010537 if (! getVShiftImm(Op, ElementBits, Cnt))
10538 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010539 if (!isIntrinsic)
10540 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10541 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010542 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010543 return true;
10544 }
10545 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010546}
10547
10548/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10549static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10550 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10551 switch (IntNo) {
10552 default:
10553 // Don't do anything for most intrinsics.
10554 break;
10555
10556 // Vector shifts: check for immediate versions and lower them.
10557 // Note: This is done during DAG combining instead of DAG legalizing because
10558 // the build_vectors for 64-bit vector element shift counts are generally
10559 // not legal, and it is hard to see their values after they get legalized to
10560 // loads from a constant pool.
10561 case Intrinsic::arm_neon_vshifts:
10562 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010563 case Intrinsic::arm_neon_vrshifts:
10564 case Intrinsic::arm_neon_vrshiftu:
10565 case Intrinsic::arm_neon_vrshiftn:
10566 case Intrinsic::arm_neon_vqshifts:
10567 case Intrinsic::arm_neon_vqshiftu:
10568 case Intrinsic::arm_neon_vqshiftsu:
10569 case Intrinsic::arm_neon_vqshiftns:
10570 case Intrinsic::arm_neon_vqshiftnu:
10571 case Intrinsic::arm_neon_vqshiftnsu:
10572 case Intrinsic::arm_neon_vqrshiftns:
10573 case Intrinsic::arm_neon_vqrshiftnu:
10574 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010575 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010576 int64_t Cnt;
10577 unsigned VShiftOpc = 0;
10578
10579 switch (IntNo) {
10580 case Intrinsic::arm_neon_vshifts:
10581 case Intrinsic::arm_neon_vshiftu:
10582 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10583 VShiftOpc = ARMISD::VSHL;
10584 break;
10585 }
10586 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10587 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10588 ARMISD::VSHRs : ARMISD::VSHRu);
10589 break;
10590 }
10591 return SDValue();
10592
Bob Wilson2e076c42009-06-22 23:27:02 +000010593 case Intrinsic::arm_neon_vrshifts:
10594 case Intrinsic::arm_neon_vrshiftu:
10595 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10596 break;
10597 return SDValue();
10598
10599 case Intrinsic::arm_neon_vqshifts:
10600 case Intrinsic::arm_neon_vqshiftu:
10601 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10602 break;
10603 return SDValue();
10604
10605 case Intrinsic::arm_neon_vqshiftsu:
10606 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10607 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010608 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010609
Bob Wilson2e076c42009-06-22 23:27:02 +000010610 case Intrinsic::arm_neon_vrshiftn:
10611 case Intrinsic::arm_neon_vqshiftns:
10612 case Intrinsic::arm_neon_vqshiftnu:
10613 case Intrinsic::arm_neon_vqshiftnsu:
10614 case Intrinsic::arm_neon_vqrshiftns:
10615 case Intrinsic::arm_neon_vqrshiftnu:
10616 case Intrinsic::arm_neon_vqrshiftnsu:
10617 // Narrowing shifts require an immediate right shift.
10618 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10619 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010620 llvm_unreachable("invalid shift count for narrowing vector shift "
10621 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010622
10623 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010624 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010625 }
10626
10627 switch (IntNo) {
10628 case Intrinsic::arm_neon_vshifts:
10629 case Intrinsic::arm_neon_vshiftu:
10630 // Opcode already set above.
10631 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010632 case Intrinsic::arm_neon_vrshifts:
10633 VShiftOpc = ARMISD::VRSHRs; break;
10634 case Intrinsic::arm_neon_vrshiftu:
10635 VShiftOpc = ARMISD::VRSHRu; break;
10636 case Intrinsic::arm_neon_vrshiftn:
10637 VShiftOpc = ARMISD::VRSHRN; break;
10638 case Intrinsic::arm_neon_vqshifts:
10639 VShiftOpc = ARMISD::VQSHLs; break;
10640 case Intrinsic::arm_neon_vqshiftu:
10641 VShiftOpc = ARMISD::VQSHLu; break;
10642 case Intrinsic::arm_neon_vqshiftsu:
10643 VShiftOpc = ARMISD::VQSHLsu; break;
10644 case Intrinsic::arm_neon_vqshiftns:
10645 VShiftOpc = ARMISD::VQSHRNs; break;
10646 case Intrinsic::arm_neon_vqshiftnu:
10647 VShiftOpc = ARMISD::VQSHRNu; break;
10648 case Intrinsic::arm_neon_vqshiftnsu:
10649 VShiftOpc = ARMISD::VQSHRNsu; break;
10650 case Intrinsic::arm_neon_vqrshiftns:
10651 VShiftOpc = ARMISD::VQRSHRNs; break;
10652 case Intrinsic::arm_neon_vqrshiftnu:
10653 VShiftOpc = ARMISD::VQRSHRNu; break;
10654 case Intrinsic::arm_neon_vqrshiftnsu:
10655 VShiftOpc = ARMISD::VQRSHRNsu; break;
10656 }
10657
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010658 SDLoc dl(N);
10659 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10660 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010661 }
10662
10663 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010664 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010665 int64_t Cnt;
10666 unsigned VShiftOpc = 0;
10667
10668 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10669 VShiftOpc = ARMISD::VSLI;
10670 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10671 VShiftOpc = ARMISD::VSRI;
10672 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010673 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010674 }
10675
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010676 SDLoc dl(N);
10677 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010678 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010679 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010680 }
10681
10682 case Intrinsic::arm_neon_vqrshifts:
10683 case Intrinsic::arm_neon_vqrshiftu:
10684 // No immediate versions of these to check for.
10685 break;
10686 }
10687
10688 return SDValue();
10689}
10690
10691/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10692/// lowers them. As with the vector shift intrinsics, this is done during DAG
10693/// combining instead of DAG legalizing because the build_vectors for 64-bit
10694/// vector element shift counts are generally not legal, and it is hard to see
10695/// their values after they get legalized to loads from a constant pool.
10696static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10697 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010698 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010699 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10700 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10701 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10702 SDValue N1 = N->getOperand(1);
10703 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10704 SDValue N0 = N->getOperand(0);
10705 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10706 DAG.MaskedValueIsZero(N0.getOperand(0),
10707 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010708 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010709 }
10710 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010711
10712 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010713 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10714 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010715 return SDValue();
10716
10717 assert(ST->hasNEON() && "unexpected vector shift");
10718 int64_t Cnt;
10719
10720 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010721 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010722
10723 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010724 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10725 SDLoc dl(N);
10726 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10727 DAG.getConstant(Cnt, dl, MVT::i32));
10728 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010729 break;
10730
10731 case ISD::SRA:
10732 case ISD::SRL:
10733 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10734 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10735 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010736 SDLoc dl(N);
10737 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10738 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010739 }
10740 }
10741 return SDValue();
10742}
10743
10744/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10745/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10746static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10747 const ARMSubtarget *ST) {
10748 SDValue N0 = N->getOperand(0);
10749
10750 // Check for sign- and zero-extensions of vector extract operations of 8-
10751 // and 16-bit vector elements. NEON supports these directly. They are
10752 // handled during DAG combining because type legalization will promote them
10753 // to 32-bit types and it is messy to recognize the operations after that.
10754 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10755 SDValue Vec = N0.getOperand(0);
10756 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010757 EVT VT = N->getValueType(0);
10758 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010759 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10760
Owen Anderson9f944592009-08-11 20:47:22 +000010761 if (VT == MVT::i32 &&
10762 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010763 TLI.isTypeLegal(Vec.getValueType()) &&
10764 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010765
10766 unsigned Opc = 0;
10767 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010768 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010769 case ISD::SIGN_EXTEND:
10770 Opc = ARMISD::VGETLANEs;
10771 break;
10772 case ISD::ZERO_EXTEND:
10773 case ISD::ANY_EXTEND:
10774 Opc = ARMISD::VGETLANEu;
10775 break;
10776 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010777 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010778 }
10779 }
10780
10781 return SDValue();
10782}
10783
James Molloy9d55f192015-11-10 14:22:05 +000010784static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10785 APInt &KnownOne) {
10786 if (Op.getOpcode() == ARMISD::BFI) {
10787 // Conservatively, we can recurse down the first operand
10788 // and just mask out all affected bits.
10789 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10790
10791 // The operand to BFI is already a mask suitable for removing the bits it
10792 // sets.
10793 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
Benjamin Kramer46e38f32016-06-08 10:01:20 +000010794 const APInt &Mask = CI->getAPIntValue();
James Molloy9d55f192015-11-10 14:22:05 +000010795 KnownZero &= Mask;
10796 KnownOne &= Mask;
10797 return;
10798 }
10799 if (Op.getOpcode() == ARMISD::CMOV) {
10800 APInt KZ2(KnownZero.getBitWidth(), 0);
10801 APInt KO2(KnownOne.getBitWidth(), 0);
10802 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10803 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10804
10805 KnownZero &= KZ2;
10806 KnownOne &= KO2;
10807 return;
10808 }
10809 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10810}
10811
10812SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10813 // If we have a CMOV, OR and AND combination such as:
10814 // if (x & CN)
10815 // y |= CM;
10816 //
10817 // And:
10818 // * CN is a single bit;
10819 // * All bits covered by CM are known zero in y
10820 //
10821 // Then we can convert this into a sequence of BFI instructions. This will
10822 // always be a win if CM is a single bit, will always be no worse than the
10823 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10824 // three bits (due to the extra IT instruction).
10825
10826 SDValue Op0 = CMOV->getOperand(0);
10827 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010828 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10829 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010830 SDValue CmpZ = CMOV->getOperand(4);
10831
James Molloy20180912015-11-16 10:49:25 +000010832 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010833 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010834 return SDValue();
10835
James Molloy9d55f192015-11-10 14:22:05 +000010836 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10837 SDValue And = CmpZ->getOperand(0);
10838 if (And->getOpcode() != ISD::AND)
10839 return SDValue();
10840 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10841 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10842 return SDValue();
10843 SDValue X = And->getOperand(0);
10844
James Molloy8e99e972015-11-12 13:49:17 +000010845 if (CC == ARMCC::EQ) {
10846 // We're performing an "equal to zero" compare. Swap the operands so we
10847 // canonicalize on a "not equal to zero" compare.
10848 std::swap(Op0, Op1);
10849 } else {
10850 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10851 }
Junmo Park1108ab02016-02-19 01:46:04 +000010852
James Molloy9d55f192015-11-10 14:22:05 +000010853 if (Op1->getOpcode() != ISD::OR)
10854 return SDValue();
10855
10856 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10857 if (!OrC)
10858 return SDValue();
10859 SDValue Y = Op1->getOperand(0);
10860
10861 if (Op0 != Y)
10862 return SDValue();
10863
10864 // Now, is it profitable to continue?
10865 APInt OrCI = OrC->getAPIntValue();
10866 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10867 if (OrCI.countPopulation() > Heuristic)
10868 return SDValue();
10869
10870 // Lastly, can we determine that the bits defined by OrCI
10871 // are zero in Y?
10872 APInt KnownZero, KnownOne;
10873 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10874 if ((OrCI & KnownZero) != OrCI)
10875 return SDValue();
10876
10877 // OK, we can do the combine.
10878 SDValue V = Y;
10879 SDLoc dl(X);
10880 EVT VT = X.getValueType();
10881 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000010882
James Molloy9d55f192015-11-10 14:22:05 +000010883 if (BitInX != 0) {
10884 // We must shift X first.
10885 X = DAG.getNode(ISD::SRL, dl, VT, X,
10886 DAG.getConstant(BitInX, dl, VT));
10887 }
10888
10889 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10890 BitInY < NumActiveBits; ++BitInY) {
10891 if (OrCI[BitInY] == 0)
10892 continue;
10893 APInt Mask(VT.getSizeInBits(), 0);
10894 Mask.setBit(BitInY);
10895 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10896 // Confusingly, the operand is an *inverted* mask.
10897 DAG.getConstant(~Mask, dl, VT));
10898 }
10899
10900 return V;
10901}
10902
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010903/// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND.
10904SDValue
10905ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const {
10906 SDValue Cmp = N->getOperand(4);
10907 if (Cmp.getOpcode() != ARMISD::CMPZ)
10908 // Only looking at NE cases.
10909 return SDValue();
10910
10911 EVT VT = N->getValueType(0);
10912 SDLoc dl(N);
10913 SDValue LHS = Cmp.getOperand(0);
10914 SDValue RHS = Cmp.getOperand(1);
10915 SDValue Chain = N->getOperand(0);
10916 SDValue BB = N->getOperand(1);
10917 SDValue ARMcc = N->getOperand(2);
10918 ARMCC::CondCodes CC =
10919 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
10920
10921 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0))
10922 // -> (brcond Chain BB CC CPSR Cmp)
10923 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() &&
10924 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV &&
10925 LHS->getOperand(0)->hasOneUse()) {
10926 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0));
10927 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1));
10928 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
10929 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
10930 if ((LHS00C && LHS00C->getZExtValue() == 0) &&
10931 (LHS01C && LHS01C->getZExtValue() == 1) &&
10932 (LHS1C && LHS1C->getZExtValue() == 1) &&
10933 (RHSC && RHSC->getZExtValue() == 0)) {
10934 return DAG.getNode(
10935 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2),
10936 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4));
10937 }
10938 }
10939
10940 return SDValue();
10941}
10942
Evan Chengf863e3f2011-07-13 00:42:17 +000010943/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10944SDValue
10945ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10946 SDValue Cmp = N->getOperand(4);
10947 if (Cmp.getOpcode() != ARMISD::CMPZ)
10948 // Only looking at EQ and NE cases.
10949 return SDValue();
10950
10951 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010952 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010953 SDValue LHS = Cmp.getOperand(0);
10954 SDValue RHS = Cmp.getOperand(1);
10955 SDValue FalseVal = N->getOperand(0);
10956 SDValue TrueVal = N->getOperand(1);
10957 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010958 ARMCC::CondCodes CC =
10959 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010960
James Molloy9d55f192015-11-10 14:22:05 +000010961 // BFI is only available on V6T2+.
10962 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10963 SDValue R = PerformCMOVToBFICombine(N, DAG);
10964 if (R)
10965 return R;
10966 }
10967
Evan Chengf863e3f2011-07-13 00:42:17 +000010968 // Simplify
10969 // mov r1, r0
10970 // cmp r1, x
10971 // mov r0, y
10972 // moveq r0, x
10973 // to
10974 // cmp r0, x
10975 // movne r0, y
10976 //
10977 // mov r1, r0
10978 // cmp r1, x
10979 // mov r0, x
10980 // movne r0, y
10981 // to
10982 // cmp r0, x
10983 // movne r0, y
10984 /// FIXME: Turn this into a target neutral optimization?
10985 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010986 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010987 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10988 N->getOperand(3), Cmp);
10989 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10990 SDValue ARMcc;
10991 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10992 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10993 N->getOperand(3), NewCmp);
10994 }
10995
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010996 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0))
10997 // -> (cmov F T CC CPSR Cmp)
10998 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) {
10999 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0));
11000 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
11001 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
11002 if ((LHS0C && LHS0C->getZExtValue() == 0) &&
11003 (LHS1C && LHS1C->getZExtValue() == 1) &&
11004 (RHSC && RHSC->getZExtValue() == 0)) {
11005 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
11006 LHS->getOperand(2), LHS->getOperand(3),
11007 LHS->getOperand(4));
11008 }
11009 }
11010
Evan Chengf863e3f2011-07-13 00:42:17 +000011011 if (Res.getNode()) {
11012 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000011013 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000011014 // Capture demanded bits information that would be otherwise lost.
11015 if (KnownZero == 0xfffffffe)
11016 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11017 DAG.getValueType(MVT::i1));
11018 else if (KnownZero == 0xffffff00)
11019 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11020 DAG.getValueType(MVT::i8));
11021 else if (KnownZero == 0xffff0000)
11022 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11023 DAG.getValueType(MVT::i16));
11024 }
11025
11026 return Res;
11027}
11028
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011029SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000011030 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011031 switch (N->getOpcode()) {
11032 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000011033 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000011034 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011035 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000011036 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011037 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000011038 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
11039 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000011040 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000011041 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000011042 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000011043 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000011044 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000011045 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000011046 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000011047 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000011048 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000011049 case ISD::FP_TO_UINT:
11050 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
11051 case ISD::FDIV:
11052 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011053 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000011054 case ISD::SHL:
11055 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011056 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000011057 case ISD::SIGN_EXTEND:
11058 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011059 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000011060 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011061 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011062 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011063 case ARMISD::VLD2DUP:
11064 case ARMISD::VLD3DUP:
11065 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011066 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000011067 case ARMISD::BUILD_VECTOR:
11068 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011069 case ISD::INTRINSIC_VOID:
11070 case ISD::INTRINSIC_W_CHAIN:
11071 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
11072 case Intrinsic::arm_neon_vld1:
11073 case Intrinsic::arm_neon_vld2:
11074 case Intrinsic::arm_neon_vld3:
11075 case Intrinsic::arm_neon_vld4:
11076 case Intrinsic::arm_neon_vld2lane:
11077 case Intrinsic::arm_neon_vld3lane:
11078 case Intrinsic::arm_neon_vld4lane:
11079 case Intrinsic::arm_neon_vst1:
11080 case Intrinsic::arm_neon_vst2:
11081 case Intrinsic::arm_neon_vst3:
11082 case Intrinsic::arm_neon_vst4:
11083 case Intrinsic::arm_neon_vst2lane:
11084 case Intrinsic::arm_neon_vst3lane:
11085 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011086 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011087 default: break;
11088 }
11089 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011090 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011091 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011092}
11093
Evan Chengd42641c2011-02-02 01:06:55 +000011094bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
11095 EVT VT) const {
11096 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
11097}
11098
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011099bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11100 unsigned,
11101 unsigned,
11102 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011103 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000011104 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000011105
11106 switch (VT.getSimpleVT().SimpleTy) {
11107 default:
11108 return false;
11109 case MVT::i8:
11110 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000011111 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011112 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000011113 if (AllowsUnaligned) {
11114 if (Fast)
11115 *Fast = Subtarget->hasV7Ops();
11116 return true;
11117 }
11118 return false;
11119 }
Evan Chengeec6bc62012-08-15 17:44:53 +000011120 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000011121 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011122 // For any little-endian targets with neon, we can support unaligned ld/st
11123 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000011124 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000011125 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011126 if (Fast)
11127 *Fast = true;
11128 return true;
11129 }
11130 return false;
11131 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000011132 }
11133}
11134
Lang Hames9929c422011-11-02 22:52:45 +000011135static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
11136 unsigned AlignCheck) {
11137 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
11138 (DstAlign == 0 || DstAlign % AlignCheck == 0));
11139}
11140
11141EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
11142 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011143 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000011144 bool MemcpyStrSrc,
11145 MachineFunction &MF) const {
11146 const Function *F = MF.getFunction();
11147
11148 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000011149 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
11150 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011151 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000011152 if (Size >= 16 &&
11153 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011154 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011155 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000011156 } else if (Size >= 8 &&
11157 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011158 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
11159 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011160 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000011161 }
11162 }
11163
Lang Hamesb85fcd02011-11-08 18:56:23 +000011164 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000011165 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000011166 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000011167 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000011168 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000011169
Lang Hames9929c422011-11-02 22:52:45 +000011170 // Let the target-independent logic figure it out.
11171 return MVT::Other;
11172}
11173
Evan Cheng9ec512d2012-12-06 19:13:27 +000011174bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11175 if (Val.getOpcode() != ISD::LOAD)
11176 return false;
11177
11178 EVT VT1 = Val.getValueType();
11179 if (!VT1.isSimple() || !VT1.isInteger() ||
11180 !VT2.isSimple() || !VT2.isInteger())
11181 return false;
11182
11183 switch (VT1.getSimpleVT().SimpleTy) {
11184 default: break;
11185 case MVT::i1:
11186 case MVT::i8:
11187 case MVT::i16:
11188 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
11189 return true;
11190 }
11191
11192 return false;
11193}
11194
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000011195bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
11196 EVT VT = ExtVal.getValueType();
11197
11198 if (!isTypeLegal(VT))
11199 return false;
11200
11201 // Don't create a loadext if we can fold the extension into a wide/long
11202 // instruction.
11203 // If there's more than one user instruction, the loadext is desirable no
11204 // matter what. There can be two uses by the same instruction.
11205 if (ExtVal->use_empty() ||
11206 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
11207 return true;
11208
11209 SDNode *U = *ExtVal->use_begin();
11210 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
11211 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
11212 return false;
11213
11214 return true;
11215}
11216
Tim Northovercc2e9032013-08-06 13:58:03 +000011217bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
11218 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11219 return false;
11220
11221 if (!isTypeLegal(EVT::getEVT(Ty1)))
11222 return false;
11223
11224 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
11225
11226 // Assuming the caller doesn't have a zeroext or signext return parameter,
11227 // truncation all the way down to i1 is valid.
11228 return true;
11229}
11230
11231
Evan Chengdc49a8d2009-08-14 20:09:37 +000011232static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
11233 if (V < 0)
11234 return false;
11235
11236 unsigned Scale = 1;
11237 switch (VT.getSimpleVT().SimpleTy) {
11238 default: return false;
11239 case MVT::i1:
11240 case MVT::i8:
11241 // Scale == 1;
11242 break;
11243 case MVT::i16:
11244 // Scale == 2;
11245 Scale = 2;
11246 break;
11247 case MVT::i32:
11248 // Scale == 4;
11249 Scale = 4;
11250 break;
11251 }
11252
11253 if ((V & (Scale - 1)) != 0)
11254 return false;
11255 V /= Scale;
11256 return V == (V & ((1LL << 5) - 1));
11257}
11258
11259static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
11260 const ARMSubtarget *Subtarget) {
11261 bool isNeg = false;
11262 if (V < 0) {
11263 isNeg = true;
11264 V = - V;
11265 }
11266
11267 switch (VT.getSimpleVT().SimpleTy) {
11268 default: return false;
11269 case MVT::i1:
11270 case MVT::i8:
11271 case MVT::i16:
11272 case MVT::i32:
11273 // + imm12 or - imm8
11274 if (isNeg)
11275 return V == (V & ((1LL << 8) - 1));
11276 return V == (V & ((1LL << 12) - 1));
11277 case MVT::f32:
11278 case MVT::f64:
11279 // Same as ARM mode. FIXME: NEON?
11280 if (!Subtarget->hasVFP2())
11281 return false;
11282 if ((V & 3) != 0)
11283 return false;
11284 V >>= 2;
11285 return V == (V & ((1LL << 8) - 1));
11286 }
11287}
11288
Evan Cheng2150b922007-03-12 23:30:29 +000011289/// isLegalAddressImmediate - Return true if the integer value can be used
11290/// as the offset of the target addressing mode for load / store of the
11291/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011292static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011293 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000011294 if (V == 0)
11295 return true;
11296
Evan Chengce5dfb62009-03-09 19:15:00 +000011297 if (!VT.isSimple())
11298 return false;
11299
Evan Chengdc49a8d2009-08-14 20:09:37 +000011300 if (Subtarget->isThumb1Only())
11301 return isLegalT1AddressImmediate(V, VT);
11302 else if (Subtarget->isThumb2())
11303 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000011304
Evan Chengdc49a8d2009-08-14 20:09:37 +000011305 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000011306 if (V < 0)
11307 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000011308 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000011309 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011310 case MVT::i1:
11311 case MVT::i8:
11312 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000011313 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011314 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011315 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000011316 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011317 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011318 case MVT::f32:
11319 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011320 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000011321 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000011322 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000011323 return false;
11324 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011325 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000011326 }
Evan Cheng10043e22007-01-19 07:51:42 +000011327}
11328
Evan Chengdc49a8d2009-08-14 20:09:37 +000011329bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
11330 EVT VT) const {
11331 int Scale = AM.Scale;
11332 if (Scale < 0)
11333 return false;
11334
11335 switch (VT.getSimpleVT().SimpleTy) {
11336 default: return false;
11337 case MVT::i1:
11338 case MVT::i8:
11339 case MVT::i16:
11340 case MVT::i32:
11341 if (Scale == 1)
11342 return true;
11343 // r + r << imm
11344 Scale = Scale & ~1;
11345 return Scale == 2 || Scale == 4 || Scale == 8;
11346 case MVT::i64:
11347 // r + r
11348 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11349 return true;
11350 return false;
11351 case MVT::isVoid:
11352 // Note, we allow "void" uses (basically, uses that aren't loads or
11353 // stores), because arm allows folding a scale into many arithmetic
11354 // operations. This should be made more precise and revisited later.
11355
11356 // Allow r << imm, but the imm has to be a multiple of two.
11357 if (Scale & 1) return false;
11358 return isPowerOf2_32(Scale);
11359 }
11360}
11361
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011362/// isLegalAddressingMode - Return true if the addressing mode represented
11363/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011364bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11365 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011366 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011367 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011368 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011369 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011370
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011371 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011372 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011373 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011374
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011375 switch (AM.Scale) {
11376 case 0: // no scale reg, must be "r+i" or "r", or "i".
11377 break;
11378 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011379 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011380 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000011381 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011382 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011383 // ARM doesn't support any R+R*scale+imm addr modes.
11384 if (AM.BaseOffs)
11385 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011386
Bob Wilson866c1742009-04-08 17:55:28 +000011387 if (!VT.isSimple())
11388 return false;
11389
Evan Chengdc49a8d2009-08-14 20:09:37 +000011390 if (Subtarget->isThumb2())
11391 return isLegalT2ScaledAddressingMode(AM, VT);
11392
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011393 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011394 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011395 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011396 case MVT::i1:
11397 case MVT::i8:
11398 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011399 if (Scale < 0) Scale = -Scale;
11400 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011401 return true;
11402 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011403 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011404 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011405 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011406 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011407 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011408 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011409 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011410
Owen Anderson9f944592009-08-11 20:47:22 +000011411 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011412 // Note, we allow "void" uses (basically, uses that aren't loads or
11413 // stores), because arm allows folding a scale into many arithmetic
11414 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011415
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011416 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011417 if (Scale & 1) return false;
11418 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011419 }
Evan Cheng2150b922007-03-12 23:30:29 +000011420 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011421 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011422}
11423
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011424/// isLegalICmpImmediate - Return true if the specified immediate is legal
11425/// icmp immediate, that is the target has icmp instructions which can compare
11426/// a register against the immediate without having to materialize the
11427/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011428bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011429 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011430 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011431 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011432 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011433 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011434 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011435 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011436}
11437
Andrew Tricka22cdb72012-07-18 18:34:27 +000011438/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11439/// *or sub* immediate, that is the target has add or sub instructions which can
11440/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011441/// immediate into a register.
11442bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011443 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011444 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011445 if (!Subtarget->isThumb())
11446 return ARM_AM::getSOImmVal(AbsImm) != -1;
11447 if (Subtarget->isThumb2())
11448 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11449 // Thumb1 only has 8-bit unsigned immediate.
11450 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011451}
11452
Owen Anderson53aa7a92009-08-10 22:56:29 +000011453static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011454 bool isSEXTLoad, SDValue &Base,
11455 SDValue &Offset, bool &isInc,
11456 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011457 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11458 return false;
11459
Owen Anderson9f944592009-08-11 20:47:22 +000011460 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011461 // AddressingMode 3
11462 Base = Ptr->getOperand(0);
11463 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011464 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011465 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011466 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011467 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011468 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011469 return true;
11470 }
11471 }
11472 isInc = (Ptr->getOpcode() == ISD::ADD);
11473 Offset = Ptr->getOperand(1);
11474 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011475 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011476 // AddressingMode 2
11477 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011478 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011479 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011480 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011481 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011482 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011483 Base = Ptr->getOperand(0);
11484 return true;
11485 }
11486 }
11487
11488 if (Ptr->getOpcode() == ISD::ADD) {
11489 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011490 ARM_AM::ShiftOpc ShOpcVal=
11491 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011492 if (ShOpcVal != ARM_AM::no_shift) {
11493 Base = Ptr->getOperand(1);
11494 Offset = Ptr->getOperand(0);
11495 } else {
11496 Base = Ptr->getOperand(0);
11497 Offset = Ptr->getOperand(1);
11498 }
11499 return true;
11500 }
11501
11502 isInc = (Ptr->getOpcode() == ISD::ADD);
11503 Base = Ptr->getOperand(0);
11504 Offset = Ptr->getOperand(1);
11505 return true;
11506 }
11507
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011508 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011509 return false;
11510}
11511
Owen Anderson53aa7a92009-08-10 22:56:29 +000011512static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011513 bool isSEXTLoad, SDValue &Base,
11514 SDValue &Offset, bool &isInc,
11515 SelectionDAG &DAG) {
11516 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11517 return false;
11518
11519 Base = Ptr->getOperand(0);
11520 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11521 int RHSC = (int)RHS->getZExtValue();
11522 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11523 assert(Ptr->getOpcode() == ISD::ADD);
11524 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011525 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011526 return true;
11527 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11528 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011529 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011530 return true;
11531 }
11532 }
11533
11534 return false;
11535}
11536
Evan Cheng10043e22007-01-19 07:51:42 +000011537/// getPreIndexedAddressParts - returns true by value, base pointer and
11538/// offset pointer and addressing mode by reference if the node's address
11539/// can be legally represented as pre-indexed load / store address.
11540bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011541ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11542 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011543 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011544 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011545 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011546 return false;
11547
Owen Anderson53aa7a92009-08-10 22:56:29 +000011548 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011549 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011550 bool isSEXTLoad = false;
11551 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11552 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011553 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011554 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11555 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11556 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011557 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011558 } else
11559 return false;
11560
11561 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011562 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011563 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011564 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11565 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011566 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011567 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011568 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011569 if (!isLegal)
11570 return false;
11571
11572 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11573 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011574}
11575
11576/// getPostIndexedAddressParts - returns true by value, base pointer and
11577/// offset pointer and addressing mode by reference if this node can be
11578/// combined with a load / store to form a post-indexed load / store.
11579bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011580 SDValue &Base,
11581 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011582 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011583 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011584 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011585 SDValue Ptr;
James Molloyb3326df2016-07-15 08:03:56 +000011586 bool isSEXTLoad = false, isNonExt;
Evan Cheng10043e22007-01-19 07:51:42 +000011587 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011588 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011589 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011590 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
James Molloyb3326df2016-07-15 08:03:56 +000011591 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD;
Evan Cheng10043e22007-01-19 07:51:42 +000011592 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011593 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011594 Ptr = ST->getBasePtr();
James Molloyb3326df2016-07-15 08:03:56 +000011595 isNonExt = !ST->isTruncatingStore();
Evan Cheng10043e22007-01-19 07:51:42 +000011596 } else
11597 return false;
11598
James Molloyb3326df2016-07-15 08:03:56 +000011599 if (Subtarget->isThumb1Only()) {
11600 // Thumb-1 can do a limited post-inc load or store as an updating LDM. It
11601 // must be non-extending/truncating, i32, with an offset of 4.
11602 assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!");
11603 if (Op->getOpcode() != ISD::ADD || !isNonExt)
11604 return false;
11605 auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1));
11606 if (!RHS || RHS->getZExtValue() != 4)
11607 return false;
11608
11609 Offset = Op->getOperand(1);
11610 Base = Op->getOperand(0);
11611 AM = ISD::POST_INC;
11612 return true;
11613 }
11614
Evan Cheng10043e22007-01-19 07:51:42 +000011615 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011616 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011617 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011618 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011619 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011620 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011621 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11622 isInc, DAG);
11623 if (!isLegal)
11624 return false;
11625
Evan Chengf19384d2010-05-18 21:31:17 +000011626 if (Ptr != Base) {
11627 // Swap base ptr and offset to catch more post-index load / store when
11628 // it's legal. In Thumb2 mode, offset must be an immediate.
11629 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11630 !Subtarget->isThumb2())
11631 std::swap(Base, Offset);
11632
11633 // Post-indexed load / store update the base pointer.
11634 if (Ptr != Base)
11635 return false;
11636 }
11637
Evan Cheng84c6cda2009-07-02 07:28:31 +000011638 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11639 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011640}
11641
Jay Foada0653a32014-05-14 21:14:37 +000011642void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11643 APInt &KnownZero,
11644 APInt &KnownOne,
11645 const SelectionDAG &DAG,
11646 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011647 unsigned BitWidth = KnownOne.getBitWidth();
11648 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011649 switch (Op.getOpcode()) {
11650 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011651 case ARMISD::ADDC:
11652 case ARMISD::ADDE:
11653 case ARMISD::SUBC:
11654 case ARMISD::SUBE:
11655 // These nodes' second result is a boolean
11656 if (Op.getResNo() == 0)
11657 break;
11658 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11659 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011660 case ARMISD::CMOV: {
11661 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011662 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011663 if (KnownZero == 0 && KnownOne == 0) return;
11664
Dan Gohmanf990faf2008-02-13 00:35:47 +000011665 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011666 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011667 KnownZero &= KnownZeroRHS;
11668 KnownOne &= KnownOneRHS;
11669 return;
11670 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011671 case ISD::INTRINSIC_W_CHAIN: {
11672 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11673 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11674 switch (IntID) {
11675 default: return;
11676 case Intrinsic::arm_ldaex:
11677 case Intrinsic::arm_ldrex: {
11678 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11679 unsigned MemBits = VT.getScalarType().getSizeInBits();
11680 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11681 return;
11682 }
11683 }
11684 }
Evan Cheng10043e22007-01-19 07:51:42 +000011685 }
11686}
11687
11688//===----------------------------------------------------------------------===//
11689// ARM Inline Assembly Support
11690//===----------------------------------------------------------------------===//
11691
Evan Cheng078b0b02011-01-08 01:24:27 +000011692bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11693 // Looking for "rev" which is V6+.
11694 if (!Subtarget->hasV6Ops())
11695 return false;
11696
11697 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11698 std::string AsmStr = IA->getAsmString();
11699 SmallVector<StringRef, 4> AsmPieces;
11700 SplitString(AsmStr, AsmPieces, ";\n");
11701
11702 switch (AsmPieces.size()) {
11703 default: return false;
11704 case 1:
11705 AsmStr = AsmPieces[0];
11706 AsmPieces.clear();
11707 SplitString(AsmStr, AsmPieces, " \t,");
11708
11709 // rev $0, $1
11710 if (AsmPieces.size() == 3 &&
11711 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11712 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011713 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011714 if (Ty && Ty->getBitWidth() == 32)
11715 return IntrinsicLowering::LowerToByteSwap(CI);
11716 }
11717 break;
11718 }
11719
11720 return false;
11721}
11722
Silviu Baranga82d04262016-04-25 14:29:18 +000011723const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const {
11724 // At this point, we have to lower this constraint to something else, so we
11725 // lower it to an "r" or "w". However, by doing this we will force the result
11726 // to be in register, while the X constraint is much more permissive.
11727 //
11728 // Although we are correct (we are free to emit anything, without
11729 // constraints), we might break use cases that would expect us to be more
11730 // efficient and emit something else.
11731 if (!Subtarget->hasVFP2())
11732 return "r";
11733 if (ConstraintVT.isFloatingPoint())
11734 return "w";
11735 if (ConstraintVT.isVector() && Subtarget->hasNEON() &&
11736 (ConstraintVT.getSizeInBits() == 64 ||
11737 ConstraintVT.getSizeInBits() == 128))
11738 return "w";
11739
11740 return "r";
11741}
11742
Evan Cheng10043e22007-01-19 07:51:42 +000011743/// getConstraintType - Given a constraint letter, return the type of
11744/// constraint it is for this target.
11745ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011746ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011747 if (Constraint.size() == 1) {
11748 switch (Constraint[0]) {
11749 default: break;
11750 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011751 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011752 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011753 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011754 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011755 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011756 // An address with a single base register. Due to the way we
11757 // currently handle addresses it is the same as an 'r' memory constraint.
11758 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011759 }
Eric Christophere256cd02011-06-21 22:10:57 +000011760 } else if (Constraint.size() == 2) {
11761 switch (Constraint[0]) {
11762 default: break;
11763 // All 'U+' constraints are addresses.
11764 case 'U': return C_Memory;
11765 }
Evan Cheng10043e22007-01-19 07:51:42 +000011766 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011767 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011768}
11769
John Thompsone8360b72010-10-29 17:29:13 +000011770/// Examine constraint type and operand type and determine a weight value.
11771/// This object must already have been set up with the operand type
11772/// and the current alternative constraint selected.
11773TargetLowering::ConstraintWeight
11774ARMTargetLowering::getSingleConstraintMatchWeight(
11775 AsmOperandInfo &info, const char *constraint) const {
11776 ConstraintWeight weight = CW_Invalid;
11777 Value *CallOperandVal = info.CallOperandVal;
11778 // If we don't have a value, we can't do a match,
11779 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011780 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011781 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011782 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011783 // Look at the constraint type.
11784 switch (*constraint) {
11785 default:
11786 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11787 break;
11788 case 'l':
11789 if (type->isIntegerTy()) {
11790 if (Subtarget->isThumb())
11791 weight = CW_SpecificReg;
11792 else
11793 weight = CW_Register;
11794 }
11795 break;
11796 case 'w':
11797 if (type->isFloatingPointTy())
11798 weight = CW_Register;
11799 break;
11800 }
11801 return weight;
11802}
11803
Eric Christophercf2007c2011-06-30 23:50:52 +000011804typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011805RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11806 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011807 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011808 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011809 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011810 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011811 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011812 return RCPair(0U, &ARM::tGPRRegClass);
11813 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011814 case 'h': // High regs or no regs.
11815 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011816 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011817 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011818 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011819 if (Subtarget->isThumb1Only())
11820 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011821 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011822 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011823 if (VT == MVT::Other)
11824 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011825 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011826 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011827 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011828 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011829 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011830 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011831 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011832 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011833 if (VT == MVT::Other)
11834 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011835 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011836 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011837 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011838 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011839 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011840 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011841 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011842 case 't':
11843 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011844 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011845 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011846 }
11847 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011848 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011849 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011850
Eric Christopher11e4df72015-02-26 22:38:43 +000011851 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011852}
11853
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011854/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11855/// vector. If it is invalid, don't add anything to Ops.
11856void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011857 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011858 std::vector<SDValue>&Ops,
11859 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011860 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011861
Eric Christopherde9399b2011-06-02 23:16:42 +000011862 // Currently only support length 1 constraints.
11863 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011864
Eric Christopherde9399b2011-06-02 23:16:42 +000011865 char ConstraintLetter = Constraint[0];
11866 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011867 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011868 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011869 case 'I': case 'J': case 'K': case 'L':
11870 case 'M': case 'N': case 'O':
11871 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11872 if (!C)
11873 return;
11874
11875 int64_t CVal64 = C->getSExtValue();
11876 int CVal = (int) CVal64;
11877 // None of these constraints allow values larger than 32 bits. Check
11878 // that the value fits in an int.
11879 if (CVal != CVal64)
11880 return;
11881
Eric Christopherde9399b2011-06-02 23:16:42 +000011882 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011883 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011884 // Constant suitable for movw, must be between 0 and
11885 // 65535.
11886 if (Subtarget->hasV6T2Ops())
11887 if (CVal >= 0 && CVal <= 65535)
11888 break;
11889 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011890 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011891 if (Subtarget->isThumb1Only()) {
11892 // This must be a constant between 0 and 255, for ADD
11893 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011894 if (CVal >= 0 && CVal <= 255)
11895 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011896 } else if (Subtarget->isThumb2()) {
11897 // A constant that can be used as an immediate value in a
11898 // data-processing instruction.
11899 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11900 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011901 } else {
11902 // A constant that can be used as an immediate value in a
11903 // data-processing instruction.
11904 if (ARM_AM::getSOImmVal(CVal) != -1)
11905 break;
11906 }
11907 return;
11908
11909 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000011910 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011911 // This must be a constant between -255 and -1, for negated ADD
11912 // immediates. This can be used in GCC with an "n" modifier that
11913 // prints the negated value, for use with SUB instructions. It is
11914 // not useful otherwise but is implemented for compatibility.
11915 if (CVal >= -255 && CVal <= -1)
11916 break;
11917 } else {
11918 // This must be a constant between -4095 and 4095. It is not clear
11919 // what this constraint is intended for. Implemented for
11920 // compatibility with GCC.
11921 if (CVal >= -4095 && CVal <= 4095)
11922 break;
11923 }
11924 return;
11925
11926 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011927 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011928 // A 32-bit value where only one byte has a nonzero value. Exclude
11929 // zero to match GCC. This constraint is used by GCC internally for
11930 // constants that can be loaded with a move/shift combination.
11931 // It is not useful otherwise but is implemented for compatibility.
11932 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11933 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011934 } else if (Subtarget->isThumb2()) {
11935 // A constant whose bitwise inverse can be used as an immediate
11936 // value in a data-processing instruction. This can be used in GCC
11937 // with a "B" modifier that prints the inverted value, for use with
11938 // BIC and MVN instructions. It is not useful otherwise but is
11939 // implemented for compatibility.
11940 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11941 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011942 } else {
11943 // A constant whose bitwise inverse can be used as an immediate
11944 // value in a data-processing instruction. This can be used in GCC
11945 // with a "B" modifier that prints the inverted value, for use with
11946 // BIC and MVN instructions. It is not useful otherwise but is
11947 // implemented for compatibility.
11948 if (ARM_AM::getSOImmVal(~CVal) != -1)
11949 break;
11950 }
11951 return;
11952
11953 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011954 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011955 // This must be a constant between -7 and 7,
11956 // for 3-operand ADD/SUB immediate instructions.
11957 if (CVal >= -7 && CVal < 7)
11958 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011959 } else if (Subtarget->isThumb2()) {
11960 // A constant whose negation can be used as an immediate value in a
11961 // data-processing instruction. This can be used in GCC with an "n"
11962 // modifier that prints the negated value, for use with SUB
11963 // instructions. It is not useful otherwise but is implemented for
11964 // compatibility.
11965 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11966 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011967 } else {
11968 // A constant whose negation can be used as an immediate value in a
11969 // data-processing instruction. This can be used in GCC with an "n"
11970 // modifier that prints the negated value, for use with SUB
11971 // instructions. It is not useful otherwise but is implemented for
11972 // compatibility.
11973 if (ARM_AM::getSOImmVal(-CVal) != -1)
11974 break;
11975 }
11976 return;
11977
11978 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000011979 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011980 // This must be a multiple of 4 between 0 and 1020, for
11981 // ADD sp + immediate.
11982 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11983 break;
11984 } else {
11985 // A power of two or a constant between 0 and 32. This is used in
11986 // GCC for the shift amount on shifted register operands, but it is
11987 // useful in general for any shift amounts.
11988 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11989 break;
11990 }
11991 return;
11992
11993 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011994 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011995 // This must be a constant between 0 and 31, for shift amounts.
11996 if (CVal >= 0 && CVal <= 31)
11997 break;
11998 }
11999 return;
12000
12001 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000012002 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012003 // This must be a multiple of 4 between -508 and 508, for
12004 // ADD/SUB sp = sp + immediate.
12005 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
12006 break;
12007 }
12008 return;
12009 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012010 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012011 break;
12012 }
12013
12014 if (Result.getNode()) {
12015 Ops.push_back(Result);
12016 return;
12017 }
Dale Johannesence97d552010-06-25 21:55:36 +000012018 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012019}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000012020
Scott Douglassd2974a62015-08-24 09:17:11 +000012021static RTLIB::Libcall getDivRemLibcall(
12022 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000012023 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
12024 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000012025 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000012026 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
12027 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000012028 RTLIB::Libcall LC;
12029 switch (SVT) {
12030 default: llvm_unreachable("Unexpected request for libcall!");
12031 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
12032 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
12033 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
12034 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
12035 }
12036 return LC;
12037}
12038
12039static TargetLowering::ArgListTy getDivRemArgList(
12040 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000012041 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
12042 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000012043 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000012044 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
12045 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000012046 TargetLowering::ArgListTy Args;
12047 TargetLowering::ArgListEntry Entry;
12048 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
12049 EVT ArgVT = N->getOperand(i).getValueType();
12050 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
12051 Entry.Node = N->getOperand(i);
12052 Entry.Ty = ArgTy;
12053 Entry.isSExt = isSigned;
12054 Entry.isZExt = !isSigned;
12055 Args.push_back(Entry);
12056 }
12057 return Args;
12058}
12059
Renato Golin87610692013-07-16 09:32:17 +000012060SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000012061 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
Rafael Espindolaa895a0c2016-06-24 21:14:33 +000012062 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000012063 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000012064 unsigned Opcode = Op->getOpcode();
12065 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000012066 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000012067 bool isSigned = (Opcode == ISD::SDIVREM);
12068 EVT VT = Op->getValueType(0);
12069 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
12070
Scott Douglassd2974a62015-08-24 09:17:11 +000012071 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
12072 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000012073 SDValue InChain = DAG.getEntryNode();
12074
Scott Douglassd2974a62015-08-24 09:17:11 +000012075 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
12076 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000012077
12078 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000012079 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000012080
Reid Kleckner343c3952014-11-20 23:51:47 +000012081 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000012082
12083 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012084 TargetLowering::CallLoweringInfo CLI(DAG);
12085 CLI.setDebugLoc(dl).setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +000012086 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012087 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000012088
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012089 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000012090 return CallInfo.first;
12091}
12092
Scott Douglassbdef6042015-08-24 09:17:18 +000012093// Lowers REM using divmod helpers
12094// see RTABI section 4.2/4.3
12095SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
12096 // Build return types (div and rem)
12097 std::vector<Type*> RetTyParams;
12098 Type *RetTyElement;
12099
12100 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
12101 default: llvm_unreachable("Unexpected request for libcall!");
12102 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
12103 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
12104 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
12105 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
12106 }
12107
12108 RetTyParams.push_back(RetTyElement);
12109 RetTyParams.push_back(RetTyElement);
12110 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
12111 Type *RetTy = StructType::get(*DAG.getContext(), ret);
12112
12113 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
12114 SimpleTy);
12115 SDValue InChain = DAG.getEntryNode();
12116 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
12117 bool isSigned = N->getOpcode() == ISD::SREM;
12118 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
12119 getPointerTy(DAG.getDataLayout()));
12120
12121 // Lower call
12122 CallLoweringInfo CLI(DAG);
12123 CLI.setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +000012124 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args))
Scott Douglassbdef6042015-08-24 09:17:18 +000012125 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
12126 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
12127
12128 // Return second (rem) result operand (first contains div)
12129 SDNode *ResNode = CallResult.first.getNode();
12130 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
12131 return ResNode->getOperand(1);
12132}
12133
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012134SDValue
12135ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
12136 assert(Subtarget->isTargetWindows() && "unsupported target platform");
12137 SDLoc DL(Op);
12138
12139 // Get the inputs.
12140 SDValue Chain = Op.getOperand(0);
12141 SDValue Size = Op.getOperand(1);
12142
12143 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012144 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012145
12146 SDValue Flag;
12147 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
12148 Flag = Chain.getValue(1);
12149
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000012150 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012151 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
12152
12153 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
12154 Chain = NewSP.getValue(1);
12155
12156 SDValue Ops[2] = { NewSP, Chain };
12157 return DAG.getMergeValues(Ops, DL);
12158}
12159
Oliver Stannard51b1d462014-08-21 12:50:31 +000012160SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
12161 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
12162 "Unexpected type for custom-lowering FP_EXTEND");
12163
12164 RTLIB::Libcall LC;
12165 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
12166
12167 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000012168 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
12169 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012170}
12171
12172SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
12173 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
12174 Subtarget->isFPOnlySP() &&
12175 "Unexpected type for custom-lowering FP_ROUND");
12176
12177 RTLIB::Libcall LC;
12178 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
12179
12180 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000012181 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
12182 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012183}
12184
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000012185bool
12186ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
12187 // The ARM target isn't yet aware of offsets.
12188 return false;
12189}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012190
Jim Grosbach11013ed2010-07-16 23:05:05 +000012191bool ARM::isBitFieldInvertedMask(unsigned v) {
12192 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000012193 return false;
12194
Jim Grosbach11013ed2010-07-16 23:05:05 +000012195 // there can be 1's on either or both "outsides", all the "inside"
12196 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000012197 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000012198}
12199
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012200/// isFPImmLegal - Returns true if the target can instruction select the
12201/// specified FP immediate natively. If false, the legalizer will
12202/// materialize the FP immediate as a load from a constant pool.
12203bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
12204 if (!Subtarget->hasVFP3())
12205 return false;
12206 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000012207 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012208 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000012209 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012210 return false;
12211}
Bob Wilson5549d492010-09-21 17:56:22 +000012212
Wesley Peck527da1b2010-11-23 03:31:01 +000012213/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000012214/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
12215/// specified in the intrinsic calls.
12216bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
12217 const CallInst &I,
12218 unsigned Intrinsic) const {
12219 switch (Intrinsic) {
12220 case Intrinsic::arm_neon_vld1:
12221 case Intrinsic::arm_neon_vld2:
12222 case Intrinsic::arm_neon_vld3:
12223 case Intrinsic::arm_neon_vld4:
12224 case Intrinsic::arm_neon_vld2lane:
12225 case Intrinsic::arm_neon_vld3lane:
12226 case Intrinsic::arm_neon_vld4lane: {
12227 Info.opc = ISD::INTRINSIC_W_CHAIN;
12228 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012229 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012230 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012231 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12232 Info.ptrVal = I.getArgOperand(0);
12233 Info.offset = 0;
12234 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12235 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12236 Info.vol = false; // volatile loads with NEON intrinsics not supported
12237 Info.readMem = true;
12238 Info.writeMem = false;
12239 return true;
12240 }
12241 case Intrinsic::arm_neon_vst1:
12242 case Intrinsic::arm_neon_vst2:
12243 case Intrinsic::arm_neon_vst3:
12244 case Intrinsic::arm_neon_vst4:
12245 case Intrinsic::arm_neon_vst2lane:
12246 case Intrinsic::arm_neon_vst3lane:
12247 case Intrinsic::arm_neon_vst4lane: {
12248 Info.opc = ISD::INTRINSIC_VOID;
12249 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012250 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000012251 unsigned NumElts = 0;
12252 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000012253 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000012254 if (!ArgTy->isVectorTy())
12255 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012256 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012257 }
12258 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12259 Info.ptrVal = I.getArgOperand(0);
12260 Info.offset = 0;
12261 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12262 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12263 Info.vol = false; // volatile stores with NEON intrinsics not supported
12264 Info.readMem = false;
12265 Info.writeMem = true;
12266 return true;
12267 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012268 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012269 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012270 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012271 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
12272 Info.opc = ISD::INTRINSIC_W_CHAIN;
12273 Info.memVT = MVT::getVT(PtrTy->getElementType());
12274 Info.ptrVal = I.getArgOperand(0);
12275 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012276 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012277 Info.vol = true;
12278 Info.readMem = true;
12279 Info.writeMem = false;
12280 return true;
12281 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012282 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012283 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012284 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012285 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
12286 Info.opc = ISD::INTRINSIC_W_CHAIN;
12287 Info.memVT = MVT::getVT(PtrTy->getElementType());
12288 Info.ptrVal = I.getArgOperand(1);
12289 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012290 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012291 Info.vol = true;
12292 Info.readMem = false;
12293 Info.writeMem = true;
12294 return true;
12295 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012296 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012297 case Intrinsic::arm_strexd: {
12298 Info.opc = ISD::INTRINSIC_W_CHAIN;
12299 Info.memVT = MVT::i64;
12300 Info.ptrVal = I.getArgOperand(2);
12301 Info.offset = 0;
12302 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012303 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012304 Info.readMem = false;
12305 Info.writeMem = true;
12306 return true;
12307 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012308 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012309 case Intrinsic::arm_ldrexd: {
12310 Info.opc = ISD::INTRINSIC_W_CHAIN;
12311 Info.memVT = MVT::i64;
12312 Info.ptrVal = I.getArgOperand(0);
12313 Info.offset = 0;
12314 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012315 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012316 Info.readMem = true;
12317 Info.writeMem = false;
12318 return true;
12319 }
Bob Wilson5549d492010-09-21 17:56:22 +000012320 default:
12321 break;
12322 }
12323
12324 return false;
12325}
Juergen Ributzka659ce002014-01-28 01:20:14 +000012326
12327/// \brief Returns true if it is beneficial to convert a load of a constant
12328/// to just the constant itself.
12329bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
12330 Type *Ty) const {
12331 assert(Ty->isIntegerTy());
12332
12333 unsigned Bits = Ty->getPrimitiveSizeInBits();
12334 if (Bits == 0 || Bits > 32)
12335 return false;
12336 return true;
12337}
Tim Northover037f26f22014-04-17 18:22:47 +000012338
Robin Morisset5349e8e2014-09-18 18:56:04 +000012339Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
12340 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012341 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000012342
12343 // First, if the target has no DMB, see what fallback we can use.
12344 if (!Subtarget->hasDataBarrier()) {
12345 // Some ARMv6 cpus can support data barriers with an mcr instruction.
12346 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
12347 // here.
12348 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
12349 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
12350 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
12351 Builder.getInt32(0), Builder.getInt32(7),
12352 Builder.getInt32(10), Builder.getInt32(5)};
12353 return Builder.CreateCall(MCR, args);
12354 } else {
12355 // Instead of using barriers, atomic accesses on these subtargets use
12356 // libcalls.
12357 llvm_unreachable("makeDMB on a target so old that it has no barriers");
12358 }
12359 } else {
12360 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
12361 // Only a full system barrier exists in the M-class architectures.
12362 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
12363 Constant *CDomain = Builder.getInt32(Domain);
12364 return Builder.CreateCall(DMB, CDomain);
12365 }
Robin Morisseta47cb412014-09-03 21:01:03 +000012366}
12367
12368// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000012369Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012370 AtomicOrdering Ord, bool IsStore,
12371 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012372 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012373 case AtomicOrdering::NotAtomic:
12374 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012375 llvm_unreachable("Invalid fence: unordered/non-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012376 case AtomicOrdering::Monotonic:
12377 case AtomicOrdering::Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012378 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012379 case AtomicOrdering::SequentiallyConsistent:
Robin Morisseta47cb412014-09-03 21:01:03 +000012380 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012381 return nullptr; // Nothing to do
12382 /*FALLTHROUGH*/
JF Bastien800f87a2016-04-06 21:19:33 +000012383 case AtomicOrdering::Release:
12384 case AtomicOrdering::AcquireRelease:
Diana Picusc5baa432016-06-23 07:47:35 +000012385 if (Subtarget->preferISHSTBarriers())
Robin Morissetdedef332014-09-23 20:31:14 +000012386 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012387 // FIXME: add a comment with a link to documentation justifying this.
12388 else
Robin Morissetdedef332014-09-23 20:31:14 +000012389 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012390 }
Robin Morissetdedef332014-09-23 20:31:14 +000012391 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012392}
12393
Robin Morissetdedef332014-09-23 20:31:14 +000012394Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012395 AtomicOrdering Ord, bool IsStore,
12396 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012397 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012398 case AtomicOrdering::NotAtomic:
12399 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012400 llvm_unreachable("Invalid fence: unordered/not-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012401 case AtomicOrdering::Monotonic:
12402 case AtomicOrdering::Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012403 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012404 case AtomicOrdering::Acquire:
12405 case AtomicOrdering::AcquireRelease:
12406 case AtomicOrdering::SequentiallyConsistent:
Robin Morissetdedef332014-09-23 20:31:14 +000012407 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012408 }
Robin Morissetdedef332014-09-23 20:31:14 +000012409 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012410}
12411
Robin Morisseted3d48f2014-09-03 21:29:59 +000012412// Loads and stores less than 64-bits are already atomic; ones above that
12413// are doomed anyway, so defer to the default libcall and blame the OS when
12414// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12415// anything for those.
12416bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12417 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12418 return (Size == 64) && !Subtarget->isMClass();
12419}
Tim Northover037f26f22014-04-17 18:22:47 +000012420
Robin Morisseted3d48f2014-09-03 21:29:59 +000012421// Loads and stores less than 64-bits are already atomic; ones above that
12422// are doomed anyway, so defer to the default libcall and blame the OS when
12423// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12424// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012425// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12426// guarantee, see DDI0406C ARM architecture reference manual,
12427// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012428TargetLowering::AtomicExpansionKind
12429ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012430 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012431 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012432 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012433}
12434
12435// For the real atomic operations, we have ldrex/strex up to 32 bits,
12436// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012437TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012438ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012439 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012440 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012441 ? AtomicExpansionKind::LLSC
12442 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012443}
12444
Ahmed Bougacha52468672015-09-11 17:08:28 +000012445bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12446 AtomicCmpXchgInst *AI) const {
Tim Northoverb629c772016-04-18 21:48:55 +000012447 // At -O0, fast-regalloc cannot cope with the live vregs necessary to
12448 // implement cmpxchg without spilling. If the address being exchanged is also
12449 // on the stack and close enough to the spill slot, this can lead to a
12450 // situation where the monitor always gets cleared and the atomic operation
12451 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
12452 return getTargetMachine().getOptLevel() != 0;
Ahmed Bougacha52468672015-09-11 17:08:28 +000012453}
12454
James Y Knightf44fc522016-03-16 22:12:04 +000012455bool ARMTargetLowering::shouldInsertFencesForAtomic(
12456 const Instruction *I) const {
12457 return InsertFencesForAtomic;
12458}
12459
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012460// This has so far only been implemented for MachO.
12461bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012462 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012463}
12464
Quentin Colombetc32615d2014-10-31 17:52:53 +000012465bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12466 unsigned &Cost) const {
12467 // If we do not have NEON, vector types are not natively supported.
12468 if (!Subtarget->hasNEON())
12469 return false;
12470
12471 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012472 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012473 // better to leave at float as we have more freedom in the addressing mode for
12474 // those.
12475 if (VectorTy->isFPOrFPVectorTy())
12476 return false;
12477
12478 // If the index is unknown at compile time, this is very expensive to lower
12479 // and it is not possible to combine the store with the extract.
12480 if (!isa<ConstantInt>(Idx))
12481 return false;
12482
12483 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12484 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12485 // We can do a store + vector extract on any vector that fits perfectly in a D
12486 // or Q register.
12487 if (BitWidth == 64 || BitWidth == 128) {
12488 Cost = 0;
12489 return true;
12490 }
12491 return false;
12492}
12493
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012494bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12495 return Subtarget->hasV6T2Ops();
12496}
12497
12498bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12499 return Subtarget->hasV6T2Ops();
12500}
12501
Tim Northover037f26f22014-04-17 18:22:47 +000012502Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12503 AtomicOrdering Ord) const {
12504 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12505 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
JF Bastien800f87a2016-04-06 21:19:33 +000012506 bool IsAcquire = isAcquireOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012507
12508 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12509 // intrinsic must return {i32, i32} and we have to recombine them into a
12510 // single i64 here.
12511 if (ValTy->getPrimitiveSizeInBits() == 64) {
12512 Intrinsic::ID Int =
12513 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12514 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12515
12516 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12517 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12518
12519 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12520 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012521 if (!Subtarget->isLittle())
12522 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012523 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12524 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12525 return Builder.CreateOr(
12526 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12527 }
12528
12529 Type *Tys[] = { Addr->getType() };
12530 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12531 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12532
12533 return Builder.CreateTruncOrBitCast(
12534 Builder.CreateCall(Ldrex, Addr),
12535 cast<PointerType>(Addr->getType())->getElementType());
12536}
12537
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012538void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12539 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012540 if (!Subtarget->hasV7Ops())
12541 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012542 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12543 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12544}
12545
Tim Northover037f26f22014-04-17 18:22:47 +000012546Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12547 Value *Addr,
12548 AtomicOrdering Ord) const {
12549 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
JF Bastien800f87a2016-04-06 21:19:33 +000012550 bool IsRelease = isReleaseOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012551
12552 // Since the intrinsics must have legal type, the i64 intrinsics take two
12553 // parameters: "i32, i32". We must marshal Val into the appropriate form
12554 // before the call.
12555 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12556 Intrinsic::ID Int =
12557 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12558 Function *Strex = Intrinsic::getDeclaration(M, Int);
12559 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12560
12561 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12562 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012563 if (!Subtarget->isLittle())
12564 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012565 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012566 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012567 }
12568
12569 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12570 Type *Tys[] = { Addr->getType() };
12571 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12572
David Blaikieff6409d2015-05-18 22:13:54 +000012573 return Builder.CreateCall(
12574 Strex, {Builder.CreateZExtOrBitCast(
12575 Val, Strex->getFunctionType()->getParamType(0)),
12576 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012577}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012578
Hao Liu2cd34bb2015-06-26 02:45:36 +000012579/// \brief Lower an interleaved load into a vldN intrinsic.
12580///
12581/// E.g. Lower an interleaved load (Factor = 2):
12582/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12583/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12584/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12585///
12586/// Into:
12587/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12588/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12589/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12590bool ARMTargetLowering::lowerInterleavedLoad(
12591 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12592 ArrayRef<unsigned> Indices, unsigned Factor) const {
12593 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12594 "Invalid interleave factor");
12595 assert(!Shuffles.empty() && "Empty shufflevector input");
12596 assert(Shuffles.size() == Indices.size() &&
12597 "Unmatched number of shufflevectors and indices");
12598
12599 VectorType *VecTy = Shuffles[0]->getType();
12600 Type *EltTy = VecTy->getVectorElementType();
12601
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012602 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012603 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
12604 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012605
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012606 // Skip if we do not have NEON and skip illegal vector types and vector types
12607 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12608 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012609 return false;
12610
12611 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12612 // load integer vectors first and then convert to pointer vectors.
12613 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012614 VecTy =
12615 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012616
12617 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12618 Intrinsic::arm_neon_vld3,
12619 Intrinsic::arm_neon_vld4};
12620
Hao Liu2cd34bb2015-06-26 02:45:36 +000012621 IRBuilder<> Builder(LI);
12622 SmallVector<Value *, 2> Ops;
12623
12624 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12625 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12626 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12627
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012628 Type *Tys[] = { VecTy, Int8Ptr };
12629 Function *VldnFunc =
12630 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012631 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12632
12633 // Replace uses of each shufflevector with the corresponding vector loaded
12634 // by ldN.
12635 for (unsigned i = 0; i < Shuffles.size(); i++) {
12636 ShuffleVectorInst *SV = Shuffles[i];
12637 unsigned Index = Indices[i];
12638
12639 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12640
12641 // Convert the integer vector to pointer vector if the element is pointer.
12642 if (EltTy->isPointerTy())
12643 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12644
12645 SV->replaceAllUsesWith(SubVec);
12646 }
12647
12648 return true;
12649}
12650
12651/// \brief Get a mask consisting of sequential integers starting from \p Start.
12652///
12653/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12654static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12655 unsigned NumElts) {
12656 SmallVector<Constant *, 16> Mask;
12657 for (unsigned i = 0; i < NumElts; i++)
12658 Mask.push_back(Builder.getInt32(Start + i));
12659
12660 return ConstantVector::get(Mask);
12661}
12662
12663/// \brief Lower an interleaved store into a vstN intrinsic.
12664///
12665/// E.g. Lower an interleaved store (Factor = 3):
12666/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12667/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12668/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12669///
12670/// Into:
12671/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12672/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12673/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12674/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12675///
12676/// Note that the new shufflevectors will be removed and we'll only generate one
12677/// vst3 instruction in CodeGen.
12678bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12679 ShuffleVectorInst *SVI,
12680 unsigned Factor) const {
12681 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12682 "Invalid interleave factor");
12683
12684 VectorType *VecTy = SVI->getType();
12685 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12686 "Invalid interleaved store");
12687
12688 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12689 Type *EltTy = VecTy->getVectorElementType();
12690 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12691
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012692 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012693 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
12694 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012695
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012696 // Skip if we do not have NEON and skip illegal vector types and vector types
12697 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12698 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12699 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012700 return false;
12701
12702 Value *Op0 = SVI->getOperand(0);
12703 Value *Op1 = SVI->getOperand(1);
12704 IRBuilder<> Builder(SI);
12705
12706 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12707 // vectors to integer vectors.
12708 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012709 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012710
12711 // Convert to the corresponding integer vector.
12712 Type *IntVecTy =
12713 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12714 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12715 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12716
12717 SubVecTy = VectorType::get(IntTy, NumSubElts);
12718 }
12719
Craig Topper26260942015-10-18 05:15:34 +000012720 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12721 Intrinsic::arm_neon_vst3,
12722 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012723 SmallVector<Value *, 6> Ops;
12724
12725 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12726 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12727
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012728 Type *Tys[] = { Int8Ptr, SubVecTy };
12729 Function *VstNFunc = Intrinsic::getDeclaration(
12730 SI->getModule(), StoreInts[Factor - 2], Tys);
12731
Hao Liu2cd34bb2015-06-26 02:45:36 +000012732 // Split the shufflevector operands into sub vectors for the new vstN call.
12733 for (unsigned i = 0; i < Factor; i++)
12734 Ops.push_back(Builder.CreateShuffleVector(
12735 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12736
12737 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12738 Builder.CreateCall(VstNFunc, Ops);
12739 return true;
12740}
12741
Oliver Stannardc24f2172014-05-09 14:01:47 +000012742enum HABaseType {
12743 HA_UNKNOWN = 0,
12744 HA_FLOAT,
12745 HA_DOUBLE,
12746 HA_VECT64,
12747 HA_VECT128
12748};
12749
12750static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12751 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012752 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012753 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12754 uint64_t SubMembers = 0;
12755 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12756 return false;
12757 Members += SubMembers;
12758 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012759 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012760 uint64_t SubMembers = 0;
12761 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12762 return false;
12763 Members += SubMembers * AT->getNumElements();
12764 } else if (Ty->isFloatTy()) {
12765 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12766 return false;
12767 Members = 1;
12768 Base = HA_FLOAT;
12769 } else if (Ty->isDoubleTy()) {
12770 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12771 return false;
12772 Members = 1;
12773 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012774 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012775 Members = 1;
12776 switch (Base) {
12777 case HA_FLOAT:
12778 case HA_DOUBLE:
12779 return false;
12780 case HA_VECT64:
12781 return VT->getBitWidth() == 64;
12782 case HA_VECT128:
12783 return VT->getBitWidth() == 128;
12784 case HA_UNKNOWN:
12785 switch (VT->getBitWidth()) {
12786 case 64:
12787 Base = HA_VECT64;
12788 return true;
12789 case 128:
12790 Base = HA_VECT128;
12791 return true;
12792 default:
12793 return false;
12794 }
12795 }
12796 }
12797
12798 return (Members > 0 && Members <= 4);
12799}
12800
Tim Northovere95c5b32015-02-24 17:22:34 +000012801/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12802/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12803/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012804bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12805 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012806 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12807 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012808 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012809
12810 HABaseType Base = HA_UNKNOWN;
12811 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012812 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12813 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12814
12815 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12816 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012817}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012818
12819unsigned ARMTargetLowering::getExceptionPointerRegister(
12820 const Constant *PersonalityFn) const {
12821 // Platforms which do not use SjLj EH may return values in these registers
12822 // via the personality function.
12823 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12824}
12825
12826unsigned ARMTargetLowering::getExceptionSelectorRegister(
12827 const Constant *PersonalityFn) const {
12828 // Platforms which do not use SjLj EH may return values in these registers
12829 // via the personality function.
12830 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12831}
Manman Ren5e9e65e2016-01-12 00:47:18 +000012832
12833void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
12834 // Update IsSplitCSR in ARMFunctionInfo.
12835 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
12836 AFI->setIsSplitCSR(true);
12837}
12838
12839void ARMTargetLowering::insertCopiesSplitCSR(
12840 MachineBasicBlock *Entry,
12841 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
12842 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
12843 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
12844 if (!IStart)
12845 return;
12846
12847 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
12848 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000012849 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000012850 for (const MCPhysReg *I = IStart; *I; ++I) {
12851 const TargetRegisterClass *RC = nullptr;
12852 if (ARM::GPRRegClass.contains(*I))
12853 RC = &ARM::GPRRegClass;
12854 else if (ARM::DPRRegClass.contains(*I))
12855 RC = &ARM::DPRRegClass;
12856 else
12857 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12858
12859 unsigned NewVR = MRI->createVirtualRegister(RC);
12860 // Create copy from CSR to a virtual register.
12861 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12862 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12863 // nounwind. If we want to generalize this later, we may need to emit
12864 // CFI pseudo-instructions.
12865 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12866 Attribute::NoUnwind) &&
12867 "Function should be nounwind in insertCopiesSplitCSR!");
12868 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000012869 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012870 .addReg(*I);
12871
Manman Rene5f807f2016-01-15 20:24:11 +000012872 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000012873 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000012874 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12875 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012876 .addReg(NewVR);
12877 }
12878}