blob: f240dca1b07bf250625e432610ad88ca08697cde [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000026#include "llvm/ADT/StringSwitch.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000027#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000028#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000041#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
John Brawn0dbcd652015-03-18 12:01:59 +000044#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000045#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000047#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000049#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000053#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000054#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000055using namespace llvm;
56
Chandler Carruth84e68b22014-04-22 02:41:26 +000057#define DEBUG_TYPE "arm-isel"
58
Dale Johannesend679ff72010-06-03 21:09:53 +000059STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000060STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000061STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000062
Evan Chengf128bdc2010-06-16 07:35:02 +000063static cl::opt<bool>
64ARMInterworking("arm-interworking", cl::Hidden,
65 cl::desc("Enable / disable ARM interworking (for debugging only)"),
66 cl::init(true));
67
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000068namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000069 class ARMCCState : public CCState {
70 public:
71 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000072 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
73 ParmContext PC)
74 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000075 assert(((PC == Call) || (PC == Prologue)) &&
76 "ARMCCState users must specify whether their context is call"
77 "or prologue generation.");
78 CallOrPrologue = PC;
79 }
80 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081}
Cameron Zwarich89019782011-06-10 20:59:24 +000082
Stuart Hastings45fe3c32011-04-20 16:47:52 +000083// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000084static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085 ARM::R0, ARM::R1, ARM::R2, ARM::R3
86};
87
Craig Topper4fa625f2012-08-12 03:16:37 +000088void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
89 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000090 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000091 setOperationAction(ISD::LOAD, VT, Promote);
92 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000093
Craig Topper4fa625f2012-08-12 03:16:37 +000094 setOperationAction(ISD::STORE, VT, Promote);
95 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000096 }
97
Craig Topper4fa625f2012-08-12 03:16:37 +000098 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +000099 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 setOperationAction(ISD::SETCC, VT, Custom);
101 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
102 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000103 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000104 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
105 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
107 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000108 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
110 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
112 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000113 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000114 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
115 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
116 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
117 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
118 setOperationAction(ISD::SELECT, VT, Expand);
119 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000120 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000122 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SHL, VT, Custom);
124 setOperationAction(ISD::SRA, VT, Custom);
125 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000126 }
127
128 // Promote all bit-wise operations.
129 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000130 setOperationAction(ISD::AND, VT, Promote);
131 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
132 setOperationAction(ISD::OR, VT, Promote);
133 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::XOR, VT, Promote);
135 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000136 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000137
138 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000139 setOperationAction(ISD::SDIV, VT, Expand);
140 setOperationAction(ISD::UDIV, VT, Expand);
141 setOperationAction(ISD::FDIV, VT, Expand);
142 setOperationAction(ISD::SREM, VT, Expand);
143 setOperationAction(ISD::UREM, VT, Expand);
144 setOperationAction(ISD::FREM, VT, Expand);
James Molloya6702e22015-07-17 17:10:55 +0000145
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000146 if (!VT.isFloatingPoint() &&
147 VT != MVT::v2i64 && VT != MVT::v1i64)
148 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
149 setOperationAction(Opcode, VT, Legal);
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000158 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Eric Christopher1889fdc2015-01-29 00:19:39 +0000162ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
163 const ARMSubtarget &STI)
164 : TargetLowering(TM), Subtarget(&STI) {
165 RegInfo = Subtarget->getRegisterInfo();
166 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000167
Duncan Sandsf2641e12011-09-06 19:07:46 +0000168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
169
Tim Northoverd6a729b2014-01-06 14:28:05 +0000170 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000172 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000174 static const struct {
175 const RTLIB::Libcall Op;
176 const char * const Name;
177 const ISD::CondCode Cond;
178 } LibraryCalls[] = {
179 // Single-precision floating-point arithmetic.
180 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
181 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
182 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
183 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000184
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000185 // Double-precision floating-point arithmetic.
186 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
187 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
189 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000190
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000191 // Single-precision comparisons.
192 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
193 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
194 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
195 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
196 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
197 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
198 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
199 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000200
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000201 // Double-precision comparisons.
202 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
203 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
204 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
205 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
206 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
207 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
208 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
209 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000210
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000211 // Floating-point to integer conversions.
212 // i64 conversions are done via library routines even when generating VFP
213 // instructions, so use the same ones.
214 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
215 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
216 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
217 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000219 // Conversions between floating types.
220 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
221 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000222
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000223 // Integer to floating-point conversions.
224 // i64 conversions are done via library routines even when generating VFP
225 // instructions, so use the same ones.
226 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
227 // e.g., __floatunsidf vs. __floatunssidfvfp.
228 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
229 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
230 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
231 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
232 };
Evan Cheng10043e22007-01-19 07:51:42 +0000233
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000234 for (const auto &LC : LibraryCalls) {
235 setLibcallName(LC.Op, LC.Name);
236 if (LC.Cond != ISD::SETCC_INVALID)
237 setCmpLibcallCC(LC.Op, LC.Cond);
238 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000239 }
Tim Northover8b403662015-10-28 22:51:16 +0000240
241 // Set the correct calling convention for ARMv7k WatchOS. It's just
242 // AAPCS_VFP for functions as simple as libcalls.
Tim Northover042a6c12016-01-27 19:32:29 +0000243 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000244 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i)
245 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP);
246 }
Evan Cheng10043e22007-01-19 07:51:42 +0000247 }
248
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000249 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000250 setLibcallName(RTLIB::SHL_I128, nullptr);
251 setLibcallName(RTLIB::SRL_I128, nullptr);
252 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000253
Renato Golin6d435f12015-11-09 12:40:30 +0000254 // RTLIB
255 if (Subtarget->isAAPCS_ABI() &&
256 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
257 Subtarget->isTargetAndroid())) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000258 static const struct {
259 const RTLIB::Libcall Op;
260 const char * const Name;
261 const CallingConv::ID CC;
262 const ISD::CondCode Cond;
263 } LibraryCalls[] = {
264 // Double-precision floating-point arithmetic helper functions
265 // RTABI chapter 4.1.2, Table 2
266 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
267 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
268 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000270
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000271 // Double-precision floating-point comparison helper functions
272 // RTABI chapter 4.1.2, Table 3
273 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
275 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000281
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000282 // Single-precision floating-point arithmetic helper functions
283 // RTABI chapter 4.1.2, Table 4
284 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
285 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
286 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000288
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000289 // Single-precision floating-point comparison helper functions
290 // RTABI chapter 4.1.2, Table 5
291 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
293 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000299
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000300 // Floating-point to integer conversions.
301 // RTABI chapter 4.1.2, Table 6
302 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000310
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000311 // Conversions between floating types.
312 // RTABI chapter 4.1.2, Table 7
313 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000314 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000315 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000316
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000317 // Integer to floating-point conversions.
318 // RTABI chapter 4.1.2, Table 8
319 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000327
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000328 // Long long helper functions
329 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000330 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000334
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000335 // Integer division functions
336 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000337 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000345 };
346
347 for (const auto &LC : LibraryCalls) {
348 setLibcallName(LC.Op, LC.Name);
349 setLibcallCallingConv(LC.Op, LC.CC);
350 if (LC.Cond != ISD::SETCC_INVALID)
351 setCmpLibcallCC(LC.Op, LC.Cond);
352 }
Renato Golin6d435f12015-11-09 12:40:30 +0000353
354 // EABI dependent RTLIB
355 if (TM.Options.EABIVersion == EABI::EABI4 ||
356 TM.Options.EABIVersion == EABI::EABI5) {
357 static const struct {
358 const RTLIB::Libcall Op;
359 const char *const Name;
360 const CallingConv::ID CC;
361 const ISD::CondCode Cond;
362 } MemOpsLibraryCalls[] = {
363 // Memory operations
364 // RTABI chapter 4.3.4
365 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
366 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
367 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
368 };
369
370 for (const auto &LC : MemOpsLibraryCalls) {
371 setLibcallName(LC.Op, LC.Name);
372 setLibcallCallingConv(LC.Op, LC.CC);
373 if (LC.Cond != ISD::SETCC_INVALID)
374 setCmpLibcallCC(LC.Op, LC.Cond);
375 }
376 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000377 }
378
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000379 if (Subtarget->isTargetWindows()) {
380 static const struct {
381 const RTLIB::Libcall Op;
382 const char * const Name;
383 const CallingConv::ID CC;
384 } LibraryCalls[] = {
385 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
386 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
387 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
388 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
389 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
390 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
391 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
392 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
393 };
394
395 for (const auto &LC : LibraryCalls) {
396 setLibcallName(LC.Op, LC.Name);
397 setLibcallCallingConv(LC.Op, LC.CC);
398 }
399 }
400
Bob Wilsonbc158992011-10-07 16:59:21 +0000401 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000402 if (Subtarget->isTargetWatchOS() ||
403 (Subtarget->isTargetIOS() &&
404 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000405 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
406 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
407 }
408
Oliver Stannard11790b22014-08-11 09:12:32 +0000409 // The half <-> float conversion functions are always soft-float, but are
410 // needed for some targets which use a hard-float calling convention by
411 // default.
412 if (Subtarget->isAAPCS_ABI()) {
413 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
415 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
416 } else {
417 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
418 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
419 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
420 }
421
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000422 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
423 // a __gnu_ prefix (which is the default).
424 if (Subtarget->isTargetAEABI()) {
425 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
426 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
427 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
428 }
429
David Goodwin22c2fba2009-07-08 23:10:31 +0000430 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000432 else
Craig Topperc7242e02012-04-20 07:30:17 +0000433 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000434 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000435 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000436 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000437 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000438 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000439
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000440 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000441 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000442 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000443 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
444 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
445 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
446 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000447
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000448 setOperationAction(ISD::MULHS, VT, Expand);
449 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
450 setOperationAction(ISD::MULHU, VT, Expand);
451 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000452
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000453 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000454 }
455
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000456 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000457 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000458
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000459 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
460 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
461
Bob Wilson2e076c42009-06-22 23:27:02 +0000462 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000463 addDRTypeForNEON(MVT::v2f32);
464 addDRTypeForNEON(MVT::v8i8);
465 addDRTypeForNEON(MVT::v4i16);
466 addDRTypeForNEON(MVT::v2i32);
467 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000468
Owen Anderson9f944592009-08-11 20:47:22 +0000469 addQRTypeForNEON(MVT::v4f32);
470 addQRTypeForNEON(MVT::v2f64);
471 addQRTypeForNEON(MVT::v16i8);
472 addQRTypeForNEON(MVT::v8i16);
473 addQRTypeForNEON(MVT::v4i32);
474 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000475
Bob Wilson194a2512009-09-15 23:55:57 +0000476 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
477 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000478 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
479 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000480 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
481 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
482 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000483 // FIXME: Code duplication: FDIV and FREM are expanded always, see
484 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000485 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
486 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000487 // FIXME: Create unittest.
488 // In another words, find a way when "copysign" appears in DAG with vector
489 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000490 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000491 // FIXME: Code duplication: SETCC has custom operation action, see
492 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000493 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000494 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000495 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
496 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
497 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
498 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
499 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
500 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
501 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
503 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
504 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
505 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
506 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000507 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000508 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
509 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
510 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
511 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
512 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000513 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000514
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000515 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
516 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
517 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
518 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
519 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
520 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
521 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
522 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
523 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
524 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000525 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
526 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
527 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
528 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000529 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000530
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000531 // Mark v2f32 intrinsics.
532 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
533 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
534 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
535 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
536 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
537 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
538 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
539 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
540 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
541 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
542 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
543 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
544 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
545 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
546 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
547
Bob Wilson6cc46572009-09-16 00:32:15 +0000548 // Neon does not support some operations on v1i64 and v2i64 types.
549 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000550 // Custom handling for some quad-vector types to detect VMULL.
551 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
552 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
553 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000554 // Custom handling for some vector types to avoid expensive expansions
555 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
556 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
557 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
558 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000559 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
560 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000561 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000562 // a destination type that is wider than the source, and nor does
563 // it have a FP_TO_[SU]INT instruction with a narrower destination than
564 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000565 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
566 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000567 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
568 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000569
Eli Friedmane6385e62012-11-15 22:44:27 +0000570 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000571 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000572
Evan Chengb4eae132012-12-04 22:41:50 +0000573 // NEON does not have single instruction CTPOP for vectors with element
574 // types wider than 8-bits. However, custom lowering can leverage the
575 // v8i8/v16i8 vcnt instruction.
576 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
577 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
578 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
579 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
Benjamin Kramer569efd22016-03-31 19:42:04 +0000580 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand);
581 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand);
Evan Chengb4eae132012-12-04 22:41:50 +0000582
Logan Chien0a43abc2015-07-13 15:37:30 +0000583 // NEON does not have single instruction CTTZ for vectors.
584 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
585 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
586 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
587 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
588
589 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
590 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
591 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
592 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
593
594 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
595 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
596 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
597 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
598
599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
600 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
602 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
603
Jim Grosbach5f215872013-02-27 21:31:12 +0000604 // NEON only has FMA instructions as of VFP4.
605 if (!Subtarget->hasVFP4()) {
606 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
607 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
608 }
609
Bob Wilson06fce872011-02-07 17:43:21 +0000610 setTargetDAGCombine(ISD::INTRINSIC_VOID);
611 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000612 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
613 setTargetDAGCombine(ISD::SHL);
614 setTargetDAGCombine(ISD::SRL);
615 setTargetDAGCombine(ISD::SRA);
616 setTargetDAGCombine(ISD::SIGN_EXTEND);
617 setTargetDAGCombine(ISD::ZERO_EXTEND);
618 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000619 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000620 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000621 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
622 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000623 setTargetDAGCombine(ISD::FP_TO_SINT);
624 setTargetDAGCombine(ISD::FP_TO_UINT);
625 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000626 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000627
James Molloy547d4c02012-02-20 09:24:05 +0000628 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000629 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
630 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000631 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000632 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
633 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
634 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000635 }
James Molloy547d4c02012-02-20 09:24:05 +0000636 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000637 }
638
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000639 // ARM and Thumb2 support UMLAL/SMLAL.
640 if (!Subtarget->isThumb1Only())
641 setTargetDAGCombine(ISD::ADDC);
642
Oliver Stannard51b1d462014-08-21 12:50:31 +0000643 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000644 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000645 // operations, f64 is legal for the few double-precision instructions which
646 // are present However, no double-precision operations other than moves,
647 // loads and stores are provided by the hardware.
648 setOperationAction(ISD::FADD, MVT::f64, Expand);
649 setOperationAction(ISD::FSUB, MVT::f64, Expand);
650 setOperationAction(ISD::FMUL, MVT::f64, Expand);
651 setOperationAction(ISD::FMA, MVT::f64, Expand);
652 setOperationAction(ISD::FDIV, MVT::f64, Expand);
653 setOperationAction(ISD::FREM, MVT::f64, Expand);
654 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
655 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
656 setOperationAction(ISD::FNEG, MVT::f64, Expand);
657 setOperationAction(ISD::FABS, MVT::f64, Expand);
658 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
659 setOperationAction(ISD::FSIN, MVT::f64, Expand);
660 setOperationAction(ISD::FCOS, MVT::f64, Expand);
661 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
662 setOperationAction(ISD::FPOW, MVT::f64, Expand);
663 setOperationAction(ISD::FLOG, MVT::f64, Expand);
664 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
665 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
666 setOperationAction(ISD::FEXP, MVT::f64, Expand);
667 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
668 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
669 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
670 setOperationAction(ISD::FRINT, MVT::f64, Expand);
671 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
672 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000673 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
674 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
675 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
676 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
677 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
678 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000679 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
680 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
681 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000682
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000683 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000684
Tim Northover4e80b582014-07-18 13:01:19 +0000685 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000686 for (MVT VT : MVT::fp_valuetypes()) {
687 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
688 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
689 }
Tim Northover4e80b582014-07-18 13:01:19 +0000690
691 // ... or truncating stores
692 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
693 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
694 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000695
Duncan Sands95d46ef2008-01-23 20:39:46 +0000696 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000697 for (MVT VT : MVT::integer_valuetypes())
698 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000699
Evan Cheng10043e22007-01-19 07:51:42 +0000700 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000701 if (!Subtarget->isThumb1Only()) {
702 for (unsigned im = (unsigned)ISD::PRE_INC;
703 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000704 setIndexedLoadAction(im, MVT::i1, Legal);
705 setIndexedLoadAction(im, MVT::i8, Legal);
706 setIndexedLoadAction(im, MVT::i16, Legal);
707 setIndexedLoadAction(im, MVT::i32, Legal);
708 setIndexedStoreAction(im, MVT::i1, Legal);
709 setIndexedStoreAction(im, MVT::i8, Legal);
710 setIndexedStoreAction(im, MVT::i16, Legal);
711 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000712 }
Evan Cheng10043e22007-01-19 07:51:42 +0000713 }
714
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000715 setOperationAction(ISD::SADDO, MVT::i32, Custom);
716 setOperationAction(ISD::UADDO, MVT::i32, Custom);
717 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
718 setOperationAction(ISD::USUBO, MVT::i32, Custom);
719
Evan Cheng10043e22007-01-19 07:51:42 +0000720 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000721 setOperationAction(ISD::MUL, MVT::i64, Expand);
722 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000723 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000724 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
725 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000726 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000727 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000728 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000729 setOperationAction(ISD::MULHS, MVT::i32, Expand);
730
Jim Grosbach5d994042009-10-31 19:38:01 +0000731 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000732 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000733 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000734 setOperationAction(ISD::SRL, MVT::i64, Custom);
735 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000736
Evan Chenge8916542011-08-30 01:34:54 +0000737 if (!Subtarget->isThumb1Only()) {
738 // FIXME: We should do this for Thumb1 as well.
739 setOperationAction(ISD::ADDC, MVT::i32, Custom);
740 setOperationAction(ISD::ADDE, MVT::i32, Custom);
741 setOperationAction(ISD::SUBC, MVT::i32, Custom);
742 setOperationAction(ISD::SUBE, MVT::i32, Custom);
743 }
744
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000745 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000746 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
747
Evan Cheng10043e22007-01-19 07:51:42 +0000748 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000749 setOperationAction(ISD::ROTL, MVT::i32, Expand);
750 for (MVT VT : MVT::vector_valuetypes()) {
751 setOperationAction(ISD::ROTL, VT, Expand);
752 setOperationAction(ISD::ROTR, VT, Expand);
753 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000754 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000755 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000756 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000757 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000758
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000759 // These just redirect to CTTZ and CTLZ on ARM.
760 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
761 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
762
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";
1123 case ARMISD::tCALL: return "ARMISD::tCALL";
1124 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1125 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001126 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001127 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001128 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001129 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1130 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001131 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001132 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001133 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1134 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001135 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001136 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001137
Evan Cheng10043e22007-01-19 07:51:42 +00001138 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001139
Evan Cheng10043e22007-01-19 07:51:42 +00001140 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1141 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1142 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001143
Evan Chenge8916542011-08-30 01:34:54 +00001144 case ARMISD::ADDC: return "ARMISD::ADDC";
1145 case ARMISD::ADDE: return "ARMISD::ADDE";
1146 case ARMISD::SUBC: return "ARMISD::SUBC";
1147 case ARMISD::SUBE: return "ARMISD::SUBE";
1148
Bob Wilson22806742010-09-22 22:09:21 +00001149 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1150 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001151
Evan Chengec6d7c92009-10-28 06:55:03 +00001152 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001153 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1154 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001155
Dale Johannesend679ff72010-06-03 21:09:53 +00001156 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001157
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001158 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001159
Evan Chengb972e562009-08-07 00:34:42 +00001160 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1161
Bob Wilson7ed59712010-10-30 00:54:37 +00001162 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001163
Evan Cheng8740ee32010-11-03 06:34:55 +00001164 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1165
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001166 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001167 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001168
Bob Wilson2e076c42009-06-22 23:27:02 +00001169 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001170 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001171 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001172 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1173 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001174 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1175 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001176 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1177 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001178 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1179 case ARMISD::VTST: return "ARMISD::VTST";
1180
1181 case ARMISD::VSHL: return "ARMISD::VSHL";
1182 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1183 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001184 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1185 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1186 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1187 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1188 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1189 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1190 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1191 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1192 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1193 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1194 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1195 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001196 case ARMISD::VSLI: return "ARMISD::VSLI";
1197 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001198 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1199 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001200 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001201 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001202 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001203 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001204 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001205 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001206 case ARMISD::VREV64: return "ARMISD::VREV64";
1207 case ARMISD::VREV32: return "ARMISD::VREV32";
1208 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001209 case ARMISD::VZIP: return "ARMISD::VZIP";
1210 case ARMISD::VUZP: return "ARMISD::VUZP";
1211 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001212 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1213 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001214 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1215 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001216 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1217 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001218 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001219 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001220 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1221 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001222 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001223 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001224 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1225 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1226 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001227 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1228 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1229 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1230 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1231 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1232 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1233 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1234 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1235 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1236 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1237 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1238 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1239 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1240 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1241 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1242 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1243 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001244 }
Matthias Braund04893f2015-05-07 21:33:59 +00001245 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001246}
1247
Mehdi Amini44ede332015-07-09 02:09:04 +00001248EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1249 EVT VT) const {
1250 if (!VT.isVector())
1251 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001252 return VT.changeVectorElementTypeToInteger();
1253}
1254
Evan Cheng4cad68e2010-05-15 02:18:07 +00001255/// getRegClassFor - Return the register class that should be used for the
1256/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001257const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001258 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1259 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1260 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001261 if (Subtarget->hasNEON()) {
1262 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001263 return &ARM::QQPRRegClass;
1264 if (VT == MVT::v8i64)
1265 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001266 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001267 return TargetLowering::getRegClassFor(VT);
1268}
1269
John Brawn0dbcd652015-03-18 12:01:59 +00001270// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1271// source/dest is aligned and the copy size is large enough. We therefore want
1272// to align such objects passed to memory intrinsics.
1273bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1274 unsigned &PrefAlign) const {
1275 if (!isa<MemIntrinsic>(CI))
1276 return false;
1277 MinSize = 8;
1278 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1279 // cycle faster than 4-byte aligned LDM.
1280 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1281 return true;
1282}
1283
Eric Christopher84bdfd82010-07-21 22:26:11 +00001284// Create a fast isel object.
1285FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001286ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1287 const TargetLibraryInfo *libInfo) const {
1288 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001289}
1290
Evan Cheng4401f882010-05-20 23:26:43 +00001291Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001292 unsigned NumVals = N->getNumValues();
1293 if (!NumVals)
1294 return Sched::RegPressure;
1295
1296 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001297 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001298 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001299 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001300 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001301 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001302 }
Evan Chengbf914992010-05-28 23:25:23 +00001303
1304 if (!N->isMachineOpcode())
1305 return Sched::RegPressure;
1306
1307 // Load are scheduled for latency even if there instruction itinerary
1308 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001309 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001310 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001311
Evan Cheng6cc775f2011-06-28 19:10:37 +00001312 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001313 return Sched::RegPressure;
1314 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001315 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001316 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001317
Evan Cheng4401f882010-05-20 23:26:43 +00001318 return Sched::RegPressure;
1319}
1320
Evan Cheng10043e22007-01-19 07:51:42 +00001321//===----------------------------------------------------------------------===//
1322// Lowering Code
1323//===----------------------------------------------------------------------===//
1324
Evan Cheng10043e22007-01-19 07:51:42 +00001325/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1326static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1327 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001328 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001329 case ISD::SETNE: return ARMCC::NE;
1330 case ISD::SETEQ: return ARMCC::EQ;
1331 case ISD::SETGT: return ARMCC::GT;
1332 case ISD::SETGE: return ARMCC::GE;
1333 case ISD::SETLT: return ARMCC::LT;
1334 case ISD::SETLE: return ARMCC::LE;
1335 case ISD::SETUGT: return ARMCC::HI;
1336 case ISD::SETUGE: return ARMCC::HS;
1337 case ISD::SETULT: return ARMCC::LO;
1338 case ISD::SETULE: return ARMCC::LS;
1339 }
1340}
1341
Bob Wilsona2e83332009-09-09 23:14:54 +00001342/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1343static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001344 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001345 CondCode2 = ARMCC::AL;
1346 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001347 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001348 case ISD::SETEQ:
1349 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1350 case ISD::SETGT:
1351 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1352 case ISD::SETGE:
1353 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1354 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001355 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001356 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1357 case ISD::SETO: CondCode = ARMCC::VC; break;
1358 case ISD::SETUO: CondCode = ARMCC::VS; break;
1359 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1360 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1361 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1362 case ISD::SETLT:
1363 case ISD::SETULT: CondCode = ARMCC::LT; break;
1364 case ISD::SETLE:
1365 case ISD::SETULE: CondCode = ARMCC::LE; break;
1366 case ISD::SETNE:
1367 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1368 }
Evan Cheng10043e22007-01-19 07:51:42 +00001369}
1370
Bob Wilsona4c22902009-04-17 19:07:39 +00001371//===----------------------------------------------------------------------===//
1372// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001373//===----------------------------------------------------------------------===//
1374
1375#include "ARMGenCallingConv.inc"
1376
Oliver Stannardc24f2172014-05-09 14:01:47 +00001377/// getEffectiveCallingConv - Get the effective calling convention, taking into
1378/// account presence of floating point hardware and calling convention
1379/// limitations, such as support for variadic functions.
1380CallingConv::ID
1381ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1382 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001383 switch (CC) {
1384 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001385 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001386 case CallingConv::ARM_AAPCS:
1387 case CallingConv::ARM_APCS:
1388 case CallingConv::GHC:
1389 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001390 case CallingConv::PreserveMost:
1391 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001392 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001393 case CallingConv::Swift:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001394 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1395 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001396 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001397 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001398 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001399 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1400 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001401 return CallingConv::ARM_AAPCS_VFP;
1402 else
1403 return CallingConv::ARM_AAPCS;
1404 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001405 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001406 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001407 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001408 return CallingConv::Fast;
1409 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001410 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001411 return CallingConv::ARM_AAPCS_VFP;
1412 else
1413 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001414 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001415}
1416
1417/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1418/// CallingConvention.
1419CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1420 bool Return,
1421 bool isVarArg) const {
1422 switch (getEffectiveCallingConv(CC, isVarArg)) {
1423 default:
1424 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001425 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001426 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001427 case CallingConv::ARM_AAPCS:
1428 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1429 case CallingConv::ARM_AAPCS_VFP:
1430 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1431 case CallingConv::Fast:
1432 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001433 case CallingConv::GHC:
1434 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001435 case CallingConv::PreserveMost:
1436 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001437 }
1438}
1439
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001440/// LowerCallResult - Lower the result values of a call into the
1441/// appropriate copies out of appropriate physical registers.
1442SDValue
1443ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001444 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001445 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001446 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001447 SmallVectorImpl<SDValue> &InVals,
1448 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001449
Bob Wilsona4c22902009-04-17 19:07:39 +00001450 // Assign locations to each value returned by this call.
1451 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001452 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1453 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001454 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001455 CCAssignFnForNode(CallConv, /* Return*/ true,
1456 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001457
1458 // Copy all of the result registers out of their specified physreg.
1459 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1460 CCValAssign VA = RVLocs[i];
1461
Stephen Linb8bd2322013-04-20 05:14:40 +00001462 // Pass 'this' value directly from the argument to return value, to avoid
1463 // reg unit interference
1464 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001465 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1466 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001467 InVals.push_back(ThisVal);
1468 continue;
1469 }
1470
Bob Wilson0041bd32009-04-25 00:33:20 +00001471 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001472 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001473 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001474 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001475 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001476 Chain = Lo.getValue(1);
1477 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001478 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001479 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001480 InFlag);
1481 Chain = Hi.getValue(1);
1482 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001483 if (!Subtarget->isLittle())
1484 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001485 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001486
Owen Anderson9f944592009-08-11 20:47:22 +00001487 if (VA.getLocVT() == MVT::v2f64) {
1488 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1489 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001490 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001491
1492 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001493 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001494 Chain = Lo.getValue(1);
1495 InFlag = Lo.getValue(2);
1496 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001497 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001498 Chain = Hi.getValue(1);
1499 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001500 if (!Subtarget->isLittle())
1501 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001502 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001503 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001504 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001505 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001506 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001507 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1508 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001509 Chain = Val.getValue(1);
1510 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001511 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001512
1513 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001514 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001515 case CCValAssign::Full: break;
1516 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001517 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001518 break;
1519 }
1520
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001521 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001522 }
1523
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001524 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001525}
1526
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001527/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001528SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001529ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1530 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001531 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001532 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001533 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001534 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001535 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001536 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1537 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001538 return DAG.getStore(
1539 Chain, dl, Arg, PtrOff,
1540 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1541 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001542}
1543
Andrew Trickef9de2a2013-05-25 02:42:55 +00001544void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001545 SDValue Chain, SDValue &Arg,
1546 RegsToPassVector &RegsToPass,
1547 CCValAssign &VA, CCValAssign &NextVA,
1548 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001549 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001550 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001551
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001552 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001553 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001554 unsigned id = Subtarget->isLittle() ? 0 : 1;
1555 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001556
1557 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001558 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001559 else {
1560 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001561 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001562 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1563 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001564
Christian Pirkerb5728192014-05-08 14:06:24 +00001565 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001566 dl, DAG, NextVA,
1567 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001568 }
1569}
1570
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001571/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001572/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1573/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001574SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001575ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001576 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001577 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001578 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001579 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1580 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1581 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001582 SDValue Chain = CLI.Chain;
1583 SDValue Callee = CLI.Callee;
1584 bool &isTailCall = CLI.IsTailCall;
1585 CallingConv::ID CallConv = CLI.CallConv;
1586 bool doesNotRet = CLI.DoesNotReturn;
1587 bool isVarArg = CLI.IsVarArg;
1588
Dale Johannesend679ff72010-06-03 21:09:53 +00001589 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001590 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1591 bool isThisReturn = false;
1592 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001593 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001594
Bob Wilson8decdc42011-10-07 17:17:49 +00001595 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001596 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001597 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001598
Dale Johannesend679ff72010-06-03 21:09:53 +00001599 if (isTailCall) {
1600 // Check if it's really possible to do a tail call.
1601 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001602 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001603 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001604 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1605 report_fatal_error("failed to perform tail call elimination on a call "
1606 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001607 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1608 // detected sibcalls.
1609 if (isTailCall) {
1610 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001611 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001612 }
1613 }
Evan Cheng10043e22007-01-19 07:51:42 +00001614
Bob Wilsona4c22902009-04-17 19:07:39 +00001615 // Analyze operands of the call, assigning locations to each operand.
1616 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001617 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1618 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001619 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001620 CCAssignFnForNode(CallConv, /* Return*/ false,
1621 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001622
Bob Wilsona4c22902009-04-17 19:07:39 +00001623 // Get a count of how many bytes are to be pushed on the stack.
1624 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001625
Dale Johannesend679ff72010-06-03 21:09:53 +00001626 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001627 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001628 NumBytes = 0;
1629
Evan Cheng10043e22007-01-19 07:51:42 +00001630 // Adjust the stack pointer for the new arguments...
1631 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001632 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001633 Chain = DAG.getCALLSEQ_START(Chain,
1634 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001635
Mehdi Amini44ede332015-07-09 02:09:04 +00001636 SDValue StackPtr =
1637 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001638
Bob Wilson2e076c42009-06-22 23:27:02 +00001639 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001640 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001641
Bob Wilsona4c22902009-04-17 19:07:39 +00001642 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001643 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001644 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1645 i != e;
1646 ++i, ++realArgIdx) {
1647 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001648 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001649 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001650 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001651
Bob Wilsona4c22902009-04-17 19:07:39 +00001652 // Promote the value if needed.
1653 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001654 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001655 case CCValAssign::Full: break;
1656 case CCValAssign::SExt:
1657 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1658 break;
1659 case CCValAssign::ZExt:
1660 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1661 break;
1662 case CCValAssign::AExt:
1663 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1664 break;
1665 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001666 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001667 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001668 }
1669
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001670 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001671 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001672 if (VA.getLocVT() == MVT::v2f64) {
1673 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001674 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001675 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001676 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001677
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001678 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001679 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1680
1681 VA = ArgLocs[++i]; // skip ahead to next loc
1682 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001683 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001684 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1685 } else {
1686 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001687
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001688 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1689 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001690 }
1691 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001692 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001693 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001694 }
1695 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001696 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1697 assert(VA.getLocVT() == MVT::i32 &&
1698 "unexpected calling convention register assignment");
1699 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001700 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001701 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001702 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001703 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001704 } else if (isByVal) {
1705 assert(VA.isMemLoc());
1706 unsigned offset = 0;
1707
1708 // True if this byval aggregate will be split between registers
1709 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001710 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001711 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001712
1713 if (CurByValIdx < ByValArgsCount) {
1714
1715 unsigned RegBegin, RegEnd;
1716 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1717
Mehdi Amini44ede332015-07-09 02:09:04 +00001718 EVT PtrVT =
1719 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001720 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001721 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001722 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001723 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1724 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1725 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001726 false, false, false,
1727 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001728 MemOpChains.push_back(Load.getValue(1));
1729 RegsToPass.push_back(std::make_pair(j, Load));
1730 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001731
1732 // If parameter size outsides register area, "offset" value
1733 // helps us to calculate stack slot for remained part properly.
1734 offset = RegEnd - RegBegin;
1735
1736 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001737 }
1738
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001739 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001740 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001741 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001742 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001743 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001744 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001745 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001746 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001747 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001748 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1749 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001750
Manman Ren9f911162012-06-01 02:44:42 +00001751 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001752 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001753 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001754 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001755 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001756 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001757 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001758
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001759 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1760 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001761 }
Evan Cheng10043e22007-01-19 07:51:42 +00001762 }
1763
1764 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001765 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001766
1767 // Build a sequence of copy-to-reg nodes chained together with token chain
1768 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001769 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001770 // Tail call byval lowering might overwrite argument registers so in case of
1771 // tail call optimization the copies to registers are lowered later.
1772 if (!isTailCall)
1773 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1774 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1775 RegsToPass[i].second, InFlag);
1776 InFlag = Chain.getValue(1);
1777 }
Evan Cheng10043e22007-01-19 07:51:42 +00001778
Dale Johannesend679ff72010-06-03 21:09:53 +00001779 // For tail calls lower the arguments to the 'real' stack slot.
1780 if (isTailCall) {
1781 // Force all the incoming stack arguments to be loaded from the stack
1782 // before any new outgoing arguments are stored to the stack, because the
1783 // outgoing stack slots may alias the incoming argument stack slots, and
1784 // the alias isn't otherwise explicit. This is slightly more conservative
1785 // than necessary, because it means that each store effectively depends
1786 // on every argument instead of just those arguments it would clobber.
1787
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001788 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001789 InFlag = SDValue();
1790 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1791 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1792 RegsToPass[i].second, InFlag);
1793 InFlag = Chain.getValue(1);
1794 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001795 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001796 }
1797
Bill Wendling24c79f22008-09-16 21:48:12 +00001798 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1799 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1800 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001801 bool isDirect = false;
1802 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001803 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001804 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001805 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001806
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001807 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001808 assert((Subtarget->isTargetWindows() ||
1809 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1810 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001811 // Handle a global address or an external symbol. If it's not one of
1812 // those, the target's already in a register, so we don't need to do
1813 // anything extra.
1814 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001815 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001816 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001817 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001818 ARMConstantPoolValue *CPV =
1819 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1820
Jim Grosbach32bb3622010-04-14 22:28:31 +00001821 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001822 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001823 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001824 Callee = DAG.getLoad(
1825 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1826 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1827 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001828 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1829 const char *Sym = S->getSymbol();
1830
1831 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001832 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001833 ARMConstantPoolValue *CPV =
1834 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1835 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001836 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001837 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001838 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001839 Callee = DAG.getLoad(
1840 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1841 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1842 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001843 }
1844 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001845 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001846 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001847 bool isDef = GV->isStrongDefinitionForLinker();
1848 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001849 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001850 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001851 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001852 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001853 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001854 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001855 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001856 Callee = DAG.getNode(
1857 ARMISD::WrapperPIC, dl, PtrVt,
1858 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1859 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001860 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1861 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001862 } else if (Subtarget->isTargetCOFF()) {
1863 assert(Subtarget->isTargetWindows() &&
1864 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001865 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1866 ? ARMII::MO_DLLIMPORT
1867 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001868 Callee =
1869 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001870 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001871 Callee =
1872 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1873 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001874 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1875 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001876 } else {
1877 // On ELF targets for PIC code, direct calls should go through the PLT
1878 unsigned OpFlags = 0;
1879 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001880 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001881 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001882 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001883 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001884 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001885 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001886 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001887 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001888 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001889 // tBX takes a register source operand.
1890 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001891 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001892 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001893 ARMConstantPoolValue *CPV =
1894 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1895 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001896 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001897 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001898 Callee = DAG.getLoad(
1899 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1900 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1901 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001902 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001903 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001904 } else {
1905 unsigned OpFlags = 0;
1906 // On ELF targets for PIC code, direct calls should go through the PLT
1907 if (Subtarget->isTargetELF() &&
1908 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1909 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001910 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001911 }
Evan Cheng10043e22007-01-19 07:51:42 +00001912 }
1913
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001914 // FIXME: handle tail calls differently.
1915 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001916 if (Subtarget->isThumb()) {
1917 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001918 CallOpc = ARMISD::CALL_NOLINK;
1919 else
1920 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1921 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001922 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001923 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001924 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001925 // Emit regular call when code size is the priority
1926 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001927 // "mov lr, pc; b _foo" to avoid confusing the RSP
1928 CallOpc = ARMISD::CALL_NOLINK;
1929 else
1930 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001931 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001932
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001933 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001934 Ops.push_back(Chain);
1935 Ops.push_back(Callee);
1936
1937 // Add argument registers to the end of the list so that they are known live
1938 // into the call.
1939 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1940 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1941 RegsToPass[i].second.getValueType()));
1942
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001943 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001944 if (!isTailCall) {
1945 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001946 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001947 if (isThisReturn) {
1948 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001949 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001950 if (!Mask) {
1951 // Set isThisReturn to false if the calling convention is not one that
1952 // allows 'returned' to be modeled in this way, so LowerCallResult does
1953 // not try to pass 'this' straight through
1954 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001955 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001956 }
1957 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001958 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001959
Matthias Braunc22630e2013-10-04 16:52:54 +00001960 assert(Mask && "Missing call preserved mask for calling convention");
1961 Ops.push_back(DAG.getRegisterMask(Mask));
1962 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001963
Gabor Greiff304a7a2008-08-28 21:40:38 +00001964 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001965 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001966
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001967 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001968 if (isTailCall) {
1969 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001970 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001971 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001972
Duncan Sands739a0542008-07-02 17:40:58 +00001973 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001974 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001975 InFlag = Chain.getValue(1);
1976
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001977 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1978 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001979 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001980 InFlag = Chain.getValue(1);
1981
Bob Wilsona4c22902009-04-17 19:07:39 +00001982 // Handle result values, copying them out of physregs into vregs that we
1983 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001984 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001985 InVals, isThisReturn,
1986 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001987}
1988
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001989/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001990/// on the stack. Remember the next parameter register to allocate,
1991/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001992/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001993void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1994 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001995 assert((State->getCallOrPrologue() == Prologue ||
1996 State->getCallOrPrologue() == Call) &&
1997 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001998
Tim Northover8cda34f2015-03-11 18:54:22 +00001999 // Byval (as with any stack) slots are always at least 4 byte aligned.
2000 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002001
Tim Northover8cda34f2015-03-11 18:54:22 +00002002 unsigned Reg = State->AllocateReg(GPRArgRegs);
2003 if (!Reg)
2004 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002005
Tim Northover8cda34f2015-03-11 18:54:22 +00002006 unsigned AlignInRegs = Align / 4;
2007 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2008 for (unsigned i = 0; i < Waste; ++i)
2009 Reg = State->AllocateReg(GPRArgRegs);
2010
2011 if (!Reg)
2012 return;
2013
2014 unsigned Excess = 4 * (ARM::R4 - Reg);
2015
2016 // Special case when NSAA != SP and parameter size greater than size of
2017 // all remained GPR regs. In that case we can't split parameter, we must
2018 // send it to stack. We also must set NCRN to R4, so waste all
2019 // remained registers.
2020 const unsigned NSAAOffset = State->getNextStackOffset();
2021 if (NSAAOffset != 0 && Size > Excess) {
2022 while (State->AllocateReg(GPRArgRegs))
2023 ;
2024 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002025 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002026
2027 // First register for byval parameter is the first register that wasn't
2028 // allocated before this method call, so it would be "reg".
2029 // If parameter is small enough to be saved in range [reg, r4), then
2030 // the end (first after last) register would be reg + param-size-in-regs,
2031 // else parameter would be splitted between registers and stack,
2032 // end register would be r4 in this case.
2033 unsigned ByValRegBegin = Reg;
2034 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2035 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2036 // Note, first register is allocated in the beginning of function already,
2037 // allocate remained amount of registers we need.
2038 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2039 State->AllocateReg(GPRArgRegs);
2040 // A byval parameter that is split between registers and memory needs its
2041 // size truncated here.
2042 // In the case where the entire structure fits in registers, we set the
2043 // size in memory to zero.
2044 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002045}
2046
Dale Johannesend679ff72010-06-03 21:09:53 +00002047/// MatchingStackOffset - Return true if the given stack call argument is
2048/// already available in the same position (relatively) of the caller's
2049/// incoming argument stack.
2050static
2051bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2052 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002053 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002054 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2055 int FI = INT_MAX;
2056 if (Arg.getOpcode() == ISD::CopyFromReg) {
2057 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002058 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002059 return false;
2060 MachineInstr *Def = MRI->getVRegDef(VR);
2061 if (!Def)
2062 return false;
2063 if (!Flags.isByVal()) {
2064 if (!TII->isLoadFromStackSlot(Def, FI))
2065 return false;
2066 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002067 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002068 }
2069 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2070 if (Flags.isByVal())
2071 // ByVal argument is passed in as a pointer but it's now being
2072 // dereferenced. e.g.
2073 // define @foo(%struct.X* %A) {
2074 // tail call @bar(%struct.X* byval %A)
2075 // }
2076 return false;
2077 SDValue Ptr = Ld->getBasePtr();
2078 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2079 if (!FINode)
2080 return false;
2081 FI = FINode->getIndex();
2082 } else
2083 return false;
2084
2085 assert(FI != INT_MAX);
2086 if (!MFI->isFixedObjectIndex(FI))
2087 return false;
2088 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2089}
2090
2091/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2092/// for tail call optimization. Targets which want to do tail call
2093/// optimization should implement this function.
2094bool
2095ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2096 CallingConv::ID CalleeCC,
2097 bool isVarArg,
2098 bool isCalleeStructRet,
2099 bool isCallerStructRet,
2100 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002101 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002102 const SmallVectorImpl<ISD::InputArg> &Ins,
2103 SelectionDAG& DAG) const {
Matthias Braun8d414362016-03-30 22:46:04 +00002104 MachineFunction &MF = DAG.getMachineFunction();
2105 const Function *CallerF = MF.getFunction();
Dale Johannesend679ff72010-06-03 21:09:53 +00002106 CallingConv::ID CallerCC = CallerF->getCallingConv();
Manman Ren4865d892016-03-18 23:41:51 +00002107
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002108 assert(Subtarget->supportsTailCall());
2109
Dale Johannesend679ff72010-06-03 21:09:53 +00002110 // Look for obvious safe cases to perform tail call optimization that do not
2111 // require ABI changes. This is what gcc calls sibcall.
2112
Jim Grosbache3864cc2010-06-16 23:45:49 +00002113 // Do not sibcall optimize vararg calls unless the call site is not passing
2114 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002115 if (isVarArg && !Outs.empty())
2116 return false;
2117
Tim Northoverd8407452013-10-01 14:33:28 +00002118 // Exception-handling functions need a special set of instructions to indicate
2119 // a return to the hardware. Tail-calling another function would probably
2120 // break this.
2121 if (CallerF->hasFnAttribute("interrupt"))
2122 return false;
2123
Dale Johannesend679ff72010-06-03 21:09:53 +00002124 // Also avoid sibcall optimization if either caller or callee uses struct
2125 // return semantics.
2126 if (isCalleeStructRet || isCallerStructRet)
2127 return false;
2128
Oliver Stannard12993dd2014-08-18 12:42:15 +00002129 // Externally-defined functions with weak linkage should not be
2130 // tail-called on ARM when the OS does not support dynamic
2131 // pre-emption of symbols, as the AAELF spec requires normal calls
2132 // to undefined weak functions to be replaced with a NOP or jump to the
2133 // next instruction. The behaviour of branch instructions in this
2134 // situation (as used for tail calls) is implementation-defined, so we
2135 // cannot rely on the linker replacing the tail call with a return.
2136 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2137 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002138 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002139 if (GV->hasExternalWeakLinkage() &&
2140 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002141 return false;
2142 }
2143
Matthias Braun8d414362016-03-30 22:46:04 +00002144 // Check that the call results are passed in the same way.
2145 LLVMContext &C = *DAG.getContext();
2146 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2147 CCAssignFnForNode(CalleeCC, true, isVarArg),
2148 CCAssignFnForNode(CallerCC, true, isVarArg)))
2149 return false;
Matthias Braun870c34f2016-04-04 18:56:13 +00002150 // The callee has to preserve all registers the caller needs to preserve.
Matthias Braun707e02c2016-04-13 21:43:25 +00002151 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2152 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
Matthias Braun870c34f2016-04-04 18:56:13 +00002153 if (CalleeCC != CallerCC) {
Matthias Braun707e02c2016-04-13 21:43:25 +00002154 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2155 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
Matthias Braun870c34f2016-04-04 18:56:13 +00002156 return false;
2157 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002158
Manman Ren7e48b252012-10-12 23:39:43 +00002159 // If Caller's vararg or byval argument has been split between registers and
2160 // stack, do not perform tail call, since part of the argument is in caller's
2161 // local frame.
Matthias Braun8d414362016-03-30 22:46:04 +00002162 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002163 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002164 return false;
2165
Dale Johannesend679ff72010-06-03 21:09:53 +00002166 // If the callee takes no arguments then go on to check the results of the
2167 // call.
2168 if (!Outs.empty()) {
2169 // Check if stack adjustment is needed. For now, do not do this if any
2170 // argument is passed on the stack.
2171 SmallVector<CCValAssign, 16> ArgLocs;
Matthias Braun8d414362016-03-30 22:46:04 +00002172 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002173 CCInfo.AnalyzeCallOperands(Outs,
2174 CCAssignFnForNode(CalleeCC, false, isVarArg));
2175 if (CCInfo.getNextStackOffset()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002176 // Check if the arguments are already laid out in the right way as
2177 // the caller's fixed stack objects.
2178 MachineFrameInfo *MFI = MF.getFrameInfo();
2179 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002180 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002181 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2182 i != e;
2183 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002184 CCValAssign &VA = ArgLocs[i];
2185 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002186 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002187 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002188 if (VA.getLocInfo() == CCValAssign::Indirect)
2189 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002190 if (VA.needsCustom()) {
2191 // f64 and vector types are split into multiple registers or
2192 // register/stack-slot combinations. The types will not match
2193 // the registers; give up on memory f64 refs until we figure
2194 // out what to do about this.
2195 if (!VA.isRegLoc())
2196 return false;
2197 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002198 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002199 if (RegVT == MVT::v2f64) {
2200 if (!ArgLocs[++i].isRegLoc())
2201 return false;
2202 if (!ArgLocs[++i].isRegLoc())
2203 return false;
2204 }
2205 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002206 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2207 MFI, MRI, TII))
2208 return false;
2209 }
2210 }
2211 }
Matthias Braun707e02c2016-04-13 21:43:25 +00002212
Matthias Braun46b0f032016-04-14 01:10:42 +00002213 const MachineRegisterInfo &MRI = MF.getRegInfo();
2214 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
2215 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002216 }
2217
2218 return true;
2219}
2220
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002221bool
2222ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2223 MachineFunction &MF, bool isVarArg,
2224 const SmallVectorImpl<ISD::OutputArg> &Outs,
2225 LLVMContext &Context) const {
2226 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002227 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002228 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2229 isVarArg));
2230}
2231
Tim Northoverd8407452013-10-01 14:33:28 +00002232static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2233 SDLoc DL, SelectionDAG &DAG) {
2234 const MachineFunction &MF = DAG.getMachineFunction();
2235 const Function *F = MF.getFunction();
2236
2237 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2238
2239 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2240 // version of the "preferred return address". These offsets affect the return
2241 // instruction if this is a return from PL1 without hypervisor extensions.
2242 // IRQ/FIQ: +4 "subs pc, lr, #4"
2243 // SWI: 0 "subs pc, lr, #0"
2244 // ABORT: +4 "subs pc, lr, #4"
2245 // UNDEF: +4/+2 "subs pc, lr, #0"
2246 // UNDEF varies depending on where the exception came from ARM or Thumb
2247 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2248
2249 int64_t LROffset;
2250 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2251 IntKind == "ABORT")
2252 LROffset = 4;
2253 else if (IntKind == "SWI" || IntKind == "UNDEF")
2254 LROffset = 0;
2255 else
2256 report_fatal_error("Unsupported interrupt attribute. If present, value "
2257 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2258
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002259 RetOps.insert(RetOps.begin() + 1,
2260 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002261
Craig Topper48d114b2014-04-26 18:35:24 +00002262 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002263}
2264
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002265SDValue
2266ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002267 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002268 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002269 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002270 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002271
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002272 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002273 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002274
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002275 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002276 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2277 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002278
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002279 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002280 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2281 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002282
Bob Wilsona4c22902009-04-17 19:07:39 +00002283 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002284 SmallVector<SDValue, 4> RetOps;
2285 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002286 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002287
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002288 MachineFunction &MF = DAG.getMachineFunction();
2289 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2290 AFI->setReturnRegsCount(RVLocs.size());
2291
Bob Wilsona4c22902009-04-17 19:07:39 +00002292 // Copy the result values into the output registers.
2293 for (unsigned i = 0, realRVLocIdx = 0;
2294 i != RVLocs.size();
2295 ++i, ++realRVLocIdx) {
2296 CCValAssign &VA = RVLocs[i];
2297 assert(VA.isRegLoc() && "Can only return in registers!");
2298
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002299 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002300
2301 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002302 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002303 case CCValAssign::Full: break;
2304 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002305 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002306 break;
2307 }
2308
Bob Wilsona4c22902009-04-17 19:07:39 +00002309 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002310 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002311 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002312 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002313 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002314 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002315 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002316
Christian Pirkerb5728192014-05-08 14:06:24 +00002317 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2318 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2319 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002320 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002321 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002322 VA = RVLocs[++i]; // skip ahead to next loc
2323 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002324 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2325 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002326 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002327 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002328 VA = RVLocs[++i]; // skip ahead to next loc
2329
2330 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002331 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002332 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002333 }
2334 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2335 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002336 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002337 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002338 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2339 fmrrd.getValue(isLittleEndian ? 0 : 1),
2340 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002341 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002342 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002343 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002344 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2345 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002346 Flag);
2347 } else
2348 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2349
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002350 // Guarantee that all emitted copies are
2351 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002352 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002353 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002354 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002355 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2356 const MCPhysReg *I =
2357 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2358 if (I) {
2359 for (; *I; ++I) {
2360 if (ARM::GPRRegClass.contains(*I))
2361 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2362 else if (ARM::DPRRegClass.contains(*I))
2363 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2364 else
2365 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2366 }
2367 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002368
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002369 // Update chain and glue.
2370 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002371 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002372 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002373
Tim Northoverd8407452013-10-01 14:33:28 +00002374 // CPUs which aren't M-class use a special sequence to return from
2375 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2376 // though we use "subs pc, lr, #N").
2377 //
2378 // M-class CPUs actually use a normal return sequence with a special
2379 // (hardware-provided) value in LR, so the normal code path works.
2380 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2381 !Subtarget->isMClass()) {
2382 if (Subtarget->isThumb1Only())
2383 report_fatal_error("interrupt attribute is not supported in Thumb1");
2384 return LowerInterruptReturn(RetOps, dl, DAG);
2385 }
2386
Craig Topper48d114b2014-04-26 18:35:24 +00002387 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002388}
2389
Evan Chengf8bad082012-04-10 01:51:00 +00002390bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002391 if (N->getNumValues() != 1)
2392 return false;
2393 if (!N->hasNUsesOfValue(1, 0))
2394 return false;
2395
Evan Chengf8bad082012-04-10 01:51:00 +00002396 SDValue TCChain = Chain;
2397 SDNode *Copy = *N->use_begin();
2398 if (Copy->getOpcode() == ISD::CopyToReg) {
2399 // If the copy has a glue operand, we conservatively assume it isn't safe to
2400 // perform a tail call.
2401 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2402 return false;
2403 TCChain = Copy->getOperand(0);
2404 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2405 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002406 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002407 SmallPtrSet<SDNode*, 2> Copies;
2408 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002409 UI != UE; ++UI) {
2410 if (UI->getOpcode() != ISD::CopyToReg)
2411 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002412 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002413 }
Evan Chengf8bad082012-04-10 01:51:00 +00002414 if (Copies.size() > 2)
2415 return false;
2416
2417 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2418 UI != UE; ++UI) {
2419 SDValue UseChain = UI->getOperand(0);
2420 if (Copies.count(UseChain.getNode()))
2421 // Second CopyToReg
2422 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002423 else {
2424 // We are at the top of this chain.
2425 // If the copy has a glue operand, we conservatively assume it
2426 // isn't safe to perform a tail call.
2427 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2428 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002429 // First CopyToReg
2430 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002431 }
Evan Chengf8bad082012-04-10 01:51:00 +00002432 }
2433 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002434 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002435 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002436 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002437 Copy = *Copy->use_begin();
2438 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002439 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002440 // If the copy has a glue operand, we conservatively assume it isn't safe to
2441 // perform a tail call.
2442 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2443 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002444 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002445 } else {
2446 return false;
2447 }
2448
Evan Cheng419ea282010-12-01 22:59:46 +00002449 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002450 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2451 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002452 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2453 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002454 return false;
2455 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002456 }
2457
Evan Chengf8bad082012-04-10 01:51:00 +00002458 if (!HasRet)
2459 return false;
2460
2461 Chain = TCChain;
2462 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002463}
2464
Evan Cheng0663f232011-03-21 01:19:09 +00002465bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002466 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002467 return false;
2468
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002469 auto Attr =
2470 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2471 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002472 return false;
2473
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002474 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002475}
2476
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002477// Trying to write a 64 bit value so need to split into two 32 bit values first,
2478// and pass the lower and high parts through.
2479static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2480 SDLoc DL(Op);
2481 SDValue WriteValue = Op->getOperand(2);
2482
2483 // This function is only supposed to be called for i64 type argument.
2484 assert(WriteValue.getValueType() == MVT::i64
2485 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2486
2487 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2488 DAG.getConstant(0, DL, MVT::i32));
2489 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2490 DAG.getConstant(1, DL, MVT::i32));
2491 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2492 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2493}
2494
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002495// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2496// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2497// one of the above mentioned nodes. It has to be wrapped because otherwise
2498// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2499// be used to form addressing mode. These wrapped nodes will be selected
2500// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002501static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002502 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002503 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002504 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002505 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002506 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002507 if (CP->isMachineConstantPoolEntry())
2508 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2509 CP->getAlignment());
2510 else
2511 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2512 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002513 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002514}
2515
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002516unsigned ARMTargetLowering::getJumpTableEncoding() const {
2517 return MachineJumpTableInfo::EK_Inline;
2518}
2519
Dan Gohman21cea8a2010-04-17 15:26:15 +00002520SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2521 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002522 MachineFunction &MF = DAG.getMachineFunction();
2523 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2524 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002525 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002526 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002527 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002528 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2529 SDValue CPAddr;
2530 if (RelocM == Reloc::Static) {
2531 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2532 } else {
2533 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002534 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002535 ARMConstantPoolValue *CPV =
2536 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2537 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002538 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2539 }
2540 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002541 SDValue Result =
2542 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2543 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2544 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002545 if (RelocM == Reloc::Static)
2546 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002548 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002549}
2550
Tim Northoverbd41cf82016-01-07 09:03:03 +00002551/// \brief Convert a TLS address reference into the correct sequence of loads
2552/// and calls to compute the variable's address for Darwin, and return an
2553/// SDValue containing the final node.
2554
2555/// Darwin only has one TLS scheme which must be capable of dealing with the
2556/// fully general situation, in the worst case. This means:
2557/// + "extern __thread" declaration.
2558/// + Defined in a possibly unknown dynamic library.
2559///
2560/// The general system is that each __thread variable has a [3 x i32] descriptor
2561/// which contains information used by the runtime to calculate the address. The
2562/// only part of this the compiler needs to know about is the first word, which
2563/// contains a function pointer that must be called with the address of the
2564/// entire descriptor in "r0".
2565///
2566/// Since this descriptor may be in a different unit, in general access must
2567/// proceed along the usual ARM rules. A common sequence to produce is:
2568///
2569/// movw rT1, :lower16:_var$non_lazy_ptr
2570/// movt rT1, :upper16:_var$non_lazy_ptr
2571/// ldr r0, [rT1]
2572/// ldr rT2, [r0]
2573/// blx rT2
2574/// [...address now in r0...]
2575SDValue
2576ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2577 SelectionDAG &DAG) const {
2578 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2579 SDLoc DL(Op);
2580
2581 // First step is to get the address of the actua global symbol. This is where
2582 // the TLS descriptor lives.
2583 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2584
2585 // The first entry in the descriptor is a function pointer that we must call
2586 // to obtain the address of the variable.
2587 SDValue Chain = DAG.getEntryNode();
2588 SDValue FuncTLVGet =
2589 DAG.getLoad(MVT::i32, DL, Chain, DescAddr,
2590 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2591 false, true, true, 4);
2592 Chain = FuncTLVGet.getValue(1);
2593
2594 MachineFunction &F = DAG.getMachineFunction();
2595 MachineFrameInfo *MFI = F.getFrameInfo();
2596 MFI->setAdjustsStack(true);
2597
2598 // TLS calls preserve all registers except those that absolutely must be
2599 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2600 // silly).
2601 auto TRI =
2602 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2603 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2604 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2605
2606 // Finally, we can make the call. This is just a degenerate version of a
2607 // normal AArch64 call node: r0 takes the address of the descriptor, and
2608 // returns the address of the variable in this thread.
2609 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2610 Chain =
2611 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2612 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2613 DAG.getRegisterMask(Mask), Chain.getValue(1));
2614 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2615}
2616
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002617SDValue
2618ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2619 SelectionDAG &DAG) const {
2620 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
2621 SDValue Chain = DAG.getEntryNode();
2622 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2623 SDLoc DL(Op);
2624
2625 // Load the current TEB (thread environment block)
2626 SDValue Ops[] = {Chain,
2627 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2628 DAG.getConstant(15, DL, MVT::i32),
2629 DAG.getConstant(0, DL, MVT::i32),
2630 DAG.getConstant(13, DL, MVT::i32),
2631 DAG.getConstant(0, DL, MVT::i32),
2632 DAG.getConstant(2, DL, MVT::i32)};
2633 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2634 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2635
2636 SDValue TEB = CurrentTEB.getValue(0);
2637 Chain = CurrentTEB.getValue(1);
2638
2639 // Load the ThreadLocalStoragePointer from the TEB
2640 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2641 SDValue TLSArray =
2642 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
2643 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo(),
2644 false, false, false, 0);
2645
2646 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2647 // offset into the TLSArray.
2648
2649 // Load the TLS index from the C runtime
2650 SDValue TLSIndex =
2651 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2652 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
2653 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo(),
2654 false, false, false, 0);
2655
2656 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2657 DAG.getConstant(2, DL, MVT::i32));
2658 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2659 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
2660 MachinePointerInfo(), false, false, false, 0);
2661
2662 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS,
2663 LowerGlobalAddressWindows(Op, DAG));
2664}
2665
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002666// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002667SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002668ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002669 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002670 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002671 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002672 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002673 MachineFunction &MF = DAG.getMachineFunction();
2674 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002675 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002676 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002677 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2678 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002679 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002680 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002681 Argument =
2682 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2683 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2684 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002685 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002686
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002687 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002688 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002689
2690 // call __tls_get_addr.
2691 ArgListTy Args;
2692 ArgListEntry Entry;
2693 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002694 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002695 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002696
Dale Johannesen555a3752009-01-30 23:10:59 +00002697 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002698 TargetLowering::CallLoweringInfo CLI(DAG);
2699 CLI.setDebugLoc(dl).setChain(Chain)
2700 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002701 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2702 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002703
Justin Holewinskiaa583972012-05-25 16:35:28 +00002704 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002705 return CallResult.first;
2706}
2707
2708// Lower ISD::GlobalTLSAddress using the "initial exec" or
2709// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002710SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002711ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002712 SelectionDAG &DAG,
2713 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002714 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002715 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002716 SDValue Offset;
2717 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002718 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002719 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002720 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002721
Hans Wennborgaea41202012-05-04 09:40:39 +00002722 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002723 MachineFunction &MF = DAG.getMachineFunction();
2724 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002725 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002726 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002727 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2728 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002729 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2730 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2731 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002732 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002733 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002734 Offset = DAG.getLoad(
2735 PtrVT, dl, Chain, Offset,
2736 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2737 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002738 Chain = Offset.getValue(1);
2739
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002740 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002741 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002742
Alex Lorenze40c8a22015-08-11 23:09:45 +00002743 Offset = DAG.getLoad(
2744 PtrVT, dl, Chain, Offset,
2745 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2746 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002747 } else {
2748 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002749 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002750 ARMConstantPoolValue *CPV =
2751 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002752 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002753 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
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 }
2759
2760 // The address of the thread local variable is the add of the thread
2761 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002762 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002763}
2764
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002765SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002766ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002767 if (Subtarget->isTargetDarwin())
2768 return LowerGlobalTLSAddressDarwin(Op, DAG);
2769
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002770 if (Subtarget->isTargetWindows())
2771 return LowerGlobalTLSAddressWindows(Op, DAG);
2772
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002773 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002774 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002775 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002776 if (DAG.getTarget().Options.EmulatedTLS)
2777 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002778
2779 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2780
2781 switch (model) {
2782 case TLSModel::GeneralDynamic:
2783 case TLSModel::LocalDynamic:
2784 return LowerToTLSGeneralDynamicModel(GA, DAG);
2785 case TLSModel::InitialExec:
2786 case TLSModel::LocalExec:
2787 return LowerToTLSExecModels(GA, DAG, model);
2788 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002789 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002790}
2791
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002792SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002793 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002794 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002795 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002796 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002797 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002798 bool UseGOT_PREL =
Peter Collingbourne99fac802015-10-26 20:46:44 +00002799 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage());
Peter Collingbourne97aae402015-10-26 18:23:16 +00002800
2801 MachineFunction &MF = DAG.getMachineFunction();
2802 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2803 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2804 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2805 SDLoc dl(Op);
2806 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2807 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2808 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2809 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2810 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002811 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002812 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002813 SDValue Result = DAG.getLoad(
2814 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2815 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2816 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002817 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002818 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2819 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2820 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002821 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002822 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002823 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002824 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002825 }
2826
2827 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002828 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002829 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002830 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002831 // FIXME: Once remat is capable of dealing with instructions with register
2832 // operands, expand this into two nodes.
2833 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2834 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002835 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002836 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2837 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002838 return DAG.getLoad(
2839 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2840 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2841 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002842 }
2843}
2844
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002845SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002846 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002847 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002848 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002849 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002850 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002851
Eric Christopherc1058df2014-07-04 01:55:26 +00002852 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002853 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002854
Tim Northover72360d22013-12-02 10:35:41 +00002855 // FIXME: Once remat is capable of dealing with instructions with register
2856 // operands, expand this into multiple nodes
2857 unsigned Wrapper =
2858 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002859
Tim Northover72360d22013-12-02 10:35:41 +00002860 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2861 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002862
Evan Cheng1b389522009-09-03 07:04:02 +00002863 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002864 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002865 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2866 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002867 return Result;
2868}
2869
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002870SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2871 SelectionDAG &DAG) const {
2872 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002873 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2874 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002875
2876 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002877 const ARMII::TOF TargetFlags =
2878 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002879 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002880 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002881 SDLoc DL(Op);
2882
2883 ++NumMovwMovt;
2884
2885 // FIXME: Once remat is capable of dealing with instructions with register
2886 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002887 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2888 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002889 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002890 if (GV->hasDLLImportStorageClass())
2891 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002892 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2893 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002894 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002895}
2896
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002897SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002898ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002899 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002900 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002901 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2902 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002903 Op.getOperand(1), Val);
2904}
2905
2906SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002907ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002908 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002909 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002910 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002911}
2912
Matthias Braun3cd00c12015-07-16 22:34:16 +00002913SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2914 SelectionDAG &DAG) const {
2915 SDLoc dl(Op);
2916 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2917 Op.getOperand(0));
2918}
2919
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002920SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002921ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002922 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002923 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002924 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002925 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002926 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002927 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002928 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002929 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002930 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002931 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00002932 case Intrinsic::thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002933 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002934 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2935 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002936 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002937 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002938 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002939 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002940 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002941 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2942 SDValue CPAddr;
2943 unsigned PCAdj = (RelocM != Reloc::PIC_)
2944 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002945 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002946 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2947 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002948 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002949 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002950 SDValue Result = DAG.getLoad(
2951 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2952 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2953 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002954
2955 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002956 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002957 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2958 }
2959 return Result;
2960 }
Evan Cheng18381b42011-03-29 23:06:19 +00002961 case Intrinsic::arm_neon_vmulls:
2962 case Intrinsic::arm_neon_vmullu: {
2963 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2964 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002965 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002966 Op.getOperand(1), Op.getOperand(2));
2967 }
James Molloyee868b22015-08-11 12:06:25 +00002968 case Intrinsic::arm_neon_vminnm:
2969 case Intrinsic::arm_neon_vmaxnm: {
2970 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2971 ? ISD::FMINNUM : ISD::FMAXNUM;
2972 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2973 Op.getOperand(1), Op.getOperand(2));
2974 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002975 case Intrinsic::arm_neon_vminu:
2976 case Intrinsic::arm_neon_vmaxu: {
2977 if (Op.getValueType().isFloatingPoint())
2978 return SDValue();
2979 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2980 ? ISD::UMIN : ISD::UMAX;
2981 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2982 Op.getOperand(1), Op.getOperand(2));
2983 }
James Molloyd616c642015-08-11 12:06:28 +00002984 case Intrinsic::arm_neon_vmins:
2985 case Intrinsic::arm_neon_vmaxs: {
2986 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002987 if (!Op.getValueType().isFloatingPoint()) {
2988 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2989 ? ISD::SMIN : ISD::SMAX;
2990 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2991 Op.getOperand(1), Op.getOperand(2));
2992 }
James Molloyd616c642015-08-11 12:06:28 +00002993 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2994 ? ISD::FMINNAN : ISD::FMAXNAN;
2995 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2996 Op.getOperand(1), Op.getOperand(2));
2997 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002998 }
2999}
3000
Eli Friedman30a49e92011-08-03 21:06:02 +00003001static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3002 const ARMSubtarget *Subtarget) {
3003 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003004 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003005 if (!Subtarget->hasDataBarrier()) {
3006 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3007 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3008 // here.
3009 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003010 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003011 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003012 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003013 }
3014
Tim Northover36b24172013-07-03 09:20:36 +00003015 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3016 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003017 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003018 if (Subtarget->isMClass()) {
3019 // Only a full system barrier exists in the M-class architectures.
3020 Domain = ARM_MB::SY;
JF Bastien800f87a2016-04-06 21:19:33 +00003021 } else if (Subtarget->isSwift() && Ord == AtomicOrdering::Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003022 // Swift happens to implement ISHST barriers in a way that's compatible with
3023 // Release semantics but weaker than ISH so we'd be fools not to use
3024 // it. Beware: other processors probably don't!
3025 Domain = ARM_MB::ISHST;
3026 }
3027
Joey Gouly926d3f52013-09-05 15:35:24 +00003028 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003029 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3030 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003031}
3032
Evan Cheng8740ee32010-11-03 06:34:55 +00003033static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3034 const ARMSubtarget *Subtarget) {
3035 // ARM pre v5TE and Thumb1 does not have preload instructions.
3036 if (!(Subtarget->isThumb2() ||
3037 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3038 // Just preserve the chain.
3039 return Op.getOperand(0);
3040
Andrew Trickef9de2a2013-05-25 02:42:55 +00003041 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003042 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3043 if (!isRead &&
3044 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3045 // ARMv7 with MP extension has PLDW.
3046 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003047
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003048 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3049 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003050 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003051 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003052 isData = ~isData & 1;
3053 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003054
3055 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003056 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3057 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003058}
3059
Dan Gohman31ae5862010-04-17 14:41:14 +00003060static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3061 MachineFunction &MF = DAG.getMachineFunction();
3062 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3063
Evan Cheng10043e22007-01-19 07:51:42 +00003064 // vastart just stores the address of the VarArgsFrameIndex slot into the
3065 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003066 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003067 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003068 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003069 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003070 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3071 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00003072}
3073
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003074SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00003075ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
3076 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003077 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003078 MachineFunction &MF = DAG.getMachineFunction();
3079 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3080
Craig Topper760b1342012-02-22 05:59:10 +00003081 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003082 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003083 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003084 else
Craig Topperc7242e02012-04-20 07:30:17 +00003085 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003086
3087 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003088 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003089 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003090
3091 SDValue ArgValue2;
3092 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003093 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00003094 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003095
3096 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003097 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003098 ArgValue2 = DAG.getLoad(
3099 MVT::i32, dl, Root, FIN,
3100 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
3101 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00003102 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003103 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003104 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003105 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003106 if (!Subtarget->isLittle())
3107 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003108 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003109}
3110
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003111// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003112// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003113// byval). Either way, we allocate stack slots adjacent to the data
3114// provided by our caller, and store the unallocated registers there.
3115// If this is a variadic function, the va_list pointer will begin with
3116// these values; otherwise, this reassembles a (byval) structure that
3117// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003118// Return: The frame index registers were stored into.
3119int
3120ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003121 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003122 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003123 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00003124 int ArgOffset,
3125 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003126 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003127 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003128 // Setup first unallocated register as first byval register;
3129 // eat all remained registers
3130 // (these two actions are performed by HandleByVal method).
3131 // Then, here, we initialize stack frame with
3132 // "store-reg" instructions.
3133 // Case #2. Var-args function, that doesn't contain byval parameters.
3134 // The same: eat all remained unallocated registers,
3135 // initialize stack frame.
3136
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003137 MachineFunction &MF = DAG.getMachineFunction();
3138 MachineFrameInfo *MFI = MF.getFrameInfo();
3139 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003140 unsigned RBegin, REnd;
3141 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3142 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003143 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003144 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003145 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003146 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003147 }
3148
Tim Northover8cda34f2015-03-11 18:54:22 +00003149 if (REnd != RBegin)
3150 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003151
Mehdi Amini44ede332015-07-09 02:09:04 +00003152 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003153 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003154 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003155
Tim Northover8cda34f2015-03-11 18:54:22 +00003156 SmallVector<SDValue, 4> MemOps;
3157 const TargetRegisterClass *RC =
3158 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003159
Tim Northover8cda34f2015-03-11 18:54:22 +00003160 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3161 unsigned VReg = MF.addLiveIn(Reg, RC);
3162 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3163 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003164 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003165 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3166 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003167 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003168 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003169
3170 if (!MemOps.empty())
3171 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3172 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003173}
3174
3175// Setup stack frame, the va_list pointer will start from.
3176void
3177ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003178 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003179 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003180 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003181 bool ForceMutable) const {
3182 MachineFunction &MF = DAG.getMachineFunction();
3183 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3184
3185 // Try to store any remaining integer argument regs
3186 // to their spots on the stack so that they may be loaded by deferencing
3187 // the result of va_next.
3188 // If there is no regs to be stored, just point address after last
3189 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003190 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3191 CCInfo.getInRegsParamsCount(),
3192 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003193 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003194}
3195
Bob Wilson2e076c42009-06-22 23:27:02 +00003196SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003197ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003198 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003199 const SmallVectorImpl<ISD::InputArg>
3200 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003201 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003202 SmallVectorImpl<SDValue> &InVals)
3203 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003204 MachineFunction &MF = DAG.getMachineFunction();
3205 MachineFrameInfo *MFI = MF.getFrameInfo();
3206
Bob Wilsona4c22902009-04-17 19:07:39 +00003207 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3208
3209 // Assign locations to all of the incoming arguments.
3210 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003211 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3212 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003213 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003214 CCAssignFnForNode(CallConv, /* Return*/ false,
3215 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003216
Bob Wilsona4c22902009-04-17 19:07:39 +00003217 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003218 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003219 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3220 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003221
3222 // Initially ArgRegsSaveSize is zero.
3223 // Then we increase this value each time we meet byval parameter.
3224 // We also increase this value in case of varargs function.
3225 AFI->setArgRegsSaveSize(0);
3226
Oliver Stannardd55e1152014-03-05 15:25:27 +00003227 // Calculate the amount of stack space that we need to allocate to store
3228 // byval and variadic arguments that are passed in registers.
3229 // We need to know this before we allocate the first byval or variadic
3230 // argument, as they will be allocated a stack slot below the CFA (Canonical
3231 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003232 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003233 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003234 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3235 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003236
Tim Northover8cda34f2015-03-11 18:54:22 +00003237 CCValAssign &VA = ArgLocs[i];
3238 unsigned Index = VA.getValNo();
3239 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3240 if (!Flags.isByVal())
3241 continue;
3242
3243 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3244 unsigned RBegin, REnd;
3245 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3246 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3247
3248 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003249 }
3250 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003251
3252 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003253 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003254 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3255 if (RegIdx != array_lengthof(GPRArgRegs))
3256 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003257 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003258
3259 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3260 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003261 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003262
Bob Wilsona4c22902009-04-17 19:07:39 +00003263 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3264 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003265 if (Ins[VA.getValNo()].isOrigArg()) {
3266 std::advance(CurOrigArg,
3267 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3268 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3269 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003270 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003271 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003272 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003273
Bob Wilsona4c22902009-04-17 19:07:39 +00003274 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003275 // f64 and vector types are split up into multiple registers or
3276 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003277 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003278 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003279 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003280 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003281 SDValue ArgValue2;
3282 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003283 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003284 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003285 ArgValue2 = DAG.getLoad(
3286 MVT::f64, dl, Chain, FIN,
3287 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3288 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003289 } else {
3290 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3291 Chain, DAG, dl);
3292 }
Owen Anderson9f944592009-08-11 20:47:22 +00003293 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3294 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003295 ArgValue, ArgValue1,
3296 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003297 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003298 ArgValue, ArgValue2,
3299 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003300 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003301 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003302
Bob Wilson2e076c42009-06-22 23:27:02 +00003303 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003304 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003305
Owen Anderson9f944592009-08-11 20:47:22 +00003306 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003307 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003308 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003309 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003310 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003311 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003312 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003313 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3314 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003315 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003316 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003317
3318 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003319 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003320 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003321 }
3322
3323 // If this is an 8 or 16-bit value, it is really passed promoted
3324 // to 32 bits. Insert an assert[sz]ext to capture this, then
3325 // truncate to the right size.
3326 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003327 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003328 case CCValAssign::Full: break;
3329 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003330 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003331 break;
3332 case CCValAssign::SExt:
3333 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3334 DAG.getValueType(VA.getValVT()));
3335 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3336 break;
3337 case CCValAssign::ZExt:
3338 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3339 DAG.getValueType(VA.getValVT()));
3340 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3341 break;
3342 }
3343
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003344 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003345
3346 } else { // VA.isRegLoc()
3347
3348 // sanity check
3349 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003350 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003351
Andrew Trick05938a52015-02-16 18:10:47 +00003352 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003353
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003354 // Some Ins[] entries become multiple ArgLoc[] entries.
3355 // Process them only once.
3356 if (index != lastInsIndex)
3357 {
3358 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003359 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003360 // This can be changed with more analysis.
3361 // In case of tail call optimization mark all arguments mutable.
3362 // Since they could be overwritten by lowering of arguments in case of
3363 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003364 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003365 assert(Ins[index].isOrigArg() &&
3366 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003367 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003368
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003369 int FrameIndex = StoreByValRegs(
3370 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3371 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003372 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003373 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003374 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003375 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003376 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003377 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003378
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003379 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003380 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003381 InVals.push_back(DAG.getLoad(
3382 VA.getValVT(), dl, Chain, FIN,
3383 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3384 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003385 }
3386 lastInsIndex = index;
3387 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003388 }
3389 }
3390
3391 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003392 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003393 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003394 CCInfo.getNextStackOffset(),
3395 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003396
Oliver Stannardb14c6252014-04-02 16:10:33 +00003397 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3398
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003399 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003400}
3401
3402/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003403static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003404 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003405 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003406 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003407 // Maybe this has already been legalized into the constant pool?
3408 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003409 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003410 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003411 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003412 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003413 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003414 } else if (Op->getOpcode() == ISD::BITCAST &&
3415 Op->getValueType(0) == MVT::f64) {
3416 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3417 // created by LowerConstantFP().
3418 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003419 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3420 isNullConstant(BitcastOp->getOperand(0)))
3421 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003422 }
3423 return false;
3424}
3425
Evan Cheng10043e22007-01-19 07:51:42 +00003426/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3427/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003428SDValue
3429ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003430 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003431 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003432 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003433 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003434 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003435 // Constant does not fit, try adjusting it by one?
3436 switch (CC) {
3437 default: break;
3438 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003439 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003440 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003441 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003442 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003443 }
3444 break;
3445 case ISD::SETULT:
3446 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003447 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003448 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003449 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003450 }
3451 break;
3452 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003453 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003454 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003455 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003456 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003457 }
3458 break;
3459 case ISD::SETULE:
3460 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003461 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003462 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003463 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003464 }
3465 break;
3466 }
3467 }
3468 }
3469
3470 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003471 ARMISD::NodeType CompareType;
3472 switch (CondCode) {
3473 default:
3474 CompareType = ARMISD::CMP;
3475 break;
3476 case ARMCC::EQ:
3477 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003478 // Uses only Z Flag
3479 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003480 break;
3481 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003482 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003483 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003484}
3485
3486/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003487SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003488ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003489 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003490 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003491 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003492 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003493 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003494 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003495 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3496 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003497}
3498
Bob Wilson45acbd02011-03-08 01:17:20 +00003499/// duplicateCmp - Glue values can have only one use, so this function
3500/// duplicates a comparison node.
3501SDValue
3502ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3503 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003504 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003505 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3506 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3507
3508 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3509 Cmp = Cmp.getOperand(0);
3510 Opc = Cmp.getOpcode();
3511 if (Opc == ARMISD::CMPFP)
3512 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3513 else {
3514 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3515 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3516 }
3517 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3518}
3519
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003520std::pair<SDValue, SDValue>
3521ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3522 SDValue &ARMcc) const {
3523 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3524
3525 SDValue Value, OverflowCmp;
3526 SDValue LHS = Op.getOperand(0);
3527 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003528 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003529
3530 // FIXME: We are currently always generating CMPs because we don't support
3531 // generating CMN through the backend. This is not as good as the natural
3532 // CMP case because it causes a register dependency and cannot be folded
3533 // later.
3534
3535 switch (Op.getOpcode()) {
3536 default:
3537 llvm_unreachable("Unknown overflow instruction!");
3538 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003539 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3540 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3541 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003542 break;
3543 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003544 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3545 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3546 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003547 break;
3548 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003549 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3550 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3551 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003552 break;
3553 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003554 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3555 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3556 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003557 break;
3558 } // switch (...)
3559
3560 return std::make_pair(Value, OverflowCmp);
3561}
3562
3563
3564SDValue
3565ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3566 // Let legalize expand this if it isn't a legal type yet.
3567 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3568 return SDValue();
3569
3570 SDValue Value, OverflowCmp;
3571 SDValue ARMcc;
3572 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3573 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003574 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003575 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003576 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3577 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003578 EVT VT = Op.getValueType();
3579
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003580 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003581 ARMcc, CCR, OverflowCmp);
3582
3583 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003584 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003585}
3586
3587
Bill Wendling6a981312010-08-11 08:43:16 +00003588SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3589 SDValue Cond = Op.getOperand(0);
3590 SDValue SelectTrue = Op.getOperand(1);
3591 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003592 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003593 unsigned Opc = Cond.getOpcode();
3594
3595 if (Cond.getResNo() == 1 &&
3596 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3597 Opc == ISD::USUBO)) {
3598 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3599 return SDValue();
3600
3601 SDValue Value, OverflowCmp;
3602 SDValue ARMcc;
3603 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3604 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3605 EVT VT = Op.getValueType();
3606
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003607 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003608 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003609 }
Bill Wendling6a981312010-08-11 08:43:16 +00003610
3611 // Convert:
3612 //
3613 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3614 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3615 //
3616 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3617 const ConstantSDNode *CMOVTrue =
3618 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3619 const ConstantSDNode *CMOVFalse =
3620 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3621
3622 if (CMOVTrue && CMOVFalse) {
3623 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3624 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3625
3626 SDValue True;
3627 SDValue False;
3628 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3629 True = SelectTrue;
3630 False = SelectFalse;
3631 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3632 True = SelectFalse;
3633 False = SelectTrue;
3634 }
3635
3636 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003637 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003638 SDValue ARMcc = Cond.getOperand(2);
3639 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003640 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003641 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003642 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003643 }
3644 }
3645 }
3646
Dan Gohmand4a77c42012-02-24 00:09:36 +00003647 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3648 // undefined bits before doing a full-word comparison with zero.
3649 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003650 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003651
Bill Wendling6a981312010-08-11 08:43:16 +00003652 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003653 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003654 SelectTrue, SelectFalse, ISD::SETNE);
3655}
3656
Joey Gouly881eab52013-08-22 15:29:11 +00003657static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3658 bool &swpCmpOps, bool &swpVselOps) {
3659 // Start by selecting the GE condition code for opcodes that return true for
3660 // 'equality'
3661 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3662 CC == ISD::SETULE)
3663 CondCode = ARMCC::GE;
3664
3665 // and GT for opcodes that return false for 'equality'.
3666 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3667 CC == ISD::SETULT)
3668 CondCode = ARMCC::GT;
3669
3670 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3671 // to swap the compare operands.
3672 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3673 CC == ISD::SETULT)
3674 swpCmpOps = true;
3675
3676 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3677 // If we have an unordered opcode, we need to swap the operands to the VSEL
3678 // instruction (effectively negating the condition).
3679 //
3680 // This also has the effect of swapping which one of 'less' or 'greater'
3681 // returns true, so we also swap the compare operands. It also switches
3682 // whether we return true for 'equality', so we compensate by picking the
3683 // opposite condition code to our original choice.
3684 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3685 CC == ISD::SETUGT) {
3686 swpCmpOps = !swpCmpOps;
3687 swpVselOps = !swpVselOps;
3688 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3689 }
3690
3691 // 'ordered' is 'anything but unordered', so use the VS condition code and
3692 // swap the VSEL operands.
3693 if (CC == ISD::SETO) {
3694 CondCode = ARMCC::VS;
3695 swpVselOps = true;
3696 }
3697
3698 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3699 // code and swap the VSEL operands.
3700 if (CC == ISD::SETUNE) {
3701 CondCode = ARMCC::EQ;
3702 swpVselOps = true;
3703 }
3704}
3705
Oliver Stannard51b1d462014-08-21 12:50:31 +00003706SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3707 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3708 SDValue Cmp, SelectionDAG &DAG) const {
3709 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3710 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3711 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3712 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3713 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3714
3715 SDValue TrueLow = TrueVal.getValue(0);
3716 SDValue TrueHigh = TrueVal.getValue(1);
3717 SDValue FalseLow = FalseVal.getValue(0);
3718 SDValue FalseHigh = FalseVal.getValue(1);
3719
3720 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3721 ARMcc, CCR, Cmp);
3722 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3723 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3724
3725 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3726 } else {
3727 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3728 Cmp);
3729 }
3730}
3731
Dan Gohman21cea8a2010-04-17 15:26:15 +00003732SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003733 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003734 SDValue LHS = Op.getOperand(0);
3735 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003736 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003737 SDValue TrueVal = Op.getOperand(2);
3738 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003739 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003740
Oliver Stannard51b1d462014-08-21 12:50:31 +00003741 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3742 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3743 dl);
3744
3745 // If softenSetCCOperands only returned one value, we should compare it to
3746 // zero.
3747 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003748 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003749 CC = ISD::SETNE;
3750 }
3751 }
3752
Owen Anderson9f944592009-08-11 20:47:22 +00003753 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003754 // Try to generate VSEL on ARMv8.
3755 // The VSEL instruction can't use all the usual ARM condition
3756 // codes: it only has two bits to select the condition code, so it's
3757 // constrained to use only GE, GT, VS and EQ.
3758 //
3759 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3760 // swap the operands of the previous compare instruction (effectively
3761 // inverting the compare condition, swapping 'less' and 'greater') and
3762 // sometimes need to swap the operands to the VSEL (which inverts the
3763 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003764 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3765 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003766 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3767 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3768 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003769 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003770 std::swap(TrueVal, FalseVal);
3771 }
3772 }
3773
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003774 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003775 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003776 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003777 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003778 }
3779
3780 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003781 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003782
Scott Douglass7ad77922015-04-08 17:18:28 +00003783 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003784 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3785 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003786 bool swpCmpOps = false;
3787 bool swpVselOps = false;
3788 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3789
3790 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3791 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3792 if (swpCmpOps)
3793 std::swap(LHS, RHS);
3794 if (swpVselOps)
3795 std::swap(TrueVal, FalseVal);
3796 }
3797 }
3798
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003799 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003800 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003801 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003802 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003803 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003804 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003805 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003806 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003807 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003808 }
3809 return Result;
3810}
3811
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003812/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3813/// to morph to an integer compare sequence.
3814static bool canChangeToInt(SDValue Op, bool &SeenZero,
3815 const ARMSubtarget *Subtarget) {
3816 SDNode *N = Op.getNode();
3817 if (!N->hasOneUse())
3818 // Otherwise it requires moving the value from fp to integer registers.
3819 return false;
3820 if (!N->getNumValues())
3821 return false;
3822 EVT VT = Op.getValueType();
3823 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3824 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3825 // vmrs are very slow, e.g. cortex-a8.
3826 return false;
3827
3828 if (isFloatingPointZero(Op)) {
3829 SeenZero = true;
3830 return true;
3831 }
3832 return ISD::isNormalLoad(N);
3833}
3834
3835static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3836 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003837 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003838
3839 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003840 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003841 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003842 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003843 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003844
3845 llvm_unreachable("Unknown VFP cmp argument!");
3846}
3847
3848static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3849 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003850 SDLoc dl(Op);
3851
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003852 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003853 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3854 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003855 return;
3856 }
3857
3858 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3859 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003860 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003861 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003862 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003863 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003864 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003865
3866 EVT PtrType = Ptr.getValueType();
3867 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003868 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3869 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3870 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003871 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003872 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003873 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003874 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003875 return;
3876 }
3877
3878 llvm_unreachable("Unknown VFP cmp argument!");
3879}
3880
3881/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3882/// f32 and even f64 comparisons to integer ones.
3883SDValue
3884ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3885 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003886 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003887 SDValue LHS = Op.getOperand(2);
3888 SDValue RHS = Op.getOperand(3);
3889 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003890 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003891
Evan Chengd12af5d2012-03-01 23:27:13 +00003892 bool LHSSeenZero = false;
3893 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3894 bool RHSSeenZero = false;
3895 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3896 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003897 // If unsafe fp math optimization is enabled and there are no other uses of
3898 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003899 // to an integer comparison.
3900 if (CC == ISD::SETOEQ)
3901 CC = ISD::SETEQ;
3902 else if (CC == ISD::SETUNE)
3903 CC = ISD::SETNE;
3904
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003905 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003906 SDValue ARMcc;
3907 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003908 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3909 bitcastf32Toi32(LHS, DAG), Mask);
3910 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3911 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003912 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3913 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3914 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3915 Chain, Dest, ARMcc, CCR, Cmp);
3916 }
3917
3918 SDValue LHS1, LHS2;
3919 SDValue RHS1, RHS2;
3920 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3921 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003922 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3923 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003924 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003925 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003926 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003927 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003928 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003929 }
3930
3931 return SDValue();
3932}
3933
3934SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3935 SDValue Chain = Op.getOperand(0);
3936 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3937 SDValue LHS = Op.getOperand(2);
3938 SDValue RHS = Op.getOperand(3);
3939 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003940 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003941
Oliver Stannard51b1d462014-08-21 12:50:31 +00003942 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3943 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3944 dl);
3945
3946 // If softenSetCCOperands only returned one value, we should compare it to
3947 // zero.
3948 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003949 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003950 CC = ISD::SETNE;
3951 }
3952 }
3953
Owen Anderson9f944592009-08-11 20:47:22 +00003954 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003955 SDValue ARMcc;
3956 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003957 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003958 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003959 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003960 }
3961
Owen Anderson9f944592009-08-11 20:47:22 +00003962 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003963
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003964 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003965 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3966 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003967 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003968 return Result;
3969 }
3970
Evan Cheng10043e22007-01-19 07:51:42 +00003971 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003972 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003973
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003974 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003975 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003976 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003977 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003978 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003979 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003980 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003981 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003982 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003983 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003984 }
3985 return Res;
3986}
3987
Dan Gohman21cea8a2010-04-17 15:26:15 +00003988SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003989 SDValue Chain = Op.getOperand(0);
3990 SDValue Table = Op.getOperand(1);
3991 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003992 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003993
Mehdi Amini44ede332015-07-09 02:09:04 +00003994 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003995 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003996 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003997 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003998 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003999 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004000 if (Subtarget->isThumb2()) {
4001 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4002 // which does another jump to the destination. This also makes it easier
4003 // to translate it to TBB / TBH later.
4004 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004005 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004006 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004007 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00004008 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004009 Addr =
4010 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
4011 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4012 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004013 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004014 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004015 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004016 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004017 Addr =
4018 DAG.getLoad(PTy, dl, Chain, Addr,
4019 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4020 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004021 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004022 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004023 }
Evan Cheng10043e22007-01-19 07:51:42 +00004024}
4025
Eli Friedman2d4055b2011-11-09 23:36:02 +00004026static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004027 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004028 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004029
James Molloy547d4c02012-02-20 09:24:05 +00004030 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4031 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4032 return Op;
4033 return DAG.UnrollVectorOp(Op.getNode());
4034 }
4035
4036 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4037 "Invalid type for custom lowering!");
4038 if (VT != MVT::v4i16)
4039 return DAG.UnrollVectorOp(Op.getNode());
4040
4041 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4042 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004043}
4044
Oliver Stannard51b1d462014-08-21 12:50:31 +00004045SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004046 EVT VT = Op.getValueType();
4047 if (VT.isVector())
4048 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004049 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4050 RTLIB::Libcall LC;
4051 if (Op.getOpcode() == ISD::FP_TO_SINT)
4052 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4053 Op.getValueType());
4054 else
4055 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4056 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004057 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004058 /*isSigned*/ false, SDLoc(Op)).first;
4059 }
4060
James Molloyfa041152015-03-23 16:15:16 +00004061 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004062}
4063
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004064static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4065 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004066 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004067
Eli Friedman2d4055b2011-11-09 23:36:02 +00004068 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4069 if (VT.getVectorElementType() == MVT::f32)
4070 return Op;
4071 return DAG.UnrollVectorOp(Op.getNode());
4072 }
4073
Duncan Sandsa41634e2011-08-12 14:54:45 +00004074 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4075 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004076 if (VT != MVT::v4f32)
4077 return DAG.UnrollVectorOp(Op.getNode());
4078
4079 unsigned CastOpc;
4080 unsigned Opc;
4081 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004082 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004083 case ISD::SINT_TO_FP:
4084 CastOpc = ISD::SIGN_EXTEND;
4085 Opc = ISD::SINT_TO_FP;
4086 break;
4087 case ISD::UINT_TO_FP:
4088 CastOpc = ISD::ZERO_EXTEND;
4089 Opc = ISD::UINT_TO_FP;
4090 break;
4091 }
4092
4093 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4094 return DAG.getNode(Opc, dl, VT, Op);
4095}
4096
Oliver Stannard51b1d462014-08-21 12:50:31 +00004097SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004098 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004099 if (VT.isVector())
4100 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004101 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4102 RTLIB::Libcall LC;
4103 if (Op.getOpcode() == ISD::SINT_TO_FP)
4104 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4105 Op.getValueType());
4106 else
4107 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4108 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004109 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004110 /*isSigned*/ false, SDLoc(Op)).first;
4111 }
4112
James Molloyfa041152015-03-23 16:15:16 +00004113 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004114}
4115
Evan Cheng25f93642010-07-08 02:08:50 +00004116SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004117 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004118 SDValue Tmp0 = Op.getOperand(0);
4119 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004120 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004121 EVT VT = Op.getValueType();
4122 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004123 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4124 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4125 bool UseNEON = !InGPR && Subtarget->hasNEON();
4126
4127 if (UseNEON) {
4128 // Use VBSL to copy the sign bit.
4129 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4130 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004131 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004132 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4133 if (VT == MVT::f64)
4134 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4135 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004136 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004137 else /*if (VT == MVT::f32)*/
4138 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4139 if (SrcVT == MVT::f32) {
4140 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4141 if (VT == MVT::f64)
4142 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4143 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004144 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004145 } else if (VT == MVT::f32)
4146 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4147 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004148 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004149 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4150 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4151
4152 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004153 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004154 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4155 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4156 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004157
Evan Chengd6b641e2011-02-23 02:24:55 +00004158 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4159 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4160 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004161 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004162 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4163 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004164 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004165 } else {
4166 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4167 }
4168
4169 return Res;
4170 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004171
4172 // Bitcast operand 1 to i32.
4173 if (SrcVT == MVT::f64)
4174 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004175 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004176 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4177
Evan Chengd6b641e2011-02-23 02:24:55 +00004178 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004179 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4180 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004181 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4182 if (VT == MVT::f32) {
4183 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4184 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4185 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4186 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004187 }
4188
Evan Chengd6b641e2011-02-23 02:24:55 +00004189 // f64: Or the high part with signbit and then combine two parts.
4190 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004191 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004192 SDValue Lo = Tmp0.getValue(0);
4193 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4194 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4195 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004196}
4197
Evan Cheng168ced92010-05-22 01:47:14 +00004198SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4199 MachineFunction &MF = DAG.getMachineFunction();
4200 MachineFrameInfo *MFI = MF.getFrameInfo();
4201 MFI->setReturnAddressIsTaken(true);
4202
Bill Wendling908bf812014-01-06 00:43:20 +00004203 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004204 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004205
Evan Cheng168ced92010-05-22 01:47:14 +00004206 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004207 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004208 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4209 if (Depth) {
4210 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004211 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004212 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4213 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004214 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004215 }
4216
4217 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004218 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004219 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4220}
4221
Dan Gohman21cea8a2010-04-17 15:26:15 +00004222SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004223 const ARMBaseRegisterInfo &ARI =
4224 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4225 MachineFunction &MF = DAG.getMachineFunction();
4226 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004227 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004228
Owen Anderson53aa7a92009-08-10 22:56:29 +00004229 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004230 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004231 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004232 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004233 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4234 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004235 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4236 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004237 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004238 return FrameAddr;
4239}
4240
Renato Golinc7aea402014-05-06 16:51:25 +00004241// FIXME? Maybe this could be a TableGen attribute on some registers and
4242// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004243unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4244 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004245 unsigned Reg = StringSwitch<unsigned>(RegName)
4246 .Case("sp", ARM::SP)
4247 .Default(0);
4248 if (Reg)
4249 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004250 report_fatal_error(Twine("Invalid register name \""
4251 + StringRef(RegName) + "\"."));
4252}
4253
4254// Result is 64 bit value so split into two 32 bit values and return as a
4255// pair of values.
4256static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4257 SelectionDAG &DAG) {
4258 SDLoc DL(N);
4259
4260 // This function is only supposed to be called for i64 type destination.
4261 assert(N->getValueType(0) == MVT::i64
4262 && "ExpandREAD_REGISTER called for non-i64 type result.");
4263
4264 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4265 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4266 N->getOperand(0),
4267 N->getOperand(1));
4268
4269 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4270 Read.getValue(1)));
4271 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004272}
4273
Quentin Colombet901f0362015-12-04 01:53:14 +00004274/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4275/// When \p DstVT, the destination type of \p BC, is on the vector
4276/// register bank and the source of bitcast, \p Op, operates on the same bank,
4277/// it might be possible to combine them, such that everything stays on the
4278/// vector register bank.
4279/// \p return The node that would replace \p BT, if the combine
4280/// is possible.
4281static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4282 SelectionDAG &DAG) {
4283 SDValue Op = BC->getOperand(0);
4284 EVT DstVT = BC->getValueType(0);
4285
4286 // The only vector instruction that can produce a scalar (remember,
4287 // since the bitcast was about to be turned into VMOVDRR, the source
4288 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4289 // Moreover, we can do this combine only if there is one use.
4290 // Finally, if the destination type is not a vector, there is not
4291 // much point on forcing everything on the vector bank.
4292 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4293 !Op.hasOneUse())
4294 return SDValue();
4295
4296 // If the index is not constant, we will introduce an additional
4297 // multiply that will stick.
4298 // Give up in that case.
4299 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4300 if (!Index)
4301 return SDValue();
4302 unsigned DstNumElt = DstVT.getVectorNumElements();
4303
4304 // Compute the new index.
4305 const APInt &APIntIndex = Index->getAPIntValue();
4306 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4307 NewIndex *= APIntIndex;
4308 // Check if the new constant index fits into i32.
4309 if (NewIndex.getBitWidth() > 32)
4310 return SDValue();
4311
4312 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4313 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4314 SDLoc dl(Op);
4315 SDValue ExtractSrc = Op.getOperand(0);
4316 EVT VecVT = EVT::getVectorVT(
4317 *DAG.getContext(), DstVT.getScalarType(),
4318 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4319 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4320 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4321 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4322}
4323
Wesley Peck527da1b2010-11-23 03:31:01 +00004324/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004325/// expand a bit convert where either the source or destination type is i64 to
4326/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4327/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4328/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004329static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004330 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004331 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004332 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004333
Bob Wilson59b70ea2010-04-17 05:30:19 +00004334 // This function is only supposed to be called for i64 types, either as the
4335 // source or destination of the bit convert.
4336 EVT SrcVT = Op.getValueType();
4337 EVT DstVT = N->getValueType(0);
4338 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004339 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004340
Bob Wilson59b70ea2010-04-17 05:30:19 +00004341 // Turn i64->f64 into VMOVDRR.
4342 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004343 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4344 // if we can combine the bitcast with its source.
4345 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4346 return Val;
4347
Owen Anderson9f944592009-08-11 20:47:22 +00004348 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004349 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004350 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004351 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004352 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004353 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004354 }
Bob Wilson7117a912009-03-20 22:42:55 +00004355
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004356 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004357 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004358 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004359 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004360 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004361 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4362 DAG.getVTList(MVT::i32, MVT::i32),
4363 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4364 else
4365 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4366 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004367 // Merge the pieces into a single i64 value.
4368 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4369 }
Bob Wilson7117a912009-03-20 22:42:55 +00004370
Bob Wilson59b70ea2010-04-17 05:30:19 +00004371 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004372}
4373
Bob Wilson2e076c42009-06-22 23:27:02 +00004374/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004375/// Zero vectors are used to represent vector negation and in those cases
4376/// will be implemented with the NEON VNEG instruction. However, VNEG does
4377/// not support i64 elements, so sometimes the zero vectors will need to be
4378/// explicitly constructed. Regardless, use a canonical VMOV to create the
4379/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004380static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004381 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004382 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004383 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004384 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4385 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004386 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004387}
4388
Jim Grosbach624fcb22009-10-31 21:00:56 +00004389/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4390/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004391SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4392 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004393 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4394 EVT VT = Op.getValueType();
4395 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004396 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004397 SDValue ShOpLo = Op.getOperand(0);
4398 SDValue ShOpHi = Op.getOperand(1);
4399 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004400 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004401 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004402
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004403 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4404
Jim Grosbach624fcb22009-10-31 21:00:56 +00004405 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004406 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004407 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4408 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004409 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004410 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4411 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004412 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004413
4414 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004415 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4416 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004417 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004418 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004419 CCR, Cmp);
4420
4421 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004422 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004423}
4424
Jim Grosbach5d994042009-10-31 19:38:01 +00004425/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4426/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004427SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4428 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004429 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4430 EVT VT = Op.getValueType();
4431 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004432 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004433 SDValue ShOpLo = Op.getOperand(0);
4434 SDValue ShOpHi = Op.getOperand(1);
4435 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004436 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004437
4438 assert(Op.getOpcode() == ISD::SHL_PARTS);
4439 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004440 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004441 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4442 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004443 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004444 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4445 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4446
4447 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4448 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004449 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4450 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004451 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004452 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004453 CCR, Cmp);
4454
4455 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004456 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004457}
4458
Jim Grosbach535d3b42010-09-08 03:54:02 +00004459SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004460 SelectionDAG &DAG) const {
4461 // The rounding mode is in bits 23:22 of the FPSCR.
4462 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4463 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4464 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004465 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004466 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004467 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004468 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004469 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004470 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004471 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004472 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004473 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004474 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004475}
4476
Jim Grosbach8546ec92010-01-18 19:58:49 +00004477static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4478 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004479 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004480 EVT VT = N->getValueType(0);
4481 if (VT.isVector()) {
4482 assert(ST->hasNEON());
4483
4484 // Compute the least significant set bit: LSB = X & -X
4485 SDValue X = N->getOperand(0);
4486 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4487 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4488
4489 EVT ElemTy = VT.getVectorElementType();
4490
4491 if (ElemTy == MVT::i8) {
4492 // Compute with: cttz(x) = ctpop(lsb - 1)
4493 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4494 DAG.getTargetConstant(1, dl, ElemTy));
4495 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4496 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4497 }
4498
4499 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4500 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4501 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4502 unsigned NumBits = ElemTy.getSizeInBits();
4503 SDValue WidthMinus1 =
4504 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4505 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4506 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4507 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4508 }
4509
4510 // Compute with: cttz(x) = ctpop(lsb - 1)
4511
4512 // Since we can only compute the number of bits in a byte with vcnt.8, we
4513 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4514 // and i64.
4515
4516 // Compute LSB - 1.
4517 SDValue Bits;
4518 if (ElemTy == MVT::i64) {
4519 // Load constant 0xffff'ffff'ffff'ffff to register.
4520 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4521 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4522 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4523 } else {
4524 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4525 DAG.getTargetConstant(1, dl, ElemTy));
4526 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4527 }
4528
4529 // Count #bits with vcnt.8.
4530 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4531 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4532 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4533
4534 // Gather the #bits with vpaddl (pairwise add.)
4535 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4536 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4537 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4538 Cnt8);
4539 if (ElemTy == MVT::i16)
4540 return Cnt16;
4541
4542 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4543 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4544 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4545 Cnt16);
4546 if (ElemTy == MVT::i32)
4547 return Cnt32;
4548
4549 assert(ElemTy == MVT::i64);
4550 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4551 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4552 Cnt32);
4553 return Cnt64;
4554 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004555
4556 if (!ST->hasV6T2Ops())
4557 return SDValue();
4558
James Molloyb5640982015-11-13 16:05:22 +00004559 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004560 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4561}
4562
Evan Chengb4eae132012-12-04 22:41:50 +00004563/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4564/// for each 16-bit element from operand, repeated. The basic idea is to
4565/// leverage vcnt to get the 8-bit counts, gather and add the results.
4566///
4567/// Trace for v4i16:
4568/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4569/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4570/// 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 +00004571/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004572/// [b0 b1 b2 b3 b4 b5 b6 b7]
4573/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4574/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4575/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4576static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4577 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004578 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004579
4580 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4581 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4582 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4583 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4584 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4585 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4586}
4587
4588/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4589/// bit-count for each 16-bit element from the operand. We need slightly
4590/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4591/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004592///
Evan Chengb4eae132012-12-04 22:41:50 +00004593/// Trace for v4i16:
4594/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4595/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4596/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4597/// v4i16:Extracted = [k0 k1 k2 k3 ]
4598static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4599 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004600 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004601
4602 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4603 if (VT.is64BitVector()) {
4604 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4605 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004606 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004607 } else {
4608 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004609 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004610 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4611 }
4612}
4613
4614/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4615/// bit-count for each 32-bit element from the operand. The idea here is
4616/// to split the vector into 16-bit elements, leverage the 16-bit count
4617/// routine, and then combine the results.
4618///
4619/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4620/// input = [v0 v1 ] (vi: 32-bit elements)
4621/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4622/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004623/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004624/// [k0 k1 k2 k3 ]
4625/// N1 =+[k1 k0 k3 k2 ]
4626/// [k0 k2 k1 k3 ]
4627/// N2 =+[k1 k3 k0 k2 ]
4628/// [k0 k2 k1 k3 ]
4629/// Extended =+[k1 k3 k0 k2 ]
4630/// [k0 k2 ]
4631/// Extracted=+[k1 k3 ]
4632///
4633static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4634 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004635 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004636
4637 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4638
4639 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4640 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4641 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4642 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4643 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4644
4645 if (VT.is64BitVector()) {
4646 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4647 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004648 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004649 } else {
4650 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004651 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004652 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4653 }
4654}
4655
4656static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4657 const ARMSubtarget *ST) {
4658 EVT VT = N->getValueType(0);
4659
4660 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004661 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4662 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004663 "Unexpected type for custom ctpop lowering");
4664
4665 if (VT.getVectorElementType() == MVT::i32)
4666 return lowerCTPOP32BitElements(N, DAG);
4667 else
4668 return lowerCTPOP16BitElements(N, DAG);
4669}
4670
Bob Wilson2e076c42009-06-22 23:27:02 +00004671static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4672 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004673 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004674 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004675
Bob Wilson7d471332010-11-18 21:16:28 +00004676 if (!VT.isVector())
4677 return SDValue();
4678
Bob Wilson2e076c42009-06-22 23:27:02 +00004679 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004680 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004681
Bob Wilson7d471332010-11-18 21:16:28 +00004682 // Left shifts translate directly to the vshiftu intrinsic.
4683 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004684 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004685 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4686 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004687 N->getOperand(0), N->getOperand(1));
4688
4689 assert((N->getOpcode() == ISD::SRA ||
4690 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4691
4692 // NEON uses the same intrinsics for both left and right shifts. For
4693 // right shifts, the shift amounts are negative, so negate the vector of
4694 // shift amounts.
4695 EVT ShiftVT = N->getOperand(1).getValueType();
4696 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4697 getZeroVector(ShiftVT, DAG, dl),
4698 N->getOperand(1));
4699 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4700 Intrinsic::arm_neon_vshifts :
4701 Intrinsic::arm_neon_vshiftu);
4702 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004703 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004704 N->getOperand(0), NegatedCount);
4705}
4706
4707static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4708 const ARMSubtarget *ST) {
4709 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004710 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004711
Eli Friedman682d8c12009-08-22 03:13:10 +00004712 // We can get here for a node like i32 = ISD::SHL i32, i64
4713 if (VT != MVT::i64)
4714 return SDValue();
4715
4716 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004717 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004718
Chris Lattnerf81d5882007-11-24 07:07:01 +00004719 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004720 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004721 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004722
Chris Lattnerf81d5882007-11-24 07:07:01 +00004723 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004724 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004725
Chris Lattnerf81d5882007-11-24 07:07:01 +00004726 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004727 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004728 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004729 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004730 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004731
Chris Lattnerf81d5882007-11-24 07:07:01 +00004732 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4733 // captures the result into a carry flag.
4734 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004735 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004736
Chris Lattnerf81d5882007-11-24 07:07:01 +00004737 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004738 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004739
Chris Lattnerf81d5882007-11-24 07:07:01 +00004740 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004741 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004742}
4743
Bob Wilson2e076c42009-06-22 23:27:02 +00004744static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4745 SDValue TmpOp0, TmpOp1;
4746 bool Invert = false;
4747 bool Swap = false;
4748 unsigned Opc = 0;
4749
4750 SDValue Op0 = Op.getOperand(0);
4751 SDValue Op1 = Op.getOperand(1);
4752 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004753 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004754 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004755 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004756 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004757
James Molloybf170092015-08-20 16:33:44 +00004758 if (CmpVT.getVectorElementType() == MVT::i64)
4759 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4760 // but it's possible that our operands are 64-bit but our result is 32-bit.
4761 // Bail in this case.
4762 return SDValue();
4763
Oliver Stannard51b1d462014-08-21 12:50:31 +00004764 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004765 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004766 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004767 case ISD::SETUNE:
4768 case ISD::SETNE: Invert = true; // Fallthrough
4769 case ISD::SETOEQ:
4770 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4771 case ISD::SETOLT:
4772 case ISD::SETLT: Swap = true; // Fallthrough
4773 case ISD::SETOGT:
4774 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4775 case ISD::SETOLE:
4776 case ISD::SETLE: Swap = true; // Fallthrough
4777 case ISD::SETOGE:
4778 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4779 case ISD::SETUGE: Swap = true; // Fallthrough
4780 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4781 case ISD::SETUGT: Swap = true; // Fallthrough
4782 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4783 case ISD::SETUEQ: Invert = true; // Fallthrough
4784 case ISD::SETONE:
4785 // Expand this to (OLT | OGT).
4786 TmpOp0 = Op0;
4787 TmpOp1 = Op1;
4788 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004789 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4790 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004791 break;
4792 case ISD::SETUO: Invert = true; // Fallthrough
4793 case ISD::SETO:
4794 // Expand this to (OLT | OGE).
4795 TmpOp0 = Op0;
4796 TmpOp1 = Op1;
4797 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004798 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4799 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004800 break;
4801 }
4802 } else {
4803 // Integer comparisons.
4804 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004805 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004806 case ISD::SETNE: Invert = true;
4807 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4808 case ISD::SETLT: Swap = true;
4809 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4810 case ISD::SETLE: Swap = true;
4811 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4812 case ISD::SETULT: Swap = true;
4813 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4814 case ISD::SETULE: Swap = true;
4815 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4816 }
4817
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004818 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004819 if (Opc == ARMISD::VCEQ) {
4820
4821 SDValue AndOp;
4822 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4823 AndOp = Op0;
4824 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4825 AndOp = Op1;
4826
4827 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004828 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004829 AndOp = AndOp.getOperand(0);
4830
4831 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4832 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004833 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4834 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004835 Invert = !Invert;
4836 }
4837 }
4838 }
4839
4840 if (Swap)
4841 std::swap(Op0, Op1);
4842
Owen Andersonc7baee32010-11-08 23:21:22 +00004843 // If one of the operands is a constant vector zero, attempt to fold the
4844 // comparison to a specialized compare-against-zero form.
4845 SDValue SingleOp;
4846 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4847 SingleOp = Op0;
4848 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4849 if (Opc == ARMISD::VCGE)
4850 Opc = ARMISD::VCLEZ;
4851 else if (Opc == ARMISD::VCGT)
4852 Opc = ARMISD::VCLTZ;
4853 SingleOp = Op1;
4854 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004855
Owen Andersonc7baee32010-11-08 23:21:22 +00004856 SDValue Result;
4857 if (SingleOp.getNode()) {
4858 switch (Opc) {
4859 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004860 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004861 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004862 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004863 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004864 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004865 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004866 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004867 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004868 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004869 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004870 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004871 }
4872 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004873 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004874 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004875
Tim Northover45aa89c2015-02-08 00:50:47 +00004876 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4877
Bob Wilson2e076c42009-06-22 23:27:02 +00004878 if (Invert)
4879 Result = DAG.getNOT(dl, Result, VT);
4880
4881 return Result;
4882}
4883
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00004884static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) {
4885 SDValue LHS = Op.getOperand(0);
4886 SDValue RHS = Op.getOperand(1);
4887 SDValue Carry = Op.getOperand(2);
4888 SDValue Cond = Op.getOperand(3);
4889 SDLoc DL(Op);
4890
4891 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only.");
4892
4893 assert(Carry.getOpcode() != ISD::CARRY_FALSE);
4894 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
4895 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
4896
4897 SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
4898 SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
4899 SDValue ARMcc = DAG.getConstant(
4900 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
4901 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4902 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
4903 Cmp.getValue(1), SDValue());
4904 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
4905 CCR, Chain.getValue(1));
4906}
4907
Bob Wilson5b2b5042010-06-14 22:19:57 +00004908/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4909/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004910/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004911static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4912 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004913 SDLoc dl, EVT &VT, bool is128Bits,
4914 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004915 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004916
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004917 // SplatBitSize is set to the smallest size that splats the vector, so a
4918 // zero vector will always have SplatBitSize == 8. However, NEON modified
4919 // immediate instructions others than VMOV do not support the 8-bit encoding
4920 // of a zero vector, and the default encoding of zero is supposed to be the
4921 // 32-bit version.
4922 if (SplatBits == 0)
4923 SplatBitSize = 32;
4924
Bob Wilson2e076c42009-06-22 23:27:02 +00004925 switch (SplatBitSize) {
4926 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004927 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004928 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004929 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004930 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004931 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004932 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004933 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004934 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004935
4936 case 16:
4937 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004938 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004939 if ((SplatBits & ~0xff) == 0) {
4940 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004941 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004942 Imm = SplatBits;
4943 break;
4944 }
4945 if ((SplatBits & ~0xff00) == 0) {
4946 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004947 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004948 Imm = SplatBits >> 8;
4949 break;
4950 }
4951 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004952
4953 case 32:
4954 // NEON's 32-bit VMOV supports splat values where:
4955 // * only one byte is nonzero, or
4956 // * the least significant byte is 0xff and the second byte is nonzero, or
4957 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004958 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004959 if ((SplatBits & ~0xff) == 0) {
4960 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004961 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004962 Imm = SplatBits;
4963 break;
4964 }
4965 if ((SplatBits & ~0xff00) == 0) {
4966 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004967 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004968 Imm = SplatBits >> 8;
4969 break;
4970 }
4971 if ((SplatBits & ~0xff0000) == 0) {
4972 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004973 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004974 Imm = SplatBits >> 16;
4975 break;
4976 }
4977 if ((SplatBits & ~0xff000000) == 0) {
4978 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004979 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004980 Imm = SplatBits >> 24;
4981 break;
4982 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004983
Owen Andersona4076922010-11-05 21:57:54 +00004984 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4985 if (type == OtherModImm) return SDValue();
4986
Bob Wilson2e076c42009-06-22 23:27:02 +00004987 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004988 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4989 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004990 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004991 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004992 break;
4993 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004994
4995 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004996 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4997 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004998 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004999 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005000 break;
5001 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005002
5003 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
5004 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
5005 // VMOV.I32. A (very) minor optimization would be to replicate the value
5006 // and fall through here to test for a valid 64-bit splat. But, then the
5007 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00005008 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005009
5010 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00005011 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005012 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005013 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00005014 uint64_t BitMask = 0xff;
5015 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005016 unsigned ImmMask = 1;
5017 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005018 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005019 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005020 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005021 Imm |= ImmMask;
5022 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005023 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005024 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005025 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005026 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005027 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005028
Mehdi Aminiffc14022015-07-08 01:00:38 +00005029 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005030 // swap higher and lower 32 bit word
5031 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5032
Bob Wilson6eae5202010-06-11 21:34:50 +00005033 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005034 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005035 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005036 break;
5037 }
5038
Bob Wilson6eae5202010-06-11 21:34:50 +00005039 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005040 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005041 }
5042
Bob Wilsona3f19012010-07-13 21:16:48 +00005043 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005044 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005045}
5046
Lang Hames591cdaf2012-03-29 21:56:11 +00005047SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5048 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005049 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005050 return SDValue();
5051
Tim Northoverf79c3a52013-08-20 08:57:11 +00005052 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005053 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005054
Oliver Stannard51b1d462014-08-21 12:50:31 +00005055 // Use the default (constant pool) lowering for double constants when we have
5056 // an SP-only FPU
5057 if (IsDouble && Subtarget->isFPOnlySP())
5058 return SDValue();
5059
Lang Hames591cdaf2012-03-29 21:56:11 +00005060 // Try splatting with a VMOV.f32...
5061 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005062 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5063
Lang Hames591cdaf2012-03-29 21:56:11 +00005064 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005065 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5066 // We have code in place to select a valid ConstantFP already, no need to
5067 // do any mangling.
5068 return Op;
5069 }
5070
5071 // It's a float and we are trying to use NEON operations where
5072 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005073 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005074 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005075 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5076 NewVal);
5077 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005078 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005079 }
5080
Tim Northoverf79c3a52013-08-20 08:57:11 +00005081 // The rest of our options are NEON only, make sure that's allowed before
5082 // proceeding..
5083 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5084 return SDValue();
5085
Lang Hames591cdaf2012-03-29 21:56:11 +00005086 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005087 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5088
5089 // It wouldn't really be worth bothering for doubles except for one very
5090 // important value, which does happen to match: 0.0. So make sure we don't do
5091 // anything stupid.
5092 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5093 return SDValue();
5094
5095 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005096 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5097 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005098 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005099 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005100 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5101 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005102 if (IsDouble)
5103 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5104
5105 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005106 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5107 VecConstant);
5108 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005109 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005110 }
5111
5112 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005113 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005114 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005115 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005116 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005117 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005118
5119 if (IsDouble)
5120 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5121
5122 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005123 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5124 VecConstant);
5125 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005126 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005127 }
5128
5129 return SDValue();
5130}
5131
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005132// check if an VEXT instruction can handle the shuffle mask when the
5133// vector sources of the shuffle are the same.
5134static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5135 unsigned NumElts = VT.getVectorNumElements();
5136
5137 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5138 if (M[0] < 0)
5139 return false;
5140
5141 Imm = M[0];
5142
5143 // If this is a VEXT shuffle, the immediate value is the index of the first
5144 // element. The other shuffle indices must be the successive elements after
5145 // the first one.
5146 unsigned ExpectedElt = Imm;
5147 for (unsigned i = 1; i < NumElts; ++i) {
5148 // Increment the expected index. If it wraps around, just follow it
5149 // back to index zero and keep going.
5150 ++ExpectedElt;
5151 if (ExpectedElt == NumElts)
5152 ExpectedElt = 0;
5153
5154 if (M[i] < 0) continue; // ignore UNDEF indices
5155 if (ExpectedElt != static_cast<unsigned>(M[i]))
5156 return false;
5157 }
5158
5159 return true;
5160}
5161
Lang Hames591cdaf2012-03-29 21:56:11 +00005162
Benjamin Kramer339ced42012-01-15 13:16:05 +00005163static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005164 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005165 unsigned NumElts = VT.getVectorNumElements();
5166 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005167
5168 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5169 if (M[0] < 0)
5170 return false;
5171
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005172 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005173
5174 // If this is a VEXT shuffle, the immediate value is the index of the first
5175 // element. The other shuffle indices must be the successive elements after
5176 // the first one.
5177 unsigned ExpectedElt = Imm;
5178 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005179 // Increment the expected index. If it wraps around, it may still be
5180 // a VEXT but the source vectors must be swapped.
5181 ExpectedElt += 1;
5182 if (ExpectedElt == NumElts * 2) {
5183 ExpectedElt = 0;
5184 ReverseVEXT = true;
5185 }
5186
Bob Wilson411dfad2010-08-17 05:54:34 +00005187 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005188 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005189 return false;
5190 }
5191
5192 // Adjust the index value if the source operands will be swapped.
5193 if (ReverseVEXT)
5194 Imm -= NumElts;
5195
Bob Wilson32cd8552009-08-19 17:03:43 +00005196 return true;
5197}
5198
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005199/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5200/// instruction with the specified blocksize. (The order of the elements
5201/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005202static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005203 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5204 "Only possible block sizes for VREV are: 16, 32, 64");
5205
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005206 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005207 if (EltSz == 64)
5208 return false;
5209
5210 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005211 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005212 // If the first shuffle index is UNDEF, be optimistic.
5213 if (M[0] < 0)
5214 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005215
5216 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5217 return false;
5218
5219 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005220 if (M[i] < 0) continue; // ignore UNDEF indices
5221 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005222 return false;
5223 }
5224
5225 return true;
5226}
5227
Benjamin Kramer339ced42012-01-15 13:16:05 +00005228static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005229 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5230 // range, then 0 is placed into the resulting vector. So pretty much any mask
5231 // of 8 elements can work here.
5232 return VT == MVT::v8i8 && M.size() == 8;
5233}
5234
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005235// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5236// checking that pairs of elements in the shuffle mask represent the same index
5237// in each vector, incrementing the expected index by 2 at each step.
5238// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5239// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5240// v2={e,f,g,h}
5241// WhichResult gives the offset for each element in the mask based on which
5242// of the two results it belongs to.
5243//
5244// The transpose can be represented either as:
5245// result1 = shufflevector v1, v2, result1_shuffle_mask
5246// result2 = shufflevector v1, v2, result2_shuffle_mask
5247// where v1/v2 and the shuffle masks have the same number of elements
5248// (here WhichResult (see below) indicates which result is being checked)
5249//
5250// or as:
5251// results = shufflevector v1, v2, shuffle_mask
5252// where both results are returned in one vector and the shuffle mask has twice
5253// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5254// want to check the low half and high half of the shuffle mask as if it were
5255// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005256static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005257 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5258 if (EltSz == 64)
5259 return false;
5260
Bob Wilsona7062312009-08-21 20:54:19 +00005261 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005262 if (M.size() != NumElts && M.size() != NumElts*2)
5263 return false;
5264
James Molloy8c995a92015-09-10 08:42:28 +00005265 // If the mask is twice as long as the input vector then we need to check the
5266 // upper and lower parts of the mask with a matching value for WhichResult
5267 // FIXME: A mask with only even values will be rejected in case the first
5268 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5269 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005270 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005271 if (M.size() == NumElts * 2)
5272 WhichResult = i / NumElts;
5273 else
5274 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005275 for (unsigned j = 0; j < NumElts; j += 2) {
5276 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5277 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5278 return false;
5279 }
Bob Wilsona7062312009-08-21 20:54:19 +00005280 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005281
5282 if (M.size() == NumElts*2)
5283 WhichResult = 0;
5284
Bob Wilsona7062312009-08-21 20:54:19 +00005285 return true;
5286}
5287
Bob Wilson0bbd3072009-12-03 06:40:55 +00005288/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5289/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5290/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005291static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005292 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5293 if (EltSz == 64)
5294 return false;
5295
5296 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005297 if (M.size() != NumElts && M.size() != NumElts*2)
5298 return false;
5299
5300 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005301 if (M.size() == NumElts * 2)
5302 WhichResult = i / NumElts;
5303 else
5304 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005305 for (unsigned j = 0; j < NumElts; j += 2) {
5306 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5307 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5308 return false;
5309 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005310 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005311
5312 if (M.size() == NumElts*2)
5313 WhichResult = 0;
5314
Bob Wilson0bbd3072009-12-03 06:40:55 +00005315 return true;
5316}
5317
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005318// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5319// that the mask elements are either all even and in steps of size 2 or all odd
5320// and in steps of size 2.
5321// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5322// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5323// v2={e,f,g,h}
5324// Requires similar checks to that of isVTRNMask with
5325// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005326static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005327 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5328 if (EltSz == 64)
5329 return false;
5330
Bob Wilsona7062312009-08-21 20:54:19 +00005331 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005332 if (M.size() != NumElts && M.size() != NumElts*2)
5333 return false;
5334
5335 for (unsigned i = 0; i < M.size(); i += NumElts) {
5336 WhichResult = M[i] == 0 ? 0 : 1;
5337 for (unsigned j = 0; j < NumElts; ++j) {
5338 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5339 return false;
5340 }
Bob Wilsona7062312009-08-21 20:54:19 +00005341 }
5342
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005343 if (M.size() == NumElts*2)
5344 WhichResult = 0;
5345
Bob Wilsona7062312009-08-21 20:54:19 +00005346 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005347 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005348 return false;
5349
5350 return true;
5351}
5352
Bob Wilson0bbd3072009-12-03 06:40:55 +00005353/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5354/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5355/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005356static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005357 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5358 if (EltSz == 64)
5359 return false;
5360
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005361 unsigned NumElts = VT.getVectorNumElements();
5362 if (M.size() != NumElts && M.size() != NumElts*2)
5363 return false;
5364
5365 unsigned Half = NumElts / 2;
5366 for (unsigned i = 0; i < M.size(); i += NumElts) {
5367 WhichResult = M[i] == 0 ? 0 : 1;
5368 for (unsigned j = 0; j < NumElts; j += Half) {
5369 unsigned Idx = WhichResult;
5370 for (unsigned k = 0; k < Half; ++k) {
5371 int MIdx = M[i + j + k];
5372 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5373 return false;
5374 Idx += 2;
5375 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005376 }
5377 }
5378
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005379 if (M.size() == NumElts*2)
5380 WhichResult = 0;
5381
Bob Wilson0bbd3072009-12-03 06:40:55 +00005382 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5383 if (VT.is64BitVector() && EltSz == 32)
5384 return false;
5385
5386 return true;
5387}
5388
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005389// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5390// that pairs of elements of the shufflemask represent the same index in each
5391// vector incrementing sequentially through the vectors.
5392// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5393// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5394// v2={e,f,g,h}
5395// Requires similar checks to that of isVTRNMask with respect the how results
5396// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005397static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005398 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5399 if (EltSz == 64)
5400 return false;
5401
Bob Wilsona7062312009-08-21 20:54:19 +00005402 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005403 if (M.size() != NumElts && M.size() != NumElts*2)
5404 return false;
5405
5406 for (unsigned i = 0; i < M.size(); i += NumElts) {
5407 WhichResult = M[i] == 0 ? 0 : 1;
5408 unsigned Idx = WhichResult * NumElts / 2;
5409 for (unsigned j = 0; j < NumElts; j += 2) {
5410 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5411 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5412 return false;
5413 Idx += 1;
5414 }
Bob Wilsona7062312009-08-21 20:54:19 +00005415 }
5416
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005417 if (M.size() == NumElts*2)
5418 WhichResult = 0;
5419
Bob Wilsona7062312009-08-21 20:54:19 +00005420 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005421 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005422 return false;
5423
5424 return true;
5425}
5426
Bob Wilson0bbd3072009-12-03 06:40:55 +00005427/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5428/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5429/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005430static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005431 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5432 if (EltSz == 64)
5433 return false;
5434
5435 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005436 if (M.size() != NumElts && M.size() != NumElts*2)
5437 return false;
5438
5439 for (unsigned i = 0; i < M.size(); i += NumElts) {
5440 WhichResult = M[i] == 0 ? 0 : 1;
5441 unsigned Idx = WhichResult * NumElts / 2;
5442 for (unsigned j = 0; j < NumElts; j += 2) {
5443 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5444 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5445 return false;
5446 Idx += 1;
5447 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005448 }
5449
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005450 if (M.size() == NumElts*2)
5451 WhichResult = 0;
5452
Bob Wilson0bbd3072009-12-03 06:40:55 +00005453 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5454 if (VT.is64BitVector() && EltSz == 32)
5455 return false;
5456
5457 return true;
5458}
5459
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005460/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5461/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5462static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5463 unsigned &WhichResult,
5464 bool &isV_UNDEF) {
5465 isV_UNDEF = false;
5466 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5467 return ARMISD::VTRN;
5468 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5469 return ARMISD::VUZP;
5470 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5471 return ARMISD::VZIP;
5472
5473 isV_UNDEF = true;
5474 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5475 return ARMISD::VTRN;
5476 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5477 return ARMISD::VUZP;
5478 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5479 return ARMISD::VZIP;
5480
5481 return 0;
5482}
5483
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005484/// \return true if this is a reverse operation on an vector.
5485static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5486 unsigned NumElts = VT.getVectorNumElements();
5487 // Make sure the mask has the right size.
5488 if (NumElts != M.size())
5489 return false;
5490
5491 // Look for <15, ..., 3, -1, 1, 0>.
5492 for (unsigned i = 0; i != NumElts; ++i)
5493 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5494 return false;
5495
5496 return true;
5497}
5498
Dale Johannesen2bff5052010-07-29 20:10:08 +00005499// If N is an integer constant that can be moved into a register in one
5500// instruction, return an SDValue of such a constant (will become a MOV
5501// instruction). Otherwise return null.
5502static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005503 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005504 uint64_t Val;
5505 if (!isa<ConstantSDNode>(N))
5506 return SDValue();
5507 Val = cast<ConstantSDNode>(N)->getZExtValue();
5508
5509 if (ST->isThumb1Only()) {
5510 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005511 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005512 } else {
5513 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005514 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005515 }
5516 return SDValue();
5517}
5518
Bob Wilson2e076c42009-06-22 23:27:02 +00005519// If this is a case we can't handle, return null and let the default
5520// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005521SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5522 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005523 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005524 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005525 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005526
5527 APInt SplatBits, SplatUndef;
5528 unsigned SplatBitSize;
5529 bool HasAnyUndefs;
5530 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005531 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005532 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005533 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005534 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005535 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005536 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005537 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005538 if (Val.getNode()) {
5539 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005540 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005541 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005542
5543 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005544 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005545 Val = isNEONModifiedImm(NegatedImm,
5546 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005547 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005548 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005549 if (Val.getNode()) {
5550 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005551 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005552 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005553
5554 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005555 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005556 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005557 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005558 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005559 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5560 }
5561 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005562 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005563 }
5564
Bob Wilson91fdf682010-05-22 00:23:12 +00005565 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005566 //
5567 // As an optimisation, even if more than one value is used it may be more
5568 // profitable to splat with one value then change some lanes.
5569 //
5570 // Heuristically we decide to do this if the vector has a "dominant" value,
5571 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005572 unsigned NumElts = VT.getVectorNumElements();
5573 bool isOnlyLowElement = true;
5574 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005575 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005576 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005577
5578 // Map of the number of times a particular SDValue appears in the
5579 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005580 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005581 SDValue Value;
5582 for (unsigned i = 0; i < NumElts; ++i) {
5583 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005584 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00005585 continue;
5586 if (i > 0)
5587 isOnlyLowElement = false;
5588 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5589 isConstant = false;
5590
James Molloy49bdbce2012-09-06 09:55:02 +00005591 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005592 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005593
James Molloy49bdbce2012-09-06 09:55:02 +00005594 // Is this value dominant? (takes up more than half of the lanes)
5595 if (++Count > (NumElts / 2)) {
5596 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005597 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005598 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005599 }
James Molloy49bdbce2012-09-06 09:55:02 +00005600 if (ValueCounts.size() != 1)
5601 usesOnlyOneValue = false;
5602 if (!Value.getNode() && ValueCounts.size() > 0)
5603 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005604
James Molloy49bdbce2012-09-06 09:55:02 +00005605 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005606 return DAG.getUNDEF(VT);
5607
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005608 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5609 // Keep going if we are hitting this case.
5610 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005611 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5612
Dale Johannesen2bff5052010-07-29 20:10:08 +00005613 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5614
Dale Johannesen710a2d92010-10-19 20:00:17 +00005615 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5616 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005617 if (hasDominantValue && EltSize <= 32) {
5618 if (!isConstant) {
5619 SDValue N;
5620
5621 // If we are VDUPing a value that comes directly from a vector, that will
5622 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005623 // just use VDUPLANE. We can only do this if the lane being extracted
5624 // is at a constant index, as the VDUP from lane instructions only have
5625 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005626 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005627 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005628 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005629 // We need to create a new undef vector to use for the VDUPLANE if the
5630 // size of the vector from which we get the value is different than the
5631 // size of the vector that we need to create. We will insert the element
5632 // such that the register coalescer will remove unnecessary copies.
5633 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005634 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5635 VT.getVectorNumElements();
5636 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5637 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005638 Value, DAG.getConstant(index, dl, MVT::i32)),
5639 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005640 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005641 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005642 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005643 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005644 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5645
5646 if (!usesOnlyOneValue) {
5647 // The dominant value was splatted as 'N', but we now have to insert
5648 // all differing elements.
5649 for (unsigned I = 0; I < NumElts; ++I) {
5650 if (Op.getOperand(I) == Value)
5651 continue;
5652 SmallVector<SDValue, 3> Ops;
5653 Ops.push_back(N);
5654 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005655 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005656 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005657 }
5658 }
5659 return N;
5660 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005661 if (VT.getVectorElementType().isFloatingPoint()) {
5662 SmallVector<SDValue, 8> Ops;
5663 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005664 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005665 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005666 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005667 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005668 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5669 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005670 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005671 }
James Molloy49bdbce2012-09-06 09:55:02 +00005672 if (usesOnlyOneValue) {
5673 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5674 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005675 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005676 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005677 }
5678
5679 // If all elements are constants and the case above didn't get hit, fall back
5680 // to the default expansion, which will generate a load from the constant
5681 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005682 if (isConstant)
5683 return SDValue();
5684
Bob Wilson6f2b8962011-01-07 21:37:30 +00005685 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5686 if (NumElts >= 4) {
5687 SDValue shuffle = ReconstructShuffle(Op, DAG);
5688 if (shuffle != SDValue())
5689 return shuffle;
5690 }
5691
Bob Wilson91fdf682010-05-22 00:23:12 +00005692 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005693 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5694 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005695 if (EltSize >= 32) {
5696 // Do the expansion with floating-point types, since that is what the VFP
5697 // registers are defined to use, and since i64 is not legal.
5698 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5699 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005700 SmallVector<SDValue, 8> Ops;
5701 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005702 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005703 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005704 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005705 }
5706
Jim Grosbach24e102a2013-07-08 18:18:52 +00005707 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5708 // know the default expansion would otherwise fall back on something even
5709 // worse. For a vector with one or two non-undef values, that's
5710 // scalar_to_vector for the elements followed by a shuffle (provided the
5711 // shuffle is valid for the target) and materialization element by element
5712 // on the stack followed by a load for everything else.
5713 if (!isConstant && !usesOnlyOneValue) {
5714 SDValue Vec = DAG.getUNDEF(VT);
5715 for (unsigned i = 0 ; i < NumElts; ++i) {
5716 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005717 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00005718 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005719 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005720 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5721 }
5722 return Vec;
5723 }
5724
Bob Wilson2e076c42009-06-22 23:27:02 +00005725 return SDValue();
5726}
5727
Bob Wilson6f2b8962011-01-07 21:37:30 +00005728// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005729// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005730SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5731 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005732 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005733 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005734 EVT VT = Op.getValueType();
5735 unsigned NumElts = VT.getVectorNumElements();
5736
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005737 struct ShuffleSourceInfo {
5738 SDValue Vec;
5739 unsigned MinElt;
5740 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005741
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005742 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5743 // be compatible with the shuffle we intend to construct. As a result
5744 // ShuffleVec will be some sliding window into the original Vec.
5745 SDValue ShuffleVec;
5746
5747 // Code should guarantee that element i in Vec starts at element "WindowBase
5748 // + i * WindowScale in ShuffleVec".
5749 int WindowBase;
5750 int WindowScale;
5751
5752 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5753 ShuffleSourceInfo(SDValue Vec)
5754 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5755 WindowScale(1) {}
5756 };
5757
5758 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5759 // node.
5760 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005761 for (unsigned i = 0; i < NumElts; ++i) {
5762 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005763 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005764 continue;
5765 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5766 // A shuffle can only come from building a vector from various
5767 // elements of other vectors.
5768 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005769 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5770 // Furthermore, shuffles require a constant mask, whereas extractelts
5771 // accept variable indices.
5772 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005773 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005774
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005775 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005776 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005777 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5778 if (Source == Sources.end())
5779 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005780
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005781 // Update the minimum and maximum lane number seen.
5782 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5783 Source->MinElt = std::min(Source->MinElt, EltNo);
5784 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005785 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005786
Bob Wilson6f2b8962011-01-07 21:37:30 +00005787 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005788 // are involved.
5789 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005790 return SDValue();
5791
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005792 // Find out the smallest element size among result and two sources, and use
5793 // it as element size to build the shuffle_vector.
5794 EVT SmallestEltTy = VT.getVectorElementType();
5795 for (auto &Source : Sources) {
5796 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5797 if (SrcEltTy.bitsLT(SmallestEltTy))
5798 SmallestEltTy = SrcEltTy;
5799 }
5800 unsigned ResMultiplier =
5801 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5802 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5803 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005804
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005805 // If the source vector is too wide or too narrow, we may nevertheless be able
5806 // to construct a compatible shuffle either by concatenating it with UNDEF or
5807 // extracting a suitable range of elements.
5808 for (auto &Src : Sources) {
5809 EVT SrcVT = Src.ShuffleVec.getValueType();
5810
5811 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005812 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005813
5814 // This stage of the search produces a source with the same element type as
5815 // the original, but with a total width matching the BUILD_VECTOR output.
5816 EVT EltVT = SrcVT.getVectorElementType();
5817 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5818 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5819
5820 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5821 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5822 return SDValue();
5823 // We can pad out the smaller vector for free, so if it's part of a
5824 // shuffle...
5825 Src.ShuffleVec =
5826 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5827 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5828 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005829 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005830
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005831 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5832 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005833
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005834 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005835 // Span too large for a VEXT to cope
5836 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005837 }
5838
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005839 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005840 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005841 Src.ShuffleVec =
5842 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5843 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5844 Src.WindowBase = -NumSrcElts;
5845 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005846 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005847 Src.ShuffleVec =
5848 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5849 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005850 } else {
5851 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005852 SDValue VEXTSrc1 =
5853 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5854 DAG.getConstant(0, dl, MVT::i32));
5855 SDValue VEXTSrc2 =
5856 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5857 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005858
5859 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5860 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005861 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005862 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005863 }
5864 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005865
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005866 // Another possible incompatibility occurs from the vector element types. We
5867 // can fix this by bitcasting the source vectors to the same type we intend
5868 // for the shuffle.
5869 for (auto &Src : Sources) {
5870 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5871 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005872 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005873 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5874 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5875 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5876 Src.WindowBase *= Src.WindowScale;
5877 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005878
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005879 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005880 DEBUG(
5881 for (auto Src : Sources)
5882 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5883 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005884
5885 // The stars all align, our next step is to produce the mask for the shuffle.
5886 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5887 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5888 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5889 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005890 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005891 continue;
5892
5893 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5894 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5895
5896 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5897 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5898 // segment.
5899 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5900 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5901 VT.getVectorElementType().getSizeInBits());
5902 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5903
5904 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5905 // starting at the appropriate offset.
5906 int *LaneMask = &Mask[i * ResMultiplier];
5907
5908 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5909 ExtractBase += NumElts * (Src - Sources.begin());
5910 for (int j = 0; j < LanesDefined; ++j)
5911 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005912 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005913
Bob Wilson6f2b8962011-01-07 21:37:30 +00005914 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005915 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5916 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005917
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005918 // We can't handle more than two sources. This should have already
5919 // been checked before this point.
5920 assert(Sources.size() <= 2 && "Too many sources!");
5921
5922 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5923 for (unsigned i = 0; i < Sources.size(); ++i)
5924 ShuffleOps[i] = Sources[i].ShuffleVec;
5925
5926 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5927 ShuffleOps[1], &Mask[0]);
5928 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005929}
5930
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005931/// isShuffleMaskLegal - Targets can use this to indicate that they only
5932/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5933/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5934/// are assumed to be legal.
5935bool
5936ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5937 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005938 if (VT.getVectorNumElements() == 4 &&
5939 (VT.is128BitVector() || VT.is64BitVector())) {
5940 unsigned PFIndexes[4];
5941 for (unsigned i = 0; i != 4; ++i) {
5942 if (M[i] < 0)
5943 PFIndexes[i] = 8;
5944 else
5945 PFIndexes[i] = M[i];
5946 }
5947
5948 // Compute the index in the perfect shuffle table.
5949 unsigned PFTableIndex =
5950 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5951 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5952 unsigned Cost = (PFEntry >> 30);
5953
5954 if (Cost <= 4)
5955 return true;
5956 }
5957
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005958 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005959 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005960
Bob Wilson846bd792010-06-07 23:53:38 +00005961 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5962 return (EltSize >= 32 ||
5963 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005964 isVREVMask(M, VT, 64) ||
5965 isVREVMask(M, VT, 32) ||
5966 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005967 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005968 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005969 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005970 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005971}
5972
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005973/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5974/// the specified operations to build the shuffle.
5975static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5976 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005977 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005978 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5979 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5980 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5981
5982 enum {
5983 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5984 OP_VREV,
5985 OP_VDUP0,
5986 OP_VDUP1,
5987 OP_VDUP2,
5988 OP_VDUP3,
5989 OP_VEXT1,
5990 OP_VEXT2,
5991 OP_VEXT3,
5992 OP_VUZPL, // VUZP, left result
5993 OP_VUZPR, // VUZP, right result
5994 OP_VZIPL, // VZIP, left result
5995 OP_VZIPR, // VZIP, right result
5996 OP_VTRNL, // VTRN, left result
5997 OP_VTRNR // VTRN, right result
5998 };
5999
6000 if (OpNum == OP_COPY) {
6001 if (LHSID == (1*9+2)*9+3) return LHS;
6002 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6003 return RHS;
6004 }
6005
6006 SDValue OpLHS, OpRHS;
6007 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6008 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6009 EVT VT = OpLHS.getValueType();
6010
6011 switch (OpNum) {
6012 default: llvm_unreachable("Unknown shuffle opcode!");
6013 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00006014 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00006015 if (VT.getVectorElementType() == MVT::i32 ||
6016 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00006017 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6018 // vrev <4 x i16> -> VREV32
6019 if (VT.getVectorElementType() == MVT::i16)
6020 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6021 // vrev <4 x i8> -> VREV16
6022 assert(VT.getVectorElementType() == MVT::i8);
6023 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006024 case OP_VDUP0:
6025 case OP_VDUP1:
6026 case OP_VDUP2:
6027 case OP_VDUP3:
6028 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006029 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006030 case OP_VEXT1:
6031 case OP_VEXT2:
6032 case OP_VEXT3:
6033 return DAG.getNode(ARMISD::VEXT, dl, VT,
6034 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006035 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006036 case OP_VUZPL:
6037 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006038 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006039 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6040 case OP_VZIPL:
6041 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006042 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006043 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6044 case OP_VTRNL:
6045 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006046 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6047 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006048 }
6049}
6050
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006051static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006052 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006053 SelectionDAG &DAG) {
6054 // Check to see if we can use the VTBL instruction.
6055 SDValue V1 = Op.getOperand(0);
6056 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006057 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006058
6059 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006060 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006061 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006062 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006063
Sanjay Patel57195842016-03-14 17:28:46 +00006064 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006065 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00006066 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006067
Owen Anderson77aa2662011-04-05 21:48:57 +00006068 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00006069 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006070}
6071
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006072static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6073 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006075 SDValue OpLHS = Op.getOperand(0);
6076 EVT VT = OpLHS.getValueType();
6077
6078 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6079 "Expect an v8i16/v16i8 type");
6080 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6081 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6082 // extract the first 8 bytes into the top double word and the last 8 bytes
6083 // into the bottom double word. The v8i16 case is similar.
6084 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6085 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006086 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006087}
6088
Bob Wilson2e076c42009-06-22 23:27:02 +00006089static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006090 SDValue V1 = Op.getOperand(0);
6091 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006092 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006093 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006094 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006095
Bob Wilsonc6800b52009-08-13 02:13:04 +00006096 // Convert shuffles that are directly supported on NEON to target-specific
6097 // DAG nodes, instead of keeping them as shuffles and matching them again
6098 // during code selection. This is more efficient and avoids the possibility
6099 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006100 // FIXME: floating-point vectors should be canonicalized to integer vectors
6101 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006102 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006103
Bob Wilson846bd792010-06-07 23:53:38 +00006104 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6105 if (EltSize <= 32) {
6106 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
6107 int Lane = SVN->getSplatIndex();
6108 // If this is undef splat, generate it via "just" vdup, if possible.
6109 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006110
Dan Gohman198b7ff2011-11-03 21:49:52 +00006111 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006112 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6113 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6114 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006115 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6116 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6117 // reaches it).
6118 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6119 !isa<ConstantSDNode>(V1.getOperand(0))) {
6120 bool IsScalarToVector = true;
6121 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006122 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006123 IsScalarToVector = false;
6124 break;
6125 }
6126 if (IsScalarToVector)
6127 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6128 }
Bob Wilson846bd792010-06-07 23:53:38 +00006129 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006130 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006131 }
Bob Wilson846bd792010-06-07 23:53:38 +00006132
6133 bool ReverseVEXT;
6134 unsigned Imm;
6135 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6136 if (ReverseVEXT)
6137 std::swap(V1, V2);
6138 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006139 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006140 }
6141
6142 if (isVREVMask(ShuffleMask, VT, 64))
6143 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6144 if (isVREVMask(ShuffleMask, VT, 32))
6145 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6146 if (isVREVMask(ShuffleMask, VT, 16))
6147 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6148
Sanjay Patel57195842016-03-14 17:28:46 +00006149 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006150 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006151 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006152 }
6153
Bob Wilson846bd792010-06-07 23:53:38 +00006154 // Check for Neon shuffles that modify both input vectors in place.
6155 // If both results are used, i.e., if there are two shuffles with the same
6156 // source operands and with masks corresponding to both results of one of
6157 // these operations, DAG memoization will ensure that a single node is
6158 // used for both shuffles.
6159 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006160 bool isV_UNDEF;
6161 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6162 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6163 if (isV_UNDEF)
6164 V2 = V1;
6165 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6166 .getValue(WhichResult);
6167 }
Bob Wilson846bd792010-06-07 23:53:38 +00006168
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006169 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6170 // shuffles that produce a result larger than their operands with:
6171 // shuffle(concat(v1, undef), concat(v2, undef))
6172 // ->
6173 // shuffle(concat(v1, v2), undef)
6174 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6175 //
6176 // This is useful in the general case, but there are special cases where
6177 // native shuffles produce larger results: the two-result ops.
6178 //
6179 // Look through the concat when lowering them:
6180 // shuffle(concat(v1, v2), undef)
6181 // ->
6182 // concat(VZIP(v1, v2):0, :1)
6183 //
Sanjay Patel57195842016-03-14 17:28:46 +00006184 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006185 SDValue SubV1 = V1->getOperand(0);
6186 SDValue SubV2 = V1->getOperand(1);
6187 EVT SubVT = SubV1.getValueType();
6188
6189 // We expect these to have been canonicalized to -1.
6190 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
6191 return i < (int)VT.getVectorNumElements();
6192 }) && "Unexpected shuffle index into UNDEF operand!");
6193
6194 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6195 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6196 if (isV_UNDEF)
6197 SubV2 = SubV1;
6198 assert((WhichResult == 0) &&
6199 "In-place shuffle of concat can only have one result!");
6200 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6201 SubV1, SubV2);
6202 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6203 Res.getValue(1));
6204 }
6205 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006206 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006207
Bob Wilsona7062312009-08-21 20:54:19 +00006208 // If the shuffle is not directly supported and it has 4 elements, use
6209 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006210 unsigned NumElts = VT.getVectorNumElements();
6211 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006212 unsigned PFIndexes[4];
6213 for (unsigned i = 0; i != 4; ++i) {
6214 if (ShuffleMask[i] < 0)
6215 PFIndexes[i] = 8;
6216 else
6217 PFIndexes[i] = ShuffleMask[i];
6218 }
6219
6220 // Compute the index in the perfect shuffle table.
6221 unsigned PFTableIndex =
6222 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006223 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6224 unsigned Cost = (PFEntry >> 30);
6225
6226 if (Cost <= 4)
6227 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6228 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006229
Bob Wilsond8a9a042010-06-04 00:04:02 +00006230 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006231 if (EltSize >= 32) {
6232 // Do the expansion with floating-point types, since that is what the VFP
6233 // registers are defined to use, and since i64 is not legal.
6234 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6235 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006236 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6237 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006238 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006239 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006240 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006241 Ops.push_back(DAG.getUNDEF(EltVT));
6242 else
6243 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6244 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6245 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006246 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006247 }
Craig Topper48d114b2014-04-26 18:35:24 +00006248 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006249 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006250 }
6251
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006252 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6253 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6254
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006255 if (VT == MVT::v8i8)
6256 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006257 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006258
Bob Wilson6f34e272009-08-14 05:16:33 +00006259 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006260}
6261
Eli Friedmana5e244c2011-10-24 23:08:52 +00006262static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6263 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6264 SDValue Lane = Op.getOperand(2);
6265 if (!isa<ConstantSDNode>(Lane))
6266 return SDValue();
6267
6268 return Op;
6269}
6270
Bob Wilson2e076c42009-06-22 23:27:02 +00006271static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006272 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006273 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006274 if (!isa<ConstantSDNode>(Lane))
6275 return SDValue();
6276
6277 SDValue Vec = Op.getOperand(0);
6278 if (Op.getValueType() == MVT::i32 &&
6279 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006280 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006281 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6282 }
6283
6284 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006285}
6286
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006287static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6288 // The only time a CONCAT_VECTORS operation can have legal types is when
6289 // two 64-bit vectors are concatenated to a 128-bit vector.
6290 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6291 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006292 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006293 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006294 SDValue Op0 = Op.getOperand(0);
6295 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006296 if (!Op0.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006297 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006298 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006299 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006300 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006301 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006302 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006303 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006304 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006305}
6306
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006307/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6308/// element has been zero/sign-extended, depending on the isSigned parameter,
6309/// from an integer type half its size.
6310static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6311 bool isSigned) {
6312 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6313 EVT VT = N->getValueType(0);
6314 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6315 SDNode *BVN = N->getOperand(0).getNode();
6316 if (BVN->getValueType(0) != MVT::v4i32 ||
6317 BVN->getOpcode() != ISD::BUILD_VECTOR)
6318 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006319 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006320 unsigned HiElt = 1 - LoElt;
6321 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6322 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6323 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6324 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6325 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6326 return false;
6327 if (isSigned) {
6328 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6329 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6330 return true;
6331 } else {
6332 if (Hi0->isNullValue() && Hi1->isNullValue())
6333 return true;
6334 }
6335 return false;
6336 }
6337
6338 if (N->getOpcode() != ISD::BUILD_VECTOR)
6339 return false;
6340
6341 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6342 SDNode *Elt = N->getOperand(i).getNode();
6343 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6344 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6345 unsigned HalfSize = EltSize / 2;
6346 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006347 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006348 return false;
6349 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006350 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006351 return false;
6352 }
6353 continue;
6354 }
6355 return false;
6356 }
6357
6358 return true;
6359}
6360
6361/// isSignExtended - Check if a node is a vector value that is sign-extended
6362/// or a constant BUILD_VECTOR with sign-extended elements.
6363static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6364 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6365 return true;
6366 if (isExtendedBUILD_VECTOR(N, DAG, true))
6367 return true;
6368 return false;
6369}
6370
6371/// isZeroExtended - Check if a node is a vector value that is zero-extended
6372/// or a constant BUILD_VECTOR with zero-extended elements.
6373static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6374 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6375 return true;
6376 if (isExtendedBUILD_VECTOR(N, DAG, false))
6377 return true;
6378 return false;
6379}
6380
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006381static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6382 if (OrigVT.getSizeInBits() >= 64)
6383 return OrigVT;
6384
6385 assert(OrigVT.isSimple() && "Expecting a simple value type");
6386
6387 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6388 switch (OrigSimpleTy) {
6389 default: llvm_unreachable("Unexpected Vector Type");
6390 case MVT::v2i8:
6391 case MVT::v2i16:
6392 return MVT::v2i32;
6393 case MVT::v4i8:
6394 return MVT::v4i16;
6395 }
6396}
6397
Sebastian Popa204f722012-11-30 19:08:04 +00006398/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6399/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6400/// We insert the required extension here to get the vector to fill a D register.
6401static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6402 const EVT &OrigTy,
6403 const EVT &ExtTy,
6404 unsigned ExtOpcode) {
6405 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6406 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6407 // 64-bits we need to insert a new extension so that it will be 64-bits.
6408 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6409 if (OrigTy.getSizeInBits() >= 64)
6410 return N;
6411
6412 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006413 EVT NewVT = getExtensionTo64Bits(OrigTy);
6414
Andrew Trickef9de2a2013-05-25 02:42:55 +00006415 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006416}
6417
6418/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6419/// does not do any sign/zero extension. If the original vector is less
6420/// than 64 bits, an appropriate extension will be added after the load to
6421/// reach a total size of 64 bits. We have to add the extension separately
6422/// because ARM does not have a sign/zero extending load for vectors.
6423static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006424 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6425
6426 // The load already has the right type.
6427 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006428 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006429 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6430 LD->isNonTemporal(), LD->isInvariant(),
6431 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006432
6433 // We need to create a zextload/sextload. We cannot just create a load
6434 // followed by a zext/zext node because LowerMUL is also run during normal
6435 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006436 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006437 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006438 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006439 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006440}
6441
6442/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6443/// extending load, or BUILD_VECTOR with extended elements, return the
6444/// unextended value. The unextended vector should be 64 bits so that it can
6445/// be used as an operand to a VMULL instruction. If the original vector size
6446/// before extension is less than 64 bits we add a an extension to resize
6447/// the vector to 64 bits.
6448static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006449 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006450 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6451 N->getOperand(0)->getValueType(0),
6452 N->getValueType(0),
6453 N->getOpcode());
6454
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006455 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006456 return SkipLoadExtensionForVMULL(LD, DAG);
6457
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006458 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6459 // have been legalized as a BITCAST from v4i32.
6460 if (N->getOpcode() == ISD::BITCAST) {
6461 SDNode *BVN = N->getOperand(0).getNode();
6462 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6463 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006464 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006465 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006466 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6467 }
6468 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6469 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6470 EVT VT = N->getValueType(0);
6471 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6472 unsigned NumElts = VT.getVectorNumElements();
6473 MVT TruncVT = MVT::getIntegerVT(EltSize);
6474 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006475 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006476 for (unsigned i = 0; i != NumElts; ++i) {
6477 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6478 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006479 // Element types smaller than 32 bits are not legal, so use i32 elements.
6480 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006481 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006482 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006483 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006484 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006485}
6486
Evan Chenge2086e72011-03-29 01:56:09 +00006487static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6488 unsigned Opcode = N->getOpcode();
6489 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6490 SDNode *N0 = N->getOperand(0).getNode();
6491 SDNode *N1 = N->getOperand(1).getNode();
6492 return N0->hasOneUse() && N1->hasOneUse() &&
6493 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6494 }
6495 return false;
6496}
6497
6498static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6499 unsigned Opcode = N->getOpcode();
6500 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6501 SDNode *N0 = N->getOperand(0).getNode();
6502 SDNode *N1 = N->getOperand(1).getNode();
6503 return N0->hasOneUse() && N1->hasOneUse() &&
6504 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6505 }
6506 return false;
6507}
6508
Bob Wilson38ab35a2010-09-01 23:50:19 +00006509static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6510 // Multiplications are only custom-lowered for 128-bit vectors so that
6511 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6512 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006513 assert(VT.is128BitVector() && VT.isInteger() &&
6514 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006515 SDNode *N0 = Op.getOperand(0).getNode();
6516 SDNode *N1 = Op.getOperand(1).getNode();
6517 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006518 bool isMLA = false;
6519 bool isN0SExt = isSignExtended(N0, DAG);
6520 bool isN1SExt = isSignExtended(N1, DAG);
6521 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006522 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006523 else {
6524 bool isN0ZExt = isZeroExtended(N0, DAG);
6525 bool isN1ZExt = isZeroExtended(N1, DAG);
6526 if (isN0ZExt && isN1ZExt)
6527 NewOpc = ARMISD::VMULLu;
6528 else if (isN1SExt || isN1ZExt) {
6529 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6530 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6531 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6532 NewOpc = ARMISD::VMULLs;
6533 isMLA = true;
6534 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6535 NewOpc = ARMISD::VMULLu;
6536 isMLA = true;
6537 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6538 std::swap(N0, N1);
6539 NewOpc = ARMISD::VMULLu;
6540 isMLA = true;
6541 }
6542 }
6543
6544 if (!NewOpc) {
6545 if (VT == MVT::v2i64)
6546 // Fall through to expand this. It is not legal.
6547 return SDValue();
6548 else
6549 // Other vector multiplications are legal.
6550 return Op;
6551 }
6552 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006553
6554 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006555 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006556 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006557 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006558 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006559 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006560 assert(Op0.getValueType().is64BitVector() &&
6561 Op1.getValueType().is64BitVector() &&
6562 "unexpected types for extended operands to VMULL");
6563 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6564 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006565
Evan Chenge2086e72011-03-29 01:56:09 +00006566 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6567 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6568 // vmull q0, d4, d6
6569 // vmlal q0, d5, d6
6570 // is faster than
6571 // vaddl q0, d4, d5
6572 // vmovl q1, d6
6573 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006574 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6575 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006576 EVT Op1VT = Op1.getValueType();
6577 return DAG.getNode(N0->getOpcode(), DL, VT,
6578 DAG.getNode(NewOpc, DL, VT,
6579 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6580 DAG.getNode(NewOpc, DL, VT,
6581 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006582}
6583
Owen Anderson77aa2662011-04-05 21:48:57 +00006584static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006585LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006586 // TODO: Should this propagate fast-math-flags?
6587
Nate Begemanfa62d502011-02-11 20:53:29 +00006588 // Convert to float
6589 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6590 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6591 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6592 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6593 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6594 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6595 // Get reciprocal estimate.
6596 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006597 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006598 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6599 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006600 // Because char has a smaller range than uchar, we can actually get away
6601 // without any newton steps. This requires that we use a weird bias
6602 // of 0xb000, however (again, this has been exhaustively tested).
6603 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6604 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6605 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006606 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006607 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6608 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6609 // Convert back to short.
6610 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6611 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6612 return X;
6613}
6614
Owen Anderson77aa2662011-04-05 21:48:57 +00006615static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006616LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006617 // TODO: Should this propagate fast-math-flags?
6618
Nate Begemanfa62d502011-02-11 20:53:29 +00006619 SDValue N2;
6620 // Convert to float.
6621 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6622 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6623 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6624 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6625 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6626 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006627
Nate Begemanfa62d502011-02-11 20:53:29 +00006628 // Use reciprocal estimate and one refinement step.
6629 // float4 recip = vrecpeq_f32(yf);
6630 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006631 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006632 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6633 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006634 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006635 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006636 N1, N2);
6637 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6638 // Because short has a smaller range than ushort, we can actually get away
6639 // with only a single newton step. This requires that we use a weird bias
6640 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006641 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006642 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6643 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006644 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006645 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6646 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6647 // Convert back to integer and return.
6648 // return vmovn_s32(vcvt_s32_f32(result));
6649 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6650 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6651 return N0;
6652}
6653
6654static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6655 EVT VT = Op.getValueType();
6656 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6657 "unexpected type for custom-lowering ISD::SDIV");
6658
Andrew Trickef9de2a2013-05-25 02:42:55 +00006659 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006660 SDValue N0 = Op.getOperand(0);
6661 SDValue N1 = Op.getOperand(1);
6662 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006663
Nate Begemanfa62d502011-02-11 20:53:29 +00006664 if (VT == MVT::v8i8) {
6665 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6666 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006667
Nate Begemanfa62d502011-02-11 20:53:29 +00006668 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006669 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006670 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006671 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006672 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006673 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006674 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006675 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006676
6677 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6678 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6679
6680 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6681 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006682
Nate Begemanfa62d502011-02-11 20:53:29 +00006683 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6684 return N0;
6685 }
6686 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6687}
6688
6689static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006690 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006691 EVT VT = Op.getValueType();
6692 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6693 "unexpected type for custom-lowering ISD::UDIV");
6694
Andrew Trickef9de2a2013-05-25 02:42:55 +00006695 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006696 SDValue N0 = Op.getOperand(0);
6697 SDValue N1 = Op.getOperand(1);
6698 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006699
Nate Begemanfa62d502011-02-11 20:53:29 +00006700 if (VT == MVT::v8i8) {
6701 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6702 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006703
Nate Begemanfa62d502011-02-11 20:53:29 +00006704 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006705 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006706 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006707 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006708 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006709 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006710 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006711 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006712
Nate Begemanfa62d502011-02-11 20:53:29 +00006713 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6714 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006715
Nate Begemanfa62d502011-02-11 20:53:29 +00006716 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6717 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006718
6719 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006720 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6721 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006722 N0);
6723 return N0;
6724 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006725
Nate Begemanfa62d502011-02-11 20:53:29 +00006726 // v4i16 sdiv ... Convert to float.
6727 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6728 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6729 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6730 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6731 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006732 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006733
6734 // Use reciprocal estimate and two refinement steps.
6735 // float4 recip = vrecpeq_f32(yf);
6736 // recip *= vrecpsq_f32(yf, recip);
6737 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006738 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006739 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6740 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006741 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006742 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006743 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006744 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006745 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006746 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006747 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006748 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6749 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6750 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6751 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006752 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006753 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6754 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006755 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006756 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6757 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6758 // Convert back to integer and return.
6759 // return vmovn_u32(vcvt_s32_f32(result));
6760 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6761 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6762 return N0;
6763}
6764
Evan Chenge8916542011-08-30 01:34:54 +00006765static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6766 EVT VT = Op.getNode()->getValueType(0);
6767 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6768
6769 unsigned Opc;
6770 bool ExtraOp = false;
6771 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006772 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006773 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6774 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6775 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6776 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6777 }
6778
6779 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006780 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006781 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006782 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006783 Op.getOperand(1), Op.getOperand(2));
6784}
6785
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006786SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6787 assert(Subtarget->isTargetDarwin());
6788
6789 // For iOS, we want to call an alternative entry point: __sincos_stret,
6790 // return values are passed via sret.
6791 SDLoc dl(Op);
6792 SDValue Arg = Op.getOperand(0);
6793 EVT ArgVT = Arg.getValueType();
6794 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006795 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006796
6797 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006798 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006799
6800 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006801 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006802 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006803
6804 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006805 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6806 SDValue SRet;
6807 if (ShouldUseSRet) {
6808 // Create stack object for sret.
6809 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6810 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6811 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6812 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6813
6814 ArgListEntry Entry;
6815 Entry.Node = SRet;
6816 Entry.Ty = RetTy->getPointerTo();
6817 Entry.isSExt = false;
6818 Entry.isZExt = false;
6819 Entry.isSRet = true;
6820 Args.push_back(Entry);
6821 RetTy = Type::getVoidTy(*DAG.getContext());
6822 }
6823
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006824 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006825 Entry.Node = Arg;
6826 Entry.Ty = ArgTy;
6827 Entry.isSExt = false;
6828 Entry.isZExt = false;
6829 Args.push_back(Entry);
6830
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006831 const char *LibcallName =
6832 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006833 RTLIB::Libcall LC =
6834 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6835 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006836 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006837
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006838 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006839 CLI.setDebugLoc(dl)
6840 .setChain(DAG.getEntryNode())
6841 .setCallee(CC, RetTy, Callee, std::move(Args), 0)
6842 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006843 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6844
Tim Northover8b403662015-10-28 22:51:16 +00006845 if (!ShouldUseSRet)
6846 return CallResult.first;
6847
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006848 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6849 MachinePointerInfo(), false, false, false, 0);
6850
6851 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006852 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006853 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006854 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6855 MachinePointerInfo(), false, false, false, 0);
6856
6857 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6858 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6859 LoadSin.getValue(0), LoadCos.getValue(0));
6860}
6861
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006862SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006863 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006864 SDValue &Chain) const {
6865 EVT VT = Op.getValueType();
6866 assert((VT == MVT::i32 || VT == MVT::i64) &&
6867 "unexpected type for custom lowering DIV");
6868 SDLoc dl(Op);
6869
6870 const auto &DL = DAG.getDataLayout();
6871 const auto &TLI = DAG.getTargetLoweringInfo();
6872
6873 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006874 if (Signed)
6875 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6876 else
6877 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006878
6879 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6880
6881 ARMTargetLowering::ArgListTy Args;
6882
6883 for (auto AI : {1, 0}) {
6884 ArgListEntry Arg;
6885 Arg.Node = Op.getOperand(AI);
6886 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6887 Args.push_back(Arg);
6888 }
6889
6890 CallLoweringInfo CLI(DAG);
6891 CLI.setDebugLoc(dl)
6892 .setChain(Chain)
6893 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6894 ES, std::move(Args), 0);
6895
6896 return LowerCallTo(CLI).first;
6897}
6898
Martell Maloned1229242015-11-26 15:34:03 +00006899SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6900 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006901 assert(Op.getValueType() == MVT::i32 &&
6902 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006903 SDLoc dl(Op);
6904
6905 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6906 DAG.getEntryNode(), Op.getOperand(1));
6907
Martell Maloned1229242015-11-26 15:34:03 +00006908 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006909}
6910
6911void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00006912 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006913 SmallVectorImpl<SDValue> &Results) const {
6914 const auto &DL = DAG.getDataLayout();
6915 const auto &TLI = DAG.getTargetLoweringInfo();
6916
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006917 assert(Op.getValueType() == MVT::i64 &&
6918 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006919 SDLoc dl(Op);
6920
6921 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6922 DAG.getConstant(0, dl, MVT::i32));
6923 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6924 DAG.getConstant(1, dl, MVT::i32));
6925 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6926
6927 SDValue DBZCHK =
6928 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6929
Martell Maloned1229242015-11-26 15:34:03 +00006930 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006931
6932 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6933 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6934 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6935 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6936
6937 Results.push_back(Lower);
6938 Results.push_back(Upper);
6939}
6940
Eli Friedman10f9ce22011-09-15 22:26:18 +00006941static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00006942 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
6943 // Acquire/Release load/store is not legal for targets without a dmb or
6944 // equivalent available.
6945 return SDValue();
Eli Friedmanba912e02011-09-15 22:18:49 +00006946
JF Bastien800f87a2016-04-06 21:19:33 +00006947 // Monotonic load/store is legal for all targets.
6948 return Op;
Eli Friedmanba912e02011-09-15 22:18:49 +00006949}
6950
Tim Northoverbc933082013-05-23 19:11:20 +00006951static void ReplaceREADCYCLECOUNTER(SDNode *N,
6952 SmallVectorImpl<SDValue> &Results,
6953 SelectionDAG &DAG,
6954 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006955 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006956 // Under Power Management extensions, the cycle-count is:
6957 // mrc p15, #0, <Rt>, c9, c13, #0
6958 SDValue Ops[] = { N->getOperand(0), // Chain
6959 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6960 DAG.getConstant(15, DL, MVT::i32),
6961 DAG.getConstant(0, DL, MVT::i32),
6962 DAG.getConstant(9, DL, MVT::i32),
6963 DAG.getConstant(13, DL, MVT::i32),
6964 DAG.getConstant(0, DL, MVT::i32)
6965 };
Tim Northoverbc933082013-05-23 19:11:20 +00006966
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006967 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6968 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6969 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6970 DAG.getConstant(0, DL, MVT::i32)));
6971 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006972}
6973
Tim Northoverb629c772016-04-18 21:48:55 +00006974static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V0, SDValue V1) {
6975 SDLoc dl(V0.getNode());
6976 SDValue RegClass =
6977 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
6978 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
6979 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
6980 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
6981 return SDValue(
6982 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
6983}
6984
6985static void ReplaceCMP_SWAP_64Results(SDNode *N,
6986 SmallVectorImpl<SDValue> & Results,
6987 SelectionDAG &DAG) {
6988 assert(N->getValueType(0) == MVT::i64 &&
6989 "AtomicCmpSwap on types less than 64 should be legal");
6990 SDValue Ops[] = {N->getOperand(1),
6991 createGPRPairNode(DAG, N->getOperand(2)->getOperand(0),
6992 N->getOperand(2)->getOperand(1)),
6993 createGPRPairNode(DAG, N->getOperand(3)->getOperand(0),
6994 N->getOperand(3)->getOperand(1)),
6995 N->getOperand(0)};
6996 SDNode *CmpSwap = DAG.getMachineNode(
6997 ARM::CMP_SWAP_64, SDLoc(N),
6998 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
6999
7000 MachineFunction &MF = DAG.getMachineFunction();
7001 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
7002 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
7003 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
7004
7005 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_0, SDLoc(N), MVT::i32,
7006 SDValue(CmpSwap, 0)));
7007 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_1, SDLoc(N), MVT::i32,
7008 SDValue(CmpSwap, 0)));
7009 Results.push_back(SDValue(CmpSwap, 2));
7010}
7011
Dan Gohman21cea8a2010-04-17 15:26:15 +00007012SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007013 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007014 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007015 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007016 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00007017 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00007018 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00007019 switch (Subtarget->getTargetTriple().getObjectFormat()) {
7020 default: llvm_unreachable("unknown object format");
7021 case Triple::COFF:
7022 return LowerGlobalAddressWindows(Op, DAG);
7023 case Triple::ELF:
7024 return LowerGlobalAddressELF(Op, DAG);
7025 case Triple::MachO:
7026 return LowerGlobalAddressDarwin(Op, DAG);
7027 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00007028 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00007029 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007030 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
7031 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007032 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00007033 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00007034 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00007035 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00007036 case ISD::SINT_TO_FP:
7037 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
7038 case ISD::FP_TO_SINT:
7039 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007040 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00007041 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00007042 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00007043 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00007044 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00007045 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00007046 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
7047 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00007048 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007049 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00007050 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00007051 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00007052 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
7053 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007054 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00007055 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007056 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007057 case ISD::CTTZ:
7058 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007059 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007060 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00007061 case ISD::SETCCE: return LowerSETCCE(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007062 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007063 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007064 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007065 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007066 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007067 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007068 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007069 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00007070 case ISD::SDIV:
7071 if (Subtarget->isTargetWindows())
7072 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
7073 return LowerSDIV(Op, DAG);
7074 case ISD::UDIV:
7075 if (Subtarget->isTargetWindows())
7076 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
7077 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007078 case ISD::ADDC:
7079 case ISD::ADDE:
7080 case ISD::SUBC:
7081 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007082 case ISD::SADDO:
7083 case ISD::UADDO:
7084 case ISD::SSUBO:
7085 case ISD::USUBO:
7086 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007087 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007088 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007089 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007090 case ISD::SDIVREM:
7091 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007092 case ISD::DYNAMIC_STACKALLOC:
7093 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7094 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7095 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007096 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7097 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007098 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007099 }
Evan Cheng10043e22007-01-19 07:51:42 +00007100}
7101
Duncan Sands6ed40142008-12-01 11:39:25 +00007102/// ReplaceNodeResults - Replace the results of node with an illegal result
7103/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007104void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007105 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007106 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007107 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007108 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007109 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007110 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007111 case ISD::READ_REGISTER:
7112 ExpandREAD_REGISTER(N, Results, DAG);
7113 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007114 case ISD::BITCAST:
7115 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007116 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007117 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007118 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007119 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007120 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007121 case ISD::SREM:
7122 case ISD::UREM:
7123 Res = LowerREM(N, DAG);
7124 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007125 case ISD::SDIVREM:
7126 case ISD::UDIVREM:
7127 Res = LowerDivRem(SDValue(N, 0), DAG);
7128 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7129 Results.push_back(Res.getValue(0));
7130 Results.push_back(Res.getValue(1));
7131 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007132 case ISD::READCYCLECOUNTER:
7133 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7134 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007135 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007136 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007137 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007138 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7139 Results);
Tim Northoverb629c772016-04-18 21:48:55 +00007140 case ISD::ATOMIC_CMP_SWAP:
7141 ReplaceCMP_SWAP_64Results(N, Results, DAG);
7142 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00007143 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007144 if (Res.getNode())
7145 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007146}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007147
Evan Cheng10043e22007-01-19 07:51:42 +00007148//===----------------------------------------------------------------------===//
7149// ARM Scheduler Hooks
7150//===----------------------------------------------------------------------===//
7151
Bill Wendling030b58e2011-10-06 22:18:16 +00007152/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7153/// registers the function context.
7154void ARMTargetLowering::
7155SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
7156 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007157 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00007158 DebugLoc dl = MI->getDebugLoc();
7159 MachineFunction *MF = MBB->getParent();
7160 MachineRegisterInfo *MRI = &MF->getRegInfo();
7161 MachineConstantPool *MCP = MF->getConstantPool();
7162 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7163 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007164
Bill Wendling374ee192011-10-03 21:25:38 +00007165 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007166 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007167
Bill Wendling374ee192011-10-03 21:25:38 +00007168 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007169 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007170 ARMConstantPoolValue *CPV =
7171 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7172 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7173
Craig Topper61e88f42014-11-21 05:58:21 +00007174 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7175 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007176
Bill Wendling030b58e2011-10-06 22:18:16 +00007177 // Grab constant pool and fixed stack memory operands.
7178 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007179 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7180 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007181
7182 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007183 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7184 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007185
7186 // Load the address of the dispatch MBB into the jump buffer.
7187 if (isThumb2) {
7188 // Incoming value: jbuf
7189 // ldr.n r5, LCPI1_1
7190 // orr r5, r5, #1
7191 // add r5, pc
7192 // str r5, [$jbuf, #+4] ; &jbuf[1]
7193 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7194 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7195 .addConstantPoolIndex(CPI)
7196 .addMemOperand(CPMMO));
7197 // Set the low bit because of thumb mode.
7198 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7199 AddDefaultCC(
7200 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7201 .addReg(NewVReg1, RegState::Kill)
7202 .addImm(0x01)));
7203 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7204 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7205 .addReg(NewVReg2, RegState::Kill)
7206 .addImm(PCLabelId);
7207 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7208 .addReg(NewVReg3, RegState::Kill)
7209 .addFrameIndex(FI)
7210 .addImm(36) // &jbuf[1] :: pc
7211 .addMemOperand(FIMMOSt));
7212 } else if (isThumb) {
7213 // Incoming value: jbuf
7214 // ldr.n r1, LCPI1_4
7215 // add r1, pc
7216 // mov r2, #1
7217 // orrs r1, r2
7218 // add r2, $jbuf, #+4 ; &jbuf[1]
7219 // str r1, [r2]
7220 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7221 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7222 .addConstantPoolIndex(CPI)
7223 .addMemOperand(CPMMO));
7224 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7225 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7226 .addReg(NewVReg1, RegState::Kill)
7227 .addImm(PCLabelId);
7228 // Set the low bit because of thumb mode.
7229 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7230 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7231 .addReg(ARM::CPSR, RegState::Define)
7232 .addImm(1));
7233 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7234 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7235 .addReg(ARM::CPSR, RegState::Define)
7236 .addReg(NewVReg2, RegState::Kill)
7237 .addReg(NewVReg3, RegState::Kill));
7238 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007239 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7240 .addFrameIndex(FI)
7241 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007242 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7243 .addReg(NewVReg4, RegState::Kill)
7244 .addReg(NewVReg5, RegState::Kill)
7245 .addImm(0)
7246 .addMemOperand(FIMMOSt));
7247 } else {
7248 // Incoming value: jbuf
7249 // ldr r1, LCPI1_1
7250 // add r1, pc, r1
7251 // str r1, [$jbuf, #+4] ; &jbuf[1]
7252 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7253 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7254 .addConstantPoolIndex(CPI)
7255 .addImm(0)
7256 .addMemOperand(CPMMO));
7257 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7258 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7259 .addReg(NewVReg1, RegState::Kill)
7260 .addImm(PCLabelId));
7261 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7262 .addReg(NewVReg2, RegState::Kill)
7263 .addFrameIndex(FI)
7264 .addImm(36) // &jbuf[1] :: pc
7265 .addMemOperand(FIMMOSt));
7266 }
7267}
7268
Matthias Brauneec4efc2015-04-28 00:37:05 +00007269void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
7270 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007271 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007272 DebugLoc dl = MI->getDebugLoc();
7273 MachineFunction *MF = MBB->getParent();
7274 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007275 MachineFrameInfo *MFI = MF->getFrameInfo();
7276 int FI = MFI->getFunctionContextIndex();
7277
Craig Topper61e88f42014-11-21 05:58:21 +00007278 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7279 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007280
Bill Wendling362c1b02011-10-06 21:29:56 +00007281 // Get a mapping of the call site numbers to all of the landing pads they're
7282 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007283 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7284 unsigned MaxCSNum = 0;
7285 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007286 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7287 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007288 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007289
7290 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7291 // pad.
7292 for (MachineBasicBlock::iterator
7293 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7294 if (!II->isEHLabel()) continue;
7295
7296 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007297 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007298
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007299 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7300 for (SmallVectorImpl<unsigned>::iterator
7301 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7302 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007303 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007304 MaxCSNum = std::max(MaxCSNum, *CSI);
7305 }
Bill Wendling202803e2011-10-05 00:02:33 +00007306 break;
7307 }
7308 }
7309
7310 // Get an ordered list of the machine basic blocks for the jump table.
7311 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007312 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007313 LPadList.reserve(CallSiteNumToLPad.size());
7314 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7315 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7316 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007317 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007318 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007319 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7320 }
Bill Wendling202803e2011-10-05 00:02:33 +00007321 }
7322
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007323 assert(!LPadList.empty() &&
7324 "No landing pad destinations for the dispatch jump table!");
7325
Bill Wendling362c1b02011-10-06 21:29:56 +00007326 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007327 MachineJumpTableInfo *JTI =
7328 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7329 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007330 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007331
Bill Wendling362c1b02011-10-06 21:29:56 +00007332 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007333
7334 // Shove the dispatch's address into the return slot in the function context.
7335 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007336 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007337
Bill Wendling324be982011-10-05 00:39:32 +00007338 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007339 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007340 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007341 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007342 else
7343 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7344
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007345 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007346 DispatchBB->addSuccessor(TrapBB);
7347
7348 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7349 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007350
Bill Wendling510fbcd2011-10-17 21:32:56 +00007351 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007352 MF->insert(MF->end(), DispatchBB);
7353 MF->insert(MF->end(), DispContBB);
7354 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007355
Bill Wendling030b58e2011-10-06 22:18:16 +00007356 // Insert code into the entry block that creates and registers the function
7357 // context.
7358 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7359
Alex Lorenze40c8a22015-08-11 23:09:45 +00007360 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7361 MachinePointerInfo::getFixedStack(*MF, FI),
7362 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007363
Chad Rosier1ec8e402012-11-06 23:05:24 +00007364 MachineInstrBuilder MIB;
7365 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7366
7367 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7368 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7369
7370 // Add a register mask with no preserved registers. This results in all
7371 // registers being marked as clobbered.
7372 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007373
Bill Wendling85833f72011-10-18 22:49:07 +00007374 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007375 if (Subtarget->isThumb2()) {
7376 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7377 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7378 .addFrameIndex(FI)
7379 .addImm(4)
7380 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007381
Bill Wendling85833f72011-10-18 22:49:07 +00007382 if (NumLPads < 256) {
7383 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7384 .addReg(NewVReg1)
7385 .addImm(LPadList.size()));
7386 } else {
7387 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7388 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007389 .addImm(NumLPads & 0xFFFF));
7390
7391 unsigned VReg2 = VReg1;
7392 if ((NumLPads & 0xFFFF0000) != 0) {
7393 VReg2 = MRI->createVirtualRegister(TRC);
7394 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7395 .addReg(VReg1)
7396 .addImm(NumLPads >> 16));
7397 }
7398
Bill Wendling85833f72011-10-18 22:49:07 +00007399 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7400 .addReg(NewVReg1)
7401 .addReg(VReg2));
7402 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007403
Bill Wendling5626c662011-10-06 22:53:00 +00007404 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7405 .addMBB(TrapBB)
7406 .addImm(ARMCC::HI)
7407 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007408
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007409 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7410 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007411 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007412
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007413 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007414 AddDefaultCC(
7415 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007416 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7417 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007418 .addReg(NewVReg1)
7419 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7420
7421 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007422 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007423 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007424 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007425 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007426 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7427 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7428 .addFrameIndex(FI)
7429 .addImm(1)
7430 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007431
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007432 if (NumLPads < 256) {
7433 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7434 .addReg(NewVReg1)
7435 .addImm(NumLPads));
7436 } else {
7437 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007438 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7439 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7440
7441 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007442 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007443 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007444 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007445 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007446
7447 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7448 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7449 .addReg(VReg1, RegState::Define)
7450 .addConstantPoolIndex(Idx));
7451 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7452 .addReg(NewVReg1)
7453 .addReg(VReg1));
7454 }
7455
Bill Wendlingb3d46782011-10-06 23:37:36 +00007456 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7457 .addMBB(TrapBB)
7458 .addImm(ARMCC::HI)
7459 .addReg(ARM::CPSR);
7460
7461 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7462 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7463 .addReg(ARM::CPSR, RegState::Define)
7464 .addReg(NewVReg1)
7465 .addImm(2));
7466
7467 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007468 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007469 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007470
7471 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7472 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7473 .addReg(ARM::CPSR, RegState::Define)
7474 .addReg(NewVReg2, RegState::Kill)
7475 .addReg(NewVReg3));
7476
Alex Lorenze40c8a22015-08-11 23:09:45 +00007477 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7478 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007479
7480 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7481 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7482 .addReg(NewVReg4, RegState::Kill)
7483 .addImm(0)
7484 .addMemOperand(JTMMOLd));
7485
Chad Rosier96603432013-03-01 18:30:38 +00007486 unsigned NewVReg6 = NewVReg5;
7487 if (RelocM == Reloc::PIC_) {
7488 NewVReg6 = MRI->createVirtualRegister(TRC);
7489 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7490 .addReg(ARM::CPSR, RegState::Define)
7491 .addReg(NewVReg5, RegState::Kill)
7492 .addReg(NewVReg3));
7493 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007494
7495 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7496 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007497 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007498 } else {
7499 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7500 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7501 .addFrameIndex(FI)
7502 .addImm(4)
7503 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007504
Bill Wendling4969dcd2011-10-18 22:52:20 +00007505 if (NumLPads < 256) {
7506 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7507 .addReg(NewVReg1)
7508 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007509 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007510 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7511 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007512 .addImm(NumLPads & 0xFFFF));
7513
7514 unsigned VReg2 = VReg1;
7515 if ((NumLPads & 0xFFFF0000) != 0) {
7516 VReg2 = MRI->createVirtualRegister(TRC);
7517 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7518 .addReg(VReg1)
7519 .addImm(NumLPads >> 16));
7520 }
7521
Bill Wendling4969dcd2011-10-18 22:52:20 +00007522 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7523 .addReg(NewVReg1)
7524 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007525 } else {
7526 MachineConstantPool *ConstantPool = MF->getConstantPool();
7527 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7528 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7529
7530 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007531 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007532 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007533 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007534 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7535
7536 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7537 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7538 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007539 .addConstantPoolIndex(Idx)
7540 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007541 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7542 .addReg(NewVReg1)
7543 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007544 }
7545
Bill Wendling5626c662011-10-06 22:53:00 +00007546 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7547 .addMBB(TrapBB)
7548 .addImm(ARMCC::HI)
7549 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007550
Bill Wendling973c8172011-10-18 22:11:18 +00007551 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007552 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007553 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007554 .addReg(NewVReg1)
7555 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007556 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7557 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007558 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007559
Alex Lorenze40c8a22015-08-11 23:09:45 +00007560 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7561 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007562 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007563 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007564 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7565 .addReg(NewVReg3, RegState::Kill)
7566 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007567 .addImm(0)
7568 .addMemOperand(JTMMOLd));
7569
Chad Rosier96603432013-03-01 18:30:38 +00007570 if (RelocM == Reloc::PIC_) {
7571 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7572 .addReg(NewVReg5, RegState::Kill)
7573 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007574 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007575 } else {
7576 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7577 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007578 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007579 }
Bill Wendling5626c662011-10-06 22:53:00 +00007580 }
Bill Wendling202803e2011-10-05 00:02:33 +00007581
Bill Wendling324be982011-10-05 00:39:32 +00007582 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007583 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007584 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007585 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7586 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007587 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007588 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007589 }
7590
Bill Wendling26d27802011-10-17 05:25:09 +00007591 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007592 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007593 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007594 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007595
7596 // Remove the landing pad successor from the invoke block and replace it
7597 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007598 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7599 BB->succ_end());
7600 while (!Successors.empty()) {
7601 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007602 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007603 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007604 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007605 }
7606 }
7607
Cong Houd97c1002015-12-01 05:29:22 +00007608 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00007609 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007610
7611 // Find the invoke call and mark all of the callee-saved registers as
7612 // 'implicit defined' so that they're spilled. This prevents code from
7613 // moving instructions to before the EH block, where they will never be
7614 // executed.
7615 for (MachineBasicBlock::reverse_iterator
7616 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007617 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007618
7619 DenseMap<unsigned, bool> DefRegs;
7620 for (MachineInstr::mop_iterator
7621 OI = II->operands_begin(), OE = II->operands_end();
7622 OI != OE; ++OI) {
7623 if (!OI->isReg()) continue;
7624 DefRegs[OI->getReg()] = true;
7625 }
7626
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007627 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007628
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007629 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007630 unsigned Reg = SavedRegs[i];
7631 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007632 !ARM::tGPRRegClass.contains(Reg) &&
7633 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007634 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007635 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007636 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007637 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007638 continue;
7639 if (!DefRegs[Reg])
7640 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007641 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007642
7643 break;
7644 }
Bill Wendling883ec972011-10-07 23:18:02 +00007645 }
Bill Wendling324be982011-10-05 00:39:32 +00007646
Bill Wendling617075f2011-10-18 18:30:49 +00007647 // Mark all former landing pads as non-landing pads. The dispatch is the only
7648 // landing pad now.
7649 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7650 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007651 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007652
Bill Wendling324be982011-10-05 00:39:32 +00007653 // The instruction is gone now.
7654 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007655}
7656
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007657static
7658MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7659 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7660 E = MBB->succ_end(); I != E; ++I)
7661 if (*I != Succ)
7662 return *I;
7663 llvm_unreachable("Expecting a BB with two successors!");
7664}
7665
Manman Renb504f492013-10-29 22:27:32 +00007666/// Return the load opcode for a given load size. If load size >= 8,
7667/// neon opcode will be returned.
7668static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7669 if (LdSize >= 8)
7670 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7671 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7672 if (IsThumb1)
7673 return LdSize == 4 ? ARM::tLDRi
7674 : LdSize == 2 ? ARM::tLDRHi
7675 : LdSize == 1 ? ARM::tLDRBi : 0;
7676 if (IsThumb2)
7677 return LdSize == 4 ? ARM::t2LDR_POST
7678 : LdSize == 2 ? ARM::t2LDRH_POST
7679 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7680 return LdSize == 4 ? ARM::LDR_POST_IMM
7681 : LdSize == 2 ? ARM::LDRH_POST
7682 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7683}
7684
7685/// Return the store opcode for a given store size. If store size >= 8,
7686/// neon opcode will be returned.
7687static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7688 if (StSize >= 8)
7689 return StSize == 16 ? ARM::VST1q32wb_fixed
7690 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7691 if (IsThumb1)
7692 return StSize == 4 ? ARM::tSTRi
7693 : StSize == 2 ? ARM::tSTRHi
7694 : StSize == 1 ? ARM::tSTRBi : 0;
7695 if (IsThumb2)
7696 return StSize == 4 ? ARM::t2STR_POST
7697 : StSize == 2 ? ARM::t2STRH_POST
7698 : StSize == 1 ? ARM::t2STRB_POST : 0;
7699 return StSize == 4 ? ARM::STR_POST_IMM
7700 : StSize == 2 ? ARM::STRH_POST
7701 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7702}
7703
7704/// Emit a post-increment load operation with given size. The instructions
7705/// will be added to BB at Pos.
7706static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7707 const TargetInstrInfo *TII, DebugLoc dl,
7708 unsigned LdSize, unsigned Data, unsigned AddrIn,
7709 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7710 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7711 assert(LdOpc != 0 && "Should have a load opcode");
7712 if (LdSize >= 8) {
7713 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7714 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7715 .addImm(0));
7716 } else if (IsThumb1) {
7717 // load + update AddrIn
7718 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7719 .addReg(AddrIn).addImm(0));
7720 MachineInstrBuilder MIB =
7721 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7722 MIB = AddDefaultT1CC(MIB);
7723 MIB.addReg(AddrIn).addImm(LdSize);
7724 AddDefaultPred(MIB);
7725 } else if (IsThumb2) {
7726 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7727 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7728 .addImm(LdSize));
7729 } else { // arm
7730 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7731 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7732 .addReg(0).addImm(LdSize));
7733 }
7734}
7735
7736/// Emit a post-increment store operation with given size. The instructions
7737/// will be added to BB at Pos.
7738static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7739 const TargetInstrInfo *TII, DebugLoc dl,
7740 unsigned StSize, unsigned Data, unsigned AddrIn,
7741 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7742 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7743 assert(StOpc != 0 && "Should have a store opcode");
7744 if (StSize >= 8) {
7745 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7746 .addReg(AddrIn).addImm(0).addReg(Data));
7747 } else if (IsThumb1) {
7748 // store + update AddrIn
7749 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7750 .addReg(AddrIn).addImm(0));
7751 MachineInstrBuilder MIB =
7752 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7753 MIB = AddDefaultT1CC(MIB);
7754 MIB.addReg(AddrIn).addImm(StSize);
7755 AddDefaultPred(MIB);
7756 } else if (IsThumb2) {
7757 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7758 .addReg(Data).addReg(AddrIn).addImm(StSize));
7759 } else { // arm
7760 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7761 .addReg(Data).addReg(AddrIn).addReg(0)
7762 .addImm(StSize));
7763 }
7764}
7765
David Peixottoc32e24a2013-10-17 19:49:22 +00007766MachineBasicBlock *
7767ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7768 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007769 // This pseudo instruction has 3 operands: dst, src, size
7770 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7771 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007772 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007773 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007774 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007775
7776 unsigned dest = MI->getOperand(0).getReg();
7777 unsigned src = MI->getOperand(1).getReg();
7778 unsigned SizeVal = MI->getOperand(2).getImm();
7779 unsigned Align = MI->getOperand(3).getImm();
7780 DebugLoc dl = MI->getDebugLoc();
7781
Manman Rene8735522012-06-01 19:33:18 +00007782 MachineFunction *MF = BB->getParent();
7783 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007784 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007785 const TargetRegisterClass *TRC = nullptr;
7786 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007787
7788 bool IsThumb1 = Subtarget->isThumb1Only();
7789 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007790
7791 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007792 UnitSize = 1;
7793 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007794 UnitSize = 2;
7795 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007796 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007797 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007798 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007799 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007800 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007801 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007802 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007803 }
7804 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007805 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007806 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007807 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007808
David Peixottob0653e532013-10-24 16:39:36 +00007809 // Select the correct opcode and register class for unit size load/store
7810 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007811 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007812 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007813 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7814 : UnitSize == 8 ? &ARM::DPRRegClass
7815 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007816
Manman Rene8735522012-06-01 19:33:18 +00007817 unsigned BytesLeft = SizeVal % UnitSize;
7818 unsigned LoopSize = SizeVal - BytesLeft;
7819
7820 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7821 // Use LDR and STR to copy.
7822 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7823 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7824 unsigned srcIn = src;
7825 unsigned destIn = dest;
7826 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007827 unsigned srcOut = MRI.createVirtualRegister(TRC);
7828 unsigned destOut = MRI.createVirtualRegister(TRC);
7829 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007830 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7831 IsThumb1, IsThumb2);
7832 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7833 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007834 srcIn = srcOut;
7835 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007836 }
7837
7838 // Handle the leftover bytes with LDRB and STRB.
7839 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7840 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007841 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007842 unsigned srcOut = MRI.createVirtualRegister(TRC);
7843 unsigned destOut = MRI.createVirtualRegister(TRC);
7844 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007845 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7846 IsThumb1, IsThumb2);
7847 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7848 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007849 srcIn = srcOut;
7850 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007851 }
7852 MI->eraseFromParent(); // The instruction is gone now.
7853 return BB;
7854 }
7855
7856 // Expand the pseudo op to a loop.
7857 // thisMBB:
7858 // ...
7859 // movw varEnd, # --> with thumb2
7860 // movt varEnd, #
7861 // ldrcp varEnd, idx --> without thumb2
7862 // fallthrough --> loopMBB
7863 // loopMBB:
7864 // PHI varPhi, varEnd, varLoop
7865 // PHI srcPhi, src, srcLoop
7866 // PHI destPhi, dst, destLoop
7867 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7868 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7869 // subs varLoop, varPhi, #UnitSize
7870 // bne loopMBB
7871 // fallthrough --> exitMBB
7872 // exitMBB:
7873 // epilogue to handle left-over bytes
7874 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7875 // [destOut] = STRB_POST(scratch, destLoop, 1)
7876 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7877 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7878 MF->insert(It, loopMBB);
7879 MF->insert(It, exitMBB);
7880
7881 // Transfer the remainder of BB and its successor edges to exitMBB.
7882 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007883 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007884 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7885
7886 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007887 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007888 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007889 unsigned Vtmp = varEnd;
7890 if ((LoopSize & 0xFFFF0000) != 0)
7891 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007892 AddDefaultPred(BuildMI(BB, dl,
7893 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7894 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007895
7896 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007897 AddDefaultPred(BuildMI(BB, dl,
7898 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7899 varEnd)
7900 .addReg(Vtmp)
7901 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007902 } else {
7903 MachineConstantPool *ConstantPool = MF->getConstantPool();
7904 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7905 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7906
7907 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007908 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007909 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007910 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007911 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7912
7913 if (IsThumb1)
7914 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7915 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7916 else
7917 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7918 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7919 }
Manman Rene8735522012-06-01 19:33:18 +00007920 BB->addSuccessor(loopMBB);
7921
7922 // Generate the loop body:
7923 // varPhi = PHI(varLoop, varEnd)
7924 // srcPhi = PHI(srcLoop, src)
7925 // destPhi = PHI(destLoop, dst)
7926 MachineBasicBlock *entryBB = BB;
7927 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007928 unsigned varLoop = MRI.createVirtualRegister(TRC);
7929 unsigned varPhi = MRI.createVirtualRegister(TRC);
7930 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7931 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7932 unsigned destLoop = MRI.createVirtualRegister(TRC);
7933 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007934
7935 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7936 .addReg(varLoop).addMBB(loopMBB)
7937 .addReg(varEnd).addMBB(entryBB);
7938 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7939 .addReg(srcLoop).addMBB(loopMBB)
7940 .addReg(src).addMBB(entryBB);
7941 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7942 .addReg(destLoop).addMBB(loopMBB)
7943 .addReg(dest).addMBB(entryBB);
7944
7945 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7946 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007947 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007948 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7949 IsThumb1, IsThumb2);
7950 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7951 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007952
7953 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007954 if (IsThumb1) {
7955 MachineInstrBuilder MIB =
7956 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7957 MIB = AddDefaultT1CC(MIB);
7958 MIB.addReg(varPhi).addImm(UnitSize);
7959 AddDefaultPred(MIB);
7960 } else {
7961 MachineInstrBuilder MIB =
7962 BuildMI(*BB, BB->end(), dl,
7963 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7964 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7965 MIB->getOperand(5).setReg(ARM::CPSR);
7966 MIB->getOperand(5).setIsDef(true);
7967 }
7968 BuildMI(*BB, BB->end(), dl,
7969 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7970 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007971
7972 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7973 BB->addSuccessor(loopMBB);
7974 BB->addSuccessor(exitMBB);
7975
7976 // Add epilogue to handle BytesLeft.
7977 BB = exitMBB;
7978 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007979
7980 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7981 // [destOut] = STRB_POST(scratch, destLoop, 1)
7982 unsigned srcIn = srcLoop;
7983 unsigned destIn = destLoop;
7984 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007985 unsigned srcOut = MRI.createVirtualRegister(TRC);
7986 unsigned destOut = MRI.createVirtualRegister(TRC);
7987 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007988 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7989 IsThumb1, IsThumb2);
7990 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7991 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007992 srcIn = srcOut;
7993 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007994 }
7995
7996 MI->eraseFromParent(); // The instruction is gone now.
7997 return BB;
7998}
7999
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00008000MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008001ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
8002 MachineBasicBlock *MBB) const {
8003 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00008004 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008005 DebugLoc DL = MI->getDebugLoc();
8006
8007 assert(Subtarget->isTargetWindows() &&
8008 "__chkstk is only supported on Windows");
8009 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
8010
8011 // __chkstk takes the number of words to allocate on the stack in R4, and
8012 // returns the stack adjustment in number of bytes in R4. This will not
8013 // clober any other registers (other than the obvious lr).
8014 //
8015 // Although, technically, IP should be considered a register which may be
8016 // clobbered, the call itself will not touch it. Windows on ARM is a pure
8017 // thumb-2 environment, so there is no interworking required. As a result, we
8018 // do not expect a veneer to be emitted by the linker, clobbering IP.
8019 //
Alp Toker1d099d92014-06-19 19:41:26 +00008020 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008021 // required, again, ensuring that IP is not clobbered.
8022 //
8023 // Finally, although some linkers may theoretically provide a trampoline for
8024 // out of range calls (which is quite common due to a 32M range limitation of
8025 // branches for Thumb), we can generate the long-call version via
8026 // -mcmodel=large, alleviating the need for the trampoline which may clobber
8027 // IP.
8028
8029 switch (TM.getCodeModel()) {
8030 case CodeModel::Small:
8031 case CodeModel::Medium:
8032 case CodeModel::Default:
8033 case CodeModel::Kernel:
8034 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
8035 .addImm((unsigned)ARMCC::AL).addReg(0)
8036 .addExternalSymbol("__chkstk")
8037 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8038 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8039 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8040 break;
8041 case CodeModel::Large:
8042 case CodeModel::JITDefault: {
8043 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
8044 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
8045
8046 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
8047 .addExternalSymbol("__chkstk");
8048 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
8049 .addImm((unsigned)ARMCC::AL).addReg(0)
8050 .addReg(Reg, RegState::Kill)
8051 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8052 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8053 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8054 break;
8055 }
8056 }
8057
8058 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
8059 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00008060 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008061
8062 MI->eraseFromParent();
8063 return MBB;
8064}
8065
8066MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008067ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
8068 MachineBasicBlock *MBB) const {
8069 DebugLoc DL = MI->getDebugLoc();
8070 MachineFunction *MF = MBB->getParent();
8071 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8072
8073 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
Saleem Abdulrasool750a90d2016-03-25 19:48:06 +00008074 MF->insert(++MBB->getIterator(), ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008075 ContBB->splice(ContBB->begin(), MBB,
8076 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008077 ContBB->transferSuccessorsAndUpdatePHIs(MBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008078
8079 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8080 MF->push_back(TrapBB);
8081 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
8082 MBB->addSuccessor(TrapBB);
8083
8084 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
8085 .addReg(MI->getOperand(0).getReg())
8086 .addMBB(TrapBB);
Saleem Abdulrasool0dab98d2016-03-25 00:34:11 +00008087 AddDefaultPred(BuildMI(*MBB, MI, DL, TII->get(ARM::t2B)).addMBB(ContBB));
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008088 MBB->addSuccessor(ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008089
8090 MI->eraseFromParent();
8091 return ContBB;
8092}
8093
8094MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008095ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008096 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008097 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00008098 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008099 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00008100 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008101 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00008102 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00008103 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008104 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008105 // The Thumb2 pre-indexed stores have the same MI operands, they just
8106 // define them differently in the .td files from the isel patterns, so
8107 // they need pseudos.
8108 case ARM::t2STR_preidx:
8109 MI->setDesc(TII->get(ARM::t2STR_PRE));
8110 return BB;
8111 case ARM::t2STRB_preidx:
8112 MI->setDesc(TII->get(ARM::t2STRB_PRE));
8113 return BB;
8114 case ARM::t2STRH_preidx:
8115 MI->setDesc(TII->get(ARM::t2STRH_PRE));
8116 return BB;
8117
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008118 case ARM::STRi_preidx:
8119 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00008120 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008121 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
8122 // Decode the offset.
8123 unsigned Offset = MI->getOperand(4).getImm();
8124 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8125 Offset = ARM_AM::getAM2Offset(Offset);
8126 if (isSub)
8127 Offset = -Offset;
8128
Jim Grosbachf402f692011-08-12 21:02:34 +00008129 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008130 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008131 .addOperand(MI->getOperand(0)) // Rn_wb
8132 .addOperand(MI->getOperand(1)) // Rt
8133 .addOperand(MI->getOperand(2)) // Rn
8134 .addImm(Offset) // offset (skip GPR==zero_reg)
8135 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00008136 .addOperand(MI->getOperand(6))
8137 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008138 MI->eraseFromParent();
8139 return BB;
8140 }
8141 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008142 case ARM::STRBr_preidx:
8143 case ARM::STRH_preidx: {
8144 unsigned NewOpc;
8145 switch (MI->getOpcode()) {
8146 default: llvm_unreachable("unexpected opcode!");
8147 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8148 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8149 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8150 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008151 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
8152 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
8153 MIB.addOperand(MI->getOperand(i));
8154 MI->eraseFromParent();
8155 return BB;
8156 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008157
Evan Chengbb2af352009-08-12 05:17:19 +00008158 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008159 // To "insert" a SELECT_CC instruction, we actually have to insert the
8160 // diamond control-flow pattern. The incoming instruction knows the
8161 // destination vreg to set, the condition code register to branch on, the
8162 // true/false values to select between, and a branch opcode to use.
8163 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008164 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008165
8166 // thisMBB:
8167 // ...
8168 // TrueVal = ...
8169 // cmpTY ccX, r1, r2
8170 // bCC copy1MBB
8171 // fallthrough --> copy0MBB
8172 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008173 MachineFunction *F = BB->getParent();
8174 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8175 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008176 F->insert(It, copy0MBB);
8177 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008178
8179 // Transfer the remainder of BB and its successor edges to sinkMBB.
8180 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008181 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008182 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8183
Dan Gohmanf4f04102010-07-06 15:49:48 +00008184 BB->addSuccessor(copy0MBB);
8185 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008186
Dan Gohman34396292010-07-06 20:24:04 +00008187 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
8188 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
8189
Evan Cheng10043e22007-01-19 07:51:42 +00008190 // copy0MBB:
8191 // %FalseValue = ...
8192 // # fallthrough to sinkMBB
8193 BB = copy0MBB;
8194
8195 // Update machine-CFG edges
8196 BB->addSuccessor(sinkMBB);
8197
8198 // sinkMBB:
8199 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8200 // ...
8201 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008202 BuildMI(*BB, BB->begin(), dl,
8203 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00008204 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8205 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8206
Dan Gohman34396292010-07-06 20:24:04 +00008207 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008208 return BB;
8209 }
Evan Chengb972e562009-08-07 00:34:42 +00008210
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008211 case ARM::BCCi64:
8212 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008213 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008214 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008215
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008216 // Compare both parts that make up the double comparison separately for
8217 // equality.
8218 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
8219
8220 unsigned LHS1 = MI->getOperand(1).getReg();
8221 unsigned LHS2 = MI->getOperand(2).getReg();
8222 if (RHSisZero) {
8223 AddDefaultPred(BuildMI(BB, dl,
8224 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8225 .addReg(LHS1).addImm(0));
8226 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8227 .addReg(LHS2).addImm(0)
8228 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8229 } else {
8230 unsigned RHS1 = MI->getOperand(3).getReg();
8231 unsigned RHS2 = MI->getOperand(4).getReg();
8232 AddDefaultPred(BuildMI(BB, dl,
8233 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8234 .addReg(LHS1).addReg(RHS1));
8235 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8236 .addReg(LHS2).addReg(RHS2)
8237 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8238 }
8239
8240 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
8241 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
8242 if (MI->getOperand(0).getImm() == ARMCC::NE)
8243 std::swap(destMBB, exitMBB);
8244
8245 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8246 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008247 if (isThumb2)
8248 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8249 else
8250 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008251
8252 MI->eraseFromParent(); // The pseudo instruction is gone now.
8253 return BB;
8254 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008255
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008256 case ARM::Int_eh_sjlj_setjmp:
8257 case ARM::Int_eh_sjlj_setjmp_nofp:
8258 case ARM::tInt_eh_sjlj_setjmp:
8259 case ARM::t2Int_eh_sjlj_setjmp:
8260 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008261 return BB;
8262
8263 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008264 EmitSjLjDispatchBlock(MI, BB);
8265 return BB;
8266
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008267 case ARM::ABS:
8268 case ARM::t2ABS: {
8269 // To insert an ABS instruction, we have to insert the
8270 // diamond control-flow pattern. The incoming instruction knows the
8271 // source vreg to test against 0, the destination vreg to set,
8272 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008273 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008274 // It transforms
8275 // V1 = ABS V0
8276 // into
8277 // V2 = MOVS V0
8278 // BCC (branch to SinkBB if V0 >= 0)
8279 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008280 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008281 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008282 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008283 MachineFunction *Fn = BB->getParent();
8284 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8285 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8286 Fn->insert(BBI, RSBBB);
8287 Fn->insert(BBI, SinkBB);
8288
8289 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8290 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008291 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008292 bool isThumb2 = Subtarget->isThumb2();
8293 MachineRegisterInfo &MRI = Fn->getRegInfo();
8294 // In Thumb mode S must not be specified if source register is the SP or
8295 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008296 unsigned NewRsbDstReg =
8297 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008298
8299 // Transfer the remainder of BB and its successor edges to sinkMBB.
8300 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008301 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008302 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8303
8304 BB->addSuccessor(RSBBB);
8305 BB->addSuccessor(SinkBB);
8306
8307 // fall through to SinkMBB
8308 RSBBB->addSuccessor(SinkBB);
8309
Manman Rene0763c72012-06-15 21:32:12 +00008310 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008311 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008312 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8313 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008314
8315 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008316 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008317 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8318 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8319
8320 // insert rsbri in RSBBB
8321 // Note: BCC and rsbri will be converted into predicated rsbmi
8322 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008323 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008324 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008325 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008326 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8327
Andrew Trick3f07c422011-10-18 18:40:53 +00008328 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008329 // reuse ABSDstReg to not change uses of ABS instruction
8330 BuildMI(*SinkBB, SinkBB->begin(), dl,
8331 TII->get(ARM::PHI), ABSDstReg)
8332 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008333 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008334
8335 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008336 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008337
8338 // return last added BB
8339 return SinkBB;
8340 }
Manman Rene8735522012-06-01 19:33:18 +00008341 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008342 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008343 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008344 case ARM::WIN__CHKSTK:
8345 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008346 case ARM::WIN__DBZCHK:
8347 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008348 }
8349}
8350
Scott Douglass953f9082015-10-05 14:49:54 +00008351/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8352/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8353/// instead of as a custom inserter because we need the use list from the SDNode.
8354static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8355 MachineInstr *MI, const SDNode *Node) {
8356 bool isThumb1 = Subtarget->isThumb1Only();
8357
8358 DebugLoc DL = MI->getDebugLoc();
8359 MachineFunction *MF = MI->getParent()->getParent();
8360 MachineRegisterInfo &MRI = MF->getRegInfo();
8361 MachineInstrBuilder MIB(*MF, MI);
8362
8363 // If the new dst/src is unused mark it as dead.
8364 if (!Node->hasAnyUseOfValue(0)) {
8365 MI->getOperand(0).setIsDead(true);
8366 }
8367 if (!Node->hasAnyUseOfValue(1)) {
8368 MI->getOperand(1).setIsDead(true);
8369 }
8370
8371 // The MEMCPY both defines and kills the scratch registers.
8372 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8373 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8374 : &ARM::GPRRegClass);
8375 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8376 }
8377}
8378
Evan Chenge6fba772011-08-30 19:09:48 +00008379void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8380 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008381 if (MI->getOpcode() == ARM::MEMCPY) {
8382 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8383 return;
8384 }
8385
Evan Cheng7f8e5632011-12-07 07:15:52 +00008386 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008387 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8388 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8389 // operand is still set to noreg. If needed, set the optional operand's
8390 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008391 //
Andrew Trick88b24502011-10-18 19:18:52 +00008392 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008393
Andrew Trick924123a2011-09-21 02:20:46 +00008394 // Rename pseudo opcodes.
8395 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8396 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008397 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008398 MCID = &TII->get(NewOpc);
8399
8400 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8401 "converted opcode should be the same except for cc_out");
8402
8403 MI->setDesc(*MCID);
8404
8405 // Add the optional cc_out operand
8406 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008407 }
Andrew Trick88b24502011-10-18 19:18:52 +00008408 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008409
8410 // Any ARM instruction that sets the 's' bit should specify an optional
8411 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008412 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008413 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008414 return;
8415 }
Andrew Trick924123a2011-09-21 02:20:46 +00008416 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8417 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008418 bool definesCPSR = false;
8419 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008420 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008421 i != e; ++i) {
8422 const MachineOperand &MO = MI->getOperand(i);
8423 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8424 definesCPSR = true;
8425 if (MO.isDead())
8426 deadCPSR = true;
8427 MI->RemoveOperand(i);
8428 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008429 }
8430 }
Andrew Trick8586e622011-09-20 03:17:40 +00008431 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008432 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008433 return;
8434 }
8435 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008436 if (deadCPSR) {
8437 assert(!MI->getOperand(ccOutIdx).getReg() &&
8438 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008439 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008440 }
Andrew Trick8586e622011-09-20 03:17:40 +00008441
Andrew Trick924123a2011-09-21 02:20:46 +00008442 // If this instruction was defined with an optional CPSR def and its dag node
8443 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008444 MachineOperand &MO = MI->getOperand(ccOutIdx);
8445 MO.setReg(ARM::CPSR);
8446 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008447}
8448
Evan Cheng10043e22007-01-19 07:51:42 +00008449//===----------------------------------------------------------------------===//
8450// ARM Optimization Hooks
8451//===----------------------------------------------------------------------===//
8452
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008453// Helper function that checks if N is a null or all ones constant.
8454static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008455 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008456}
8457
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008458// Return true if N is conditionally 0 or all ones.
8459// Detects these expressions where cc is an i1 value:
8460//
8461// (select cc 0, y) [AllOnes=0]
8462// (select cc y, 0) [AllOnes=0]
8463// (zext cc) [AllOnes=0]
8464// (sext cc) [AllOnes=0/1]
8465// (select cc -1, y) [AllOnes=1]
8466// (select cc y, -1) [AllOnes=1]
8467//
8468// Invert is set when N is the null/all ones constant when CC is false.
8469// OtherOp is set to the alternative value of N.
8470static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8471 SDValue &CC, bool &Invert,
8472 SDValue &OtherOp,
8473 SelectionDAG &DAG) {
8474 switch (N->getOpcode()) {
8475 default: return false;
8476 case ISD::SELECT: {
8477 CC = N->getOperand(0);
8478 SDValue N1 = N->getOperand(1);
8479 SDValue N2 = N->getOperand(2);
8480 if (isZeroOrAllOnes(N1, AllOnes)) {
8481 Invert = false;
8482 OtherOp = N2;
8483 return true;
8484 }
8485 if (isZeroOrAllOnes(N2, AllOnes)) {
8486 Invert = true;
8487 OtherOp = N1;
8488 return true;
8489 }
8490 return false;
8491 }
8492 case ISD::ZERO_EXTEND:
8493 // (zext cc) can never be the all ones value.
8494 if (AllOnes)
8495 return false;
8496 // Fall through.
8497 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008498 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008499 EVT VT = N->getValueType(0);
8500 CC = N->getOperand(0);
8501 if (CC.getValueType() != MVT::i1)
8502 return false;
8503 Invert = !AllOnes;
8504 if (AllOnes)
8505 // When looking for an AllOnes constant, N is an sext, and the 'other'
8506 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008507 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008508 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8509 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008510 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008511 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008512 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8513 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008514 return true;
8515 }
8516 }
8517}
8518
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008519// Combine a constant select operand into its use:
8520//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008521// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8522// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8523// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8524// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8525// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008526//
8527// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008528// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008529//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008530// Also recognize sext/zext from i1:
8531//
8532// (add (zext cc), x) -> (select cc (add x, 1), x)
8533// (add (sext cc), x) -> (select cc (add x, -1), x)
8534//
8535// These transformations eventually create predicated instructions.
8536//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008537// @param N The node to transform.
8538// @param Slct The N operand that is a select.
8539// @param OtherOp The other N operand (x above).
8540// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008541// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008542// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008543static
8544SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008545 TargetLowering::DAGCombinerInfo &DCI,
8546 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008547 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008548 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008549 SDValue NonConstantVal;
8550 SDValue CCOp;
8551 bool SwapSelectOps;
8552 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8553 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008554 return SDValue();
8555
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008556 // Slct is now know to be the desired identity constant when CC is true.
8557 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008558 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008559 OtherOp, NonConstantVal);
8560 // Unless SwapSelectOps says CC should be false.
8561 if (SwapSelectOps)
8562 std::swap(TrueVal, FalseVal);
8563
Andrew Trickef9de2a2013-05-25 02:42:55 +00008564 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008565 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008566}
8567
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008568// Attempt combineSelectAndUse on each operand of a commutative operator N.
8569static
8570SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8571 TargetLowering::DAGCombinerInfo &DCI) {
8572 SDValue N0 = N->getOperand(0);
8573 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008574 if (N0.getNode()->hasOneUse())
8575 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008576 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008577 if (N1.getNode()->hasOneUse())
8578 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008579 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008580 return SDValue();
8581}
8582
Eric Christopher1b8b94192011-06-29 21:10:36 +00008583// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008584// (only after legalization).
8585static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8586 TargetLowering::DAGCombinerInfo &DCI,
8587 const ARMSubtarget *Subtarget) {
8588
8589 // Only perform optimization if after legalize, and if NEON is available. We
8590 // also expected both operands to be BUILD_VECTORs.
8591 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8592 || N0.getOpcode() != ISD::BUILD_VECTOR
8593 || N1.getOpcode() != ISD::BUILD_VECTOR)
8594 return SDValue();
8595
8596 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8597 EVT VT = N->getValueType(0);
8598 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8599 return SDValue();
8600
8601 // Check that the vector operands are of the right form.
8602 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8603 // operands, where N is the size of the formed vector.
8604 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8605 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008606
8607 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008608 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008609 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008610 SDValue Vec = N0->getOperand(0)->getOperand(0);
8611 SDNode *V = Vec.getNode();
8612 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008613
Eric Christopher1b8b94192011-06-29 21:10:36 +00008614 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008615 // check to see if each of their operands are an EXTRACT_VECTOR with
8616 // the same vector and appropriate index.
8617 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8618 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8619 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008620
Tanya Lattnere9e67052011-06-14 23:48:48 +00008621 SDValue ExtVec0 = N0->getOperand(i);
8622 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008623
Tanya Lattnere9e67052011-06-14 23:48:48 +00008624 // First operand is the vector, verify its the same.
8625 if (V != ExtVec0->getOperand(0).getNode() ||
8626 V != ExtVec1->getOperand(0).getNode())
8627 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008628
Tanya Lattnere9e67052011-06-14 23:48:48 +00008629 // Second is the constant, verify its correct.
8630 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8631 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008632
Tanya Lattnere9e67052011-06-14 23:48:48 +00008633 // For the constant, we want to see all the even or all the odd.
8634 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8635 || C1->getZExtValue() != nextIndex+1)
8636 return SDValue();
8637
8638 // Increment index.
8639 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008640 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008641 return SDValue();
8642 }
8643
8644 // Create VPADDL node.
8645 SelectionDAG &DAG = DCI.DAG;
8646 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008647
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008648 SDLoc dl(N);
8649
Tanya Lattnere9e67052011-06-14 23:48:48 +00008650 // Build operand list.
8651 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008652 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008653 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008654
8655 // Input is the vector.
8656 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008657
Tanya Lattnere9e67052011-06-14 23:48:48 +00008658 // Get widened type and narrowed type.
8659 MVT widenType;
8660 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00008661
Silviu Barangaa3106e62014-04-03 10:44:27 +00008662 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8663 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008664 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8665 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8666 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8667 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008668 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008669 }
8670
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008671 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008672 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008673 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008674}
8675
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008676static SDValue findMUL_LOHI(SDValue V) {
8677 if (V->getOpcode() == ISD::UMUL_LOHI ||
8678 V->getOpcode() == ISD::SMUL_LOHI)
8679 return V;
8680 return SDValue();
8681}
8682
8683static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8684 TargetLowering::DAGCombinerInfo &DCI,
8685 const ARMSubtarget *Subtarget) {
8686
8687 if (Subtarget->isThumb1Only()) return SDValue();
8688
8689 // Only perform the checks after legalize when the pattern is available.
8690 if (DCI.isBeforeLegalize()) return SDValue();
8691
8692 // Look for multiply add opportunities.
8693 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8694 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8695 // a glue link from the first add to the second add.
8696 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8697 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008698 // UMUL_LOHI
8699 // / :lo \ :hi
8700 // / \ [no multiline comment]
8701 // loAdd -> ADDE |
8702 // \ :glue /
8703 // \ /
8704 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008705 //
8706 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8707 SDValue AddcOp0 = AddcNode->getOperand(0);
8708 SDValue AddcOp1 = AddcNode->getOperand(1);
8709
8710 // Check if the two operands are from the same mul_lohi node.
8711 if (AddcOp0.getNode() == AddcOp1.getNode())
8712 return SDValue();
8713
8714 assert(AddcNode->getNumValues() == 2 &&
8715 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008716 "Expect ADDC with two result values. First: i32");
8717
8718 // Check that we have a glued ADDC node.
8719 if (AddcNode->getValueType(1) != MVT::Glue)
8720 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008721
8722 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8723 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8724 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8725 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8726 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8727 return SDValue();
8728
8729 // Look for the glued ADDE.
8730 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008731 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008732 return SDValue();
8733
8734 // Make sure it is really an ADDE.
8735 if (AddeNode->getOpcode() != ISD::ADDE)
8736 return SDValue();
8737
8738 assert(AddeNode->getNumOperands() == 3 &&
8739 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8740 "ADDE node has the wrong inputs");
8741
8742 // Check for the triangle shape.
8743 SDValue AddeOp0 = AddeNode->getOperand(0);
8744 SDValue AddeOp1 = AddeNode->getOperand(1);
8745
8746 // Make sure that the ADDE operands are not coming from the same node.
8747 if (AddeOp0.getNode() == AddeOp1.getNode())
8748 return SDValue();
8749
8750 // Find the MUL_LOHI node walking up ADDE's operands.
8751 bool IsLeftOperandMUL = false;
8752 SDValue MULOp = findMUL_LOHI(AddeOp0);
8753 if (MULOp == SDValue())
8754 MULOp = findMUL_LOHI(AddeOp1);
8755 else
8756 IsLeftOperandMUL = true;
8757 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008758 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008759
8760 // Figure out the right opcode.
8761 unsigned Opc = MULOp->getOpcode();
8762 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8763
8764 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008765 SDValue* HiAdd = nullptr;
8766 SDValue* LoMul = nullptr;
8767 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008768
Jyoti Allurf1d70502015-01-23 09:10:03 +00008769 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8770 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8771 return SDValue();
8772
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008773 if (IsLeftOperandMUL)
8774 HiAdd = &AddeOp1;
8775 else
8776 HiAdd = &AddeOp0;
8777
8778
Jyoti Allurf1d70502015-01-23 09:10:03 +00008779 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8780 // whose low result is fed to the ADDC we are checking.
8781
8782 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008783 LoMul = &AddcOp0;
8784 LowAdd = &AddcOp1;
8785 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008786 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008787 LoMul = &AddcOp1;
8788 LowAdd = &AddcOp0;
8789 }
8790
Craig Topper062a2ba2014-04-25 05:30:21 +00008791 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008792 return SDValue();
8793
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008794 // Create the merged node.
8795 SelectionDAG &DAG = DCI.DAG;
8796
8797 // Build operand list.
8798 SmallVector<SDValue, 8> Ops;
8799 Ops.push_back(LoMul->getOperand(0));
8800 Ops.push_back(LoMul->getOperand(1));
8801 Ops.push_back(*LowAdd);
8802 Ops.push_back(*HiAdd);
8803
Andrew Trickef9de2a2013-05-25 02:42:55 +00008804 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008805 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008806
8807 // Replace the ADDs' nodes uses by the MLA node's values.
8808 SDValue HiMLALResult(MLALNode.getNode(), 1);
8809 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8810
8811 SDValue LoMLALResult(MLALNode.getNode(), 0);
8812 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8813
8814 // Return original node to notify the driver to stop replacing.
8815 SDValue resNode(AddcNode, 0);
8816 return resNode;
8817}
8818
8819/// PerformADDCCombine - Target-specific dag combine transform from
8820/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8821static SDValue PerformADDCCombine(SDNode *N,
8822 TargetLowering::DAGCombinerInfo &DCI,
8823 const ARMSubtarget *Subtarget) {
8824
8825 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8826
8827}
8828
Bob Wilson728eb292010-07-29 20:34:14 +00008829/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8830/// operands N0 and N1. This is a helper for PerformADDCombine that is
8831/// called with the default operands, and if that fails, with commuted
8832/// operands.
8833static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008834 TargetLowering::DAGCombinerInfo &DCI,
8835 const ARMSubtarget *Subtarget){
8836
8837 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008838 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00008839 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008840
Chris Lattner4147f082009-03-12 06:52:53 +00008841 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008842 if (N0.getNode()->hasOneUse())
8843 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
8844 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00008845 return SDValue();
8846}
8847
Bob Wilson728eb292010-07-29 20:34:14 +00008848/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8849///
8850static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008851 TargetLowering::DAGCombinerInfo &DCI,
8852 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008853 SDValue N0 = N->getOperand(0);
8854 SDValue N1 = N->getOperand(1);
8855
8856 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008857 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00008858 return Result;
8859
8860 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008861 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008862}
8863
Chris Lattner4147f082009-03-12 06:52:53 +00008864/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008865///
Chris Lattner4147f082009-03-12 06:52:53 +00008866static SDValue PerformSUBCombine(SDNode *N,
8867 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008868 SDValue N0 = N->getOperand(0);
8869 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008870
Chris Lattner4147f082009-03-12 06:52:53 +00008871 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008872 if (N1.getNode()->hasOneUse())
8873 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
8874 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00008875
Chris Lattner4147f082009-03-12 06:52:53 +00008876 return SDValue();
8877}
8878
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008879/// PerformVMULCombine
8880/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8881/// special multiplier accumulator forwarding.
8882/// vmul d3, d0, d2
8883/// vmla d3, d1, d2
8884/// is faster than
8885/// vadd d3, d0, d1
8886/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008887// However, for (A + B) * (A + B),
8888// vadd d2, d0, d1
8889// vmul d3, d0, d2
8890// vmla d3, d1, d2
8891// is slower than
8892// vadd d2, d0, d1
8893// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008894static SDValue PerformVMULCombine(SDNode *N,
8895 TargetLowering::DAGCombinerInfo &DCI,
8896 const ARMSubtarget *Subtarget) {
8897 if (!Subtarget->hasVMLxForwarding())
8898 return SDValue();
8899
8900 SelectionDAG &DAG = DCI.DAG;
8901 SDValue N0 = N->getOperand(0);
8902 SDValue N1 = N->getOperand(1);
8903 unsigned Opcode = N0.getOpcode();
8904 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8905 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008906 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008907 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8908 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8909 return SDValue();
8910 std::swap(N0, N1);
8911 }
8912
Weiming Zhao2052f482013-09-25 23:12:06 +00008913 if (N0 == N1)
8914 return SDValue();
8915
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008916 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008917 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008918 SDValue N00 = N0->getOperand(0);
8919 SDValue N01 = N0->getOperand(1);
8920 return DAG.getNode(Opcode, DL, VT,
8921 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8922 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8923}
8924
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008925static SDValue PerformMULCombine(SDNode *N,
8926 TargetLowering::DAGCombinerInfo &DCI,
8927 const ARMSubtarget *Subtarget) {
8928 SelectionDAG &DAG = DCI.DAG;
8929
8930 if (Subtarget->isThumb1Only())
8931 return SDValue();
8932
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008933 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8934 return SDValue();
8935
8936 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008937 if (VT.is64BitVector() || VT.is128BitVector())
8938 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008939 if (VT != MVT::i32)
8940 return SDValue();
8941
8942 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8943 if (!C)
8944 return SDValue();
8945
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008946 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008947 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008948
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008949 ShiftAmt = ShiftAmt & (32 - 1);
8950 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008951 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008952
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008953 SDValue Res;
8954 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008955
8956 if (MulAmt >= 0) {
8957 if (isPowerOf2_32(MulAmt - 1)) {
8958 // (mul x, 2^N + 1) => (add (shl x, N), x)
8959 Res = DAG.getNode(ISD::ADD, DL, VT,
8960 V,
8961 DAG.getNode(ISD::SHL, DL, VT,
8962 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008963 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008964 MVT::i32)));
8965 } else if (isPowerOf2_32(MulAmt + 1)) {
8966 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8967 Res = DAG.getNode(ISD::SUB, DL, VT,
8968 DAG.getNode(ISD::SHL, DL, VT,
8969 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008970 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008971 MVT::i32)),
8972 V);
8973 } else
8974 return SDValue();
8975 } else {
8976 uint64_t MulAmtAbs = -MulAmt;
8977 if (isPowerOf2_32(MulAmtAbs + 1)) {
8978 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8979 Res = DAG.getNode(ISD::SUB, DL, VT,
8980 V,
8981 DAG.getNode(ISD::SHL, DL, VT,
8982 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008983 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008984 MVT::i32)));
8985 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8986 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8987 Res = DAG.getNode(ISD::ADD, DL, VT,
8988 V,
8989 DAG.getNode(ISD::SHL, DL, VT,
8990 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008991 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008992 MVT::i32)));
8993 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008994 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008995
8996 } else
8997 return SDValue();
8998 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008999
9000 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009001 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009002 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009003
9004 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009005 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009006 return SDValue();
9007}
9008
Owen Anderson30c48922010-11-05 19:27:46 +00009009static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00009010 TargetLowering::DAGCombinerInfo &DCI,
9011 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00009012
Owen Anderson30c48922010-11-05 19:27:46 +00009013 // Attempt to use immediate-form VBIC
9014 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009015 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00009016 EVT VT = N->getValueType(0);
9017 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009018
Tanya Lattner266792a2011-04-07 15:24:20 +00009019 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9020 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009021
Owen Anderson30c48922010-11-05 19:27:46 +00009022 APInt SplatBits, SplatUndef;
9023 unsigned SplatBitSize;
9024 bool HasAnyUndefs;
9025 if (BVN &&
9026 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9027 if (SplatBitSize <= 64) {
9028 EVT VbicVT;
9029 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
9030 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009031 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009032 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00009033 if (Val.getNode()) {
9034 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009035 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00009036 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009037 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00009038 }
9039 }
9040 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009041
Evan Chenge87681c2012-02-23 01:19:06 +00009042 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009043 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009044 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009045 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009046 }
9047
Owen Anderson30c48922010-11-05 19:27:46 +00009048 return SDValue();
9049}
9050
Jim Grosbach11013ed2010-07-16 23:05:05 +00009051/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
9052static SDValue PerformORCombine(SDNode *N,
9053 TargetLowering::DAGCombinerInfo &DCI,
9054 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009055 // Attempt to use immediate-form VORR
9056 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009057 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009058 EVT VT = N->getValueType(0);
9059 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009060
Tanya Lattner266792a2011-04-07 15:24:20 +00009061 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9062 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009063
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009064 APInt SplatBits, SplatUndef;
9065 unsigned SplatBitSize;
9066 bool HasAnyUndefs;
9067 if (BVN && Subtarget->hasNEON() &&
9068 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9069 if (SplatBitSize <= 64) {
9070 EVT VorrVT;
9071 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9072 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009073 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009074 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009075 if (Val.getNode()) {
9076 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009077 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009078 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009079 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009080 }
9081 }
9082 }
9083
Evan Chenge87681c2012-02-23 01:19:06 +00009084 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009085 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009086 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009087 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009088 }
9089
Nadav Rotem3a94c542012-08-13 18:52:44 +00009090 // The code below optimizes (or (and X, Y), Z).
9091 // The AND operand needs to have a single user to make these optimizations
9092 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009093 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009094 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009095 return SDValue();
9096 SDValue N1 = N->getOperand(1);
9097
9098 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9099 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9100 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9101 APInt SplatUndef;
9102 unsigned SplatBitSize;
9103 bool HasAnyUndefs;
9104
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009105 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009106 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009107 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9108 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009109 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009110 HasAnyUndefs) && !HasAnyUndefs) {
9111 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9112 HasAnyUndefs) && !HasAnyUndefs) {
9113 // Ensure that the bit width of the constants are the same and that
9114 // the splat arguments are logical inverses as per the pattern we
9115 // are trying to simplify.
9116 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9117 SplatBits0 == ~SplatBits1) {
9118 // Canonicalize the vector type to make instruction selection
9119 // simpler.
9120 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9121 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9122 N0->getOperand(1),
9123 N0->getOperand(0),
9124 N1->getOperand(0));
9125 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9126 }
9127 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009128 }
9129 }
9130
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009131 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9132 // reasonable.
9133
Jim Grosbach11013ed2010-07-16 23:05:05 +00009134 // BFI is only available on V6T2+
9135 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9136 return SDValue();
9137
Andrew Trickef9de2a2013-05-25 02:42:55 +00009138 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009139 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009140 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009141 //
9142 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009143 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009144 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009145 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009146 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009147 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009148
Jim Grosbach11013ed2010-07-16 23:05:05 +00009149 if (VT != MVT::i32)
9150 return SDValue();
9151
Evan Cheng2e51bb42010-12-13 20:32:54 +00009152 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009153
Jim Grosbach11013ed2010-07-16 23:05:05 +00009154 // The value and the mask need to be constants so we can verify this is
9155 // actually a bitfield set. If the mask is 0xffff, we can do better
9156 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009157 SDValue MaskOp = N0.getOperand(1);
9158 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9159 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009160 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009161 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009162 if (Mask == 0xffff)
9163 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009164 SDValue Res;
9165 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009166 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9167 if (N1C) {
9168 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009169 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009170 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009171
Evan Cheng34345752010-12-11 04:11:38 +00009172 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009173 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009174
Evan Cheng2e51bb42010-12-13 20:32:54 +00009175 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009176 DAG.getConstant(Val, DL, MVT::i32),
9177 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009178
9179 // Do not add new nodes to DAG combiner worklist.
9180 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009181 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009182 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009183 } else if (N1.getOpcode() == ISD::AND) {
9184 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009185 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9186 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009187 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009188 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009189
Eric Christopherd5530962011-03-26 01:21:03 +00009190 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9191 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009192 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009193 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009194 // The pack halfword instruction works better for masks that fit it,
9195 // so use that when it's available.
9196 if (Subtarget->hasT2ExtractPack() &&
9197 (Mask == 0xffff || Mask == 0xffff0000))
9198 return SDValue();
9199 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009200 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009201 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009202 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009203 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009204 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009205 // Do not add new nodes to DAG combiner worklist.
9206 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009207 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009208 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009209 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009210 // The pack halfword instruction works better for masks that fit it,
9211 // so use that when it's available.
9212 if (Subtarget->hasT2ExtractPack() &&
9213 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9214 return SDValue();
9215 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009216 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009217 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009218 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009219 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009220 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009221 // Do not add new nodes to DAG combiner worklist.
9222 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009223 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009224 }
9225 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009226
Evan Cheng2e51bb42010-12-13 20:32:54 +00009227 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9228 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9229 ARM::isBitFieldInvertedMask(~Mask)) {
9230 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9231 // where lsb(mask) == #shamt and masked bits of B are known zero.
9232 SDValue ShAmt = N00.getOperand(1);
9233 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009234 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009235 if (ShAmtC != LSB)
9236 return SDValue();
9237
9238 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009239 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009240
9241 // Do not add new nodes to DAG combiner worklist.
9242 DCI.CombineTo(N, Res, false);
9243 }
9244
Jim Grosbach11013ed2010-07-16 23:05:05 +00009245 return SDValue();
9246}
9247
Evan Chenge87681c2012-02-23 01:19:06 +00009248static SDValue PerformXORCombine(SDNode *N,
9249 TargetLowering::DAGCombinerInfo &DCI,
9250 const ARMSubtarget *Subtarget) {
9251 EVT VT = N->getValueType(0);
9252 SelectionDAG &DAG = DCI.DAG;
9253
9254 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9255 return SDValue();
9256
9257 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009258 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009259 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009260 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009261 }
9262
9263 return SDValue();
9264}
9265
James Molloyce12c922015-11-11 15:40:40 +00009266// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9267// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9268// their position in "to" (Rd).
9269static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9270 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009271
James Molloyce12c922015-11-11 15:40:40 +00009272 SDValue From = N->getOperand(1);
9273 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9274 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9275
9276 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9277 // #C in the base of the SHR.
9278 if (From->getOpcode() == ISD::SRL &&
9279 isa<ConstantSDNode>(From->getOperand(1))) {
9280 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9281 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9282 FromMask <<= Shift.getLimitedValue(31);
9283 From = From->getOperand(0);
9284 }
9285
9286 return From;
9287}
9288
9289// If A and B contain one contiguous set of bits, does A | B == A . B?
9290//
9291// Neither A nor B must be zero.
9292static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9293 unsigned LastActiveBitInA = A.countTrailingZeros();
9294 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9295 return LastActiveBitInA - 1 == FirstActiveBitInB;
9296}
9297
9298static SDValue FindBFIToCombineWith(SDNode *N) {
9299 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9300 // if one exists.
9301 APInt ToMask, FromMask;
9302 SDValue From = ParseBFI(N, ToMask, FromMask);
9303 SDValue To = N->getOperand(0);
9304
9305 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9306 // aren't compatible, but not if they set the same bit in their destination as
9307 // we do (or that of any BFI we're going to combine with).
9308 SDValue V = To;
9309 APInt CombinedToMask = ToMask;
9310 while (V.getOpcode() == ARMISD::BFI) {
9311 APInt NewToMask, NewFromMask;
9312 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9313 if (NewFrom != From) {
9314 // This BFI has a different base. Keep going.
9315 CombinedToMask |= NewToMask;
9316 V = V.getOperand(0);
9317 continue;
9318 }
9319
9320 // Do the written bits conflict with any we've seen so far?
9321 if ((NewToMask & CombinedToMask).getBoolValue())
9322 // Conflicting bits - bail out because going further is unsafe.
9323 return SDValue();
9324
9325 // Are the new bits contiguous when combined with the old bits?
9326 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9327 BitsProperlyConcatenate(FromMask, NewFromMask))
9328 return V;
9329 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9330 BitsProperlyConcatenate(NewFromMask, FromMask))
9331 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009332
James Molloyce12c922015-11-11 15:40:40 +00009333 // We've seen a write to some bits, so track it.
9334 CombinedToMask |= NewToMask;
9335 // Keep going...
9336 V = V.getOperand(0);
9337 }
9338
9339 return SDValue();
9340}
9341
Evan Chengc1778132010-12-14 03:22:07 +00009342static SDValue PerformBFICombine(SDNode *N,
9343 TargetLowering::DAGCombinerInfo &DCI) {
9344 SDValue N1 = N->getOperand(1);
9345 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009346 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9347 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009348 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9349 if (!N11C)
9350 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009351 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009352 unsigned LSB = countTrailingZeros(~InvMask);
9353 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009354 assert(Width <
9355 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009356 "undefined behavior");
9357 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009358 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009359 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009360 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009361 N->getOperand(0), N1.getOperand(0),
9362 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009363 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9364 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9365 // Keep track of any consecutive bits set that all come from the same base
9366 // value. We can combine these together into a single BFI.
9367 SDValue CombineBFI = FindBFIToCombineWith(N);
9368 if (CombineBFI == SDValue())
9369 return SDValue();
9370
9371 // We've found a BFI.
9372 APInt ToMask1, FromMask1;
9373 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9374
9375 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009376 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9377 assert(From1 == From2);
9378 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009379
James Molloyce12c922015-11-11 15:40:40 +00009380 // First, unlink CombineBFI.
9381 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9382 // Then create a new BFI, combining the two together.
9383 APInt NewFromMask = FromMask1 | FromMask2;
9384 APInt NewToMask = ToMask1 | ToMask2;
9385
9386 EVT VT = N->getValueType(0);
9387 SDLoc dl(N);
9388
9389 if (NewFromMask[0] == 0)
9390 From1 = DCI.DAG.getNode(
9391 ISD::SRL, dl, VT, From1,
9392 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9393 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9394 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009395 }
9396 return SDValue();
9397}
9398
Bob Wilson22806742010-09-22 22:09:21 +00009399/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9400/// ARMISD::VMOVRRD.
9401static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009402 TargetLowering::DAGCombinerInfo &DCI,
9403 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009404 // vmovrrd(vmovdrr x, y) -> x,y
9405 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009406 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009407 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009408
9409 // vmovrrd(load f64) -> (load i32), (load i32)
9410 SDNode *InNode = InDouble.getNode();
9411 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9412 InNode->getValueType(0) == MVT::f64 &&
9413 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9414 !cast<LoadSDNode>(InNode)->isVolatile()) {
9415 // TODO: Should this be done for non-FrameIndex operands?
9416 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9417
9418 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009419 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009420 SDValue BasePtr = LD->getBasePtr();
9421 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9422 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009423 LD->isNonTemporal(), LD->isInvariant(),
9424 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009425
9426 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009427 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009428 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9429 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009430 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009431 std::min(4U, LD->getAlignment() / 2));
9432
9433 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009434 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009435 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009436 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009437 return Result;
9438 }
9439
Bob Wilson22806742010-09-22 22:09:21 +00009440 return SDValue();
9441}
9442
9443/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9444/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9445static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9446 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9447 SDValue Op0 = N->getOperand(0);
9448 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009449 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009450 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009451 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009452 Op1 = Op1.getOperand(0);
9453 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9454 Op0.getNode() == Op1.getNode() &&
9455 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009456 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009457 N->getValueType(0), Op0.getOperand(0));
9458 return SDValue();
9459}
9460
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009461/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9462/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9463/// i64 vector to have f64 elements, since the value can then be loaded
9464/// directly into a VFP register.
9465static bool hasNormalLoadOperand(SDNode *N) {
9466 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9467 for (unsigned i = 0; i < NumElts; ++i) {
9468 SDNode *Elt = N->getOperand(i).getNode();
9469 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9470 return true;
9471 }
9472 return false;
9473}
9474
Bob Wilsoncb6db982010-09-17 22:59:05 +00009475/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9476/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009477static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009478 TargetLowering::DAGCombinerInfo &DCI,
9479 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009480 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9481 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9482 // into a pair of GPRs, which is fine when the value is used as a scalar,
9483 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009484 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009485 if (N->getNumOperands() == 2)
9486 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009487 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +00009488
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009489 // Load i64 elements as f64 values so that type legalization does not split
9490 // them up into i32 values.
9491 EVT VT = N->getValueType(0);
9492 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9493 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009494 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009495 SmallVector<SDValue, 8> Ops;
9496 unsigned NumElts = VT.getVectorNumElements();
9497 for (unsigned i = 0; i < NumElts; ++i) {
9498 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9499 Ops.push_back(V);
9500 // Make the DAGCombiner fold the bitcast.
9501 DCI.AddToWorklist(V.getNode());
9502 }
9503 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009504 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009505 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9506}
9507
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009508/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9509static SDValue
9510PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9511 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9512 // At that time, we may have inserted bitcasts from integer to float.
9513 // If these bitcasts have survived DAGCombine, change the lowering of this
9514 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9515 // force to use floating point types.
9516
9517 // Make sure we can change the type of the vector.
9518 // This is possible iff:
9519 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9520 // 1.1. Vector is used only once.
9521 // 1.2. Use is a bit convert to an integer type.
9522 // 2. The size of its operands are 32-bits (64-bits are not legal).
9523 EVT VT = N->getValueType(0);
9524 EVT EltVT = VT.getVectorElementType();
9525
9526 // Check 1.1. and 2.
9527 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9528 return SDValue();
9529
9530 // By construction, the input type must be float.
9531 assert(EltVT == MVT::f32 && "Unexpected type!");
9532
9533 // Check 1.2.
9534 SDNode *Use = *N->use_begin();
9535 if (Use->getOpcode() != ISD::BITCAST ||
9536 Use->getValueType(0).isFloatingPoint())
9537 return SDValue();
9538
9539 // Check profitability.
9540 // Model is, if more than half of the relevant operands are bitcast from
9541 // i32, turn the build_vector into a sequence of insert_vector_elt.
9542 // Relevant operands are everything that is not statically
9543 // (i.e., at compile time) bitcasted.
9544 unsigned NumOfBitCastedElts = 0;
9545 unsigned NumElts = VT.getVectorNumElements();
9546 unsigned NumOfRelevantElts = NumElts;
9547 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9548 SDValue Elt = N->getOperand(Idx);
9549 if (Elt->getOpcode() == ISD::BITCAST) {
9550 // Assume only bit cast to i32 will go away.
9551 if (Elt->getOperand(0).getValueType() == MVT::i32)
9552 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +00009553 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009554 // Constants are statically casted, thus do not count them as
9555 // relevant operands.
9556 --NumOfRelevantElts;
9557 }
9558
9559 // Check if more than half of the elements require a non-free bitcast.
9560 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9561 return SDValue();
9562
9563 SelectionDAG &DAG = DCI.DAG;
9564 // Create the new vector type.
9565 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9566 // Check if the type is legal.
9567 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9568 if (!TLI.isTypeLegal(VecVT))
9569 return SDValue();
9570
9571 // Combine:
9572 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9573 // => BITCAST INSERT_VECTOR_ELT
9574 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9575 // (BITCAST EN), N.
9576 SDValue Vec = DAG.getUNDEF(VecVT);
9577 SDLoc dl(N);
9578 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9579 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +00009580 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009581 continue;
9582 if (V.getOpcode() == ISD::BITCAST &&
9583 V->getOperand(0).getValueType() == MVT::i32)
9584 // Fold obvious case.
9585 V = V.getOperand(0);
9586 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009587 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009588 // Make the DAGCombiner fold the bitcasts.
9589 DCI.AddToWorklist(V.getNode());
9590 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009591 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009592 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9593 }
9594 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9595 // Make the DAGCombiner fold the bitcasts.
9596 DCI.AddToWorklist(Vec.getNode());
9597 return Vec;
9598}
9599
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009600/// PerformInsertEltCombine - Target-specific dag combine xforms for
9601/// ISD::INSERT_VECTOR_ELT.
9602static SDValue PerformInsertEltCombine(SDNode *N,
9603 TargetLowering::DAGCombinerInfo &DCI) {
9604 // Bitcast an i64 load inserted into a vector to f64.
9605 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9606 EVT VT = N->getValueType(0);
9607 SDNode *Elt = N->getOperand(1).getNode();
9608 if (VT.getVectorElementType() != MVT::i64 ||
9609 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9610 return SDValue();
9611
9612 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009613 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009614 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9615 VT.getVectorNumElements());
9616 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9617 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9618 // Make the DAGCombiner fold the bitcasts.
9619 DCI.AddToWorklist(Vec.getNode());
9620 DCI.AddToWorklist(V.getNode());
9621 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9622 Vec, V, N->getOperand(2));
9623 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009624}
9625
Bob Wilsonc7334a12010-10-27 20:38:28 +00009626/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9627/// ISD::VECTOR_SHUFFLE.
9628static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9629 // The LLVM shufflevector instruction does not require the shuffle mask
9630 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9631 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9632 // operands do not match the mask length, they are extended by concatenating
9633 // them with undef vectors. That is probably the right thing for other
9634 // targets, but for NEON it is better to concatenate two double-register
9635 // size vector operands into a single quad-register size vector. Do that
9636 // transformation here:
9637 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9638 // shuffle(concat(v1, v2), undef)
9639 SDValue Op0 = N->getOperand(0);
9640 SDValue Op1 = N->getOperand(1);
9641 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9642 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9643 Op0.getNumOperands() != 2 ||
9644 Op1.getNumOperands() != 2)
9645 return SDValue();
9646 SDValue Concat0Op1 = Op0.getOperand(1);
9647 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00009648 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +00009649 return SDValue();
9650 // Skip the transformation if any of the types are illegal.
9651 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9652 EVT VT = N->getValueType(0);
9653 if (!TLI.isTypeLegal(VT) ||
9654 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9655 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9656 return SDValue();
9657
Andrew Trickef9de2a2013-05-25 02:42:55 +00009658 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009659 Op0.getOperand(0), Op1.getOperand(0));
9660 // Translate the shuffle mask.
9661 SmallVector<int, 16> NewMask;
9662 unsigned NumElts = VT.getVectorNumElements();
9663 unsigned HalfElts = NumElts/2;
9664 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9665 for (unsigned n = 0; n < NumElts; ++n) {
9666 int MaskElt = SVN->getMaskElt(n);
9667 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009668 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009669 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009670 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009671 NewElt = HalfElts + MaskElt - NumElts;
9672 NewMask.push_back(NewElt);
9673 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009674 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009675 DAG.getUNDEF(VT), NewMask.data());
9676}
9677
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009678/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9679/// NEON load/store intrinsics, and generic vector load/stores, to merge
9680/// base address updates.
9681/// For generic load/stores, the memory type is assumed to be a vector.
9682/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009683static SDValue CombineBaseUpdate(SDNode *N,
9684 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009685 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009686 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9687 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009688 const bool isStore = N->getOpcode() == ISD::STORE;
9689 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009690 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009691 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009692 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009693
9694 // Search for a use of the address operand that is an increment.
9695 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9696 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9697 SDNode *User = *UI;
9698 if (User->getOpcode() != ISD::ADD ||
9699 UI.getUse().getResNo() != Addr.getResNo())
9700 continue;
9701
9702 // Check that the add is independent of the load/store. Otherwise, folding
9703 // it would create a cycle.
9704 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9705 continue;
9706
9707 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009708 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009709 bool isLaneOp = false;
9710 unsigned NewOpc = 0;
9711 unsigned NumVecs = 0;
9712 if (isIntrinsic) {
9713 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9714 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009715 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009716 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9717 NumVecs = 1; break;
9718 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9719 NumVecs = 2; break;
9720 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9721 NumVecs = 3; break;
9722 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9723 NumVecs = 4; break;
9724 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9725 NumVecs = 2; isLaneOp = true; break;
9726 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9727 NumVecs = 3; isLaneOp = true; break;
9728 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9729 NumVecs = 4; isLaneOp = true; break;
9730 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009731 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009732 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009733 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009734 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009735 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009736 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009737 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009738 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009739 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009740 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009741 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009742 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009743 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009744 }
9745 } else {
9746 isLaneOp = true;
9747 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009748 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009749 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9750 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9751 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009752 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9753 NumVecs = 1; isLaneOp = false; break;
9754 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9755 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009756 }
9757 }
9758
9759 // Find the size of memory referenced by the load/store.
9760 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009761 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009762 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009763 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009764 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009765 } else {
9766 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9767 VecTy = N->getOperand(1).getValueType();
9768 }
9769
Bob Wilson06fce872011-02-07 17:43:21 +00009770 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9771 if (isLaneOp)
9772 NumBytes /= VecTy.getVectorNumElements();
9773
9774 // If the increment is a constant, it must match the memory ref size.
9775 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9776 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9777 uint64_t IncVal = CInc->getZExtValue();
9778 if (IncVal != NumBytes)
9779 continue;
9780 } else if (NumBytes >= 3 * 16) {
9781 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9782 // separate instructions that make it harder to use a non-constant update.
9783 continue;
9784 }
9785
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009786 // OK, we found an ADD we can fold into the base update.
9787 // Now, create a _UPD node, taking care of not breaking alignment.
9788
9789 EVT AlignedVecTy = VecTy;
9790 unsigned Alignment = MemN->getAlignment();
9791
9792 // If this is a less-than-standard-aligned load/store, change the type to
9793 // match the standard alignment.
9794 // The alignment is overlooked when selecting _UPD variants; and it's
9795 // easier to introduce bitcasts here than fix that.
9796 // There are 3 ways to get to this base-update combine:
9797 // - intrinsics: they are assumed to be properly aligned (to the standard
9798 // alignment of the memory type), so we don't need to do anything.
9799 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9800 // intrinsics, so, likewise, there's nothing to do.
9801 // - generic load/store instructions: the alignment is specified as an
9802 // explicit operand, rather than implicitly as the standard alignment
9803 // of the memory type (like the intrisics). We need to change the
9804 // memory type to match the explicit alignment. That way, we don't
9805 // generate non-standard-aligned ARMISD::VLDx nodes.
9806 if (isa<LSBaseSDNode>(N)) {
9807 if (Alignment == 0)
9808 Alignment = 1;
9809 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9810 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9811 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9812 assert(!isLaneOp && "Unexpected generic load/store lane.");
9813 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9814 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9815 }
9816 // Don't set an explicit alignment on regular load/stores that we want
9817 // to transform to VLD/VST 1_UPD nodes.
9818 // This matches the behavior of regular load/stores, which only get an
9819 // explicit alignment if the MMO alignment is larger than the standard
9820 // alignment of the memory type.
9821 // Intrinsics, however, always get an explicit alignment, set to the
9822 // alignment of the MMO.
9823 Alignment = 1;
9824 }
9825
Bob Wilson06fce872011-02-07 17:43:21 +00009826 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009827 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009828 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009829 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009830 unsigned n;
9831 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009832 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009833 Tys[n++] = MVT::i32;
9834 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009835 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009836
9837 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009838 SmallVector<SDValue, 8> Ops;
9839 Ops.push_back(N->getOperand(0)); // incoming chain
9840 Ops.push_back(N->getOperand(AddrOpIdx));
9841 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009842
9843 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9844 // Try to match the intrinsic's signature
9845 Ops.push_back(StN->getValue());
9846 } else {
9847 // Loads (and of course intrinsics) match the intrinsics' signature,
9848 // so just add all but the alignment operand.
9849 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9850 Ops.push_back(N->getOperand(i));
9851 }
9852
9853 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009854 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009855
9856 // If this is a non-standard-aligned STORE, the penultimate operand is the
9857 // stored value. Bitcast it to the aligned type.
9858 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9859 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009860 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009861 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009862
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009863 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009864 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009865 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009866
9867 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009868 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009869 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009870 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009871
9872 // If this is an non-standard-aligned LOAD, the first result is the loaded
9873 // value. Bitcast it to the expected result type.
9874 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9875 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009876 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009877 }
9878
Bob Wilson06fce872011-02-07 17:43:21 +00009879 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9880 DCI.CombineTo(N, NewResults);
9881 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9882
9883 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009884 }
Bob Wilson06fce872011-02-07 17:43:21 +00009885 return SDValue();
9886}
9887
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009888static SDValue PerformVLDCombine(SDNode *N,
9889 TargetLowering::DAGCombinerInfo &DCI) {
9890 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9891 return SDValue();
9892
9893 return CombineBaseUpdate(N, DCI);
9894}
9895
Bob Wilson2d790df2010-11-28 06:51:26 +00009896/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9897/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9898/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9899/// return true.
9900static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9901 SelectionDAG &DAG = DCI.DAG;
9902 EVT VT = N->getValueType(0);
9903 // vldN-dup instructions only support 64-bit vectors for N > 1.
9904 if (!VT.is64BitVector())
9905 return false;
9906
9907 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9908 SDNode *VLD = N->getOperand(0).getNode();
9909 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9910 return false;
9911 unsigned NumVecs = 0;
9912 unsigned NewOpc = 0;
9913 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9914 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9915 NumVecs = 2;
9916 NewOpc = ARMISD::VLD2DUP;
9917 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9918 NumVecs = 3;
9919 NewOpc = ARMISD::VLD3DUP;
9920 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9921 NumVecs = 4;
9922 NewOpc = ARMISD::VLD4DUP;
9923 } else {
9924 return false;
9925 }
9926
9927 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9928 // numbers match the load.
9929 unsigned VLDLaneNo =
9930 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9931 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9932 UI != UE; ++UI) {
9933 // Ignore uses of the chain result.
9934 if (UI.getUse().getResNo() == NumVecs)
9935 continue;
9936 SDNode *User = *UI;
9937 if (User->getOpcode() != ARMISD::VDUPLANE ||
9938 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9939 return false;
9940 }
9941
9942 // Create the vldN-dup node.
9943 EVT Tys[5];
9944 unsigned n;
9945 for (n = 0; n < NumVecs; ++n)
9946 Tys[n] = VT;
9947 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009948 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009949 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9950 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009951 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009952 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009953 VLDMemInt->getMemOperand());
9954
9955 // Update the uses.
9956 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9957 UI != UE; ++UI) {
9958 unsigned ResNo = UI.getUse().getResNo();
9959 // Ignore uses of the chain result.
9960 if (ResNo == NumVecs)
9961 continue;
9962 SDNode *User = *UI;
9963 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9964 }
9965
9966 // Now the vldN-lane intrinsic is dead except for its chain result.
9967 // Update uses of the chain.
9968 std::vector<SDValue> VLDDupResults;
9969 for (unsigned n = 0; n < NumVecs; ++n)
9970 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9971 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9972 DCI.CombineTo(VLD, VLDDupResults);
9973
9974 return true;
9975}
9976
Bob Wilson103a0dc2010-07-14 01:22:12 +00009977/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9978/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009979static SDValue PerformVDUPLANECombine(SDNode *N,
9980 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009981 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009982
Bob Wilson2d790df2010-11-28 06:51:26 +00009983 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9984 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9985 if (CombineVLDDUP(N, DCI))
9986 return SDValue(N, 0);
9987
9988 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9989 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009990 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009991 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009992 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009993 return SDValue();
9994
9995 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9996 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9997 // The canonical VMOV for a zero vector uses a 32-bit element size.
9998 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9999 unsigned EltBits;
10000 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
10001 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +000010002 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010003 if (EltSize > VT.getVectorElementType().getSizeInBits())
10004 return SDValue();
10005
Andrew Trickef9de2a2013-05-25 02:42:55 +000010006 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010007}
10008
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010009static SDValue PerformLOADCombine(SDNode *N,
10010 TargetLowering::DAGCombinerInfo &DCI) {
10011 EVT VT = N->getValueType(0);
10012
10013 // If this is a legal vector load, try to combine it into a VLD1_UPD.
10014 if (ISD::isNormalLoad(N) && VT.isVector() &&
10015 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10016 return CombineBaseUpdate(N, DCI);
10017
10018 return SDValue();
10019}
10020
Ahmed Bougacha23167462014-12-09 21:26:53 +000010021/// PerformSTORECombine - Target-specific dag combine xforms for
10022/// ISD::STORE.
10023static SDValue PerformSTORECombine(SDNode *N,
10024 TargetLowering::DAGCombinerInfo &DCI) {
10025 StoreSDNode *St = cast<StoreSDNode>(N);
10026 if (St->isVolatile())
10027 return SDValue();
10028
10029 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
10030 // pack all of the elements in one place. Next, store to memory in fewer
10031 // chunks.
10032 SDValue StVal = St->getValue();
10033 EVT VT = StVal.getValueType();
10034 if (St->isTruncatingStore() && VT.isVector()) {
10035 SelectionDAG &DAG = DCI.DAG;
10036 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10037 EVT StVT = St->getMemoryVT();
10038 unsigned NumElems = VT.getVectorNumElements();
10039 assert(StVT != VT && "Cannot truncate to the same type");
10040 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
10041 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
10042
10043 // From, To sizes and ElemCount must be pow of two
10044 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
10045
10046 // We are going to use the original vector elt for storing.
10047 // Accumulated smaller vector elements must be a multiple of the store size.
10048 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
10049
10050 unsigned SizeRatio = FromEltSz / ToEltSz;
10051 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
10052
10053 // Create a type on which we perform the shuffle.
10054 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
10055 NumElems*SizeRatio);
10056 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
10057
10058 SDLoc DL(St);
10059 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
10060 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
10061 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +000010062 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
10063 ? (i + 1) * SizeRatio - 1
10064 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +000010065
10066 // Can't shuffle using an illegal type.
10067 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10068
10069 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10070 DAG.getUNDEF(WideVec.getValueType()),
10071 ShuffleVec.data());
10072 // At this point all of the data is stored at the bottom of the
10073 // register. We now need to save it to mem.
10074
10075 // Find the largest store unit
10076 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010077 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010078 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10079 StoreType = Tp;
10080 }
10081 // Didn't find a legal store type.
10082 if (!TLI.isTypeLegal(StoreType))
10083 return SDValue();
10084
10085 // Bitcast the original vector into a vector of store-size units
10086 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10087 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10088 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10089 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10090 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010091 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10092 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010093 SDValue BasePtr = St->getBasePtr();
10094
10095 // Perform one or more big stores into memory.
10096 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10097 for (unsigned I = 0; I < E; I++) {
10098 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10099 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010100 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010101 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
10102 St->getPointerInfo(), St->isVolatile(),
10103 St->isNonTemporal(), St->getAlignment());
10104 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10105 Increment);
10106 Chains.push_back(Ch);
10107 }
10108 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10109 }
10110
10111 if (!ISD::isNormalStore(St))
10112 return SDValue();
10113
10114 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10115 // ARM stores of arguments in the same cache line.
10116 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10117 StVal.getNode()->hasOneUse()) {
10118 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010119 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010120 SDLoc DL(St);
10121 SDValue BasePtr = St->getBasePtr();
10122 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
10123 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
10124 BasePtr, St->getPointerInfo(), St->isVolatile(),
10125 St->isNonTemporal(), St->getAlignment());
10126
10127 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010128 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010129 return DAG.getStore(NewST1.getValue(0), DL,
10130 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
10131 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
10132 St->isNonTemporal(),
10133 std::min(4U, St->getAlignment() / 2));
10134 }
10135
10136 if (StVal.getValueType() == MVT::i64 &&
10137 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10138
10139 // Bitcast an i64 store extracted from a vector to f64.
10140 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10141 SelectionDAG &DAG = DCI.DAG;
10142 SDLoc dl(StVal);
10143 SDValue IntVec = StVal.getOperand(0);
10144 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10145 IntVec.getValueType().getVectorNumElements());
10146 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10147 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10148 Vec, StVal.getOperand(1));
10149 dl = SDLoc(N);
10150 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10151 // Make the DAGCombiner fold the bitcasts.
10152 DCI.AddToWorklist(Vec.getNode());
10153 DCI.AddToWorklist(ExtElt.getNode());
10154 DCI.AddToWorklist(V.getNode());
10155 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
10156 St->getPointerInfo(), St->isVolatile(),
10157 St->isNonTemporal(), St->getAlignment(),
10158 St->getAAInfo());
10159 }
10160
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010161 // If this is a legal vector store, try to combine it into a VST1_UPD.
10162 if (ISD::isNormalStore(N) && VT.isVector() &&
10163 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10164 return CombineBaseUpdate(N, DCI);
10165
Ahmed Bougacha23167462014-12-09 21:26:53 +000010166 return SDValue();
10167}
10168
Chad Rosierfa8d8932011-06-24 19:23:04 +000010169/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10170/// can replace combinations of VMUL and VCVT (floating-point to integer)
10171/// when the VMUL has a constant operand that is a power of 2.
10172///
10173/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10174/// vmul.f32 d16, d17, d16
10175/// vcvt.s32.f32 d16, d16
10176/// becomes:
10177/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010178static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010179 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010180 if (!Subtarget->hasNEON())
10181 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010182
Chad Rosiera087fd22015-10-06 20:23:42 +000010183 SDValue Op = N->getOperand(0);
Tim Northover498c56c2016-03-17 20:10:28 +000010184 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
10185 Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010186 return SDValue();
10187
Chad Rosierfa8d8932011-06-24 19:23:04 +000010188 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010189 if (!isa<BuildVectorSDNode>(ConstVec))
10190 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010191
Tim Northover7cbc2152013-06-28 15:29:25 +000010192 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010193 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010194 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010195 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010196 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010197 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010198 // These instructions only exist converting from f32 to i32. We can handle
10199 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010200 // be lossy. We also can't handle more then 4 lanes, since these intructions
10201 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010202 return SDValue();
10203 }
10204
Chad Rosier169865f2015-10-07 17:28:58 +000010205 BitVector UndefElements;
10206 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10207 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10208 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010209 return SDValue();
10210
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010211 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010212 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010213 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10214 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010215 SDValue FixConv = DAG.getNode(
10216 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10217 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010218 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010219
Chad Rosier9df4aff2015-10-06 20:45:45 +000010220 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010221 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010222
10223 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010224}
10225
10226/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10227/// can replace combinations of VCVT (integer to floating-point) and VDIV
10228/// when the VDIV has a constant operand that is a power of 2.
10229///
10230/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10231/// vcvt.f32.s32 d16, d16
10232/// vdiv.f32 d16, d17, d16
10233/// becomes:
10234/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010235static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010236 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010237 if (!Subtarget->hasNEON())
10238 return SDValue();
10239
Chad Rosierfa8d8932011-06-24 19:23:04 +000010240 SDValue Op = N->getOperand(0);
10241 unsigned OpOpcode = Op.getNode()->getOpcode();
Tim Northover498c56c2016-03-17 20:10:28 +000010242 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010243 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10244 return SDValue();
10245
Chad Rosierfa8d8932011-06-24 19:23:04 +000010246 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010247 if (!isa<BuildVectorSDNode>(ConstVec))
10248 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010249
Tim Northover7cbc2152013-06-28 15:29:25 +000010250 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010251 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010252 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010253 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010254 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10255 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010256 // These instructions only exist converting from i32 to f32. We can handle
10257 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010258 // be lossy. We also can't handle more then 4 lanes, since these intructions
10259 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010260 return SDValue();
10261 }
10262
Chad Rosier169865f2015-10-07 17:28:58 +000010263 BitVector UndefElements;
10264 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10265 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10266 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010267 return SDValue();
10268
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010269 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010270 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010271 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010272 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010273 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010274 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010275 ConvInput);
10276
Eric Christopher1b8b94192011-06-29 21:10:36 +000010277 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010278 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010279 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010280 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010281 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010282 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010283}
10284
10285/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010286/// operand of a vector shift operation, where all the elements of the
10287/// build_vector must have the same constant integer value.
10288static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10289 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010290 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010291 Op = Op.getOperand(0);
10292 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10293 APInt SplatBits, SplatUndef;
10294 unsigned SplatBitSize;
10295 bool HasAnyUndefs;
10296 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10297 HasAnyUndefs, ElementBits) ||
10298 SplatBitSize > ElementBits)
10299 return false;
10300 Cnt = SplatBits.getSExtValue();
10301 return true;
10302}
10303
10304/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10305/// operand of a vector shift left operation. That value must be in the range:
10306/// 0 <= Value < ElementBits for a left shift; or
10307/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010308static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010309 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010310 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010311 if (! getVShiftImm(Op, ElementBits, Cnt))
10312 return false;
10313 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10314}
10315
10316/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10317/// operand of a vector shift right operation. For a shift opcode, the value
10318/// is positive, but for an intrinsic the value count must be negative. The
10319/// absolute value must be in the range:
10320/// 1 <= |Value| <= ElementBits for a right shift; or
10321/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010322static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010323 int64_t &Cnt) {
10324 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010325 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010326 if (! getVShiftImm(Op, ElementBits, Cnt))
10327 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010328 if (!isIntrinsic)
10329 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10330 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010331 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010332 return true;
10333 }
10334 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010335}
10336
10337/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10338static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10339 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10340 switch (IntNo) {
10341 default:
10342 // Don't do anything for most intrinsics.
10343 break;
10344
10345 // Vector shifts: check for immediate versions and lower them.
10346 // Note: This is done during DAG combining instead of DAG legalizing because
10347 // the build_vectors for 64-bit vector element shift counts are generally
10348 // not legal, and it is hard to see their values after they get legalized to
10349 // loads from a constant pool.
10350 case Intrinsic::arm_neon_vshifts:
10351 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010352 case Intrinsic::arm_neon_vrshifts:
10353 case Intrinsic::arm_neon_vrshiftu:
10354 case Intrinsic::arm_neon_vrshiftn:
10355 case Intrinsic::arm_neon_vqshifts:
10356 case Intrinsic::arm_neon_vqshiftu:
10357 case Intrinsic::arm_neon_vqshiftsu:
10358 case Intrinsic::arm_neon_vqshiftns:
10359 case Intrinsic::arm_neon_vqshiftnu:
10360 case Intrinsic::arm_neon_vqshiftnsu:
10361 case Intrinsic::arm_neon_vqrshiftns:
10362 case Intrinsic::arm_neon_vqrshiftnu:
10363 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010364 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010365 int64_t Cnt;
10366 unsigned VShiftOpc = 0;
10367
10368 switch (IntNo) {
10369 case Intrinsic::arm_neon_vshifts:
10370 case Intrinsic::arm_neon_vshiftu:
10371 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10372 VShiftOpc = ARMISD::VSHL;
10373 break;
10374 }
10375 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10376 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10377 ARMISD::VSHRs : ARMISD::VSHRu);
10378 break;
10379 }
10380 return SDValue();
10381
Bob Wilson2e076c42009-06-22 23:27:02 +000010382 case Intrinsic::arm_neon_vrshifts:
10383 case Intrinsic::arm_neon_vrshiftu:
10384 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10385 break;
10386 return SDValue();
10387
10388 case Intrinsic::arm_neon_vqshifts:
10389 case Intrinsic::arm_neon_vqshiftu:
10390 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10391 break;
10392 return SDValue();
10393
10394 case Intrinsic::arm_neon_vqshiftsu:
10395 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10396 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010397 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010398
Bob Wilson2e076c42009-06-22 23:27:02 +000010399 case Intrinsic::arm_neon_vrshiftn:
10400 case Intrinsic::arm_neon_vqshiftns:
10401 case Intrinsic::arm_neon_vqshiftnu:
10402 case Intrinsic::arm_neon_vqshiftnsu:
10403 case Intrinsic::arm_neon_vqrshiftns:
10404 case Intrinsic::arm_neon_vqrshiftnu:
10405 case Intrinsic::arm_neon_vqrshiftnsu:
10406 // Narrowing shifts require an immediate right shift.
10407 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10408 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010409 llvm_unreachable("invalid shift count for narrowing vector shift "
10410 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010411
10412 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010413 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010414 }
10415
10416 switch (IntNo) {
10417 case Intrinsic::arm_neon_vshifts:
10418 case Intrinsic::arm_neon_vshiftu:
10419 // Opcode already set above.
10420 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010421 case Intrinsic::arm_neon_vrshifts:
10422 VShiftOpc = ARMISD::VRSHRs; break;
10423 case Intrinsic::arm_neon_vrshiftu:
10424 VShiftOpc = ARMISD::VRSHRu; break;
10425 case Intrinsic::arm_neon_vrshiftn:
10426 VShiftOpc = ARMISD::VRSHRN; break;
10427 case Intrinsic::arm_neon_vqshifts:
10428 VShiftOpc = ARMISD::VQSHLs; break;
10429 case Intrinsic::arm_neon_vqshiftu:
10430 VShiftOpc = ARMISD::VQSHLu; break;
10431 case Intrinsic::arm_neon_vqshiftsu:
10432 VShiftOpc = ARMISD::VQSHLsu; break;
10433 case Intrinsic::arm_neon_vqshiftns:
10434 VShiftOpc = ARMISD::VQSHRNs; break;
10435 case Intrinsic::arm_neon_vqshiftnu:
10436 VShiftOpc = ARMISD::VQSHRNu; break;
10437 case Intrinsic::arm_neon_vqshiftnsu:
10438 VShiftOpc = ARMISD::VQSHRNsu; break;
10439 case Intrinsic::arm_neon_vqrshiftns:
10440 VShiftOpc = ARMISD::VQRSHRNs; break;
10441 case Intrinsic::arm_neon_vqrshiftnu:
10442 VShiftOpc = ARMISD::VQRSHRNu; break;
10443 case Intrinsic::arm_neon_vqrshiftnsu:
10444 VShiftOpc = ARMISD::VQRSHRNsu; break;
10445 }
10446
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010447 SDLoc dl(N);
10448 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10449 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010450 }
10451
10452 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010453 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010454 int64_t Cnt;
10455 unsigned VShiftOpc = 0;
10456
10457 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10458 VShiftOpc = ARMISD::VSLI;
10459 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10460 VShiftOpc = ARMISD::VSRI;
10461 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010462 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010463 }
10464
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010465 SDLoc dl(N);
10466 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010467 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010468 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010469 }
10470
10471 case Intrinsic::arm_neon_vqrshifts:
10472 case Intrinsic::arm_neon_vqrshiftu:
10473 // No immediate versions of these to check for.
10474 break;
10475 }
10476
10477 return SDValue();
10478}
10479
10480/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10481/// lowers them. As with the vector shift intrinsics, this is done during DAG
10482/// combining instead of DAG legalizing because the build_vectors for 64-bit
10483/// vector element shift counts are generally not legal, and it is hard to see
10484/// their values after they get legalized to loads from a constant pool.
10485static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10486 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010487 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010488 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10489 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10490 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10491 SDValue N1 = N->getOperand(1);
10492 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10493 SDValue N0 = N->getOperand(0);
10494 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10495 DAG.MaskedValueIsZero(N0.getOperand(0),
10496 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010497 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010498 }
10499 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010500
10501 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010502 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10503 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010504 return SDValue();
10505
10506 assert(ST->hasNEON() && "unexpected vector shift");
10507 int64_t Cnt;
10508
10509 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010510 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010511
10512 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010513 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10514 SDLoc dl(N);
10515 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10516 DAG.getConstant(Cnt, dl, MVT::i32));
10517 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010518 break;
10519
10520 case ISD::SRA:
10521 case ISD::SRL:
10522 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10523 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10524 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010525 SDLoc dl(N);
10526 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10527 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010528 }
10529 }
10530 return SDValue();
10531}
10532
10533/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10534/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10535static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10536 const ARMSubtarget *ST) {
10537 SDValue N0 = N->getOperand(0);
10538
10539 // Check for sign- and zero-extensions of vector extract operations of 8-
10540 // and 16-bit vector elements. NEON supports these directly. They are
10541 // handled during DAG combining because type legalization will promote them
10542 // to 32-bit types and it is messy to recognize the operations after that.
10543 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10544 SDValue Vec = N0.getOperand(0);
10545 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010546 EVT VT = N->getValueType(0);
10547 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010548 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10549
Owen Anderson9f944592009-08-11 20:47:22 +000010550 if (VT == MVT::i32 &&
10551 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010552 TLI.isTypeLegal(Vec.getValueType()) &&
10553 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010554
10555 unsigned Opc = 0;
10556 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010557 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010558 case ISD::SIGN_EXTEND:
10559 Opc = ARMISD::VGETLANEs;
10560 break;
10561 case ISD::ZERO_EXTEND:
10562 case ISD::ANY_EXTEND:
10563 Opc = ARMISD::VGETLANEu;
10564 break;
10565 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010566 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010567 }
10568 }
10569
10570 return SDValue();
10571}
10572
James Molloy9d55f192015-11-10 14:22:05 +000010573static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10574 APInt &KnownOne) {
10575 if (Op.getOpcode() == ARMISD::BFI) {
10576 // Conservatively, we can recurse down the first operand
10577 // and just mask out all affected bits.
10578 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10579
10580 // The operand to BFI is already a mask suitable for removing the bits it
10581 // sets.
10582 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
10583 APInt Mask = CI->getAPIntValue();
10584 KnownZero &= Mask;
10585 KnownOne &= Mask;
10586 return;
10587 }
10588 if (Op.getOpcode() == ARMISD::CMOV) {
10589 APInt KZ2(KnownZero.getBitWidth(), 0);
10590 APInt KO2(KnownOne.getBitWidth(), 0);
10591 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10592 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10593
10594 KnownZero &= KZ2;
10595 KnownOne &= KO2;
10596 return;
10597 }
10598 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10599}
10600
10601SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10602 // If we have a CMOV, OR and AND combination such as:
10603 // if (x & CN)
10604 // y |= CM;
10605 //
10606 // And:
10607 // * CN is a single bit;
10608 // * All bits covered by CM are known zero in y
10609 //
10610 // Then we can convert this into a sequence of BFI instructions. This will
10611 // always be a win if CM is a single bit, will always be no worse than the
10612 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10613 // three bits (due to the extra IT instruction).
10614
10615 SDValue Op0 = CMOV->getOperand(0);
10616 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010617 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10618 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010619 SDValue CmpZ = CMOV->getOperand(4);
10620
James Molloy20180912015-11-16 10:49:25 +000010621 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010622 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010623 return SDValue();
10624
James Molloy9d55f192015-11-10 14:22:05 +000010625 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10626 SDValue And = CmpZ->getOperand(0);
10627 if (And->getOpcode() != ISD::AND)
10628 return SDValue();
10629 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10630 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10631 return SDValue();
10632 SDValue X = And->getOperand(0);
10633
James Molloy8e99e972015-11-12 13:49:17 +000010634 if (CC == ARMCC::EQ) {
10635 // We're performing an "equal to zero" compare. Swap the operands so we
10636 // canonicalize on a "not equal to zero" compare.
10637 std::swap(Op0, Op1);
10638 } else {
10639 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10640 }
Junmo Park1108ab02016-02-19 01:46:04 +000010641
James Molloy9d55f192015-11-10 14:22:05 +000010642 if (Op1->getOpcode() != ISD::OR)
10643 return SDValue();
10644
10645 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10646 if (!OrC)
10647 return SDValue();
10648 SDValue Y = Op1->getOperand(0);
10649
10650 if (Op0 != Y)
10651 return SDValue();
10652
10653 // Now, is it profitable to continue?
10654 APInt OrCI = OrC->getAPIntValue();
10655 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10656 if (OrCI.countPopulation() > Heuristic)
10657 return SDValue();
10658
10659 // Lastly, can we determine that the bits defined by OrCI
10660 // are zero in Y?
10661 APInt KnownZero, KnownOne;
10662 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10663 if ((OrCI & KnownZero) != OrCI)
10664 return SDValue();
10665
10666 // OK, we can do the combine.
10667 SDValue V = Y;
10668 SDLoc dl(X);
10669 EVT VT = X.getValueType();
10670 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000010671
James Molloy9d55f192015-11-10 14:22:05 +000010672 if (BitInX != 0) {
10673 // We must shift X first.
10674 X = DAG.getNode(ISD::SRL, dl, VT, X,
10675 DAG.getConstant(BitInX, dl, VT));
10676 }
10677
10678 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10679 BitInY < NumActiveBits; ++BitInY) {
10680 if (OrCI[BitInY] == 0)
10681 continue;
10682 APInt Mask(VT.getSizeInBits(), 0);
10683 Mask.setBit(BitInY);
10684 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10685 // Confusingly, the operand is an *inverted* mask.
10686 DAG.getConstant(~Mask, dl, VT));
10687 }
10688
10689 return V;
10690}
10691
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010692/// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND.
10693SDValue
10694ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const {
10695 SDValue Cmp = N->getOperand(4);
10696 if (Cmp.getOpcode() != ARMISD::CMPZ)
10697 // Only looking at NE cases.
10698 return SDValue();
10699
10700 EVT VT = N->getValueType(0);
10701 SDLoc dl(N);
10702 SDValue LHS = Cmp.getOperand(0);
10703 SDValue RHS = Cmp.getOperand(1);
10704 SDValue Chain = N->getOperand(0);
10705 SDValue BB = N->getOperand(1);
10706 SDValue ARMcc = N->getOperand(2);
10707 ARMCC::CondCodes CC =
10708 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
10709
10710 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0))
10711 // -> (brcond Chain BB CC CPSR Cmp)
10712 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() &&
10713 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV &&
10714 LHS->getOperand(0)->hasOneUse()) {
10715 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0));
10716 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1));
10717 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
10718 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
10719 if ((LHS00C && LHS00C->getZExtValue() == 0) &&
10720 (LHS01C && LHS01C->getZExtValue() == 1) &&
10721 (LHS1C && LHS1C->getZExtValue() == 1) &&
10722 (RHSC && RHSC->getZExtValue() == 0)) {
10723 return DAG.getNode(
10724 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2),
10725 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4));
10726 }
10727 }
10728
10729 return SDValue();
10730}
10731
Evan Chengf863e3f2011-07-13 00:42:17 +000010732/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10733SDValue
10734ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10735 SDValue Cmp = N->getOperand(4);
10736 if (Cmp.getOpcode() != ARMISD::CMPZ)
10737 // Only looking at EQ and NE cases.
10738 return SDValue();
10739
10740 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010741 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010742 SDValue LHS = Cmp.getOperand(0);
10743 SDValue RHS = Cmp.getOperand(1);
10744 SDValue FalseVal = N->getOperand(0);
10745 SDValue TrueVal = N->getOperand(1);
10746 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010747 ARMCC::CondCodes CC =
10748 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010749
James Molloy9d55f192015-11-10 14:22:05 +000010750 // BFI is only available on V6T2+.
10751 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10752 SDValue R = PerformCMOVToBFICombine(N, DAG);
10753 if (R)
10754 return R;
10755 }
10756
Evan Chengf863e3f2011-07-13 00:42:17 +000010757 // Simplify
10758 // mov r1, r0
10759 // cmp r1, x
10760 // mov r0, y
10761 // moveq r0, x
10762 // to
10763 // cmp r0, x
10764 // movne r0, y
10765 //
10766 // mov r1, r0
10767 // cmp r1, x
10768 // mov r0, x
10769 // movne r0, y
10770 // to
10771 // cmp r0, x
10772 // movne r0, y
10773 /// FIXME: Turn this into a target neutral optimization?
10774 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010775 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010776 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10777 N->getOperand(3), Cmp);
10778 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10779 SDValue ARMcc;
10780 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10781 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10782 N->getOperand(3), NewCmp);
10783 }
10784
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010785 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0))
10786 // -> (cmov F T CC CPSR Cmp)
10787 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) {
10788 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0));
10789 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
10790 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
10791 if ((LHS0C && LHS0C->getZExtValue() == 0) &&
10792 (LHS1C && LHS1C->getZExtValue() == 1) &&
10793 (RHSC && RHSC->getZExtValue() == 0)) {
10794 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
10795 LHS->getOperand(2), LHS->getOperand(3),
10796 LHS->getOperand(4));
10797 }
10798 }
10799
Evan Chengf863e3f2011-07-13 00:42:17 +000010800 if (Res.getNode()) {
10801 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010802 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010803 // Capture demanded bits information that would be otherwise lost.
10804 if (KnownZero == 0xfffffffe)
10805 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10806 DAG.getValueType(MVT::i1));
10807 else if (KnownZero == 0xffffff00)
10808 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10809 DAG.getValueType(MVT::i8));
10810 else if (KnownZero == 0xffff0000)
10811 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10812 DAG.getValueType(MVT::i16));
10813 }
10814
10815 return Res;
10816}
10817
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010818SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010819 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010820 switch (N->getOpcode()) {
10821 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010822 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010823 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010824 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010825 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010826 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010827 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10828 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010829 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010830 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010831 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010832 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010833 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010834 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010835 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010836 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010837 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010838 case ISD::FP_TO_UINT:
10839 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10840 case ISD::FDIV:
10841 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010842 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010843 case ISD::SHL:
10844 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010845 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010846 case ISD::SIGN_EXTEND:
10847 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010848 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010849 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000010850 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010851 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010852 case ARMISD::VLD2DUP:
10853 case ARMISD::VLD3DUP:
10854 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010855 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010856 case ARMISD::BUILD_VECTOR:
10857 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010858 case ISD::INTRINSIC_VOID:
10859 case ISD::INTRINSIC_W_CHAIN:
10860 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10861 case Intrinsic::arm_neon_vld1:
10862 case Intrinsic::arm_neon_vld2:
10863 case Intrinsic::arm_neon_vld3:
10864 case Intrinsic::arm_neon_vld4:
10865 case Intrinsic::arm_neon_vld2lane:
10866 case Intrinsic::arm_neon_vld3lane:
10867 case Intrinsic::arm_neon_vld4lane:
10868 case Intrinsic::arm_neon_vst1:
10869 case Intrinsic::arm_neon_vst2:
10870 case Intrinsic::arm_neon_vst3:
10871 case Intrinsic::arm_neon_vst4:
10872 case Intrinsic::arm_neon_vst2lane:
10873 case Intrinsic::arm_neon_vst3lane:
10874 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010875 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010876 default: break;
10877 }
10878 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010879 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010880 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010881}
10882
Evan Chengd42641c2011-02-02 01:06:55 +000010883bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10884 EVT VT) const {
10885 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10886}
10887
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010888bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10889 unsigned,
10890 unsigned,
10891 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010892 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010893 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010894
10895 switch (VT.getSimpleVT().SimpleTy) {
10896 default:
10897 return false;
10898 case MVT::i8:
10899 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010900 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010901 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010902 if (AllowsUnaligned) {
10903 if (Fast)
10904 *Fast = Subtarget->hasV7Ops();
10905 return true;
10906 }
10907 return false;
10908 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010909 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010910 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010911 // For any little-endian targets with neon, we can support unaligned ld/st
10912 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010913 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010914 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010915 if (Fast)
10916 *Fast = true;
10917 return true;
10918 }
10919 return false;
10920 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010921 }
10922}
10923
Lang Hames9929c422011-11-02 22:52:45 +000010924static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10925 unsigned AlignCheck) {
10926 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10927 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10928}
10929
10930EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10931 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010932 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010933 bool MemcpyStrSrc,
10934 MachineFunction &MF) const {
10935 const Function *F = MF.getFunction();
10936
10937 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010938 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10939 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010940 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010941 if (Size >= 16 &&
10942 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010943 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010944 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010945 } else if (Size >= 8 &&
10946 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010947 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10948 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010949 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010950 }
10951 }
10952
Lang Hamesb85fcd02011-11-08 18:56:23 +000010953 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010954 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010955 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010956 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010957 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010958
Lang Hames9929c422011-11-02 22:52:45 +000010959 // Let the target-independent logic figure it out.
10960 return MVT::Other;
10961}
10962
Evan Cheng9ec512d2012-12-06 19:13:27 +000010963bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10964 if (Val.getOpcode() != ISD::LOAD)
10965 return false;
10966
10967 EVT VT1 = Val.getValueType();
10968 if (!VT1.isSimple() || !VT1.isInteger() ||
10969 !VT2.isSimple() || !VT2.isInteger())
10970 return false;
10971
10972 switch (VT1.getSimpleVT().SimpleTy) {
10973 default: break;
10974 case MVT::i1:
10975 case MVT::i8:
10976 case MVT::i16:
10977 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10978 return true;
10979 }
10980
10981 return false;
10982}
10983
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010984bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10985 EVT VT = ExtVal.getValueType();
10986
10987 if (!isTypeLegal(VT))
10988 return false;
10989
10990 // Don't create a loadext if we can fold the extension into a wide/long
10991 // instruction.
10992 // If there's more than one user instruction, the loadext is desirable no
10993 // matter what. There can be two uses by the same instruction.
10994 if (ExtVal->use_empty() ||
10995 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10996 return true;
10997
10998 SDNode *U = *ExtVal->use_begin();
10999 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
11000 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
11001 return false;
11002
11003 return true;
11004}
11005
Tim Northovercc2e9032013-08-06 13:58:03 +000011006bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
11007 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11008 return false;
11009
11010 if (!isTypeLegal(EVT::getEVT(Ty1)))
11011 return false;
11012
11013 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
11014
11015 // Assuming the caller doesn't have a zeroext or signext return parameter,
11016 // truncation all the way down to i1 is valid.
11017 return true;
11018}
11019
11020
Evan Chengdc49a8d2009-08-14 20:09:37 +000011021static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
11022 if (V < 0)
11023 return false;
11024
11025 unsigned Scale = 1;
11026 switch (VT.getSimpleVT().SimpleTy) {
11027 default: return false;
11028 case MVT::i1:
11029 case MVT::i8:
11030 // Scale == 1;
11031 break;
11032 case MVT::i16:
11033 // Scale == 2;
11034 Scale = 2;
11035 break;
11036 case MVT::i32:
11037 // Scale == 4;
11038 Scale = 4;
11039 break;
11040 }
11041
11042 if ((V & (Scale - 1)) != 0)
11043 return false;
11044 V /= Scale;
11045 return V == (V & ((1LL << 5) - 1));
11046}
11047
11048static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
11049 const ARMSubtarget *Subtarget) {
11050 bool isNeg = false;
11051 if (V < 0) {
11052 isNeg = true;
11053 V = - V;
11054 }
11055
11056 switch (VT.getSimpleVT().SimpleTy) {
11057 default: return false;
11058 case MVT::i1:
11059 case MVT::i8:
11060 case MVT::i16:
11061 case MVT::i32:
11062 // + imm12 or - imm8
11063 if (isNeg)
11064 return V == (V & ((1LL << 8) - 1));
11065 return V == (V & ((1LL << 12) - 1));
11066 case MVT::f32:
11067 case MVT::f64:
11068 // Same as ARM mode. FIXME: NEON?
11069 if (!Subtarget->hasVFP2())
11070 return false;
11071 if ((V & 3) != 0)
11072 return false;
11073 V >>= 2;
11074 return V == (V & ((1LL << 8) - 1));
11075 }
11076}
11077
Evan Cheng2150b922007-03-12 23:30:29 +000011078/// isLegalAddressImmediate - Return true if the integer value can be used
11079/// as the offset of the target addressing mode for load / store of the
11080/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011081static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011082 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000011083 if (V == 0)
11084 return true;
11085
Evan Chengce5dfb62009-03-09 19:15:00 +000011086 if (!VT.isSimple())
11087 return false;
11088
Evan Chengdc49a8d2009-08-14 20:09:37 +000011089 if (Subtarget->isThumb1Only())
11090 return isLegalT1AddressImmediate(V, VT);
11091 else if (Subtarget->isThumb2())
11092 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000011093
Evan Chengdc49a8d2009-08-14 20:09:37 +000011094 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000011095 if (V < 0)
11096 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000011097 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000011098 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011099 case MVT::i1:
11100 case MVT::i8:
11101 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000011102 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011103 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011104 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000011105 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011106 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011107 case MVT::f32:
11108 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011109 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000011110 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000011111 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000011112 return false;
11113 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011114 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000011115 }
Evan Cheng10043e22007-01-19 07:51:42 +000011116}
11117
Evan Chengdc49a8d2009-08-14 20:09:37 +000011118bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
11119 EVT VT) const {
11120 int Scale = AM.Scale;
11121 if (Scale < 0)
11122 return false;
11123
11124 switch (VT.getSimpleVT().SimpleTy) {
11125 default: return false;
11126 case MVT::i1:
11127 case MVT::i8:
11128 case MVT::i16:
11129 case MVT::i32:
11130 if (Scale == 1)
11131 return true;
11132 // r + r << imm
11133 Scale = Scale & ~1;
11134 return Scale == 2 || Scale == 4 || Scale == 8;
11135 case MVT::i64:
11136 // r + r
11137 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11138 return true;
11139 return false;
11140 case MVT::isVoid:
11141 // Note, we allow "void" uses (basically, uses that aren't loads or
11142 // stores), because arm allows folding a scale into many arithmetic
11143 // operations. This should be made more precise and revisited later.
11144
11145 // Allow r << imm, but the imm has to be a multiple of two.
11146 if (Scale & 1) return false;
11147 return isPowerOf2_32(Scale);
11148 }
11149}
11150
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011151/// isLegalAddressingMode - Return true if the addressing mode represented
11152/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011153bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11154 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011155 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011156 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011157 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011158 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011159
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011160 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011161 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011162 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011163
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011164 switch (AM.Scale) {
11165 case 0: // no scale reg, must be "r+i" or "r", or "i".
11166 break;
11167 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011168 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011169 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000011170 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011171 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011172 // ARM doesn't support any R+R*scale+imm addr modes.
11173 if (AM.BaseOffs)
11174 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011175
Bob Wilson866c1742009-04-08 17:55:28 +000011176 if (!VT.isSimple())
11177 return false;
11178
Evan Chengdc49a8d2009-08-14 20:09:37 +000011179 if (Subtarget->isThumb2())
11180 return isLegalT2ScaledAddressingMode(AM, VT);
11181
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011182 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011183 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011184 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011185 case MVT::i1:
11186 case MVT::i8:
11187 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011188 if (Scale < 0) Scale = -Scale;
11189 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011190 return true;
11191 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011192 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011193 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011194 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011195 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011196 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011197 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011198 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011199
Owen Anderson9f944592009-08-11 20:47:22 +000011200 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011201 // Note, we allow "void" uses (basically, uses that aren't loads or
11202 // stores), because arm allows folding a scale into many arithmetic
11203 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011204
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011205 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011206 if (Scale & 1) return false;
11207 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011208 }
Evan Cheng2150b922007-03-12 23:30:29 +000011209 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011210 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011211}
11212
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011213/// isLegalICmpImmediate - Return true if the specified immediate is legal
11214/// icmp immediate, that is the target has icmp instructions which can compare
11215/// a register against the immediate without having to materialize the
11216/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011217bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011218 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011219 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011220 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011221 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011222 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011223 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011224 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011225}
11226
Andrew Tricka22cdb72012-07-18 18:34:27 +000011227/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11228/// *or sub* immediate, that is the target has add or sub instructions which can
11229/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011230/// immediate into a register.
11231bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011232 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011233 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011234 if (!Subtarget->isThumb())
11235 return ARM_AM::getSOImmVal(AbsImm) != -1;
11236 if (Subtarget->isThumb2())
11237 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11238 // Thumb1 only has 8-bit unsigned immediate.
11239 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011240}
11241
Owen Anderson53aa7a92009-08-10 22:56:29 +000011242static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011243 bool isSEXTLoad, SDValue &Base,
11244 SDValue &Offset, bool &isInc,
11245 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011246 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11247 return false;
11248
Owen Anderson9f944592009-08-11 20:47:22 +000011249 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011250 // AddressingMode 3
11251 Base = Ptr->getOperand(0);
11252 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011253 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011254 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011255 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011256 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011257 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011258 return true;
11259 }
11260 }
11261 isInc = (Ptr->getOpcode() == ISD::ADD);
11262 Offset = Ptr->getOperand(1);
11263 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011264 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011265 // AddressingMode 2
11266 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011267 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011268 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011269 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011270 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011271 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011272 Base = Ptr->getOperand(0);
11273 return true;
11274 }
11275 }
11276
11277 if (Ptr->getOpcode() == ISD::ADD) {
11278 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011279 ARM_AM::ShiftOpc ShOpcVal=
11280 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011281 if (ShOpcVal != ARM_AM::no_shift) {
11282 Base = Ptr->getOperand(1);
11283 Offset = Ptr->getOperand(0);
11284 } else {
11285 Base = Ptr->getOperand(0);
11286 Offset = Ptr->getOperand(1);
11287 }
11288 return true;
11289 }
11290
11291 isInc = (Ptr->getOpcode() == ISD::ADD);
11292 Base = Ptr->getOperand(0);
11293 Offset = Ptr->getOperand(1);
11294 return true;
11295 }
11296
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011297 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011298 return false;
11299}
11300
Owen Anderson53aa7a92009-08-10 22:56:29 +000011301static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011302 bool isSEXTLoad, SDValue &Base,
11303 SDValue &Offset, bool &isInc,
11304 SelectionDAG &DAG) {
11305 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11306 return false;
11307
11308 Base = Ptr->getOperand(0);
11309 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11310 int RHSC = (int)RHS->getZExtValue();
11311 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11312 assert(Ptr->getOpcode() == ISD::ADD);
11313 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011314 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011315 return true;
11316 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11317 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011318 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011319 return true;
11320 }
11321 }
11322
11323 return false;
11324}
11325
Evan Cheng10043e22007-01-19 07:51:42 +000011326/// getPreIndexedAddressParts - returns true by value, base pointer and
11327/// offset pointer and addressing mode by reference if the node's address
11328/// can be legally represented as pre-indexed load / store address.
11329bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011330ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11331 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011332 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011333 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011334 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011335 return false;
11336
Owen Anderson53aa7a92009-08-10 22:56:29 +000011337 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011338 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011339 bool isSEXTLoad = false;
11340 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11341 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011342 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011343 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11344 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11345 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011346 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011347 } else
11348 return false;
11349
11350 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011351 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011352 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011353 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11354 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011355 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011356 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011357 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011358 if (!isLegal)
11359 return false;
11360
11361 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11362 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011363}
11364
11365/// getPostIndexedAddressParts - returns true by value, base pointer and
11366/// offset pointer and addressing mode by reference if this node can be
11367/// combined with a load / store to form a post-indexed load / store.
11368bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011369 SDValue &Base,
11370 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011371 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011372 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011373 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011374 return false;
11375
Owen Anderson53aa7a92009-08-10 22:56:29 +000011376 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011377 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011378 bool isSEXTLoad = false;
11379 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011380 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011381 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011382 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11383 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011384 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011385 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011386 } else
11387 return false;
11388
11389 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011390 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011391 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011392 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011393 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011394 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011395 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11396 isInc, DAG);
11397 if (!isLegal)
11398 return false;
11399
Evan Chengf19384d2010-05-18 21:31:17 +000011400 if (Ptr != Base) {
11401 // Swap base ptr and offset to catch more post-index load / store when
11402 // it's legal. In Thumb2 mode, offset must be an immediate.
11403 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11404 !Subtarget->isThumb2())
11405 std::swap(Base, Offset);
11406
11407 // Post-indexed load / store update the base pointer.
11408 if (Ptr != Base)
11409 return false;
11410 }
11411
Evan Cheng84c6cda2009-07-02 07:28:31 +000011412 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11413 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011414}
11415
Jay Foada0653a32014-05-14 21:14:37 +000011416void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11417 APInt &KnownZero,
11418 APInt &KnownOne,
11419 const SelectionDAG &DAG,
11420 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011421 unsigned BitWidth = KnownOne.getBitWidth();
11422 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011423 switch (Op.getOpcode()) {
11424 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011425 case ARMISD::ADDC:
11426 case ARMISD::ADDE:
11427 case ARMISD::SUBC:
11428 case ARMISD::SUBE:
11429 // These nodes' second result is a boolean
11430 if (Op.getResNo() == 0)
11431 break;
11432 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11433 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011434 case ARMISD::CMOV: {
11435 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011436 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011437 if (KnownZero == 0 && KnownOne == 0) return;
11438
Dan Gohmanf990faf2008-02-13 00:35:47 +000011439 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011440 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011441 KnownZero &= KnownZeroRHS;
11442 KnownOne &= KnownOneRHS;
11443 return;
11444 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011445 case ISD::INTRINSIC_W_CHAIN: {
11446 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11447 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11448 switch (IntID) {
11449 default: return;
11450 case Intrinsic::arm_ldaex:
11451 case Intrinsic::arm_ldrex: {
11452 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11453 unsigned MemBits = VT.getScalarType().getSizeInBits();
11454 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11455 return;
11456 }
11457 }
11458 }
Evan Cheng10043e22007-01-19 07:51:42 +000011459 }
11460}
11461
11462//===----------------------------------------------------------------------===//
11463// ARM Inline Assembly Support
11464//===----------------------------------------------------------------------===//
11465
Evan Cheng078b0b02011-01-08 01:24:27 +000011466bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11467 // Looking for "rev" which is V6+.
11468 if (!Subtarget->hasV6Ops())
11469 return false;
11470
11471 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11472 std::string AsmStr = IA->getAsmString();
11473 SmallVector<StringRef, 4> AsmPieces;
11474 SplitString(AsmStr, AsmPieces, ";\n");
11475
11476 switch (AsmPieces.size()) {
11477 default: return false;
11478 case 1:
11479 AsmStr = AsmPieces[0];
11480 AsmPieces.clear();
11481 SplitString(AsmStr, AsmPieces, " \t,");
11482
11483 // rev $0, $1
11484 if (AsmPieces.size() == 3 &&
11485 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11486 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011487 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011488 if (Ty && Ty->getBitWidth() == 32)
11489 return IntrinsicLowering::LowerToByteSwap(CI);
11490 }
11491 break;
11492 }
11493
11494 return false;
11495}
11496
Evan Cheng10043e22007-01-19 07:51:42 +000011497/// getConstraintType - Given a constraint letter, return the type of
11498/// constraint it is for this target.
11499ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011500ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011501 if (Constraint.size() == 1) {
11502 switch (Constraint[0]) {
11503 default: break;
11504 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011505 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011506 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011507 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011508 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011509 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011510 // An address with a single base register. Due to the way we
11511 // currently handle addresses it is the same as an 'r' memory constraint.
11512 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011513 }
Eric Christophere256cd02011-06-21 22:10:57 +000011514 } else if (Constraint.size() == 2) {
11515 switch (Constraint[0]) {
11516 default: break;
11517 // All 'U+' constraints are addresses.
11518 case 'U': return C_Memory;
11519 }
Evan Cheng10043e22007-01-19 07:51:42 +000011520 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011521 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011522}
11523
John Thompsone8360b72010-10-29 17:29:13 +000011524/// Examine constraint type and operand type and determine a weight value.
11525/// This object must already have been set up with the operand type
11526/// and the current alternative constraint selected.
11527TargetLowering::ConstraintWeight
11528ARMTargetLowering::getSingleConstraintMatchWeight(
11529 AsmOperandInfo &info, const char *constraint) const {
11530 ConstraintWeight weight = CW_Invalid;
11531 Value *CallOperandVal = info.CallOperandVal;
11532 // If we don't have a value, we can't do a match,
11533 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011534 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011535 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011536 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011537 // Look at the constraint type.
11538 switch (*constraint) {
11539 default:
11540 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11541 break;
11542 case 'l':
11543 if (type->isIntegerTy()) {
11544 if (Subtarget->isThumb())
11545 weight = CW_SpecificReg;
11546 else
11547 weight = CW_Register;
11548 }
11549 break;
11550 case 'w':
11551 if (type->isFloatingPointTy())
11552 weight = CW_Register;
11553 break;
11554 }
11555 return weight;
11556}
11557
Eric Christophercf2007c2011-06-30 23:50:52 +000011558typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011559RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11560 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011561 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011562 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011563 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011564 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011565 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011566 return RCPair(0U, &ARM::tGPRRegClass);
11567 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011568 case 'h': // High regs or no regs.
11569 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011570 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011571 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011572 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011573 if (Subtarget->isThumb1Only())
11574 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011575 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011576 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011577 if (VT == MVT::Other)
11578 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011579 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011580 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011581 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011582 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011583 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011584 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011585 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011586 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011587 if (VT == MVT::Other)
11588 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011589 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011590 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011591 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011592 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011593 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011594 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011595 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011596 case 't':
11597 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011598 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011599 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011600 }
11601 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011602 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011603 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011604
Eric Christopher11e4df72015-02-26 22:38:43 +000011605 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011606}
11607
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011608/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11609/// vector. If it is invalid, don't add anything to Ops.
11610void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011611 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011612 std::vector<SDValue>&Ops,
11613 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011614 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011615
Eric Christopherde9399b2011-06-02 23:16:42 +000011616 // Currently only support length 1 constraints.
11617 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011618
Eric Christopherde9399b2011-06-02 23:16:42 +000011619 char ConstraintLetter = Constraint[0];
11620 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011621 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011622 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011623 case 'I': case 'J': case 'K': case 'L':
11624 case 'M': case 'N': case 'O':
11625 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11626 if (!C)
11627 return;
11628
11629 int64_t CVal64 = C->getSExtValue();
11630 int CVal = (int) CVal64;
11631 // None of these constraints allow values larger than 32 bits. Check
11632 // that the value fits in an int.
11633 if (CVal != CVal64)
11634 return;
11635
Eric Christopherde9399b2011-06-02 23:16:42 +000011636 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011637 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011638 // Constant suitable for movw, must be between 0 and
11639 // 65535.
11640 if (Subtarget->hasV6T2Ops())
11641 if (CVal >= 0 && CVal <= 65535)
11642 break;
11643 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011644 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011645 if (Subtarget->isThumb1Only()) {
11646 // This must be a constant between 0 and 255, for ADD
11647 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011648 if (CVal >= 0 && CVal <= 255)
11649 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011650 } else if (Subtarget->isThumb2()) {
11651 // A constant that can be used as an immediate value in a
11652 // data-processing instruction.
11653 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11654 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011655 } else {
11656 // A constant that can be used as an immediate value in a
11657 // data-processing instruction.
11658 if (ARM_AM::getSOImmVal(CVal) != -1)
11659 break;
11660 }
11661 return;
11662
11663 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000011664 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011665 // This must be a constant between -255 and -1, for negated ADD
11666 // immediates. This can be used in GCC with an "n" modifier that
11667 // prints the negated value, for use with SUB instructions. It is
11668 // not useful otherwise but is implemented for compatibility.
11669 if (CVal >= -255 && CVal <= -1)
11670 break;
11671 } else {
11672 // This must be a constant between -4095 and 4095. It is not clear
11673 // what this constraint is intended for. Implemented for
11674 // compatibility with GCC.
11675 if (CVal >= -4095 && CVal <= 4095)
11676 break;
11677 }
11678 return;
11679
11680 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011681 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011682 // A 32-bit value where only one byte has a nonzero value. Exclude
11683 // zero to match GCC. This constraint is used by GCC internally for
11684 // constants that can be loaded with a move/shift combination.
11685 // It is not useful otherwise but is implemented for compatibility.
11686 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11687 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011688 } else if (Subtarget->isThumb2()) {
11689 // A constant whose bitwise inverse can be used as an immediate
11690 // value in a data-processing instruction. This can be used in GCC
11691 // with a "B" modifier that prints the inverted value, for use with
11692 // BIC and MVN instructions. It is not useful otherwise but is
11693 // implemented for compatibility.
11694 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11695 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011696 } else {
11697 // A constant whose bitwise inverse can be used as an immediate
11698 // value in a data-processing instruction. This can be used in GCC
11699 // with a "B" modifier that prints the inverted value, for use with
11700 // BIC and MVN instructions. It is not useful otherwise but is
11701 // implemented for compatibility.
11702 if (ARM_AM::getSOImmVal(~CVal) != -1)
11703 break;
11704 }
11705 return;
11706
11707 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011708 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011709 // This must be a constant between -7 and 7,
11710 // for 3-operand ADD/SUB immediate instructions.
11711 if (CVal >= -7 && CVal < 7)
11712 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011713 } else if (Subtarget->isThumb2()) {
11714 // A constant whose negation can be used as an immediate value in a
11715 // data-processing instruction. This can be used in GCC with an "n"
11716 // modifier that prints the negated value, for use with SUB
11717 // instructions. It is not useful otherwise but is implemented for
11718 // compatibility.
11719 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11720 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011721 } else {
11722 // A constant whose negation can be used as an immediate value in a
11723 // data-processing instruction. This can be used in GCC with an "n"
11724 // modifier that prints the negated value, for use with SUB
11725 // instructions. It is not useful otherwise but is implemented for
11726 // compatibility.
11727 if (ARM_AM::getSOImmVal(-CVal) != -1)
11728 break;
11729 }
11730 return;
11731
11732 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000011733 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011734 // This must be a multiple of 4 between 0 and 1020, for
11735 // ADD sp + immediate.
11736 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11737 break;
11738 } else {
11739 // A power of two or a constant between 0 and 32. This is used in
11740 // GCC for the shift amount on shifted register operands, but it is
11741 // useful in general for any shift amounts.
11742 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11743 break;
11744 }
11745 return;
11746
11747 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011748 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011749 // This must be a constant between 0 and 31, for shift amounts.
11750 if (CVal >= 0 && CVal <= 31)
11751 break;
11752 }
11753 return;
11754
11755 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011756 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011757 // This must be a multiple of 4 between -508 and 508, for
11758 // ADD/SUB sp = sp + immediate.
11759 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11760 break;
11761 }
11762 return;
11763 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011764 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011765 break;
11766 }
11767
11768 if (Result.getNode()) {
11769 Ops.push_back(Result);
11770 return;
11771 }
Dale Johannesence97d552010-06-25 21:55:36 +000011772 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011773}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011774
Scott Douglassd2974a62015-08-24 09:17:11 +000011775static RTLIB::Libcall getDivRemLibcall(
11776 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011777 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11778 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011779 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011780 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11781 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011782 RTLIB::Libcall LC;
11783 switch (SVT) {
11784 default: llvm_unreachable("Unexpected request for libcall!");
11785 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11786 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11787 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11788 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11789 }
11790 return LC;
11791}
11792
11793static TargetLowering::ArgListTy getDivRemArgList(
11794 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011795 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11796 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011797 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011798 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11799 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011800 TargetLowering::ArgListTy Args;
11801 TargetLowering::ArgListEntry Entry;
11802 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11803 EVT ArgVT = N->getOperand(i).getValueType();
11804 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11805 Entry.Node = N->getOperand(i);
11806 Entry.Ty = ArgTy;
11807 Entry.isSExt = isSigned;
11808 Entry.isZExt = !isSigned;
11809 Args.push_back(Entry);
11810 }
11811 return Args;
11812}
11813
Renato Golin87610692013-07-16 09:32:17 +000011814SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000011815 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
11816 Subtarget->isTargetGNUAEABI()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011817 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011818 unsigned Opcode = Op->getOpcode();
11819 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011820 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011821 bool isSigned = (Opcode == ISD::SDIVREM);
11822 EVT VT = Op->getValueType(0);
11823 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11824
Scott Douglassd2974a62015-08-24 09:17:11 +000011825 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11826 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011827 SDValue InChain = DAG.getEntryNode();
11828
Scott Douglassd2974a62015-08-24 09:17:11 +000011829 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11830 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011831
11832 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011833 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011834
Reid Kleckner343c3952014-11-20 23:51:47 +000011835 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011836
11837 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011838 TargetLowering::CallLoweringInfo CLI(DAG);
11839 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011840 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011841 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011842
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011843 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011844 return CallInfo.first;
11845}
11846
Scott Douglassbdef6042015-08-24 09:17:18 +000011847// Lowers REM using divmod helpers
11848// see RTABI section 4.2/4.3
11849SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11850 // Build return types (div and rem)
11851 std::vector<Type*> RetTyParams;
11852 Type *RetTyElement;
11853
11854 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11855 default: llvm_unreachable("Unexpected request for libcall!");
11856 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11857 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11858 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11859 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11860 }
11861
11862 RetTyParams.push_back(RetTyElement);
11863 RetTyParams.push_back(RetTyElement);
11864 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11865 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11866
11867 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11868 SimpleTy);
11869 SDValue InChain = DAG.getEntryNode();
11870 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11871 bool isSigned = N->getOpcode() == ISD::SREM;
11872 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11873 getPointerTy(DAG.getDataLayout()));
11874
11875 // Lower call
11876 CallLoweringInfo CLI(DAG);
11877 CLI.setChain(InChain)
11878 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11879 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11880 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11881
11882 // Return second (rem) result operand (first contains div)
11883 SDNode *ResNode = CallResult.first.getNode();
11884 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11885 return ResNode->getOperand(1);
11886}
11887
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011888SDValue
11889ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11890 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11891 SDLoc DL(Op);
11892
11893 // Get the inputs.
11894 SDValue Chain = Op.getOperand(0);
11895 SDValue Size = Op.getOperand(1);
11896
11897 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011898 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011899
11900 SDValue Flag;
11901 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11902 Flag = Chain.getValue(1);
11903
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011904 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011905 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11906
11907 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11908 Chain = NewSP.getValue(1);
11909
11910 SDValue Ops[2] = { NewSP, Chain };
11911 return DAG.getMergeValues(Ops, DL);
11912}
11913
Oliver Stannard51b1d462014-08-21 12:50:31 +000011914SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11915 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11916 "Unexpected type for custom-lowering FP_EXTEND");
11917
11918 RTLIB::Libcall LC;
11919 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11920
11921 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011922 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11923 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011924}
11925
11926SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11927 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11928 Subtarget->isFPOnlySP() &&
11929 "Unexpected type for custom-lowering FP_ROUND");
11930
11931 RTLIB::Libcall LC;
11932 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11933
11934 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011935 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11936 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011937}
11938
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011939bool
11940ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11941 // The ARM target isn't yet aware of offsets.
11942 return false;
11943}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011944
Jim Grosbach11013ed2010-07-16 23:05:05 +000011945bool ARM::isBitFieldInvertedMask(unsigned v) {
11946 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011947 return false;
11948
Jim Grosbach11013ed2010-07-16 23:05:05 +000011949 // there can be 1's on either or both "outsides", all the "inside"
11950 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011951 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011952}
11953
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011954/// isFPImmLegal - Returns true if the target can instruction select the
11955/// specified FP immediate natively. If false, the legalizer will
11956/// materialize the FP immediate as a load from a constant pool.
11957bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11958 if (!Subtarget->hasVFP3())
11959 return false;
11960 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011961 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011962 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011963 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011964 return false;
11965}
Bob Wilson5549d492010-09-21 17:56:22 +000011966
Wesley Peck527da1b2010-11-23 03:31:01 +000011967/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011968/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11969/// specified in the intrinsic calls.
11970bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11971 const CallInst &I,
11972 unsigned Intrinsic) const {
11973 switch (Intrinsic) {
11974 case Intrinsic::arm_neon_vld1:
11975 case Intrinsic::arm_neon_vld2:
11976 case Intrinsic::arm_neon_vld3:
11977 case Intrinsic::arm_neon_vld4:
11978 case Intrinsic::arm_neon_vld2lane:
11979 case Intrinsic::arm_neon_vld3lane:
11980 case Intrinsic::arm_neon_vld4lane: {
11981 Info.opc = ISD::INTRINSIC_W_CHAIN;
11982 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011983 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011984 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011985 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11986 Info.ptrVal = I.getArgOperand(0);
11987 Info.offset = 0;
11988 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11989 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11990 Info.vol = false; // volatile loads with NEON intrinsics not supported
11991 Info.readMem = true;
11992 Info.writeMem = false;
11993 return true;
11994 }
11995 case Intrinsic::arm_neon_vst1:
11996 case Intrinsic::arm_neon_vst2:
11997 case Intrinsic::arm_neon_vst3:
11998 case Intrinsic::arm_neon_vst4:
11999 case Intrinsic::arm_neon_vst2lane:
12000 case Intrinsic::arm_neon_vst3lane:
12001 case Intrinsic::arm_neon_vst4lane: {
12002 Info.opc = ISD::INTRINSIC_VOID;
12003 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012004 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000012005 unsigned NumElts = 0;
12006 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000012007 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000012008 if (!ArgTy->isVectorTy())
12009 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012010 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012011 }
12012 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12013 Info.ptrVal = I.getArgOperand(0);
12014 Info.offset = 0;
12015 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12016 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12017 Info.vol = false; // volatile stores with NEON intrinsics not supported
12018 Info.readMem = false;
12019 Info.writeMem = true;
12020 return true;
12021 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012022 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012023 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012024 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012025 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
12026 Info.opc = ISD::INTRINSIC_W_CHAIN;
12027 Info.memVT = MVT::getVT(PtrTy->getElementType());
12028 Info.ptrVal = I.getArgOperand(0);
12029 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012030 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012031 Info.vol = true;
12032 Info.readMem = true;
12033 Info.writeMem = false;
12034 return true;
12035 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012036 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012037 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012038 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012039 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
12040 Info.opc = ISD::INTRINSIC_W_CHAIN;
12041 Info.memVT = MVT::getVT(PtrTy->getElementType());
12042 Info.ptrVal = I.getArgOperand(1);
12043 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012044 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012045 Info.vol = true;
12046 Info.readMem = false;
12047 Info.writeMem = true;
12048 return true;
12049 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012050 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012051 case Intrinsic::arm_strexd: {
12052 Info.opc = ISD::INTRINSIC_W_CHAIN;
12053 Info.memVT = MVT::i64;
12054 Info.ptrVal = I.getArgOperand(2);
12055 Info.offset = 0;
12056 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012057 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012058 Info.readMem = false;
12059 Info.writeMem = true;
12060 return true;
12061 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012062 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012063 case Intrinsic::arm_ldrexd: {
12064 Info.opc = ISD::INTRINSIC_W_CHAIN;
12065 Info.memVT = MVT::i64;
12066 Info.ptrVal = I.getArgOperand(0);
12067 Info.offset = 0;
12068 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012069 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012070 Info.readMem = true;
12071 Info.writeMem = false;
12072 return true;
12073 }
Bob Wilson5549d492010-09-21 17:56:22 +000012074 default:
12075 break;
12076 }
12077
12078 return false;
12079}
Juergen Ributzka659ce002014-01-28 01:20:14 +000012080
12081/// \brief Returns true if it is beneficial to convert a load of a constant
12082/// to just the constant itself.
12083bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
12084 Type *Ty) const {
12085 assert(Ty->isIntegerTy());
12086
12087 unsigned Bits = Ty->getPrimitiveSizeInBits();
12088 if (Bits == 0 || Bits > 32)
12089 return false;
12090 return true;
12091}
Tim Northover037f26f22014-04-17 18:22:47 +000012092
Robin Morisset5349e8e2014-09-18 18:56:04 +000012093Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
12094 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012095 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000012096
12097 // First, if the target has no DMB, see what fallback we can use.
12098 if (!Subtarget->hasDataBarrier()) {
12099 // Some ARMv6 cpus can support data barriers with an mcr instruction.
12100 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
12101 // here.
12102 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
12103 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
12104 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
12105 Builder.getInt32(0), Builder.getInt32(7),
12106 Builder.getInt32(10), Builder.getInt32(5)};
12107 return Builder.CreateCall(MCR, args);
12108 } else {
12109 // Instead of using barriers, atomic accesses on these subtargets use
12110 // libcalls.
12111 llvm_unreachable("makeDMB on a target so old that it has no barriers");
12112 }
12113 } else {
12114 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
12115 // Only a full system barrier exists in the M-class architectures.
12116 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
12117 Constant *CDomain = Builder.getInt32(Domain);
12118 return Builder.CreateCall(DMB, CDomain);
12119 }
Robin Morisseta47cb412014-09-03 21:01:03 +000012120}
12121
12122// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000012123Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012124 AtomicOrdering Ord, bool IsStore,
12125 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012126 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012127 case AtomicOrdering::NotAtomic:
12128 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012129 llvm_unreachable("Invalid fence: unordered/non-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012130 case AtomicOrdering::Monotonic:
12131 case AtomicOrdering::Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012132 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012133 case AtomicOrdering::SequentiallyConsistent:
Robin Morisseta47cb412014-09-03 21:01:03 +000012134 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012135 return nullptr; // Nothing to do
12136 /*FALLTHROUGH*/
JF Bastien800f87a2016-04-06 21:19:33 +000012137 case AtomicOrdering::Release:
12138 case AtomicOrdering::AcquireRelease:
Robin Morisseta47cb412014-09-03 21:01:03 +000012139 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000012140 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012141 // FIXME: add a comment with a link to documentation justifying this.
12142 else
Robin Morissetdedef332014-09-23 20:31:14 +000012143 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012144 }
Robin Morissetdedef332014-09-23 20:31:14 +000012145 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012146}
12147
Robin Morissetdedef332014-09-23 20:31:14 +000012148Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012149 AtomicOrdering Ord, bool IsStore,
12150 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012151 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012152 case AtomicOrdering::NotAtomic:
12153 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012154 llvm_unreachable("Invalid fence: unordered/not-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012155 case AtomicOrdering::Monotonic:
12156 case AtomicOrdering::Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012157 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012158 case AtomicOrdering::Acquire:
12159 case AtomicOrdering::AcquireRelease:
12160 case AtomicOrdering::SequentiallyConsistent:
Robin Morissetdedef332014-09-23 20:31:14 +000012161 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012162 }
Robin Morissetdedef332014-09-23 20:31:14 +000012163 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012164}
12165
Robin Morisseted3d48f2014-09-03 21:29:59 +000012166// Loads and stores less than 64-bits are already atomic; ones above that
12167// are doomed anyway, so defer to the default libcall and blame the OS when
12168// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12169// anything for those.
12170bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12171 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12172 return (Size == 64) && !Subtarget->isMClass();
12173}
Tim Northover037f26f22014-04-17 18:22:47 +000012174
Robin Morisseted3d48f2014-09-03 21:29:59 +000012175// Loads and stores less than 64-bits are already atomic; ones above that
12176// are doomed anyway, so defer to the default libcall and blame the OS when
12177// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12178// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012179// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12180// guarantee, see DDI0406C ARM architecture reference manual,
12181// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012182TargetLowering::AtomicExpansionKind
12183ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012184 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012185 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012186 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012187}
12188
12189// For the real atomic operations, we have ldrex/strex up to 32 bits,
12190// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012191TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012192ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012193 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012194 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012195 ? AtomicExpansionKind::LLSC
12196 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012197}
12198
Ahmed Bougacha52468672015-09-11 17:08:28 +000012199bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12200 AtomicCmpXchgInst *AI) const {
Tim Northoverb629c772016-04-18 21:48:55 +000012201 // At -O0, fast-regalloc cannot cope with the live vregs necessary to
12202 // implement cmpxchg without spilling. If the address being exchanged is also
12203 // on the stack and close enough to the spill slot, this can lead to a
12204 // situation where the monitor always gets cleared and the atomic operation
12205 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
12206 return getTargetMachine().getOptLevel() != 0;
Ahmed Bougacha52468672015-09-11 17:08:28 +000012207}
12208
James Y Knightf44fc522016-03-16 22:12:04 +000012209bool ARMTargetLowering::shouldInsertFencesForAtomic(
12210 const Instruction *I) const {
12211 return InsertFencesForAtomic;
12212}
12213
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012214// This has so far only been implemented for MachO.
12215bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012216 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012217}
12218
Quentin Colombetc32615d2014-10-31 17:52:53 +000012219bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12220 unsigned &Cost) const {
12221 // If we do not have NEON, vector types are not natively supported.
12222 if (!Subtarget->hasNEON())
12223 return false;
12224
12225 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012226 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012227 // better to leave at float as we have more freedom in the addressing mode for
12228 // those.
12229 if (VectorTy->isFPOrFPVectorTy())
12230 return false;
12231
12232 // If the index is unknown at compile time, this is very expensive to lower
12233 // and it is not possible to combine the store with the extract.
12234 if (!isa<ConstantInt>(Idx))
12235 return false;
12236
12237 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12238 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12239 // We can do a store + vector extract on any vector that fits perfectly in a D
12240 // or Q register.
12241 if (BitWidth == 64 || BitWidth == 128) {
12242 Cost = 0;
12243 return true;
12244 }
12245 return false;
12246}
12247
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012248bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12249 return Subtarget->hasV6T2Ops();
12250}
12251
12252bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12253 return Subtarget->hasV6T2Ops();
12254}
12255
Tim Northover037f26f22014-04-17 18:22:47 +000012256Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12257 AtomicOrdering Ord) const {
12258 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12259 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
JF Bastien800f87a2016-04-06 21:19:33 +000012260 bool IsAcquire = isAcquireOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012261
12262 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12263 // intrinsic must return {i32, i32} and we have to recombine them into a
12264 // single i64 here.
12265 if (ValTy->getPrimitiveSizeInBits() == 64) {
12266 Intrinsic::ID Int =
12267 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12268 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12269
12270 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12271 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12272
12273 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12274 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012275 if (!Subtarget->isLittle())
12276 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012277 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12278 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12279 return Builder.CreateOr(
12280 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12281 }
12282
12283 Type *Tys[] = { Addr->getType() };
12284 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12285 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12286
12287 return Builder.CreateTruncOrBitCast(
12288 Builder.CreateCall(Ldrex, Addr),
12289 cast<PointerType>(Addr->getType())->getElementType());
12290}
12291
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012292void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12293 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012294 if (!Subtarget->hasV7Ops())
12295 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012296 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12297 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12298}
12299
Tim Northover037f26f22014-04-17 18:22:47 +000012300Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12301 Value *Addr,
12302 AtomicOrdering Ord) const {
12303 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
JF Bastien800f87a2016-04-06 21:19:33 +000012304 bool IsRelease = isReleaseOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012305
12306 // Since the intrinsics must have legal type, the i64 intrinsics take two
12307 // parameters: "i32, i32". We must marshal Val into the appropriate form
12308 // before the call.
12309 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12310 Intrinsic::ID Int =
12311 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12312 Function *Strex = Intrinsic::getDeclaration(M, Int);
12313 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12314
12315 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12316 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012317 if (!Subtarget->isLittle())
12318 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012319 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012320 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012321 }
12322
12323 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12324 Type *Tys[] = { Addr->getType() };
12325 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12326
David Blaikieff6409d2015-05-18 22:13:54 +000012327 return Builder.CreateCall(
12328 Strex, {Builder.CreateZExtOrBitCast(
12329 Val, Strex->getFunctionType()->getParamType(0)),
12330 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012331}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012332
Hao Liu2cd34bb2015-06-26 02:45:36 +000012333/// \brief Lower an interleaved load into a vldN intrinsic.
12334///
12335/// E.g. Lower an interleaved load (Factor = 2):
12336/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12337/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12338/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12339///
12340/// Into:
12341/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12342/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12343/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12344bool ARMTargetLowering::lowerInterleavedLoad(
12345 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12346 ArrayRef<unsigned> Indices, unsigned Factor) const {
12347 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12348 "Invalid interleave factor");
12349 assert(!Shuffles.empty() && "Empty shufflevector input");
12350 assert(Shuffles.size() == Indices.size() &&
12351 "Unmatched number of shufflevectors and indices");
12352
12353 VectorType *VecTy = Shuffles[0]->getType();
12354 Type *EltTy = VecTy->getVectorElementType();
12355
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012356 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012357 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
12358 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012359
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012360 // Skip if we do not have NEON and skip illegal vector types and vector types
12361 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12362 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012363 return false;
12364
12365 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12366 // load integer vectors first and then convert to pointer vectors.
12367 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012368 VecTy =
12369 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012370
12371 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12372 Intrinsic::arm_neon_vld3,
12373 Intrinsic::arm_neon_vld4};
12374
Hao Liu2cd34bb2015-06-26 02:45:36 +000012375 IRBuilder<> Builder(LI);
12376 SmallVector<Value *, 2> Ops;
12377
12378 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12379 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12380 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12381
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012382 Type *Tys[] = { VecTy, Int8Ptr };
12383 Function *VldnFunc =
12384 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012385 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12386
12387 // Replace uses of each shufflevector with the corresponding vector loaded
12388 // by ldN.
12389 for (unsigned i = 0; i < Shuffles.size(); i++) {
12390 ShuffleVectorInst *SV = Shuffles[i];
12391 unsigned Index = Indices[i];
12392
12393 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12394
12395 // Convert the integer vector to pointer vector if the element is pointer.
12396 if (EltTy->isPointerTy())
12397 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12398
12399 SV->replaceAllUsesWith(SubVec);
12400 }
12401
12402 return true;
12403}
12404
12405/// \brief Get a mask consisting of sequential integers starting from \p Start.
12406///
12407/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12408static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12409 unsigned NumElts) {
12410 SmallVector<Constant *, 16> Mask;
12411 for (unsigned i = 0; i < NumElts; i++)
12412 Mask.push_back(Builder.getInt32(Start + i));
12413
12414 return ConstantVector::get(Mask);
12415}
12416
12417/// \brief Lower an interleaved store into a vstN intrinsic.
12418///
12419/// E.g. Lower an interleaved store (Factor = 3):
12420/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12421/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12422/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12423///
12424/// Into:
12425/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12426/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12427/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12428/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12429///
12430/// Note that the new shufflevectors will be removed and we'll only generate one
12431/// vst3 instruction in CodeGen.
12432bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12433 ShuffleVectorInst *SVI,
12434 unsigned Factor) const {
12435 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12436 "Invalid interleave factor");
12437
12438 VectorType *VecTy = SVI->getType();
12439 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12440 "Invalid interleaved store");
12441
12442 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12443 Type *EltTy = VecTy->getVectorElementType();
12444 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12445
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012446 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012447 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
12448 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012449
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012450 // Skip if we do not have NEON and skip illegal vector types and vector types
12451 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12452 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12453 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012454 return false;
12455
12456 Value *Op0 = SVI->getOperand(0);
12457 Value *Op1 = SVI->getOperand(1);
12458 IRBuilder<> Builder(SI);
12459
12460 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12461 // vectors to integer vectors.
12462 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012463 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012464
12465 // Convert to the corresponding integer vector.
12466 Type *IntVecTy =
12467 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12468 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12469 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12470
12471 SubVecTy = VectorType::get(IntTy, NumSubElts);
12472 }
12473
Craig Topper26260942015-10-18 05:15:34 +000012474 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12475 Intrinsic::arm_neon_vst3,
12476 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012477 SmallVector<Value *, 6> Ops;
12478
12479 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12480 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12481
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012482 Type *Tys[] = { Int8Ptr, SubVecTy };
12483 Function *VstNFunc = Intrinsic::getDeclaration(
12484 SI->getModule(), StoreInts[Factor - 2], Tys);
12485
Hao Liu2cd34bb2015-06-26 02:45:36 +000012486 // Split the shufflevector operands into sub vectors for the new vstN call.
12487 for (unsigned i = 0; i < Factor; i++)
12488 Ops.push_back(Builder.CreateShuffleVector(
12489 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12490
12491 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12492 Builder.CreateCall(VstNFunc, Ops);
12493 return true;
12494}
12495
Oliver Stannardc24f2172014-05-09 14:01:47 +000012496enum HABaseType {
12497 HA_UNKNOWN = 0,
12498 HA_FLOAT,
12499 HA_DOUBLE,
12500 HA_VECT64,
12501 HA_VECT128
12502};
12503
12504static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12505 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012506 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012507 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12508 uint64_t SubMembers = 0;
12509 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12510 return false;
12511 Members += SubMembers;
12512 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012513 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012514 uint64_t SubMembers = 0;
12515 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12516 return false;
12517 Members += SubMembers * AT->getNumElements();
12518 } else if (Ty->isFloatTy()) {
12519 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12520 return false;
12521 Members = 1;
12522 Base = HA_FLOAT;
12523 } else if (Ty->isDoubleTy()) {
12524 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12525 return false;
12526 Members = 1;
12527 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012528 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012529 Members = 1;
12530 switch (Base) {
12531 case HA_FLOAT:
12532 case HA_DOUBLE:
12533 return false;
12534 case HA_VECT64:
12535 return VT->getBitWidth() == 64;
12536 case HA_VECT128:
12537 return VT->getBitWidth() == 128;
12538 case HA_UNKNOWN:
12539 switch (VT->getBitWidth()) {
12540 case 64:
12541 Base = HA_VECT64;
12542 return true;
12543 case 128:
12544 Base = HA_VECT128;
12545 return true;
12546 default:
12547 return false;
12548 }
12549 }
12550 }
12551
12552 return (Members > 0 && Members <= 4);
12553}
12554
Tim Northovere95c5b32015-02-24 17:22:34 +000012555/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12556/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12557/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012558bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12559 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012560 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12561 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012562 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012563
12564 HABaseType Base = HA_UNKNOWN;
12565 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012566 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12567 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12568
12569 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12570 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012571}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012572
12573unsigned ARMTargetLowering::getExceptionPointerRegister(
12574 const Constant *PersonalityFn) const {
12575 // Platforms which do not use SjLj EH may return values in these registers
12576 // via the personality function.
12577 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12578}
12579
12580unsigned ARMTargetLowering::getExceptionSelectorRegister(
12581 const Constant *PersonalityFn) const {
12582 // Platforms which do not use SjLj EH may return values in these registers
12583 // via the personality function.
12584 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12585}
Manman Ren5e9e65e2016-01-12 00:47:18 +000012586
12587void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
12588 // Update IsSplitCSR in ARMFunctionInfo.
12589 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
12590 AFI->setIsSplitCSR(true);
12591}
12592
12593void ARMTargetLowering::insertCopiesSplitCSR(
12594 MachineBasicBlock *Entry,
12595 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
12596 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
12597 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
12598 if (!IStart)
12599 return;
12600
12601 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
12602 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000012603 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000012604 for (const MCPhysReg *I = IStart; *I; ++I) {
12605 const TargetRegisterClass *RC = nullptr;
12606 if (ARM::GPRRegClass.contains(*I))
12607 RC = &ARM::GPRRegClass;
12608 else if (ARM::DPRRegClass.contains(*I))
12609 RC = &ARM::DPRRegClass;
12610 else
12611 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12612
12613 unsigned NewVR = MRI->createVirtualRegister(RC);
12614 // Create copy from CSR to a virtual register.
12615 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12616 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12617 // nounwind. If we want to generalize this later, we may need to emit
12618 // CFI pseudo-instructions.
12619 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12620 Attribute::NoUnwind) &&
12621 "Function should be nounwind in insertCopiesSplitCSR!");
12622 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000012623 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012624 .addReg(*I);
12625
Manman Rene5f807f2016-01-15 20:24:11 +000012626 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000012627 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000012628 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12629 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012630 .addReg(NewVR);
12631 }
12632}