blob: a61bd4022a9b504858a155fa2add25155891771f [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"
Rafael Espindola7ad97b22016-05-31 15:31:55 +000027#include "llvm/CodeGen/Analysis.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000028#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000029#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000037#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000038#include "llvm/IR/CallingConv.h"
39#include "llvm/IR/Constants.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000042#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Instructions.h"
John Brawn0dbcd652015-03-18 12:01:59 +000045#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000046#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000048#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000050#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000051#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000052#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000053#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000055#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000056using namespace llvm;
57
Chandler Carruth84e68b22014-04-22 02:41:26 +000058#define DEBUG_TYPE "arm-isel"
59
Dale Johannesend679ff72010-06-03 21:09:53 +000060STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000061STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000062STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000063
Evan Chengf128bdc2010-06-16 07:35:02 +000064static cl::opt<bool>
65ARMInterworking("arm-interworking", cl::Hidden,
66 cl::desc("Enable / disable ARM interworking (for debugging only)"),
67 cl::init(true));
68
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000069namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000070 class ARMCCState : public CCState {
71 public:
72 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000073 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
74 ParmContext PC)
75 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000076 assert(((PC == Call) || (PC == Prologue)) &&
77 "ARMCCState users must specify whether their context is call"
78 "or prologue generation.");
79 CallOrPrologue = PC;
80 }
81 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000082}
Cameron Zwarich89019782011-06-10 20:59:24 +000083
Stuart Hastings45fe3c32011-04-20 16:47:52 +000084// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000085static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000086 ARM::R0, ARM::R1, ARM::R2, ARM::R3
87};
88
Craig Topper4fa625f2012-08-12 03:16:37 +000089void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
90 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000091 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000092 setOperationAction(ISD::LOAD, VT, Promote);
93 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000094
Craig Topper4fa625f2012-08-12 03:16:37 +000095 setOperationAction(ISD::STORE, VT, Promote);
96 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000097 }
98
Craig Topper4fa625f2012-08-12 03:16:37 +000099 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000101 setOperationAction(ISD::SETCC, VT, Custom);
102 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000104 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
108 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000109 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000110 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
113 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000114 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000115 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
116 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
119 setOperationAction(ISD::SELECT, VT, Expand);
120 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000121 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000124 setOperationAction(ISD::SHL, VT, Custom);
125 setOperationAction(ISD::SRA, VT, Custom);
126 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000127 }
128
129 // Promote all bit-wise operations.
130 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000131 setOperationAction(ISD::AND, VT, Promote);
132 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
133 setOperationAction(ISD::OR, VT, Promote);
134 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
135 setOperationAction(ISD::XOR, VT, Promote);
136 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000137 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000138
139 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000140 setOperationAction(ISD::SDIV, VT, Expand);
141 setOperationAction(ISD::UDIV, VT, Expand);
142 setOperationAction(ISD::FDIV, VT, Expand);
143 setOperationAction(ISD::SREM, VT, Expand);
144 setOperationAction(ISD::UREM, VT, Expand);
145 setOperationAction(ISD::FREM, VT, Expand);
James Molloya6702e22015-07-17 17:10:55 +0000146
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000147 if (!VT.isFloatingPoint() &&
148 VT != MVT::v2i64 && VT != MVT::v1i64)
149 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
150 setOperationAction(Opcode, VT, Legal);
Bob Wilson2e076c42009-06-22 23:27:02 +0000151}
152
Craig Topper4fa625f2012-08-12 03:16:37 +0000153void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000154 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000155 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000156}
157
Craig Topper4fa625f2012-08-12 03:16:37 +0000158void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000159 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000160 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000161}
162
Eric Christopher1889fdc2015-01-29 00:19:39 +0000163ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
164 const ARMSubtarget &STI)
165 : TargetLowering(TM), Subtarget(&STI) {
166 RegInfo = Subtarget->getRegisterInfo();
167 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000168
Duncan Sandsf2641e12011-09-06 19:07:46 +0000169 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
170
Tim Northoverd6a729b2014-01-06 14:28:05 +0000171 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000172 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000173 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000174 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000175 static const struct {
176 const RTLIB::Libcall Op;
177 const char * const Name;
178 const ISD::CondCode Cond;
179 } LibraryCalls[] = {
180 // Single-precision floating-point arithmetic.
181 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
182 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
183 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
184 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000185
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000186 // Double-precision floating-point arithmetic.
187 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
189 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
190 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000191
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000192 // Single-precision comparisons.
193 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
194 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
195 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
196 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
197 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
198 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
199 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
200 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000201
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000202 // Double-precision comparisons.
203 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
204 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
205 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
206 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
207 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
208 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
209 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
210 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000211
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000212 // Floating-point to integer conversions.
213 // i64 conversions are done via library routines even when generating VFP
214 // instructions, so use the same ones.
215 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
216 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
217 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
218 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000219
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000220 // Conversions between floating types.
221 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
222 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000223
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000224 // Integer to floating-point conversions.
225 // i64 conversions are done via library routines even when generating VFP
226 // instructions, so use the same ones.
227 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
228 // e.g., __floatunsidf vs. __floatunssidfvfp.
229 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
230 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
231 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
232 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
233 };
Evan Cheng10043e22007-01-19 07:51:42 +0000234
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000235 for (const auto &LC : LibraryCalls) {
236 setLibcallName(LC.Op, LC.Name);
237 if (LC.Cond != ISD::SETCC_INVALID)
238 setCmpLibcallCC(LC.Op, LC.Cond);
239 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000240 }
Tim Northover8b403662015-10-28 22:51:16 +0000241
242 // Set the correct calling convention for ARMv7k WatchOS. It's just
243 // AAPCS_VFP for functions as simple as libcalls.
Tim Northover042a6c12016-01-27 19:32:29 +0000244 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000245 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i)
246 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP);
247 }
Evan Cheng10043e22007-01-19 07:51:42 +0000248 }
249
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000250 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000251 setLibcallName(RTLIB::SHL_I128, nullptr);
252 setLibcallName(RTLIB::SRL_I128, nullptr);
253 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000254
Renato Golin6d435f12015-11-09 12:40:30 +0000255 // RTLIB
256 if (Subtarget->isAAPCS_ABI() &&
257 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
258 Subtarget->isTargetAndroid())) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000259 static const struct {
260 const RTLIB::Libcall Op;
261 const char * const Name;
262 const CallingConv::ID CC;
263 const ISD::CondCode Cond;
264 } LibraryCalls[] = {
265 // Double-precision floating-point arithmetic helper functions
266 // RTABI chapter 4.1.2, Table 2
267 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
268 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
270 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000271
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000272 // Double-precision floating-point comparison helper functions
273 // RTABI chapter 4.1.2, Table 3
274 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
276 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
281 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000282
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000283 // Single-precision floating-point arithmetic helper functions
284 // RTABI chapter 4.1.2, Table 4
285 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
286 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
288 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000289
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000290 // Single-precision floating-point comparison helper functions
291 // RTABI chapter 4.1.2, Table 5
292 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
294 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
299 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000300
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000301 // Floating-point to integer conversions.
302 // RTABI chapter 4.1.2, Table 6
303 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
310 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000311
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000312 // Conversions between floating types.
313 // RTABI chapter 4.1.2, Table 7
314 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000315 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000316 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000317
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000318 // Integer to floating-point conversions.
319 // RTABI chapter 4.1.2, Table 8
320 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000328
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000329 // Long long helper functions
330 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000331 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000335
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000336 // Integer division functions
337 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000338 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000346 };
347
348 for (const auto &LC : LibraryCalls) {
349 setLibcallName(LC.Op, LC.Name);
350 setLibcallCallingConv(LC.Op, LC.CC);
351 if (LC.Cond != ISD::SETCC_INVALID)
352 setCmpLibcallCC(LC.Op, LC.Cond);
353 }
Renato Golin6d435f12015-11-09 12:40:30 +0000354
355 // EABI dependent RTLIB
356 if (TM.Options.EABIVersion == EABI::EABI4 ||
357 TM.Options.EABIVersion == EABI::EABI5) {
358 static const struct {
359 const RTLIB::Libcall Op;
360 const char *const Name;
361 const CallingConv::ID CC;
362 const ISD::CondCode Cond;
363 } MemOpsLibraryCalls[] = {
364 // Memory operations
365 // RTABI chapter 4.3.4
366 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
367 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
368 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
369 };
370
371 for (const auto &LC : MemOpsLibraryCalls) {
372 setLibcallName(LC.Op, LC.Name);
373 setLibcallCallingConv(LC.Op, LC.CC);
374 if (LC.Cond != ISD::SETCC_INVALID)
375 setCmpLibcallCC(LC.Op, LC.Cond);
376 }
377 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000378 }
379
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000380 if (Subtarget->isTargetWindows()) {
381 static const struct {
382 const RTLIB::Libcall Op;
383 const char * const Name;
384 const CallingConv::ID CC;
385 } LibraryCalls[] = {
386 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
387 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
388 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
389 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
390 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
391 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
392 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
393 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
394 };
395
396 for (const auto &LC : LibraryCalls) {
397 setLibcallName(LC.Op, LC.Name);
398 setLibcallCallingConv(LC.Op, LC.CC);
399 }
400 }
401
Bob Wilsonbc158992011-10-07 16:59:21 +0000402 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000403 if (Subtarget->isTargetWatchOS() ||
404 (Subtarget->isTargetIOS() &&
405 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000406 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
407 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
408 }
409
Oliver Stannard11790b22014-08-11 09:12:32 +0000410 // The half <-> float conversion functions are always soft-float, but are
411 // needed for some targets which use a hard-float calling convention by
412 // default.
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 }
422
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000423 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
424 // a __gnu_ prefix (which is the default).
425 if (Subtarget->isTargetAEABI()) {
426 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
427 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
428 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
429 }
430
David Goodwin22c2fba2009-07-08 23:10:31 +0000431 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000432 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000433 else
Craig Topperc7242e02012-04-20 07:30:17 +0000434 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000435 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000436 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000437 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000438 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000439 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000440
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000441 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000442 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000443 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000444 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
445 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
446 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
447 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000448
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000449 setOperationAction(ISD::MULHS, VT, Expand);
450 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
451 setOperationAction(ISD::MULHU, VT, Expand);
452 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000453
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000454 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000455 }
456
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000457 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000458 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000459
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000460 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
461 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
462
Bob Wilson2e076c42009-06-22 23:27:02 +0000463 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000464 addDRTypeForNEON(MVT::v2f32);
465 addDRTypeForNEON(MVT::v8i8);
466 addDRTypeForNEON(MVT::v4i16);
467 addDRTypeForNEON(MVT::v2i32);
468 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000469
Owen Anderson9f944592009-08-11 20:47:22 +0000470 addQRTypeForNEON(MVT::v4f32);
471 addQRTypeForNEON(MVT::v2f64);
472 addQRTypeForNEON(MVT::v16i8);
473 addQRTypeForNEON(MVT::v8i16);
474 addQRTypeForNEON(MVT::v4i32);
475 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000476
Bob Wilson194a2512009-09-15 23:55:57 +0000477 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
478 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000479 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
480 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000481 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
482 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
483 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000484 // FIXME: Code duplication: FDIV and FREM are expanded always, see
485 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000486 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
487 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000488 // FIXME: Create unittest.
489 // In another words, find a way when "copysign" appears in DAG with vector
490 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000491 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000492 // FIXME: Code duplication: SETCC has custom operation action, see
493 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000494 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000495 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000496 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
497 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
498 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
499 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
500 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
501 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
502 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
503 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
504 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
505 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
506 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
507 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000508 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000509 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
510 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
511 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
512 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
513 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000514 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000515
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000516 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
517 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
518 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
519 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
520 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
521 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
522 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
523 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
524 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
525 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000526 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
527 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
528 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
529 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000530 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000531
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000532 // Mark v2f32 intrinsics.
533 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
534 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
535 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
536 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
537 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
538 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
539 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
540 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
541 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
542 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
543 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
544 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
545 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
546 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
547 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
548
Bob Wilson6cc46572009-09-16 00:32:15 +0000549 // Neon does not support some operations on v1i64 and v2i64 types.
550 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000551 // Custom handling for some quad-vector types to detect VMULL.
552 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
553 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
554 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000555 // Custom handling for some vector types to avoid expensive expansions
556 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
557 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
558 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
559 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000560 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
561 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000562 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000563 // a destination type that is wider than the source, and nor does
564 // it have a FP_TO_[SU]INT instruction with a narrower destination than
565 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000566 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
567 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000568 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
569 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000570
Eli Friedmane6385e62012-11-15 22:44:27 +0000571 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000572 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000573
Evan Chengb4eae132012-12-04 22:41:50 +0000574 // NEON does not have single instruction CTPOP for vectors with element
575 // types wider than 8-bits. However, custom lowering can leverage the
576 // v8i8/v16i8 vcnt instruction.
577 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
578 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
579 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
580 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
Benjamin Kramer569efd22016-03-31 19:42:04 +0000581 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand);
582 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand);
Evan Chengb4eae132012-12-04 22:41:50 +0000583
Craig Topperedb4a6b2016-04-26 05:04:33 +0000584 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand);
585 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand);
586
Logan Chien0a43abc2015-07-13 15:37:30 +0000587 // NEON does not have single instruction CTTZ for vectors.
588 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
589 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
590 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
591 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
592
593 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
594 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
595 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
596 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
597
598 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
600 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
602
603 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
604 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
605 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
606 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
607
Jim Grosbach5f215872013-02-27 21:31:12 +0000608 // NEON only has FMA instructions as of VFP4.
609 if (!Subtarget->hasVFP4()) {
610 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
611 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
612 }
613
Bob Wilson06fce872011-02-07 17:43:21 +0000614 setTargetDAGCombine(ISD::INTRINSIC_VOID);
615 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000616 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
617 setTargetDAGCombine(ISD::SHL);
618 setTargetDAGCombine(ISD::SRL);
619 setTargetDAGCombine(ISD::SRA);
620 setTargetDAGCombine(ISD::SIGN_EXTEND);
621 setTargetDAGCombine(ISD::ZERO_EXTEND);
622 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000623 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000624 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000625 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
626 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000627 setTargetDAGCombine(ISD::FP_TO_SINT);
628 setTargetDAGCombine(ISD::FP_TO_UINT);
629 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000630 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000631
James Molloy547d4c02012-02-20 09:24:05 +0000632 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000633 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
634 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000635 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000636 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
637 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
638 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000639 }
James Molloy547d4c02012-02-20 09:24:05 +0000640 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000641 }
642
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000643 // ARM and Thumb2 support UMLAL/SMLAL.
644 if (!Subtarget->isThumb1Only())
645 setTargetDAGCombine(ISD::ADDC);
646
Oliver Stannard51b1d462014-08-21 12:50:31 +0000647 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000648 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000649 // operations, f64 is legal for the few double-precision instructions which
650 // are present However, no double-precision operations other than moves,
651 // loads and stores are provided by the hardware.
652 setOperationAction(ISD::FADD, MVT::f64, Expand);
653 setOperationAction(ISD::FSUB, MVT::f64, Expand);
654 setOperationAction(ISD::FMUL, MVT::f64, Expand);
655 setOperationAction(ISD::FMA, MVT::f64, Expand);
656 setOperationAction(ISD::FDIV, MVT::f64, Expand);
657 setOperationAction(ISD::FREM, MVT::f64, Expand);
658 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
659 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
660 setOperationAction(ISD::FNEG, MVT::f64, Expand);
661 setOperationAction(ISD::FABS, MVT::f64, Expand);
662 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
663 setOperationAction(ISD::FSIN, MVT::f64, Expand);
664 setOperationAction(ISD::FCOS, MVT::f64, Expand);
665 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
666 setOperationAction(ISD::FPOW, MVT::f64, Expand);
667 setOperationAction(ISD::FLOG, MVT::f64, Expand);
668 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
669 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
670 setOperationAction(ISD::FEXP, MVT::f64, Expand);
671 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
672 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
673 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
674 setOperationAction(ISD::FRINT, MVT::f64, Expand);
675 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
676 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000677 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
678 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
679 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
680 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
681 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
682 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000683 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
684 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
685 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000686
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000687 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000688
Tim Northover4e80b582014-07-18 13:01:19 +0000689 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000690 for (MVT VT : MVT::fp_valuetypes()) {
691 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
692 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
693 }
Tim Northover4e80b582014-07-18 13:01:19 +0000694
695 // ... or truncating stores
696 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
697 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
698 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000699
Duncan Sands95d46ef2008-01-23 20:39:46 +0000700 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000701 for (MVT VT : MVT::integer_valuetypes())
702 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000703
Evan Cheng10043e22007-01-19 07:51:42 +0000704 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000705 if (!Subtarget->isThumb1Only()) {
706 for (unsigned im = (unsigned)ISD::PRE_INC;
707 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000708 setIndexedLoadAction(im, MVT::i1, Legal);
709 setIndexedLoadAction(im, MVT::i8, Legal);
710 setIndexedLoadAction(im, MVT::i16, Legal);
711 setIndexedLoadAction(im, MVT::i32, Legal);
712 setIndexedStoreAction(im, MVT::i1, Legal);
713 setIndexedStoreAction(im, MVT::i8, Legal);
714 setIndexedStoreAction(im, MVT::i16, Legal);
715 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000716 }
Evan Cheng10043e22007-01-19 07:51:42 +0000717 }
718
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000719 setOperationAction(ISD::SADDO, MVT::i32, Custom);
720 setOperationAction(ISD::UADDO, MVT::i32, Custom);
721 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
722 setOperationAction(ISD::USUBO, MVT::i32, Custom);
723
Evan Cheng10043e22007-01-19 07:51:42 +0000724 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000725 setOperationAction(ISD::MUL, MVT::i64, Expand);
726 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000727 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000728 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
729 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000730 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000731 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000732 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000733 setOperationAction(ISD::MULHS, MVT::i32, Expand);
734
Jim Grosbach5d994042009-10-31 19:38:01 +0000735 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000736 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000737 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000738 setOperationAction(ISD::SRL, MVT::i64, Custom);
739 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000740
Evan Chenge8916542011-08-30 01:34:54 +0000741 if (!Subtarget->isThumb1Only()) {
742 // FIXME: We should do this for Thumb1 as well.
743 setOperationAction(ISD::ADDC, MVT::i32, Custom);
744 setOperationAction(ISD::ADDE, MVT::i32, Custom);
745 setOperationAction(ISD::SUBC, MVT::i32, Custom);
746 setOperationAction(ISD::SUBE, MVT::i32, Custom);
747 }
748
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000749 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000750 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
751
Evan Cheng10043e22007-01-19 07:51:42 +0000752 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000753 setOperationAction(ISD::ROTL, MVT::i32, Expand);
754 for (MVT VT : MVT::vector_valuetypes()) {
755 setOperationAction(ISD::ROTL, VT, Expand);
756 setOperationAction(ISD::ROTR, VT, Expand);
757 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000758 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000759 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000760 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000761 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000762
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000763 // @llvm.readcyclecounter requires the Performance Monitors extension.
764 // Default to the 0 expansion on unsupported platforms.
765 // FIXME: Technically there are older ARM CPUs that have
766 // implementation-specific ways of obtaining this information.
767 if (Subtarget->hasPerfMon())
768 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000769
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000770 // Only ARMv6 has BSWAP.
771 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000772 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000773
Bradley Smith519563e2016-01-15 10:25:35 +0000774 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide()
775 : Subtarget->hasDivideInARMMode();
776 if (!hasDivide) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000777 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000778 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
779 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000780 }
Renato Golin87610692013-07-16 09:32:17 +0000781
Saleem Abdulrasool071a0992016-03-17 14:10:49 +0000782 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
783 setOperationAction(ISD::SDIV, MVT::i32, Custom);
784 setOperationAction(ISD::UDIV, MVT::i32, Custom);
785
786 setOperationAction(ISD::SDIV, MVT::i64, Custom);
787 setOperationAction(ISD::UDIV, MVT::i64, Custom);
788 }
789
Chad Rosierad7c9102014-08-23 18:29:43 +0000790 setOperationAction(ISD::SREM, MVT::i32, Expand);
791 setOperationAction(ISD::UREM, MVT::i32, Expand);
792 // Register based DivRem for AEABI (RTABI 4.2)
Renato Golin6027dd38e2016-02-03 16:10:54 +0000793 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
794 Subtarget->isTargetGNUAEABI()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000795 setOperationAction(ISD::SREM, MVT::i64, Custom);
796 setOperationAction(ISD::UREM, MVT::i64, Custom);
797
Chad Rosierad7c9102014-08-23 18:29:43 +0000798 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
799 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
800 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
801 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
802 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
803 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
804 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
805 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
806
807 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
808 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
809 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
810 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
811 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
812 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
813 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
814 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
815
816 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
817 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Renato Golin175c6d62016-03-04 19:19:36 +0000818 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
819 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Chad Rosierad7c9102014-08-23 18:29:43 +0000820 } else {
Renato Golin87610692013-07-16 09:32:17 +0000821 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
822 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
823 }
Bob Wilson7117a912009-03-20 22:42:55 +0000824
Owen Anderson9f944592009-08-11 20:47:22 +0000825 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
826 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000827 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000828 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000829
Evan Cheng74d92c12011-04-08 21:37:21 +0000830 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000831
Evan Cheng10043e22007-01-19 07:51:42 +0000832 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000833 setOperationAction(ISD::VASTART, MVT::Other, Custom);
834 setOperationAction(ISD::VAARG, MVT::Other, Expand);
835 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
836 setOperationAction(ISD::VAEND, MVT::Other, Expand);
837 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
838 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000839
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000840 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
841 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
842 else
843 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
844
Evan Cheng6e809de2010-08-11 06:22:01 +0000845 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
James Y Knighte6a46462016-04-01 19:33:19 +0000846 // the default expansion.
James Y Knightf44fc522016-03-16 22:12:04 +0000847 InsertFencesForAtomic = false;
James Y Knighte6a46462016-04-01 19:33:19 +0000848 if (Subtarget->hasAnyDataBarrier() &&
849 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000850 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
851 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000852 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverb629c772016-04-18 21:48:55 +0000853 if (!Subtarget->isThumb() || !Subtarget->isMClass())
854 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000855
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000856 // On v8, we have particularly efficient implementations of atomic fences
857 // if they can be combined with nearby atomic loads and stores.
Tim Northoverb629c772016-04-18 21:48:55 +0000858 if (!Subtarget->hasV8Ops() || getTargetMachine().getOptLevel() == 0) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000859 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
James Y Knightf44fc522016-03-16 22:12:04 +0000860 InsertFencesForAtomic = true;
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000861 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000862 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000863 // If there's anything we can use as a barrier, go through custom lowering
864 // for ATOMIC_FENCE.
865 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
866 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
867
Jim Grosbach6860bb72010-06-18 22:35:32 +0000868 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000869 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000870 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000871 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000872 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000873 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000874 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000875 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000876 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000877 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000878 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000879 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000880 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000881 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
882 // Unordered/Monotonic case.
883 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
884 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000885 }
Evan Cheng10043e22007-01-19 07:51:42 +0000886
Evan Cheng21acf9f2010-11-04 05:19:35 +0000887 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000888
Eli Friedman8cfa7712010-06-26 04:36:50 +0000889 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
890 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000891 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
892 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000893 }
Owen Anderson9f944592009-08-11 20:47:22 +0000894 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000895
Eric Christopher824f42f2015-05-12 01:26:05 +0000896 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000897 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000898 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000899 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000900 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000901 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
902 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000903
904 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000905 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000906 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
907 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
908 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +0000909 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +0000910 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000911
Owen Anderson9f944592009-08-11 20:47:22 +0000912 setOperationAction(ISD::SETCC, MVT::i32, Expand);
913 setOperationAction(ISD::SETCC, MVT::f32, Expand);
914 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000915 setOperationAction(ISD::SELECT, MVT::i32, Custom);
916 setOperationAction(ISD::SELECT, MVT::f32, Custom);
917 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000918 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
919 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
920 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000921
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +0000922 // Thumb-1 cannot currently select ARMISD::SUBE.
923 if (!Subtarget->isThumb1Only())
924 setOperationAction(ISD::SETCCE, MVT::i32, Custom);
925
Owen Anderson9f944592009-08-11 20:47:22 +0000926 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
927 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
928 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
929 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
930 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000931
Dan Gohman482732a2007-10-11 23:21:31 +0000932 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000933 setOperationAction(ISD::FSIN, MVT::f64, Expand);
934 setOperationAction(ISD::FSIN, MVT::f32, Expand);
935 setOperationAction(ISD::FCOS, MVT::f32, Expand);
936 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000937 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
938 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000939 setOperationAction(ISD::FREM, MVT::f64, Expand);
940 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000941 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000942 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000943 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
944 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000945 }
Owen Anderson9f944592009-08-11 20:47:22 +0000946 setOperationAction(ISD::FPOW, MVT::f64, Expand);
947 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000948
Evan Chengd0007f32012-04-10 21:40:28 +0000949 if (!Subtarget->hasVFP4()) {
950 setOperationAction(ISD::FMA, MVT::f64, Expand);
951 setOperationAction(ISD::FMA, MVT::f32, Expand);
952 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000953
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000954 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000955 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000956 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
957 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000958 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
959 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
960 }
961
962 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000963 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000964 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
965 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000966 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000967 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000968
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000969 // Combine sin / cos into one node or libcall if possible.
970 if (Subtarget->hasSinCos()) {
971 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
972 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover042a6c12016-01-27 19:32:29 +0000973 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000974 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
975 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
976 }
977 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000978 // For iOS, we don't want to the normal expansion of a libcall to
979 // sincos. We want to issue a libcall to __sincos_stret.
980 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
981 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
982 }
983 }
Evan Cheng10043e22007-01-19 07:51:42 +0000984
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000985 // FP-ARMv8 implements a lot of rounding-like FP operations.
986 if (Subtarget->hasFPARMv8()) {
987 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
988 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
989 setOperationAction(ISD::FROUND, MVT::f32, Legal);
990 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
991 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
992 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000993 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
994 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000995 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
996 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
997 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
998 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
999
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001000 if (!Subtarget->isFPOnlySP()) {
1001 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1002 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1003 setOperationAction(ISD::FROUND, MVT::f64, Legal);
1004 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1005 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1006 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001007 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1008 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001009 }
1010 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001011
James Molloy974838f2015-08-17 19:37:12 +00001012 if (Subtarget->hasNEON()) {
1013 // vmin and vmax aren't available in a scalar form, so we use
1014 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001015 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1016 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001017 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1018 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1019 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1020 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1021 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001022
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001023 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001024 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001025 setTargetDAGCombine(ISD::ADD);
1026 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001027 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001028 setTargetDAGCombine(ISD::AND);
1029 setTargetDAGCombine(ISD::OR);
1030 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001031
Evan Chengf258a152012-02-23 02:58:19 +00001032 if (Subtarget->hasV6Ops())
1033 setTargetDAGCombine(ISD::SRL);
1034
Evan Cheng10043e22007-01-19 07:51:42 +00001035 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001036
Eric Christopher824f42f2015-05-12 01:26:05 +00001037 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001038 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001039 setSchedulingPreference(Sched::RegPressure);
1040 else
1041 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001042
Evan Cheng3ae2b792011-01-06 06:52:41 +00001043 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001044 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001045 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001046 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001047 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001048 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001049 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001050
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001051 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1052 // are at least 4 bytes aligned.
1053 setMinStackArgumentAlignment(4);
1054
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001055 // Prefer likely predicted branches to selects on out-of-order cores.
Junmo Park453f4aa2016-02-23 09:56:58 +00001056 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001057
Eli Friedman2518f832011-05-06 20:34:06 +00001058 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001059}
1060
Eric Christopher824f42f2015-05-12 01:26:05 +00001061bool ARMTargetLowering::useSoftFloat() const {
1062 return Subtarget->useSoftFloat();
1063}
1064
Andrew Trick43f25632011-01-19 02:35:27 +00001065// FIXME: It might make sense to define the representative register class as the
1066// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1067// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1068// SPR's representative would be DPR_VFP2. This should work well if register
1069// pressure tracking were modified such that a register use would increment the
1070// pressure of the register class's representative and all of it's super
1071// classes' representatives transitively. We have not implemented this because
1072// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001073// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001074// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001075std::pair<const TargetRegisterClass *, uint8_t>
1076ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1077 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001078 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001079 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001080 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001081 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001082 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001083 // Use DPR as representative register class for all floating point
1084 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1085 // the cost is 1 for both f32 and f64.
1086 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001087 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001088 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001089 // When NEON is used for SP, only half of the register file is available
1090 // because operations that define both SP and DP results will be constrained
1091 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1092 // coalescing by double-counting the SP regs. See the FIXME above.
1093 if (Subtarget->useNEONForSinglePrecisionFP())
1094 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001095 break;
1096 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1097 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001098 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001099 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001100 break;
1101 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001102 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001103 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001104 break;
1105 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001106 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001107 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001108 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001109 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001110 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001111}
1112
Evan Cheng10043e22007-01-19 07:51:42 +00001113const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001114 switch ((ARMISD::NodeType)Opcode) {
1115 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001116 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001117 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001118 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001119 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001120 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001121 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001122 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
Evan Cheng10043e22007-01-19 07:51:42 +00001123 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1124 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001125 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001126 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001127 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001128 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1129 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001130 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001131 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001132 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1133 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001134 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001135 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001136
Evan Cheng10043e22007-01-19 07:51:42 +00001137 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001138
Evan Cheng10043e22007-01-19 07:51:42 +00001139 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1140 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1141 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001142
Evan Chenge8916542011-08-30 01:34:54 +00001143 case ARMISD::ADDC: return "ARMISD::ADDC";
1144 case ARMISD::ADDE: return "ARMISD::ADDE";
1145 case ARMISD::SUBC: return "ARMISD::SUBC";
1146 case ARMISD::SUBE: return "ARMISD::SUBE";
1147
Bob Wilson22806742010-09-22 22:09:21 +00001148 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1149 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001150
Evan Chengec6d7c92009-10-28 06:55:03 +00001151 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001152 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1153 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001154
Dale Johannesend679ff72010-06-03 21:09:53 +00001155 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001156
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001157 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001158
Evan Chengb972e562009-08-07 00:34:42 +00001159 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1160
Bob Wilson7ed59712010-10-30 00:54:37 +00001161 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001162
Evan Cheng8740ee32010-11-03 06:34:55 +00001163 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1164
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001165 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001166 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001167
Bob Wilson2e076c42009-06-22 23:27:02 +00001168 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001169 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001170 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001171 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1172 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001173 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1174 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001175 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1176 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001177 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1178 case ARMISD::VTST: return "ARMISD::VTST";
1179
1180 case ARMISD::VSHL: return "ARMISD::VSHL";
1181 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1182 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001183 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1184 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1185 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1186 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1187 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1188 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1189 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1190 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1191 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1192 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1193 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1194 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001195 case ARMISD::VSLI: return "ARMISD::VSLI";
1196 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001197 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1198 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001199 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001200 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001201 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001202 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001203 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001204 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001205 case ARMISD::VREV64: return "ARMISD::VREV64";
1206 case ARMISD::VREV32: return "ARMISD::VREV32";
1207 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001208 case ARMISD::VZIP: return "ARMISD::VZIP";
1209 case ARMISD::VUZP: return "ARMISD::VUZP";
1210 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001211 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1212 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001213 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1214 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001215 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1216 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001217 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001218 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001219 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1220 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001221 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001222 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001223 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1224 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1225 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001226 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1227 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1228 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1229 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1230 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1231 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1232 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1233 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1234 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1235 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1236 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1237 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1238 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1239 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1240 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1241 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1242 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001243 }
Matthias Braund04893f2015-05-07 21:33:59 +00001244 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001245}
1246
Mehdi Amini44ede332015-07-09 02:09:04 +00001247EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1248 EVT VT) const {
1249 if (!VT.isVector())
1250 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001251 return VT.changeVectorElementTypeToInteger();
1252}
1253
Evan Cheng4cad68e2010-05-15 02:18:07 +00001254/// getRegClassFor - Return the register class that should be used for the
1255/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001256const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001257 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1258 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1259 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001260 if (Subtarget->hasNEON()) {
1261 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001262 return &ARM::QQPRRegClass;
1263 if (VT == MVT::v8i64)
1264 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001265 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001266 return TargetLowering::getRegClassFor(VT);
1267}
1268
John Brawn0dbcd652015-03-18 12:01:59 +00001269// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1270// source/dest is aligned and the copy size is large enough. We therefore want
1271// to align such objects passed to memory intrinsics.
1272bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1273 unsigned &PrefAlign) const {
1274 if (!isa<MemIntrinsic>(CI))
1275 return false;
1276 MinSize = 8;
1277 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1278 // cycle faster than 4-byte aligned LDM.
1279 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1280 return true;
1281}
1282
Eric Christopher84bdfd82010-07-21 22:26:11 +00001283// Create a fast isel object.
1284FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001285ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1286 const TargetLibraryInfo *libInfo) const {
1287 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001288}
1289
Evan Cheng4401f882010-05-20 23:26:43 +00001290Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001291 unsigned NumVals = N->getNumValues();
1292 if (!NumVals)
1293 return Sched::RegPressure;
1294
1295 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001296 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001297 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001298 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001299 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001300 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001301 }
Evan Chengbf914992010-05-28 23:25:23 +00001302
1303 if (!N->isMachineOpcode())
1304 return Sched::RegPressure;
1305
1306 // Load are scheduled for latency even if there instruction itinerary
1307 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001308 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001309 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001310
Evan Cheng6cc775f2011-06-28 19:10:37 +00001311 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001312 return Sched::RegPressure;
1313 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001314 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001315 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001316
Evan Cheng4401f882010-05-20 23:26:43 +00001317 return Sched::RegPressure;
1318}
1319
Evan Cheng10043e22007-01-19 07:51:42 +00001320//===----------------------------------------------------------------------===//
1321// Lowering Code
1322//===----------------------------------------------------------------------===//
1323
Evan Cheng10043e22007-01-19 07:51:42 +00001324/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1325static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1326 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001327 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001328 case ISD::SETNE: return ARMCC::NE;
1329 case ISD::SETEQ: return ARMCC::EQ;
1330 case ISD::SETGT: return ARMCC::GT;
1331 case ISD::SETGE: return ARMCC::GE;
1332 case ISD::SETLT: return ARMCC::LT;
1333 case ISD::SETLE: return ARMCC::LE;
1334 case ISD::SETUGT: return ARMCC::HI;
1335 case ISD::SETUGE: return ARMCC::HS;
1336 case ISD::SETULT: return ARMCC::LO;
1337 case ISD::SETULE: return ARMCC::LS;
1338 }
1339}
1340
Bob Wilsona2e83332009-09-09 23:14:54 +00001341/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1342static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001343 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001344 CondCode2 = ARMCC::AL;
1345 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001346 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001347 case ISD::SETEQ:
1348 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1349 case ISD::SETGT:
1350 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1351 case ISD::SETGE:
1352 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1353 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001354 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001355 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1356 case ISD::SETO: CondCode = ARMCC::VC; break;
1357 case ISD::SETUO: CondCode = ARMCC::VS; break;
1358 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1359 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1360 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1361 case ISD::SETLT:
1362 case ISD::SETULT: CondCode = ARMCC::LT; break;
1363 case ISD::SETLE:
1364 case ISD::SETULE: CondCode = ARMCC::LE; break;
1365 case ISD::SETNE:
1366 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1367 }
Evan Cheng10043e22007-01-19 07:51:42 +00001368}
1369
Bob Wilsona4c22902009-04-17 19:07:39 +00001370//===----------------------------------------------------------------------===//
1371// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001372//===----------------------------------------------------------------------===//
1373
1374#include "ARMGenCallingConv.inc"
1375
Oliver Stannardc24f2172014-05-09 14:01:47 +00001376/// getEffectiveCallingConv - Get the effective calling convention, taking into
1377/// account presence of floating point hardware and calling convention
1378/// limitations, such as support for variadic functions.
1379CallingConv::ID
1380ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1381 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001382 switch (CC) {
1383 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001384 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001385 case CallingConv::ARM_AAPCS:
1386 case CallingConv::ARM_APCS:
1387 case CallingConv::GHC:
1388 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001389 case CallingConv::PreserveMost:
1390 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001391 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001392 case CallingConv::Swift:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001393 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1394 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001395 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001396 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001397 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001398 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1399 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001400 return CallingConv::ARM_AAPCS_VFP;
1401 else
1402 return CallingConv::ARM_AAPCS;
1403 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001404 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001405 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001406 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001407 return CallingConv::Fast;
1408 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001409 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001410 return CallingConv::ARM_AAPCS_VFP;
1411 else
1412 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001413 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001414}
1415
1416/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1417/// CallingConvention.
1418CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1419 bool Return,
1420 bool isVarArg) const {
1421 switch (getEffectiveCallingConv(CC, isVarArg)) {
1422 default:
1423 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001424 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001425 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001426 case CallingConv::ARM_AAPCS:
1427 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1428 case CallingConv::ARM_AAPCS_VFP:
1429 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1430 case CallingConv::Fast:
1431 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001432 case CallingConv::GHC:
1433 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001434 case CallingConv::PreserveMost:
1435 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001436 }
1437}
1438
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001439/// LowerCallResult - Lower the result values of a call into the
1440/// appropriate copies out of appropriate physical registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001441SDValue ARMTargetLowering::LowerCallResult(
1442 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1443 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1444 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
1445 SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001446
Bob Wilsona4c22902009-04-17 19:07:39 +00001447 // Assign locations to each value returned by this call.
1448 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001449 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1450 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001451 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001452 CCAssignFnForNode(CallConv, /* Return*/ true,
1453 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001454
1455 // Copy all of the result registers out of their specified physreg.
1456 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1457 CCValAssign VA = RVLocs[i];
1458
Stephen Linb8bd2322013-04-20 05:14:40 +00001459 // Pass 'this' value directly from the argument to return value, to avoid
1460 // reg unit interference
1461 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001462 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1463 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001464 InVals.push_back(ThisVal);
1465 continue;
1466 }
1467
Bob Wilson0041bd32009-04-25 00:33:20 +00001468 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001469 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001470 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001471 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001472 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001473 Chain = Lo.getValue(1);
1474 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001475 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001476 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001477 InFlag);
1478 Chain = Hi.getValue(1);
1479 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001480 if (!Subtarget->isLittle())
1481 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001482 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001483
Owen Anderson9f944592009-08-11 20:47:22 +00001484 if (VA.getLocVT() == MVT::v2f64) {
1485 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1486 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001487 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001488
1489 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001490 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001491 Chain = Lo.getValue(1);
1492 InFlag = Lo.getValue(2);
1493 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001494 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001495 Chain = Hi.getValue(1);
1496 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001497 if (!Subtarget->isLittle())
1498 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001499 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001500 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001501 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001502 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001503 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001504 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1505 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001506 Chain = Val.getValue(1);
1507 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001508 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001509
1510 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001511 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001512 case CCValAssign::Full: break;
1513 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001514 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001515 break;
1516 }
1517
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001518 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001519 }
1520
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001521 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001522}
1523
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001524/// LowerMemOpCallTo - Store the argument to the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001525SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
1526 SDValue Arg, const SDLoc &dl,
1527 SelectionDAG &DAG,
1528 const CCValAssign &VA,
1529 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001530 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001531 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001532 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1533 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001534 return DAG.getStore(
1535 Chain, dl, Arg, PtrOff,
1536 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1537 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001538}
1539
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001540void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001541 SDValue Chain, SDValue &Arg,
1542 RegsToPassVector &RegsToPass,
1543 CCValAssign &VA, CCValAssign &NextVA,
1544 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001545 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001546 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001547
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001548 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001549 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001550 unsigned id = Subtarget->isLittle() ? 0 : 1;
1551 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001552
1553 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001554 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001555 else {
1556 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001557 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001558 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1559 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001560
Christian Pirkerb5728192014-05-08 14:06:24 +00001561 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001562 dl, DAG, NextVA,
1563 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001564 }
1565}
1566
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001567/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001568/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1569/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001570SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001571ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001572 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001573 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001574 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001575 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1576 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1577 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001578 SDValue Chain = CLI.Chain;
1579 SDValue Callee = CLI.Callee;
1580 bool &isTailCall = CLI.IsTailCall;
1581 CallingConv::ID CallConv = CLI.CallConv;
1582 bool doesNotRet = CLI.DoesNotReturn;
1583 bool isVarArg = CLI.IsVarArg;
1584
Dale Johannesend679ff72010-06-03 21:09:53 +00001585 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001586 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1587 bool isThisReturn = false;
1588 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001589 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001590
Bob Wilson8decdc42011-10-07 17:17:49 +00001591 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001592 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001593 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001594
Dale Johannesend679ff72010-06-03 21:09:53 +00001595 if (isTailCall) {
1596 // Check if it's really possible to do a tail call.
1597 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001598 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001599 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001600 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1601 report_fatal_error("failed to perform tail call elimination on a call "
1602 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001603 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1604 // detected sibcalls.
1605 if (isTailCall) {
1606 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001607 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001608 }
1609 }
Evan Cheng10043e22007-01-19 07:51:42 +00001610
Bob Wilsona4c22902009-04-17 19:07:39 +00001611 // Analyze operands of the call, assigning locations to each operand.
1612 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001613 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1614 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001615 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001616 CCAssignFnForNode(CallConv, /* Return*/ false,
1617 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001618
Bob Wilsona4c22902009-04-17 19:07:39 +00001619 // Get a count of how many bytes are to be pushed on the stack.
1620 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001621
Dale Johannesend679ff72010-06-03 21:09:53 +00001622 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001623 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001624 NumBytes = 0;
1625
Evan Cheng10043e22007-01-19 07:51:42 +00001626 // Adjust the stack pointer for the new arguments...
1627 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001628 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001629 Chain = DAG.getCALLSEQ_START(Chain,
1630 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001631
Mehdi Amini44ede332015-07-09 02:09:04 +00001632 SDValue StackPtr =
1633 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001634
Bob Wilson2e076c42009-06-22 23:27:02 +00001635 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001636 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001637
Bob Wilsona4c22902009-04-17 19:07:39 +00001638 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001639 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001640 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1641 i != e;
1642 ++i, ++realArgIdx) {
1643 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001644 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001645 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001646 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001647
Bob Wilsona4c22902009-04-17 19:07:39 +00001648 // Promote the value if needed.
1649 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001650 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001651 case CCValAssign::Full: break;
1652 case CCValAssign::SExt:
1653 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1654 break;
1655 case CCValAssign::ZExt:
1656 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1657 break;
1658 case CCValAssign::AExt:
1659 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1660 break;
1661 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001662 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001663 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001664 }
1665
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001666 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001667 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001668 if (VA.getLocVT() == MVT::v2f64) {
1669 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001670 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001671 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001672 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001673
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001674 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001675 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1676
1677 VA = ArgLocs[++i]; // skip ahead to next loc
1678 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001679 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001680 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1681 } else {
1682 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001683
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001684 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1685 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001686 }
1687 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001688 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001689 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001690 }
1691 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001692 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1693 assert(VA.getLocVT() == MVT::i32 &&
1694 "unexpected calling convention register assignment");
1695 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001696 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001697 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001698 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001699 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001700 } else if (isByVal) {
1701 assert(VA.isMemLoc());
1702 unsigned offset = 0;
1703
1704 // True if this byval aggregate will be split between registers
1705 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001706 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001707 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001708
1709 if (CurByValIdx < ByValArgsCount) {
1710
1711 unsigned RegBegin, RegEnd;
1712 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1713
Mehdi Amini44ede332015-07-09 02:09:04 +00001714 EVT PtrVT =
1715 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001716 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001717 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001718 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001719 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1720 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1721 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001722 false, false, false,
1723 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001724 MemOpChains.push_back(Load.getValue(1));
1725 RegsToPass.push_back(std::make_pair(j, Load));
1726 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001727
1728 // If parameter size outsides register area, "offset" value
1729 // helps us to calculate stack slot for remained part properly.
1730 offset = RegEnd - RegBegin;
1731
1732 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001733 }
1734
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001735 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001736 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001737 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001738 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001739 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001740 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001741 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001742 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001743 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001744 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1745 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001746
Manman Ren9f911162012-06-01 02:44:42 +00001747 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001748 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001749 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001750 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001751 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001752 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001753 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001754
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001755 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1756 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001757 }
Evan Cheng10043e22007-01-19 07:51:42 +00001758 }
1759
1760 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001761 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001762
1763 // Build a sequence of copy-to-reg nodes chained together with token chain
1764 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001765 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001766 // Tail call byval lowering might overwrite argument registers so in case of
1767 // tail call optimization the copies to registers are lowered later.
1768 if (!isTailCall)
1769 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1770 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1771 RegsToPass[i].second, InFlag);
1772 InFlag = Chain.getValue(1);
1773 }
Evan Cheng10043e22007-01-19 07:51:42 +00001774
Dale Johannesend679ff72010-06-03 21:09:53 +00001775 // For tail calls lower the arguments to the 'real' stack slot.
1776 if (isTailCall) {
1777 // Force all the incoming stack arguments to be loaded from the stack
1778 // before any new outgoing arguments are stored to the stack, because the
1779 // outgoing stack slots may alias the incoming argument stack slots, and
1780 // the alias isn't otherwise explicit. This is slightly more conservative
1781 // than necessary, because it means that each store effectively depends
1782 // on every argument instead of just those arguments it would clobber.
1783
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001784 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001785 InFlag = SDValue();
1786 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1787 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1788 RegsToPass[i].second, InFlag);
1789 InFlag = Chain.getValue(1);
1790 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001791 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001792 }
1793
Bill Wendling24c79f22008-09-16 21:48:12 +00001794 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1795 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1796 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001797 bool isDirect = false;
Rafael Espindola3888bdb2016-06-16 15:22:01 +00001798
1799 const TargetMachine &TM = getTargetMachine();
1800 Reloc::Model RM = TM.getRelocationModel();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00001801 const Triple &TargetTriple = TM.getTargetTriple();
1802 const Module *Mod = MF.getFunction()->getParent();
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001803 const GlobalValue *GV = nullptr;
1804 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1805 GV = G->getGlobal();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00001806 bool isStub =
1807 !shouldAssumeDSOLocal(RM, TargetTriple, *Mod, GV) &&
1808 Subtarget->isTargetMachO();
Rafael Espindola3888bdb2016-06-16 15:22:01 +00001809
Rafael Espindola9ba9c5b2016-06-16 15:44:06 +00001810 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001811 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001812 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001813 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001814
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001815 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001816 assert((Subtarget->isTargetWindows() ||
Rafael Espindola3888bdb2016-06-16 15:22:01 +00001817 RM == Reloc::Static) &&
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001818 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001819 // Handle a global address or an external symbol. If it's not one of
1820 // those, the target's already in a register, so we don't need to do
1821 // anything extra.
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001822 if (isa<GlobalAddressSDNode>(Callee)) {
Jim Grosbach32bb3622010-04-14 22:28:31 +00001823 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001824 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001825 ARMConstantPoolValue *CPV =
1826 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1827
Jim Grosbach32bb3622010-04-14 22:28:31 +00001828 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001829 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001830 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001831 Callee = DAG.getLoad(
1832 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1833 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1834 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001835 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1836 const char *Sym = S->getSymbol();
1837
1838 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001839 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001840 ARMConstantPoolValue *CPV =
1841 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1842 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001843 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001844 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001845 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001846 Callee = DAG.getLoad(
1847 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1848 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1849 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001850 }
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001851 } else if (isa<GlobalAddressSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001852 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001853 bool isDef = GV->isStrongDefinitionForLinker();
Rafael Espindola41410cc2016-06-01 21:57:11 +00001854
Evan Chengc3c949b42007-06-19 21:05:09 +00001855 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001856 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001857 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001858 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001859 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001860 Callee = DAG.getNode(
1861 ARMISD::WrapperPIC, dl, PtrVt,
1862 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1863 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001864 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1865 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001866 } else if (Subtarget->isTargetCOFF()) {
1867 assert(Subtarget->isTargetWindows() &&
1868 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001869 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1870 ? ARMII::MO_DLLIMPORT
1871 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001872 Callee =
1873 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001874 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001875 Callee =
1876 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1877 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001878 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1879 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001880 } else {
1881 // On ELF targets for PIC code, direct calls should go through the PLT
1882 unsigned OpFlags = 0;
1883 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001884 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001885 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001886 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001887 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001888 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001889 isDirect = true;
Evan Cheng83f35172007-01-30 20:37:08 +00001890 // tBX takes a register source operand.
1891 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001892 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001893 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001894 ARMConstantPoolValue *CPV =
1895 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1896 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001897 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001898 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001899 Callee = DAG.getLoad(
1900 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1901 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1902 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001903 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001904 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001905 } else {
1906 unsigned OpFlags = 0;
1907 // On ELF targets for PIC code, direct calls should go through the PLT
1908 if (Subtarget->isTargetELF() &&
1909 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1910 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001911 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001912 }
Evan Cheng10043e22007-01-19 07:51:42 +00001913 }
1914
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001915 // FIXME: handle tail calls differently.
1916 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001917 if (Subtarget->isThumb()) {
1918 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001919 CallOpc = ARMISD::CALL_NOLINK;
1920 else
Tim Northoverb5ece522016-05-10 19:17:47 +00001921 CallOpc = ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001922 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001923 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001924 CallOpc = ARMISD::CALL_NOLINK;
Sjoerd Meijerd906bf12016-06-03 14:03:27 +00001925 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001926 // Emit regular call when code size is the priority
1927 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001928 // "mov lr, pc; b _foo" to avoid confusing the RSP
1929 CallOpc = ARMISD::CALL_NOLINK;
1930 else
1931 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001932 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001933
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001934 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001935 Ops.push_back(Chain);
1936 Ops.push_back(Callee);
1937
1938 // Add argument registers to the end of the list so that they are known live
1939 // into the call.
1940 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1941 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1942 RegsToPass[i].second.getValueType()));
1943
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001944 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001945 if (!isTailCall) {
1946 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001947 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001948 if (isThisReturn) {
1949 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001950 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001951 if (!Mask) {
1952 // Set isThisReturn to false if the calling convention is not one that
1953 // allows 'returned' to be modeled in this way, so LowerCallResult does
1954 // not try to pass 'this' straight through
1955 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001956 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001957 }
1958 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001959 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001960
Matthias Braunc22630e2013-10-04 16:52:54 +00001961 assert(Mask && "Missing call preserved mask for calling convention");
1962 Ops.push_back(DAG.getRegisterMask(Mask));
1963 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001964
Gabor Greiff304a7a2008-08-28 21:40:38 +00001965 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001966 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001967
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001968 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001969 if (isTailCall) {
1970 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001971 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001972 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001973
Duncan Sands739a0542008-07-02 17:40:58 +00001974 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001975 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001976 InFlag = Chain.getValue(1);
1977
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001978 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1979 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001980 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001981 InFlag = Chain.getValue(1);
1982
Bob Wilsona4c22902009-04-17 19:07:39 +00001983 // Handle result values, copying them out of physregs into vregs that we
1984 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001985 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001986 InVals, isThisReturn,
1987 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001988}
1989
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001990/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001991/// on the stack. Remember the next parameter register to allocate,
1992/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001993/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001994void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1995 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001996 assert((State->getCallOrPrologue() == Prologue ||
1997 State->getCallOrPrologue() == Call) &&
1998 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001999
Tim Northover8cda34f2015-03-11 18:54:22 +00002000 // Byval (as with any stack) slots are always at least 4 byte aligned.
2001 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002002
Tim Northover8cda34f2015-03-11 18:54:22 +00002003 unsigned Reg = State->AllocateReg(GPRArgRegs);
2004 if (!Reg)
2005 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002006
Tim Northover8cda34f2015-03-11 18:54:22 +00002007 unsigned AlignInRegs = Align / 4;
2008 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2009 for (unsigned i = 0; i < Waste; ++i)
2010 Reg = State->AllocateReg(GPRArgRegs);
2011
2012 if (!Reg)
2013 return;
2014
2015 unsigned Excess = 4 * (ARM::R4 - Reg);
2016
2017 // Special case when NSAA != SP and parameter size greater than size of
2018 // all remained GPR regs. In that case we can't split parameter, we must
2019 // send it to stack. We also must set NCRN to R4, so waste all
2020 // remained registers.
2021 const unsigned NSAAOffset = State->getNextStackOffset();
2022 if (NSAAOffset != 0 && Size > Excess) {
2023 while (State->AllocateReg(GPRArgRegs))
2024 ;
2025 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002026 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002027
2028 // First register for byval parameter is the first register that wasn't
2029 // allocated before this method call, so it would be "reg".
2030 // If parameter is small enough to be saved in range [reg, r4), then
2031 // the end (first after last) register would be reg + param-size-in-regs,
2032 // else parameter would be splitted between registers and stack,
2033 // end register would be r4 in this case.
2034 unsigned ByValRegBegin = Reg;
2035 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2036 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2037 // Note, first register is allocated in the beginning of function already,
2038 // allocate remained amount of registers we need.
2039 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2040 State->AllocateReg(GPRArgRegs);
2041 // A byval parameter that is split between registers and memory needs its
2042 // size truncated here.
2043 // In the case where the entire structure fits in registers, we set the
2044 // size in memory to zero.
2045 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002046}
2047
Dale Johannesend679ff72010-06-03 21:09:53 +00002048/// MatchingStackOffset - Return true if the given stack call argument is
2049/// already available in the same position (relatively) of the caller's
2050/// incoming argument stack.
2051static
2052bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2053 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002054 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002055 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2056 int FI = INT_MAX;
2057 if (Arg.getOpcode() == ISD::CopyFromReg) {
2058 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002059 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002060 return false;
2061 MachineInstr *Def = MRI->getVRegDef(VR);
2062 if (!Def)
2063 return false;
2064 if (!Flags.isByVal()) {
2065 if (!TII->isLoadFromStackSlot(Def, FI))
2066 return false;
2067 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002068 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002069 }
2070 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2071 if (Flags.isByVal())
2072 // ByVal argument is passed in as a pointer but it's now being
2073 // dereferenced. e.g.
2074 // define @foo(%struct.X* %A) {
2075 // tail call @bar(%struct.X* byval %A)
2076 // }
2077 return false;
2078 SDValue Ptr = Ld->getBasePtr();
2079 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2080 if (!FINode)
2081 return false;
2082 FI = FINode->getIndex();
2083 } else
2084 return false;
2085
2086 assert(FI != INT_MAX);
2087 if (!MFI->isFixedObjectIndex(FI))
2088 return false;
2089 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2090}
2091
2092/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2093/// for tail call optimization. Targets which want to do tail call
2094/// optimization should implement this function.
2095bool
2096ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2097 CallingConv::ID CalleeCC,
2098 bool isVarArg,
2099 bool isCalleeStructRet,
2100 bool isCallerStructRet,
2101 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002102 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002103 const SmallVectorImpl<ISD::InputArg> &Ins,
2104 SelectionDAG& DAG) const {
Matthias Braun8d414362016-03-30 22:46:04 +00002105 MachineFunction &MF = DAG.getMachineFunction();
2106 const Function *CallerF = MF.getFunction();
Dale Johannesend679ff72010-06-03 21:09:53 +00002107 CallingConv::ID CallerCC = CallerF->getCallingConv();
Manman Ren4865d892016-03-18 23:41:51 +00002108
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002109 assert(Subtarget->supportsTailCall());
2110
Dale Johannesend679ff72010-06-03 21:09:53 +00002111 // Look for obvious safe cases to perform tail call optimization that do not
2112 // require ABI changes. This is what gcc calls sibcall.
2113
Jim Grosbache3864cc2010-06-16 23:45:49 +00002114 // Do not sibcall optimize vararg calls unless the call site is not passing
2115 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002116 if (isVarArg && !Outs.empty())
2117 return false;
2118
Tim Northoverd8407452013-10-01 14:33:28 +00002119 // Exception-handling functions need a special set of instructions to indicate
2120 // a return to the hardware. Tail-calling another function would probably
2121 // break this.
2122 if (CallerF->hasFnAttribute("interrupt"))
2123 return false;
2124
Dale Johannesend679ff72010-06-03 21:09:53 +00002125 // Also avoid sibcall optimization if either caller or callee uses struct
2126 // return semantics.
2127 if (isCalleeStructRet || isCallerStructRet)
2128 return false;
2129
Oliver Stannard12993dd2014-08-18 12:42:15 +00002130 // Externally-defined functions with weak linkage should not be
2131 // tail-called on ARM when the OS does not support dynamic
2132 // pre-emption of symbols, as the AAELF spec requires normal calls
2133 // to undefined weak functions to be replaced with a NOP or jump to the
2134 // next instruction. The behaviour of branch instructions in this
2135 // situation (as used for tail calls) is implementation-defined, so we
2136 // cannot rely on the linker replacing the tail call with a return.
2137 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2138 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002139 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002140 if (GV->hasExternalWeakLinkage() &&
2141 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002142 return false;
2143 }
2144
Matthias Braun8d414362016-03-30 22:46:04 +00002145 // Check that the call results are passed in the same way.
2146 LLVMContext &C = *DAG.getContext();
2147 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2148 CCAssignFnForNode(CalleeCC, true, isVarArg),
2149 CCAssignFnForNode(CallerCC, true, isVarArg)))
2150 return false;
Matthias Braun870c34f2016-04-04 18:56:13 +00002151 // The callee has to preserve all registers the caller needs to preserve.
Matthias Braun707e02c2016-04-13 21:43:25 +00002152 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2153 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
Matthias Braun870c34f2016-04-04 18:56:13 +00002154 if (CalleeCC != CallerCC) {
Matthias Braun707e02c2016-04-13 21:43:25 +00002155 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2156 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
Matthias Braun870c34f2016-04-04 18:56:13 +00002157 return false;
2158 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002159
Manman Ren7e48b252012-10-12 23:39:43 +00002160 // If Caller's vararg or byval argument has been split between registers and
2161 // stack, do not perform tail call, since part of the argument is in caller's
2162 // local frame.
Matthias Braun8d414362016-03-30 22:46:04 +00002163 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002164 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002165 return false;
2166
Dale Johannesend679ff72010-06-03 21:09:53 +00002167 // If the callee takes no arguments then go on to check the results of the
2168 // call.
2169 if (!Outs.empty()) {
2170 // Check if stack adjustment is needed. For now, do not do this if any
2171 // argument is passed on the stack.
2172 SmallVector<CCValAssign, 16> ArgLocs;
Matthias Braun8d414362016-03-30 22:46:04 +00002173 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002174 CCInfo.AnalyzeCallOperands(Outs,
2175 CCAssignFnForNode(CalleeCC, false, isVarArg));
2176 if (CCInfo.getNextStackOffset()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002177 // Check if the arguments are already laid out in the right way as
2178 // the caller's fixed stack objects.
2179 MachineFrameInfo *MFI = MF.getFrameInfo();
2180 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002181 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002182 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2183 i != e;
2184 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002185 CCValAssign &VA = ArgLocs[i];
2186 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002187 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002188 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002189 if (VA.getLocInfo() == CCValAssign::Indirect)
2190 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002191 if (VA.needsCustom()) {
2192 // f64 and vector types are split into multiple registers or
2193 // register/stack-slot combinations. The types will not match
2194 // the registers; give up on memory f64 refs until we figure
2195 // out what to do about this.
2196 if (!VA.isRegLoc())
2197 return false;
2198 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002199 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002200 if (RegVT == MVT::v2f64) {
2201 if (!ArgLocs[++i].isRegLoc())
2202 return false;
2203 if (!ArgLocs[++i].isRegLoc())
2204 return false;
2205 }
2206 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002207 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2208 MFI, MRI, TII))
2209 return false;
2210 }
2211 }
2212 }
Matthias Braun707e02c2016-04-13 21:43:25 +00002213
Matthias Braun46b0f032016-04-14 01:10:42 +00002214 const MachineRegisterInfo &MRI = MF.getRegInfo();
2215 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
2216 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002217 }
2218
2219 return true;
2220}
2221
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002222bool
2223ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2224 MachineFunction &MF, bool isVarArg,
2225 const SmallVectorImpl<ISD::OutputArg> &Outs,
2226 LLVMContext &Context) const {
2227 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002228 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002229 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2230 isVarArg));
2231}
2232
Tim Northoverd8407452013-10-01 14:33:28 +00002233static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002234 const SDLoc &DL, SelectionDAG &DAG) {
Tim Northoverd8407452013-10-01 14:33:28 +00002235 const MachineFunction &MF = DAG.getMachineFunction();
2236 const Function *F = MF.getFunction();
2237
2238 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2239
2240 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2241 // version of the "preferred return address". These offsets affect the return
2242 // instruction if this is a return from PL1 without hypervisor extensions.
2243 // IRQ/FIQ: +4 "subs pc, lr, #4"
2244 // SWI: 0 "subs pc, lr, #0"
2245 // ABORT: +4 "subs pc, lr, #4"
2246 // UNDEF: +4/+2 "subs pc, lr, #0"
2247 // UNDEF varies depending on where the exception came from ARM or Thumb
2248 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2249
2250 int64_t LROffset;
2251 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2252 IntKind == "ABORT")
2253 LROffset = 4;
2254 else if (IntKind == "SWI" || IntKind == "UNDEF")
2255 LROffset = 0;
2256 else
2257 report_fatal_error("Unsupported interrupt attribute. If present, value "
2258 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2259
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002260 RetOps.insert(RetOps.begin() + 1,
2261 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002262
Craig Topper48d114b2014-04-26 18:35:24 +00002263 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002264}
2265
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002266SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002267ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2268 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002269 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002270 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002271 const SDLoc &dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002272
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002273 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002274 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002275
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002276 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002277 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2278 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002279
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002280 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002281 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2282 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002283
Bob Wilsona4c22902009-04-17 19:07:39 +00002284 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002285 SmallVector<SDValue, 4> RetOps;
2286 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002287 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002288
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002289 MachineFunction &MF = DAG.getMachineFunction();
2290 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2291 AFI->setReturnRegsCount(RVLocs.size());
2292
Bob Wilsona4c22902009-04-17 19:07:39 +00002293 // Copy the result values into the output registers.
2294 for (unsigned i = 0, realRVLocIdx = 0;
2295 i != RVLocs.size();
2296 ++i, ++realRVLocIdx) {
2297 CCValAssign &VA = RVLocs[i];
2298 assert(VA.isRegLoc() && "Can only return in registers!");
2299
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002300 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002301
2302 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002303 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002304 case CCValAssign::Full: break;
2305 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002306 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002307 break;
2308 }
2309
Bob Wilsona4c22902009-04-17 19:07:39 +00002310 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002311 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002312 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002313 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002314 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002315 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002316 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002317
Christian Pirkerb5728192014-05-08 14:06:24 +00002318 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2319 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2320 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002321 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002322 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002323 VA = RVLocs[++i]; // skip ahead to next loc
2324 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002325 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2326 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002327 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002328 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002329 VA = RVLocs[++i]; // skip ahead to next loc
2330
2331 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002332 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002333 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002334 }
2335 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2336 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002337 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002338 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002339 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2340 fmrrd.getValue(isLittleEndian ? 0 : 1),
2341 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002342 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002343 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002344 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002345 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2346 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002347 Flag);
2348 } else
2349 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2350
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002351 // Guarantee that all emitted copies are
2352 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002353 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002354 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002355 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002356 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2357 const MCPhysReg *I =
2358 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2359 if (I) {
2360 for (; *I; ++I) {
2361 if (ARM::GPRRegClass.contains(*I))
2362 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2363 else if (ARM::DPRRegClass.contains(*I))
2364 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2365 else
2366 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2367 }
2368 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002369
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002370 // Update chain and glue.
2371 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002372 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002373 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002374
Tim Northoverd8407452013-10-01 14:33:28 +00002375 // CPUs which aren't M-class use a special sequence to return from
2376 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2377 // though we use "subs pc, lr, #N").
2378 //
2379 // M-class CPUs actually use a normal return sequence with a special
2380 // (hardware-provided) value in LR, so the normal code path works.
2381 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2382 !Subtarget->isMClass()) {
2383 if (Subtarget->isThumb1Only())
2384 report_fatal_error("interrupt attribute is not supported in Thumb1");
2385 return LowerInterruptReturn(RetOps, dl, DAG);
2386 }
2387
Craig Topper48d114b2014-04-26 18:35:24 +00002388 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002389}
2390
Evan Chengf8bad082012-04-10 01:51:00 +00002391bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002392 if (N->getNumValues() != 1)
2393 return false;
2394 if (!N->hasNUsesOfValue(1, 0))
2395 return false;
2396
Evan Chengf8bad082012-04-10 01:51:00 +00002397 SDValue TCChain = Chain;
2398 SDNode *Copy = *N->use_begin();
2399 if (Copy->getOpcode() == ISD::CopyToReg) {
2400 // If the copy has a glue operand, we conservatively assume it isn't safe to
2401 // perform a tail call.
2402 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2403 return false;
2404 TCChain = Copy->getOperand(0);
2405 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2406 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002407 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002408 SmallPtrSet<SDNode*, 2> Copies;
2409 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002410 UI != UE; ++UI) {
2411 if (UI->getOpcode() != ISD::CopyToReg)
2412 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002413 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002414 }
Evan Chengf8bad082012-04-10 01:51:00 +00002415 if (Copies.size() > 2)
2416 return false;
2417
2418 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2419 UI != UE; ++UI) {
2420 SDValue UseChain = UI->getOperand(0);
2421 if (Copies.count(UseChain.getNode()))
2422 // Second CopyToReg
2423 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002424 else {
2425 // We are at the top of this chain.
2426 // If the copy has a glue operand, we conservatively assume it
2427 // isn't safe to perform a tail call.
2428 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2429 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002430 // First CopyToReg
2431 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002432 }
Evan Chengf8bad082012-04-10 01:51:00 +00002433 }
2434 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002435 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002436 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002437 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002438 Copy = *Copy->use_begin();
2439 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002440 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002441 // If the copy has a glue operand, we conservatively assume it isn't safe to
2442 // perform a tail call.
2443 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2444 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002445 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002446 } else {
2447 return false;
2448 }
2449
Evan Cheng419ea282010-12-01 22:59:46 +00002450 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002451 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2452 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002453 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2454 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002455 return false;
2456 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002457 }
2458
Evan Chengf8bad082012-04-10 01:51:00 +00002459 if (!HasRet)
2460 return false;
2461
2462 Chain = TCChain;
2463 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002464}
2465
Evan Cheng0663f232011-03-21 01:19:09 +00002466bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002467 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002468 return false;
2469
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002470 auto Attr =
2471 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2472 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002473 return false;
2474
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002475 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002476}
2477
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002478// Trying to write a 64 bit value so need to split into two 32 bit values first,
2479// and pass the lower and high parts through.
2480static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2481 SDLoc DL(Op);
2482 SDValue WriteValue = Op->getOperand(2);
2483
2484 // This function is only supposed to be called for i64 type argument.
2485 assert(WriteValue.getValueType() == MVT::i64
2486 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2487
2488 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2489 DAG.getConstant(0, DL, MVT::i32));
2490 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2491 DAG.getConstant(1, DL, MVT::i32));
2492 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2493 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2494}
2495
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002496// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2497// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2498// one of the above mentioned nodes. It has to be wrapped because otherwise
2499// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2500// be used to form addressing mode. These wrapped nodes will be selected
2501// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002502static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002503 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002504 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002505 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002506 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002507 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002508 if (CP->isMachineConstantPoolEntry())
2509 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2510 CP->getAlignment());
2511 else
2512 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2513 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002514 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002515}
2516
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002517unsigned ARMTargetLowering::getJumpTableEncoding() const {
2518 return MachineJumpTableInfo::EK_Inline;
2519}
2520
Dan Gohman21cea8a2010-04-17 15:26:15 +00002521SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2522 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002523 MachineFunction &MF = DAG.getMachineFunction();
2524 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2525 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002526 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002527 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002528 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002529 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2530 SDValue CPAddr;
2531 if (RelocM == Reloc::Static) {
2532 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2533 } else {
2534 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002535 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002536 ARMConstantPoolValue *CPV =
2537 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2538 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002539 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2540 }
2541 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002542 SDValue Result =
2543 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2544 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2545 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002546 if (RelocM == Reloc::Static)
2547 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002548 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002549 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002550}
2551
Tim Northoverbd41cf82016-01-07 09:03:03 +00002552/// \brief Convert a TLS address reference into the correct sequence of loads
2553/// and calls to compute the variable's address for Darwin, and return an
2554/// SDValue containing the final node.
2555
2556/// Darwin only has one TLS scheme which must be capable of dealing with the
2557/// fully general situation, in the worst case. This means:
2558/// + "extern __thread" declaration.
2559/// + Defined in a possibly unknown dynamic library.
2560///
2561/// The general system is that each __thread variable has a [3 x i32] descriptor
2562/// which contains information used by the runtime to calculate the address. The
2563/// only part of this the compiler needs to know about is the first word, which
2564/// contains a function pointer that must be called with the address of the
2565/// entire descriptor in "r0".
2566///
2567/// Since this descriptor may be in a different unit, in general access must
2568/// proceed along the usual ARM rules. A common sequence to produce is:
2569///
2570/// movw rT1, :lower16:_var$non_lazy_ptr
2571/// movt rT1, :upper16:_var$non_lazy_ptr
2572/// ldr r0, [rT1]
2573/// ldr rT2, [r0]
2574/// blx rT2
2575/// [...address now in r0...]
2576SDValue
2577ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2578 SelectionDAG &DAG) const {
2579 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2580 SDLoc DL(Op);
2581
2582 // First step is to get the address of the actua global symbol. This is where
2583 // the TLS descriptor lives.
2584 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2585
2586 // The first entry in the descriptor is a function pointer that we must call
2587 // to obtain the address of the variable.
2588 SDValue Chain = DAG.getEntryNode();
2589 SDValue FuncTLVGet =
2590 DAG.getLoad(MVT::i32, DL, Chain, DescAddr,
2591 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2592 false, true, true, 4);
2593 Chain = FuncTLVGet.getValue(1);
2594
2595 MachineFunction &F = DAG.getMachineFunction();
2596 MachineFrameInfo *MFI = F.getFrameInfo();
2597 MFI->setAdjustsStack(true);
2598
2599 // TLS calls preserve all registers except those that absolutely must be
2600 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2601 // silly).
2602 auto TRI =
2603 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2604 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2605 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2606
2607 // Finally, we can make the call. This is just a degenerate version of a
2608 // normal AArch64 call node: r0 takes the address of the descriptor, and
2609 // returns the address of the variable in this thread.
2610 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2611 Chain =
2612 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2613 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2614 DAG.getRegisterMask(Mask), Chain.getValue(1));
2615 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2616}
2617
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002618SDValue
2619ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2620 SelectionDAG &DAG) const {
2621 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002622
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002623 SDValue Chain = DAG.getEntryNode();
2624 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2625 SDLoc DL(Op);
2626
2627 // Load the current TEB (thread environment block)
2628 SDValue Ops[] = {Chain,
2629 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2630 DAG.getConstant(15, DL, MVT::i32),
2631 DAG.getConstant(0, DL, MVT::i32),
2632 DAG.getConstant(13, DL, MVT::i32),
2633 DAG.getConstant(0, DL, MVT::i32),
2634 DAG.getConstant(2, DL, MVT::i32)};
2635 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2636 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2637
2638 SDValue TEB = CurrentTEB.getValue(0);
2639 Chain = CurrentTEB.getValue(1);
2640
2641 // Load the ThreadLocalStoragePointer from the TEB
2642 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2643 SDValue TLSArray =
2644 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
2645 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo(),
2646 false, false, false, 0);
2647
2648 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2649 // offset into the TLSArray.
2650
2651 // Load the TLS index from the C runtime
2652 SDValue TLSIndex =
2653 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2654 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
2655 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo(),
2656 false, false, false, 0);
2657
2658 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2659 DAG.getConstant(2, DL, MVT::i32));
2660 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2661 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
2662 MachinePointerInfo(), false, false, false, 0);
2663
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002664 // Get the offset of the start of the .tls section (section base)
2665 const auto *GA = cast<GlobalAddressSDNode>(Op);
2666 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL);
2667 SDValue Offset =
2668 DAG.getLoad(PtrVT, DL, Chain,
2669 DAG.getNode(ARMISD::Wrapper, DL, MVT::i32,
2670 DAG.getTargetConstantPool(CPV, PtrVT, 4)),
2671 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2672 false, false, false, 0);
2673
2674 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset);
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002675}
2676
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002677// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002678SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002679ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002680 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002681 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002682 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002683 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002684 MachineFunction &MF = DAG.getMachineFunction();
2685 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002686 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002687 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002688 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2689 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002690 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002691 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002692 Argument =
2693 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2694 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2695 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002696 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002697
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002698 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002699 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002700
2701 // call __tls_get_addr.
2702 ArgListTy Args;
2703 ArgListEntry Entry;
2704 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002705 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002706 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002707
Dale Johannesen555a3752009-01-30 23:10:59 +00002708 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002709 TargetLowering::CallLoweringInfo CLI(DAG);
2710 CLI.setDebugLoc(dl).setChain(Chain)
2711 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002712 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2713 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002714
Justin Holewinskiaa583972012-05-25 16:35:28 +00002715 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002716 return CallResult.first;
2717}
2718
2719// Lower ISD::GlobalTLSAddress using the "initial exec" or
2720// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002721SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002722ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002723 SelectionDAG &DAG,
2724 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002725 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002726 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002727 SDValue Offset;
2728 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002729 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002730 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002731 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002732
Hans Wennborgaea41202012-05-04 09:40:39 +00002733 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002734 MachineFunction &MF = DAG.getMachineFunction();
2735 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002736 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002737 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002738 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2739 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002740 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2741 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2742 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002743 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002744 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002745 Offset = DAG.getLoad(
2746 PtrVT, dl, Chain, Offset,
2747 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2748 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002749 Chain = Offset.getValue(1);
2750
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002751 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002752 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002753
Alex Lorenze40c8a22015-08-11 23:09:45 +00002754 Offset = DAG.getLoad(
2755 PtrVT, dl, Chain, Offset,
2756 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2757 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002758 } else {
2759 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002760 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002761 ARMConstantPoolValue *CPV =
2762 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002763 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002764 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002765 Offset = DAG.getLoad(
2766 PtrVT, dl, Chain, Offset,
2767 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2768 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002769 }
2770
2771 // The address of the thread local variable is the add of the thread
2772 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002773 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002774}
2775
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002776SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002777ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002778 if (Subtarget->isTargetDarwin())
2779 return LowerGlobalTLSAddressDarwin(Op, DAG);
2780
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002781 if (Subtarget->isTargetWindows())
2782 return LowerGlobalTLSAddressWindows(Op, DAG);
2783
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002784 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002785 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002786 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002787 if (DAG.getTarget().Options.EmulatedTLS)
2788 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002789
2790 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2791
2792 switch (model) {
2793 case TLSModel::GeneralDynamic:
2794 case TLSModel::LocalDynamic:
2795 return LowerToTLSGeneralDynamicModel(GA, DAG);
2796 case TLSModel::InitialExec:
2797 case TLSModel::LocalExec:
2798 return LowerToTLSExecModels(GA, DAG, model);
2799 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002800 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002801}
2802
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002803SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002804 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002805 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002806 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002807 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola7ad97b22016-05-31 15:31:55 +00002808 const TargetMachine &TM = getTargetMachine();
2809 Reloc::Model RM = TM.getRelocationModel();
2810 const Triple &TargetTriple = TM.getTargetTriple();
2811 if (RM == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002812 bool UseGOT_PREL =
Rafael Espindola7ad97b22016-05-31 15:31:55 +00002813 !shouldAssumeDSOLocal(RM, TargetTriple, *GV->getParent(), GV);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002814
2815 MachineFunction &MF = DAG.getMachineFunction();
2816 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2817 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2818 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2819 SDLoc dl(Op);
2820 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2821 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2822 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2823 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2824 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002825 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002826 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002827 SDValue Result = DAG.getLoad(
2828 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2829 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2830 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002831 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002832 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2833 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2834 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002835 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002836 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002837 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002838 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002839 }
2840
2841 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002842 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002843 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002844 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002845 // FIXME: Once remat is capable of dealing with instructions with register
2846 // operands, expand this into two nodes.
2847 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2848 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002849 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002850 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2851 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002852 return DAG.getLoad(
2853 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2854 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2855 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002856 }
2857}
2858
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002859SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002860 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002861 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002862 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002863 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002864 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002865
Eric Christopherc1058df2014-07-04 01:55:26 +00002866 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002867 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002868
Tim Northover72360d22013-12-02 10:35:41 +00002869 // FIXME: Once remat is capable of dealing with instructions with register
2870 // operands, expand this into multiple nodes
2871 unsigned Wrapper =
2872 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002873
Tim Northover72360d22013-12-02 10:35:41 +00002874 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2875 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002876
Evan Cheng1b389522009-09-03 07:04:02 +00002877 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002878 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002879 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2880 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002881 return Result;
2882}
2883
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002884SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2885 SelectionDAG &DAG) const {
2886 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002887 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2888 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002889
2890 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002891 const ARMII::TOF TargetFlags =
2892 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002893 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002894 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002895 SDLoc DL(Op);
2896
2897 ++NumMovwMovt;
2898
2899 // FIXME: Once remat is capable of dealing with instructions with register
2900 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002901 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2902 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002903 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002904 if (GV->hasDLLImportStorageClass())
2905 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002906 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2907 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002908 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002909}
2910
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002911SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002912ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002913 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002914 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002915 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2916 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002917 Op.getOperand(1), Val);
2918}
2919
2920SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002921ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002922 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002923 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002924 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002925}
2926
Matthias Braun3cd00c12015-07-16 22:34:16 +00002927SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2928 SelectionDAG &DAG) const {
2929 SDLoc dl(Op);
2930 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2931 Op.getOperand(0));
2932}
2933
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002934SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002935ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002936 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002937 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002938 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002939 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002940 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002941 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002942 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002943 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002944 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002945 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00002946 case Intrinsic::thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002947 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002948 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2949 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002950 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002951 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002952 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002953 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002954 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002955 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2956 SDValue CPAddr;
2957 unsigned PCAdj = (RelocM != Reloc::PIC_)
2958 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002959 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002960 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2961 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002962 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002963 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002964 SDValue Result = DAG.getLoad(
2965 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2966 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2967 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002968
2969 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002970 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002971 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2972 }
2973 return Result;
2974 }
Evan Cheng18381b42011-03-29 23:06:19 +00002975 case Intrinsic::arm_neon_vmulls:
2976 case Intrinsic::arm_neon_vmullu: {
2977 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2978 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002979 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002980 Op.getOperand(1), Op.getOperand(2));
2981 }
James Molloyee868b22015-08-11 12:06:25 +00002982 case Intrinsic::arm_neon_vminnm:
2983 case Intrinsic::arm_neon_vmaxnm: {
2984 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2985 ? ISD::FMINNUM : ISD::FMAXNUM;
2986 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2987 Op.getOperand(1), Op.getOperand(2));
2988 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002989 case Intrinsic::arm_neon_vminu:
2990 case Intrinsic::arm_neon_vmaxu: {
2991 if (Op.getValueType().isFloatingPoint())
2992 return SDValue();
2993 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2994 ? ISD::UMIN : ISD::UMAX;
2995 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2996 Op.getOperand(1), Op.getOperand(2));
2997 }
James Molloyd616c642015-08-11 12:06:28 +00002998 case Intrinsic::arm_neon_vmins:
2999 case Intrinsic::arm_neon_vmaxs: {
3000 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00003001 if (!Op.getValueType().isFloatingPoint()) {
3002 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3003 ? ISD::SMIN : ISD::SMAX;
3004 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3005 Op.getOperand(1), Op.getOperand(2));
3006 }
James Molloyd616c642015-08-11 12:06:28 +00003007 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3008 ? ISD::FMINNAN : ISD::FMAXNAN;
3009 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3010 Op.getOperand(1), Op.getOperand(2));
3011 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003012 }
3013}
3014
Eli Friedman30a49e92011-08-03 21:06:02 +00003015static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3016 const ARMSubtarget *Subtarget) {
3017 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003018 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003019 if (!Subtarget->hasDataBarrier()) {
3020 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3021 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3022 // here.
3023 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003024 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003025 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003026 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003027 }
3028
Tim Northover36b24172013-07-03 09:20:36 +00003029 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3030 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003031 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003032 if (Subtarget->isMClass()) {
3033 // Only a full system barrier exists in the M-class architectures.
3034 Domain = ARM_MB::SY;
JF Bastien800f87a2016-04-06 21:19:33 +00003035 } else if (Subtarget->isSwift() && Ord == AtomicOrdering::Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003036 // Swift happens to implement ISHST barriers in a way that's compatible with
3037 // Release semantics but weaker than ISH so we'd be fools not to use
3038 // it. Beware: other processors probably don't!
3039 Domain = ARM_MB::ISHST;
3040 }
3041
Joey Gouly926d3f52013-09-05 15:35:24 +00003042 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003043 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3044 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003045}
3046
Evan Cheng8740ee32010-11-03 06:34:55 +00003047static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3048 const ARMSubtarget *Subtarget) {
3049 // ARM pre v5TE and Thumb1 does not have preload instructions.
3050 if (!(Subtarget->isThumb2() ||
3051 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3052 // Just preserve the chain.
3053 return Op.getOperand(0);
3054
Andrew Trickef9de2a2013-05-25 02:42:55 +00003055 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003056 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3057 if (!isRead &&
3058 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3059 // ARMv7 with MP extension has PLDW.
3060 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003061
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003062 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3063 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003064 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003065 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003066 isData = ~isData & 1;
3067 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003068
3069 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003070 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3071 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003072}
3073
Dan Gohman31ae5862010-04-17 14:41:14 +00003074static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3075 MachineFunction &MF = DAG.getMachineFunction();
3076 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3077
Evan Cheng10043e22007-01-19 07:51:42 +00003078 // vastart just stores the address of the VarArgsFrameIndex slot into the
3079 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003080 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003081 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003082 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003083 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003084 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3085 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00003086}
3087
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003088SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA,
3089 CCValAssign &NextVA,
3090 SDValue &Root,
3091 SelectionDAG &DAG,
3092 const SDLoc &dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003093 MachineFunction &MF = DAG.getMachineFunction();
3094 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3095
Craig Topper760b1342012-02-22 05:59:10 +00003096 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003097 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003098 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003099 else
Craig Topperc7242e02012-04-20 07:30:17 +00003100 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003101
3102 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003103 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003104 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003105
3106 SDValue ArgValue2;
3107 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003108 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00003109 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003110
3111 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003112 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003113 ArgValue2 = DAG.getLoad(
3114 MVT::i32, dl, Root, FIN,
3115 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
3116 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00003117 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003118 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003119 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003120 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003121 if (!Subtarget->isLittle())
3122 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003123 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003124}
3125
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003126// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003127// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003128// byval). Either way, we allocate stack slots adjacent to the data
3129// provided by our caller, and store the unallocated registers there.
3130// If this is a variadic function, the va_list pointer will begin with
3131// these values; otherwise, this reassembles a (byval) structure that
3132// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003133// Return: The frame index registers were stored into.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003134int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
3135 const SDLoc &dl, SDValue &Chain,
3136 const Value *OrigArg,
3137 unsigned InRegsParamRecordIdx,
3138 int ArgOffset, unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003139 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003140 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003141 // Setup first unallocated register as first byval register;
3142 // eat all remained registers
3143 // (these two actions are performed by HandleByVal method).
3144 // Then, here, we initialize stack frame with
3145 // "store-reg" instructions.
3146 // Case #2. Var-args function, that doesn't contain byval parameters.
3147 // The same: eat all remained unallocated registers,
3148 // initialize stack frame.
3149
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003150 MachineFunction &MF = DAG.getMachineFunction();
3151 MachineFrameInfo *MFI = MF.getFrameInfo();
3152 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003153 unsigned RBegin, REnd;
3154 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3155 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003156 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003157 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003158 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003159 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003160 }
3161
Tim Northover8cda34f2015-03-11 18:54:22 +00003162 if (REnd != RBegin)
3163 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003164
Mehdi Amini44ede332015-07-09 02:09:04 +00003165 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003166 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003167 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003168
Tim Northover8cda34f2015-03-11 18:54:22 +00003169 SmallVector<SDValue, 4> MemOps;
3170 const TargetRegisterClass *RC =
3171 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003172
Tim Northover8cda34f2015-03-11 18:54:22 +00003173 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3174 unsigned VReg = MF.addLiveIn(Reg, RC);
3175 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3176 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003177 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003178 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3179 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003180 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003181 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003182
3183 if (!MemOps.empty())
3184 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3185 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003186}
3187
3188// Setup stack frame, the va_list pointer will start from.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003189void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
3190 const SDLoc &dl, SDValue &Chain,
3191 unsigned ArgOffset,
3192 unsigned TotalArgRegsSaveSize,
3193 bool ForceMutable) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003194 MachineFunction &MF = DAG.getMachineFunction();
3195 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3196
3197 // Try to store any remaining integer argument regs
3198 // to their spots on the stack so that they may be loaded by deferencing
3199 // the result of va_next.
3200 // If there is no regs to be stored, just point address after last
3201 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003202 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3203 CCInfo.getInRegsParamsCount(),
3204 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003205 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003206}
3207
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003208SDValue ARMTargetLowering::LowerFormalArguments(
3209 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3210 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3211 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003212 MachineFunction &MF = DAG.getMachineFunction();
3213 MachineFrameInfo *MFI = MF.getFrameInfo();
3214
Bob Wilsona4c22902009-04-17 19:07:39 +00003215 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3216
3217 // Assign locations to all of the incoming arguments.
3218 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003219 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3220 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003221 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003222 CCAssignFnForNode(CallConv, /* Return*/ false,
3223 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003224
Bob Wilsona4c22902009-04-17 19:07:39 +00003225 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003226 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003227 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3228 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003229
3230 // Initially ArgRegsSaveSize is zero.
3231 // Then we increase this value each time we meet byval parameter.
3232 // We also increase this value in case of varargs function.
3233 AFI->setArgRegsSaveSize(0);
3234
Oliver Stannardd55e1152014-03-05 15:25:27 +00003235 // Calculate the amount of stack space that we need to allocate to store
3236 // byval and variadic arguments that are passed in registers.
3237 // We need to know this before we allocate the first byval or variadic
3238 // argument, as they will be allocated a stack slot below the CFA (Canonical
3239 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003240 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003241 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003242 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3243 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003244
Tim Northover8cda34f2015-03-11 18:54:22 +00003245 CCValAssign &VA = ArgLocs[i];
3246 unsigned Index = VA.getValNo();
3247 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3248 if (!Flags.isByVal())
3249 continue;
3250
3251 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3252 unsigned RBegin, REnd;
3253 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3254 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3255
3256 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003257 }
3258 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003259
3260 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003261 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003262 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3263 if (RegIdx != array_lengthof(GPRArgRegs))
3264 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003265 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003266
3267 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3268 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003269 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003270
Bob Wilsona4c22902009-04-17 19:07:39 +00003271 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3272 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003273 if (Ins[VA.getValNo()].isOrigArg()) {
3274 std::advance(CurOrigArg,
3275 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3276 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3277 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003278 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003279 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003280 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003281
Bob Wilsona4c22902009-04-17 19:07:39 +00003282 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003283 // f64 and vector types are split up into multiple registers or
3284 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003285 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003286 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003287 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003288 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003289 SDValue ArgValue2;
3290 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003291 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003292 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003293 ArgValue2 = DAG.getLoad(
3294 MVT::f64, dl, Chain, FIN,
3295 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3296 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003297 } else {
3298 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3299 Chain, DAG, dl);
3300 }
Owen Anderson9f944592009-08-11 20:47:22 +00003301 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3302 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003303 ArgValue, ArgValue1,
3304 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003305 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003306 ArgValue, ArgValue2,
3307 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003308 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003309 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003310
Bob Wilson2e076c42009-06-22 23:27:02 +00003311 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003312 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003313
Owen Anderson9f944592009-08-11 20:47:22 +00003314 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003315 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003316 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003317 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003318 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003319 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003320 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003321 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3322 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003323 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003324 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003325
3326 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003327 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003328 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003329 }
3330
3331 // If this is an 8 or 16-bit value, it is really passed promoted
3332 // to 32 bits. Insert an assert[sz]ext to capture this, then
3333 // truncate to the right size.
3334 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003335 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003336 case CCValAssign::Full: break;
3337 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003338 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003339 break;
3340 case CCValAssign::SExt:
3341 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3342 DAG.getValueType(VA.getValVT()));
3343 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3344 break;
3345 case CCValAssign::ZExt:
3346 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3347 DAG.getValueType(VA.getValVT()));
3348 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3349 break;
3350 }
3351
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003352 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003353
3354 } else { // VA.isRegLoc()
3355
3356 // sanity check
3357 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003358 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003359
Andrew Trick05938a52015-02-16 18:10:47 +00003360 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003361
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003362 // Some Ins[] entries become multiple ArgLoc[] entries.
3363 // Process them only once.
3364 if (index != lastInsIndex)
3365 {
3366 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003367 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003368 // This can be changed with more analysis.
3369 // In case of tail call optimization mark all arguments mutable.
3370 // Since they could be overwritten by lowering of arguments in case of
3371 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003372 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003373 assert(Ins[index].isOrigArg() &&
3374 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003375 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003376
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003377 int FrameIndex = StoreByValRegs(
3378 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3379 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003380 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003381 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003382 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003383 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003384 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003385 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003386
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003387 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003388 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003389 InVals.push_back(DAG.getLoad(
3390 VA.getValVT(), dl, Chain, FIN,
3391 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3392 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003393 }
3394 lastInsIndex = index;
3395 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003396 }
3397 }
3398
3399 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003400 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003401 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003402 CCInfo.getNextStackOffset(),
3403 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003404
Oliver Stannardb14c6252014-04-02 16:10:33 +00003405 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3406
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003407 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003408}
3409
3410/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003411static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003412 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003413 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003414 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003415 // Maybe this has already been legalized into the constant pool?
3416 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003417 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003418 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003419 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003420 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003421 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003422 } else if (Op->getOpcode() == ISD::BITCAST &&
3423 Op->getValueType(0) == MVT::f64) {
3424 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3425 // created by LowerConstantFP().
3426 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003427 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3428 isNullConstant(BitcastOp->getOperand(0)))
3429 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003430 }
3431 return false;
3432}
3433
Evan Cheng10043e22007-01-19 07:51:42 +00003434/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3435/// the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003436SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3437 SDValue &ARMcc, SelectionDAG &DAG,
3438 const SDLoc &dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003439 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003440 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003441 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003442 // Constant does not fit, try adjusting it by one?
3443 switch (CC) {
3444 default: break;
3445 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003446 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003447 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003448 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003449 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003450 }
3451 break;
3452 case ISD::SETULT:
3453 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003454 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003455 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003456 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003457 }
3458 break;
3459 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003460 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003461 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003462 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003463 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003464 }
3465 break;
3466 case ISD::SETULE:
3467 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003468 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003469 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003470 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003471 }
3472 break;
3473 }
3474 }
3475 }
3476
3477 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003478 ARMISD::NodeType CompareType;
3479 switch (CondCode) {
3480 default:
3481 CompareType = ARMISD::CMP;
3482 break;
3483 case ARMCC::EQ:
3484 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003485 // Uses only Z Flag
3486 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003487 break;
3488 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003489 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003490 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003491}
3492
3493/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003494SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS,
3495 SelectionDAG &DAG, const SDLoc &dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003496 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003497 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003498 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003499 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003500 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003501 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3502 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003503}
3504
Bob Wilson45acbd02011-03-08 01:17:20 +00003505/// duplicateCmp - Glue values can have only one use, so this function
3506/// duplicates a comparison node.
3507SDValue
3508ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3509 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003510 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003511 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3512 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3513
3514 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3515 Cmp = Cmp.getOperand(0);
3516 Opc = Cmp.getOpcode();
3517 if (Opc == ARMISD::CMPFP)
3518 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3519 else {
3520 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3521 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3522 }
3523 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3524}
3525
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003526std::pair<SDValue, SDValue>
3527ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3528 SDValue &ARMcc) const {
3529 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3530
3531 SDValue Value, OverflowCmp;
3532 SDValue LHS = Op.getOperand(0);
3533 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003534 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003535
3536 // FIXME: We are currently always generating CMPs because we don't support
3537 // generating CMN through the backend. This is not as good as the natural
3538 // CMP case because it causes a register dependency and cannot be folded
3539 // later.
3540
3541 switch (Op.getOpcode()) {
3542 default:
3543 llvm_unreachable("Unknown overflow instruction!");
3544 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003545 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3546 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3547 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003548 break;
3549 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003550 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3551 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3552 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003553 break;
3554 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003555 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3556 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3557 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003558 break;
3559 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003560 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3561 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3562 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003563 break;
3564 } // switch (...)
3565
3566 return std::make_pair(Value, OverflowCmp);
3567}
3568
3569
3570SDValue
3571ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3572 // Let legalize expand this if it isn't a legal type yet.
3573 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3574 return SDValue();
3575
3576 SDValue Value, OverflowCmp;
3577 SDValue ARMcc;
3578 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3579 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003580 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003581 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003582 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3583 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003584 EVT VT = Op.getValueType();
3585
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003586 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003587 ARMcc, CCR, OverflowCmp);
3588
3589 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003590 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003591}
3592
3593
Bill Wendling6a981312010-08-11 08:43:16 +00003594SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3595 SDValue Cond = Op.getOperand(0);
3596 SDValue SelectTrue = Op.getOperand(1);
3597 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003598 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003599 unsigned Opc = Cond.getOpcode();
3600
3601 if (Cond.getResNo() == 1 &&
3602 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3603 Opc == ISD::USUBO)) {
3604 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3605 return SDValue();
3606
3607 SDValue Value, OverflowCmp;
3608 SDValue ARMcc;
3609 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3610 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3611 EVT VT = Op.getValueType();
3612
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003613 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003614 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003615 }
Bill Wendling6a981312010-08-11 08:43:16 +00003616
3617 // Convert:
3618 //
3619 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3620 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3621 //
3622 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3623 const ConstantSDNode *CMOVTrue =
3624 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3625 const ConstantSDNode *CMOVFalse =
3626 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3627
3628 if (CMOVTrue && CMOVFalse) {
3629 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3630 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3631
3632 SDValue True;
3633 SDValue False;
3634 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3635 True = SelectTrue;
3636 False = SelectFalse;
3637 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3638 True = SelectFalse;
3639 False = SelectTrue;
3640 }
3641
3642 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003643 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003644 SDValue ARMcc = Cond.getOperand(2);
3645 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003646 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003647 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003648 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003649 }
3650 }
3651 }
3652
Dan Gohmand4a77c42012-02-24 00:09:36 +00003653 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3654 // undefined bits before doing a full-word comparison with zero.
3655 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003656 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003657
Bill Wendling6a981312010-08-11 08:43:16 +00003658 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003659 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003660 SelectTrue, SelectFalse, ISD::SETNE);
3661}
3662
Joey Gouly881eab52013-08-22 15:29:11 +00003663static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3664 bool &swpCmpOps, bool &swpVselOps) {
3665 // Start by selecting the GE condition code for opcodes that return true for
3666 // 'equality'
3667 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3668 CC == ISD::SETULE)
3669 CondCode = ARMCC::GE;
3670
3671 // and GT for opcodes that return false for 'equality'.
3672 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3673 CC == ISD::SETULT)
3674 CondCode = ARMCC::GT;
3675
3676 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3677 // to swap the compare operands.
3678 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3679 CC == ISD::SETULT)
3680 swpCmpOps = true;
3681
3682 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3683 // If we have an unordered opcode, we need to swap the operands to the VSEL
3684 // instruction (effectively negating the condition).
3685 //
3686 // This also has the effect of swapping which one of 'less' or 'greater'
3687 // returns true, so we also swap the compare operands. It also switches
3688 // whether we return true for 'equality', so we compensate by picking the
3689 // opposite condition code to our original choice.
3690 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3691 CC == ISD::SETUGT) {
3692 swpCmpOps = !swpCmpOps;
3693 swpVselOps = !swpVselOps;
3694 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3695 }
3696
3697 // 'ordered' is 'anything but unordered', so use the VS condition code and
3698 // swap the VSEL operands.
3699 if (CC == ISD::SETO) {
3700 CondCode = ARMCC::VS;
3701 swpVselOps = true;
3702 }
3703
3704 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3705 // code and swap the VSEL operands.
3706 if (CC == ISD::SETUNE) {
3707 CondCode = ARMCC::EQ;
3708 swpVselOps = true;
3709 }
3710}
3711
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003712SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003713 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3714 SDValue Cmp, SelectionDAG &DAG) const {
3715 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3716 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3717 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3718 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3719 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3720
3721 SDValue TrueLow = TrueVal.getValue(0);
3722 SDValue TrueHigh = TrueVal.getValue(1);
3723 SDValue FalseLow = FalseVal.getValue(0);
3724 SDValue FalseHigh = FalseVal.getValue(1);
3725
3726 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3727 ARMcc, CCR, Cmp);
3728 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3729 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3730
3731 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3732 } else {
3733 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3734 Cmp);
3735 }
3736}
3737
Dan Gohman21cea8a2010-04-17 15:26:15 +00003738SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003739 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003740 SDValue LHS = Op.getOperand(0);
3741 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003742 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003743 SDValue TrueVal = Op.getOperand(2);
3744 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003745 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003746
Oliver Stannard51b1d462014-08-21 12:50:31 +00003747 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3748 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3749 dl);
3750
3751 // If softenSetCCOperands only returned one value, we should compare it to
3752 // zero.
3753 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003754 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003755 CC = ISD::SETNE;
3756 }
3757 }
3758
Owen Anderson9f944592009-08-11 20:47:22 +00003759 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003760 // Try to generate VSEL on ARMv8.
3761 // The VSEL instruction can't use all the usual ARM condition
3762 // codes: it only has two bits to select the condition code, so it's
3763 // constrained to use only GE, GT, VS and EQ.
3764 //
3765 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3766 // swap the operands of the previous compare instruction (effectively
3767 // inverting the compare condition, swapping 'less' and 'greater') and
3768 // sometimes need to swap the operands to the VSEL (which inverts the
3769 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003770 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3771 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003772 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3773 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3774 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003775 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003776 std::swap(TrueVal, FalseVal);
3777 }
3778 }
3779
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003780 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003781 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003782 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003783 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003784 }
3785
3786 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003787 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003788
Scott Douglass7ad77922015-04-08 17:18:28 +00003789 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003790 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3791 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003792 bool swpCmpOps = false;
3793 bool swpVselOps = false;
3794 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3795
3796 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3797 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3798 if (swpCmpOps)
3799 std::swap(LHS, RHS);
3800 if (swpVselOps)
3801 std::swap(TrueVal, FalseVal);
3802 }
3803 }
3804
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003805 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003806 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003808 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003809 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003810 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003811 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003812 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003813 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003814 }
3815 return Result;
3816}
3817
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003818/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3819/// to morph to an integer compare sequence.
3820static bool canChangeToInt(SDValue Op, bool &SeenZero,
3821 const ARMSubtarget *Subtarget) {
3822 SDNode *N = Op.getNode();
3823 if (!N->hasOneUse())
3824 // Otherwise it requires moving the value from fp to integer registers.
3825 return false;
3826 if (!N->getNumValues())
3827 return false;
3828 EVT VT = Op.getValueType();
3829 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3830 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3831 // vmrs are very slow, e.g. cortex-a8.
3832 return false;
3833
3834 if (isFloatingPointZero(Op)) {
3835 SeenZero = true;
3836 return true;
3837 }
3838 return ISD::isNormalLoad(N);
3839}
3840
3841static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3842 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003843 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003844
3845 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003846 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003847 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003848 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003849 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003850
3851 llvm_unreachable("Unknown VFP cmp argument!");
3852}
3853
3854static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3855 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003856 SDLoc dl(Op);
3857
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003858 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003859 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3860 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003861 return;
3862 }
3863
3864 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3865 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003866 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003867 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003868 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003869 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003870 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003871
3872 EVT PtrType = Ptr.getValueType();
3873 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003874 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3875 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3876 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003877 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003878 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003879 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003880 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003881 return;
3882 }
3883
3884 llvm_unreachable("Unknown VFP cmp argument!");
3885}
3886
3887/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3888/// f32 and even f64 comparisons to integer ones.
3889SDValue
3890ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3891 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003892 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003893 SDValue LHS = Op.getOperand(2);
3894 SDValue RHS = Op.getOperand(3);
3895 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003896 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003897
Evan Chengd12af5d2012-03-01 23:27:13 +00003898 bool LHSSeenZero = false;
3899 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3900 bool RHSSeenZero = false;
3901 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3902 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003903 // If unsafe fp math optimization is enabled and there are no other uses of
3904 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003905 // to an integer comparison.
3906 if (CC == ISD::SETOEQ)
3907 CC = ISD::SETEQ;
3908 else if (CC == ISD::SETUNE)
3909 CC = ISD::SETNE;
3910
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003911 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003912 SDValue ARMcc;
3913 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003914 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3915 bitcastf32Toi32(LHS, DAG), Mask);
3916 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3917 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003918 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3919 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3920 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3921 Chain, Dest, ARMcc, CCR, Cmp);
3922 }
3923
3924 SDValue LHS1, LHS2;
3925 SDValue RHS1, RHS2;
3926 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3927 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003928 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3929 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003930 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003931 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003932 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003933 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003934 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003935 }
3936
3937 return SDValue();
3938}
3939
3940SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3941 SDValue Chain = Op.getOperand(0);
3942 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3943 SDValue LHS = Op.getOperand(2);
3944 SDValue RHS = Op.getOperand(3);
3945 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003946 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003947
Oliver Stannard51b1d462014-08-21 12:50:31 +00003948 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3949 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3950 dl);
3951
3952 // If softenSetCCOperands only returned one value, we should compare it to
3953 // zero.
3954 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003955 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003956 CC = ISD::SETNE;
3957 }
3958 }
3959
Owen Anderson9f944592009-08-11 20:47:22 +00003960 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003961 SDValue ARMcc;
3962 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003963 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003964 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003965 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003966 }
3967
Owen Anderson9f944592009-08-11 20:47:22 +00003968 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003969
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003970 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003971 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3972 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003973 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003974 return Result;
3975 }
3976
Evan Cheng10043e22007-01-19 07:51:42 +00003977 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003978 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003979
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003980 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003981 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003982 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003983 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003984 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003985 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003986 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003987 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003988 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003989 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003990 }
3991 return Res;
3992}
3993
Dan Gohman21cea8a2010-04-17 15:26:15 +00003994SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003995 SDValue Chain = Op.getOperand(0);
3996 SDValue Table = Op.getOperand(1);
3997 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003998 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003999
Mehdi Amini44ede332015-07-09 02:09:04 +00004000 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00004001 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004002 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00004003 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004004 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00004005 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004006 if (Subtarget->isThumb2()) {
4007 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4008 // which does another jump to the destination. This also makes it easier
4009 // to translate it to TBB / TBH later.
4010 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004011 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004012 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004013 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00004014 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004015 Addr =
4016 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
4017 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4018 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004019 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004020 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004021 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004022 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004023 Addr =
4024 DAG.getLoad(PTy, dl, Chain, Addr,
4025 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4026 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004027 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004028 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004029 }
Evan Cheng10043e22007-01-19 07:51:42 +00004030}
4031
Eli Friedman2d4055b2011-11-09 23:36:02 +00004032static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004033 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004034 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004035
James Molloy547d4c02012-02-20 09:24:05 +00004036 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4037 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4038 return Op;
4039 return DAG.UnrollVectorOp(Op.getNode());
4040 }
4041
4042 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4043 "Invalid type for custom lowering!");
4044 if (VT != MVT::v4i16)
4045 return DAG.UnrollVectorOp(Op.getNode());
4046
4047 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4048 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004049}
4050
Oliver Stannard51b1d462014-08-21 12:50:31 +00004051SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004052 EVT VT = Op.getValueType();
4053 if (VT.isVector())
4054 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004055 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4056 RTLIB::Libcall LC;
4057 if (Op.getOpcode() == ISD::FP_TO_SINT)
4058 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4059 Op.getValueType());
4060 else
4061 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4062 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004063 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004064 /*isSigned*/ false, SDLoc(Op)).first;
4065 }
4066
James Molloyfa041152015-03-23 16:15:16 +00004067 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004068}
4069
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004070static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4071 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004072 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004073
Eli Friedman2d4055b2011-11-09 23:36:02 +00004074 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4075 if (VT.getVectorElementType() == MVT::f32)
4076 return Op;
4077 return DAG.UnrollVectorOp(Op.getNode());
4078 }
4079
Duncan Sandsa41634e2011-08-12 14:54:45 +00004080 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4081 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004082 if (VT != MVT::v4f32)
4083 return DAG.UnrollVectorOp(Op.getNode());
4084
4085 unsigned CastOpc;
4086 unsigned Opc;
4087 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004088 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004089 case ISD::SINT_TO_FP:
4090 CastOpc = ISD::SIGN_EXTEND;
4091 Opc = ISD::SINT_TO_FP;
4092 break;
4093 case ISD::UINT_TO_FP:
4094 CastOpc = ISD::ZERO_EXTEND;
4095 Opc = ISD::UINT_TO_FP;
4096 break;
4097 }
4098
4099 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4100 return DAG.getNode(Opc, dl, VT, Op);
4101}
4102
Oliver Stannard51b1d462014-08-21 12:50:31 +00004103SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004104 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004105 if (VT.isVector())
4106 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004107 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4108 RTLIB::Libcall LC;
4109 if (Op.getOpcode() == ISD::SINT_TO_FP)
4110 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4111 Op.getValueType());
4112 else
4113 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4114 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004115 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004116 /*isSigned*/ false, SDLoc(Op)).first;
4117 }
4118
James Molloyfa041152015-03-23 16:15:16 +00004119 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004120}
4121
Evan Cheng25f93642010-07-08 02:08:50 +00004122SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004123 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004124 SDValue Tmp0 = Op.getOperand(0);
4125 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004126 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004127 EVT VT = Op.getValueType();
4128 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004129 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4130 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4131 bool UseNEON = !InGPR && Subtarget->hasNEON();
4132
4133 if (UseNEON) {
4134 // Use VBSL to copy the sign bit.
4135 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4136 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004137 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004138 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4139 if (VT == MVT::f64)
4140 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4141 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004142 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004143 else /*if (VT == MVT::f32)*/
4144 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4145 if (SrcVT == MVT::f32) {
4146 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4147 if (VT == MVT::f64)
4148 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4149 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004150 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004151 } else if (VT == MVT::f32)
4152 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4153 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004154 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004155 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4156 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4157
4158 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004159 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004160 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4161 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4162 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004163
Evan Chengd6b641e2011-02-23 02:24:55 +00004164 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4165 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4166 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004167 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004168 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4169 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004170 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004171 } else {
4172 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4173 }
4174
4175 return Res;
4176 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004177
4178 // Bitcast operand 1 to i32.
4179 if (SrcVT == MVT::f64)
4180 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004181 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004182 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4183
Evan Chengd6b641e2011-02-23 02:24:55 +00004184 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004185 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4186 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004187 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4188 if (VT == MVT::f32) {
4189 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4190 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4191 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4192 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004193 }
4194
Evan Chengd6b641e2011-02-23 02:24:55 +00004195 // f64: Or the high part with signbit and then combine two parts.
4196 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004197 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004198 SDValue Lo = Tmp0.getValue(0);
4199 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4200 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4201 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004202}
4203
Evan Cheng168ced92010-05-22 01:47:14 +00004204SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4205 MachineFunction &MF = DAG.getMachineFunction();
4206 MachineFrameInfo *MFI = MF.getFrameInfo();
4207 MFI->setReturnAddressIsTaken(true);
4208
Bill Wendling908bf812014-01-06 00:43:20 +00004209 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004210 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004211
Evan Cheng168ced92010-05-22 01:47:14 +00004212 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004213 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004214 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4215 if (Depth) {
4216 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004217 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004218 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4219 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004220 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004221 }
4222
4223 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004224 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004225 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4226}
4227
Dan Gohman21cea8a2010-04-17 15:26:15 +00004228SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004229 const ARMBaseRegisterInfo &ARI =
4230 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4231 MachineFunction &MF = DAG.getMachineFunction();
4232 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004233 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004234
Owen Anderson53aa7a92009-08-10 22:56:29 +00004235 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004237 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004238 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004239 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4240 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004241 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4242 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004243 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004244 return FrameAddr;
4245}
4246
Renato Golinc7aea402014-05-06 16:51:25 +00004247// FIXME? Maybe this could be a TableGen attribute on some registers and
4248// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004249unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4250 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004251 unsigned Reg = StringSwitch<unsigned>(RegName)
4252 .Case("sp", ARM::SP)
4253 .Default(0);
4254 if (Reg)
4255 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004256 report_fatal_error(Twine("Invalid register name \""
4257 + StringRef(RegName) + "\"."));
4258}
4259
4260// Result is 64 bit value so split into two 32 bit values and return as a
4261// pair of values.
4262static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4263 SelectionDAG &DAG) {
4264 SDLoc DL(N);
4265
4266 // This function is only supposed to be called for i64 type destination.
4267 assert(N->getValueType(0) == MVT::i64
4268 && "ExpandREAD_REGISTER called for non-i64 type result.");
4269
4270 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4271 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4272 N->getOperand(0),
4273 N->getOperand(1));
4274
4275 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4276 Read.getValue(1)));
4277 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004278}
4279
Quentin Colombet901f0362015-12-04 01:53:14 +00004280/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4281/// When \p DstVT, the destination type of \p BC, is on the vector
4282/// register bank and the source of bitcast, \p Op, operates on the same bank,
4283/// it might be possible to combine them, such that everything stays on the
4284/// vector register bank.
4285/// \p return The node that would replace \p BT, if the combine
4286/// is possible.
4287static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4288 SelectionDAG &DAG) {
4289 SDValue Op = BC->getOperand(0);
4290 EVT DstVT = BC->getValueType(0);
4291
4292 // The only vector instruction that can produce a scalar (remember,
4293 // since the bitcast was about to be turned into VMOVDRR, the source
4294 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4295 // Moreover, we can do this combine only if there is one use.
4296 // Finally, if the destination type is not a vector, there is not
4297 // much point on forcing everything on the vector bank.
4298 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4299 !Op.hasOneUse())
4300 return SDValue();
4301
4302 // If the index is not constant, we will introduce an additional
4303 // multiply that will stick.
4304 // Give up in that case.
4305 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4306 if (!Index)
4307 return SDValue();
4308 unsigned DstNumElt = DstVT.getVectorNumElements();
4309
4310 // Compute the new index.
4311 const APInt &APIntIndex = Index->getAPIntValue();
4312 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4313 NewIndex *= APIntIndex;
4314 // Check if the new constant index fits into i32.
4315 if (NewIndex.getBitWidth() > 32)
4316 return SDValue();
4317
4318 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4319 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4320 SDLoc dl(Op);
4321 SDValue ExtractSrc = Op.getOperand(0);
4322 EVT VecVT = EVT::getVectorVT(
4323 *DAG.getContext(), DstVT.getScalarType(),
4324 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4325 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4326 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4327 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4328}
4329
Wesley Peck527da1b2010-11-23 03:31:01 +00004330/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004331/// expand a bit convert where either the source or destination type is i64 to
4332/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4333/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4334/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004335static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004337 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004338 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004339
Bob Wilson59b70ea2010-04-17 05:30:19 +00004340 // This function is only supposed to be called for i64 types, either as the
4341 // source or destination of the bit convert.
4342 EVT SrcVT = Op.getValueType();
4343 EVT DstVT = N->getValueType(0);
4344 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004345 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004346
Bob Wilson59b70ea2010-04-17 05:30:19 +00004347 // Turn i64->f64 into VMOVDRR.
4348 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004349 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4350 // if we can combine the bitcast with its source.
4351 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4352 return Val;
4353
Owen Anderson9f944592009-08-11 20:47:22 +00004354 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004355 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004356 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004357 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004358 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004359 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004360 }
Bob Wilson7117a912009-03-20 22:42:55 +00004361
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004362 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004363 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004364 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004365 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004366 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004367 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4368 DAG.getVTList(MVT::i32, MVT::i32),
4369 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4370 else
4371 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4372 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004373 // Merge the pieces into a single i64 value.
4374 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4375 }
Bob Wilson7117a912009-03-20 22:42:55 +00004376
Bob Wilson59b70ea2010-04-17 05:30:19 +00004377 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004378}
4379
Bob Wilson2e076c42009-06-22 23:27:02 +00004380/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004381/// Zero vectors are used to represent vector negation and in those cases
4382/// will be implemented with the NEON VNEG instruction. However, VNEG does
4383/// not support i64 elements, so sometimes the zero vectors will need to be
4384/// explicitly constructed. Regardless, use a canonical VMOV to create the
4385/// zero vector.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004386static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004387 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004388 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004389 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004390 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4391 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004392 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004393}
4394
Jim Grosbach624fcb22009-10-31 21:00:56 +00004395/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4396/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004397SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4398 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004399 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4400 EVT VT = Op.getValueType();
4401 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004402 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004403 SDValue ShOpLo = Op.getOperand(0);
4404 SDValue ShOpHi = Op.getOperand(1);
4405 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004406 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004407 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004408
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004409 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4410
Jim Grosbach624fcb22009-10-31 21:00:56 +00004411 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004412 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004413 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4414 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004415 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004416 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4417 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004418 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004419
4420 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004421 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4422 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004423 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004424 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004425 CCR, Cmp);
4426
4427 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004428 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004429}
4430
Jim Grosbach5d994042009-10-31 19:38:01 +00004431/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4432/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004433SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4434 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004435 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4436 EVT VT = Op.getValueType();
4437 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004438 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004439 SDValue ShOpLo = Op.getOperand(0);
4440 SDValue ShOpHi = Op.getOperand(1);
4441 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004442 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004443
4444 assert(Op.getOpcode() == ISD::SHL_PARTS);
4445 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004446 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004447 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4448 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004449 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004450 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4451 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4452
4453 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4454 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004455 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4456 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004457 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004458 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004459 CCR, Cmp);
4460
4461 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004462 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004463}
4464
Jim Grosbach535d3b42010-09-08 03:54:02 +00004465SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004466 SelectionDAG &DAG) const {
4467 // The rounding mode is in bits 23:22 of the FPSCR.
4468 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4469 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4470 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004471 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004472 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004473 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004474 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004475 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004476 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004477 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004478 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004479 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004480 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004481}
4482
Jim Grosbach8546ec92010-01-18 19:58:49 +00004483static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4484 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004485 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004486 EVT VT = N->getValueType(0);
4487 if (VT.isVector()) {
4488 assert(ST->hasNEON());
4489
4490 // Compute the least significant set bit: LSB = X & -X
4491 SDValue X = N->getOperand(0);
4492 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4493 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4494
4495 EVT ElemTy = VT.getVectorElementType();
4496
4497 if (ElemTy == MVT::i8) {
4498 // Compute with: cttz(x) = ctpop(lsb - 1)
4499 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4500 DAG.getTargetConstant(1, dl, ElemTy));
4501 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4502 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4503 }
4504
4505 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4506 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4507 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4508 unsigned NumBits = ElemTy.getSizeInBits();
4509 SDValue WidthMinus1 =
4510 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4511 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4512 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4513 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4514 }
4515
4516 // Compute with: cttz(x) = ctpop(lsb - 1)
4517
4518 // Since we can only compute the number of bits in a byte with vcnt.8, we
4519 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4520 // and i64.
4521
4522 // Compute LSB - 1.
4523 SDValue Bits;
4524 if (ElemTy == MVT::i64) {
4525 // Load constant 0xffff'ffff'ffff'ffff to register.
4526 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4527 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4528 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4529 } else {
4530 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4531 DAG.getTargetConstant(1, dl, ElemTy));
4532 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4533 }
4534
4535 // Count #bits with vcnt.8.
4536 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4537 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4538 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4539
4540 // Gather the #bits with vpaddl (pairwise add.)
4541 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4542 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4543 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4544 Cnt8);
4545 if (ElemTy == MVT::i16)
4546 return Cnt16;
4547
4548 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4549 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4550 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4551 Cnt16);
4552 if (ElemTy == MVT::i32)
4553 return Cnt32;
4554
4555 assert(ElemTy == MVT::i64);
4556 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4557 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4558 Cnt32);
4559 return Cnt64;
4560 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004561
4562 if (!ST->hasV6T2Ops())
4563 return SDValue();
4564
James Molloyb5640982015-11-13 16:05:22 +00004565 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004566 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4567}
4568
Evan Chengb4eae132012-12-04 22:41:50 +00004569/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4570/// for each 16-bit element from operand, repeated. The basic idea is to
4571/// leverage vcnt to get the 8-bit counts, gather and add the results.
4572///
4573/// Trace for v4i16:
4574/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4575/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4576/// 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 +00004577/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004578/// [b0 b1 b2 b3 b4 b5 b6 b7]
4579/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4580/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4581/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4582static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4583 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004584 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004585
4586 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4587 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4588 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4589 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4590 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4591 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4592}
4593
4594/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4595/// bit-count for each 16-bit element from the operand. We need slightly
4596/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4597/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004598///
Evan Chengb4eae132012-12-04 22:41:50 +00004599/// Trace for v4i16:
4600/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4601/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4602/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4603/// v4i16:Extracted = [k0 k1 k2 k3 ]
4604static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4605 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004606 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004607
4608 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4609 if (VT.is64BitVector()) {
4610 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4611 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004612 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004613 } else {
4614 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004615 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004616 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4617 }
4618}
4619
4620/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4621/// bit-count for each 32-bit element from the operand. The idea here is
4622/// to split the vector into 16-bit elements, leverage the 16-bit count
4623/// routine, and then combine the results.
4624///
4625/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4626/// input = [v0 v1 ] (vi: 32-bit elements)
4627/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4628/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004629/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004630/// [k0 k1 k2 k3 ]
4631/// N1 =+[k1 k0 k3 k2 ]
4632/// [k0 k2 k1 k3 ]
4633/// N2 =+[k1 k3 k0 k2 ]
4634/// [k0 k2 k1 k3 ]
4635/// Extended =+[k1 k3 k0 k2 ]
4636/// [k0 k2 ]
4637/// Extracted=+[k1 k3 ]
4638///
4639static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4640 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004641 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004642
4643 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4644
4645 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4646 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4647 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4648 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4649 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4650
4651 if (VT.is64BitVector()) {
4652 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4653 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004654 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004655 } else {
4656 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004657 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004658 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4659 }
4660}
4661
4662static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4663 const ARMSubtarget *ST) {
4664 EVT VT = N->getValueType(0);
4665
4666 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004667 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4668 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004669 "Unexpected type for custom ctpop lowering");
4670
4671 if (VT.getVectorElementType() == MVT::i32)
4672 return lowerCTPOP32BitElements(N, DAG);
4673 else
4674 return lowerCTPOP16BitElements(N, DAG);
4675}
4676
Bob Wilson2e076c42009-06-22 23:27:02 +00004677static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4678 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004679 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004680 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004681
Bob Wilson7d471332010-11-18 21:16:28 +00004682 if (!VT.isVector())
4683 return SDValue();
4684
Bob Wilson2e076c42009-06-22 23:27:02 +00004685 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004686 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004687
Bob Wilson7d471332010-11-18 21:16:28 +00004688 // Left shifts translate directly to the vshiftu intrinsic.
4689 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004690 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004691 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4692 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004693 N->getOperand(0), N->getOperand(1));
4694
4695 assert((N->getOpcode() == ISD::SRA ||
4696 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4697
4698 // NEON uses the same intrinsics for both left and right shifts. For
4699 // right shifts, the shift amounts are negative, so negate the vector of
4700 // shift amounts.
4701 EVT ShiftVT = N->getOperand(1).getValueType();
4702 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4703 getZeroVector(ShiftVT, DAG, dl),
4704 N->getOperand(1));
4705 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4706 Intrinsic::arm_neon_vshifts :
4707 Intrinsic::arm_neon_vshiftu);
4708 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004709 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004710 N->getOperand(0), NegatedCount);
4711}
4712
4713static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4714 const ARMSubtarget *ST) {
4715 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004716 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004717
Eli Friedman682d8c12009-08-22 03:13:10 +00004718 // We can get here for a node like i32 = ISD::SHL i32, i64
4719 if (VT != MVT::i64)
4720 return SDValue();
4721
4722 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004723 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004724
Chris Lattnerf81d5882007-11-24 07:07:01 +00004725 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004726 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004727 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004728
Chris Lattnerf81d5882007-11-24 07:07:01 +00004729 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004730 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004731
Chris Lattnerf81d5882007-11-24 07:07:01 +00004732 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004733 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004734 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004735 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004736 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004737
Chris Lattnerf81d5882007-11-24 07:07:01 +00004738 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4739 // captures the result into a carry flag.
4740 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004741 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004742
Chris Lattnerf81d5882007-11-24 07:07:01 +00004743 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004744 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004745
Chris Lattnerf81d5882007-11-24 07:07:01 +00004746 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004747 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004748}
4749
Bob Wilson2e076c42009-06-22 23:27:02 +00004750static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4751 SDValue TmpOp0, TmpOp1;
4752 bool Invert = false;
4753 bool Swap = false;
4754 unsigned Opc = 0;
4755
4756 SDValue Op0 = Op.getOperand(0);
4757 SDValue Op1 = Op.getOperand(1);
4758 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004759 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004760 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004761 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004762 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004763
James Molloybf170092015-08-20 16:33:44 +00004764 if (CmpVT.getVectorElementType() == MVT::i64)
4765 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4766 // but it's possible that our operands are 64-bit but our result is 32-bit.
4767 // Bail in this case.
4768 return SDValue();
4769
Oliver Stannard51b1d462014-08-21 12:50:31 +00004770 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004771 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004772 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004773 case ISD::SETUNE:
4774 case ISD::SETNE: Invert = true; // Fallthrough
4775 case ISD::SETOEQ:
4776 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4777 case ISD::SETOLT:
4778 case ISD::SETLT: Swap = true; // Fallthrough
4779 case ISD::SETOGT:
4780 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4781 case ISD::SETOLE:
4782 case ISD::SETLE: Swap = true; // Fallthrough
4783 case ISD::SETOGE:
4784 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4785 case ISD::SETUGE: Swap = true; // Fallthrough
4786 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4787 case ISD::SETUGT: Swap = true; // Fallthrough
4788 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4789 case ISD::SETUEQ: Invert = true; // Fallthrough
4790 case ISD::SETONE:
4791 // Expand this to (OLT | OGT).
4792 TmpOp0 = Op0;
4793 TmpOp1 = Op1;
4794 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004795 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4796 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004797 break;
4798 case ISD::SETUO: Invert = true; // Fallthrough
4799 case ISD::SETO:
4800 // Expand this to (OLT | OGE).
4801 TmpOp0 = Op0;
4802 TmpOp1 = Op1;
4803 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004804 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4805 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004806 break;
4807 }
4808 } else {
4809 // Integer comparisons.
4810 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004811 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004812 case ISD::SETNE: Invert = true;
4813 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4814 case ISD::SETLT: Swap = true;
4815 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4816 case ISD::SETLE: Swap = true;
4817 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4818 case ISD::SETULT: Swap = true;
4819 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4820 case ISD::SETULE: Swap = true;
4821 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4822 }
4823
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004824 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004825 if (Opc == ARMISD::VCEQ) {
4826
4827 SDValue AndOp;
4828 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4829 AndOp = Op0;
4830 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4831 AndOp = Op1;
4832
4833 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004834 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004835 AndOp = AndOp.getOperand(0);
4836
4837 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4838 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004839 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4840 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004841 Invert = !Invert;
4842 }
4843 }
4844 }
4845
4846 if (Swap)
4847 std::swap(Op0, Op1);
4848
Owen Andersonc7baee32010-11-08 23:21:22 +00004849 // If one of the operands is a constant vector zero, attempt to fold the
4850 // comparison to a specialized compare-against-zero form.
4851 SDValue SingleOp;
4852 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4853 SingleOp = Op0;
4854 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4855 if (Opc == ARMISD::VCGE)
4856 Opc = ARMISD::VCLEZ;
4857 else if (Opc == ARMISD::VCGT)
4858 Opc = ARMISD::VCLTZ;
4859 SingleOp = Op1;
4860 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004861
Owen Andersonc7baee32010-11-08 23:21:22 +00004862 SDValue Result;
4863 if (SingleOp.getNode()) {
4864 switch (Opc) {
4865 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004866 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004867 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004868 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004869 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004870 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004871 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004872 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004873 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004874 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004875 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004876 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004877 }
4878 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004879 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004880 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004881
Tim Northover45aa89c2015-02-08 00:50:47 +00004882 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4883
Bob Wilson2e076c42009-06-22 23:27:02 +00004884 if (Invert)
4885 Result = DAG.getNOT(dl, Result, VT);
4886
4887 return Result;
4888}
4889
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00004890static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) {
4891 SDValue LHS = Op.getOperand(0);
4892 SDValue RHS = Op.getOperand(1);
4893 SDValue Carry = Op.getOperand(2);
4894 SDValue Cond = Op.getOperand(3);
4895 SDLoc DL(Op);
4896
4897 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only.");
4898
4899 assert(Carry.getOpcode() != ISD::CARRY_FALSE);
4900 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
4901 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
4902
4903 SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
4904 SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
4905 SDValue ARMcc = DAG.getConstant(
4906 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
4907 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4908 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
4909 Cmp.getValue(1), SDValue());
4910 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
4911 CCR, Chain.getValue(1));
4912}
4913
Bob Wilson5b2b5042010-06-14 22:19:57 +00004914/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4915/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004916/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004917static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4918 unsigned SplatBitSize, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004919 const SDLoc &dl, EVT &VT, bool is128Bits,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004920 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004921 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004922
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004923 // SplatBitSize is set to the smallest size that splats the vector, so a
4924 // zero vector will always have SplatBitSize == 8. However, NEON modified
4925 // immediate instructions others than VMOV do not support the 8-bit encoding
4926 // of a zero vector, and the default encoding of zero is supposed to be the
4927 // 32-bit version.
4928 if (SplatBits == 0)
4929 SplatBitSize = 32;
4930
Bob Wilson2e076c42009-06-22 23:27:02 +00004931 switch (SplatBitSize) {
4932 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004933 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004934 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004935 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004936 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004937 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004938 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004939 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004940 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004941
4942 case 16:
4943 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004944 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004945 if ((SplatBits & ~0xff) == 0) {
4946 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004947 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004948 Imm = SplatBits;
4949 break;
4950 }
4951 if ((SplatBits & ~0xff00) == 0) {
4952 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004953 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004954 Imm = SplatBits >> 8;
4955 break;
4956 }
4957 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004958
4959 case 32:
4960 // NEON's 32-bit VMOV supports splat values where:
4961 // * only one byte is nonzero, or
4962 // * the least significant byte is 0xff and the second byte is nonzero, or
4963 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004964 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004965 if ((SplatBits & ~0xff) == 0) {
4966 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004967 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004968 Imm = SplatBits;
4969 break;
4970 }
4971 if ((SplatBits & ~0xff00) == 0) {
4972 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004973 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004974 Imm = SplatBits >> 8;
4975 break;
4976 }
4977 if ((SplatBits & ~0xff0000) == 0) {
4978 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004979 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004980 Imm = SplatBits >> 16;
4981 break;
4982 }
4983 if ((SplatBits & ~0xff000000) == 0) {
4984 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004985 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004986 Imm = SplatBits >> 24;
4987 break;
4988 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004989
Owen Andersona4076922010-11-05 21:57:54 +00004990 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4991 if (type == OtherModImm) return SDValue();
4992
Bob Wilson2e076c42009-06-22 23:27:02 +00004993 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004994 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4995 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004996 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004997 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004998 break;
4999 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005000
5001 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005002 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
5003 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005004 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00005005 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005006 break;
5007 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005008
5009 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
5010 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
5011 // VMOV.I32. A (very) minor optimization would be to replicate the value
5012 // and fall through here to test for a valid 64-bit splat. But, then the
5013 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00005014 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005015
5016 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00005017 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005018 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005019 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00005020 uint64_t BitMask = 0xff;
5021 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005022 unsigned ImmMask = 1;
5023 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005024 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005025 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005026 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005027 Imm |= ImmMask;
5028 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005029 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005030 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005031 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005032 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005033 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005034
Mehdi Aminiffc14022015-07-08 01:00:38 +00005035 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005036 // swap higher and lower 32 bit word
5037 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5038
Bob Wilson6eae5202010-06-11 21:34:50 +00005039 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005040 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005041 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005042 break;
5043 }
5044
Bob Wilson6eae5202010-06-11 21:34:50 +00005045 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005046 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005047 }
5048
Bob Wilsona3f19012010-07-13 21:16:48 +00005049 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005050 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005051}
5052
Lang Hames591cdaf2012-03-29 21:56:11 +00005053SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5054 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005055 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005056 return SDValue();
5057
Tim Northoverf79c3a52013-08-20 08:57:11 +00005058 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005059 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005060
Oliver Stannard51b1d462014-08-21 12:50:31 +00005061 // Use the default (constant pool) lowering for double constants when we have
5062 // an SP-only FPU
5063 if (IsDouble && Subtarget->isFPOnlySP())
5064 return SDValue();
5065
Lang Hames591cdaf2012-03-29 21:56:11 +00005066 // Try splatting with a VMOV.f32...
Benjamin Kramer46e38f32016-06-08 10:01:20 +00005067 const APFloat &FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005068 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5069
Lang Hames591cdaf2012-03-29 21:56:11 +00005070 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005071 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5072 // We have code in place to select a valid ConstantFP already, no need to
5073 // do any mangling.
5074 return Op;
5075 }
5076
5077 // It's a float and we are trying to use NEON operations where
5078 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005079 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005080 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005081 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5082 NewVal);
5083 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005084 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005085 }
5086
Tim Northoverf79c3a52013-08-20 08:57:11 +00005087 // The rest of our options are NEON only, make sure that's allowed before
5088 // proceeding..
5089 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5090 return SDValue();
5091
Lang Hames591cdaf2012-03-29 21:56:11 +00005092 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005093 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5094
5095 // It wouldn't really be worth bothering for doubles except for one very
5096 // important value, which does happen to match: 0.0. So make sure we don't do
5097 // anything stupid.
5098 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5099 return SDValue();
5100
5101 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005102 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5103 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005104 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005105 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005106 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5107 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005108 if (IsDouble)
5109 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5110
5111 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005112 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5113 VecConstant);
5114 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005115 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005116 }
5117
5118 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005119 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005120 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005121 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005122 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005123 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005124
5125 if (IsDouble)
5126 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5127
5128 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005129 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5130 VecConstant);
5131 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005132 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005133 }
5134
5135 return SDValue();
5136}
5137
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005138// check if an VEXT instruction can handle the shuffle mask when the
5139// vector sources of the shuffle are the same.
5140static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5141 unsigned NumElts = VT.getVectorNumElements();
5142
5143 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5144 if (M[0] < 0)
5145 return false;
5146
5147 Imm = M[0];
5148
5149 // If this is a VEXT shuffle, the immediate value is the index of the first
5150 // element. The other shuffle indices must be the successive elements after
5151 // the first one.
5152 unsigned ExpectedElt = Imm;
5153 for (unsigned i = 1; i < NumElts; ++i) {
5154 // Increment the expected index. If it wraps around, just follow it
5155 // back to index zero and keep going.
5156 ++ExpectedElt;
5157 if (ExpectedElt == NumElts)
5158 ExpectedElt = 0;
5159
5160 if (M[i] < 0) continue; // ignore UNDEF indices
5161 if (ExpectedElt != static_cast<unsigned>(M[i]))
5162 return false;
5163 }
5164
5165 return true;
5166}
5167
Lang Hames591cdaf2012-03-29 21:56:11 +00005168
Benjamin Kramer339ced42012-01-15 13:16:05 +00005169static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005170 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005171 unsigned NumElts = VT.getVectorNumElements();
5172 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005173
5174 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5175 if (M[0] < 0)
5176 return false;
5177
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005178 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005179
5180 // If this is a VEXT shuffle, the immediate value is the index of the first
5181 // element. The other shuffle indices must be the successive elements after
5182 // the first one.
5183 unsigned ExpectedElt = Imm;
5184 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005185 // Increment the expected index. If it wraps around, it may still be
5186 // a VEXT but the source vectors must be swapped.
5187 ExpectedElt += 1;
5188 if (ExpectedElt == NumElts * 2) {
5189 ExpectedElt = 0;
5190 ReverseVEXT = true;
5191 }
5192
Bob Wilson411dfad2010-08-17 05:54:34 +00005193 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005194 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005195 return false;
5196 }
5197
5198 // Adjust the index value if the source operands will be swapped.
5199 if (ReverseVEXT)
5200 Imm -= NumElts;
5201
Bob Wilson32cd8552009-08-19 17:03:43 +00005202 return true;
5203}
5204
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005205/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5206/// instruction with the specified blocksize. (The order of the elements
5207/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005208static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005209 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5210 "Only possible block sizes for VREV are: 16, 32, 64");
5211
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005212 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005213 if (EltSz == 64)
5214 return false;
5215
5216 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005217 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005218 // If the first shuffle index is UNDEF, be optimistic.
5219 if (M[0] < 0)
5220 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005221
5222 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5223 return false;
5224
5225 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005226 if (M[i] < 0) continue; // ignore UNDEF indices
5227 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005228 return false;
5229 }
5230
5231 return true;
5232}
5233
Benjamin Kramer339ced42012-01-15 13:16:05 +00005234static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005235 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5236 // range, then 0 is placed into the resulting vector. So pretty much any mask
5237 // of 8 elements can work here.
5238 return VT == MVT::v8i8 && M.size() == 8;
5239}
5240
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005241// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5242// checking that pairs of elements in the shuffle mask represent the same index
5243// in each vector, incrementing the expected index by 2 at each step.
5244// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5245// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5246// v2={e,f,g,h}
5247// WhichResult gives the offset for each element in the mask based on which
5248// of the two results it belongs to.
5249//
5250// The transpose can be represented either as:
5251// result1 = shufflevector v1, v2, result1_shuffle_mask
5252// result2 = shufflevector v1, v2, result2_shuffle_mask
5253// where v1/v2 and the shuffle masks have the same number of elements
5254// (here WhichResult (see below) indicates which result is being checked)
5255//
5256// or as:
5257// results = shufflevector v1, v2, shuffle_mask
5258// where both results are returned in one vector and the shuffle mask has twice
5259// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5260// want to check the low half and high half of the shuffle mask as if it were
5261// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005262static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005263 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5264 if (EltSz == 64)
5265 return false;
5266
Bob Wilsona7062312009-08-21 20:54:19 +00005267 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005268 if (M.size() != NumElts && M.size() != NumElts*2)
5269 return false;
5270
James Molloy8c995a92015-09-10 08:42:28 +00005271 // If the mask is twice as long as the input vector then we need to check the
5272 // upper and lower parts of the mask with a matching value for WhichResult
5273 // FIXME: A mask with only even values will be rejected in case the first
5274 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5275 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005276 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005277 if (M.size() == NumElts * 2)
5278 WhichResult = i / NumElts;
5279 else
5280 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005281 for (unsigned j = 0; j < NumElts; j += 2) {
5282 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5283 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5284 return false;
5285 }
Bob Wilsona7062312009-08-21 20:54:19 +00005286 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005287
5288 if (M.size() == NumElts*2)
5289 WhichResult = 0;
5290
Bob Wilsona7062312009-08-21 20:54:19 +00005291 return true;
5292}
5293
Bob Wilson0bbd3072009-12-03 06:40:55 +00005294/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5295/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5296/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005297static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005298 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5299 if (EltSz == 64)
5300 return false;
5301
5302 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005303 if (M.size() != NumElts && M.size() != NumElts*2)
5304 return false;
5305
5306 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005307 if (M.size() == NumElts * 2)
5308 WhichResult = i / NumElts;
5309 else
5310 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005311 for (unsigned j = 0; j < NumElts; j += 2) {
5312 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5313 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5314 return false;
5315 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005316 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005317
5318 if (M.size() == NumElts*2)
5319 WhichResult = 0;
5320
Bob Wilson0bbd3072009-12-03 06:40:55 +00005321 return true;
5322}
5323
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005324// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5325// that the mask elements are either all even and in steps of size 2 or all odd
5326// and in steps of size 2.
5327// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5328// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5329// v2={e,f,g,h}
5330// Requires similar checks to that of isVTRNMask with
5331// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005332static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005333 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5334 if (EltSz == 64)
5335 return false;
5336
Bob Wilsona7062312009-08-21 20:54:19 +00005337 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005338 if (M.size() != NumElts && M.size() != NumElts*2)
5339 return false;
5340
5341 for (unsigned i = 0; i < M.size(); i += NumElts) {
5342 WhichResult = M[i] == 0 ? 0 : 1;
5343 for (unsigned j = 0; j < NumElts; ++j) {
5344 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5345 return false;
5346 }
Bob Wilsona7062312009-08-21 20:54:19 +00005347 }
5348
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005349 if (M.size() == NumElts*2)
5350 WhichResult = 0;
5351
Bob Wilsona7062312009-08-21 20:54:19 +00005352 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005353 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005354 return false;
5355
5356 return true;
5357}
5358
Bob Wilson0bbd3072009-12-03 06:40:55 +00005359/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5360/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5361/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005362static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005363 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5364 if (EltSz == 64)
5365 return false;
5366
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005367 unsigned NumElts = VT.getVectorNumElements();
5368 if (M.size() != NumElts && M.size() != NumElts*2)
5369 return false;
5370
5371 unsigned Half = NumElts / 2;
5372 for (unsigned i = 0; i < M.size(); i += NumElts) {
5373 WhichResult = M[i] == 0 ? 0 : 1;
5374 for (unsigned j = 0; j < NumElts; j += Half) {
5375 unsigned Idx = WhichResult;
5376 for (unsigned k = 0; k < Half; ++k) {
5377 int MIdx = M[i + j + k];
5378 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5379 return false;
5380 Idx += 2;
5381 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005382 }
5383 }
5384
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005385 if (M.size() == NumElts*2)
5386 WhichResult = 0;
5387
Bob Wilson0bbd3072009-12-03 06:40:55 +00005388 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5389 if (VT.is64BitVector() && EltSz == 32)
5390 return false;
5391
5392 return true;
5393}
5394
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005395// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5396// that pairs of elements of the shufflemask represent the same index in each
5397// vector incrementing sequentially through the vectors.
5398// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5399// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5400// v2={e,f,g,h}
5401// Requires similar checks to that of isVTRNMask with respect the how results
5402// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005403static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005404 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5405 if (EltSz == 64)
5406 return false;
5407
Bob Wilsona7062312009-08-21 20:54:19 +00005408 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005409 if (M.size() != NumElts && M.size() != NumElts*2)
5410 return false;
5411
5412 for (unsigned i = 0; i < M.size(); i += NumElts) {
5413 WhichResult = M[i] == 0 ? 0 : 1;
5414 unsigned Idx = WhichResult * NumElts / 2;
5415 for (unsigned j = 0; j < NumElts; j += 2) {
5416 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5417 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5418 return false;
5419 Idx += 1;
5420 }
Bob Wilsona7062312009-08-21 20:54:19 +00005421 }
5422
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005423 if (M.size() == NumElts*2)
5424 WhichResult = 0;
5425
Bob Wilsona7062312009-08-21 20:54:19 +00005426 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005427 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005428 return false;
5429
5430 return true;
5431}
5432
Bob Wilson0bbd3072009-12-03 06:40:55 +00005433/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5434/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5435/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005436static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005437 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5438 if (EltSz == 64)
5439 return false;
5440
5441 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005442 if (M.size() != NumElts && M.size() != NumElts*2)
5443 return false;
5444
5445 for (unsigned i = 0; i < M.size(); i += NumElts) {
5446 WhichResult = M[i] == 0 ? 0 : 1;
5447 unsigned Idx = WhichResult * NumElts / 2;
5448 for (unsigned j = 0; j < NumElts; j += 2) {
5449 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5450 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5451 return false;
5452 Idx += 1;
5453 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005454 }
5455
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005456 if (M.size() == NumElts*2)
5457 WhichResult = 0;
5458
Bob Wilson0bbd3072009-12-03 06:40:55 +00005459 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5460 if (VT.is64BitVector() && EltSz == 32)
5461 return false;
5462
5463 return true;
5464}
5465
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005466/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5467/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5468static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5469 unsigned &WhichResult,
5470 bool &isV_UNDEF) {
5471 isV_UNDEF = false;
5472 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5473 return ARMISD::VTRN;
5474 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5475 return ARMISD::VUZP;
5476 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5477 return ARMISD::VZIP;
5478
5479 isV_UNDEF = true;
5480 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5481 return ARMISD::VTRN;
5482 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5483 return ARMISD::VUZP;
5484 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5485 return ARMISD::VZIP;
5486
5487 return 0;
5488}
5489
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005490/// \return true if this is a reverse operation on an vector.
5491static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5492 unsigned NumElts = VT.getVectorNumElements();
5493 // Make sure the mask has the right size.
5494 if (NumElts != M.size())
5495 return false;
5496
5497 // Look for <15, ..., 3, -1, 1, 0>.
5498 for (unsigned i = 0; i != NumElts; ++i)
5499 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5500 return false;
5501
5502 return true;
5503}
5504
Dale Johannesen2bff5052010-07-29 20:10:08 +00005505// If N is an integer constant that can be moved into a register in one
5506// instruction, return an SDValue of such a constant (will become a MOV
5507// instruction). Otherwise return null.
5508static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005509 const ARMSubtarget *ST, const SDLoc &dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005510 uint64_t Val;
5511 if (!isa<ConstantSDNode>(N))
5512 return SDValue();
5513 Val = cast<ConstantSDNode>(N)->getZExtValue();
5514
5515 if (ST->isThumb1Only()) {
5516 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005517 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005518 } else {
5519 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005520 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005521 }
5522 return SDValue();
5523}
5524
Bob Wilson2e076c42009-06-22 23:27:02 +00005525// If this is a case we can't handle, return null and let the default
5526// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005527SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5528 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005529 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005530 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005531 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005532
5533 APInt SplatBits, SplatUndef;
5534 unsigned SplatBitSize;
5535 bool HasAnyUndefs;
5536 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005537 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005538 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005539 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005540 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005541 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005542 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005543 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005544 if (Val.getNode()) {
5545 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005546 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005547 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005548
5549 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005550 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005551 Val = isNEONModifiedImm(NegatedImm,
5552 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005553 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005554 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005555 if (Val.getNode()) {
5556 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005557 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005558 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005559
5560 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005561 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005562 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005563 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005564 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005565 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5566 }
5567 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005568 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005569 }
5570
Bob Wilson91fdf682010-05-22 00:23:12 +00005571 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005572 //
5573 // As an optimisation, even if more than one value is used it may be more
5574 // profitable to splat with one value then change some lanes.
5575 //
5576 // Heuristically we decide to do this if the vector has a "dominant" value,
5577 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005578 unsigned NumElts = VT.getVectorNumElements();
5579 bool isOnlyLowElement = true;
5580 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005581 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005582 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005583
5584 // Map of the number of times a particular SDValue appears in the
5585 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005586 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005587 SDValue Value;
5588 for (unsigned i = 0; i < NumElts; ++i) {
5589 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005590 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00005591 continue;
5592 if (i > 0)
5593 isOnlyLowElement = false;
5594 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5595 isConstant = false;
5596
James Molloy49bdbce2012-09-06 09:55:02 +00005597 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005598 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005599
James Molloy49bdbce2012-09-06 09:55:02 +00005600 // Is this value dominant? (takes up more than half of the lanes)
5601 if (++Count > (NumElts / 2)) {
5602 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005603 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005604 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005605 }
James Molloy49bdbce2012-09-06 09:55:02 +00005606 if (ValueCounts.size() != 1)
5607 usesOnlyOneValue = false;
5608 if (!Value.getNode() && ValueCounts.size() > 0)
5609 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005610
James Molloy49bdbce2012-09-06 09:55:02 +00005611 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005612 return DAG.getUNDEF(VT);
5613
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005614 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5615 // Keep going if we are hitting this case.
5616 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005617 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5618
Dale Johannesen2bff5052010-07-29 20:10:08 +00005619 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5620
Dale Johannesen710a2d92010-10-19 20:00:17 +00005621 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5622 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005623 if (hasDominantValue && EltSize <= 32) {
5624 if (!isConstant) {
5625 SDValue N;
5626
5627 // If we are VDUPing a value that comes directly from a vector, that will
5628 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005629 // just use VDUPLANE. We can only do this if the lane being extracted
5630 // is at a constant index, as the VDUP from lane instructions only have
5631 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005632 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005633 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005634 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005635 // We need to create a new undef vector to use for the VDUPLANE if the
5636 // size of the vector from which we get the value is different than the
5637 // size of the vector that we need to create. We will insert the element
5638 // such that the register coalescer will remove unnecessary copies.
5639 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005640 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5641 VT.getVectorNumElements();
5642 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5643 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005644 Value, DAG.getConstant(index, dl, MVT::i32)),
5645 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005646 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005647 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005648 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005649 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005650 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5651
5652 if (!usesOnlyOneValue) {
5653 // The dominant value was splatted as 'N', but we now have to insert
5654 // all differing elements.
5655 for (unsigned I = 0; I < NumElts; ++I) {
5656 if (Op.getOperand(I) == Value)
5657 continue;
5658 SmallVector<SDValue, 3> Ops;
5659 Ops.push_back(N);
5660 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005661 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005662 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005663 }
5664 }
5665 return N;
5666 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005667 if (VT.getVectorElementType().isFloatingPoint()) {
5668 SmallVector<SDValue, 8> Ops;
5669 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005670 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005671 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005672 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00005673 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005674 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5675 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005676 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005677 }
James Molloy49bdbce2012-09-06 09:55:02 +00005678 if (usesOnlyOneValue) {
5679 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5680 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005681 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005682 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005683 }
5684
5685 // If all elements are constants and the case above didn't get hit, fall back
5686 // to the default expansion, which will generate a load from the constant
5687 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005688 if (isConstant)
5689 return SDValue();
5690
Bob Wilson6f2b8962011-01-07 21:37:30 +00005691 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5692 if (NumElts >= 4) {
5693 SDValue shuffle = ReconstructShuffle(Op, DAG);
5694 if (shuffle != SDValue())
5695 return shuffle;
5696 }
5697
Bob Wilson91fdf682010-05-22 00:23:12 +00005698 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005699 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5700 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005701 if (EltSize >= 32) {
5702 // Do the expansion with floating-point types, since that is what the VFP
5703 // registers are defined to use, and since i64 is not legal.
5704 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5705 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005706 SmallVector<SDValue, 8> Ops;
5707 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005708 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005709 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005710 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005711 }
5712
Jim Grosbach24e102a2013-07-08 18:18:52 +00005713 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5714 // know the default expansion would otherwise fall back on something even
5715 // worse. For a vector with one or two non-undef values, that's
5716 // scalar_to_vector for the elements followed by a shuffle (provided the
5717 // shuffle is valid for the target) and materialization element by element
5718 // on the stack followed by a load for everything else.
5719 if (!isConstant && !usesOnlyOneValue) {
5720 SDValue Vec = DAG.getUNDEF(VT);
5721 for (unsigned i = 0 ; i < NumElts; ++i) {
5722 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005723 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00005724 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005725 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005726 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5727 }
5728 return Vec;
5729 }
5730
Bob Wilson2e076c42009-06-22 23:27:02 +00005731 return SDValue();
5732}
5733
Bob Wilson6f2b8962011-01-07 21:37:30 +00005734// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005735// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005736SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5737 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005738 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005739 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005740 EVT VT = Op.getValueType();
5741 unsigned NumElts = VT.getVectorNumElements();
5742
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005743 struct ShuffleSourceInfo {
5744 SDValue Vec;
5745 unsigned MinElt;
5746 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005747
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005748 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5749 // be compatible with the shuffle we intend to construct. As a result
5750 // ShuffleVec will be some sliding window into the original Vec.
5751 SDValue ShuffleVec;
5752
5753 // Code should guarantee that element i in Vec starts at element "WindowBase
5754 // + i * WindowScale in ShuffleVec".
5755 int WindowBase;
5756 int WindowScale;
5757
5758 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5759 ShuffleSourceInfo(SDValue Vec)
5760 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5761 WindowScale(1) {}
5762 };
5763
5764 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5765 // node.
5766 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005767 for (unsigned i = 0; i < NumElts; ++i) {
5768 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005769 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005770 continue;
5771 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5772 // A shuffle can only come from building a vector from various
5773 // elements of other vectors.
5774 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005775 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5776 // Furthermore, shuffles require a constant mask, whereas extractelts
5777 // accept variable indices.
5778 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005779 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005780
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005781 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005782 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005783 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5784 if (Source == Sources.end())
5785 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005786
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005787 // Update the minimum and maximum lane number seen.
5788 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5789 Source->MinElt = std::min(Source->MinElt, EltNo);
5790 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005791 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005792
Bob Wilson6f2b8962011-01-07 21:37:30 +00005793 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005794 // are involved.
5795 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005796 return SDValue();
5797
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005798 // Find out the smallest element size among result and two sources, and use
5799 // it as element size to build the shuffle_vector.
5800 EVT SmallestEltTy = VT.getVectorElementType();
5801 for (auto &Source : Sources) {
5802 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5803 if (SrcEltTy.bitsLT(SmallestEltTy))
5804 SmallestEltTy = SrcEltTy;
5805 }
5806 unsigned ResMultiplier =
5807 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5808 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5809 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005810
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005811 // If the source vector is too wide or too narrow, we may nevertheless be able
5812 // to construct a compatible shuffle either by concatenating it with UNDEF or
5813 // extracting a suitable range of elements.
5814 for (auto &Src : Sources) {
5815 EVT SrcVT = Src.ShuffleVec.getValueType();
5816
5817 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005818 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005819
5820 // This stage of the search produces a source with the same element type as
5821 // the original, but with a total width matching the BUILD_VECTOR output.
5822 EVT EltVT = SrcVT.getVectorElementType();
5823 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5824 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5825
5826 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5827 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5828 return SDValue();
5829 // We can pad out the smaller vector for free, so if it's part of a
5830 // shuffle...
5831 Src.ShuffleVec =
5832 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5833 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5834 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005835 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005836
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005837 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5838 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005839
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005840 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005841 // Span too large for a VEXT to cope
5842 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005843 }
5844
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005845 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005846 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005847 Src.ShuffleVec =
5848 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5849 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5850 Src.WindowBase = -NumSrcElts;
5851 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005852 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005853 Src.ShuffleVec =
5854 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5855 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005856 } else {
5857 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005858 SDValue VEXTSrc1 =
5859 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5860 DAG.getConstant(0, dl, MVT::i32));
5861 SDValue VEXTSrc2 =
5862 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5863 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005864
5865 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5866 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005867 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005868 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005869 }
5870 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005871
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005872 // Another possible incompatibility occurs from the vector element types. We
5873 // can fix this by bitcasting the source vectors to the same type we intend
5874 // for the shuffle.
5875 for (auto &Src : Sources) {
5876 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5877 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005878 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005879 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5880 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5881 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5882 Src.WindowBase *= Src.WindowScale;
5883 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005884
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005885 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005886 DEBUG(
5887 for (auto Src : Sources)
5888 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5889 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005890
5891 // The stars all align, our next step is to produce the mask for the shuffle.
5892 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5893 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5894 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5895 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005896 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005897 continue;
5898
5899 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5900 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5901
5902 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5903 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5904 // segment.
5905 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5906 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5907 VT.getVectorElementType().getSizeInBits());
5908 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5909
5910 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5911 // starting at the appropriate offset.
5912 int *LaneMask = &Mask[i * ResMultiplier];
5913
5914 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5915 ExtractBase += NumElts * (Src - Sources.begin());
5916 for (int j = 0; j < LanesDefined; ++j)
5917 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005918 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005919
Bob Wilson6f2b8962011-01-07 21:37:30 +00005920 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005921 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5922 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005923
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005924 // We can't handle more than two sources. This should have already
5925 // been checked before this point.
5926 assert(Sources.size() <= 2 && "Too many sources!");
5927
5928 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5929 for (unsigned i = 0; i < Sources.size(); ++i)
5930 ShuffleOps[i] = Sources[i].ShuffleVec;
5931
5932 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5933 ShuffleOps[1], &Mask[0]);
5934 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005935}
5936
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005937/// isShuffleMaskLegal - Targets can use this to indicate that they only
5938/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5939/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5940/// are assumed to be legal.
5941bool
5942ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5943 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005944 if (VT.getVectorNumElements() == 4 &&
5945 (VT.is128BitVector() || VT.is64BitVector())) {
5946 unsigned PFIndexes[4];
5947 for (unsigned i = 0; i != 4; ++i) {
5948 if (M[i] < 0)
5949 PFIndexes[i] = 8;
5950 else
5951 PFIndexes[i] = M[i];
5952 }
5953
5954 // Compute the index in the perfect shuffle table.
5955 unsigned PFTableIndex =
5956 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5957 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5958 unsigned Cost = (PFEntry >> 30);
5959
5960 if (Cost <= 4)
5961 return true;
5962 }
5963
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005964 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005965 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005966
Bob Wilson846bd792010-06-07 23:53:38 +00005967 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5968 return (EltSize >= 32 ||
5969 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005970 isVREVMask(M, VT, 64) ||
5971 isVREVMask(M, VT, 32) ||
5972 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005973 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005974 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005975 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005976 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005977}
5978
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005979/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5980/// the specified operations to build the shuffle.
5981static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5982 SDValue RHS, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005983 const SDLoc &dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005984 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5985 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5986 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5987
5988 enum {
5989 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5990 OP_VREV,
5991 OP_VDUP0,
5992 OP_VDUP1,
5993 OP_VDUP2,
5994 OP_VDUP3,
5995 OP_VEXT1,
5996 OP_VEXT2,
5997 OP_VEXT3,
5998 OP_VUZPL, // VUZP, left result
5999 OP_VUZPR, // VUZP, right result
6000 OP_VZIPL, // VZIP, left result
6001 OP_VZIPR, // VZIP, right result
6002 OP_VTRNL, // VTRN, left result
6003 OP_VTRNR // VTRN, right result
6004 };
6005
6006 if (OpNum == OP_COPY) {
6007 if (LHSID == (1*9+2)*9+3) return LHS;
6008 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6009 return RHS;
6010 }
6011
6012 SDValue OpLHS, OpRHS;
6013 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6014 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6015 EVT VT = OpLHS.getValueType();
6016
6017 switch (OpNum) {
6018 default: llvm_unreachable("Unknown shuffle opcode!");
6019 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00006020 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00006021 if (VT.getVectorElementType() == MVT::i32 ||
6022 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00006023 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6024 // vrev <4 x i16> -> VREV32
6025 if (VT.getVectorElementType() == MVT::i16)
6026 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6027 // vrev <4 x i8> -> VREV16
6028 assert(VT.getVectorElementType() == MVT::i8);
6029 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006030 case OP_VDUP0:
6031 case OP_VDUP1:
6032 case OP_VDUP2:
6033 case OP_VDUP3:
6034 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006035 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006036 case OP_VEXT1:
6037 case OP_VEXT2:
6038 case OP_VEXT3:
6039 return DAG.getNode(ARMISD::VEXT, dl, VT,
6040 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006041 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006042 case OP_VUZPL:
6043 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006044 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006045 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6046 case OP_VZIPL:
6047 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006048 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006049 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6050 case OP_VTRNL:
6051 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006052 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6053 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006054 }
6055}
6056
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006057static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006058 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006059 SelectionDAG &DAG) {
6060 // Check to see if we can use the VTBL instruction.
6061 SDValue V1 = Op.getOperand(0);
6062 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006063 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006064
6065 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006066 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006067 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006068 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006069
Sanjay Patel57195842016-03-14 17:28:46 +00006070 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006071 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006072 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006073
Owen Anderson77aa2662011-04-05 21:48:57 +00006074 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006075 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006076}
6077
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006078static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6079 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006080 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006081 SDValue OpLHS = Op.getOperand(0);
6082 EVT VT = OpLHS.getValueType();
6083
6084 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6085 "Expect an v8i16/v16i8 type");
6086 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6087 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6088 // extract the first 8 bytes into the top double word and the last 8 bytes
6089 // into the bottom double word. The v8i16 case is similar.
6090 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6091 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006092 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006093}
6094
Bob Wilson2e076c42009-06-22 23:27:02 +00006095static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006096 SDValue V1 = Op.getOperand(0);
6097 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006098 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006099 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006100 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006101
Bob Wilsonc6800b52009-08-13 02:13:04 +00006102 // Convert shuffles that are directly supported on NEON to target-specific
6103 // DAG nodes, instead of keeping them as shuffles and matching them again
6104 // during code selection. This is more efficient and avoids the possibility
6105 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006106 // FIXME: floating-point vectors should be canonicalized to integer vectors
6107 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006108 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006109
Bob Wilson846bd792010-06-07 23:53:38 +00006110 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6111 if (EltSize <= 32) {
6112 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
6113 int Lane = SVN->getSplatIndex();
6114 // If this is undef splat, generate it via "just" vdup, if possible.
6115 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006116
Dan Gohman198b7ff2011-11-03 21:49:52 +00006117 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006118 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6119 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6120 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006121 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6122 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6123 // reaches it).
6124 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6125 !isa<ConstantSDNode>(V1.getOperand(0))) {
6126 bool IsScalarToVector = true;
6127 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006128 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006129 IsScalarToVector = false;
6130 break;
6131 }
6132 if (IsScalarToVector)
6133 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6134 }
Bob Wilson846bd792010-06-07 23:53:38 +00006135 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006136 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006137 }
Bob Wilson846bd792010-06-07 23:53:38 +00006138
6139 bool ReverseVEXT;
6140 unsigned Imm;
6141 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6142 if (ReverseVEXT)
6143 std::swap(V1, V2);
6144 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006145 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006146 }
6147
6148 if (isVREVMask(ShuffleMask, VT, 64))
6149 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6150 if (isVREVMask(ShuffleMask, VT, 32))
6151 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6152 if (isVREVMask(ShuffleMask, VT, 16))
6153 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6154
Sanjay Patel57195842016-03-14 17:28:46 +00006155 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006156 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006157 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006158 }
6159
Bob Wilson846bd792010-06-07 23:53:38 +00006160 // Check for Neon shuffles that modify both input vectors in place.
6161 // If both results are used, i.e., if there are two shuffles with the same
6162 // source operands and with masks corresponding to both results of one of
6163 // these operations, DAG memoization will ensure that a single node is
6164 // used for both shuffles.
6165 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006166 bool isV_UNDEF;
6167 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6168 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6169 if (isV_UNDEF)
6170 V2 = V1;
6171 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6172 .getValue(WhichResult);
6173 }
Bob Wilson846bd792010-06-07 23:53:38 +00006174
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006175 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6176 // shuffles that produce a result larger than their operands with:
6177 // shuffle(concat(v1, undef), concat(v2, undef))
6178 // ->
6179 // shuffle(concat(v1, v2), undef)
6180 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6181 //
6182 // This is useful in the general case, but there are special cases where
6183 // native shuffles produce larger results: the two-result ops.
6184 //
6185 // Look through the concat when lowering them:
6186 // shuffle(concat(v1, v2), undef)
6187 // ->
6188 // concat(VZIP(v1, v2):0, :1)
6189 //
Sanjay Patel57195842016-03-14 17:28:46 +00006190 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006191 SDValue SubV1 = V1->getOperand(0);
6192 SDValue SubV2 = V1->getOperand(1);
6193 EVT SubVT = SubV1.getValueType();
6194
6195 // We expect these to have been canonicalized to -1.
6196 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
6197 return i < (int)VT.getVectorNumElements();
6198 }) && "Unexpected shuffle index into UNDEF operand!");
6199
6200 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6201 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6202 if (isV_UNDEF)
6203 SubV2 = SubV1;
6204 assert((WhichResult == 0) &&
6205 "In-place shuffle of concat can only have one result!");
6206 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6207 SubV1, SubV2);
6208 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6209 Res.getValue(1));
6210 }
6211 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006212 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006213
Bob Wilsona7062312009-08-21 20:54:19 +00006214 // If the shuffle is not directly supported and it has 4 elements, use
6215 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006216 unsigned NumElts = VT.getVectorNumElements();
6217 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006218 unsigned PFIndexes[4];
6219 for (unsigned i = 0; i != 4; ++i) {
6220 if (ShuffleMask[i] < 0)
6221 PFIndexes[i] = 8;
6222 else
6223 PFIndexes[i] = ShuffleMask[i];
6224 }
6225
6226 // Compute the index in the perfect shuffle table.
6227 unsigned PFTableIndex =
6228 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006229 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6230 unsigned Cost = (PFEntry >> 30);
6231
6232 if (Cost <= 4)
6233 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6234 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006235
Bob Wilsond8a9a042010-06-04 00:04:02 +00006236 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006237 if (EltSize >= 32) {
6238 // Do the expansion with floating-point types, since that is what the VFP
6239 // registers are defined to use, and since i64 is not legal.
6240 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6241 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006242 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6243 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006244 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006245 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006246 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006247 Ops.push_back(DAG.getUNDEF(EltVT));
6248 else
6249 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6250 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6251 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006252 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006253 }
Craig Topper48d114b2014-04-26 18:35:24 +00006254 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006255 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006256 }
6257
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006258 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6259 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6260
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006261 if (VT == MVT::v8i8)
6262 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006263 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006264
Bob Wilson6f34e272009-08-14 05:16:33 +00006265 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006266}
6267
Eli Friedmana5e244c2011-10-24 23:08:52 +00006268static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6269 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6270 SDValue Lane = Op.getOperand(2);
6271 if (!isa<ConstantSDNode>(Lane))
6272 return SDValue();
6273
6274 return Op;
6275}
6276
Bob Wilson2e076c42009-06-22 23:27:02 +00006277static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006278 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006279 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006280 if (!isa<ConstantSDNode>(Lane))
6281 return SDValue();
6282
6283 SDValue Vec = Op.getOperand(0);
6284 if (Op.getValueType() == MVT::i32 &&
6285 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006286 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006287 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6288 }
6289
6290 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006291}
6292
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006293static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6294 // The only time a CONCAT_VECTORS operation can have legal types is when
6295 // two 64-bit vectors are concatenated to a 128-bit vector.
6296 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6297 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006298 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006299 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006300 SDValue Op0 = Op.getOperand(0);
6301 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006302 if (!Op0.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006303 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006304 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006305 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006306 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006307 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006308 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006309 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006310 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006311}
6312
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006313/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6314/// element has been zero/sign-extended, depending on the isSigned parameter,
6315/// from an integer type half its size.
6316static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6317 bool isSigned) {
6318 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6319 EVT VT = N->getValueType(0);
6320 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6321 SDNode *BVN = N->getOperand(0).getNode();
6322 if (BVN->getValueType(0) != MVT::v4i32 ||
6323 BVN->getOpcode() != ISD::BUILD_VECTOR)
6324 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006325 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006326 unsigned HiElt = 1 - LoElt;
6327 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6328 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6329 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6330 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6331 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6332 return false;
6333 if (isSigned) {
6334 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6335 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6336 return true;
6337 } else {
6338 if (Hi0->isNullValue() && Hi1->isNullValue())
6339 return true;
6340 }
6341 return false;
6342 }
6343
6344 if (N->getOpcode() != ISD::BUILD_VECTOR)
6345 return false;
6346
6347 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6348 SDNode *Elt = N->getOperand(i).getNode();
6349 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6350 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6351 unsigned HalfSize = EltSize / 2;
6352 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006353 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006354 return false;
6355 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006356 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006357 return false;
6358 }
6359 continue;
6360 }
6361 return false;
6362 }
6363
6364 return true;
6365}
6366
6367/// isSignExtended - Check if a node is a vector value that is sign-extended
6368/// or a constant BUILD_VECTOR with sign-extended elements.
6369static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6370 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6371 return true;
6372 if (isExtendedBUILD_VECTOR(N, DAG, true))
6373 return true;
6374 return false;
6375}
6376
6377/// isZeroExtended - Check if a node is a vector value that is zero-extended
6378/// or a constant BUILD_VECTOR with zero-extended elements.
6379static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6380 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6381 return true;
6382 if (isExtendedBUILD_VECTOR(N, DAG, false))
6383 return true;
6384 return false;
6385}
6386
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006387static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6388 if (OrigVT.getSizeInBits() >= 64)
6389 return OrigVT;
6390
6391 assert(OrigVT.isSimple() && "Expecting a simple value type");
6392
6393 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6394 switch (OrigSimpleTy) {
6395 default: llvm_unreachable("Unexpected Vector Type");
6396 case MVT::v2i8:
6397 case MVT::v2i16:
6398 return MVT::v2i32;
6399 case MVT::v4i8:
6400 return MVT::v4i16;
6401 }
6402}
6403
Sebastian Popa204f722012-11-30 19:08:04 +00006404/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6405/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6406/// We insert the required extension here to get the vector to fill a D register.
6407static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6408 const EVT &OrigTy,
6409 const EVT &ExtTy,
6410 unsigned ExtOpcode) {
6411 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6412 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6413 // 64-bits we need to insert a new extension so that it will be 64-bits.
6414 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6415 if (OrigTy.getSizeInBits() >= 64)
6416 return N;
6417
6418 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006419 EVT NewVT = getExtensionTo64Bits(OrigTy);
6420
Andrew Trickef9de2a2013-05-25 02:42:55 +00006421 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006422}
6423
6424/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6425/// does not do any sign/zero extension. If the original vector is less
6426/// than 64 bits, an appropriate extension will be added after the load to
6427/// reach a total size of 64 bits. We have to add the extension separately
6428/// because ARM does not have a sign/zero extending load for vectors.
6429static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006430 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6431
6432 // The load already has the right type.
6433 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006434 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006435 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6436 LD->isNonTemporal(), LD->isInvariant(),
6437 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006438
6439 // We need to create a zextload/sextload. We cannot just create a load
6440 // followed by a zext/zext node because LowerMUL is also run during normal
6441 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006442 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006443 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006444 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006445 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006446}
6447
6448/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6449/// extending load, or BUILD_VECTOR with extended elements, return the
6450/// unextended value. The unextended vector should be 64 bits so that it can
6451/// be used as an operand to a VMULL instruction. If the original vector size
6452/// before extension is less than 64 bits we add a an extension to resize
6453/// the vector to 64 bits.
6454static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006455 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006456 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6457 N->getOperand(0)->getValueType(0),
6458 N->getValueType(0),
6459 N->getOpcode());
6460
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006461 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006462 return SkipLoadExtensionForVMULL(LD, DAG);
6463
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006464 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6465 // have been legalized as a BITCAST from v4i32.
6466 if (N->getOpcode() == ISD::BITCAST) {
6467 SDNode *BVN = N->getOperand(0).getNode();
6468 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6469 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006470 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006471 return DAG.getBuildVector(
6472 MVT::v2i32, SDLoc(N),
6473 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)});
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006474 }
6475 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6476 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6477 EVT VT = N->getValueType(0);
6478 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6479 unsigned NumElts = VT.getVectorNumElements();
6480 MVT TruncVT = MVT::getIntegerVT(EltSize);
6481 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006482 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006483 for (unsigned i = 0; i != NumElts; ++i) {
6484 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6485 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006486 // Element types smaller than 32 bits are not legal, so use i32 elements.
6487 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006488 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006489 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006490 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006491}
6492
Evan Chenge2086e72011-03-29 01:56:09 +00006493static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6494 unsigned Opcode = N->getOpcode();
6495 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6496 SDNode *N0 = N->getOperand(0).getNode();
6497 SDNode *N1 = N->getOperand(1).getNode();
6498 return N0->hasOneUse() && N1->hasOneUse() &&
6499 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6500 }
6501 return false;
6502}
6503
6504static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6505 unsigned Opcode = N->getOpcode();
6506 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6507 SDNode *N0 = N->getOperand(0).getNode();
6508 SDNode *N1 = N->getOperand(1).getNode();
6509 return N0->hasOneUse() && N1->hasOneUse() &&
6510 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6511 }
6512 return false;
6513}
6514
Bob Wilson38ab35a2010-09-01 23:50:19 +00006515static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6516 // Multiplications are only custom-lowered for 128-bit vectors so that
6517 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6518 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006519 assert(VT.is128BitVector() && VT.isInteger() &&
6520 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006521 SDNode *N0 = Op.getOperand(0).getNode();
6522 SDNode *N1 = Op.getOperand(1).getNode();
6523 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006524 bool isMLA = false;
6525 bool isN0SExt = isSignExtended(N0, DAG);
6526 bool isN1SExt = isSignExtended(N1, DAG);
6527 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006528 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006529 else {
6530 bool isN0ZExt = isZeroExtended(N0, DAG);
6531 bool isN1ZExt = isZeroExtended(N1, DAG);
6532 if (isN0ZExt && isN1ZExt)
6533 NewOpc = ARMISD::VMULLu;
6534 else if (isN1SExt || isN1ZExt) {
6535 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6536 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6537 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6538 NewOpc = ARMISD::VMULLs;
6539 isMLA = true;
6540 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6541 NewOpc = ARMISD::VMULLu;
6542 isMLA = true;
6543 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6544 std::swap(N0, N1);
6545 NewOpc = ARMISD::VMULLu;
6546 isMLA = true;
6547 }
6548 }
6549
6550 if (!NewOpc) {
6551 if (VT == MVT::v2i64)
6552 // Fall through to expand this. It is not legal.
6553 return SDValue();
6554 else
6555 // Other vector multiplications are legal.
6556 return Op;
6557 }
6558 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006559
6560 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006561 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006562 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006563 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006564 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006565 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006566 assert(Op0.getValueType().is64BitVector() &&
6567 Op1.getValueType().is64BitVector() &&
6568 "unexpected types for extended operands to VMULL");
6569 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6570 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006571
Evan Chenge2086e72011-03-29 01:56:09 +00006572 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6573 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6574 // vmull q0, d4, d6
6575 // vmlal q0, d5, d6
6576 // is faster than
6577 // vaddl q0, d4, d5
6578 // vmovl q1, d6
6579 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006580 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6581 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006582 EVT Op1VT = Op1.getValueType();
6583 return DAG.getNode(N0->getOpcode(), DL, VT,
6584 DAG.getNode(NewOpc, DL, VT,
6585 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6586 DAG.getNode(NewOpc, DL, VT,
6587 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006588}
6589
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006590static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl,
6591 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006592 // TODO: Should this propagate fast-math-flags?
6593
Nate Begemanfa62d502011-02-11 20:53:29 +00006594 // Convert to float
6595 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6596 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6597 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6598 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6599 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6600 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6601 // Get reciprocal estimate.
6602 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006603 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006604 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6605 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006606 // Because char has a smaller range than uchar, we can actually get away
6607 // without any newton steps. This requires that we use a weird bias
6608 // of 0xb000, however (again, this has been exhaustively tested).
6609 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6610 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6611 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006612 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006613 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6614 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6615 // Convert back to short.
6616 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6617 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6618 return X;
6619}
6620
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006621static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl,
6622 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006623 // TODO: Should this propagate fast-math-flags?
6624
Nate Begemanfa62d502011-02-11 20:53:29 +00006625 SDValue N2;
6626 // Convert to float.
6627 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6628 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6629 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6630 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6631 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6632 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006633
Nate Begemanfa62d502011-02-11 20:53:29 +00006634 // Use reciprocal estimate and one refinement step.
6635 // float4 recip = vrecpeq_f32(yf);
6636 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006637 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006638 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6639 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006640 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006641 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006642 N1, N2);
6643 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6644 // Because short has a smaller range than ushort, we can actually get away
6645 // with only a single newton step. This requires that we use a weird bias
6646 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006647 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006648 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6649 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006650 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006651 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6652 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6653 // Convert back to integer and return.
6654 // return vmovn_s32(vcvt_s32_f32(result));
6655 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6656 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6657 return N0;
6658}
6659
6660static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6661 EVT VT = Op.getValueType();
6662 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6663 "unexpected type for custom-lowering ISD::SDIV");
6664
Andrew Trickef9de2a2013-05-25 02:42:55 +00006665 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006666 SDValue N0 = Op.getOperand(0);
6667 SDValue N1 = Op.getOperand(1);
6668 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006669
Nate Begemanfa62d502011-02-11 20:53:29 +00006670 if (VT == MVT::v8i8) {
6671 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6672 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006673
Nate Begemanfa62d502011-02-11 20:53:29 +00006674 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006675 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006676 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006677 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006678 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006679 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006680 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006681 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006682
6683 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6684 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6685
6686 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6687 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006688
Nate Begemanfa62d502011-02-11 20:53:29 +00006689 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6690 return N0;
6691 }
6692 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6693}
6694
6695static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006696 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006697 EVT VT = Op.getValueType();
6698 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6699 "unexpected type for custom-lowering ISD::UDIV");
6700
Andrew Trickef9de2a2013-05-25 02:42:55 +00006701 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006702 SDValue N0 = Op.getOperand(0);
6703 SDValue N1 = Op.getOperand(1);
6704 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006705
Nate Begemanfa62d502011-02-11 20:53:29 +00006706 if (VT == MVT::v8i8) {
6707 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6708 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006709
Nate Begemanfa62d502011-02-11 20:53:29 +00006710 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006711 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006712 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006713 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006714 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006715 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006716 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006717 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006718
Nate Begemanfa62d502011-02-11 20:53:29 +00006719 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6720 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006721
Nate Begemanfa62d502011-02-11 20:53:29 +00006722 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6723 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006724
6725 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006726 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6727 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006728 N0);
6729 return N0;
6730 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006731
Nate Begemanfa62d502011-02-11 20:53:29 +00006732 // v4i16 sdiv ... Convert to float.
6733 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6734 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6735 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6736 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6737 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006738 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006739
6740 // Use reciprocal estimate and two refinement steps.
6741 // float4 recip = vrecpeq_f32(yf);
6742 // recip *= vrecpsq_f32(yf, recip);
6743 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006744 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006745 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6746 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006747 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006748 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006749 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006750 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006751 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006752 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006753 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006754 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6755 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6756 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6757 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006758 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006759 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6760 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006761 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006762 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6763 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6764 // Convert back to integer and return.
6765 // return vmovn_u32(vcvt_s32_f32(result));
6766 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6767 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6768 return N0;
6769}
6770
Evan Chenge8916542011-08-30 01:34:54 +00006771static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6772 EVT VT = Op.getNode()->getValueType(0);
6773 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6774
6775 unsigned Opc;
6776 bool ExtraOp = false;
6777 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006778 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006779 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6780 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6781 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6782 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6783 }
6784
6785 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006786 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006787 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006788 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006789 Op.getOperand(1), Op.getOperand(2));
6790}
6791
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006792SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6793 assert(Subtarget->isTargetDarwin());
6794
6795 // For iOS, we want to call an alternative entry point: __sincos_stret,
6796 // return values are passed via sret.
6797 SDLoc dl(Op);
6798 SDValue Arg = Op.getOperand(0);
6799 EVT ArgVT = Arg.getValueType();
6800 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006801 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006802
6803 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006804 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006805
6806 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006807 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006808 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006809
6810 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006811 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6812 SDValue SRet;
6813 if (ShouldUseSRet) {
6814 // Create stack object for sret.
6815 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6816 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6817 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6818 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6819
6820 ArgListEntry Entry;
6821 Entry.Node = SRet;
6822 Entry.Ty = RetTy->getPointerTo();
6823 Entry.isSExt = false;
6824 Entry.isZExt = false;
6825 Entry.isSRet = true;
6826 Args.push_back(Entry);
6827 RetTy = Type::getVoidTy(*DAG.getContext());
6828 }
6829
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006830 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006831 Entry.Node = Arg;
6832 Entry.Ty = ArgTy;
6833 Entry.isSExt = false;
6834 Entry.isZExt = false;
6835 Args.push_back(Entry);
6836
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006837 const char *LibcallName =
6838 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006839 RTLIB::Libcall LC =
6840 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6841 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006842 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006843
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006844 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006845 CLI.setDebugLoc(dl)
6846 .setChain(DAG.getEntryNode())
6847 .setCallee(CC, RetTy, Callee, std::move(Args), 0)
6848 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006849 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6850
Tim Northover8b403662015-10-28 22:51:16 +00006851 if (!ShouldUseSRet)
6852 return CallResult.first;
6853
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006854 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6855 MachinePointerInfo(), false, false, false, 0);
6856
6857 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006858 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006859 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006860 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6861 MachinePointerInfo(), false, false, false, 0);
6862
6863 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6864 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6865 LoadSin.getValue(0), LoadCos.getValue(0));
6866}
6867
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006868SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006869 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006870 SDValue &Chain) const {
6871 EVT VT = Op.getValueType();
6872 assert((VT == MVT::i32 || VT == MVT::i64) &&
6873 "unexpected type for custom lowering DIV");
6874 SDLoc dl(Op);
6875
6876 const auto &DL = DAG.getDataLayout();
6877 const auto &TLI = DAG.getTargetLoweringInfo();
6878
6879 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006880 if (Signed)
6881 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6882 else
6883 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006884
6885 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6886
6887 ARMTargetLowering::ArgListTy Args;
6888
6889 for (auto AI : {1, 0}) {
6890 ArgListEntry Arg;
6891 Arg.Node = Op.getOperand(AI);
6892 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6893 Args.push_back(Arg);
6894 }
6895
6896 CallLoweringInfo CLI(DAG);
6897 CLI.setDebugLoc(dl)
6898 .setChain(Chain)
6899 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6900 ES, std::move(Args), 0);
6901
6902 return LowerCallTo(CLI).first;
6903}
6904
Martell Maloned1229242015-11-26 15:34:03 +00006905SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6906 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006907 assert(Op.getValueType() == MVT::i32 &&
6908 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006909 SDLoc dl(Op);
6910
6911 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6912 DAG.getEntryNode(), Op.getOperand(1));
6913
Martell Maloned1229242015-11-26 15:34:03 +00006914 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006915}
6916
6917void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00006918 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006919 SmallVectorImpl<SDValue> &Results) const {
6920 const auto &DL = DAG.getDataLayout();
6921 const auto &TLI = DAG.getTargetLoweringInfo();
6922
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006923 assert(Op.getValueType() == MVT::i64 &&
6924 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006925 SDLoc dl(Op);
6926
6927 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6928 DAG.getConstant(0, dl, MVT::i32));
6929 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6930 DAG.getConstant(1, dl, MVT::i32));
6931 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6932
6933 SDValue DBZCHK =
6934 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6935
Martell Maloned1229242015-11-26 15:34:03 +00006936 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006937
6938 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6939 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6940 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6941 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6942
6943 Results.push_back(Lower);
6944 Results.push_back(Upper);
6945}
6946
Eli Friedman10f9ce22011-09-15 22:26:18 +00006947static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00006948 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
6949 // Acquire/Release load/store is not legal for targets without a dmb or
6950 // equivalent available.
6951 return SDValue();
Eli Friedmanba912e02011-09-15 22:18:49 +00006952
JF Bastien800f87a2016-04-06 21:19:33 +00006953 // Monotonic load/store is legal for all targets.
6954 return Op;
Eli Friedmanba912e02011-09-15 22:18:49 +00006955}
6956
Tim Northoverbc933082013-05-23 19:11:20 +00006957static void ReplaceREADCYCLECOUNTER(SDNode *N,
6958 SmallVectorImpl<SDValue> &Results,
6959 SelectionDAG &DAG,
6960 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006961 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006962 // Under Power Management extensions, the cycle-count is:
6963 // mrc p15, #0, <Rt>, c9, c13, #0
6964 SDValue Ops[] = { N->getOperand(0), // Chain
6965 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6966 DAG.getConstant(15, DL, MVT::i32),
6967 DAG.getConstant(0, DL, MVT::i32),
6968 DAG.getConstant(9, DL, MVT::i32),
6969 DAG.getConstant(13, DL, MVT::i32),
6970 DAG.getConstant(0, DL, MVT::i32)
6971 };
Tim Northoverbc933082013-05-23 19:11:20 +00006972
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006973 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6974 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6975 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6976 DAG.getConstant(0, DL, MVT::i32)));
6977 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006978}
6979
Tim Northover1ee27c72016-04-19 22:25:02 +00006980static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) {
6981 SDLoc dl(V.getNode());
6982 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32);
6983 SDValue VHi = DAG.getAnyExtOrTrunc(
6984 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)),
6985 dl, MVT::i32);
Tim Northoverb629c772016-04-18 21:48:55 +00006986 SDValue RegClass =
6987 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
6988 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
6989 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
Tim Northover1ee27c72016-04-19 22:25:02 +00006990 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
Tim Northoverb629c772016-04-18 21:48:55 +00006991 return SDValue(
6992 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
6993}
6994
6995static void ReplaceCMP_SWAP_64Results(SDNode *N,
6996 SmallVectorImpl<SDValue> & Results,
6997 SelectionDAG &DAG) {
6998 assert(N->getValueType(0) == MVT::i64 &&
6999 "AtomicCmpSwap on types less than 64 should be legal");
7000 SDValue Ops[] = {N->getOperand(1),
Tim Northover1ee27c72016-04-19 22:25:02 +00007001 createGPRPairNode(DAG, N->getOperand(2)),
7002 createGPRPairNode(DAG, N->getOperand(3)),
Tim Northoverb629c772016-04-18 21:48:55 +00007003 N->getOperand(0)};
7004 SDNode *CmpSwap = DAG.getMachineNode(
7005 ARM::CMP_SWAP_64, SDLoc(N),
7006 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
7007
7008 MachineFunction &MF = DAG.getMachineFunction();
7009 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
7010 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
7011 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
7012
7013 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_0, SDLoc(N), MVT::i32,
7014 SDValue(CmpSwap, 0)));
7015 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_1, SDLoc(N), MVT::i32,
7016 SDValue(CmpSwap, 0)));
7017 Results.push_back(SDValue(CmpSwap, 2));
7018}
7019
Dan Gohman21cea8a2010-04-17 15:26:15 +00007020SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007021 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007022 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007023 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007024 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00007025 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00007026 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00007027 switch (Subtarget->getTargetTriple().getObjectFormat()) {
7028 default: llvm_unreachable("unknown object format");
7029 case Triple::COFF:
7030 return LowerGlobalAddressWindows(Op, DAG);
7031 case Triple::ELF:
7032 return LowerGlobalAddressELF(Op, DAG);
7033 case Triple::MachO:
7034 return LowerGlobalAddressDarwin(Op, DAG);
7035 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00007036 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00007037 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007038 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
7039 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007040 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00007041 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00007042 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00007043 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00007044 case ISD::SINT_TO_FP:
7045 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
7046 case ISD::FP_TO_SINT:
7047 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007048 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00007049 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00007050 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00007051 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00007052 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00007053 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00007054 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
7055 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00007056 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007057 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00007058 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00007059 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00007060 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
7061 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007062 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00007063 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007064 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007065 case ISD::CTTZ:
7066 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007067 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007068 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00007069 case ISD::SETCCE: return LowerSETCCE(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007070 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007071 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007072 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007073 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007074 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007075 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007076 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007077 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00007078 case ISD::SDIV:
7079 if (Subtarget->isTargetWindows())
7080 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
7081 return LowerSDIV(Op, DAG);
7082 case ISD::UDIV:
7083 if (Subtarget->isTargetWindows())
7084 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
7085 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007086 case ISD::ADDC:
7087 case ISD::ADDE:
7088 case ISD::SUBC:
7089 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007090 case ISD::SADDO:
7091 case ISD::UADDO:
7092 case ISD::SSUBO:
7093 case ISD::USUBO:
7094 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007095 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007096 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007097 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007098 case ISD::SDIVREM:
7099 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007100 case ISD::DYNAMIC_STACKALLOC:
7101 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7102 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7103 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007104 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7105 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007106 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007107 }
Evan Cheng10043e22007-01-19 07:51:42 +00007108}
7109
Duncan Sands6ed40142008-12-01 11:39:25 +00007110/// ReplaceNodeResults - Replace the results of node with an illegal result
7111/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007112void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007113 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007114 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007115 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007116 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007117 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007118 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007119 case ISD::READ_REGISTER:
7120 ExpandREAD_REGISTER(N, Results, DAG);
7121 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007122 case ISD::BITCAST:
7123 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007124 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007125 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007126 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007127 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007128 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007129 case ISD::SREM:
7130 case ISD::UREM:
7131 Res = LowerREM(N, DAG);
7132 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007133 case ISD::SDIVREM:
7134 case ISD::UDIVREM:
7135 Res = LowerDivRem(SDValue(N, 0), DAG);
7136 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7137 Results.push_back(Res.getValue(0));
7138 Results.push_back(Res.getValue(1));
7139 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007140 case ISD::READCYCLECOUNTER:
7141 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7142 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007143 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007144 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007145 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007146 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7147 Results);
Tim Northoverb629c772016-04-18 21:48:55 +00007148 case ISD::ATOMIC_CMP_SWAP:
7149 ReplaceCMP_SWAP_64Results(N, Results, DAG);
7150 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00007151 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007152 if (Res.getNode())
7153 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007154}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007155
Evan Cheng10043e22007-01-19 07:51:42 +00007156//===----------------------------------------------------------------------===//
7157// ARM Scheduler Hooks
7158//===----------------------------------------------------------------------===//
7159
Bill Wendling030b58e2011-10-06 22:18:16 +00007160/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7161/// registers the function context.
7162void ARMTargetLowering::
7163SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
7164 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007165 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00007166 DebugLoc dl = MI->getDebugLoc();
7167 MachineFunction *MF = MBB->getParent();
7168 MachineRegisterInfo *MRI = &MF->getRegInfo();
7169 MachineConstantPool *MCP = MF->getConstantPool();
7170 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7171 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007172
Bill Wendling374ee192011-10-03 21:25:38 +00007173 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007174 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007175
Bill Wendling374ee192011-10-03 21:25:38 +00007176 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007177 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007178 ARMConstantPoolValue *CPV =
7179 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7180 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7181
Craig Topper61e88f42014-11-21 05:58:21 +00007182 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7183 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007184
Bill Wendling030b58e2011-10-06 22:18:16 +00007185 // Grab constant pool and fixed stack memory operands.
7186 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007187 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7188 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007189
7190 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007191 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7192 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007193
7194 // Load the address of the dispatch MBB into the jump buffer.
7195 if (isThumb2) {
7196 // Incoming value: jbuf
7197 // ldr.n r5, LCPI1_1
7198 // orr r5, r5, #1
7199 // add r5, pc
7200 // str r5, [$jbuf, #+4] ; &jbuf[1]
7201 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7202 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7203 .addConstantPoolIndex(CPI)
7204 .addMemOperand(CPMMO));
7205 // Set the low bit because of thumb mode.
7206 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7207 AddDefaultCC(
7208 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7209 .addReg(NewVReg1, RegState::Kill)
7210 .addImm(0x01)));
7211 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7212 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7213 .addReg(NewVReg2, RegState::Kill)
7214 .addImm(PCLabelId);
7215 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7216 .addReg(NewVReg3, RegState::Kill)
7217 .addFrameIndex(FI)
7218 .addImm(36) // &jbuf[1] :: pc
7219 .addMemOperand(FIMMOSt));
7220 } else if (isThumb) {
7221 // Incoming value: jbuf
7222 // ldr.n r1, LCPI1_4
7223 // add r1, pc
7224 // mov r2, #1
7225 // orrs r1, r2
7226 // add r2, $jbuf, #+4 ; &jbuf[1]
7227 // str r1, [r2]
7228 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7229 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7230 .addConstantPoolIndex(CPI)
7231 .addMemOperand(CPMMO));
7232 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7233 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7234 .addReg(NewVReg1, RegState::Kill)
7235 .addImm(PCLabelId);
7236 // Set the low bit because of thumb mode.
7237 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7238 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7239 .addReg(ARM::CPSR, RegState::Define)
7240 .addImm(1));
7241 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7242 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7243 .addReg(ARM::CPSR, RegState::Define)
7244 .addReg(NewVReg2, RegState::Kill)
7245 .addReg(NewVReg3, RegState::Kill));
7246 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007247 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7248 .addFrameIndex(FI)
7249 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007250 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7251 .addReg(NewVReg4, RegState::Kill)
7252 .addReg(NewVReg5, RegState::Kill)
7253 .addImm(0)
7254 .addMemOperand(FIMMOSt));
7255 } else {
7256 // Incoming value: jbuf
7257 // ldr r1, LCPI1_1
7258 // add r1, pc, r1
7259 // str r1, [$jbuf, #+4] ; &jbuf[1]
7260 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7261 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7262 .addConstantPoolIndex(CPI)
7263 .addImm(0)
7264 .addMemOperand(CPMMO));
7265 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7266 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7267 .addReg(NewVReg1, RegState::Kill)
7268 .addImm(PCLabelId));
7269 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7270 .addReg(NewVReg2, RegState::Kill)
7271 .addFrameIndex(FI)
7272 .addImm(36) // &jbuf[1] :: pc
7273 .addMemOperand(FIMMOSt));
7274 }
7275}
7276
Matthias Brauneec4efc2015-04-28 00:37:05 +00007277void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
7278 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007279 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007280 DebugLoc dl = MI->getDebugLoc();
7281 MachineFunction *MF = MBB->getParent();
7282 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007283 MachineFrameInfo *MFI = MF->getFrameInfo();
7284 int FI = MFI->getFunctionContextIndex();
7285
Craig Topper61e88f42014-11-21 05:58:21 +00007286 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7287 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007288
Bill Wendling362c1b02011-10-06 21:29:56 +00007289 // Get a mapping of the call site numbers to all of the landing pads they're
7290 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007291 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7292 unsigned MaxCSNum = 0;
7293 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007294 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7295 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007296 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007297
7298 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7299 // pad.
7300 for (MachineBasicBlock::iterator
7301 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7302 if (!II->isEHLabel()) continue;
7303
7304 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007305 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007306
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007307 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7308 for (SmallVectorImpl<unsigned>::iterator
7309 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7310 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007311 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007312 MaxCSNum = std::max(MaxCSNum, *CSI);
7313 }
Bill Wendling202803e2011-10-05 00:02:33 +00007314 break;
7315 }
7316 }
7317
7318 // Get an ordered list of the machine basic blocks for the jump table.
7319 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007320 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007321 LPadList.reserve(CallSiteNumToLPad.size());
7322 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7323 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7324 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007325 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007326 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007327 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7328 }
Bill Wendling202803e2011-10-05 00:02:33 +00007329 }
7330
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007331 assert(!LPadList.empty() &&
7332 "No landing pad destinations for the dispatch jump table!");
7333
Bill Wendling362c1b02011-10-06 21:29:56 +00007334 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007335 MachineJumpTableInfo *JTI =
7336 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7337 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007338 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007339
Bill Wendling362c1b02011-10-06 21:29:56 +00007340 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007341
7342 // Shove the dispatch's address into the return slot in the function context.
7343 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007344 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007345
Bill Wendling324be982011-10-05 00:39:32 +00007346 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007347 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007348 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007349 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007350 else
7351 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7352
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007353 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007354 DispatchBB->addSuccessor(TrapBB);
7355
7356 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7357 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007358
Bill Wendling510fbcd2011-10-17 21:32:56 +00007359 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007360 MF->insert(MF->end(), DispatchBB);
7361 MF->insert(MF->end(), DispContBB);
7362 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007363
Bill Wendling030b58e2011-10-06 22:18:16 +00007364 // Insert code into the entry block that creates and registers the function
7365 // context.
7366 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7367
Alex Lorenze40c8a22015-08-11 23:09:45 +00007368 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7369 MachinePointerInfo::getFixedStack(*MF, FI),
7370 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007371
Chad Rosier1ec8e402012-11-06 23:05:24 +00007372 MachineInstrBuilder MIB;
7373 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7374
7375 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7376 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7377
7378 // Add a register mask with no preserved registers. This results in all
7379 // registers being marked as clobbered.
7380 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007381
Bill Wendling85833f72011-10-18 22:49:07 +00007382 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007383 if (Subtarget->isThumb2()) {
7384 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7385 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7386 .addFrameIndex(FI)
7387 .addImm(4)
7388 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007389
Bill Wendling85833f72011-10-18 22:49:07 +00007390 if (NumLPads < 256) {
7391 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7392 .addReg(NewVReg1)
7393 .addImm(LPadList.size()));
7394 } else {
7395 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7396 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007397 .addImm(NumLPads & 0xFFFF));
7398
7399 unsigned VReg2 = VReg1;
7400 if ((NumLPads & 0xFFFF0000) != 0) {
7401 VReg2 = MRI->createVirtualRegister(TRC);
7402 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7403 .addReg(VReg1)
7404 .addImm(NumLPads >> 16));
7405 }
7406
Bill Wendling85833f72011-10-18 22:49:07 +00007407 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7408 .addReg(NewVReg1)
7409 .addReg(VReg2));
7410 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007411
Bill Wendling5626c662011-10-06 22:53:00 +00007412 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7413 .addMBB(TrapBB)
7414 .addImm(ARMCC::HI)
7415 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007416
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007417 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7418 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007419 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007420
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007421 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007422 AddDefaultCC(
7423 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007424 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7425 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007426 .addReg(NewVReg1)
7427 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7428
7429 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007430 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007431 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007432 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007433 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007434 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7435 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7436 .addFrameIndex(FI)
7437 .addImm(1)
7438 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007439
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007440 if (NumLPads < 256) {
7441 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7442 .addReg(NewVReg1)
7443 .addImm(NumLPads));
7444 } else {
7445 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007446 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7447 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7448
7449 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007450 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007451 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007452 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007453 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007454
7455 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7456 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7457 .addReg(VReg1, RegState::Define)
7458 .addConstantPoolIndex(Idx));
7459 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7460 .addReg(NewVReg1)
7461 .addReg(VReg1));
7462 }
7463
Bill Wendlingb3d46782011-10-06 23:37:36 +00007464 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7465 .addMBB(TrapBB)
7466 .addImm(ARMCC::HI)
7467 .addReg(ARM::CPSR);
7468
7469 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7470 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7471 .addReg(ARM::CPSR, RegState::Define)
7472 .addReg(NewVReg1)
7473 .addImm(2));
7474
7475 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007476 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007477 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007478
7479 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7480 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7481 .addReg(ARM::CPSR, RegState::Define)
7482 .addReg(NewVReg2, RegState::Kill)
7483 .addReg(NewVReg3));
7484
Alex Lorenze40c8a22015-08-11 23:09:45 +00007485 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7486 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007487
7488 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7489 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7490 .addReg(NewVReg4, RegState::Kill)
7491 .addImm(0)
7492 .addMemOperand(JTMMOLd));
7493
Chad Rosier96603432013-03-01 18:30:38 +00007494 unsigned NewVReg6 = NewVReg5;
7495 if (RelocM == Reloc::PIC_) {
7496 NewVReg6 = MRI->createVirtualRegister(TRC);
7497 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7498 .addReg(ARM::CPSR, RegState::Define)
7499 .addReg(NewVReg5, RegState::Kill)
7500 .addReg(NewVReg3));
7501 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007502
7503 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7504 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007505 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007506 } else {
7507 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7508 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7509 .addFrameIndex(FI)
7510 .addImm(4)
7511 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007512
Bill Wendling4969dcd2011-10-18 22:52:20 +00007513 if (NumLPads < 256) {
7514 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7515 .addReg(NewVReg1)
7516 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007517 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007518 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7519 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007520 .addImm(NumLPads & 0xFFFF));
7521
7522 unsigned VReg2 = VReg1;
7523 if ((NumLPads & 0xFFFF0000) != 0) {
7524 VReg2 = MRI->createVirtualRegister(TRC);
7525 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7526 .addReg(VReg1)
7527 .addImm(NumLPads >> 16));
7528 }
7529
Bill Wendling4969dcd2011-10-18 22:52:20 +00007530 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7531 .addReg(NewVReg1)
7532 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007533 } else {
7534 MachineConstantPool *ConstantPool = MF->getConstantPool();
7535 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7536 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7537
7538 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007539 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007540 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007541 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007542 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7543
7544 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7545 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7546 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007547 .addConstantPoolIndex(Idx)
7548 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007549 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7550 .addReg(NewVReg1)
7551 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007552 }
7553
Bill Wendling5626c662011-10-06 22:53:00 +00007554 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7555 .addMBB(TrapBB)
7556 .addImm(ARMCC::HI)
7557 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007558
Bill Wendling973c8172011-10-18 22:11:18 +00007559 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007560 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007561 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007562 .addReg(NewVReg1)
7563 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007564 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7565 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007566 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007567
Alex Lorenze40c8a22015-08-11 23:09:45 +00007568 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7569 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007570 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007571 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007572 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7573 .addReg(NewVReg3, RegState::Kill)
7574 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007575 .addImm(0)
7576 .addMemOperand(JTMMOLd));
7577
Chad Rosier96603432013-03-01 18:30:38 +00007578 if (RelocM == Reloc::PIC_) {
7579 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7580 .addReg(NewVReg5, RegState::Kill)
7581 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007582 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007583 } else {
7584 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7585 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007586 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007587 }
Bill Wendling5626c662011-10-06 22:53:00 +00007588 }
Bill Wendling202803e2011-10-05 00:02:33 +00007589
Bill Wendling324be982011-10-05 00:39:32 +00007590 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007591 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007592 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007593 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7594 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007595 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007596 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007597 }
7598
Bill Wendling26d27802011-10-17 05:25:09 +00007599 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007600 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007601 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007602 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007603
7604 // Remove the landing pad successor from the invoke block and replace it
7605 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007606 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7607 BB->succ_end());
7608 while (!Successors.empty()) {
7609 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007610 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007611 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007612 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007613 }
7614 }
7615
Cong Houd97c1002015-12-01 05:29:22 +00007616 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00007617 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007618
7619 // Find the invoke call and mark all of the callee-saved registers as
7620 // 'implicit defined' so that they're spilled. This prevents code from
7621 // moving instructions to before the EH block, where they will never be
7622 // executed.
7623 for (MachineBasicBlock::reverse_iterator
7624 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007625 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007626
7627 DenseMap<unsigned, bool> DefRegs;
7628 for (MachineInstr::mop_iterator
7629 OI = II->operands_begin(), OE = II->operands_end();
7630 OI != OE; ++OI) {
7631 if (!OI->isReg()) continue;
7632 DefRegs[OI->getReg()] = true;
7633 }
7634
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007635 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007636
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007637 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007638 unsigned Reg = SavedRegs[i];
7639 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007640 !ARM::tGPRRegClass.contains(Reg) &&
7641 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007642 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007643 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007644 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007645 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007646 continue;
7647 if (!DefRegs[Reg])
7648 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007649 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007650
7651 break;
7652 }
Bill Wendling883ec972011-10-07 23:18:02 +00007653 }
Bill Wendling324be982011-10-05 00:39:32 +00007654
Bill Wendling617075f2011-10-18 18:30:49 +00007655 // Mark all former landing pads as non-landing pads. The dispatch is the only
7656 // landing pad now.
7657 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7658 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007659 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007660
Bill Wendling324be982011-10-05 00:39:32 +00007661 // The instruction is gone now.
7662 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007663}
7664
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007665static
7666MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7667 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7668 E = MBB->succ_end(); I != E; ++I)
7669 if (*I != Succ)
7670 return *I;
7671 llvm_unreachable("Expecting a BB with two successors!");
7672}
7673
Manman Renb504f492013-10-29 22:27:32 +00007674/// Return the load opcode for a given load size. If load size >= 8,
7675/// neon opcode will be returned.
7676static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7677 if (LdSize >= 8)
7678 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7679 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7680 if (IsThumb1)
7681 return LdSize == 4 ? ARM::tLDRi
7682 : LdSize == 2 ? ARM::tLDRHi
7683 : LdSize == 1 ? ARM::tLDRBi : 0;
7684 if (IsThumb2)
7685 return LdSize == 4 ? ARM::t2LDR_POST
7686 : LdSize == 2 ? ARM::t2LDRH_POST
7687 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7688 return LdSize == 4 ? ARM::LDR_POST_IMM
7689 : LdSize == 2 ? ARM::LDRH_POST
7690 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7691}
7692
7693/// Return the store opcode for a given store size. If store size >= 8,
7694/// neon opcode will be returned.
7695static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7696 if (StSize >= 8)
7697 return StSize == 16 ? ARM::VST1q32wb_fixed
7698 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7699 if (IsThumb1)
7700 return StSize == 4 ? ARM::tSTRi
7701 : StSize == 2 ? ARM::tSTRHi
7702 : StSize == 1 ? ARM::tSTRBi : 0;
7703 if (IsThumb2)
7704 return StSize == 4 ? ARM::t2STR_POST
7705 : StSize == 2 ? ARM::t2STRH_POST
7706 : StSize == 1 ? ARM::t2STRB_POST : 0;
7707 return StSize == 4 ? ARM::STR_POST_IMM
7708 : StSize == 2 ? ARM::STRH_POST
7709 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7710}
7711
7712/// Emit a post-increment load operation with given size. The instructions
7713/// will be added to BB at Pos.
7714static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007715 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00007716 unsigned LdSize, unsigned Data, unsigned AddrIn,
7717 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7718 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7719 assert(LdOpc != 0 && "Should have a load opcode");
7720 if (LdSize >= 8) {
7721 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7722 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7723 .addImm(0));
7724 } else if (IsThumb1) {
7725 // load + update AddrIn
7726 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7727 .addReg(AddrIn).addImm(0));
7728 MachineInstrBuilder MIB =
7729 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7730 MIB = AddDefaultT1CC(MIB);
7731 MIB.addReg(AddrIn).addImm(LdSize);
7732 AddDefaultPred(MIB);
7733 } else if (IsThumb2) {
7734 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7735 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7736 .addImm(LdSize));
7737 } else { // arm
7738 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7739 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7740 .addReg(0).addImm(LdSize));
7741 }
7742}
7743
7744/// Emit a post-increment store operation with given size. The instructions
7745/// will be added to BB at Pos.
7746static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007747 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00007748 unsigned StSize, unsigned Data, unsigned AddrIn,
7749 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7750 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7751 assert(StOpc != 0 && "Should have a store opcode");
7752 if (StSize >= 8) {
7753 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7754 .addReg(AddrIn).addImm(0).addReg(Data));
7755 } else if (IsThumb1) {
7756 // store + update AddrIn
7757 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7758 .addReg(AddrIn).addImm(0));
7759 MachineInstrBuilder MIB =
7760 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7761 MIB = AddDefaultT1CC(MIB);
7762 MIB.addReg(AddrIn).addImm(StSize);
7763 AddDefaultPred(MIB);
7764 } else if (IsThumb2) {
7765 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7766 .addReg(Data).addReg(AddrIn).addImm(StSize));
7767 } else { // arm
7768 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7769 .addReg(Data).addReg(AddrIn).addReg(0)
7770 .addImm(StSize));
7771 }
7772}
7773
David Peixottoc32e24a2013-10-17 19:49:22 +00007774MachineBasicBlock *
7775ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7776 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007777 // This pseudo instruction has 3 operands: dst, src, size
7778 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7779 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007780 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007781 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007782 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007783
7784 unsigned dest = MI->getOperand(0).getReg();
7785 unsigned src = MI->getOperand(1).getReg();
7786 unsigned SizeVal = MI->getOperand(2).getImm();
7787 unsigned Align = MI->getOperand(3).getImm();
7788 DebugLoc dl = MI->getDebugLoc();
7789
Manman Rene8735522012-06-01 19:33:18 +00007790 MachineFunction *MF = BB->getParent();
7791 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007792 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007793 const TargetRegisterClass *TRC = nullptr;
7794 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007795
7796 bool IsThumb1 = Subtarget->isThumb1Only();
7797 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007798
7799 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007800 UnitSize = 1;
7801 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007802 UnitSize = 2;
7803 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007804 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007805 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007806 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007807 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007808 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007809 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007810 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007811 }
7812 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007813 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007814 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007815 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007816
David Peixottob0653e532013-10-24 16:39:36 +00007817 // Select the correct opcode and register class for unit size load/store
7818 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007819 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007820 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007821 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7822 : UnitSize == 8 ? &ARM::DPRRegClass
7823 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007824
Manman Rene8735522012-06-01 19:33:18 +00007825 unsigned BytesLeft = SizeVal % UnitSize;
7826 unsigned LoopSize = SizeVal - BytesLeft;
7827
7828 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7829 // Use LDR and STR to copy.
7830 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7831 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7832 unsigned srcIn = src;
7833 unsigned destIn = dest;
7834 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007835 unsigned srcOut = MRI.createVirtualRegister(TRC);
7836 unsigned destOut = MRI.createVirtualRegister(TRC);
7837 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007838 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7839 IsThumb1, IsThumb2);
7840 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7841 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007842 srcIn = srcOut;
7843 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007844 }
7845
7846 // Handle the leftover bytes with LDRB and STRB.
7847 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7848 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007849 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007850 unsigned srcOut = MRI.createVirtualRegister(TRC);
7851 unsigned destOut = MRI.createVirtualRegister(TRC);
7852 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007853 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7854 IsThumb1, IsThumb2);
7855 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7856 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007857 srcIn = srcOut;
7858 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007859 }
7860 MI->eraseFromParent(); // The instruction is gone now.
7861 return BB;
7862 }
7863
7864 // Expand the pseudo op to a loop.
7865 // thisMBB:
7866 // ...
7867 // movw varEnd, # --> with thumb2
7868 // movt varEnd, #
7869 // ldrcp varEnd, idx --> without thumb2
7870 // fallthrough --> loopMBB
7871 // loopMBB:
7872 // PHI varPhi, varEnd, varLoop
7873 // PHI srcPhi, src, srcLoop
7874 // PHI destPhi, dst, destLoop
7875 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7876 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7877 // subs varLoop, varPhi, #UnitSize
7878 // bne loopMBB
7879 // fallthrough --> exitMBB
7880 // exitMBB:
7881 // epilogue to handle left-over bytes
7882 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7883 // [destOut] = STRB_POST(scratch, destLoop, 1)
7884 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7885 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7886 MF->insert(It, loopMBB);
7887 MF->insert(It, exitMBB);
7888
7889 // Transfer the remainder of BB and its successor edges to exitMBB.
7890 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007891 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007892 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7893
7894 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007895 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007896 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007897 unsigned Vtmp = varEnd;
7898 if ((LoopSize & 0xFFFF0000) != 0)
7899 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007900 AddDefaultPred(BuildMI(BB, dl,
7901 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7902 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007903
7904 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007905 AddDefaultPred(BuildMI(BB, dl,
7906 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7907 varEnd)
7908 .addReg(Vtmp)
7909 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007910 } else {
7911 MachineConstantPool *ConstantPool = MF->getConstantPool();
7912 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7913 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7914
7915 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007916 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007917 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007918 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007919 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7920
7921 if (IsThumb1)
7922 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7923 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7924 else
7925 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7926 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7927 }
Manman Rene8735522012-06-01 19:33:18 +00007928 BB->addSuccessor(loopMBB);
7929
7930 // Generate the loop body:
7931 // varPhi = PHI(varLoop, varEnd)
7932 // srcPhi = PHI(srcLoop, src)
7933 // destPhi = PHI(destLoop, dst)
7934 MachineBasicBlock *entryBB = BB;
7935 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007936 unsigned varLoop = MRI.createVirtualRegister(TRC);
7937 unsigned varPhi = MRI.createVirtualRegister(TRC);
7938 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7939 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7940 unsigned destLoop = MRI.createVirtualRegister(TRC);
7941 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007942
7943 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7944 .addReg(varLoop).addMBB(loopMBB)
7945 .addReg(varEnd).addMBB(entryBB);
7946 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7947 .addReg(srcLoop).addMBB(loopMBB)
7948 .addReg(src).addMBB(entryBB);
7949 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7950 .addReg(destLoop).addMBB(loopMBB)
7951 .addReg(dest).addMBB(entryBB);
7952
7953 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7954 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007955 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007956 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7957 IsThumb1, IsThumb2);
7958 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7959 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007960
7961 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007962 if (IsThumb1) {
7963 MachineInstrBuilder MIB =
7964 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7965 MIB = AddDefaultT1CC(MIB);
7966 MIB.addReg(varPhi).addImm(UnitSize);
7967 AddDefaultPred(MIB);
7968 } else {
7969 MachineInstrBuilder MIB =
7970 BuildMI(*BB, BB->end(), dl,
7971 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7972 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7973 MIB->getOperand(5).setReg(ARM::CPSR);
7974 MIB->getOperand(5).setIsDef(true);
7975 }
7976 BuildMI(*BB, BB->end(), dl,
7977 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7978 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007979
7980 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7981 BB->addSuccessor(loopMBB);
7982 BB->addSuccessor(exitMBB);
7983
7984 // Add epilogue to handle BytesLeft.
7985 BB = exitMBB;
7986 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007987
7988 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7989 // [destOut] = STRB_POST(scratch, destLoop, 1)
7990 unsigned srcIn = srcLoop;
7991 unsigned destIn = destLoop;
7992 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007993 unsigned srcOut = MRI.createVirtualRegister(TRC);
7994 unsigned destOut = MRI.createVirtualRegister(TRC);
7995 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007996 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7997 IsThumb1, IsThumb2);
7998 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7999 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008000 srcIn = srcOut;
8001 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008002 }
8003
8004 MI->eraseFromParent(); // The instruction is gone now.
8005 return BB;
8006}
8007
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00008008MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008009ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
8010 MachineBasicBlock *MBB) const {
8011 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00008012 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008013 DebugLoc DL = MI->getDebugLoc();
8014
8015 assert(Subtarget->isTargetWindows() &&
8016 "__chkstk is only supported on Windows");
8017 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
8018
8019 // __chkstk takes the number of words to allocate on the stack in R4, and
8020 // returns the stack adjustment in number of bytes in R4. This will not
8021 // clober any other registers (other than the obvious lr).
8022 //
8023 // Although, technically, IP should be considered a register which may be
8024 // clobbered, the call itself will not touch it. Windows on ARM is a pure
8025 // thumb-2 environment, so there is no interworking required. As a result, we
8026 // do not expect a veneer to be emitted by the linker, clobbering IP.
8027 //
Alp Toker1d099d92014-06-19 19:41:26 +00008028 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008029 // required, again, ensuring that IP is not clobbered.
8030 //
8031 // Finally, although some linkers may theoretically provide a trampoline for
8032 // out of range calls (which is quite common due to a 32M range limitation of
8033 // branches for Thumb), we can generate the long-call version via
8034 // -mcmodel=large, alleviating the need for the trampoline which may clobber
8035 // IP.
8036
8037 switch (TM.getCodeModel()) {
8038 case CodeModel::Small:
8039 case CodeModel::Medium:
8040 case CodeModel::Default:
8041 case CodeModel::Kernel:
8042 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
8043 .addImm((unsigned)ARMCC::AL).addReg(0)
8044 .addExternalSymbol("__chkstk")
8045 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8046 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8047 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8048 break;
8049 case CodeModel::Large:
8050 case CodeModel::JITDefault: {
8051 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
8052 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
8053
8054 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
8055 .addExternalSymbol("__chkstk");
8056 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
8057 .addImm((unsigned)ARMCC::AL).addReg(0)
8058 .addReg(Reg, RegState::Kill)
8059 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8060 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8061 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8062 break;
8063 }
8064 }
8065
8066 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
8067 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +00008068 .addReg(ARM::SP, RegState::Kill)
8069 .addReg(ARM::R4, RegState::Kill)
8070 .setMIFlags(MachineInstr::FrameSetup)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008071
8072 MI->eraseFromParent();
8073 return MBB;
8074}
8075
8076MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008077ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
8078 MachineBasicBlock *MBB) const {
8079 DebugLoc DL = MI->getDebugLoc();
8080 MachineFunction *MF = MBB->getParent();
8081 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8082
8083 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
Saleem Abdulrasool750a90d2016-03-25 19:48:06 +00008084 MF->insert(++MBB->getIterator(), ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008085 ContBB->splice(ContBB->begin(), MBB,
8086 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008087 ContBB->transferSuccessorsAndUpdatePHIs(MBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008088
8089 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8090 MF->push_back(TrapBB);
8091 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
8092 MBB->addSuccessor(TrapBB);
8093
8094 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
8095 .addReg(MI->getOperand(0).getReg())
8096 .addMBB(TrapBB);
Saleem Abdulrasool0dab98d2016-03-25 00:34:11 +00008097 AddDefaultPred(BuildMI(*MBB, MI, DL, TII->get(ARM::t2B)).addMBB(ContBB));
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008098 MBB->addSuccessor(ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008099
8100 MI->eraseFromParent();
8101 return ContBB;
8102}
8103
8104MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008105ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008106 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008107 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00008108 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008109 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00008110 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008111 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00008112 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00008113 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008114 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008115 // The Thumb2 pre-indexed stores have the same MI operands, they just
8116 // define them differently in the .td files from the isel patterns, so
8117 // they need pseudos.
8118 case ARM::t2STR_preidx:
8119 MI->setDesc(TII->get(ARM::t2STR_PRE));
8120 return BB;
8121 case ARM::t2STRB_preidx:
8122 MI->setDesc(TII->get(ARM::t2STRB_PRE));
8123 return BB;
8124 case ARM::t2STRH_preidx:
8125 MI->setDesc(TII->get(ARM::t2STRH_PRE));
8126 return BB;
8127
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008128 case ARM::STRi_preidx:
8129 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00008130 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008131 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
8132 // Decode the offset.
8133 unsigned Offset = MI->getOperand(4).getImm();
8134 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8135 Offset = ARM_AM::getAM2Offset(Offset);
8136 if (isSub)
8137 Offset = -Offset;
8138
Jim Grosbachf402f692011-08-12 21:02:34 +00008139 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008140 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008141 .addOperand(MI->getOperand(0)) // Rn_wb
8142 .addOperand(MI->getOperand(1)) // Rt
8143 .addOperand(MI->getOperand(2)) // Rn
8144 .addImm(Offset) // offset (skip GPR==zero_reg)
8145 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00008146 .addOperand(MI->getOperand(6))
8147 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008148 MI->eraseFromParent();
8149 return BB;
8150 }
8151 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008152 case ARM::STRBr_preidx:
8153 case ARM::STRH_preidx: {
8154 unsigned NewOpc;
8155 switch (MI->getOpcode()) {
8156 default: llvm_unreachable("unexpected opcode!");
8157 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8158 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8159 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8160 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008161 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
8162 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
8163 MIB.addOperand(MI->getOperand(i));
8164 MI->eraseFromParent();
8165 return BB;
8166 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008167
Evan Chengbb2af352009-08-12 05:17:19 +00008168 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008169 // To "insert" a SELECT_CC instruction, we actually have to insert the
8170 // diamond control-flow pattern. The incoming instruction knows the
8171 // destination vreg to set, the condition code register to branch on, the
8172 // true/false values to select between, and a branch opcode to use.
8173 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008174 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008175
8176 // thisMBB:
8177 // ...
8178 // TrueVal = ...
8179 // cmpTY ccX, r1, r2
8180 // bCC copy1MBB
8181 // fallthrough --> copy0MBB
8182 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008183 MachineFunction *F = BB->getParent();
8184 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8185 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008186 F->insert(It, copy0MBB);
8187 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008188
8189 // Transfer the remainder of BB and its successor edges to sinkMBB.
8190 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008191 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008192 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8193
Dan Gohmanf4f04102010-07-06 15:49:48 +00008194 BB->addSuccessor(copy0MBB);
8195 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008196
Dan Gohman34396292010-07-06 20:24:04 +00008197 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
8198 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
8199
Evan Cheng10043e22007-01-19 07:51:42 +00008200 // copy0MBB:
8201 // %FalseValue = ...
8202 // # fallthrough to sinkMBB
8203 BB = copy0MBB;
8204
8205 // Update machine-CFG edges
8206 BB->addSuccessor(sinkMBB);
8207
8208 // sinkMBB:
8209 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8210 // ...
8211 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008212 BuildMI(*BB, BB->begin(), dl,
8213 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00008214 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8215 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8216
Dan Gohman34396292010-07-06 20:24:04 +00008217 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008218 return BB;
8219 }
Evan Chengb972e562009-08-07 00:34:42 +00008220
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008221 case ARM::BCCi64:
8222 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008223 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008224 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008225
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008226 // Compare both parts that make up the double comparison separately for
8227 // equality.
8228 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
8229
8230 unsigned LHS1 = MI->getOperand(1).getReg();
8231 unsigned LHS2 = MI->getOperand(2).getReg();
8232 if (RHSisZero) {
8233 AddDefaultPred(BuildMI(BB, dl,
8234 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8235 .addReg(LHS1).addImm(0));
8236 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8237 .addReg(LHS2).addImm(0)
8238 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8239 } else {
8240 unsigned RHS1 = MI->getOperand(3).getReg();
8241 unsigned RHS2 = MI->getOperand(4).getReg();
8242 AddDefaultPred(BuildMI(BB, dl,
8243 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8244 .addReg(LHS1).addReg(RHS1));
8245 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8246 .addReg(LHS2).addReg(RHS2)
8247 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8248 }
8249
8250 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
8251 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
8252 if (MI->getOperand(0).getImm() == ARMCC::NE)
8253 std::swap(destMBB, exitMBB);
8254
8255 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8256 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008257 if (isThumb2)
8258 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8259 else
8260 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008261
8262 MI->eraseFromParent(); // The pseudo instruction is gone now.
8263 return BB;
8264 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008265
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008266 case ARM::Int_eh_sjlj_setjmp:
8267 case ARM::Int_eh_sjlj_setjmp_nofp:
8268 case ARM::tInt_eh_sjlj_setjmp:
8269 case ARM::t2Int_eh_sjlj_setjmp:
8270 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008271 return BB;
8272
8273 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008274 EmitSjLjDispatchBlock(MI, BB);
8275 return BB;
8276
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008277 case ARM::ABS:
8278 case ARM::t2ABS: {
8279 // To insert an ABS instruction, we have to insert the
8280 // diamond control-flow pattern. The incoming instruction knows the
8281 // source vreg to test against 0, the destination vreg to set,
8282 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008283 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008284 // It transforms
8285 // V1 = ABS V0
8286 // into
8287 // V2 = MOVS V0
8288 // BCC (branch to SinkBB if V0 >= 0)
8289 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008290 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008291 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008292 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008293 MachineFunction *Fn = BB->getParent();
8294 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8295 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8296 Fn->insert(BBI, RSBBB);
8297 Fn->insert(BBI, SinkBB);
8298
8299 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8300 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008301 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008302 bool isThumb2 = Subtarget->isThumb2();
8303 MachineRegisterInfo &MRI = Fn->getRegInfo();
8304 // In Thumb mode S must not be specified if source register is the SP or
8305 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008306 unsigned NewRsbDstReg =
8307 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008308
8309 // Transfer the remainder of BB and its successor edges to sinkMBB.
8310 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008311 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008312 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8313
8314 BB->addSuccessor(RSBBB);
8315 BB->addSuccessor(SinkBB);
8316
8317 // fall through to SinkMBB
8318 RSBBB->addSuccessor(SinkBB);
8319
Manman Rene0763c72012-06-15 21:32:12 +00008320 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008321 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008322 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8323 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008324
8325 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008326 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008327 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8328 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8329
8330 // insert rsbri in RSBBB
8331 // Note: BCC and rsbri will be converted into predicated rsbmi
8332 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008333 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008334 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008335 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008336 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8337
Andrew Trick3f07c422011-10-18 18:40:53 +00008338 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008339 // reuse ABSDstReg to not change uses of ABS instruction
8340 BuildMI(*SinkBB, SinkBB->begin(), dl,
8341 TII->get(ARM::PHI), ABSDstReg)
8342 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008343 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008344
8345 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008346 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008347
8348 // return last added BB
8349 return SinkBB;
8350 }
Manman Rene8735522012-06-01 19:33:18 +00008351 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008352 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008353 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008354 case ARM::WIN__CHKSTK:
8355 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008356 case ARM::WIN__DBZCHK:
8357 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008358 }
8359}
8360
Scott Douglass953f9082015-10-05 14:49:54 +00008361/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8362/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8363/// instead of as a custom inserter because we need the use list from the SDNode.
8364static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8365 MachineInstr *MI, const SDNode *Node) {
8366 bool isThumb1 = Subtarget->isThumb1Only();
8367
8368 DebugLoc DL = MI->getDebugLoc();
8369 MachineFunction *MF = MI->getParent()->getParent();
8370 MachineRegisterInfo &MRI = MF->getRegInfo();
8371 MachineInstrBuilder MIB(*MF, MI);
8372
8373 // If the new dst/src is unused mark it as dead.
8374 if (!Node->hasAnyUseOfValue(0)) {
8375 MI->getOperand(0).setIsDead(true);
8376 }
8377 if (!Node->hasAnyUseOfValue(1)) {
8378 MI->getOperand(1).setIsDead(true);
8379 }
8380
8381 // The MEMCPY both defines and kills the scratch registers.
8382 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8383 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8384 : &ARM::GPRRegClass);
8385 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8386 }
8387}
8388
Evan Chenge6fba772011-08-30 19:09:48 +00008389void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8390 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008391 if (MI->getOpcode() == ARM::MEMCPY) {
8392 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8393 return;
8394 }
8395
Evan Cheng7f8e5632011-12-07 07:15:52 +00008396 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008397 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8398 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8399 // operand is still set to noreg. If needed, set the optional operand's
8400 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008401 //
Andrew Trick88b24502011-10-18 19:18:52 +00008402 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008403
Andrew Trick924123a2011-09-21 02:20:46 +00008404 // Rename pseudo opcodes.
8405 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8406 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008407 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008408 MCID = &TII->get(NewOpc);
8409
8410 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8411 "converted opcode should be the same except for cc_out");
8412
8413 MI->setDesc(*MCID);
8414
8415 // Add the optional cc_out operand
8416 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008417 }
Andrew Trick88b24502011-10-18 19:18:52 +00008418 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008419
8420 // Any ARM instruction that sets the 's' bit should specify an optional
8421 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008422 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008423 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008424 return;
8425 }
Andrew Trick924123a2011-09-21 02:20:46 +00008426 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8427 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008428 bool definesCPSR = false;
8429 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008430 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008431 i != e; ++i) {
8432 const MachineOperand &MO = MI->getOperand(i);
8433 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8434 definesCPSR = true;
8435 if (MO.isDead())
8436 deadCPSR = true;
8437 MI->RemoveOperand(i);
8438 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008439 }
8440 }
Andrew Trick8586e622011-09-20 03:17:40 +00008441 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008442 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008443 return;
8444 }
8445 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008446 if (deadCPSR) {
8447 assert(!MI->getOperand(ccOutIdx).getReg() &&
8448 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008449 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008450 }
Andrew Trick8586e622011-09-20 03:17:40 +00008451
Andrew Trick924123a2011-09-21 02:20:46 +00008452 // If this instruction was defined with an optional CPSR def and its dag node
8453 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008454 MachineOperand &MO = MI->getOperand(ccOutIdx);
8455 MO.setReg(ARM::CPSR);
8456 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008457}
8458
Evan Cheng10043e22007-01-19 07:51:42 +00008459//===----------------------------------------------------------------------===//
8460// ARM Optimization Hooks
8461//===----------------------------------------------------------------------===//
8462
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008463// Helper function that checks if N is a null or all ones constant.
8464static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008465 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008466}
8467
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008468// Return true if N is conditionally 0 or all ones.
8469// Detects these expressions where cc is an i1 value:
8470//
8471// (select cc 0, y) [AllOnes=0]
8472// (select cc y, 0) [AllOnes=0]
8473// (zext cc) [AllOnes=0]
8474// (sext cc) [AllOnes=0/1]
8475// (select cc -1, y) [AllOnes=1]
8476// (select cc y, -1) [AllOnes=1]
8477//
8478// Invert is set when N is the null/all ones constant when CC is false.
8479// OtherOp is set to the alternative value of N.
8480static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8481 SDValue &CC, bool &Invert,
8482 SDValue &OtherOp,
8483 SelectionDAG &DAG) {
8484 switch (N->getOpcode()) {
8485 default: return false;
8486 case ISD::SELECT: {
8487 CC = N->getOperand(0);
8488 SDValue N1 = N->getOperand(1);
8489 SDValue N2 = N->getOperand(2);
8490 if (isZeroOrAllOnes(N1, AllOnes)) {
8491 Invert = false;
8492 OtherOp = N2;
8493 return true;
8494 }
8495 if (isZeroOrAllOnes(N2, AllOnes)) {
8496 Invert = true;
8497 OtherOp = N1;
8498 return true;
8499 }
8500 return false;
8501 }
8502 case ISD::ZERO_EXTEND:
8503 // (zext cc) can never be the all ones value.
8504 if (AllOnes)
8505 return false;
8506 // Fall through.
8507 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008508 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008509 EVT VT = N->getValueType(0);
8510 CC = N->getOperand(0);
8511 if (CC.getValueType() != MVT::i1)
8512 return false;
8513 Invert = !AllOnes;
8514 if (AllOnes)
8515 // When looking for an AllOnes constant, N is an sext, and the 'other'
8516 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008517 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008518 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8519 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008520 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008521 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008522 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8523 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008524 return true;
8525 }
8526 }
8527}
8528
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008529// Combine a constant select operand into its use:
8530//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008531// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8532// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8533// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8534// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8535// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008536//
8537// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008538// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008539//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008540// Also recognize sext/zext from i1:
8541//
8542// (add (zext cc), x) -> (select cc (add x, 1), x)
8543// (add (sext cc), x) -> (select cc (add x, -1), x)
8544//
8545// These transformations eventually create predicated instructions.
8546//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008547// @param N The node to transform.
8548// @param Slct The N operand that is a select.
8549// @param OtherOp The other N operand (x above).
8550// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008551// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008552// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008553static
8554SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008555 TargetLowering::DAGCombinerInfo &DCI,
8556 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008557 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008558 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008559 SDValue NonConstantVal;
8560 SDValue CCOp;
8561 bool SwapSelectOps;
8562 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8563 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008564 return SDValue();
8565
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008566 // Slct is now know to be the desired identity constant when CC is true.
8567 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008568 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008569 OtherOp, NonConstantVal);
8570 // Unless SwapSelectOps says CC should be false.
8571 if (SwapSelectOps)
8572 std::swap(TrueVal, FalseVal);
8573
Andrew Trickef9de2a2013-05-25 02:42:55 +00008574 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008575 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008576}
8577
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008578// Attempt combineSelectAndUse on each operand of a commutative operator N.
8579static
8580SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8581 TargetLowering::DAGCombinerInfo &DCI) {
8582 SDValue N0 = N->getOperand(0);
8583 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008584 if (N0.getNode()->hasOneUse())
8585 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008586 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008587 if (N1.getNode()->hasOneUse())
8588 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008589 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008590 return SDValue();
8591}
8592
Eric Christopher1b8b94192011-06-29 21:10:36 +00008593// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008594// (only after legalization).
8595static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8596 TargetLowering::DAGCombinerInfo &DCI,
8597 const ARMSubtarget *Subtarget) {
8598
8599 // Only perform optimization if after legalize, and if NEON is available. We
8600 // also expected both operands to be BUILD_VECTORs.
8601 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8602 || N0.getOpcode() != ISD::BUILD_VECTOR
8603 || N1.getOpcode() != ISD::BUILD_VECTOR)
8604 return SDValue();
8605
8606 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8607 EVT VT = N->getValueType(0);
8608 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8609 return SDValue();
8610
8611 // Check that the vector operands are of the right form.
8612 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8613 // operands, where N is the size of the formed vector.
8614 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8615 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008616
8617 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008618 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008619 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008620 SDValue Vec = N0->getOperand(0)->getOperand(0);
8621 SDNode *V = Vec.getNode();
8622 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008623
Eric Christopher1b8b94192011-06-29 21:10:36 +00008624 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008625 // check to see if each of their operands are an EXTRACT_VECTOR with
8626 // the same vector and appropriate index.
8627 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8628 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8629 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008630
Tanya Lattnere9e67052011-06-14 23:48:48 +00008631 SDValue ExtVec0 = N0->getOperand(i);
8632 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008633
Tanya Lattnere9e67052011-06-14 23:48:48 +00008634 // First operand is the vector, verify its the same.
8635 if (V != ExtVec0->getOperand(0).getNode() ||
8636 V != ExtVec1->getOperand(0).getNode())
8637 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008638
Tanya Lattnere9e67052011-06-14 23:48:48 +00008639 // Second is the constant, verify its correct.
8640 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8641 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008642
Tanya Lattnere9e67052011-06-14 23:48:48 +00008643 // For the constant, we want to see all the even or all the odd.
8644 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8645 || C1->getZExtValue() != nextIndex+1)
8646 return SDValue();
8647
8648 // Increment index.
8649 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008650 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008651 return SDValue();
8652 }
8653
8654 // Create VPADDL node.
8655 SelectionDAG &DAG = DCI.DAG;
8656 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008657
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008658 SDLoc dl(N);
8659
Tanya Lattnere9e67052011-06-14 23:48:48 +00008660 // Build operand list.
8661 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008662 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008663 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008664
8665 // Input is the vector.
8666 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008667
Tanya Lattnere9e67052011-06-14 23:48:48 +00008668 // Get widened type and narrowed type.
8669 MVT widenType;
8670 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00008671
Silviu Barangaa3106e62014-04-03 10:44:27 +00008672 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8673 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008674 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8675 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8676 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8677 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008678 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008679 }
8680
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008681 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008682 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008683 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008684}
8685
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008686static SDValue findMUL_LOHI(SDValue V) {
8687 if (V->getOpcode() == ISD::UMUL_LOHI ||
8688 V->getOpcode() == ISD::SMUL_LOHI)
8689 return V;
8690 return SDValue();
8691}
8692
8693static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8694 TargetLowering::DAGCombinerInfo &DCI,
8695 const ARMSubtarget *Subtarget) {
8696
8697 if (Subtarget->isThumb1Only()) return SDValue();
8698
8699 // Only perform the checks after legalize when the pattern is available.
8700 if (DCI.isBeforeLegalize()) return SDValue();
8701
8702 // Look for multiply add opportunities.
8703 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8704 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8705 // a glue link from the first add to the second add.
8706 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8707 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008708 // UMUL_LOHI
8709 // / :lo \ :hi
8710 // / \ [no multiline comment]
8711 // loAdd -> ADDE |
8712 // \ :glue /
8713 // \ /
8714 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008715 //
8716 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8717 SDValue AddcOp0 = AddcNode->getOperand(0);
8718 SDValue AddcOp1 = AddcNode->getOperand(1);
8719
8720 // Check if the two operands are from the same mul_lohi node.
8721 if (AddcOp0.getNode() == AddcOp1.getNode())
8722 return SDValue();
8723
8724 assert(AddcNode->getNumValues() == 2 &&
8725 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008726 "Expect ADDC with two result values. First: i32");
8727
8728 // Check that we have a glued ADDC node.
8729 if (AddcNode->getValueType(1) != MVT::Glue)
8730 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008731
8732 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8733 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8734 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8735 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8736 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8737 return SDValue();
8738
8739 // Look for the glued ADDE.
8740 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008741 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008742 return SDValue();
8743
8744 // Make sure it is really an ADDE.
8745 if (AddeNode->getOpcode() != ISD::ADDE)
8746 return SDValue();
8747
8748 assert(AddeNode->getNumOperands() == 3 &&
8749 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8750 "ADDE node has the wrong inputs");
8751
8752 // Check for the triangle shape.
8753 SDValue AddeOp0 = AddeNode->getOperand(0);
8754 SDValue AddeOp1 = AddeNode->getOperand(1);
8755
8756 // Make sure that the ADDE operands are not coming from the same node.
8757 if (AddeOp0.getNode() == AddeOp1.getNode())
8758 return SDValue();
8759
8760 // Find the MUL_LOHI node walking up ADDE's operands.
8761 bool IsLeftOperandMUL = false;
8762 SDValue MULOp = findMUL_LOHI(AddeOp0);
8763 if (MULOp == SDValue())
8764 MULOp = findMUL_LOHI(AddeOp1);
8765 else
8766 IsLeftOperandMUL = true;
8767 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008768 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008769
8770 // Figure out the right opcode.
8771 unsigned Opc = MULOp->getOpcode();
8772 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8773
8774 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008775 SDValue* HiAdd = nullptr;
8776 SDValue* LoMul = nullptr;
8777 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008778
Jyoti Allurf1d70502015-01-23 09:10:03 +00008779 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8780 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8781 return SDValue();
8782
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008783 if (IsLeftOperandMUL)
8784 HiAdd = &AddeOp1;
8785 else
8786 HiAdd = &AddeOp0;
8787
8788
Jyoti Allurf1d70502015-01-23 09:10:03 +00008789 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8790 // whose low result is fed to the ADDC we are checking.
8791
8792 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008793 LoMul = &AddcOp0;
8794 LowAdd = &AddcOp1;
8795 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008796 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008797 LoMul = &AddcOp1;
8798 LowAdd = &AddcOp0;
8799 }
8800
Craig Topper062a2ba2014-04-25 05:30:21 +00008801 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008802 return SDValue();
8803
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008804 // Create the merged node.
8805 SelectionDAG &DAG = DCI.DAG;
8806
8807 // Build operand list.
8808 SmallVector<SDValue, 8> Ops;
8809 Ops.push_back(LoMul->getOperand(0));
8810 Ops.push_back(LoMul->getOperand(1));
8811 Ops.push_back(*LowAdd);
8812 Ops.push_back(*HiAdd);
8813
Andrew Trickef9de2a2013-05-25 02:42:55 +00008814 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008815 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008816
8817 // Replace the ADDs' nodes uses by the MLA node's values.
8818 SDValue HiMLALResult(MLALNode.getNode(), 1);
8819 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8820
8821 SDValue LoMLALResult(MLALNode.getNode(), 0);
8822 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8823
8824 // Return original node to notify the driver to stop replacing.
8825 SDValue resNode(AddcNode, 0);
8826 return resNode;
8827}
8828
8829/// PerformADDCCombine - Target-specific dag combine transform from
8830/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8831static SDValue PerformADDCCombine(SDNode *N,
8832 TargetLowering::DAGCombinerInfo &DCI,
8833 const ARMSubtarget *Subtarget) {
8834
8835 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8836
8837}
8838
Bob Wilson728eb292010-07-29 20:34:14 +00008839/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8840/// operands N0 and N1. This is a helper for PerformADDCombine that is
8841/// called with the default operands, and if that fails, with commuted
8842/// operands.
8843static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008844 TargetLowering::DAGCombinerInfo &DCI,
8845 const ARMSubtarget *Subtarget){
8846
8847 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008848 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00008849 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008850
Chris Lattner4147f082009-03-12 06:52:53 +00008851 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008852 if (N0.getNode()->hasOneUse())
8853 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
8854 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00008855 return SDValue();
8856}
8857
Bob Wilson728eb292010-07-29 20:34:14 +00008858/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8859///
8860static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008861 TargetLowering::DAGCombinerInfo &DCI,
8862 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008863 SDValue N0 = N->getOperand(0);
8864 SDValue N1 = N->getOperand(1);
8865
8866 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008867 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00008868 return Result;
8869
8870 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008871 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008872}
8873
Chris Lattner4147f082009-03-12 06:52:53 +00008874/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008875///
Chris Lattner4147f082009-03-12 06:52:53 +00008876static SDValue PerformSUBCombine(SDNode *N,
8877 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008878 SDValue N0 = N->getOperand(0);
8879 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008880
Chris Lattner4147f082009-03-12 06:52:53 +00008881 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008882 if (N1.getNode()->hasOneUse())
8883 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
8884 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00008885
Chris Lattner4147f082009-03-12 06:52:53 +00008886 return SDValue();
8887}
8888
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008889/// PerformVMULCombine
8890/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8891/// special multiplier accumulator forwarding.
8892/// vmul d3, d0, d2
8893/// vmla d3, d1, d2
8894/// is faster than
8895/// vadd d3, d0, d1
8896/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008897// However, for (A + B) * (A + B),
8898// vadd d2, d0, d1
8899// vmul d3, d0, d2
8900// vmla d3, d1, d2
8901// is slower than
8902// vadd d2, d0, d1
8903// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008904static SDValue PerformVMULCombine(SDNode *N,
8905 TargetLowering::DAGCombinerInfo &DCI,
8906 const ARMSubtarget *Subtarget) {
8907 if (!Subtarget->hasVMLxForwarding())
8908 return SDValue();
8909
8910 SelectionDAG &DAG = DCI.DAG;
8911 SDValue N0 = N->getOperand(0);
8912 SDValue N1 = N->getOperand(1);
8913 unsigned Opcode = N0.getOpcode();
8914 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8915 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008916 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008917 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8918 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8919 return SDValue();
8920 std::swap(N0, N1);
8921 }
8922
Weiming Zhao2052f482013-09-25 23:12:06 +00008923 if (N0 == N1)
8924 return SDValue();
8925
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008926 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008927 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008928 SDValue N00 = N0->getOperand(0);
8929 SDValue N01 = N0->getOperand(1);
8930 return DAG.getNode(Opcode, DL, VT,
8931 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8932 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8933}
8934
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008935static SDValue PerformMULCombine(SDNode *N,
8936 TargetLowering::DAGCombinerInfo &DCI,
8937 const ARMSubtarget *Subtarget) {
8938 SelectionDAG &DAG = DCI.DAG;
8939
8940 if (Subtarget->isThumb1Only())
8941 return SDValue();
8942
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008943 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8944 return SDValue();
8945
8946 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008947 if (VT.is64BitVector() || VT.is128BitVector())
8948 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008949 if (VT != MVT::i32)
8950 return SDValue();
8951
8952 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8953 if (!C)
8954 return SDValue();
8955
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008956 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008957 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008958
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008959 ShiftAmt = ShiftAmt & (32 - 1);
8960 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008961 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008962
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008963 SDValue Res;
8964 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008965
8966 if (MulAmt >= 0) {
8967 if (isPowerOf2_32(MulAmt - 1)) {
8968 // (mul x, 2^N + 1) => (add (shl x, N), x)
8969 Res = DAG.getNode(ISD::ADD, DL, VT,
8970 V,
8971 DAG.getNode(ISD::SHL, DL, VT,
8972 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008973 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008974 MVT::i32)));
8975 } else if (isPowerOf2_32(MulAmt + 1)) {
8976 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8977 Res = DAG.getNode(ISD::SUB, DL, VT,
8978 DAG.getNode(ISD::SHL, DL, VT,
8979 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008980 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008981 MVT::i32)),
8982 V);
8983 } else
8984 return SDValue();
8985 } else {
8986 uint64_t MulAmtAbs = -MulAmt;
8987 if (isPowerOf2_32(MulAmtAbs + 1)) {
8988 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8989 Res = DAG.getNode(ISD::SUB, DL, VT,
8990 V,
8991 DAG.getNode(ISD::SHL, DL, VT,
8992 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008993 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008994 MVT::i32)));
8995 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8996 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8997 Res = DAG.getNode(ISD::ADD, DL, VT,
8998 V,
8999 DAG.getNode(ISD::SHL, DL, VT,
9000 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009001 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009002 MVT::i32)));
9003 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009004 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009005
9006 } else
9007 return SDValue();
9008 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009009
9010 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009011 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009012 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009013
9014 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009015 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009016 return SDValue();
9017}
9018
Owen Anderson30c48922010-11-05 19:27:46 +00009019static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00009020 TargetLowering::DAGCombinerInfo &DCI,
9021 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00009022
Owen Anderson30c48922010-11-05 19:27:46 +00009023 // Attempt to use immediate-form VBIC
9024 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009025 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00009026 EVT VT = N->getValueType(0);
9027 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009028
Tanya Lattner266792a2011-04-07 15:24:20 +00009029 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9030 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009031
Owen Anderson30c48922010-11-05 19:27:46 +00009032 APInt SplatBits, SplatUndef;
9033 unsigned SplatBitSize;
9034 bool HasAnyUndefs;
9035 if (BVN &&
9036 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9037 if (SplatBitSize <= 64) {
9038 EVT VbicVT;
9039 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
9040 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009041 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009042 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00009043 if (Val.getNode()) {
9044 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009045 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00009046 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009047 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00009048 }
9049 }
9050 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009051
Evan Chenge87681c2012-02-23 01:19:06 +00009052 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009053 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009054 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009055 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009056 }
9057
Owen Anderson30c48922010-11-05 19:27:46 +00009058 return SDValue();
9059}
9060
Jim Grosbach11013ed2010-07-16 23:05:05 +00009061/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
9062static SDValue PerformORCombine(SDNode *N,
9063 TargetLowering::DAGCombinerInfo &DCI,
9064 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009065 // Attempt to use immediate-form VORR
9066 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009067 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009068 EVT VT = N->getValueType(0);
9069 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009070
Tanya Lattner266792a2011-04-07 15:24:20 +00009071 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9072 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009073
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009074 APInt SplatBits, SplatUndef;
9075 unsigned SplatBitSize;
9076 bool HasAnyUndefs;
9077 if (BVN && Subtarget->hasNEON() &&
9078 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9079 if (SplatBitSize <= 64) {
9080 EVT VorrVT;
9081 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9082 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009083 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009084 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009085 if (Val.getNode()) {
9086 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009087 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009088 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009089 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009090 }
9091 }
9092 }
9093
Evan Chenge87681c2012-02-23 01:19:06 +00009094 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009095 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009096 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009097 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009098 }
9099
Nadav Rotem3a94c542012-08-13 18:52:44 +00009100 // The code below optimizes (or (and X, Y), Z).
9101 // The AND operand needs to have a single user to make these optimizations
9102 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009103 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009104 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009105 return SDValue();
9106 SDValue N1 = N->getOperand(1);
9107
9108 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9109 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9110 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9111 APInt SplatUndef;
9112 unsigned SplatBitSize;
9113 bool HasAnyUndefs;
9114
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009115 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009116 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009117 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9118 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009119 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009120 HasAnyUndefs) && !HasAnyUndefs) {
9121 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9122 HasAnyUndefs) && !HasAnyUndefs) {
9123 // Ensure that the bit width of the constants are the same and that
9124 // the splat arguments are logical inverses as per the pattern we
9125 // are trying to simplify.
9126 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9127 SplatBits0 == ~SplatBits1) {
9128 // Canonicalize the vector type to make instruction selection
9129 // simpler.
9130 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9131 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9132 N0->getOperand(1),
9133 N0->getOperand(0),
9134 N1->getOperand(0));
9135 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9136 }
9137 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009138 }
9139 }
9140
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009141 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9142 // reasonable.
9143
Jim Grosbach11013ed2010-07-16 23:05:05 +00009144 // BFI is only available on V6T2+
9145 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9146 return SDValue();
9147
Andrew Trickef9de2a2013-05-25 02:42:55 +00009148 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009149 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009150 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009151 //
9152 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009153 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009154 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009155 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009156 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009157 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009158
Jim Grosbach11013ed2010-07-16 23:05:05 +00009159 if (VT != MVT::i32)
9160 return SDValue();
9161
Evan Cheng2e51bb42010-12-13 20:32:54 +00009162 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009163
Jim Grosbach11013ed2010-07-16 23:05:05 +00009164 // The value and the mask need to be constants so we can verify this is
9165 // actually a bitfield set. If the mask is 0xffff, we can do better
9166 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009167 SDValue MaskOp = N0.getOperand(1);
9168 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9169 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009170 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009171 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009172 if (Mask == 0xffff)
9173 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009174 SDValue Res;
9175 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009176 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9177 if (N1C) {
9178 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009179 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009180 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009181
Evan Cheng34345752010-12-11 04:11:38 +00009182 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009183 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009184
Evan Cheng2e51bb42010-12-13 20:32:54 +00009185 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009186 DAG.getConstant(Val, DL, MVT::i32),
9187 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009188
9189 // Do not add new nodes to DAG combiner worklist.
9190 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009191 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009192 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009193 } else if (N1.getOpcode() == ISD::AND) {
9194 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009195 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9196 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009197 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009198 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009199
Eric Christopherd5530962011-03-26 01:21:03 +00009200 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9201 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009202 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009203 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009204 // The pack halfword instruction works better for masks that fit it,
9205 // so use that when it's available.
9206 if (Subtarget->hasT2ExtractPack() &&
9207 (Mask == 0xffff || Mask == 0xffff0000))
9208 return SDValue();
9209 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009210 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009211 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009212 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009213 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009214 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009215 // Do not add new nodes to DAG combiner worklist.
9216 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009217 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009218 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009219 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009220 // The pack halfword instruction works better for masks that fit it,
9221 // so use that when it's available.
9222 if (Subtarget->hasT2ExtractPack() &&
9223 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9224 return SDValue();
9225 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009226 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009227 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009228 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009229 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009230 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009231 // Do not add new nodes to DAG combiner worklist.
9232 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009233 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009234 }
9235 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009236
Evan Cheng2e51bb42010-12-13 20:32:54 +00009237 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9238 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9239 ARM::isBitFieldInvertedMask(~Mask)) {
9240 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9241 // where lsb(mask) == #shamt and masked bits of B are known zero.
9242 SDValue ShAmt = N00.getOperand(1);
9243 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009244 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009245 if (ShAmtC != LSB)
9246 return SDValue();
9247
9248 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009249 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009250
9251 // Do not add new nodes to DAG combiner worklist.
9252 DCI.CombineTo(N, Res, false);
9253 }
9254
Jim Grosbach11013ed2010-07-16 23:05:05 +00009255 return SDValue();
9256}
9257
Evan Chenge87681c2012-02-23 01:19:06 +00009258static SDValue PerformXORCombine(SDNode *N,
9259 TargetLowering::DAGCombinerInfo &DCI,
9260 const ARMSubtarget *Subtarget) {
9261 EVT VT = N->getValueType(0);
9262 SelectionDAG &DAG = DCI.DAG;
9263
9264 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9265 return SDValue();
9266
9267 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009268 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009269 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009270 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009271 }
9272
9273 return SDValue();
9274}
9275
James Molloyce12c922015-11-11 15:40:40 +00009276// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9277// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9278// their position in "to" (Rd).
9279static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9280 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009281
James Molloyce12c922015-11-11 15:40:40 +00009282 SDValue From = N->getOperand(1);
9283 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9284 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9285
9286 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9287 // #C in the base of the SHR.
9288 if (From->getOpcode() == ISD::SRL &&
9289 isa<ConstantSDNode>(From->getOperand(1))) {
9290 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9291 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9292 FromMask <<= Shift.getLimitedValue(31);
9293 From = From->getOperand(0);
9294 }
9295
9296 return From;
9297}
9298
9299// If A and B contain one contiguous set of bits, does A | B == A . B?
9300//
9301// Neither A nor B must be zero.
9302static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9303 unsigned LastActiveBitInA = A.countTrailingZeros();
9304 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9305 return LastActiveBitInA - 1 == FirstActiveBitInB;
9306}
9307
9308static SDValue FindBFIToCombineWith(SDNode *N) {
9309 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9310 // if one exists.
9311 APInt ToMask, FromMask;
9312 SDValue From = ParseBFI(N, ToMask, FromMask);
9313 SDValue To = N->getOperand(0);
9314
9315 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9316 // aren't compatible, but not if they set the same bit in their destination as
9317 // we do (or that of any BFI we're going to combine with).
9318 SDValue V = To;
9319 APInt CombinedToMask = ToMask;
9320 while (V.getOpcode() == ARMISD::BFI) {
9321 APInt NewToMask, NewFromMask;
9322 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9323 if (NewFrom != From) {
9324 // This BFI has a different base. Keep going.
9325 CombinedToMask |= NewToMask;
9326 V = V.getOperand(0);
9327 continue;
9328 }
9329
9330 // Do the written bits conflict with any we've seen so far?
9331 if ((NewToMask & CombinedToMask).getBoolValue())
9332 // Conflicting bits - bail out because going further is unsafe.
9333 return SDValue();
9334
9335 // Are the new bits contiguous when combined with the old bits?
9336 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9337 BitsProperlyConcatenate(FromMask, NewFromMask))
9338 return V;
9339 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9340 BitsProperlyConcatenate(NewFromMask, FromMask))
9341 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009342
James Molloyce12c922015-11-11 15:40:40 +00009343 // We've seen a write to some bits, so track it.
9344 CombinedToMask |= NewToMask;
9345 // Keep going...
9346 V = V.getOperand(0);
9347 }
9348
9349 return SDValue();
9350}
9351
Evan Chengc1778132010-12-14 03:22:07 +00009352static SDValue PerformBFICombine(SDNode *N,
9353 TargetLowering::DAGCombinerInfo &DCI) {
9354 SDValue N1 = N->getOperand(1);
9355 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009356 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9357 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009358 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9359 if (!N11C)
9360 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009361 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009362 unsigned LSB = countTrailingZeros(~InvMask);
9363 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009364 assert(Width <
9365 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009366 "undefined behavior");
9367 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009368 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009369 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009370 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009371 N->getOperand(0), N1.getOperand(0),
9372 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009373 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9374 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9375 // Keep track of any consecutive bits set that all come from the same base
9376 // value. We can combine these together into a single BFI.
9377 SDValue CombineBFI = FindBFIToCombineWith(N);
9378 if (CombineBFI == SDValue())
9379 return SDValue();
9380
9381 // We've found a BFI.
9382 APInt ToMask1, FromMask1;
9383 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9384
9385 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009386 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9387 assert(From1 == From2);
9388 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009389
James Molloyce12c922015-11-11 15:40:40 +00009390 // First, unlink CombineBFI.
9391 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9392 // Then create a new BFI, combining the two together.
9393 APInt NewFromMask = FromMask1 | FromMask2;
9394 APInt NewToMask = ToMask1 | ToMask2;
9395
9396 EVT VT = N->getValueType(0);
9397 SDLoc dl(N);
9398
9399 if (NewFromMask[0] == 0)
9400 From1 = DCI.DAG.getNode(
9401 ISD::SRL, dl, VT, From1,
9402 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9403 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9404 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009405 }
9406 return SDValue();
9407}
9408
Bob Wilson22806742010-09-22 22:09:21 +00009409/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9410/// ARMISD::VMOVRRD.
9411static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009412 TargetLowering::DAGCombinerInfo &DCI,
9413 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009414 // vmovrrd(vmovdrr x, y) -> x,y
9415 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009416 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009417 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009418
9419 // vmovrrd(load f64) -> (load i32), (load i32)
9420 SDNode *InNode = InDouble.getNode();
9421 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9422 InNode->getValueType(0) == MVT::f64 &&
9423 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9424 !cast<LoadSDNode>(InNode)->isVolatile()) {
9425 // TODO: Should this be done for non-FrameIndex operands?
9426 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9427
9428 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009429 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009430 SDValue BasePtr = LD->getBasePtr();
9431 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9432 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009433 LD->isNonTemporal(), LD->isInvariant(),
9434 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009435
9436 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009437 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009438 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9439 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009440 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009441 std::min(4U, LD->getAlignment() / 2));
9442
9443 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009444 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009445 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009446 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009447 return Result;
9448 }
9449
Bob Wilson22806742010-09-22 22:09:21 +00009450 return SDValue();
9451}
9452
9453/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9454/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9455static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9456 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9457 SDValue Op0 = N->getOperand(0);
9458 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009459 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009460 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009461 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009462 Op1 = Op1.getOperand(0);
9463 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9464 Op0.getNode() == Op1.getNode() &&
9465 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009466 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009467 N->getValueType(0), Op0.getOperand(0));
9468 return SDValue();
9469}
9470
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009471/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9472/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9473/// i64 vector to have f64 elements, since the value can then be loaded
9474/// directly into a VFP register.
9475static bool hasNormalLoadOperand(SDNode *N) {
9476 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9477 for (unsigned i = 0; i < NumElts; ++i) {
9478 SDNode *Elt = N->getOperand(i).getNode();
9479 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9480 return true;
9481 }
9482 return false;
9483}
9484
Bob Wilsoncb6db982010-09-17 22:59:05 +00009485/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9486/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009487static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009488 TargetLowering::DAGCombinerInfo &DCI,
9489 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009490 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9491 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9492 // into a pair of GPRs, which is fine when the value is used as a scalar,
9493 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009494 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009495 if (N->getNumOperands() == 2)
9496 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009497 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +00009498
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009499 // Load i64 elements as f64 values so that type legalization does not split
9500 // them up into i32 values.
9501 EVT VT = N->getValueType(0);
9502 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9503 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009504 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009505 SmallVector<SDValue, 8> Ops;
9506 unsigned NumElts = VT.getVectorNumElements();
9507 for (unsigned i = 0; i < NumElts; ++i) {
9508 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9509 Ops.push_back(V);
9510 // Make the DAGCombiner fold the bitcast.
9511 DCI.AddToWorklist(V.getNode());
9512 }
9513 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00009514 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009515 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9516}
9517
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009518/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9519static SDValue
9520PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9521 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9522 // At that time, we may have inserted bitcasts from integer to float.
9523 // If these bitcasts have survived DAGCombine, change the lowering of this
9524 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9525 // force to use floating point types.
9526
9527 // Make sure we can change the type of the vector.
9528 // This is possible iff:
9529 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9530 // 1.1. Vector is used only once.
9531 // 1.2. Use is a bit convert to an integer type.
9532 // 2. The size of its operands are 32-bits (64-bits are not legal).
9533 EVT VT = N->getValueType(0);
9534 EVT EltVT = VT.getVectorElementType();
9535
9536 // Check 1.1. and 2.
9537 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9538 return SDValue();
9539
9540 // By construction, the input type must be float.
9541 assert(EltVT == MVT::f32 && "Unexpected type!");
9542
9543 // Check 1.2.
9544 SDNode *Use = *N->use_begin();
9545 if (Use->getOpcode() != ISD::BITCAST ||
9546 Use->getValueType(0).isFloatingPoint())
9547 return SDValue();
9548
9549 // Check profitability.
9550 // Model is, if more than half of the relevant operands are bitcast from
9551 // i32, turn the build_vector into a sequence of insert_vector_elt.
9552 // Relevant operands are everything that is not statically
9553 // (i.e., at compile time) bitcasted.
9554 unsigned NumOfBitCastedElts = 0;
9555 unsigned NumElts = VT.getVectorNumElements();
9556 unsigned NumOfRelevantElts = NumElts;
9557 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9558 SDValue Elt = N->getOperand(Idx);
9559 if (Elt->getOpcode() == ISD::BITCAST) {
9560 // Assume only bit cast to i32 will go away.
9561 if (Elt->getOperand(0).getValueType() == MVT::i32)
9562 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +00009563 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009564 // Constants are statically casted, thus do not count them as
9565 // relevant operands.
9566 --NumOfRelevantElts;
9567 }
9568
9569 // Check if more than half of the elements require a non-free bitcast.
9570 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9571 return SDValue();
9572
9573 SelectionDAG &DAG = DCI.DAG;
9574 // Create the new vector type.
9575 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9576 // Check if the type is legal.
9577 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9578 if (!TLI.isTypeLegal(VecVT))
9579 return SDValue();
9580
9581 // Combine:
9582 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9583 // => BITCAST INSERT_VECTOR_ELT
9584 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9585 // (BITCAST EN), N.
9586 SDValue Vec = DAG.getUNDEF(VecVT);
9587 SDLoc dl(N);
9588 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9589 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +00009590 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009591 continue;
9592 if (V.getOpcode() == ISD::BITCAST &&
9593 V->getOperand(0).getValueType() == MVT::i32)
9594 // Fold obvious case.
9595 V = V.getOperand(0);
9596 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009597 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009598 // Make the DAGCombiner fold the bitcasts.
9599 DCI.AddToWorklist(V.getNode());
9600 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009601 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009602 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9603 }
9604 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9605 // Make the DAGCombiner fold the bitcasts.
9606 DCI.AddToWorklist(Vec.getNode());
9607 return Vec;
9608}
9609
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009610/// PerformInsertEltCombine - Target-specific dag combine xforms for
9611/// ISD::INSERT_VECTOR_ELT.
9612static SDValue PerformInsertEltCombine(SDNode *N,
9613 TargetLowering::DAGCombinerInfo &DCI) {
9614 // Bitcast an i64 load inserted into a vector to f64.
9615 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9616 EVT VT = N->getValueType(0);
9617 SDNode *Elt = N->getOperand(1).getNode();
9618 if (VT.getVectorElementType() != MVT::i64 ||
9619 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9620 return SDValue();
9621
9622 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009623 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009624 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9625 VT.getVectorNumElements());
9626 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9627 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9628 // Make the DAGCombiner fold the bitcasts.
9629 DCI.AddToWorklist(Vec.getNode());
9630 DCI.AddToWorklist(V.getNode());
9631 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9632 Vec, V, N->getOperand(2));
9633 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009634}
9635
Bob Wilsonc7334a12010-10-27 20:38:28 +00009636/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9637/// ISD::VECTOR_SHUFFLE.
9638static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9639 // The LLVM shufflevector instruction does not require the shuffle mask
9640 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9641 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9642 // operands do not match the mask length, they are extended by concatenating
9643 // them with undef vectors. That is probably the right thing for other
9644 // targets, but for NEON it is better to concatenate two double-register
9645 // size vector operands into a single quad-register size vector. Do that
9646 // transformation here:
9647 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9648 // shuffle(concat(v1, v2), undef)
9649 SDValue Op0 = N->getOperand(0);
9650 SDValue Op1 = N->getOperand(1);
9651 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9652 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9653 Op0.getNumOperands() != 2 ||
9654 Op1.getNumOperands() != 2)
9655 return SDValue();
9656 SDValue Concat0Op1 = Op0.getOperand(1);
9657 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00009658 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +00009659 return SDValue();
9660 // Skip the transformation if any of the types are illegal.
9661 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9662 EVT VT = N->getValueType(0);
9663 if (!TLI.isTypeLegal(VT) ||
9664 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9665 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9666 return SDValue();
9667
Andrew Trickef9de2a2013-05-25 02:42:55 +00009668 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009669 Op0.getOperand(0), Op1.getOperand(0));
9670 // Translate the shuffle mask.
9671 SmallVector<int, 16> NewMask;
9672 unsigned NumElts = VT.getVectorNumElements();
9673 unsigned HalfElts = NumElts/2;
9674 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9675 for (unsigned n = 0; n < NumElts; ++n) {
9676 int MaskElt = SVN->getMaskElt(n);
9677 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009678 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009679 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009680 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009681 NewElt = HalfElts + MaskElt - NumElts;
9682 NewMask.push_back(NewElt);
9683 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009684 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009685 DAG.getUNDEF(VT), NewMask.data());
9686}
9687
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009688/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9689/// NEON load/store intrinsics, and generic vector load/stores, to merge
9690/// base address updates.
9691/// For generic load/stores, the memory type is assumed to be a vector.
9692/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009693static SDValue CombineBaseUpdate(SDNode *N,
9694 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009695 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009696 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9697 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009698 const bool isStore = N->getOpcode() == ISD::STORE;
9699 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009700 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009701 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009702 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009703
9704 // Search for a use of the address operand that is an increment.
9705 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9706 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9707 SDNode *User = *UI;
9708 if (User->getOpcode() != ISD::ADD ||
9709 UI.getUse().getResNo() != Addr.getResNo())
9710 continue;
9711
9712 // Check that the add is independent of the load/store. Otherwise, folding
9713 // it would create a cycle.
9714 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9715 continue;
9716
9717 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009718 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009719 bool isLaneOp = false;
9720 unsigned NewOpc = 0;
9721 unsigned NumVecs = 0;
9722 if (isIntrinsic) {
9723 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9724 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009725 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009726 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9727 NumVecs = 1; break;
9728 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9729 NumVecs = 2; break;
9730 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9731 NumVecs = 3; break;
9732 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9733 NumVecs = 4; break;
9734 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9735 NumVecs = 2; isLaneOp = true; break;
9736 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9737 NumVecs = 3; isLaneOp = true; break;
9738 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9739 NumVecs = 4; isLaneOp = true; break;
9740 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009741 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009742 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009743 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009744 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009745 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009746 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009747 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009748 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009749 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009750 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009751 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009752 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009753 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009754 }
9755 } else {
9756 isLaneOp = true;
9757 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009758 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009759 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9760 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9761 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009762 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9763 NumVecs = 1; isLaneOp = false; break;
9764 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9765 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009766 }
9767 }
9768
9769 // Find the size of memory referenced by the load/store.
9770 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009771 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009772 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009773 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009774 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009775 } else {
9776 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9777 VecTy = N->getOperand(1).getValueType();
9778 }
9779
Bob Wilson06fce872011-02-07 17:43:21 +00009780 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9781 if (isLaneOp)
9782 NumBytes /= VecTy.getVectorNumElements();
9783
9784 // If the increment is a constant, it must match the memory ref size.
9785 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9786 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9787 uint64_t IncVal = CInc->getZExtValue();
9788 if (IncVal != NumBytes)
9789 continue;
9790 } else if (NumBytes >= 3 * 16) {
9791 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9792 // separate instructions that make it harder to use a non-constant update.
9793 continue;
9794 }
9795
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009796 // OK, we found an ADD we can fold into the base update.
9797 // Now, create a _UPD node, taking care of not breaking alignment.
9798
9799 EVT AlignedVecTy = VecTy;
9800 unsigned Alignment = MemN->getAlignment();
9801
9802 // If this is a less-than-standard-aligned load/store, change the type to
9803 // match the standard alignment.
9804 // The alignment is overlooked when selecting _UPD variants; and it's
9805 // easier to introduce bitcasts here than fix that.
9806 // There are 3 ways to get to this base-update combine:
9807 // - intrinsics: they are assumed to be properly aligned (to the standard
9808 // alignment of the memory type), so we don't need to do anything.
9809 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9810 // intrinsics, so, likewise, there's nothing to do.
9811 // - generic load/store instructions: the alignment is specified as an
9812 // explicit operand, rather than implicitly as the standard alignment
9813 // of the memory type (like the intrisics). We need to change the
9814 // memory type to match the explicit alignment. That way, we don't
9815 // generate non-standard-aligned ARMISD::VLDx nodes.
9816 if (isa<LSBaseSDNode>(N)) {
9817 if (Alignment == 0)
9818 Alignment = 1;
9819 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9820 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9821 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9822 assert(!isLaneOp && "Unexpected generic load/store lane.");
9823 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9824 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9825 }
9826 // Don't set an explicit alignment on regular load/stores that we want
9827 // to transform to VLD/VST 1_UPD nodes.
9828 // This matches the behavior of regular load/stores, which only get an
9829 // explicit alignment if the MMO alignment is larger than the standard
9830 // alignment of the memory type.
9831 // Intrinsics, however, always get an explicit alignment, set to the
9832 // alignment of the MMO.
9833 Alignment = 1;
9834 }
9835
Bob Wilson06fce872011-02-07 17:43:21 +00009836 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009837 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009838 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009839 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009840 unsigned n;
9841 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009842 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009843 Tys[n++] = MVT::i32;
9844 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009845 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009846
9847 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009848 SmallVector<SDValue, 8> Ops;
9849 Ops.push_back(N->getOperand(0)); // incoming chain
9850 Ops.push_back(N->getOperand(AddrOpIdx));
9851 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009852
9853 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9854 // Try to match the intrinsic's signature
9855 Ops.push_back(StN->getValue());
9856 } else {
9857 // Loads (and of course intrinsics) match the intrinsics' signature,
9858 // so just add all but the alignment operand.
9859 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9860 Ops.push_back(N->getOperand(i));
9861 }
9862
9863 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009864 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009865
9866 // If this is a non-standard-aligned STORE, the penultimate operand is the
9867 // stored value. Bitcast it to the aligned type.
9868 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9869 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009870 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009871 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009872
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009873 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009874 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009875 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009876
9877 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009878 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009879 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009880 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009881
9882 // If this is an non-standard-aligned LOAD, the first result is the loaded
9883 // value. Bitcast it to the expected result type.
9884 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9885 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009886 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009887 }
9888
Bob Wilson06fce872011-02-07 17:43:21 +00009889 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9890 DCI.CombineTo(N, NewResults);
9891 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9892
9893 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009894 }
Bob Wilson06fce872011-02-07 17:43:21 +00009895 return SDValue();
9896}
9897
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009898static SDValue PerformVLDCombine(SDNode *N,
9899 TargetLowering::DAGCombinerInfo &DCI) {
9900 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9901 return SDValue();
9902
9903 return CombineBaseUpdate(N, DCI);
9904}
9905
Bob Wilson2d790df2010-11-28 06:51:26 +00009906/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9907/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9908/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9909/// return true.
9910static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9911 SelectionDAG &DAG = DCI.DAG;
9912 EVT VT = N->getValueType(0);
9913 // vldN-dup instructions only support 64-bit vectors for N > 1.
9914 if (!VT.is64BitVector())
9915 return false;
9916
9917 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9918 SDNode *VLD = N->getOperand(0).getNode();
9919 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9920 return false;
9921 unsigned NumVecs = 0;
9922 unsigned NewOpc = 0;
9923 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9924 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9925 NumVecs = 2;
9926 NewOpc = ARMISD::VLD2DUP;
9927 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9928 NumVecs = 3;
9929 NewOpc = ARMISD::VLD3DUP;
9930 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9931 NumVecs = 4;
9932 NewOpc = ARMISD::VLD4DUP;
9933 } else {
9934 return false;
9935 }
9936
9937 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9938 // numbers match the load.
9939 unsigned VLDLaneNo =
9940 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9941 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9942 UI != UE; ++UI) {
9943 // Ignore uses of the chain result.
9944 if (UI.getUse().getResNo() == NumVecs)
9945 continue;
9946 SDNode *User = *UI;
9947 if (User->getOpcode() != ARMISD::VDUPLANE ||
9948 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9949 return false;
9950 }
9951
9952 // Create the vldN-dup node.
9953 EVT Tys[5];
9954 unsigned n;
9955 for (n = 0; n < NumVecs; ++n)
9956 Tys[n] = VT;
9957 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009958 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009959 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9960 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009961 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009962 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009963 VLDMemInt->getMemOperand());
9964
9965 // Update the uses.
9966 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9967 UI != UE; ++UI) {
9968 unsigned ResNo = UI.getUse().getResNo();
9969 // Ignore uses of the chain result.
9970 if (ResNo == NumVecs)
9971 continue;
9972 SDNode *User = *UI;
9973 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9974 }
9975
9976 // Now the vldN-lane intrinsic is dead except for its chain result.
9977 // Update uses of the chain.
9978 std::vector<SDValue> VLDDupResults;
9979 for (unsigned n = 0; n < NumVecs; ++n)
9980 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9981 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9982 DCI.CombineTo(VLD, VLDDupResults);
9983
9984 return true;
9985}
9986
Bob Wilson103a0dc2010-07-14 01:22:12 +00009987/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9988/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009989static SDValue PerformVDUPLANECombine(SDNode *N,
9990 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009991 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009992
Bob Wilson2d790df2010-11-28 06:51:26 +00009993 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9994 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9995 if (CombineVLDDUP(N, DCI))
9996 return SDValue(N, 0);
9997
9998 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9999 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +000010000 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010001 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +000010002 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010003 return SDValue();
10004
10005 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
10006 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
10007 // The canonical VMOV for a zero vector uses a 32-bit element size.
10008 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10009 unsigned EltBits;
10010 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
10011 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +000010012 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010013 if (EltSize > VT.getVectorElementType().getSizeInBits())
10014 return SDValue();
10015
Andrew Trickef9de2a2013-05-25 02:42:55 +000010016 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010017}
10018
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010019static SDValue PerformLOADCombine(SDNode *N,
10020 TargetLowering::DAGCombinerInfo &DCI) {
10021 EVT VT = N->getValueType(0);
10022
10023 // If this is a legal vector load, try to combine it into a VLD1_UPD.
10024 if (ISD::isNormalLoad(N) && VT.isVector() &&
10025 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10026 return CombineBaseUpdate(N, DCI);
10027
10028 return SDValue();
10029}
10030
Ahmed Bougacha23167462014-12-09 21:26:53 +000010031/// PerformSTORECombine - Target-specific dag combine xforms for
10032/// ISD::STORE.
10033static SDValue PerformSTORECombine(SDNode *N,
10034 TargetLowering::DAGCombinerInfo &DCI) {
10035 StoreSDNode *St = cast<StoreSDNode>(N);
10036 if (St->isVolatile())
10037 return SDValue();
10038
10039 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
10040 // pack all of the elements in one place. Next, store to memory in fewer
10041 // chunks.
10042 SDValue StVal = St->getValue();
10043 EVT VT = StVal.getValueType();
10044 if (St->isTruncatingStore() && VT.isVector()) {
10045 SelectionDAG &DAG = DCI.DAG;
10046 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10047 EVT StVT = St->getMemoryVT();
10048 unsigned NumElems = VT.getVectorNumElements();
10049 assert(StVT != VT && "Cannot truncate to the same type");
10050 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
10051 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
10052
10053 // From, To sizes and ElemCount must be pow of two
10054 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
10055
10056 // We are going to use the original vector elt for storing.
10057 // Accumulated smaller vector elements must be a multiple of the store size.
10058 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
10059
10060 unsigned SizeRatio = FromEltSz / ToEltSz;
10061 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
10062
10063 // Create a type on which we perform the shuffle.
10064 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
10065 NumElems*SizeRatio);
10066 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
10067
10068 SDLoc DL(St);
10069 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
10070 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
10071 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +000010072 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
10073 ? (i + 1) * SizeRatio - 1
10074 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +000010075
10076 // Can't shuffle using an illegal type.
10077 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10078
10079 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10080 DAG.getUNDEF(WideVec.getValueType()),
10081 ShuffleVec.data());
10082 // At this point all of the data is stored at the bottom of the
10083 // register. We now need to save it to mem.
10084
10085 // Find the largest store unit
10086 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010087 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010088 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10089 StoreType = Tp;
10090 }
10091 // Didn't find a legal store type.
10092 if (!TLI.isTypeLegal(StoreType))
10093 return SDValue();
10094
10095 // Bitcast the original vector into a vector of store-size units
10096 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10097 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10098 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10099 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10100 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010101 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10102 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010103 SDValue BasePtr = St->getBasePtr();
10104
10105 // Perform one or more big stores into memory.
10106 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10107 for (unsigned I = 0; I < E; I++) {
10108 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10109 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010110 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010111 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
10112 St->getPointerInfo(), St->isVolatile(),
10113 St->isNonTemporal(), St->getAlignment());
10114 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10115 Increment);
10116 Chains.push_back(Ch);
10117 }
10118 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10119 }
10120
10121 if (!ISD::isNormalStore(St))
10122 return SDValue();
10123
10124 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10125 // ARM stores of arguments in the same cache line.
10126 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10127 StVal.getNode()->hasOneUse()) {
10128 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010129 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010130 SDLoc DL(St);
10131 SDValue BasePtr = St->getBasePtr();
10132 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
10133 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
10134 BasePtr, St->getPointerInfo(), St->isVolatile(),
10135 St->isNonTemporal(), St->getAlignment());
10136
10137 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010138 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010139 return DAG.getStore(NewST1.getValue(0), DL,
10140 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
10141 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
10142 St->isNonTemporal(),
10143 std::min(4U, St->getAlignment() / 2));
10144 }
10145
10146 if (StVal.getValueType() == MVT::i64 &&
10147 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10148
10149 // Bitcast an i64 store extracted from a vector to f64.
10150 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10151 SelectionDAG &DAG = DCI.DAG;
10152 SDLoc dl(StVal);
10153 SDValue IntVec = StVal.getOperand(0);
10154 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10155 IntVec.getValueType().getVectorNumElements());
10156 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10157 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10158 Vec, StVal.getOperand(1));
10159 dl = SDLoc(N);
10160 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10161 // Make the DAGCombiner fold the bitcasts.
10162 DCI.AddToWorklist(Vec.getNode());
10163 DCI.AddToWorklist(ExtElt.getNode());
10164 DCI.AddToWorklist(V.getNode());
10165 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
10166 St->getPointerInfo(), St->isVolatile(),
10167 St->isNonTemporal(), St->getAlignment(),
10168 St->getAAInfo());
10169 }
10170
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010171 // If this is a legal vector store, try to combine it into a VST1_UPD.
10172 if (ISD::isNormalStore(N) && VT.isVector() &&
10173 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10174 return CombineBaseUpdate(N, DCI);
10175
Ahmed Bougacha23167462014-12-09 21:26:53 +000010176 return SDValue();
10177}
10178
Chad Rosierfa8d8932011-06-24 19:23:04 +000010179/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10180/// can replace combinations of VMUL and VCVT (floating-point to integer)
10181/// when the VMUL has a constant operand that is a power of 2.
10182///
10183/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10184/// vmul.f32 d16, d17, d16
10185/// vcvt.s32.f32 d16, d16
10186/// becomes:
10187/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010188static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010189 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010190 if (!Subtarget->hasNEON())
10191 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010192
Chad Rosiera087fd22015-10-06 20:23:42 +000010193 SDValue Op = N->getOperand(0);
Tim Northover498c56c2016-03-17 20:10:28 +000010194 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
10195 Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010196 return SDValue();
10197
Chad Rosierfa8d8932011-06-24 19:23:04 +000010198 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010199 if (!isa<BuildVectorSDNode>(ConstVec))
10200 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010201
Tim Northover7cbc2152013-06-28 15:29:25 +000010202 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010203 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010204 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010205 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010206 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010207 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010208 // These instructions only exist converting from f32 to i32. We can handle
10209 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010210 // be lossy. We also can't handle more then 4 lanes, since these intructions
10211 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010212 return SDValue();
10213 }
10214
Chad Rosier169865f2015-10-07 17:28:58 +000010215 BitVector UndefElements;
10216 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10217 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10218 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010219 return SDValue();
10220
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010221 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010222 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010223 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10224 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010225 SDValue FixConv = DAG.getNode(
10226 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10227 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010228 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010229
Chad Rosier9df4aff2015-10-06 20:45:45 +000010230 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010231 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010232
10233 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010234}
10235
10236/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10237/// can replace combinations of VCVT (integer to floating-point) and VDIV
10238/// when the VDIV has a constant operand that is a power of 2.
10239///
10240/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10241/// vcvt.f32.s32 d16, d16
10242/// vdiv.f32 d16, d17, d16
10243/// becomes:
10244/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010245static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010246 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010247 if (!Subtarget->hasNEON())
10248 return SDValue();
10249
Chad Rosierfa8d8932011-06-24 19:23:04 +000010250 SDValue Op = N->getOperand(0);
10251 unsigned OpOpcode = Op.getNode()->getOpcode();
Tim Northover498c56c2016-03-17 20:10:28 +000010252 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010253 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10254 return SDValue();
10255
Chad Rosierfa8d8932011-06-24 19:23:04 +000010256 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010257 if (!isa<BuildVectorSDNode>(ConstVec))
10258 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010259
Tim Northover7cbc2152013-06-28 15:29:25 +000010260 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010261 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010262 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010263 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010264 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10265 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010266 // These instructions only exist converting from i32 to f32. We can handle
10267 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010268 // be lossy. We also can't handle more then 4 lanes, since these intructions
10269 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010270 return SDValue();
10271 }
10272
Chad Rosier169865f2015-10-07 17:28:58 +000010273 BitVector UndefElements;
10274 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10275 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10276 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010277 return SDValue();
10278
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010279 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010280 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010281 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010282 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010283 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010284 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010285 ConvInput);
10286
Eric Christopher1b8b94192011-06-29 21:10:36 +000010287 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010288 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010289 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010290 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010291 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010292 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010293}
10294
10295/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010296/// operand of a vector shift operation, where all the elements of the
10297/// build_vector must have the same constant integer value.
10298static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10299 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010300 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010301 Op = Op.getOperand(0);
10302 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10303 APInt SplatBits, SplatUndef;
10304 unsigned SplatBitSize;
10305 bool HasAnyUndefs;
10306 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10307 HasAnyUndefs, ElementBits) ||
10308 SplatBitSize > ElementBits)
10309 return false;
10310 Cnt = SplatBits.getSExtValue();
10311 return true;
10312}
10313
10314/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10315/// operand of a vector shift left operation. That value must be in the range:
10316/// 0 <= Value < ElementBits for a left shift; or
10317/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010318static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010319 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010320 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010321 if (! getVShiftImm(Op, ElementBits, Cnt))
10322 return false;
10323 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10324}
10325
10326/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10327/// operand of a vector shift right operation. For a shift opcode, the value
10328/// is positive, but for an intrinsic the value count must be negative. The
10329/// absolute value must be in the range:
10330/// 1 <= |Value| <= ElementBits for a right shift; or
10331/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010332static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010333 int64_t &Cnt) {
10334 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010335 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010336 if (! getVShiftImm(Op, ElementBits, Cnt))
10337 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010338 if (!isIntrinsic)
10339 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10340 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010341 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010342 return true;
10343 }
10344 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010345}
10346
10347/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10348static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10349 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10350 switch (IntNo) {
10351 default:
10352 // Don't do anything for most intrinsics.
10353 break;
10354
10355 // Vector shifts: check for immediate versions and lower them.
10356 // Note: This is done during DAG combining instead of DAG legalizing because
10357 // the build_vectors for 64-bit vector element shift counts are generally
10358 // not legal, and it is hard to see their values after they get legalized to
10359 // loads from a constant pool.
10360 case Intrinsic::arm_neon_vshifts:
10361 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010362 case Intrinsic::arm_neon_vrshifts:
10363 case Intrinsic::arm_neon_vrshiftu:
10364 case Intrinsic::arm_neon_vrshiftn:
10365 case Intrinsic::arm_neon_vqshifts:
10366 case Intrinsic::arm_neon_vqshiftu:
10367 case Intrinsic::arm_neon_vqshiftsu:
10368 case Intrinsic::arm_neon_vqshiftns:
10369 case Intrinsic::arm_neon_vqshiftnu:
10370 case Intrinsic::arm_neon_vqshiftnsu:
10371 case Intrinsic::arm_neon_vqrshiftns:
10372 case Intrinsic::arm_neon_vqrshiftnu:
10373 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010374 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010375 int64_t Cnt;
10376 unsigned VShiftOpc = 0;
10377
10378 switch (IntNo) {
10379 case Intrinsic::arm_neon_vshifts:
10380 case Intrinsic::arm_neon_vshiftu:
10381 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10382 VShiftOpc = ARMISD::VSHL;
10383 break;
10384 }
10385 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10386 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10387 ARMISD::VSHRs : ARMISD::VSHRu);
10388 break;
10389 }
10390 return SDValue();
10391
Bob Wilson2e076c42009-06-22 23:27:02 +000010392 case Intrinsic::arm_neon_vrshifts:
10393 case Intrinsic::arm_neon_vrshiftu:
10394 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10395 break;
10396 return SDValue();
10397
10398 case Intrinsic::arm_neon_vqshifts:
10399 case Intrinsic::arm_neon_vqshiftu:
10400 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10401 break;
10402 return SDValue();
10403
10404 case Intrinsic::arm_neon_vqshiftsu:
10405 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10406 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010407 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010408
Bob Wilson2e076c42009-06-22 23:27:02 +000010409 case Intrinsic::arm_neon_vrshiftn:
10410 case Intrinsic::arm_neon_vqshiftns:
10411 case Intrinsic::arm_neon_vqshiftnu:
10412 case Intrinsic::arm_neon_vqshiftnsu:
10413 case Intrinsic::arm_neon_vqrshiftns:
10414 case Intrinsic::arm_neon_vqrshiftnu:
10415 case Intrinsic::arm_neon_vqrshiftnsu:
10416 // Narrowing shifts require an immediate right shift.
10417 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10418 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010419 llvm_unreachable("invalid shift count for narrowing vector shift "
10420 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010421
10422 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010423 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010424 }
10425
10426 switch (IntNo) {
10427 case Intrinsic::arm_neon_vshifts:
10428 case Intrinsic::arm_neon_vshiftu:
10429 // Opcode already set above.
10430 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010431 case Intrinsic::arm_neon_vrshifts:
10432 VShiftOpc = ARMISD::VRSHRs; break;
10433 case Intrinsic::arm_neon_vrshiftu:
10434 VShiftOpc = ARMISD::VRSHRu; break;
10435 case Intrinsic::arm_neon_vrshiftn:
10436 VShiftOpc = ARMISD::VRSHRN; break;
10437 case Intrinsic::arm_neon_vqshifts:
10438 VShiftOpc = ARMISD::VQSHLs; break;
10439 case Intrinsic::arm_neon_vqshiftu:
10440 VShiftOpc = ARMISD::VQSHLu; break;
10441 case Intrinsic::arm_neon_vqshiftsu:
10442 VShiftOpc = ARMISD::VQSHLsu; break;
10443 case Intrinsic::arm_neon_vqshiftns:
10444 VShiftOpc = ARMISD::VQSHRNs; break;
10445 case Intrinsic::arm_neon_vqshiftnu:
10446 VShiftOpc = ARMISD::VQSHRNu; break;
10447 case Intrinsic::arm_neon_vqshiftnsu:
10448 VShiftOpc = ARMISD::VQSHRNsu; break;
10449 case Intrinsic::arm_neon_vqrshiftns:
10450 VShiftOpc = ARMISD::VQRSHRNs; break;
10451 case Intrinsic::arm_neon_vqrshiftnu:
10452 VShiftOpc = ARMISD::VQRSHRNu; break;
10453 case Intrinsic::arm_neon_vqrshiftnsu:
10454 VShiftOpc = ARMISD::VQRSHRNsu; break;
10455 }
10456
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010457 SDLoc dl(N);
10458 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10459 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010460 }
10461
10462 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010463 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010464 int64_t Cnt;
10465 unsigned VShiftOpc = 0;
10466
10467 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10468 VShiftOpc = ARMISD::VSLI;
10469 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10470 VShiftOpc = ARMISD::VSRI;
10471 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010472 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010473 }
10474
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010475 SDLoc dl(N);
10476 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010477 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010478 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010479 }
10480
10481 case Intrinsic::arm_neon_vqrshifts:
10482 case Intrinsic::arm_neon_vqrshiftu:
10483 // No immediate versions of these to check for.
10484 break;
10485 }
10486
10487 return SDValue();
10488}
10489
10490/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10491/// lowers them. As with the vector shift intrinsics, this is done during DAG
10492/// combining instead of DAG legalizing because the build_vectors for 64-bit
10493/// vector element shift counts are generally not legal, and it is hard to see
10494/// their values after they get legalized to loads from a constant pool.
10495static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10496 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010497 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010498 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10499 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10500 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10501 SDValue N1 = N->getOperand(1);
10502 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10503 SDValue N0 = N->getOperand(0);
10504 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10505 DAG.MaskedValueIsZero(N0.getOperand(0),
10506 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010507 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010508 }
10509 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010510
10511 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010512 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10513 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010514 return SDValue();
10515
10516 assert(ST->hasNEON() && "unexpected vector shift");
10517 int64_t Cnt;
10518
10519 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010520 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010521
10522 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010523 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10524 SDLoc dl(N);
10525 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10526 DAG.getConstant(Cnt, dl, MVT::i32));
10527 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010528 break;
10529
10530 case ISD::SRA:
10531 case ISD::SRL:
10532 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10533 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10534 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010535 SDLoc dl(N);
10536 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10537 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010538 }
10539 }
10540 return SDValue();
10541}
10542
10543/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10544/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10545static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10546 const ARMSubtarget *ST) {
10547 SDValue N0 = N->getOperand(0);
10548
10549 // Check for sign- and zero-extensions of vector extract operations of 8-
10550 // and 16-bit vector elements. NEON supports these directly. They are
10551 // handled during DAG combining because type legalization will promote them
10552 // to 32-bit types and it is messy to recognize the operations after that.
10553 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10554 SDValue Vec = N0.getOperand(0);
10555 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010556 EVT VT = N->getValueType(0);
10557 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010558 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10559
Owen Anderson9f944592009-08-11 20:47:22 +000010560 if (VT == MVT::i32 &&
10561 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010562 TLI.isTypeLegal(Vec.getValueType()) &&
10563 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010564
10565 unsigned Opc = 0;
10566 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010567 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010568 case ISD::SIGN_EXTEND:
10569 Opc = ARMISD::VGETLANEs;
10570 break;
10571 case ISD::ZERO_EXTEND:
10572 case ISD::ANY_EXTEND:
10573 Opc = ARMISD::VGETLANEu;
10574 break;
10575 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010576 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010577 }
10578 }
10579
10580 return SDValue();
10581}
10582
James Molloy9d55f192015-11-10 14:22:05 +000010583static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10584 APInt &KnownOne) {
10585 if (Op.getOpcode() == ARMISD::BFI) {
10586 // Conservatively, we can recurse down the first operand
10587 // and just mask out all affected bits.
10588 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10589
10590 // The operand to BFI is already a mask suitable for removing the bits it
10591 // sets.
10592 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
Benjamin Kramer46e38f32016-06-08 10:01:20 +000010593 const APInt &Mask = CI->getAPIntValue();
James Molloy9d55f192015-11-10 14:22:05 +000010594 KnownZero &= Mask;
10595 KnownOne &= Mask;
10596 return;
10597 }
10598 if (Op.getOpcode() == ARMISD::CMOV) {
10599 APInt KZ2(KnownZero.getBitWidth(), 0);
10600 APInt KO2(KnownOne.getBitWidth(), 0);
10601 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10602 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10603
10604 KnownZero &= KZ2;
10605 KnownOne &= KO2;
10606 return;
10607 }
10608 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10609}
10610
10611SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10612 // If we have a CMOV, OR and AND combination such as:
10613 // if (x & CN)
10614 // y |= CM;
10615 //
10616 // And:
10617 // * CN is a single bit;
10618 // * All bits covered by CM are known zero in y
10619 //
10620 // Then we can convert this into a sequence of BFI instructions. This will
10621 // always be a win if CM is a single bit, will always be no worse than the
10622 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10623 // three bits (due to the extra IT instruction).
10624
10625 SDValue Op0 = CMOV->getOperand(0);
10626 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010627 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10628 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010629 SDValue CmpZ = CMOV->getOperand(4);
10630
James Molloy20180912015-11-16 10:49:25 +000010631 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010632 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010633 return SDValue();
10634
James Molloy9d55f192015-11-10 14:22:05 +000010635 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10636 SDValue And = CmpZ->getOperand(0);
10637 if (And->getOpcode() != ISD::AND)
10638 return SDValue();
10639 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10640 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10641 return SDValue();
10642 SDValue X = And->getOperand(0);
10643
James Molloy8e99e972015-11-12 13:49:17 +000010644 if (CC == ARMCC::EQ) {
10645 // We're performing an "equal to zero" compare. Swap the operands so we
10646 // canonicalize on a "not equal to zero" compare.
10647 std::swap(Op0, Op1);
10648 } else {
10649 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10650 }
Junmo Park1108ab02016-02-19 01:46:04 +000010651
James Molloy9d55f192015-11-10 14:22:05 +000010652 if (Op1->getOpcode() != ISD::OR)
10653 return SDValue();
10654
10655 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10656 if (!OrC)
10657 return SDValue();
10658 SDValue Y = Op1->getOperand(0);
10659
10660 if (Op0 != Y)
10661 return SDValue();
10662
10663 // Now, is it profitable to continue?
10664 APInt OrCI = OrC->getAPIntValue();
10665 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10666 if (OrCI.countPopulation() > Heuristic)
10667 return SDValue();
10668
10669 // Lastly, can we determine that the bits defined by OrCI
10670 // are zero in Y?
10671 APInt KnownZero, KnownOne;
10672 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10673 if ((OrCI & KnownZero) != OrCI)
10674 return SDValue();
10675
10676 // OK, we can do the combine.
10677 SDValue V = Y;
10678 SDLoc dl(X);
10679 EVT VT = X.getValueType();
10680 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000010681
James Molloy9d55f192015-11-10 14:22:05 +000010682 if (BitInX != 0) {
10683 // We must shift X first.
10684 X = DAG.getNode(ISD::SRL, dl, VT, X,
10685 DAG.getConstant(BitInX, dl, VT));
10686 }
10687
10688 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10689 BitInY < NumActiveBits; ++BitInY) {
10690 if (OrCI[BitInY] == 0)
10691 continue;
10692 APInt Mask(VT.getSizeInBits(), 0);
10693 Mask.setBit(BitInY);
10694 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10695 // Confusingly, the operand is an *inverted* mask.
10696 DAG.getConstant(~Mask, dl, VT));
10697 }
10698
10699 return V;
10700}
10701
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010702/// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND.
10703SDValue
10704ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const {
10705 SDValue Cmp = N->getOperand(4);
10706 if (Cmp.getOpcode() != ARMISD::CMPZ)
10707 // Only looking at NE cases.
10708 return SDValue();
10709
10710 EVT VT = N->getValueType(0);
10711 SDLoc dl(N);
10712 SDValue LHS = Cmp.getOperand(0);
10713 SDValue RHS = Cmp.getOperand(1);
10714 SDValue Chain = N->getOperand(0);
10715 SDValue BB = N->getOperand(1);
10716 SDValue ARMcc = N->getOperand(2);
10717 ARMCC::CondCodes CC =
10718 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
10719
10720 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0))
10721 // -> (brcond Chain BB CC CPSR Cmp)
10722 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() &&
10723 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV &&
10724 LHS->getOperand(0)->hasOneUse()) {
10725 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0));
10726 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1));
10727 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
10728 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
10729 if ((LHS00C && LHS00C->getZExtValue() == 0) &&
10730 (LHS01C && LHS01C->getZExtValue() == 1) &&
10731 (LHS1C && LHS1C->getZExtValue() == 1) &&
10732 (RHSC && RHSC->getZExtValue() == 0)) {
10733 return DAG.getNode(
10734 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2),
10735 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4));
10736 }
10737 }
10738
10739 return SDValue();
10740}
10741
Evan Chengf863e3f2011-07-13 00:42:17 +000010742/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10743SDValue
10744ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10745 SDValue Cmp = N->getOperand(4);
10746 if (Cmp.getOpcode() != ARMISD::CMPZ)
10747 // Only looking at EQ and NE cases.
10748 return SDValue();
10749
10750 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010751 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010752 SDValue LHS = Cmp.getOperand(0);
10753 SDValue RHS = Cmp.getOperand(1);
10754 SDValue FalseVal = N->getOperand(0);
10755 SDValue TrueVal = N->getOperand(1);
10756 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010757 ARMCC::CondCodes CC =
10758 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010759
James Molloy9d55f192015-11-10 14:22:05 +000010760 // BFI is only available on V6T2+.
10761 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10762 SDValue R = PerformCMOVToBFICombine(N, DAG);
10763 if (R)
10764 return R;
10765 }
10766
Evan Chengf863e3f2011-07-13 00:42:17 +000010767 // Simplify
10768 // mov r1, r0
10769 // cmp r1, x
10770 // mov r0, y
10771 // moveq r0, x
10772 // to
10773 // cmp r0, x
10774 // movne r0, y
10775 //
10776 // mov r1, r0
10777 // cmp r1, x
10778 // mov r0, x
10779 // movne r0, y
10780 // to
10781 // cmp r0, x
10782 // movne r0, y
10783 /// FIXME: Turn this into a target neutral optimization?
10784 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010785 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010786 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10787 N->getOperand(3), Cmp);
10788 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10789 SDValue ARMcc;
10790 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10791 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10792 N->getOperand(3), NewCmp);
10793 }
10794
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010795 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0))
10796 // -> (cmov F T CC CPSR Cmp)
10797 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) {
10798 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0));
10799 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
10800 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
10801 if ((LHS0C && LHS0C->getZExtValue() == 0) &&
10802 (LHS1C && LHS1C->getZExtValue() == 1) &&
10803 (RHSC && RHSC->getZExtValue() == 0)) {
10804 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
10805 LHS->getOperand(2), LHS->getOperand(3),
10806 LHS->getOperand(4));
10807 }
10808 }
10809
Evan Chengf863e3f2011-07-13 00:42:17 +000010810 if (Res.getNode()) {
10811 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010812 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010813 // Capture demanded bits information that would be otherwise lost.
10814 if (KnownZero == 0xfffffffe)
10815 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10816 DAG.getValueType(MVT::i1));
10817 else if (KnownZero == 0xffffff00)
10818 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10819 DAG.getValueType(MVT::i8));
10820 else if (KnownZero == 0xffff0000)
10821 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10822 DAG.getValueType(MVT::i16));
10823 }
10824
10825 return Res;
10826}
10827
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010828SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010829 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010830 switch (N->getOpcode()) {
10831 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010832 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010833 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010834 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010835 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010836 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010837 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10838 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010839 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010840 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010841 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010842 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010843 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010844 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010845 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010846 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010847 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010848 case ISD::FP_TO_UINT:
10849 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10850 case ISD::FDIV:
10851 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010852 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010853 case ISD::SHL:
10854 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010855 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010856 case ISD::SIGN_EXTEND:
10857 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010858 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010859 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010860 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010861 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010862 case ARMISD::VLD2DUP:
10863 case ARMISD::VLD3DUP:
10864 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010865 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010866 case ARMISD::BUILD_VECTOR:
10867 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010868 case ISD::INTRINSIC_VOID:
10869 case ISD::INTRINSIC_W_CHAIN:
10870 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10871 case Intrinsic::arm_neon_vld1:
10872 case Intrinsic::arm_neon_vld2:
10873 case Intrinsic::arm_neon_vld3:
10874 case Intrinsic::arm_neon_vld4:
10875 case Intrinsic::arm_neon_vld2lane:
10876 case Intrinsic::arm_neon_vld3lane:
10877 case Intrinsic::arm_neon_vld4lane:
10878 case Intrinsic::arm_neon_vst1:
10879 case Intrinsic::arm_neon_vst2:
10880 case Intrinsic::arm_neon_vst3:
10881 case Intrinsic::arm_neon_vst4:
10882 case Intrinsic::arm_neon_vst2lane:
10883 case Intrinsic::arm_neon_vst3lane:
10884 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010885 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010886 default: break;
10887 }
10888 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010889 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010890 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010891}
10892
Evan Chengd42641c2011-02-02 01:06:55 +000010893bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10894 EVT VT) const {
10895 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10896}
10897
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010898bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10899 unsigned,
10900 unsigned,
10901 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010902 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010903 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010904
10905 switch (VT.getSimpleVT().SimpleTy) {
10906 default:
10907 return false;
10908 case MVT::i8:
10909 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010910 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010911 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010912 if (AllowsUnaligned) {
10913 if (Fast)
10914 *Fast = Subtarget->hasV7Ops();
10915 return true;
10916 }
10917 return false;
10918 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010919 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010920 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010921 // For any little-endian targets with neon, we can support unaligned ld/st
10922 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010923 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010924 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010925 if (Fast)
10926 *Fast = true;
10927 return true;
10928 }
10929 return false;
10930 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010931 }
10932}
10933
Lang Hames9929c422011-11-02 22:52:45 +000010934static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10935 unsigned AlignCheck) {
10936 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10937 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10938}
10939
10940EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10941 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010942 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010943 bool MemcpyStrSrc,
10944 MachineFunction &MF) const {
10945 const Function *F = MF.getFunction();
10946
10947 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010948 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10949 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010950 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010951 if (Size >= 16 &&
10952 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010953 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010954 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010955 } else if (Size >= 8 &&
10956 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010957 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10958 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010959 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010960 }
10961 }
10962
Lang Hamesb85fcd02011-11-08 18:56:23 +000010963 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010964 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010965 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010966 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010967 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010968
Lang Hames9929c422011-11-02 22:52:45 +000010969 // Let the target-independent logic figure it out.
10970 return MVT::Other;
10971}
10972
Evan Cheng9ec512d2012-12-06 19:13:27 +000010973bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10974 if (Val.getOpcode() != ISD::LOAD)
10975 return false;
10976
10977 EVT VT1 = Val.getValueType();
10978 if (!VT1.isSimple() || !VT1.isInteger() ||
10979 !VT2.isSimple() || !VT2.isInteger())
10980 return false;
10981
10982 switch (VT1.getSimpleVT().SimpleTy) {
10983 default: break;
10984 case MVT::i1:
10985 case MVT::i8:
10986 case MVT::i16:
10987 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10988 return true;
10989 }
10990
10991 return false;
10992}
10993
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010994bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10995 EVT VT = ExtVal.getValueType();
10996
10997 if (!isTypeLegal(VT))
10998 return false;
10999
11000 // Don't create a loadext if we can fold the extension into a wide/long
11001 // instruction.
11002 // If there's more than one user instruction, the loadext is desirable no
11003 // matter what. There can be two uses by the same instruction.
11004 if (ExtVal->use_empty() ||
11005 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
11006 return true;
11007
11008 SDNode *U = *ExtVal->use_begin();
11009 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
11010 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
11011 return false;
11012
11013 return true;
11014}
11015
Tim Northovercc2e9032013-08-06 13:58:03 +000011016bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
11017 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11018 return false;
11019
11020 if (!isTypeLegal(EVT::getEVT(Ty1)))
11021 return false;
11022
11023 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
11024
11025 // Assuming the caller doesn't have a zeroext or signext return parameter,
11026 // truncation all the way down to i1 is valid.
11027 return true;
11028}
11029
11030
Evan Chengdc49a8d2009-08-14 20:09:37 +000011031static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
11032 if (V < 0)
11033 return false;
11034
11035 unsigned Scale = 1;
11036 switch (VT.getSimpleVT().SimpleTy) {
11037 default: return false;
11038 case MVT::i1:
11039 case MVT::i8:
11040 // Scale == 1;
11041 break;
11042 case MVT::i16:
11043 // Scale == 2;
11044 Scale = 2;
11045 break;
11046 case MVT::i32:
11047 // Scale == 4;
11048 Scale = 4;
11049 break;
11050 }
11051
11052 if ((V & (Scale - 1)) != 0)
11053 return false;
11054 V /= Scale;
11055 return V == (V & ((1LL << 5) - 1));
11056}
11057
11058static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
11059 const ARMSubtarget *Subtarget) {
11060 bool isNeg = false;
11061 if (V < 0) {
11062 isNeg = true;
11063 V = - V;
11064 }
11065
11066 switch (VT.getSimpleVT().SimpleTy) {
11067 default: return false;
11068 case MVT::i1:
11069 case MVT::i8:
11070 case MVT::i16:
11071 case MVT::i32:
11072 // + imm12 or - imm8
11073 if (isNeg)
11074 return V == (V & ((1LL << 8) - 1));
11075 return V == (V & ((1LL << 12) - 1));
11076 case MVT::f32:
11077 case MVT::f64:
11078 // Same as ARM mode. FIXME: NEON?
11079 if (!Subtarget->hasVFP2())
11080 return false;
11081 if ((V & 3) != 0)
11082 return false;
11083 V >>= 2;
11084 return V == (V & ((1LL << 8) - 1));
11085 }
11086}
11087
Evan Cheng2150b922007-03-12 23:30:29 +000011088/// isLegalAddressImmediate - Return true if the integer value can be used
11089/// as the offset of the target addressing mode for load / store of the
11090/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011091static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011092 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000011093 if (V == 0)
11094 return true;
11095
Evan Chengce5dfb62009-03-09 19:15:00 +000011096 if (!VT.isSimple())
11097 return false;
11098
Evan Chengdc49a8d2009-08-14 20:09:37 +000011099 if (Subtarget->isThumb1Only())
11100 return isLegalT1AddressImmediate(V, VT);
11101 else if (Subtarget->isThumb2())
11102 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000011103
Evan Chengdc49a8d2009-08-14 20:09:37 +000011104 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000011105 if (V < 0)
11106 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000011107 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000011108 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011109 case MVT::i1:
11110 case MVT::i8:
11111 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000011112 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011113 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011114 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000011115 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011116 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011117 case MVT::f32:
11118 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011119 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000011120 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000011121 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000011122 return false;
11123 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011124 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000011125 }
Evan Cheng10043e22007-01-19 07:51:42 +000011126}
11127
Evan Chengdc49a8d2009-08-14 20:09:37 +000011128bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
11129 EVT VT) const {
11130 int Scale = AM.Scale;
11131 if (Scale < 0)
11132 return false;
11133
11134 switch (VT.getSimpleVT().SimpleTy) {
11135 default: return false;
11136 case MVT::i1:
11137 case MVT::i8:
11138 case MVT::i16:
11139 case MVT::i32:
11140 if (Scale == 1)
11141 return true;
11142 // r + r << imm
11143 Scale = Scale & ~1;
11144 return Scale == 2 || Scale == 4 || Scale == 8;
11145 case MVT::i64:
11146 // r + r
11147 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11148 return true;
11149 return false;
11150 case MVT::isVoid:
11151 // Note, we allow "void" uses (basically, uses that aren't loads or
11152 // stores), because arm allows folding a scale into many arithmetic
11153 // operations. This should be made more precise and revisited later.
11154
11155 // Allow r << imm, but the imm has to be a multiple of two.
11156 if (Scale & 1) return false;
11157 return isPowerOf2_32(Scale);
11158 }
11159}
11160
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011161/// isLegalAddressingMode - Return true if the addressing mode represented
11162/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011163bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11164 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011165 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011166 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011167 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011168 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011169
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011170 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011171 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011172 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011173
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011174 switch (AM.Scale) {
11175 case 0: // no scale reg, must be "r+i" or "r", or "i".
11176 break;
11177 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011178 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011179 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000011180 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011181 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011182 // ARM doesn't support any R+R*scale+imm addr modes.
11183 if (AM.BaseOffs)
11184 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011185
Bob Wilson866c1742009-04-08 17:55:28 +000011186 if (!VT.isSimple())
11187 return false;
11188
Evan Chengdc49a8d2009-08-14 20:09:37 +000011189 if (Subtarget->isThumb2())
11190 return isLegalT2ScaledAddressingMode(AM, VT);
11191
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011192 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011193 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011194 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011195 case MVT::i1:
11196 case MVT::i8:
11197 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011198 if (Scale < 0) Scale = -Scale;
11199 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011200 return true;
11201 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011202 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011203 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011204 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011205 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011206 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011207 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011208 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011209
Owen Anderson9f944592009-08-11 20:47:22 +000011210 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011211 // Note, we allow "void" uses (basically, uses that aren't loads or
11212 // stores), because arm allows folding a scale into many arithmetic
11213 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011214
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011215 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011216 if (Scale & 1) return false;
11217 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011218 }
Evan Cheng2150b922007-03-12 23:30:29 +000011219 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011220 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011221}
11222
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011223/// isLegalICmpImmediate - Return true if the specified immediate is legal
11224/// icmp immediate, that is the target has icmp instructions which can compare
11225/// a register against the immediate without having to materialize the
11226/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011227bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011228 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011229 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011230 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011231 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011232 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011233 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011234 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011235}
11236
Andrew Tricka22cdb72012-07-18 18:34:27 +000011237/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11238/// *or sub* immediate, that is the target has add or sub instructions which can
11239/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011240/// immediate into a register.
11241bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011242 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011243 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011244 if (!Subtarget->isThumb())
11245 return ARM_AM::getSOImmVal(AbsImm) != -1;
11246 if (Subtarget->isThumb2())
11247 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11248 // Thumb1 only has 8-bit unsigned immediate.
11249 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011250}
11251
Owen Anderson53aa7a92009-08-10 22:56:29 +000011252static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011253 bool isSEXTLoad, SDValue &Base,
11254 SDValue &Offset, bool &isInc,
11255 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011256 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11257 return false;
11258
Owen Anderson9f944592009-08-11 20:47:22 +000011259 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011260 // AddressingMode 3
11261 Base = Ptr->getOperand(0);
11262 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011263 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011264 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011265 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011266 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011267 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011268 return true;
11269 }
11270 }
11271 isInc = (Ptr->getOpcode() == ISD::ADD);
11272 Offset = Ptr->getOperand(1);
11273 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011274 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011275 // AddressingMode 2
11276 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011277 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011278 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011279 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011280 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011281 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011282 Base = Ptr->getOperand(0);
11283 return true;
11284 }
11285 }
11286
11287 if (Ptr->getOpcode() == ISD::ADD) {
11288 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011289 ARM_AM::ShiftOpc ShOpcVal=
11290 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011291 if (ShOpcVal != ARM_AM::no_shift) {
11292 Base = Ptr->getOperand(1);
11293 Offset = Ptr->getOperand(0);
11294 } else {
11295 Base = Ptr->getOperand(0);
11296 Offset = Ptr->getOperand(1);
11297 }
11298 return true;
11299 }
11300
11301 isInc = (Ptr->getOpcode() == ISD::ADD);
11302 Base = Ptr->getOperand(0);
11303 Offset = Ptr->getOperand(1);
11304 return true;
11305 }
11306
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011307 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011308 return false;
11309}
11310
Owen Anderson53aa7a92009-08-10 22:56:29 +000011311static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011312 bool isSEXTLoad, SDValue &Base,
11313 SDValue &Offset, bool &isInc,
11314 SelectionDAG &DAG) {
11315 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11316 return false;
11317
11318 Base = Ptr->getOperand(0);
11319 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11320 int RHSC = (int)RHS->getZExtValue();
11321 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11322 assert(Ptr->getOpcode() == ISD::ADD);
11323 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011324 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011325 return true;
11326 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11327 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011328 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011329 return true;
11330 }
11331 }
11332
11333 return false;
11334}
11335
Evan Cheng10043e22007-01-19 07:51:42 +000011336/// getPreIndexedAddressParts - returns true by value, base pointer and
11337/// offset pointer and addressing mode by reference if the node's address
11338/// can be legally represented as pre-indexed load / store address.
11339bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011340ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11341 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011342 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011343 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011344 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011345 return false;
11346
Owen Anderson53aa7a92009-08-10 22:56:29 +000011347 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011348 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011349 bool isSEXTLoad = false;
11350 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11351 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011352 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011353 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11354 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11355 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011356 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011357 } else
11358 return false;
11359
11360 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011361 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011362 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011363 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11364 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011365 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011366 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011367 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011368 if (!isLegal)
11369 return false;
11370
11371 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11372 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011373}
11374
11375/// getPostIndexedAddressParts - returns true by value, base pointer and
11376/// offset pointer and addressing mode by reference if this node can be
11377/// combined with a load / store to form a post-indexed load / store.
11378bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011379 SDValue &Base,
11380 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011381 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011382 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011383 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011384 return false;
11385
Owen Anderson53aa7a92009-08-10 22:56:29 +000011386 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011387 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011388 bool isSEXTLoad = false;
11389 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011390 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011391 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011392 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11393 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011394 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011395 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011396 } else
11397 return false;
11398
11399 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011400 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011401 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011402 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011403 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011404 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011405 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11406 isInc, DAG);
11407 if (!isLegal)
11408 return false;
11409
Evan Chengf19384d2010-05-18 21:31:17 +000011410 if (Ptr != Base) {
11411 // Swap base ptr and offset to catch more post-index load / store when
11412 // it's legal. In Thumb2 mode, offset must be an immediate.
11413 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11414 !Subtarget->isThumb2())
11415 std::swap(Base, Offset);
11416
11417 // Post-indexed load / store update the base pointer.
11418 if (Ptr != Base)
11419 return false;
11420 }
11421
Evan Cheng84c6cda2009-07-02 07:28:31 +000011422 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11423 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011424}
11425
Jay Foada0653a32014-05-14 21:14:37 +000011426void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11427 APInt &KnownZero,
11428 APInt &KnownOne,
11429 const SelectionDAG &DAG,
11430 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011431 unsigned BitWidth = KnownOne.getBitWidth();
11432 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011433 switch (Op.getOpcode()) {
11434 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011435 case ARMISD::ADDC:
11436 case ARMISD::ADDE:
11437 case ARMISD::SUBC:
11438 case ARMISD::SUBE:
11439 // These nodes' second result is a boolean
11440 if (Op.getResNo() == 0)
11441 break;
11442 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11443 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011444 case ARMISD::CMOV: {
11445 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011446 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011447 if (KnownZero == 0 && KnownOne == 0) return;
11448
Dan Gohmanf990faf2008-02-13 00:35:47 +000011449 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011450 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011451 KnownZero &= KnownZeroRHS;
11452 KnownOne &= KnownOneRHS;
11453 return;
11454 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011455 case ISD::INTRINSIC_W_CHAIN: {
11456 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11457 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11458 switch (IntID) {
11459 default: return;
11460 case Intrinsic::arm_ldaex:
11461 case Intrinsic::arm_ldrex: {
11462 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11463 unsigned MemBits = VT.getScalarType().getSizeInBits();
11464 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11465 return;
11466 }
11467 }
11468 }
Evan Cheng10043e22007-01-19 07:51:42 +000011469 }
11470}
11471
11472//===----------------------------------------------------------------------===//
11473// ARM Inline Assembly Support
11474//===----------------------------------------------------------------------===//
11475
Evan Cheng078b0b02011-01-08 01:24:27 +000011476bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11477 // Looking for "rev" which is V6+.
11478 if (!Subtarget->hasV6Ops())
11479 return false;
11480
11481 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11482 std::string AsmStr = IA->getAsmString();
11483 SmallVector<StringRef, 4> AsmPieces;
11484 SplitString(AsmStr, AsmPieces, ";\n");
11485
11486 switch (AsmPieces.size()) {
11487 default: return false;
11488 case 1:
11489 AsmStr = AsmPieces[0];
11490 AsmPieces.clear();
11491 SplitString(AsmStr, AsmPieces, " \t,");
11492
11493 // rev $0, $1
11494 if (AsmPieces.size() == 3 &&
11495 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11496 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011497 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011498 if (Ty && Ty->getBitWidth() == 32)
11499 return IntrinsicLowering::LowerToByteSwap(CI);
11500 }
11501 break;
11502 }
11503
11504 return false;
11505}
11506
Silviu Baranga82d04262016-04-25 14:29:18 +000011507const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const {
11508 // At this point, we have to lower this constraint to something else, so we
11509 // lower it to an "r" or "w". However, by doing this we will force the result
11510 // to be in register, while the X constraint is much more permissive.
11511 //
11512 // Although we are correct (we are free to emit anything, without
11513 // constraints), we might break use cases that would expect us to be more
11514 // efficient and emit something else.
11515 if (!Subtarget->hasVFP2())
11516 return "r";
11517 if (ConstraintVT.isFloatingPoint())
11518 return "w";
11519 if (ConstraintVT.isVector() && Subtarget->hasNEON() &&
11520 (ConstraintVT.getSizeInBits() == 64 ||
11521 ConstraintVT.getSizeInBits() == 128))
11522 return "w";
11523
11524 return "r";
11525}
11526
Evan Cheng10043e22007-01-19 07:51:42 +000011527/// getConstraintType - Given a constraint letter, return the type of
11528/// constraint it is for this target.
11529ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011530ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011531 if (Constraint.size() == 1) {
11532 switch (Constraint[0]) {
11533 default: break;
11534 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011535 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011536 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011537 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011538 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011539 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011540 // An address with a single base register. Due to the way we
11541 // currently handle addresses it is the same as an 'r' memory constraint.
11542 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011543 }
Eric Christophere256cd02011-06-21 22:10:57 +000011544 } else if (Constraint.size() == 2) {
11545 switch (Constraint[0]) {
11546 default: break;
11547 // All 'U+' constraints are addresses.
11548 case 'U': return C_Memory;
11549 }
Evan Cheng10043e22007-01-19 07:51:42 +000011550 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011551 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011552}
11553
John Thompsone8360b72010-10-29 17:29:13 +000011554/// Examine constraint type and operand type and determine a weight value.
11555/// This object must already have been set up with the operand type
11556/// and the current alternative constraint selected.
11557TargetLowering::ConstraintWeight
11558ARMTargetLowering::getSingleConstraintMatchWeight(
11559 AsmOperandInfo &info, const char *constraint) const {
11560 ConstraintWeight weight = CW_Invalid;
11561 Value *CallOperandVal = info.CallOperandVal;
11562 // If we don't have a value, we can't do a match,
11563 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011564 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011565 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011566 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011567 // Look at the constraint type.
11568 switch (*constraint) {
11569 default:
11570 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11571 break;
11572 case 'l':
11573 if (type->isIntegerTy()) {
11574 if (Subtarget->isThumb())
11575 weight = CW_SpecificReg;
11576 else
11577 weight = CW_Register;
11578 }
11579 break;
11580 case 'w':
11581 if (type->isFloatingPointTy())
11582 weight = CW_Register;
11583 break;
11584 }
11585 return weight;
11586}
11587
Eric Christophercf2007c2011-06-30 23:50:52 +000011588typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011589RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11590 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011591 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011592 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011593 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011594 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011595 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011596 return RCPair(0U, &ARM::tGPRRegClass);
11597 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011598 case 'h': // High regs or no regs.
11599 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011600 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011601 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011602 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011603 if (Subtarget->isThumb1Only())
11604 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011605 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011606 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011607 if (VT == MVT::Other)
11608 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011609 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011610 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011611 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011612 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011613 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011614 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011615 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011616 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011617 if (VT == MVT::Other)
11618 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011619 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011620 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011621 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011622 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011623 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011624 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011625 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011626 case 't':
11627 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011628 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011629 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011630 }
11631 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011632 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011633 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011634
Eric Christopher11e4df72015-02-26 22:38:43 +000011635 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011636}
11637
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011638/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11639/// vector. If it is invalid, don't add anything to Ops.
11640void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011641 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011642 std::vector<SDValue>&Ops,
11643 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011644 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011645
Eric Christopherde9399b2011-06-02 23:16:42 +000011646 // Currently only support length 1 constraints.
11647 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011648
Eric Christopherde9399b2011-06-02 23:16:42 +000011649 char ConstraintLetter = Constraint[0];
11650 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011651 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011652 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011653 case 'I': case 'J': case 'K': case 'L':
11654 case 'M': case 'N': case 'O':
11655 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11656 if (!C)
11657 return;
11658
11659 int64_t CVal64 = C->getSExtValue();
11660 int CVal = (int) CVal64;
11661 // None of these constraints allow values larger than 32 bits. Check
11662 // that the value fits in an int.
11663 if (CVal != CVal64)
11664 return;
11665
Eric Christopherde9399b2011-06-02 23:16:42 +000011666 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011667 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011668 // Constant suitable for movw, must be between 0 and
11669 // 65535.
11670 if (Subtarget->hasV6T2Ops())
11671 if (CVal >= 0 && CVal <= 65535)
11672 break;
11673 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011674 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011675 if (Subtarget->isThumb1Only()) {
11676 // This must be a constant between 0 and 255, for ADD
11677 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011678 if (CVal >= 0 && CVal <= 255)
11679 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011680 } else if (Subtarget->isThumb2()) {
11681 // A constant that can be used as an immediate value in a
11682 // data-processing instruction.
11683 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11684 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011685 } else {
11686 // A constant that can be used as an immediate value in a
11687 // data-processing instruction.
11688 if (ARM_AM::getSOImmVal(CVal) != -1)
11689 break;
11690 }
11691 return;
11692
11693 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000011694 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011695 // This must be a constant between -255 and -1, for negated ADD
11696 // immediates. This can be used in GCC with an "n" modifier that
11697 // prints the negated value, for use with SUB instructions. It is
11698 // not useful otherwise but is implemented for compatibility.
11699 if (CVal >= -255 && CVal <= -1)
11700 break;
11701 } else {
11702 // This must be a constant between -4095 and 4095. It is not clear
11703 // what this constraint is intended for. Implemented for
11704 // compatibility with GCC.
11705 if (CVal >= -4095 && CVal <= 4095)
11706 break;
11707 }
11708 return;
11709
11710 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011711 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011712 // A 32-bit value where only one byte has a nonzero value. Exclude
11713 // zero to match GCC. This constraint is used by GCC internally for
11714 // constants that can be loaded with a move/shift combination.
11715 // It is not useful otherwise but is implemented for compatibility.
11716 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11717 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011718 } else if (Subtarget->isThumb2()) {
11719 // A constant whose bitwise inverse can be used as an immediate
11720 // value in a data-processing instruction. This can be used in GCC
11721 // with a "B" modifier that prints the inverted value, for use with
11722 // BIC and MVN instructions. It is not useful otherwise but is
11723 // implemented for compatibility.
11724 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11725 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011726 } else {
11727 // A constant whose bitwise inverse can be used as an immediate
11728 // value in a data-processing instruction. This can be used in GCC
11729 // with a "B" modifier that prints the inverted value, for use with
11730 // BIC and MVN instructions. It is not useful otherwise but is
11731 // implemented for compatibility.
11732 if (ARM_AM::getSOImmVal(~CVal) != -1)
11733 break;
11734 }
11735 return;
11736
11737 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011738 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011739 // This must be a constant between -7 and 7,
11740 // for 3-operand ADD/SUB immediate instructions.
11741 if (CVal >= -7 && CVal < 7)
11742 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011743 } else if (Subtarget->isThumb2()) {
11744 // A constant whose negation can be used as an immediate value in a
11745 // data-processing instruction. This can be used in GCC with an "n"
11746 // modifier that prints the negated value, for use with SUB
11747 // instructions. It is not useful otherwise but is implemented for
11748 // compatibility.
11749 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11750 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011751 } else {
11752 // A constant whose negation can be used as an immediate value in a
11753 // data-processing instruction. This can be used in GCC with an "n"
11754 // modifier that prints the negated value, for use with SUB
11755 // instructions. It is not useful otherwise but is implemented for
11756 // compatibility.
11757 if (ARM_AM::getSOImmVal(-CVal) != -1)
11758 break;
11759 }
11760 return;
11761
11762 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000011763 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011764 // This must be a multiple of 4 between 0 and 1020, for
11765 // ADD sp + immediate.
11766 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11767 break;
11768 } else {
11769 // A power of two or a constant between 0 and 32. This is used in
11770 // GCC for the shift amount on shifted register operands, but it is
11771 // useful in general for any shift amounts.
11772 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11773 break;
11774 }
11775 return;
11776
11777 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011778 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011779 // This must be a constant between 0 and 31, for shift amounts.
11780 if (CVal >= 0 && CVal <= 31)
11781 break;
11782 }
11783 return;
11784
11785 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011786 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011787 // This must be a multiple of 4 between -508 and 508, for
11788 // ADD/SUB sp = sp + immediate.
11789 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11790 break;
11791 }
11792 return;
11793 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011794 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011795 break;
11796 }
11797
11798 if (Result.getNode()) {
11799 Ops.push_back(Result);
11800 return;
11801 }
Dale Johannesence97d552010-06-25 21:55:36 +000011802 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011803}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011804
Scott Douglassd2974a62015-08-24 09:17:11 +000011805static RTLIB::Libcall getDivRemLibcall(
11806 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011807 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11808 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011809 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011810 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11811 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011812 RTLIB::Libcall LC;
11813 switch (SVT) {
11814 default: llvm_unreachable("Unexpected request for libcall!");
11815 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11816 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11817 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11818 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11819 }
11820 return LC;
11821}
11822
11823static TargetLowering::ArgListTy getDivRemArgList(
11824 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011825 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11826 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011827 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011828 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11829 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011830 TargetLowering::ArgListTy Args;
11831 TargetLowering::ArgListEntry Entry;
11832 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11833 EVT ArgVT = N->getOperand(i).getValueType();
11834 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11835 Entry.Node = N->getOperand(i);
11836 Entry.Ty = ArgTy;
11837 Entry.isSExt = isSigned;
11838 Entry.isZExt = !isSigned;
11839 Args.push_back(Entry);
11840 }
11841 return Args;
11842}
11843
Renato Golin87610692013-07-16 09:32:17 +000011844SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000011845 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
11846 Subtarget->isTargetGNUAEABI()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011847 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011848 unsigned Opcode = Op->getOpcode();
11849 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011850 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011851 bool isSigned = (Opcode == ISD::SDIVREM);
11852 EVT VT = Op->getValueType(0);
11853 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11854
Scott Douglassd2974a62015-08-24 09:17:11 +000011855 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11856 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011857 SDValue InChain = DAG.getEntryNode();
11858
Scott Douglassd2974a62015-08-24 09:17:11 +000011859 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11860 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011861
11862 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011863 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011864
Reid Kleckner343c3952014-11-20 23:51:47 +000011865 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011866
11867 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011868 TargetLowering::CallLoweringInfo CLI(DAG);
11869 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011870 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011871 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011872
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011873 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011874 return CallInfo.first;
11875}
11876
Scott Douglassbdef6042015-08-24 09:17:18 +000011877// Lowers REM using divmod helpers
11878// see RTABI section 4.2/4.3
11879SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11880 // Build return types (div and rem)
11881 std::vector<Type*> RetTyParams;
11882 Type *RetTyElement;
11883
11884 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11885 default: llvm_unreachable("Unexpected request for libcall!");
11886 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11887 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11888 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11889 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11890 }
11891
11892 RetTyParams.push_back(RetTyElement);
11893 RetTyParams.push_back(RetTyElement);
11894 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11895 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11896
11897 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11898 SimpleTy);
11899 SDValue InChain = DAG.getEntryNode();
11900 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11901 bool isSigned = N->getOpcode() == ISD::SREM;
11902 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11903 getPointerTy(DAG.getDataLayout()));
11904
11905 // Lower call
11906 CallLoweringInfo CLI(DAG);
11907 CLI.setChain(InChain)
11908 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11909 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11910 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11911
11912 // Return second (rem) result operand (first contains div)
11913 SDNode *ResNode = CallResult.first.getNode();
11914 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11915 return ResNode->getOperand(1);
11916}
11917
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011918SDValue
11919ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11920 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11921 SDLoc DL(Op);
11922
11923 // Get the inputs.
11924 SDValue Chain = Op.getOperand(0);
11925 SDValue Size = Op.getOperand(1);
11926
11927 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011928 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011929
11930 SDValue Flag;
11931 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11932 Flag = Chain.getValue(1);
11933
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011934 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011935 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11936
11937 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11938 Chain = NewSP.getValue(1);
11939
11940 SDValue Ops[2] = { NewSP, Chain };
11941 return DAG.getMergeValues(Ops, DL);
11942}
11943
Oliver Stannard51b1d462014-08-21 12:50:31 +000011944SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11945 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11946 "Unexpected type for custom-lowering FP_EXTEND");
11947
11948 RTLIB::Libcall LC;
11949 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11950
11951 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011952 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11953 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011954}
11955
11956SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11957 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11958 Subtarget->isFPOnlySP() &&
11959 "Unexpected type for custom-lowering FP_ROUND");
11960
11961 RTLIB::Libcall LC;
11962 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11963
11964 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011965 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11966 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011967}
11968
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011969bool
11970ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11971 // The ARM target isn't yet aware of offsets.
11972 return false;
11973}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011974
Jim Grosbach11013ed2010-07-16 23:05:05 +000011975bool ARM::isBitFieldInvertedMask(unsigned v) {
11976 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011977 return false;
11978
Jim Grosbach11013ed2010-07-16 23:05:05 +000011979 // there can be 1's on either or both "outsides", all the "inside"
11980 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011981 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011982}
11983
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011984/// isFPImmLegal - Returns true if the target can instruction select the
11985/// specified FP immediate natively. If false, the legalizer will
11986/// materialize the FP immediate as a load from a constant pool.
11987bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11988 if (!Subtarget->hasVFP3())
11989 return false;
11990 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011991 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011992 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011993 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011994 return false;
11995}
Bob Wilson5549d492010-09-21 17:56:22 +000011996
Wesley Peck527da1b2010-11-23 03:31:01 +000011997/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011998/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11999/// specified in the intrinsic calls.
12000bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
12001 const CallInst &I,
12002 unsigned Intrinsic) const {
12003 switch (Intrinsic) {
12004 case Intrinsic::arm_neon_vld1:
12005 case Intrinsic::arm_neon_vld2:
12006 case Intrinsic::arm_neon_vld3:
12007 case Intrinsic::arm_neon_vld4:
12008 case Intrinsic::arm_neon_vld2lane:
12009 case Intrinsic::arm_neon_vld3lane:
12010 case Intrinsic::arm_neon_vld4lane: {
12011 Info.opc = ISD::INTRINSIC_W_CHAIN;
12012 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012013 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012014 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012015 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12016 Info.ptrVal = I.getArgOperand(0);
12017 Info.offset = 0;
12018 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12019 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12020 Info.vol = false; // volatile loads with NEON intrinsics not supported
12021 Info.readMem = true;
12022 Info.writeMem = false;
12023 return true;
12024 }
12025 case Intrinsic::arm_neon_vst1:
12026 case Intrinsic::arm_neon_vst2:
12027 case Intrinsic::arm_neon_vst3:
12028 case Intrinsic::arm_neon_vst4:
12029 case Intrinsic::arm_neon_vst2lane:
12030 case Intrinsic::arm_neon_vst3lane:
12031 case Intrinsic::arm_neon_vst4lane: {
12032 Info.opc = ISD::INTRINSIC_VOID;
12033 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012034 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000012035 unsigned NumElts = 0;
12036 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000012037 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000012038 if (!ArgTy->isVectorTy())
12039 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012040 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012041 }
12042 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12043 Info.ptrVal = I.getArgOperand(0);
12044 Info.offset = 0;
12045 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12046 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12047 Info.vol = false; // volatile stores with NEON intrinsics not supported
12048 Info.readMem = false;
12049 Info.writeMem = true;
12050 return true;
12051 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012052 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012053 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012054 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012055 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
12056 Info.opc = ISD::INTRINSIC_W_CHAIN;
12057 Info.memVT = MVT::getVT(PtrTy->getElementType());
12058 Info.ptrVal = I.getArgOperand(0);
12059 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012060 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012061 Info.vol = true;
12062 Info.readMem = true;
12063 Info.writeMem = false;
12064 return true;
12065 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012066 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012067 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012068 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012069 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
12070 Info.opc = ISD::INTRINSIC_W_CHAIN;
12071 Info.memVT = MVT::getVT(PtrTy->getElementType());
12072 Info.ptrVal = I.getArgOperand(1);
12073 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012074 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012075 Info.vol = true;
12076 Info.readMem = false;
12077 Info.writeMem = true;
12078 return true;
12079 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012080 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012081 case Intrinsic::arm_strexd: {
12082 Info.opc = ISD::INTRINSIC_W_CHAIN;
12083 Info.memVT = MVT::i64;
12084 Info.ptrVal = I.getArgOperand(2);
12085 Info.offset = 0;
12086 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012087 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012088 Info.readMem = false;
12089 Info.writeMem = true;
12090 return true;
12091 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012092 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012093 case Intrinsic::arm_ldrexd: {
12094 Info.opc = ISD::INTRINSIC_W_CHAIN;
12095 Info.memVT = MVT::i64;
12096 Info.ptrVal = I.getArgOperand(0);
12097 Info.offset = 0;
12098 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012099 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012100 Info.readMem = true;
12101 Info.writeMem = false;
12102 return true;
12103 }
Bob Wilson5549d492010-09-21 17:56:22 +000012104 default:
12105 break;
12106 }
12107
12108 return false;
12109}
Juergen Ributzka659ce002014-01-28 01:20:14 +000012110
12111/// \brief Returns true if it is beneficial to convert a load of a constant
12112/// to just the constant itself.
12113bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
12114 Type *Ty) const {
12115 assert(Ty->isIntegerTy());
12116
12117 unsigned Bits = Ty->getPrimitiveSizeInBits();
12118 if (Bits == 0 || Bits > 32)
12119 return false;
12120 return true;
12121}
Tim Northover037f26f22014-04-17 18:22:47 +000012122
Robin Morisset5349e8e2014-09-18 18:56:04 +000012123Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
12124 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012125 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000012126
12127 // First, if the target has no DMB, see what fallback we can use.
12128 if (!Subtarget->hasDataBarrier()) {
12129 // Some ARMv6 cpus can support data barriers with an mcr instruction.
12130 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
12131 // here.
12132 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
12133 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
12134 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
12135 Builder.getInt32(0), Builder.getInt32(7),
12136 Builder.getInt32(10), Builder.getInt32(5)};
12137 return Builder.CreateCall(MCR, args);
12138 } else {
12139 // Instead of using barriers, atomic accesses on these subtargets use
12140 // libcalls.
12141 llvm_unreachable("makeDMB on a target so old that it has no barriers");
12142 }
12143 } else {
12144 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
12145 // Only a full system barrier exists in the M-class architectures.
12146 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
12147 Constant *CDomain = Builder.getInt32(Domain);
12148 return Builder.CreateCall(DMB, CDomain);
12149 }
Robin Morisseta47cb412014-09-03 21:01:03 +000012150}
12151
12152// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000012153Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012154 AtomicOrdering Ord, bool IsStore,
12155 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012156 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012157 case AtomicOrdering::NotAtomic:
12158 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012159 llvm_unreachable("Invalid fence: unordered/non-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012160 case AtomicOrdering::Monotonic:
12161 case AtomicOrdering::Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012162 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012163 case AtomicOrdering::SequentiallyConsistent:
Robin Morisseta47cb412014-09-03 21:01:03 +000012164 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012165 return nullptr; // Nothing to do
12166 /*FALLTHROUGH*/
JF Bastien800f87a2016-04-06 21:19:33 +000012167 case AtomicOrdering::Release:
12168 case AtomicOrdering::AcquireRelease:
Robin Morisseta47cb412014-09-03 21:01:03 +000012169 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000012170 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012171 // FIXME: add a comment with a link to documentation justifying this.
12172 else
Robin Morissetdedef332014-09-23 20:31:14 +000012173 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012174 }
Robin Morissetdedef332014-09-23 20:31:14 +000012175 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012176}
12177
Robin Morissetdedef332014-09-23 20:31:14 +000012178Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012179 AtomicOrdering Ord, bool IsStore,
12180 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012181 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012182 case AtomicOrdering::NotAtomic:
12183 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012184 llvm_unreachable("Invalid fence: unordered/not-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012185 case AtomicOrdering::Monotonic:
12186 case AtomicOrdering::Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012187 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012188 case AtomicOrdering::Acquire:
12189 case AtomicOrdering::AcquireRelease:
12190 case AtomicOrdering::SequentiallyConsistent:
Robin Morissetdedef332014-09-23 20:31:14 +000012191 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012192 }
Robin Morissetdedef332014-09-23 20:31:14 +000012193 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012194}
12195
Robin Morisseted3d48f2014-09-03 21:29:59 +000012196// Loads and stores less than 64-bits are already atomic; ones above that
12197// are doomed anyway, so defer to the default libcall and blame the OS when
12198// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12199// anything for those.
12200bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12201 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12202 return (Size == 64) && !Subtarget->isMClass();
12203}
Tim Northover037f26f22014-04-17 18:22:47 +000012204
Robin Morisseted3d48f2014-09-03 21:29:59 +000012205// Loads and stores less than 64-bits are already atomic; ones above that
12206// are doomed anyway, so defer to the default libcall and blame the OS when
12207// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12208// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012209// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12210// guarantee, see DDI0406C ARM architecture reference manual,
12211// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012212TargetLowering::AtomicExpansionKind
12213ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012214 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012215 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012216 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012217}
12218
12219// For the real atomic operations, we have ldrex/strex up to 32 bits,
12220// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012221TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012222ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012223 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012224 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012225 ? AtomicExpansionKind::LLSC
12226 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012227}
12228
Ahmed Bougacha52468672015-09-11 17:08:28 +000012229bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12230 AtomicCmpXchgInst *AI) const {
Tim Northoverb629c772016-04-18 21:48:55 +000012231 // At -O0, fast-regalloc cannot cope with the live vregs necessary to
12232 // implement cmpxchg without spilling. If the address being exchanged is also
12233 // on the stack and close enough to the spill slot, this can lead to a
12234 // situation where the monitor always gets cleared and the atomic operation
12235 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
12236 return getTargetMachine().getOptLevel() != 0;
Ahmed Bougacha52468672015-09-11 17:08:28 +000012237}
12238
James Y Knightf44fc522016-03-16 22:12:04 +000012239bool ARMTargetLowering::shouldInsertFencesForAtomic(
12240 const Instruction *I) const {
12241 return InsertFencesForAtomic;
12242}
12243
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012244// This has so far only been implemented for MachO.
12245bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012246 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012247}
12248
Quentin Colombetc32615d2014-10-31 17:52:53 +000012249bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12250 unsigned &Cost) const {
12251 // If we do not have NEON, vector types are not natively supported.
12252 if (!Subtarget->hasNEON())
12253 return false;
12254
12255 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012256 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012257 // better to leave at float as we have more freedom in the addressing mode for
12258 // those.
12259 if (VectorTy->isFPOrFPVectorTy())
12260 return false;
12261
12262 // If the index is unknown at compile time, this is very expensive to lower
12263 // and it is not possible to combine the store with the extract.
12264 if (!isa<ConstantInt>(Idx))
12265 return false;
12266
12267 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12268 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12269 // We can do a store + vector extract on any vector that fits perfectly in a D
12270 // or Q register.
12271 if (BitWidth == 64 || BitWidth == 128) {
12272 Cost = 0;
12273 return true;
12274 }
12275 return false;
12276}
12277
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012278bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12279 return Subtarget->hasV6T2Ops();
12280}
12281
12282bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12283 return Subtarget->hasV6T2Ops();
12284}
12285
Tim Northover037f26f22014-04-17 18:22:47 +000012286Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12287 AtomicOrdering Ord) const {
12288 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12289 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
JF Bastien800f87a2016-04-06 21:19:33 +000012290 bool IsAcquire = isAcquireOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012291
12292 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12293 // intrinsic must return {i32, i32} and we have to recombine them into a
12294 // single i64 here.
12295 if (ValTy->getPrimitiveSizeInBits() == 64) {
12296 Intrinsic::ID Int =
12297 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12298 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12299
12300 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12301 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12302
12303 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12304 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012305 if (!Subtarget->isLittle())
12306 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012307 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12308 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12309 return Builder.CreateOr(
12310 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12311 }
12312
12313 Type *Tys[] = { Addr->getType() };
12314 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12315 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12316
12317 return Builder.CreateTruncOrBitCast(
12318 Builder.CreateCall(Ldrex, Addr),
12319 cast<PointerType>(Addr->getType())->getElementType());
12320}
12321
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012322void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12323 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012324 if (!Subtarget->hasV7Ops())
12325 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012326 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12327 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12328}
12329
Tim Northover037f26f22014-04-17 18:22:47 +000012330Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12331 Value *Addr,
12332 AtomicOrdering Ord) const {
12333 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
JF Bastien800f87a2016-04-06 21:19:33 +000012334 bool IsRelease = isReleaseOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012335
12336 // Since the intrinsics must have legal type, the i64 intrinsics take two
12337 // parameters: "i32, i32". We must marshal Val into the appropriate form
12338 // before the call.
12339 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12340 Intrinsic::ID Int =
12341 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12342 Function *Strex = Intrinsic::getDeclaration(M, Int);
12343 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12344
12345 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12346 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012347 if (!Subtarget->isLittle())
12348 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012349 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012350 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012351 }
12352
12353 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12354 Type *Tys[] = { Addr->getType() };
12355 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12356
David Blaikieff6409d2015-05-18 22:13:54 +000012357 return Builder.CreateCall(
12358 Strex, {Builder.CreateZExtOrBitCast(
12359 Val, Strex->getFunctionType()->getParamType(0)),
12360 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012361}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012362
Hao Liu2cd34bb2015-06-26 02:45:36 +000012363/// \brief Lower an interleaved load into a vldN intrinsic.
12364///
12365/// E.g. Lower an interleaved load (Factor = 2):
12366/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12367/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12368/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12369///
12370/// Into:
12371/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12372/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12373/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12374bool ARMTargetLowering::lowerInterleavedLoad(
12375 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12376 ArrayRef<unsigned> Indices, unsigned Factor) const {
12377 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12378 "Invalid interleave factor");
12379 assert(!Shuffles.empty() && "Empty shufflevector input");
12380 assert(Shuffles.size() == Indices.size() &&
12381 "Unmatched number of shufflevectors and indices");
12382
12383 VectorType *VecTy = Shuffles[0]->getType();
12384 Type *EltTy = VecTy->getVectorElementType();
12385
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012386 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012387 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
12388 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012389
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012390 // Skip if we do not have NEON and skip illegal vector types and vector types
12391 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12392 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012393 return false;
12394
12395 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12396 // load integer vectors first and then convert to pointer vectors.
12397 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012398 VecTy =
12399 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012400
12401 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12402 Intrinsic::arm_neon_vld3,
12403 Intrinsic::arm_neon_vld4};
12404
Hao Liu2cd34bb2015-06-26 02:45:36 +000012405 IRBuilder<> Builder(LI);
12406 SmallVector<Value *, 2> Ops;
12407
12408 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12409 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12410 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12411
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012412 Type *Tys[] = { VecTy, Int8Ptr };
12413 Function *VldnFunc =
12414 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012415 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12416
12417 // Replace uses of each shufflevector with the corresponding vector loaded
12418 // by ldN.
12419 for (unsigned i = 0; i < Shuffles.size(); i++) {
12420 ShuffleVectorInst *SV = Shuffles[i];
12421 unsigned Index = Indices[i];
12422
12423 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12424
12425 // Convert the integer vector to pointer vector if the element is pointer.
12426 if (EltTy->isPointerTy())
12427 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12428
12429 SV->replaceAllUsesWith(SubVec);
12430 }
12431
12432 return true;
12433}
12434
12435/// \brief Get a mask consisting of sequential integers starting from \p Start.
12436///
12437/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12438static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12439 unsigned NumElts) {
12440 SmallVector<Constant *, 16> Mask;
12441 for (unsigned i = 0; i < NumElts; i++)
12442 Mask.push_back(Builder.getInt32(Start + i));
12443
12444 return ConstantVector::get(Mask);
12445}
12446
12447/// \brief Lower an interleaved store into a vstN intrinsic.
12448///
12449/// E.g. Lower an interleaved store (Factor = 3):
12450/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12451/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12452/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12453///
12454/// Into:
12455/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12456/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12457/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12458/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12459///
12460/// Note that the new shufflevectors will be removed and we'll only generate one
12461/// vst3 instruction in CodeGen.
12462bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12463 ShuffleVectorInst *SVI,
12464 unsigned Factor) const {
12465 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12466 "Invalid interleave factor");
12467
12468 VectorType *VecTy = SVI->getType();
12469 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12470 "Invalid interleaved store");
12471
12472 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12473 Type *EltTy = VecTy->getVectorElementType();
12474 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12475
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012476 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012477 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
12478 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012479
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012480 // Skip if we do not have NEON and skip illegal vector types and vector types
12481 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12482 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12483 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012484 return false;
12485
12486 Value *Op0 = SVI->getOperand(0);
12487 Value *Op1 = SVI->getOperand(1);
12488 IRBuilder<> Builder(SI);
12489
12490 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12491 // vectors to integer vectors.
12492 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012493 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012494
12495 // Convert to the corresponding integer vector.
12496 Type *IntVecTy =
12497 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12498 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12499 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12500
12501 SubVecTy = VectorType::get(IntTy, NumSubElts);
12502 }
12503
Craig Topper26260942015-10-18 05:15:34 +000012504 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12505 Intrinsic::arm_neon_vst3,
12506 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012507 SmallVector<Value *, 6> Ops;
12508
12509 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12510 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12511
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012512 Type *Tys[] = { Int8Ptr, SubVecTy };
12513 Function *VstNFunc = Intrinsic::getDeclaration(
12514 SI->getModule(), StoreInts[Factor - 2], Tys);
12515
Hao Liu2cd34bb2015-06-26 02:45:36 +000012516 // Split the shufflevector operands into sub vectors for the new vstN call.
12517 for (unsigned i = 0; i < Factor; i++)
12518 Ops.push_back(Builder.CreateShuffleVector(
12519 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12520
12521 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12522 Builder.CreateCall(VstNFunc, Ops);
12523 return true;
12524}
12525
Oliver Stannardc24f2172014-05-09 14:01:47 +000012526enum HABaseType {
12527 HA_UNKNOWN = 0,
12528 HA_FLOAT,
12529 HA_DOUBLE,
12530 HA_VECT64,
12531 HA_VECT128
12532};
12533
12534static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12535 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012536 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012537 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12538 uint64_t SubMembers = 0;
12539 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12540 return false;
12541 Members += SubMembers;
12542 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012543 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012544 uint64_t SubMembers = 0;
12545 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12546 return false;
12547 Members += SubMembers * AT->getNumElements();
12548 } else if (Ty->isFloatTy()) {
12549 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12550 return false;
12551 Members = 1;
12552 Base = HA_FLOAT;
12553 } else if (Ty->isDoubleTy()) {
12554 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12555 return false;
12556 Members = 1;
12557 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012558 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012559 Members = 1;
12560 switch (Base) {
12561 case HA_FLOAT:
12562 case HA_DOUBLE:
12563 return false;
12564 case HA_VECT64:
12565 return VT->getBitWidth() == 64;
12566 case HA_VECT128:
12567 return VT->getBitWidth() == 128;
12568 case HA_UNKNOWN:
12569 switch (VT->getBitWidth()) {
12570 case 64:
12571 Base = HA_VECT64;
12572 return true;
12573 case 128:
12574 Base = HA_VECT128;
12575 return true;
12576 default:
12577 return false;
12578 }
12579 }
12580 }
12581
12582 return (Members > 0 && Members <= 4);
12583}
12584
Tim Northovere95c5b32015-02-24 17:22:34 +000012585/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12586/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12587/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012588bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12589 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012590 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12591 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012592 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012593
12594 HABaseType Base = HA_UNKNOWN;
12595 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012596 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12597 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12598
12599 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12600 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012601}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012602
12603unsigned ARMTargetLowering::getExceptionPointerRegister(
12604 const Constant *PersonalityFn) const {
12605 // Platforms which do not use SjLj EH may return values in these registers
12606 // via the personality function.
12607 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12608}
12609
12610unsigned ARMTargetLowering::getExceptionSelectorRegister(
12611 const Constant *PersonalityFn) const {
12612 // Platforms which do not use SjLj EH may return values in these registers
12613 // via the personality function.
12614 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12615}
Manman Ren5e9e65e2016-01-12 00:47:18 +000012616
12617void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
12618 // Update IsSplitCSR in ARMFunctionInfo.
12619 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
12620 AFI->setIsSplitCSR(true);
12621}
12622
12623void ARMTargetLowering::insertCopiesSplitCSR(
12624 MachineBasicBlock *Entry,
12625 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
12626 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
12627 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
12628 if (!IStart)
12629 return;
12630
12631 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
12632 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000012633 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000012634 for (const MCPhysReg *I = IStart; *I; ++I) {
12635 const TargetRegisterClass *RC = nullptr;
12636 if (ARM::GPRRegClass.contains(*I))
12637 RC = &ARM::GPRRegClass;
12638 else if (ARM::DPRRegClass.contains(*I))
12639 RC = &ARM::DPRRegClass;
12640 else
12641 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12642
12643 unsigned NewVR = MRI->createVirtualRegister(RC);
12644 // Create copy from CSR to a virtual register.
12645 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12646 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12647 // nounwind. If we want to generalize this later, we may need to emit
12648 // CFI pseudo-instructions.
12649 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12650 Attribute::NoUnwind) &&
12651 "Function should be nounwind in insertCopiesSplitCSR!");
12652 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000012653 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012654 .addReg(*I);
12655
Manman Rene5f807f2016-01-15 20:24:11 +000012656 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000012657 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000012658 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12659 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012660 .addReg(NewVR);
12661 }
12662}