blob: 0cc41812d71cd4d0aeba84f282c7839e02025e67 [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
146 if (VT.isInteger()) {
147 setOperationAction(ISD::SABSDIFF, VT, Legal);
148 setOperationAction(ISD::UABSDIFF, VT, Legal);
149 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000150 if (!VT.isFloatingPoint() &&
151 VT != MVT::v2i64 && VT != MVT::v1i64)
152 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
153 setOperationAction(Opcode, VT, Legal);
154
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000158 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Craig Topper4fa625f2012-08-12 03:16:37 +0000162void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000163 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000164 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000165}
166
Eric Christopher1889fdc2015-01-29 00:19:39 +0000167ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
168 const ARMSubtarget &STI)
169 : TargetLowering(TM), Subtarget(&STI) {
170 RegInfo = Subtarget->getRegisterInfo();
171 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000172
Duncan Sandsf2641e12011-09-06 19:07:46 +0000173 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
174
Tim Northoverd6a729b2014-01-06 14:28:05 +0000175 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000176 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000177 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000178 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000179 static const struct {
180 const RTLIB::Libcall Op;
181 const char * const Name;
182 const ISD::CondCode Cond;
183 } LibraryCalls[] = {
184 // Single-precision floating-point arithmetic.
185 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
186 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
187 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000189
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000190 // Double-precision floating-point arithmetic.
191 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
192 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
193 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
194 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000195
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000196 // Single-precision comparisons.
197 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
198 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
199 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
200 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
201 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
202 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
203 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
204 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000205
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000206 // Double-precision comparisons.
207 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
208 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
209 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
210 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
211 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
212 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
213 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
214 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000215
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000216 // Floating-point to integer conversions.
217 // i64 conversions are done via library routines even when generating VFP
218 // instructions, so use the same ones.
219 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
220 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
221 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
222 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000223
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000224 // Conversions between floating types.
225 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
226 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000227
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000228 // Integer to floating-point conversions.
229 // i64 conversions are done via library routines even when generating VFP
230 // instructions, so use the same ones.
231 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
232 // e.g., __floatunsidf vs. __floatunssidfvfp.
233 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
234 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
235 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
236 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
237 };
Evan Cheng10043e22007-01-19 07:51:42 +0000238
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000239 for (const auto &LC : LibraryCalls) {
240 setLibcallName(LC.Op, LC.Name);
241 if (LC.Cond != ISD::SETCC_INVALID)
242 setCmpLibcallCC(LC.Op, LC.Cond);
243 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000244 }
Tim Northover8b403662015-10-28 22:51:16 +0000245
246 // Set the correct calling convention for ARMv7k WatchOS. It's just
247 // AAPCS_VFP for functions as simple as libcalls.
248 if (Subtarget->isTargetWatchOS()) {
249 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i)
250 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP);
251 }
Evan Cheng10043e22007-01-19 07:51:42 +0000252 }
253
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000254 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000255 setLibcallName(RTLIB::SHL_I128, nullptr);
256 setLibcallName(RTLIB::SRL_I128, nullptr);
257 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000258
Renato Golin6d435f12015-11-09 12:40:30 +0000259 // RTLIB
260 if (Subtarget->isAAPCS_ABI() &&
261 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
262 Subtarget->isTargetAndroid())) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000263 static const struct {
264 const RTLIB::Libcall Op;
265 const char * const Name;
266 const CallingConv::ID CC;
267 const ISD::CondCode Cond;
268 } LibraryCalls[] = {
269 // Double-precision floating-point arithmetic helper functions
270 // RTABI chapter 4.1.2, Table 2
271 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
272 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
273 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
274 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000275
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000276 // Double-precision floating-point comparison helper functions
277 // RTABI chapter 4.1.2, Table 3
278 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
280 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
281 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
282 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
283 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
284 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
285 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000286
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000287 // Single-precision floating-point arithmetic helper functions
288 // RTABI chapter 4.1.2, Table 4
289 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
290 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
291 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
292 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000293
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000294 // Single-precision floating-point comparison helper functions
295 // RTABI chapter 4.1.2, Table 5
296 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
298 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
299 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
300 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
301 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
302 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
303 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000304
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000305 // Floating-point to integer conversions.
306 // RTABI chapter 4.1.2, Table 6
307 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
310 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
311 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
312 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
313 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
314 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000315
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000316 // Conversions between floating types.
317 // RTABI chapter 4.1.2, Table 7
318 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000319 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000320 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000321
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000322 // Integer to floating-point conversions.
323 // RTABI chapter 4.1.2, Table 8
324 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
329 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
330 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000332
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000333 // Long long helper functions
334 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000335 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000339
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000340 // Integer division functions
341 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000342 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
347 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
348 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
349 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000350 };
351
352 for (const auto &LC : LibraryCalls) {
353 setLibcallName(LC.Op, LC.Name);
354 setLibcallCallingConv(LC.Op, LC.CC);
355 if (LC.Cond != ISD::SETCC_INVALID)
356 setCmpLibcallCC(LC.Op, LC.Cond);
357 }
Renato Golin6d435f12015-11-09 12:40:30 +0000358
359 // EABI dependent RTLIB
360 if (TM.Options.EABIVersion == EABI::EABI4 ||
361 TM.Options.EABIVersion == EABI::EABI5) {
362 static const struct {
363 const RTLIB::Libcall Op;
364 const char *const Name;
365 const CallingConv::ID CC;
366 const ISD::CondCode Cond;
367 } MemOpsLibraryCalls[] = {
368 // Memory operations
369 // RTABI chapter 4.3.4
370 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
371 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
372 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
373 };
374
375 for (const auto &LC : MemOpsLibraryCalls) {
376 setLibcallName(LC.Op, LC.Name);
377 setLibcallCallingConv(LC.Op, LC.CC);
378 if (LC.Cond != ISD::SETCC_INVALID)
379 setCmpLibcallCC(LC.Op, LC.Cond);
380 }
381 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000382 }
383
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000384 if (Subtarget->isTargetWindows()) {
385 static const struct {
386 const RTLIB::Libcall Op;
387 const char * const Name;
388 const CallingConv::ID CC;
389 } LibraryCalls[] = {
390 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
391 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
392 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
393 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
394 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
395 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
396 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
397 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
Martell Malonea6b867e2015-11-23 13:11:39 +0000398 { RTLIB::SDIV_I32, "__rt_sdiv", CallingConv::ARM_AAPCS_VFP },
Martell Maloned1229242015-11-26 15:34:03 +0000399 { RTLIB::UDIV_I32, "__rt_udiv", CallingConv::ARM_AAPCS_VFP },
Martell Malonea6b867e2015-11-23 13:11:39 +0000400 { RTLIB::SDIV_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS_VFP },
Martell Maloned1229242015-11-26 15:34:03 +0000401 { RTLIB::UDIV_I64, "__rt_udiv64", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000402 };
403
404 for (const auto &LC : LibraryCalls) {
405 setLibcallName(LC.Op, LC.Name);
406 setLibcallCallingConv(LC.Op, LC.CC);
407 }
408 }
409
Bob Wilsonbc158992011-10-07 16:59:21 +0000410 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000411 if (Subtarget->isTargetWatchOS() ||
412 (Subtarget->isTargetIOS() &&
413 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000414 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
415 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
416 }
417
Oliver Stannard11790b22014-08-11 09:12:32 +0000418 // The half <-> float conversion functions are always soft-float, but are
419 // needed for some targets which use a hard-float calling convention by
420 // default.
421 if (Subtarget->isAAPCS_ABI()) {
422 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
423 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
424 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
425 } else {
426 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
427 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
428 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
429 }
430
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000431 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
432 // a __gnu_ prefix (which is the default).
433 if (Subtarget->isTargetAEABI()) {
434 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
435 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
436 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
437 }
438
David Goodwin22c2fba2009-07-08 23:10:31 +0000439 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000440 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000441 else
Craig Topperc7242e02012-04-20 07:30:17 +0000442 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000443 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000444 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000445 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000446 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000447 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000448
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000449 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000450 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000451 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000452 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
453 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
454 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
455 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000456
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000457 setOperationAction(ISD::MULHS, VT, Expand);
458 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
459 setOperationAction(ISD::MULHU, VT, Expand);
460 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000461
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000462 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000463 }
464
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000465 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000466 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000467
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000468 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
469 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
470
Bob Wilson2e076c42009-06-22 23:27:02 +0000471 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000472 addDRTypeForNEON(MVT::v2f32);
473 addDRTypeForNEON(MVT::v8i8);
474 addDRTypeForNEON(MVT::v4i16);
475 addDRTypeForNEON(MVT::v2i32);
476 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000477
Owen Anderson9f944592009-08-11 20:47:22 +0000478 addQRTypeForNEON(MVT::v4f32);
479 addQRTypeForNEON(MVT::v2f64);
480 addQRTypeForNEON(MVT::v16i8);
481 addQRTypeForNEON(MVT::v8i16);
482 addQRTypeForNEON(MVT::v4i32);
483 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000484
Bob Wilson194a2512009-09-15 23:55:57 +0000485 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
486 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000487 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
488 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000489 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
490 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
491 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000492 // FIXME: Code duplication: FDIV and FREM are expanded always, see
493 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000494 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
495 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000496 // FIXME: Create unittest.
497 // In another words, find a way when "copysign" appears in DAG with vector
498 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000499 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000500 // FIXME: Code duplication: SETCC has custom operation action, see
501 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000502 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000503 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000504 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
505 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
506 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
507 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
508 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
509 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
510 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
511 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
512 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
513 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
514 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
515 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000516 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000517 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
518 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
519 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
520 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
521 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000522 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000523
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000524 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
525 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
526 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
527 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
528 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
529 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
530 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
531 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
532 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
533 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000534 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
535 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
536 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
537 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000538 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000539
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000540 // Mark v2f32 intrinsics.
541 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
542 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
543 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
544 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
545 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
546 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
547 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
548 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
549 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
550 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
551 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
552 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
553 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
554 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
555 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
556
Bob Wilson6cc46572009-09-16 00:32:15 +0000557 // Neon does not support some operations on v1i64 and v2i64 types.
558 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000559 // Custom handling for some quad-vector types to detect VMULL.
560 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
561 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
562 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000563 // Custom handling for some vector types to avoid expensive expansions
564 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
565 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
566 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
567 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000568 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
569 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000570 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000571 // a destination type that is wider than the source, and nor does
572 // it have a FP_TO_[SU]INT instruction with a narrower destination than
573 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000574 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
575 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000576 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
577 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000578
Eli Friedmane6385e62012-11-15 22:44:27 +0000579 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000580 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000581
Evan Chengb4eae132012-12-04 22:41:50 +0000582 // NEON does not have single instruction CTPOP for vectors with element
583 // types wider than 8-bits. However, custom lowering can leverage the
584 // v8i8/v16i8 vcnt instruction.
585 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
586 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
587 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
588 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
589
Logan Chien0a43abc2015-07-13 15:37:30 +0000590 // NEON does not have single instruction CTTZ for vectors.
591 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
592 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
593 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
594 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
595
596 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
597 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
598 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
599 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
600
601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
602 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
603 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
604 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
605
606 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
607 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
608 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
609 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
610
Jim Grosbach5f215872013-02-27 21:31:12 +0000611 // NEON only has FMA instructions as of VFP4.
612 if (!Subtarget->hasVFP4()) {
613 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
614 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
615 }
616
Bob Wilson06fce872011-02-07 17:43:21 +0000617 setTargetDAGCombine(ISD::INTRINSIC_VOID);
618 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000619 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
620 setTargetDAGCombine(ISD::SHL);
621 setTargetDAGCombine(ISD::SRL);
622 setTargetDAGCombine(ISD::SRA);
623 setTargetDAGCombine(ISD::SIGN_EXTEND);
624 setTargetDAGCombine(ISD::ZERO_EXTEND);
625 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000626 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000627 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000628 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
629 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000630 setTargetDAGCombine(ISD::FP_TO_SINT);
631 setTargetDAGCombine(ISD::FP_TO_UINT);
632 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000633 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000634
James Molloy547d4c02012-02-20 09:24:05 +0000635 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000636 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
637 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000638 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000639 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
640 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
641 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000642 }
James Molloy547d4c02012-02-20 09:24:05 +0000643 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000644 }
645
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000646 // ARM and Thumb2 support UMLAL/SMLAL.
647 if (!Subtarget->isThumb1Only())
648 setTargetDAGCombine(ISD::ADDC);
649
Oliver Stannard51b1d462014-08-21 12:50:31 +0000650 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000651 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000652 // operations, f64 is legal for the few double-precision instructions which
653 // are present However, no double-precision operations other than moves,
654 // loads and stores are provided by the hardware.
655 setOperationAction(ISD::FADD, MVT::f64, Expand);
656 setOperationAction(ISD::FSUB, MVT::f64, Expand);
657 setOperationAction(ISD::FMUL, MVT::f64, Expand);
658 setOperationAction(ISD::FMA, MVT::f64, Expand);
659 setOperationAction(ISD::FDIV, MVT::f64, Expand);
660 setOperationAction(ISD::FREM, MVT::f64, Expand);
661 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
662 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
663 setOperationAction(ISD::FNEG, MVT::f64, Expand);
664 setOperationAction(ISD::FABS, MVT::f64, Expand);
665 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
666 setOperationAction(ISD::FSIN, MVT::f64, Expand);
667 setOperationAction(ISD::FCOS, MVT::f64, Expand);
668 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
669 setOperationAction(ISD::FPOW, MVT::f64, Expand);
670 setOperationAction(ISD::FLOG, MVT::f64, Expand);
671 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
672 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
673 setOperationAction(ISD::FEXP, MVT::f64, Expand);
674 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
675 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
676 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
677 setOperationAction(ISD::FRINT, MVT::f64, Expand);
678 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
679 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000680 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
681 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
682 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
683 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
684 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
685 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000686 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
687 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
688 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000689
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000690 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000691
Tim Northover4e80b582014-07-18 13:01:19 +0000692 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000693 for (MVT VT : MVT::fp_valuetypes()) {
694 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
695 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
696 }
Tim Northover4e80b582014-07-18 13:01:19 +0000697
698 // ... or truncating stores
699 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
700 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
701 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000702
Duncan Sands95d46ef2008-01-23 20:39:46 +0000703 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000704 for (MVT VT : MVT::integer_valuetypes())
705 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000706
Evan Cheng10043e22007-01-19 07:51:42 +0000707 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000708 if (!Subtarget->isThumb1Only()) {
709 for (unsigned im = (unsigned)ISD::PRE_INC;
710 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000711 setIndexedLoadAction(im, MVT::i1, Legal);
712 setIndexedLoadAction(im, MVT::i8, Legal);
713 setIndexedLoadAction(im, MVT::i16, Legal);
714 setIndexedLoadAction(im, MVT::i32, Legal);
715 setIndexedStoreAction(im, MVT::i1, Legal);
716 setIndexedStoreAction(im, MVT::i8, Legal);
717 setIndexedStoreAction(im, MVT::i16, Legal);
718 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000719 }
Evan Cheng10043e22007-01-19 07:51:42 +0000720 }
721
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000722 setOperationAction(ISD::SADDO, MVT::i32, Custom);
723 setOperationAction(ISD::UADDO, MVT::i32, Custom);
724 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
725 setOperationAction(ISD::USUBO, MVT::i32, Custom);
726
Evan Cheng10043e22007-01-19 07:51:42 +0000727 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000728 setOperationAction(ISD::MUL, MVT::i64, Expand);
729 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000730 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000731 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
732 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000733 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000734 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000735 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000736 setOperationAction(ISD::MULHS, MVT::i32, Expand);
737
Jim Grosbach5d994042009-10-31 19:38:01 +0000738 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000739 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000740 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000741 setOperationAction(ISD::SRL, MVT::i64, Custom);
742 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000743
Evan Chenge8916542011-08-30 01:34:54 +0000744 if (!Subtarget->isThumb1Only()) {
745 // FIXME: We should do this for Thumb1 as well.
746 setOperationAction(ISD::ADDC, MVT::i32, Custom);
747 setOperationAction(ISD::ADDE, MVT::i32, Custom);
748 setOperationAction(ISD::SUBC, MVT::i32, Custom);
749 setOperationAction(ISD::SUBE, MVT::i32, Custom);
750 }
751
James Molloyb5640982015-11-13 16:05:22 +0000752 if (!Subtarget->isThumb1Only())
753 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
754
Evan Cheng10043e22007-01-19 07:51:42 +0000755 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000756 setOperationAction(ISD::ROTL, MVT::i32, Expand);
757 for (MVT VT : MVT::vector_valuetypes()) {
758 setOperationAction(ISD::ROTL, VT, Expand);
759 setOperationAction(ISD::ROTR, VT, Expand);
760 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000761 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000762 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000763 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000764 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000765
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000766 // These just redirect to CTTZ and CTLZ on ARM.
767 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
768 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
769
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000770 // @llvm.readcyclecounter requires the Performance Monitors extension.
771 // Default to the 0 expansion on unsupported platforms.
772 // FIXME: Technically there are older ARM CPUs that have
773 // implementation-specific ways of obtaining this information.
774 if (Subtarget->hasPerfMon())
775 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000776
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000777 // Only ARMv6 has BSWAP.
778 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000779 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000780
Bob Wilsone8a549c2012-09-29 21:43:49 +0000781 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
782 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
783 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000784 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
785 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000786 }
Renato Golin87610692013-07-16 09:32:17 +0000787
Chad Rosierad7c9102014-08-23 18:29:43 +0000788 setOperationAction(ISD::SREM, MVT::i32, Expand);
789 setOperationAction(ISD::UREM, MVT::i32, Expand);
790 // Register based DivRem for AEABI (RTABI 4.2)
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000791 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000792 setOperationAction(ISD::SREM, MVT::i64, Custom);
793 setOperationAction(ISD::UREM, MVT::i64, Custom);
794
Chad Rosierad7c9102014-08-23 18:29:43 +0000795 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
796 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
797 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
798 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
799 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
800 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
801 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
802 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
803
804 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
805 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
806 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
807 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
808 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
809 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
810 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
811 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
812
813 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
814 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
815 } else {
Renato Golin87610692013-07-16 09:32:17 +0000816 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
817 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
818 }
Bob Wilson7117a912009-03-20 22:42:55 +0000819
Owen Anderson9f944592009-08-11 20:47:22 +0000820 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
821 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000822 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000823 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000824
Evan Cheng74d92c12011-04-08 21:37:21 +0000825 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000826
Evan Cheng10043e22007-01-19 07:51:42 +0000827 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000828 setOperationAction(ISD::VASTART, MVT::Other, Custom);
829 setOperationAction(ISD::VAARG, MVT::Other, Expand);
830 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
831 setOperationAction(ISD::VAEND, MVT::Other, Expand);
832 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
833 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000834
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000835 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
836 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
837 else
838 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
839
Evan Cheng6e809de2010-08-11 06:22:01 +0000840 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000841 // the default expansion. If we are targeting a single threaded system,
842 // then set them all for expand so we can lower them later into their
843 // non-atomic form.
844 if (TM.Options.ThreadModel == ThreadModel::Single)
845 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
846 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000847 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
848 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000849 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000850
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000851 // On v8, we have particularly efficient implementations of atomic fences
852 // if they can be combined with nearby atomic loads and stores.
853 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000854 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000855 setInsertFencesForAtomic(true);
856 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000857 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000858 // If there's anything we can use as a barrier, go through custom lowering
859 // for ATOMIC_FENCE.
860 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
861 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
862
Jim Grosbach6860bb72010-06-18 22:35:32 +0000863 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000864 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000865 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000866 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000867 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000868 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000869 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000870 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000871 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000872 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000873 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000874 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000875 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000876 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
877 // Unordered/Monotonic case.
878 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
879 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000880 }
Evan Cheng10043e22007-01-19 07:51:42 +0000881
Evan Cheng21acf9f2010-11-04 05:19:35 +0000882 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000883
Eli Friedman8cfa7712010-06-26 04:36:50 +0000884 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
885 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000886 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
887 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000888 }
Owen Anderson9f944592009-08-11 20:47:22 +0000889 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000890
Eric Christopher824f42f2015-05-12 01:26:05 +0000891 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000892 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000893 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000894 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000895 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000896 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
897 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000898
899 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000900 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000901 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
902 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
903 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +0000904 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +0000905 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000906
Owen Anderson9f944592009-08-11 20:47:22 +0000907 setOperationAction(ISD::SETCC, MVT::i32, Expand);
908 setOperationAction(ISD::SETCC, MVT::f32, Expand);
909 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000910 setOperationAction(ISD::SELECT, MVT::i32, Custom);
911 setOperationAction(ISD::SELECT, MVT::f32, Custom);
912 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000913 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
914 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
915 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000916
Owen Anderson9f944592009-08-11 20:47:22 +0000917 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
918 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
919 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
920 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
921 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000922
Dan Gohman482732a2007-10-11 23:21:31 +0000923 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000924 setOperationAction(ISD::FSIN, MVT::f64, Expand);
925 setOperationAction(ISD::FSIN, MVT::f32, Expand);
926 setOperationAction(ISD::FCOS, MVT::f32, Expand);
927 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000928 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
929 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000930 setOperationAction(ISD::FREM, MVT::f64, Expand);
931 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000932 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000933 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000934 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
935 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000936 }
Owen Anderson9f944592009-08-11 20:47:22 +0000937 setOperationAction(ISD::FPOW, MVT::f64, Expand);
938 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000939
Evan Chengd0007f32012-04-10 21:40:28 +0000940 if (!Subtarget->hasVFP4()) {
941 setOperationAction(ISD::FMA, MVT::f64, Expand);
942 setOperationAction(ISD::FMA, MVT::f32, Expand);
943 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000944
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000945 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000946 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000947 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
948 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000949 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
950 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
951 }
952
953 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000954 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000955 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
956 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000957 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000958 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000959
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000960 // Combine sin / cos into one node or libcall if possible.
961 if (Subtarget->hasSinCos()) {
962 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
963 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover8b403662015-10-28 22:51:16 +0000964 if (Subtarget->isTargetWatchOS()) {
965 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
966 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
967 }
968 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000969 // For iOS, we don't want to the normal expansion of a libcall to
970 // sincos. We want to issue a libcall to __sincos_stret.
971 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
972 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
973 }
974 }
Evan Cheng10043e22007-01-19 07:51:42 +0000975
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000976 // FP-ARMv8 implements a lot of rounding-like FP operations.
977 if (Subtarget->hasFPARMv8()) {
978 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
979 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
980 setOperationAction(ISD::FROUND, MVT::f32, Legal);
981 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
982 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
983 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000984 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
985 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000986 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
987 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
988 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
989 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
990
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000991 if (!Subtarget->isFPOnlySP()) {
992 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
993 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
994 setOperationAction(ISD::FROUND, MVT::f64, Legal);
995 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
996 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
997 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000998 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
999 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001000 }
1001 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001002
James Molloy974838f2015-08-17 19:37:12 +00001003 if (Subtarget->hasNEON()) {
1004 // vmin and vmax aren't available in a scalar form, so we use
1005 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001006 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1007 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001008 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1009 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1010 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1011 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1012 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001013
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001014 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001015 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001016 setTargetDAGCombine(ISD::ADD);
1017 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001018 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001019 setTargetDAGCombine(ISD::AND);
1020 setTargetDAGCombine(ISD::OR);
1021 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001022
Evan Chengf258a152012-02-23 02:58:19 +00001023 if (Subtarget->hasV6Ops())
1024 setTargetDAGCombine(ISD::SRL);
1025
Evan Cheng10043e22007-01-19 07:51:42 +00001026 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001027
Eric Christopher824f42f2015-05-12 01:26:05 +00001028 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001029 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001030 setSchedulingPreference(Sched::RegPressure);
1031 else
1032 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001033
Evan Cheng3ae2b792011-01-06 06:52:41 +00001034 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001035 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001036 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001037 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001038 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001039 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001040 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001041
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001042 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1043 // are at least 4 bytes aligned.
1044 setMinStackArgumentAlignment(4);
1045
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001046 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001047 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001048
Eli Friedman2518f832011-05-06 20:34:06 +00001049 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001050}
1051
Eric Christopher824f42f2015-05-12 01:26:05 +00001052bool ARMTargetLowering::useSoftFloat() const {
1053 return Subtarget->useSoftFloat();
1054}
1055
Andrew Trick43f25632011-01-19 02:35:27 +00001056// FIXME: It might make sense to define the representative register class as the
1057// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1058// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1059// SPR's representative would be DPR_VFP2. This should work well if register
1060// pressure tracking were modified such that a register use would increment the
1061// pressure of the register class's representative and all of it's super
1062// classes' representatives transitively. We have not implemented this because
1063// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001064// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001065// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001066std::pair<const TargetRegisterClass *, uint8_t>
1067ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1068 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001069 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001070 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001071 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001072 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001073 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001074 // Use DPR as representative register class for all floating point
1075 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1076 // the cost is 1 for both f32 and f64.
1077 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001078 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001079 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001080 // When NEON is used for SP, only half of the register file is available
1081 // because operations that define both SP and DP results will be constrained
1082 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1083 // coalescing by double-counting the SP regs. See the FIXME above.
1084 if (Subtarget->useNEONForSinglePrecisionFP())
1085 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001086 break;
1087 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1088 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001089 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001090 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001091 break;
1092 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001093 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001094 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001095 break;
1096 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001097 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001098 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001099 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001100 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001101 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001102}
1103
Evan Cheng10043e22007-01-19 07:51:42 +00001104const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001105 switch ((ARMISD::NodeType)Opcode) {
1106 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001107 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001108 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001109 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001110 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001111 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001112 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001113 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1114 case ARMISD::tCALL: return "ARMISD::tCALL";
1115 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1116 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001117 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001118 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001119 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001120 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1121 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001122 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001123 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001124 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1125 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001126 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001127 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001128
Evan Cheng10043e22007-01-19 07:51:42 +00001129 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001130
Evan Cheng10043e22007-01-19 07:51:42 +00001131 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1132 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1133 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001134
Evan Chenge8916542011-08-30 01:34:54 +00001135 case ARMISD::ADDC: return "ARMISD::ADDC";
1136 case ARMISD::ADDE: return "ARMISD::ADDE";
1137 case ARMISD::SUBC: return "ARMISD::SUBC";
1138 case ARMISD::SUBE: return "ARMISD::SUBE";
1139
Bob Wilson22806742010-09-22 22:09:21 +00001140 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1141 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001142
Evan Chengec6d7c92009-10-28 06:55:03 +00001143 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001144 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1145 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001146
Dale Johannesend679ff72010-06-03 21:09:53 +00001147 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001148
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001149 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001150
Evan Chengb972e562009-08-07 00:34:42 +00001151 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1152
Bob Wilson7ed59712010-10-30 00:54:37 +00001153 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001154
Evan Cheng8740ee32010-11-03 06:34:55 +00001155 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1156
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001157 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001158 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001159
Bob Wilson2e076c42009-06-22 23:27:02 +00001160 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001161 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001162 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001163 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1164 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001165 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1166 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001167 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1168 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001169 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1170 case ARMISD::VTST: return "ARMISD::VTST";
1171
1172 case ARMISD::VSHL: return "ARMISD::VSHL";
1173 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1174 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001175 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1176 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1177 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1178 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1179 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1180 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1181 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1182 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1183 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1184 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1185 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1186 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001187 case ARMISD::VSLI: return "ARMISD::VSLI";
1188 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001189 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1190 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001191 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001192 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001193 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001194 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001195 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001196 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001197 case ARMISD::VREV64: return "ARMISD::VREV64";
1198 case ARMISD::VREV32: return "ARMISD::VREV32";
1199 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001200 case ARMISD::VZIP: return "ARMISD::VZIP";
1201 case ARMISD::VUZP: return "ARMISD::VUZP";
1202 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001203 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1204 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001205 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1206 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001207 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1208 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001209 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001210 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001211 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1212 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001213 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001214 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001215 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1216 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1217 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001218 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1219 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1220 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1221 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1222 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1223 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1224 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1225 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1226 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1227 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1228 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1229 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1230 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1231 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1232 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1233 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1234 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001235 }
Matthias Braund04893f2015-05-07 21:33:59 +00001236 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001237}
1238
Mehdi Amini44ede332015-07-09 02:09:04 +00001239EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1240 EVT VT) const {
1241 if (!VT.isVector())
1242 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001243 return VT.changeVectorElementTypeToInteger();
1244}
1245
Evan Cheng4cad68e2010-05-15 02:18:07 +00001246/// getRegClassFor - Return the register class that should be used for the
1247/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001248const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001249 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1250 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1251 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001252 if (Subtarget->hasNEON()) {
1253 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001254 return &ARM::QQPRRegClass;
1255 if (VT == MVT::v8i64)
1256 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001257 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001258 return TargetLowering::getRegClassFor(VT);
1259}
1260
John Brawn0dbcd652015-03-18 12:01:59 +00001261// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1262// source/dest is aligned and the copy size is large enough. We therefore want
1263// to align such objects passed to memory intrinsics.
1264bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1265 unsigned &PrefAlign) const {
1266 if (!isa<MemIntrinsic>(CI))
1267 return false;
1268 MinSize = 8;
1269 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1270 // cycle faster than 4-byte aligned LDM.
1271 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1272 return true;
1273}
1274
Eric Christopher84bdfd82010-07-21 22:26:11 +00001275// Create a fast isel object.
1276FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001277ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1278 const TargetLibraryInfo *libInfo) const {
1279 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001280}
1281
Evan Cheng4401f882010-05-20 23:26:43 +00001282Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001283 unsigned NumVals = N->getNumValues();
1284 if (!NumVals)
1285 return Sched::RegPressure;
1286
1287 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001288 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001289 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001290 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001291 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001292 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001293 }
Evan Chengbf914992010-05-28 23:25:23 +00001294
1295 if (!N->isMachineOpcode())
1296 return Sched::RegPressure;
1297
1298 // Load are scheduled for latency even if there instruction itinerary
1299 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001300 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001301 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001302
Evan Cheng6cc775f2011-06-28 19:10:37 +00001303 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001304 return Sched::RegPressure;
1305 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001306 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001307 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001308
Evan Cheng4401f882010-05-20 23:26:43 +00001309 return Sched::RegPressure;
1310}
1311
Evan Cheng10043e22007-01-19 07:51:42 +00001312//===----------------------------------------------------------------------===//
1313// Lowering Code
1314//===----------------------------------------------------------------------===//
1315
Evan Cheng10043e22007-01-19 07:51:42 +00001316/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1317static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1318 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001319 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001320 case ISD::SETNE: return ARMCC::NE;
1321 case ISD::SETEQ: return ARMCC::EQ;
1322 case ISD::SETGT: return ARMCC::GT;
1323 case ISD::SETGE: return ARMCC::GE;
1324 case ISD::SETLT: return ARMCC::LT;
1325 case ISD::SETLE: return ARMCC::LE;
1326 case ISD::SETUGT: return ARMCC::HI;
1327 case ISD::SETUGE: return ARMCC::HS;
1328 case ISD::SETULT: return ARMCC::LO;
1329 case ISD::SETULE: return ARMCC::LS;
1330 }
1331}
1332
Bob Wilsona2e83332009-09-09 23:14:54 +00001333/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1334static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001335 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001336 CondCode2 = ARMCC::AL;
1337 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001338 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001339 case ISD::SETEQ:
1340 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1341 case ISD::SETGT:
1342 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1343 case ISD::SETGE:
1344 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1345 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001346 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001347 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1348 case ISD::SETO: CondCode = ARMCC::VC; break;
1349 case ISD::SETUO: CondCode = ARMCC::VS; break;
1350 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1351 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1352 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1353 case ISD::SETLT:
1354 case ISD::SETULT: CondCode = ARMCC::LT; break;
1355 case ISD::SETLE:
1356 case ISD::SETULE: CondCode = ARMCC::LE; break;
1357 case ISD::SETNE:
1358 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1359 }
Evan Cheng10043e22007-01-19 07:51:42 +00001360}
1361
Bob Wilsona4c22902009-04-17 19:07:39 +00001362//===----------------------------------------------------------------------===//
1363// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001364//===----------------------------------------------------------------------===//
1365
1366#include "ARMGenCallingConv.inc"
1367
Oliver Stannardc24f2172014-05-09 14:01:47 +00001368/// getEffectiveCallingConv - Get the effective calling convention, taking into
1369/// account presence of floating point hardware and calling convention
1370/// limitations, such as support for variadic functions.
1371CallingConv::ID
1372ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1373 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001374 switch (CC) {
1375 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001376 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001377 case CallingConv::ARM_AAPCS:
1378 case CallingConv::ARM_APCS:
1379 case CallingConv::GHC:
1380 return CC;
1381 case CallingConv::ARM_AAPCS_VFP:
1382 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1383 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001384 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001385 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001386 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001387 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1388 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001389 return CallingConv::ARM_AAPCS_VFP;
1390 else
1391 return CallingConv::ARM_AAPCS;
1392 case CallingConv::Fast:
1393 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001394 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001395 return CallingConv::Fast;
1396 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001397 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001398 return CallingConv::ARM_AAPCS_VFP;
1399 else
1400 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001401 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001402}
1403
1404/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1405/// CallingConvention.
1406CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1407 bool Return,
1408 bool isVarArg) const {
1409 switch (getEffectiveCallingConv(CC, isVarArg)) {
1410 default:
1411 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001412 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001413 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001414 case CallingConv::ARM_AAPCS:
1415 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1416 case CallingConv::ARM_AAPCS_VFP:
1417 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1418 case CallingConv::Fast:
1419 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001420 case CallingConv::GHC:
1421 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001422 }
1423}
1424
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001425/// LowerCallResult - Lower the result values of a call into the
1426/// appropriate copies out of appropriate physical registers.
1427SDValue
1428ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001429 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001430 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001431 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001432 SmallVectorImpl<SDValue> &InVals,
1433 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001434
Bob Wilsona4c22902009-04-17 19:07:39 +00001435 // Assign locations to each value returned by this call.
1436 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001437 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1438 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001439 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001440 CCAssignFnForNode(CallConv, /* Return*/ true,
1441 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001442
1443 // Copy all of the result registers out of their specified physreg.
1444 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1445 CCValAssign VA = RVLocs[i];
1446
Stephen Linb8bd2322013-04-20 05:14:40 +00001447 // Pass 'this' value directly from the argument to return value, to avoid
1448 // reg unit interference
1449 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001450 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1451 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001452 InVals.push_back(ThisVal);
1453 continue;
1454 }
1455
Bob Wilson0041bd32009-04-25 00:33:20 +00001456 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001457 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001458 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001459 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001460 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001461 Chain = Lo.getValue(1);
1462 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001463 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001464 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001465 InFlag);
1466 Chain = Hi.getValue(1);
1467 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001468 if (!Subtarget->isLittle())
1469 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001470 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001471
Owen Anderson9f944592009-08-11 20:47:22 +00001472 if (VA.getLocVT() == MVT::v2f64) {
1473 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1474 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001475 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001476
1477 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001478 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001479 Chain = Lo.getValue(1);
1480 InFlag = Lo.getValue(2);
1481 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001482 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001483 Chain = Hi.getValue(1);
1484 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001485 if (!Subtarget->isLittle())
1486 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001487 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001488 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001489 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001490 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001491 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001492 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1493 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001494 Chain = Val.getValue(1);
1495 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001496 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001497
1498 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001499 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001500 case CCValAssign::Full: break;
1501 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001502 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001503 break;
1504 }
1505
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001506 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001507 }
1508
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001509 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001510}
1511
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001512/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001513SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001514ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1515 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001516 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001517 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001518 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001519 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001520 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001521 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1522 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001523 return DAG.getStore(
1524 Chain, dl, Arg, PtrOff,
1525 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1526 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001527}
1528
Andrew Trickef9de2a2013-05-25 02:42:55 +00001529void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001530 SDValue Chain, SDValue &Arg,
1531 RegsToPassVector &RegsToPass,
1532 CCValAssign &VA, CCValAssign &NextVA,
1533 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001534 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001535 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001536
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001537 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001538 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001539 unsigned id = Subtarget->isLittle() ? 0 : 1;
1540 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001541
1542 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001543 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001544 else {
1545 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001546 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001547 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1548 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001549
Christian Pirkerb5728192014-05-08 14:06:24 +00001550 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001551 dl, DAG, NextVA,
1552 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001553 }
1554}
1555
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001556/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001557/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1558/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001559SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001560ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001561 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001562 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001563 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001564 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1565 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1566 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001567 SDValue Chain = CLI.Chain;
1568 SDValue Callee = CLI.Callee;
1569 bool &isTailCall = CLI.IsTailCall;
1570 CallingConv::ID CallConv = CLI.CallConv;
1571 bool doesNotRet = CLI.DoesNotReturn;
1572 bool isVarArg = CLI.IsVarArg;
1573
Dale Johannesend679ff72010-06-03 21:09:53 +00001574 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001575 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1576 bool isThisReturn = false;
1577 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001578 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001579
Bob Wilson8decdc42011-10-07 17:17:49 +00001580 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001581 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001582 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001583
Dale Johannesend679ff72010-06-03 21:09:53 +00001584 if (isTailCall) {
1585 // Check if it's really possible to do a tail call.
1586 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001587 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001588 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001589 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1590 report_fatal_error("failed to perform tail call elimination on a call "
1591 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001592 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1593 // detected sibcalls.
1594 if (isTailCall) {
1595 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001596 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001597 }
1598 }
Evan Cheng10043e22007-01-19 07:51:42 +00001599
Bob Wilsona4c22902009-04-17 19:07:39 +00001600 // Analyze operands of the call, assigning locations to each operand.
1601 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001602 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1603 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001604 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001605 CCAssignFnForNode(CallConv, /* Return*/ false,
1606 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001607
Bob Wilsona4c22902009-04-17 19:07:39 +00001608 // Get a count of how many bytes are to be pushed on the stack.
1609 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001610
Dale Johannesend679ff72010-06-03 21:09:53 +00001611 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001612 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001613 NumBytes = 0;
1614
Evan Cheng10043e22007-01-19 07:51:42 +00001615 // Adjust the stack pointer for the new arguments...
1616 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001617 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001618 Chain = DAG.getCALLSEQ_START(Chain,
1619 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001620
Mehdi Amini44ede332015-07-09 02:09:04 +00001621 SDValue StackPtr =
1622 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001623
Bob Wilson2e076c42009-06-22 23:27:02 +00001624 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001625 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001626
Bob Wilsona4c22902009-04-17 19:07:39 +00001627 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001628 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001629 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1630 i != e;
1631 ++i, ++realArgIdx) {
1632 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001633 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001634 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001635 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001636
Bob Wilsona4c22902009-04-17 19:07:39 +00001637 // Promote the value if needed.
1638 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001639 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001640 case CCValAssign::Full: break;
1641 case CCValAssign::SExt:
1642 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1643 break;
1644 case CCValAssign::ZExt:
1645 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1646 break;
1647 case CCValAssign::AExt:
1648 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1649 break;
1650 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001651 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001652 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001653 }
1654
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001655 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001656 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001657 if (VA.getLocVT() == MVT::v2f64) {
1658 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001659 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001660 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001661 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001662
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001663 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001664 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1665
1666 VA = ArgLocs[++i]; // skip ahead to next loc
1667 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001668 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001669 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1670 } else {
1671 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001672
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001673 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1674 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001675 }
1676 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001677 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001678 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001679 }
1680 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001681 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1682 assert(VA.getLocVT() == MVT::i32 &&
1683 "unexpected calling convention register assignment");
1684 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001685 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001686 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001687 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001688 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001689 } else if (isByVal) {
1690 assert(VA.isMemLoc());
1691 unsigned offset = 0;
1692
1693 // True if this byval aggregate will be split between registers
1694 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001695 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001696 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001697
1698 if (CurByValIdx < ByValArgsCount) {
1699
1700 unsigned RegBegin, RegEnd;
1701 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1702
Mehdi Amini44ede332015-07-09 02:09:04 +00001703 EVT PtrVT =
1704 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001705 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001706 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001707 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001708 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1709 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1710 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001711 false, false, false,
1712 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001713 MemOpChains.push_back(Load.getValue(1));
1714 RegsToPass.push_back(std::make_pair(j, Load));
1715 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001716
1717 // If parameter size outsides register area, "offset" value
1718 // helps us to calculate stack slot for remained part properly.
1719 offset = RegEnd - RegBegin;
1720
1721 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001722 }
1723
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001724 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001725 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001726 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001727 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001728 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001729 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001730 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001731 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001732 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001733 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1734 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001735
Manman Ren9f911162012-06-01 02:44:42 +00001736 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001737 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001738 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001739 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001740 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001741 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001742 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001743
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001744 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1745 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001746 }
Evan Cheng10043e22007-01-19 07:51:42 +00001747 }
1748
1749 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001750 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001751
1752 // Build a sequence of copy-to-reg nodes chained together with token chain
1753 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001754 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001755 // Tail call byval lowering might overwrite argument registers so in case of
1756 // tail call optimization the copies to registers are lowered later.
1757 if (!isTailCall)
1758 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1759 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1760 RegsToPass[i].second, InFlag);
1761 InFlag = Chain.getValue(1);
1762 }
Evan Cheng10043e22007-01-19 07:51:42 +00001763
Dale Johannesend679ff72010-06-03 21:09:53 +00001764 // For tail calls lower the arguments to the 'real' stack slot.
1765 if (isTailCall) {
1766 // Force all the incoming stack arguments to be loaded from the stack
1767 // before any new outgoing arguments are stored to the stack, because the
1768 // outgoing stack slots may alias the incoming argument stack slots, and
1769 // the alias isn't otherwise explicit. This is slightly more conservative
1770 // than necessary, because it means that each store effectively depends
1771 // on every argument instead of just those arguments it would clobber.
1772
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001773 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001774 InFlag = SDValue();
1775 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1776 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1777 RegsToPass[i].second, InFlag);
1778 InFlag = Chain.getValue(1);
1779 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001780 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001781 }
1782
Bill Wendling24c79f22008-09-16 21:48:12 +00001783 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1784 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1785 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001786 bool isDirect = false;
1787 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001788 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001789 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001790 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001791
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001792 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001793 assert((Subtarget->isTargetWindows() ||
1794 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1795 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001796 // Handle a global address or an external symbol. If it's not one of
1797 // those, the target's already in a register, so we don't need to do
1798 // anything extra.
1799 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001800 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001801 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001802 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001803 ARMConstantPoolValue *CPV =
1804 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1805
Jim Grosbach32bb3622010-04-14 22:28:31 +00001806 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001807 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001808 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001809 Callee = DAG.getLoad(
1810 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1811 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1812 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001813 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1814 const char *Sym = S->getSymbol();
1815
1816 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001817 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001818 ARMConstantPoolValue *CPV =
1819 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1820 ARMPCLabelIndex, 0);
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 }
1829 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001830 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001831 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001832 bool isDef = GV->isStrongDefinitionForLinker();
1833 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001834 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001835 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001836 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001837 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001838 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001839 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001840 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001841 Callee = DAG.getNode(
1842 ARMISD::WrapperPIC, dl, PtrVt,
1843 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1844 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001845 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1846 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001847 } else if (Subtarget->isTargetCOFF()) {
1848 assert(Subtarget->isTargetWindows() &&
1849 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001850 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1851 ? ARMII::MO_DLLIMPORT
1852 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001853 Callee =
1854 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001855 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001856 Callee =
1857 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1858 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001859 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1860 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001861 } else {
1862 // On ELF targets for PIC code, direct calls should go through the PLT
1863 unsigned OpFlags = 0;
1864 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001865 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001866 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001867 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001868 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001869 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001870 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001871 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001872 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001873 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001874 // tBX takes a register source operand.
1875 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001876 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001877 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001878 ARMConstantPoolValue *CPV =
1879 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1880 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001881 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001882 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001883 Callee = DAG.getLoad(
1884 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1885 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1886 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001887 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001888 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001889 } else {
1890 unsigned OpFlags = 0;
1891 // On ELF targets for PIC code, direct calls should go through the PLT
1892 if (Subtarget->isTargetELF() &&
1893 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1894 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001895 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001896 }
Evan Cheng10043e22007-01-19 07:51:42 +00001897 }
1898
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001899 // FIXME: handle tail calls differently.
1900 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001901 if (Subtarget->isThumb()) {
1902 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001903 CallOpc = ARMISD::CALL_NOLINK;
1904 else
1905 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1906 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001907 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001908 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001909 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001910 // Emit regular call when code size is the priority
1911 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001912 // "mov lr, pc; b _foo" to avoid confusing the RSP
1913 CallOpc = ARMISD::CALL_NOLINK;
1914 else
1915 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001916 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001917
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001918 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001919 Ops.push_back(Chain);
1920 Ops.push_back(Callee);
1921
1922 // Add argument registers to the end of the list so that they are known live
1923 // into the call.
1924 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1925 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1926 RegsToPass[i].second.getValueType()));
1927
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001928 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001929 if (!isTailCall) {
1930 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001931 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001932 if (isThisReturn) {
1933 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001934 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001935 if (!Mask) {
1936 // Set isThisReturn to false if the calling convention is not one that
1937 // allows 'returned' to be modeled in this way, so LowerCallResult does
1938 // not try to pass 'this' straight through
1939 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001940 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001941 }
1942 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001943 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001944
Matthias Braunc22630e2013-10-04 16:52:54 +00001945 assert(Mask && "Missing call preserved mask for calling convention");
1946 Ops.push_back(DAG.getRegisterMask(Mask));
1947 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001948
Gabor Greiff304a7a2008-08-28 21:40:38 +00001949 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001950 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001951
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001952 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001953 if (isTailCall) {
1954 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001955 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001956 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001957
Duncan Sands739a0542008-07-02 17:40:58 +00001958 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001959 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001960 InFlag = Chain.getValue(1);
1961
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001962 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1963 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001964 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001965 InFlag = Chain.getValue(1);
1966
Bob Wilsona4c22902009-04-17 19:07:39 +00001967 // Handle result values, copying them out of physregs into vregs that we
1968 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001969 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001970 InVals, isThisReturn,
1971 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001972}
1973
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001974/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001975/// on the stack. Remember the next parameter register to allocate,
1976/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001977/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001978void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1979 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001980 assert((State->getCallOrPrologue() == Prologue ||
1981 State->getCallOrPrologue() == Call) &&
1982 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001983
Tim Northover8cda34f2015-03-11 18:54:22 +00001984 // Byval (as with any stack) slots are always at least 4 byte aligned.
1985 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001986
Tim Northover8cda34f2015-03-11 18:54:22 +00001987 unsigned Reg = State->AllocateReg(GPRArgRegs);
1988 if (!Reg)
1989 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001990
Tim Northover8cda34f2015-03-11 18:54:22 +00001991 unsigned AlignInRegs = Align / 4;
1992 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1993 for (unsigned i = 0; i < Waste; ++i)
1994 Reg = State->AllocateReg(GPRArgRegs);
1995
1996 if (!Reg)
1997 return;
1998
1999 unsigned Excess = 4 * (ARM::R4 - Reg);
2000
2001 // Special case when NSAA != SP and parameter size greater than size of
2002 // all remained GPR regs. In that case we can't split parameter, we must
2003 // send it to stack. We also must set NCRN to R4, so waste all
2004 // remained registers.
2005 const unsigned NSAAOffset = State->getNextStackOffset();
2006 if (NSAAOffset != 0 && Size > Excess) {
2007 while (State->AllocateReg(GPRArgRegs))
2008 ;
2009 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002010 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002011
2012 // First register for byval parameter is the first register that wasn't
2013 // allocated before this method call, so it would be "reg".
2014 // If parameter is small enough to be saved in range [reg, r4), then
2015 // the end (first after last) register would be reg + param-size-in-regs,
2016 // else parameter would be splitted between registers and stack,
2017 // end register would be r4 in this case.
2018 unsigned ByValRegBegin = Reg;
2019 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2020 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2021 // Note, first register is allocated in the beginning of function already,
2022 // allocate remained amount of registers we need.
2023 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2024 State->AllocateReg(GPRArgRegs);
2025 // A byval parameter that is split between registers and memory needs its
2026 // size truncated here.
2027 // In the case where the entire structure fits in registers, we set the
2028 // size in memory to zero.
2029 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002030}
2031
Dale Johannesend679ff72010-06-03 21:09:53 +00002032/// MatchingStackOffset - Return true if the given stack call argument is
2033/// already available in the same position (relatively) of the caller's
2034/// incoming argument stack.
2035static
2036bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2037 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002038 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002039 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2040 int FI = INT_MAX;
2041 if (Arg.getOpcode() == ISD::CopyFromReg) {
2042 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002043 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002044 return false;
2045 MachineInstr *Def = MRI->getVRegDef(VR);
2046 if (!Def)
2047 return false;
2048 if (!Flags.isByVal()) {
2049 if (!TII->isLoadFromStackSlot(Def, FI))
2050 return false;
2051 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002052 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002053 }
2054 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2055 if (Flags.isByVal())
2056 // ByVal argument is passed in as a pointer but it's now being
2057 // dereferenced. e.g.
2058 // define @foo(%struct.X* %A) {
2059 // tail call @bar(%struct.X* byval %A)
2060 // }
2061 return false;
2062 SDValue Ptr = Ld->getBasePtr();
2063 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2064 if (!FINode)
2065 return false;
2066 FI = FINode->getIndex();
2067 } else
2068 return false;
2069
2070 assert(FI != INT_MAX);
2071 if (!MFI->isFixedObjectIndex(FI))
2072 return false;
2073 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2074}
2075
2076/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2077/// for tail call optimization. Targets which want to do tail call
2078/// optimization should implement this function.
2079bool
2080ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2081 CallingConv::ID CalleeCC,
2082 bool isVarArg,
2083 bool isCalleeStructRet,
2084 bool isCallerStructRet,
2085 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002086 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002087 const SmallVectorImpl<ISD::InputArg> &Ins,
2088 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002089 const Function *CallerF = DAG.getMachineFunction().getFunction();
2090 CallingConv::ID CallerCC = CallerF->getCallingConv();
2091 bool CCMatch = CallerCC == CalleeCC;
2092
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002093 assert(Subtarget->supportsTailCall());
2094
Dale Johannesend679ff72010-06-03 21:09:53 +00002095 // Look for obvious safe cases to perform tail call optimization that do not
2096 // require ABI changes. This is what gcc calls sibcall.
2097
Jim Grosbache3864cc2010-06-16 23:45:49 +00002098 // Do not sibcall optimize vararg calls unless the call site is not passing
2099 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002100 if (isVarArg && !Outs.empty())
2101 return false;
2102
Tim Northoverd8407452013-10-01 14:33:28 +00002103 // Exception-handling functions need a special set of instructions to indicate
2104 // a return to the hardware. Tail-calling another function would probably
2105 // break this.
2106 if (CallerF->hasFnAttribute("interrupt"))
2107 return false;
2108
Dale Johannesend679ff72010-06-03 21:09:53 +00002109 // Also avoid sibcall optimization if either caller or callee uses struct
2110 // return semantics.
2111 if (isCalleeStructRet || isCallerStructRet)
2112 return false;
2113
Oliver Stannard12993dd2014-08-18 12:42:15 +00002114 // Externally-defined functions with weak linkage should not be
2115 // tail-called on ARM when the OS does not support dynamic
2116 // pre-emption of symbols, as the AAELF spec requires normal calls
2117 // to undefined weak functions to be replaced with a NOP or jump to the
2118 // next instruction. The behaviour of branch instructions in this
2119 // situation (as used for tail calls) is implementation-defined, so we
2120 // cannot rely on the linker replacing the tail call with a return.
2121 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2122 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002123 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002124 if (GV->hasExternalWeakLinkage() &&
2125 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002126 return false;
2127 }
2128
Dale Johannesend679ff72010-06-03 21:09:53 +00002129 // If the calling conventions do not match, then we'd better make sure the
2130 // results are returned in the same way as what the caller expects.
2131 if (!CCMatch) {
2132 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002133 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2134 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002135 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2136
2137 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002138 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2139 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002140 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2141
2142 if (RVLocs1.size() != RVLocs2.size())
2143 return false;
2144 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2145 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2146 return false;
2147 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2148 return false;
2149 if (RVLocs1[i].isRegLoc()) {
2150 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2151 return false;
2152 } else {
2153 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2154 return false;
2155 }
2156 }
2157 }
2158
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.
2162 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2163 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002164 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002165 return false;
2166
Dale Johannesend679ff72010-06-03 21:09:53 +00002167 // If the callee takes no arguments then go on to check the results of the
2168 // call.
2169 if (!Outs.empty()) {
2170 // Check if stack adjustment is needed. For now, do not do this if any
2171 // argument is passed on the stack.
2172 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002173 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2174 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002175 CCInfo.AnalyzeCallOperands(Outs,
2176 CCAssignFnForNode(CalleeCC, false, isVarArg));
2177 if (CCInfo.getNextStackOffset()) {
2178 MachineFunction &MF = DAG.getMachineFunction();
2179
2180 // Check if the arguments are already laid out in the right way as
2181 // the caller's fixed stack objects.
2182 MachineFrameInfo *MFI = MF.getFrameInfo();
2183 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002184 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002185 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2186 i != e;
2187 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002188 CCValAssign &VA = ArgLocs[i];
2189 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002190 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002191 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002192 if (VA.getLocInfo() == CCValAssign::Indirect)
2193 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002194 if (VA.needsCustom()) {
2195 // f64 and vector types are split into multiple registers or
2196 // register/stack-slot combinations. The types will not match
2197 // the registers; give up on memory f64 refs until we figure
2198 // out what to do about this.
2199 if (!VA.isRegLoc())
2200 return false;
2201 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002202 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002203 if (RegVT == MVT::v2f64) {
2204 if (!ArgLocs[++i].isRegLoc())
2205 return false;
2206 if (!ArgLocs[++i].isRegLoc())
2207 return false;
2208 }
2209 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002210 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2211 MFI, MRI, TII))
2212 return false;
2213 }
2214 }
2215 }
2216 }
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 }
2355
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002356 // Update chain and glue.
2357 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002358 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002359 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002360
Tim Northoverd8407452013-10-01 14:33:28 +00002361 // CPUs which aren't M-class use a special sequence to return from
2362 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2363 // though we use "subs pc, lr, #N").
2364 //
2365 // M-class CPUs actually use a normal return sequence with a special
2366 // (hardware-provided) value in LR, so the normal code path works.
2367 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2368 !Subtarget->isMClass()) {
2369 if (Subtarget->isThumb1Only())
2370 report_fatal_error("interrupt attribute is not supported in Thumb1");
2371 return LowerInterruptReturn(RetOps, dl, DAG);
2372 }
2373
Craig Topper48d114b2014-04-26 18:35:24 +00002374 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002375}
2376
Evan Chengf8bad082012-04-10 01:51:00 +00002377bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002378 if (N->getNumValues() != 1)
2379 return false;
2380 if (!N->hasNUsesOfValue(1, 0))
2381 return false;
2382
Evan Chengf8bad082012-04-10 01:51:00 +00002383 SDValue TCChain = Chain;
2384 SDNode *Copy = *N->use_begin();
2385 if (Copy->getOpcode() == ISD::CopyToReg) {
2386 // If the copy has a glue operand, we conservatively assume it isn't safe to
2387 // perform a tail call.
2388 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2389 return false;
2390 TCChain = Copy->getOperand(0);
2391 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2392 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002393 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002394 SmallPtrSet<SDNode*, 2> Copies;
2395 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002396 UI != UE; ++UI) {
2397 if (UI->getOpcode() != ISD::CopyToReg)
2398 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002399 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002400 }
Evan Chengf8bad082012-04-10 01:51:00 +00002401 if (Copies.size() > 2)
2402 return false;
2403
2404 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2405 UI != UE; ++UI) {
2406 SDValue UseChain = UI->getOperand(0);
2407 if (Copies.count(UseChain.getNode()))
2408 // Second CopyToReg
2409 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002410 else {
2411 // We are at the top of this chain.
2412 // If the copy has a glue operand, we conservatively assume it
2413 // isn't safe to perform a tail call.
2414 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2415 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002416 // First CopyToReg
2417 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002418 }
Evan Chengf8bad082012-04-10 01:51:00 +00002419 }
2420 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002421 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002422 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002423 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002424 Copy = *Copy->use_begin();
2425 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002426 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002427 // If the copy has a glue operand, we conservatively assume it isn't safe to
2428 // perform a tail call.
2429 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2430 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002431 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002432 } else {
2433 return false;
2434 }
2435
Evan Cheng419ea282010-12-01 22:59:46 +00002436 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002437 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2438 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002439 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2440 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002441 return false;
2442 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002443 }
2444
Evan Chengf8bad082012-04-10 01:51:00 +00002445 if (!HasRet)
2446 return false;
2447
2448 Chain = TCChain;
2449 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002450}
2451
Evan Cheng0663f232011-03-21 01:19:09 +00002452bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002453 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002454 return false;
2455
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002456 auto Attr =
2457 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2458 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002459 return false;
2460
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002461 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002462}
2463
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002464// Trying to write a 64 bit value so need to split into two 32 bit values first,
2465// and pass the lower and high parts through.
2466static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2467 SDLoc DL(Op);
2468 SDValue WriteValue = Op->getOperand(2);
2469
2470 // This function is only supposed to be called for i64 type argument.
2471 assert(WriteValue.getValueType() == MVT::i64
2472 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2473
2474 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2475 DAG.getConstant(0, DL, MVT::i32));
2476 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2477 DAG.getConstant(1, DL, MVT::i32));
2478 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2479 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2480}
2481
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002482// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2483// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2484// one of the above mentioned nodes. It has to be wrapped because otherwise
2485// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2486// be used to form addressing mode. These wrapped nodes will be selected
2487// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002488static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002489 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002490 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002491 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002492 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002493 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002494 if (CP->isMachineConstantPoolEntry())
2495 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2496 CP->getAlignment());
2497 else
2498 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2499 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002500 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002501}
2502
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002503unsigned ARMTargetLowering::getJumpTableEncoding() const {
2504 return MachineJumpTableInfo::EK_Inline;
2505}
2506
Dan Gohman21cea8a2010-04-17 15:26:15 +00002507SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2508 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002509 MachineFunction &MF = DAG.getMachineFunction();
2510 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2511 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002512 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002513 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002514 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002515 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2516 SDValue CPAddr;
2517 if (RelocM == Reloc::Static) {
2518 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2519 } else {
2520 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002521 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002522 ARMConstantPoolValue *CPV =
2523 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2524 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002525 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2526 }
2527 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002528 SDValue Result =
2529 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2530 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2531 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002532 if (RelocM == Reloc::Static)
2533 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002534 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002535 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002536}
2537
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002538// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002539SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002540ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002541 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002542 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002543 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002544 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002545 MachineFunction &MF = DAG.getMachineFunction();
2546 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002547 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002548 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002549 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2550 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002551 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002552 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002553 Argument =
2554 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2555 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2556 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002557 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002558
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002559 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002560 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002561
2562 // call __tls_get_addr.
2563 ArgListTy Args;
2564 ArgListEntry Entry;
2565 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002566 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002567 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002568
Dale Johannesen555a3752009-01-30 23:10:59 +00002569 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002570 TargetLowering::CallLoweringInfo CLI(DAG);
2571 CLI.setDebugLoc(dl).setChain(Chain)
2572 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002573 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2574 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002575
Justin Holewinskiaa583972012-05-25 16:35:28 +00002576 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002577 return CallResult.first;
2578}
2579
2580// Lower ISD::GlobalTLSAddress using the "initial exec" or
2581// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002582SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002583ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002584 SelectionDAG &DAG,
2585 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002586 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002587 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002588 SDValue Offset;
2589 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002590 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002591 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002592 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002593
Hans Wennborgaea41202012-05-04 09:40:39 +00002594 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002595 MachineFunction &MF = DAG.getMachineFunction();
2596 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002597 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002598 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002599 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2600 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002601 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2602 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2603 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002604 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002605 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002606 Offset = DAG.getLoad(
2607 PtrVT, dl, Chain, Offset,
2608 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2609 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002610 Chain = Offset.getValue(1);
2611
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002612 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002613 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002614
Alex Lorenze40c8a22015-08-11 23:09:45 +00002615 Offset = DAG.getLoad(
2616 PtrVT, dl, Chain, Offset,
2617 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2618 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002619 } else {
2620 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002621 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002622 ARMConstantPoolValue *CPV =
2623 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002624 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002625 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002626 Offset = DAG.getLoad(
2627 PtrVT, dl, Chain, Offset,
2628 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2629 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002630 }
2631
2632 // The address of the thread local variable is the add of the thread
2633 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002634 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002635}
2636
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002637SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002638ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002639 // TODO: implement the "local dynamic" model
2640 assert(Subtarget->isTargetELF() &&
2641 "TLS not implemented for non-ELF targets");
2642 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002643 if (DAG.getTarget().Options.EmulatedTLS)
2644 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002645
2646 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2647
2648 switch (model) {
2649 case TLSModel::GeneralDynamic:
2650 case TLSModel::LocalDynamic:
2651 return LowerToTLSGeneralDynamicModel(GA, DAG);
2652 case TLSModel::InitialExec:
2653 case TLSModel::LocalExec:
2654 return LowerToTLSExecModels(GA, DAG, model);
2655 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002656 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002657}
2658
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002659SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002660 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002661 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002662 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002663 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002664 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002665 bool UseGOT_PREL =
Peter Collingbourne99fac802015-10-26 20:46:44 +00002666 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage());
Peter Collingbourne97aae402015-10-26 18:23:16 +00002667
2668 MachineFunction &MF = DAG.getMachineFunction();
2669 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2670 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2671 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2672 SDLoc dl(Op);
2673 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2674 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2675 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2676 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2677 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002678 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002679 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002680 SDValue Result = DAG.getLoad(
2681 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2682 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2683 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002684 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002685 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2686 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2687 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002688 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002689 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002690 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002691 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002692 }
2693
2694 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002695 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002696 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002697 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002698 // FIXME: Once remat is capable of dealing with instructions with register
2699 // operands, expand this into two nodes.
2700 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2701 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002702 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002703 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2704 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002705 return DAG.getLoad(
2706 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2707 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2708 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002709 }
2710}
2711
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002712SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002713 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002714 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002715 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002716 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002717 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002718
Eric Christopherc1058df2014-07-04 01:55:26 +00002719 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002720 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002721
Tim Northover72360d22013-12-02 10:35:41 +00002722 // FIXME: Once remat is capable of dealing with instructions with register
2723 // operands, expand this into multiple nodes
2724 unsigned Wrapper =
2725 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002726
Tim Northover72360d22013-12-02 10:35:41 +00002727 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2728 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002729
Evan Cheng1b389522009-09-03 07:04:02 +00002730 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002731 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002732 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2733 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002734 return Result;
2735}
2736
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002737SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2738 SelectionDAG &DAG) const {
2739 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002740 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2741 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002742
2743 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002744 const ARMII::TOF TargetFlags =
2745 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002746 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002747 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002748 SDLoc DL(Op);
2749
2750 ++NumMovwMovt;
2751
2752 // FIXME: Once remat is capable of dealing with instructions with register
2753 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002754 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2755 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002756 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002757 if (GV->hasDLLImportStorageClass())
2758 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002759 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2760 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002761 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002762}
2763
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002764SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002765ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002766 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002767 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002768 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2769 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002770 Op.getOperand(1), Val);
2771}
2772
2773SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002774ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002775 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002776 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002777 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002778}
2779
Matthias Braun3cd00c12015-07-16 22:34:16 +00002780SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2781 SelectionDAG &DAG) const {
2782 SDLoc dl(Op);
2783 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2784 Op.getOperand(0));
2785}
2786
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002787SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002788ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002789 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002790 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002791 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002792 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002793 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002794 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002795 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002796 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002797 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002798 }
Bob Wilson17f88782009-08-04 00:25:01 +00002799 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002800 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002801 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2802 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002803 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002804 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002805 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002806 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002807 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002808 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2809 SDValue CPAddr;
2810 unsigned PCAdj = (RelocM != Reloc::PIC_)
2811 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002812 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002813 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2814 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002815 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002816 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002817 SDValue Result = DAG.getLoad(
2818 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2819 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2820 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002821
2822 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002823 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002824 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2825 }
2826 return Result;
2827 }
Evan Cheng18381b42011-03-29 23:06:19 +00002828 case Intrinsic::arm_neon_vmulls:
2829 case Intrinsic::arm_neon_vmullu: {
2830 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2831 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002832 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002833 Op.getOperand(1), Op.getOperand(2));
2834 }
James Molloyee868b22015-08-11 12:06:25 +00002835 case Intrinsic::arm_neon_vminnm:
2836 case Intrinsic::arm_neon_vmaxnm: {
2837 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2838 ? ISD::FMINNUM : ISD::FMAXNUM;
2839 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2840 Op.getOperand(1), Op.getOperand(2));
2841 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002842 case Intrinsic::arm_neon_vminu:
2843 case Intrinsic::arm_neon_vmaxu: {
2844 if (Op.getValueType().isFloatingPoint())
2845 return SDValue();
2846 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2847 ? ISD::UMIN : ISD::UMAX;
2848 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2849 Op.getOperand(1), Op.getOperand(2));
2850 }
James Molloyd616c642015-08-11 12:06:28 +00002851 case Intrinsic::arm_neon_vmins:
2852 case Intrinsic::arm_neon_vmaxs: {
2853 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002854 if (!Op.getValueType().isFloatingPoint()) {
2855 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2856 ? ISD::SMIN : ISD::SMAX;
2857 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2858 Op.getOperand(1), Op.getOperand(2));
2859 }
James Molloyd616c642015-08-11 12:06:28 +00002860 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2861 ? ISD::FMINNAN : ISD::FMAXNAN;
2862 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2863 Op.getOperand(1), Op.getOperand(2));
2864 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002865 }
2866}
2867
Eli Friedman30a49e92011-08-03 21:06:02 +00002868static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2869 const ARMSubtarget *Subtarget) {
2870 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002871 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002872 if (!Subtarget->hasDataBarrier()) {
2873 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2874 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2875 // here.
2876 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002877 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002878 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002879 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002880 }
2881
Tim Northover36b24172013-07-03 09:20:36 +00002882 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2883 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002884 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002885 if (Subtarget->isMClass()) {
2886 // Only a full system barrier exists in the M-class architectures.
2887 Domain = ARM_MB::SY;
2888 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002889 // Swift happens to implement ISHST barriers in a way that's compatible with
2890 // Release semantics but weaker than ISH so we'd be fools not to use
2891 // it. Beware: other processors probably don't!
2892 Domain = ARM_MB::ISHST;
2893 }
2894
Joey Gouly926d3f52013-09-05 15:35:24 +00002895 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002896 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2897 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002898}
2899
Evan Cheng8740ee32010-11-03 06:34:55 +00002900static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2901 const ARMSubtarget *Subtarget) {
2902 // ARM pre v5TE and Thumb1 does not have preload instructions.
2903 if (!(Subtarget->isThumb2() ||
2904 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2905 // Just preserve the chain.
2906 return Op.getOperand(0);
2907
Andrew Trickef9de2a2013-05-25 02:42:55 +00002908 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002909 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2910 if (!isRead &&
2911 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2912 // ARMv7 with MP extension has PLDW.
2913 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002914
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002915 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2916 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002917 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002918 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002919 isData = ~isData & 1;
2920 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002921
2922 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002923 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2924 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002925}
2926
Dan Gohman31ae5862010-04-17 14:41:14 +00002927static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2928 MachineFunction &MF = DAG.getMachineFunction();
2929 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2930
Evan Cheng10043e22007-01-19 07:51:42 +00002931 // vastart just stores the address of the VarArgsFrameIndex slot into the
2932 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002933 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002934 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002935 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002936 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002937 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2938 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002939}
2940
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002941SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002942ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2943 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002944 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002945 MachineFunction &MF = DAG.getMachineFunction();
2946 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2947
Craig Topper760b1342012-02-22 05:59:10 +00002948 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002949 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002950 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002951 else
Craig Topperc7242e02012-04-20 07:30:17 +00002952 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002953
2954 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002955 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002956 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002957
2958 SDValue ArgValue2;
2959 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002960 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002961 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002962
2963 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002964 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00002965 ArgValue2 = DAG.getLoad(
2966 MVT::i32, dl, Root, FIN,
2967 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
2968 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002969 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002970 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002971 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002972 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002973 if (!Subtarget->isLittle())
2974 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002975 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002976}
2977
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002978// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002979// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002980// byval). Either way, we allocate stack slots adjacent to the data
2981// provided by our caller, and store the unallocated registers there.
2982// If this is a variadic function, the va_list pointer will begin with
2983// these values; otherwise, this reassembles a (byval) structure that
2984// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002985// Return: The frame index registers were stored into.
2986int
2987ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002988 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002989 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002990 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002991 int ArgOffset,
2992 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002993 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002994 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002995 // Setup first unallocated register as first byval register;
2996 // eat all remained registers
2997 // (these two actions are performed by HandleByVal method).
2998 // Then, here, we initialize stack frame with
2999 // "store-reg" instructions.
3000 // Case #2. Var-args function, that doesn't contain byval parameters.
3001 // The same: eat all remained unallocated registers,
3002 // initialize stack frame.
3003
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003004 MachineFunction &MF = DAG.getMachineFunction();
3005 MachineFrameInfo *MFI = MF.getFrameInfo();
3006 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003007 unsigned RBegin, REnd;
3008 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3009 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003010 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003011 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003012 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003013 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003014 }
3015
Tim Northover8cda34f2015-03-11 18:54:22 +00003016 if (REnd != RBegin)
3017 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003018
Mehdi Amini44ede332015-07-09 02:09:04 +00003019 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003020 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003021 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003022
Tim Northover8cda34f2015-03-11 18:54:22 +00003023 SmallVector<SDValue, 4> MemOps;
3024 const TargetRegisterClass *RC =
3025 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003026
Tim Northover8cda34f2015-03-11 18:54:22 +00003027 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3028 unsigned VReg = MF.addLiveIn(Reg, RC);
3029 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3030 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003031 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003032 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3033 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003034 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003035 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003036
3037 if (!MemOps.empty())
3038 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3039 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003040}
3041
3042// Setup stack frame, the va_list pointer will start from.
3043void
3044ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003045 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003046 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003047 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003048 bool ForceMutable) const {
3049 MachineFunction &MF = DAG.getMachineFunction();
3050 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3051
3052 // Try to store any remaining integer argument regs
3053 // to their spots on the stack so that they may be loaded by deferencing
3054 // the result of va_next.
3055 // If there is no regs to be stored, just point address after last
3056 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003057 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3058 CCInfo.getInRegsParamsCount(),
3059 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003060 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003061}
3062
Bob Wilson2e076c42009-06-22 23:27:02 +00003063SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003064ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003065 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003066 const SmallVectorImpl<ISD::InputArg>
3067 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003068 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003069 SmallVectorImpl<SDValue> &InVals)
3070 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003071 MachineFunction &MF = DAG.getMachineFunction();
3072 MachineFrameInfo *MFI = MF.getFrameInfo();
3073
Bob Wilsona4c22902009-04-17 19:07:39 +00003074 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3075
3076 // Assign locations to all of the incoming arguments.
3077 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003078 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3079 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003080 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003081 CCAssignFnForNode(CallConv, /* Return*/ false,
3082 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003083
Bob Wilsona4c22902009-04-17 19:07:39 +00003084 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003085 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003086 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3087 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003088
3089 // Initially ArgRegsSaveSize is zero.
3090 // Then we increase this value each time we meet byval parameter.
3091 // We also increase this value in case of varargs function.
3092 AFI->setArgRegsSaveSize(0);
3093
Oliver Stannardd55e1152014-03-05 15:25:27 +00003094 // Calculate the amount of stack space that we need to allocate to store
3095 // byval and variadic arguments that are passed in registers.
3096 // We need to know this before we allocate the first byval or variadic
3097 // argument, as they will be allocated a stack slot below the CFA (Canonical
3098 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003099 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003100 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003101 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3102 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003103
Tim Northover8cda34f2015-03-11 18:54:22 +00003104 CCValAssign &VA = ArgLocs[i];
3105 unsigned Index = VA.getValNo();
3106 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3107 if (!Flags.isByVal())
3108 continue;
3109
3110 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3111 unsigned RBegin, REnd;
3112 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3113 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3114
3115 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003116 }
3117 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003118
3119 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003120 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003121 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3122 if (RegIdx != array_lengthof(GPRArgRegs))
3123 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003124 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003125
3126 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3127 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003128 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003129
Bob Wilsona4c22902009-04-17 19:07:39 +00003130 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3131 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003132 if (Ins[VA.getValNo()].isOrigArg()) {
3133 std::advance(CurOrigArg,
3134 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3135 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3136 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003137 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003138 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003139 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003140
Bob Wilsona4c22902009-04-17 19:07:39 +00003141 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003142 // f64 and vector types are split up into multiple registers or
3143 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003144 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003145 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003146 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003147 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003148 SDValue ArgValue2;
3149 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003150 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003151 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003152 ArgValue2 = DAG.getLoad(
3153 MVT::f64, dl, Chain, FIN,
3154 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3155 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003156 } else {
3157 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3158 Chain, DAG, dl);
3159 }
Owen Anderson9f944592009-08-11 20:47:22 +00003160 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3161 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003162 ArgValue, ArgValue1,
3163 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003164 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003165 ArgValue, ArgValue2,
3166 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003167 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003168 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003169
Bob Wilson2e076c42009-06-22 23:27:02 +00003170 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003171 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003172
Owen Anderson9f944592009-08-11 20:47:22 +00003173 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003174 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003175 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003176 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003177 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003178 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003179 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003180 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3181 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003182 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003183 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003184
3185 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003186 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003187 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003188 }
3189
3190 // If this is an 8 or 16-bit value, it is really passed promoted
3191 // to 32 bits. Insert an assert[sz]ext to capture this, then
3192 // truncate to the right size.
3193 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003194 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003195 case CCValAssign::Full: break;
3196 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003197 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003198 break;
3199 case CCValAssign::SExt:
3200 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3201 DAG.getValueType(VA.getValVT()));
3202 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3203 break;
3204 case CCValAssign::ZExt:
3205 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3206 DAG.getValueType(VA.getValVT()));
3207 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3208 break;
3209 }
3210
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003211 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003212
3213 } else { // VA.isRegLoc()
3214
3215 // sanity check
3216 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003217 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003218
Andrew Trick05938a52015-02-16 18:10:47 +00003219 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003220
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003221 // Some Ins[] entries become multiple ArgLoc[] entries.
3222 // Process them only once.
3223 if (index != lastInsIndex)
3224 {
3225 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003226 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003227 // This can be changed with more analysis.
3228 // In case of tail call optimization mark all arguments mutable.
3229 // Since they could be overwritten by lowering of arguments in case of
3230 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003231 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003232 assert(Ins[index].isOrigArg() &&
3233 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003234 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003235
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003236 int FrameIndex = StoreByValRegs(
3237 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3238 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003239 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003240 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003241 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003242 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003243 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003244 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003245
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003246 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003247 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003248 InVals.push_back(DAG.getLoad(
3249 VA.getValVT(), dl, Chain, FIN,
3250 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3251 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003252 }
3253 lastInsIndex = index;
3254 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003255 }
3256 }
3257
3258 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003259 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003260 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003261 CCInfo.getNextStackOffset(),
3262 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003263
Oliver Stannardb14c6252014-04-02 16:10:33 +00003264 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3265
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003266 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003267}
3268
3269/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003270static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003271 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003272 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003273 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003274 // Maybe this has already been legalized into the constant pool?
3275 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003276 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003277 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003278 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003279 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003280 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003281 } else if (Op->getOpcode() == ISD::BITCAST &&
3282 Op->getValueType(0) == MVT::f64) {
3283 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3284 // created by LowerConstantFP().
3285 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003286 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3287 isNullConstant(BitcastOp->getOperand(0)))
3288 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003289 }
3290 return false;
3291}
3292
Evan Cheng10043e22007-01-19 07:51:42 +00003293/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3294/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003295SDValue
3296ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003297 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003298 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003299 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003300 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003301 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003302 // Constant does not fit, try adjusting it by one?
3303 switch (CC) {
3304 default: break;
3305 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003306 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003307 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003308 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003309 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003310 }
3311 break;
3312 case ISD::SETULT:
3313 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003314 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003315 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003316 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003317 }
3318 break;
3319 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003320 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003321 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003322 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003323 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003324 }
3325 break;
3326 case ISD::SETULE:
3327 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003328 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003329 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003330 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003331 }
3332 break;
3333 }
3334 }
3335 }
3336
3337 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003338 ARMISD::NodeType CompareType;
3339 switch (CondCode) {
3340 default:
3341 CompareType = ARMISD::CMP;
3342 break;
3343 case ARMCC::EQ:
3344 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003345 // Uses only Z Flag
3346 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003347 break;
3348 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003349 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003350 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003351}
3352
3353/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003354SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003355ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003356 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003357 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003358 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003359 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003360 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003361 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003362 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3363 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003364}
3365
Bob Wilson45acbd02011-03-08 01:17:20 +00003366/// duplicateCmp - Glue values can have only one use, so this function
3367/// duplicates a comparison node.
3368SDValue
3369ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3370 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003371 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003372 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3373 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3374
3375 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3376 Cmp = Cmp.getOperand(0);
3377 Opc = Cmp.getOpcode();
3378 if (Opc == ARMISD::CMPFP)
3379 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3380 else {
3381 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3382 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3383 }
3384 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3385}
3386
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003387std::pair<SDValue, SDValue>
3388ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3389 SDValue &ARMcc) const {
3390 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3391
3392 SDValue Value, OverflowCmp;
3393 SDValue LHS = Op.getOperand(0);
3394 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003395 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003396
3397 // FIXME: We are currently always generating CMPs because we don't support
3398 // generating CMN through the backend. This is not as good as the natural
3399 // CMP case because it causes a register dependency and cannot be folded
3400 // later.
3401
3402 switch (Op.getOpcode()) {
3403 default:
3404 llvm_unreachable("Unknown overflow instruction!");
3405 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003406 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3407 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3408 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003409 break;
3410 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3412 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3413 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003414 break;
3415 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003416 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3417 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3418 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003419 break;
3420 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003421 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3422 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3423 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003424 break;
3425 } // switch (...)
3426
3427 return std::make_pair(Value, OverflowCmp);
3428}
3429
3430
3431SDValue
3432ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3433 // Let legalize expand this if it isn't a legal type yet.
3434 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3435 return SDValue();
3436
3437 SDValue Value, OverflowCmp;
3438 SDValue ARMcc;
3439 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3440 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003441 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003442 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003443 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3444 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003445 EVT VT = Op.getValueType();
3446
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003447 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003448 ARMcc, CCR, OverflowCmp);
3449
3450 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003451 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003452}
3453
3454
Bill Wendling6a981312010-08-11 08:43:16 +00003455SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3456 SDValue Cond = Op.getOperand(0);
3457 SDValue SelectTrue = Op.getOperand(1);
3458 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003459 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003460 unsigned Opc = Cond.getOpcode();
3461
3462 if (Cond.getResNo() == 1 &&
3463 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3464 Opc == ISD::USUBO)) {
3465 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3466 return SDValue();
3467
3468 SDValue Value, OverflowCmp;
3469 SDValue ARMcc;
3470 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3471 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3472 EVT VT = Op.getValueType();
3473
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003474 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003475 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003476 }
Bill Wendling6a981312010-08-11 08:43:16 +00003477
3478 // Convert:
3479 //
3480 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3481 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3482 //
3483 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3484 const ConstantSDNode *CMOVTrue =
3485 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3486 const ConstantSDNode *CMOVFalse =
3487 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3488
3489 if (CMOVTrue && CMOVFalse) {
3490 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3491 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3492
3493 SDValue True;
3494 SDValue False;
3495 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3496 True = SelectTrue;
3497 False = SelectFalse;
3498 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3499 True = SelectFalse;
3500 False = SelectTrue;
3501 }
3502
3503 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003504 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003505 SDValue ARMcc = Cond.getOperand(2);
3506 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003507 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003508 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003509 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003510 }
3511 }
3512 }
3513
Dan Gohmand4a77c42012-02-24 00:09:36 +00003514 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3515 // undefined bits before doing a full-word comparison with zero.
3516 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003517 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003518
Bill Wendling6a981312010-08-11 08:43:16 +00003519 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003520 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003521 SelectTrue, SelectFalse, ISD::SETNE);
3522}
3523
Joey Gouly881eab52013-08-22 15:29:11 +00003524static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3525 bool &swpCmpOps, bool &swpVselOps) {
3526 // Start by selecting the GE condition code for opcodes that return true for
3527 // 'equality'
3528 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3529 CC == ISD::SETULE)
3530 CondCode = ARMCC::GE;
3531
3532 // and GT for opcodes that return false for 'equality'.
3533 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3534 CC == ISD::SETULT)
3535 CondCode = ARMCC::GT;
3536
3537 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3538 // to swap the compare operands.
3539 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3540 CC == ISD::SETULT)
3541 swpCmpOps = true;
3542
3543 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3544 // If we have an unordered opcode, we need to swap the operands to the VSEL
3545 // instruction (effectively negating the condition).
3546 //
3547 // This also has the effect of swapping which one of 'less' or 'greater'
3548 // returns true, so we also swap the compare operands. It also switches
3549 // whether we return true for 'equality', so we compensate by picking the
3550 // opposite condition code to our original choice.
3551 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3552 CC == ISD::SETUGT) {
3553 swpCmpOps = !swpCmpOps;
3554 swpVselOps = !swpVselOps;
3555 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3556 }
3557
3558 // 'ordered' is 'anything but unordered', so use the VS condition code and
3559 // swap the VSEL operands.
3560 if (CC == ISD::SETO) {
3561 CondCode = ARMCC::VS;
3562 swpVselOps = true;
3563 }
3564
3565 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3566 // code and swap the VSEL operands.
3567 if (CC == ISD::SETUNE) {
3568 CondCode = ARMCC::EQ;
3569 swpVselOps = true;
3570 }
3571}
3572
Oliver Stannard51b1d462014-08-21 12:50:31 +00003573SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3574 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3575 SDValue Cmp, SelectionDAG &DAG) const {
3576 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3577 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3578 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3579 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3580 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3581
3582 SDValue TrueLow = TrueVal.getValue(0);
3583 SDValue TrueHigh = TrueVal.getValue(1);
3584 SDValue FalseLow = FalseVal.getValue(0);
3585 SDValue FalseHigh = FalseVal.getValue(1);
3586
3587 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3588 ARMcc, CCR, Cmp);
3589 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3590 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3591
3592 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3593 } else {
3594 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3595 Cmp);
3596 }
3597}
3598
Dan Gohman21cea8a2010-04-17 15:26:15 +00003599SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003600 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003601 SDValue LHS = Op.getOperand(0);
3602 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003603 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003604 SDValue TrueVal = Op.getOperand(2);
3605 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003606 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003607
Oliver Stannard51b1d462014-08-21 12:50:31 +00003608 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3609 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3610 dl);
3611
3612 // If softenSetCCOperands only returned one value, we should compare it to
3613 // zero.
3614 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003615 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003616 CC = ISD::SETNE;
3617 }
3618 }
3619
Owen Anderson9f944592009-08-11 20:47:22 +00003620 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003621 // Try to generate VSEL on ARMv8.
3622 // The VSEL instruction can't use all the usual ARM condition
3623 // codes: it only has two bits to select the condition code, so it's
3624 // constrained to use only GE, GT, VS and EQ.
3625 //
3626 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3627 // swap the operands of the previous compare instruction (effectively
3628 // inverting the compare condition, swapping 'less' and 'greater') and
3629 // sometimes need to swap the operands to the VSEL (which inverts the
3630 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003631 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3632 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003633 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3634 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3635 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003636 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003637 std::swap(TrueVal, FalseVal);
3638 }
3639 }
3640
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003641 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003642 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003643 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003644 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003645 }
3646
3647 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003648 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003649
Scott Douglass7ad77922015-04-08 17:18:28 +00003650 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003651 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3652 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003653 bool swpCmpOps = false;
3654 bool swpVselOps = false;
3655 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3656
3657 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3658 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3659 if (swpCmpOps)
3660 std::swap(LHS, RHS);
3661 if (swpVselOps)
3662 std::swap(TrueVal, FalseVal);
3663 }
3664 }
3665
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003666 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003667 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003668 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003669 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003670 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003671 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003672 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003673 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003674 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003675 }
3676 return Result;
3677}
3678
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003679/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3680/// to morph to an integer compare sequence.
3681static bool canChangeToInt(SDValue Op, bool &SeenZero,
3682 const ARMSubtarget *Subtarget) {
3683 SDNode *N = Op.getNode();
3684 if (!N->hasOneUse())
3685 // Otherwise it requires moving the value from fp to integer registers.
3686 return false;
3687 if (!N->getNumValues())
3688 return false;
3689 EVT VT = Op.getValueType();
3690 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3691 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3692 // vmrs are very slow, e.g. cortex-a8.
3693 return false;
3694
3695 if (isFloatingPointZero(Op)) {
3696 SeenZero = true;
3697 return true;
3698 }
3699 return ISD::isNormalLoad(N);
3700}
3701
3702static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3703 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003704 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003705
3706 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003707 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003708 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003709 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003710 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003711
3712 llvm_unreachable("Unknown VFP cmp argument!");
3713}
3714
3715static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3716 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003717 SDLoc dl(Op);
3718
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003719 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003720 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3721 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003722 return;
3723 }
3724
3725 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3726 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003727 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003728 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003729 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003730 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003731 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003732
3733 EVT PtrType = Ptr.getValueType();
3734 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003735 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3736 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3737 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003738 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003739 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003740 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003741 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003742 return;
3743 }
3744
3745 llvm_unreachable("Unknown VFP cmp argument!");
3746}
3747
3748/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3749/// f32 and even f64 comparisons to integer ones.
3750SDValue
3751ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3752 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003753 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003754 SDValue LHS = Op.getOperand(2);
3755 SDValue RHS = Op.getOperand(3);
3756 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003757 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003758
Evan Chengd12af5d2012-03-01 23:27:13 +00003759 bool LHSSeenZero = false;
3760 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3761 bool RHSSeenZero = false;
3762 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3763 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003764 // If unsafe fp math optimization is enabled and there are no other uses of
3765 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003766 // to an integer comparison.
3767 if (CC == ISD::SETOEQ)
3768 CC = ISD::SETEQ;
3769 else if (CC == ISD::SETUNE)
3770 CC = ISD::SETNE;
3771
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003772 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003773 SDValue ARMcc;
3774 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003775 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3776 bitcastf32Toi32(LHS, DAG), Mask);
3777 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3778 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003779 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3780 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3781 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3782 Chain, Dest, ARMcc, CCR, Cmp);
3783 }
3784
3785 SDValue LHS1, LHS2;
3786 SDValue RHS1, RHS2;
3787 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3788 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003789 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3790 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003791 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003792 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003793 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003794 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003795 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003796 }
3797
3798 return SDValue();
3799}
3800
3801SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3802 SDValue Chain = Op.getOperand(0);
3803 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3804 SDValue LHS = Op.getOperand(2);
3805 SDValue RHS = Op.getOperand(3);
3806 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003807 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003808
Oliver Stannard51b1d462014-08-21 12:50:31 +00003809 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3810 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3811 dl);
3812
3813 // If softenSetCCOperands only returned one value, we should compare it to
3814 // zero.
3815 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003816 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003817 CC = ISD::SETNE;
3818 }
3819 }
3820
Owen Anderson9f944592009-08-11 20:47:22 +00003821 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003822 SDValue ARMcc;
3823 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003824 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003825 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003826 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003827 }
3828
Owen Anderson9f944592009-08-11 20:47:22 +00003829 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003830
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003831 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003832 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3833 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3834 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3835 if (Result.getNode())
3836 return Result;
3837 }
3838
Evan Cheng10043e22007-01-19 07:51:42 +00003839 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003840 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003841
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003842 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003843 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003844 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003845 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003846 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003847 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003848 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003849 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003850 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003851 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003852 }
3853 return Res;
3854}
3855
Dan Gohman21cea8a2010-04-17 15:26:15 +00003856SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003857 SDValue Chain = Op.getOperand(0);
3858 SDValue Table = Op.getOperand(1);
3859 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003860 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003861
Mehdi Amini44ede332015-07-09 02:09:04 +00003862 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003863 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003864 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003865 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003866 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003867 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003868 if (Subtarget->isThumb2()) {
3869 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3870 // which does another jump to the destination. This also makes it easier
3871 // to translate it to TBB / TBH later.
3872 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003873 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003874 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003875 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003876 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003877 Addr =
3878 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
3879 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3880 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003881 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003882 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003883 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003884 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003885 Addr =
3886 DAG.getLoad(PTy, dl, Chain, Addr,
3887 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3888 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003889 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003890 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003891 }
Evan Cheng10043e22007-01-19 07:51:42 +00003892}
3893
Eli Friedman2d4055b2011-11-09 23:36:02 +00003894static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003895 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003896 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003897
James Molloy547d4c02012-02-20 09:24:05 +00003898 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3899 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3900 return Op;
3901 return DAG.UnrollVectorOp(Op.getNode());
3902 }
3903
3904 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3905 "Invalid type for custom lowering!");
3906 if (VT != MVT::v4i16)
3907 return DAG.UnrollVectorOp(Op.getNode());
3908
3909 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3910 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003911}
3912
Oliver Stannard51b1d462014-08-21 12:50:31 +00003913SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003914 EVT VT = Op.getValueType();
3915 if (VT.isVector())
3916 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003917 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3918 RTLIB::Libcall LC;
3919 if (Op.getOpcode() == ISD::FP_TO_SINT)
3920 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3921 Op.getValueType());
3922 else
3923 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3924 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00003925 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00003926 /*isSigned*/ false, SDLoc(Op)).first;
3927 }
3928
James Molloyfa041152015-03-23 16:15:16 +00003929 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003930}
3931
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003932static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3933 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003934 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003935
Eli Friedman2d4055b2011-11-09 23:36:02 +00003936 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3937 if (VT.getVectorElementType() == MVT::f32)
3938 return Op;
3939 return DAG.UnrollVectorOp(Op.getNode());
3940 }
3941
Duncan Sandsa41634e2011-08-12 14:54:45 +00003942 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3943 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003944 if (VT != MVT::v4f32)
3945 return DAG.UnrollVectorOp(Op.getNode());
3946
3947 unsigned CastOpc;
3948 unsigned Opc;
3949 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003950 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003951 case ISD::SINT_TO_FP:
3952 CastOpc = ISD::SIGN_EXTEND;
3953 Opc = ISD::SINT_TO_FP;
3954 break;
3955 case ISD::UINT_TO_FP:
3956 CastOpc = ISD::ZERO_EXTEND;
3957 Opc = ISD::UINT_TO_FP;
3958 break;
3959 }
3960
3961 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3962 return DAG.getNode(Opc, dl, VT, Op);
3963}
3964
Oliver Stannard51b1d462014-08-21 12:50:31 +00003965SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003966 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003967 if (VT.isVector())
3968 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003969 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3970 RTLIB::Libcall LC;
3971 if (Op.getOpcode() == ISD::SINT_TO_FP)
3972 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3973 Op.getValueType());
3974 else
3975 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3976 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00003977 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00003978 /*isSigned*/ false, SDLoc(Op)).first;
3979 }
3980
James Molloyfa041152015-03-23 16:15:16 +00003981 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003982}
3983
Evan Cheng25f93642010-07-08 02:08:50 +00003984SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003985 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003986 SDValue Tmp0 = Op.getOperand(0);
3987 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003988 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003989 EVT VT = Op.getValueType();
3990 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003991 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3992 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3993 bool UseNEON = !InGPR && Subtarget->hasNEON();
3994
3995 if (UseNEON) {
3996 // Use VBSL to copy the sign bit.
3997 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3998 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003999 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004000 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4001 if (VT == MVT::f64)
4002 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4003 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004004 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004005 else /*if (VT == MVT::f32)*/
4006 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4007 if (SrcVT == MVT::f32) {
4008 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4009 if (VT == MVT::f64)
4010 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4011 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004012 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004013 } else if (VT == MVT::f32)
4014 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4015 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004016 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004017 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4018 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4019
4020 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004021 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004022 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4023 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4024 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004025
Evan Chengd6b641e2011-02-23 02:24:55 +00004026 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4027 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4028 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004029 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004030 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4031 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004032 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004033 } else {
4034 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4035 }
4036
4037 return Res;
4038 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004039
4040 // Bitcast operand 1 to i32.
4041 if (SrcVT == MVT::f64)
4042 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004043 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004044 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4045
Evan Chengd6b641e2011-02-23 02:24:55 +00004046 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004047 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4048 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004049 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4050 if (VT == MVT::f32) {
4051 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4052 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4053 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4054 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004055 }
4056
Evan Chengd6b641e2011-02-23 02:24:55 +00004057 // f64: Or the high part with signbit and then combine two parts.
4058 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004059 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004060 SDValue Lo = Tmp0.getValue(0);
4061 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4062 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4063 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004064}
4065
Evan Cheng168ced92010-05-22 01:47:14 +00004066SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4067 MachineFunction &MF = DAG.getMachineFunction();
4068 MachineFrameInfo *MFI = MF.getFrameInfo();
4069 MFI->setReturnAddressIsTaken(true);
4070
Bill Wendling908bf812014-01-06 00:43:20 +00004071 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004072 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004073
Evan Cheng168ced92010-05-22 01:47:14 +00004074 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004075 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004076 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4077 if (Depth) {
4078 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004079 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004080 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4081 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004082 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004083 }
4084
4085 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004086 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004087 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4088}
4089
Dan Gohman21cea8a2010-04-17 15:26:15 +00004090SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004091 const ARMBaseRegisterInfo &ARI =
4092 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4093 MachineFunction &MF = DAG.getMachineFunction();
4094 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004095 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004096
Owen Anderson53aa7a92009-08-10 22:56:29 +00004097 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004098 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004099 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004100 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004101 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4102 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004103 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4104 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004105 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004106 return FrameAddr;
4107}
4108
Renato Golinc7aea402014-05-06 16:51:25 +00004109// FIXME? Maybe this could be a TableGen attribute on some registers and
4110// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004111unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4112 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004113 unsigned Reg = StringSwitch<unsigned>(RegName)
4114 .Case("sp", ARM::SP)
4115 .Default(0);
4116 if (Reg)
4117 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004118 report_fatal_error(Twine("Invalid register name \""
4119 + StringRef(RegName) + "\"."));
4120}
4121
4122// Result is 64 bit value so split into two 32 bit values and return as a
4123// pair of values.
4124static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4125 SelectionDAG &DAG) {
4126 SDLoc DL(N);
4127
4128 // This function is only supposed to be called for i64 type destination.
4129 assert(N->getValueType(0) == MVT::i64
4130 && "ExpandREAD_REGISTER called for non-i64 type result.");
4131
4132 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4133 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4134 N->getOperand(0),
4135 N->getOperand(1));
4136
4137 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4138 Read.getValue(1)));
4139 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004140}
4141
Wesley Peck527da1b2010-11-23 03:31:01 +00004142/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004143/// expand a bit convert where either the source or destination type is i64 to
4144/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4145/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4146/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004147static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004148 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004149 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004150 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004151
Bob Wilson59b70ea2010-04-17 05:30:19 +00004152 // This function is only supposed to be called for i64 types, either as the
4153 // source or destination of the bit convert.
4154 EVT SrcVT = Op.getValueType();
4155 EVT DstVT = N->getValueType(0);
4156 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004157 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004158
Bob Wilson59b70ea2010-04-17 05:30:19 +00004159 // Turn i64->f64 into VMOVDRR.
4160 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004161 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004162 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004163 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004164 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004165 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004166 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004167 }
Bob Wilson7117a912009-03-20 22:42:55 +00004168
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004169 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004170 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004171 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004172 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004173 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004174 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4175 DAG.getVTList(MVT::i32, MVT::i32),
4176 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4177 else
4178 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4179 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004180 // Merge the pieces into a single i64 value.
4181 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4182 }
Bob Wilson7117a912009-03-20 22:42:55 +00004183
Bob Wilson59b70ea2010-04-17 05:30:19 +00004184 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004185}
4186
Bob Wilson2e076c42009-06-22 23:27:02 +00004187/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004188/// Zero vectors are used to represent vector negation and in those cases
4189/// will be implemented with the NEON VNEG instruction. However, VNEG does
4190/// not support i64 elements, so sometimes the zero vectors will need to be
4191/// explicitly constructed. Regardless, use a canonical VMOV to create the
4192/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004193static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004194 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004195 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004196 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004197 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4198 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004199 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004200}
4201
Jim Grosbach624fcb22009-10-31 21:00:56 +00004202/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4203/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004204SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4205 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004206 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4207 EVT VT = Op.getValueType();
4208 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004209 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004210 SDValue ShOpLo = Op.getOperand(0);
4211 SDValue ShOpHi = Op.getOperand(1);
4212 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004213 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004214 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004215
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004216 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4217
Jim Grosbach624fcb22009-10-31 21:00:56 +00004218 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004219 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004220 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4221 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004222 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004223 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4224 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004225 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004226
4227 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004228 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4229 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004230 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004231 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004232 CCR, Cmp);
4233
4234 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004235 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004236}
4237
Jim Grosbach5d994042009-10-31 19:38:01 +00004238/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4239/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004240SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4241 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004242 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4243 EVT VT = Op.getValueType();
4244 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004245 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004246 SDValue ShOpLo = Op.getOperand(0);
4247 SDValue ShOpHi = Op.getOperand(1);
4248 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004249 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004250
4251 assert(Op.getOpcode() == ISD::SHL_PARTS);
4252 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004253 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004254 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4255 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004256 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004257 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4258 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4259
4260 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4261 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004262 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4263 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004264 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004265 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004266 CCR, Cmp);
4267
4268 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004269 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004270}
4271
Jim Grosbach535d3b42010-09-08 03:54:02 +00004272SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004273 SelectionDAG &DAG) const {
4274 // The rounding mode is in bits 23:22 of the FPSCR.
4275 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4276 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4277 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004278 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004279 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004280 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004281 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004282 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004283 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004284 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004285 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004286 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004287 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004288}
4289
Jim Grosbach8546ec92010-01-18 19:58:49 +00004290static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4291 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004292 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004293 EVT VT = N->getValueType(0);
4294 if (VT.isVector()) {
4295 assert(ST->hasNEON());
4296
4297 // Compute the least significant set bit: LSB = X & -X
4298 SDValue X = N->getOperand(0);
4299 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4300 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4301
4302 EVT ElemTy = VT.getVectorElementType();
4303
4304 if (ElemTy == MVT::i8) {
4305 // Compute with: cttz(x) = ctpop(lsb - 1)
4306 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4307 DAG.getTargetConstant(1, dl, ElemTy));
4308 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4309 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4310 }
4311
4312 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4313 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4314 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4315 unsigned NumBits = ElemTy.getSizeInBits();
4316 SDValue WidthMinus1 =
4317 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4318 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4319 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4320 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4321 }
4322
4323 // Compute with: cttz(x) = ctpop(lsb - 1)
4324
4325 // Since we can only compute the number of bits in a byte with vcnt.8, we
4326 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4327 // and i64.
4328
4329 // Compute LSB - 1.
4330 SDValue Bits;
4331 if (ElemTy == MVT::i64) {
4332 // Load constant 0xffff'ffff'ffff'ffff to register.
4333 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4334 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4335 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4336 } else {
4337 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4338 DAG.getTargetConstant(1, dl, ElemTy));
4339 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4340 }
4341
4342 // Count #bits with vcnt.8.
4343 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4344 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4345 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4346
4347 // Gather the #bits with vpaddl (pairwise add.)
4348 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4349 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4350 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4351 Cnt8);
4352 if (ElemTy == MVT::i16)
4353 return Cnt16;
4354
4355 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4356 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4357 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4358 Cnt16);
4359 if (ElemTy == MVT::i32)
4360 return Cnt32;
4361
4362 assert(ElemTy == MVT::i64);
4363 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4364 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4365 Cnt32);
4366 return Cnt64;
4367 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004368
4369 if (!ST->hasV6T2Ops())
4370 return SDValue();
4371
James Molloyb5640982015-11-13 16:05:22 +00004372 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004373 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4374}
4375
Evan Chengb4eae132012-12-04 22:41:50 +00004376/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4377/// for each 16-bit element from operand, repeated. The basic idea is to
4378/// leverage vcnt to get the 8-bit counts, gather and add the results.
4379///
4380/// Trace for v4i16:
4381/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4382/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4383/// 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 +00004384/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004385/// [b0 b1 b2 b3 b4 b5 b6 b7]
4386/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4387/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4388/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4389static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4390 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004391 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004392
4393 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4394 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4395 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4396 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4397 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4398 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4399}
4400
4401/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4402/// bit-count for each 16-bit element from the operand. We need slightly
4403/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4404/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004405///
Evan Chengb4eae132012-12-04 22:41:50 +00004406/// Trace for v4i16:
4407/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4408/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4409/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4410/// v4i16:Extracted = [k0 k1 k2 k3 ]
4411static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4412 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004413 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004414
4415 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4416 if (VT.is64BitVector()) {
4417 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4418 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004419 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004420 } else {
4421 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004422 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004423 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4424 }
4425}
4426
4427/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4428/// bit-count for each 32-bit element from the operand. The idea here is
4429/// to split the vector into 16-bit elements, leverage the 16-bit count
4430/// routine, and then combine the results.
4431///
4432/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4433/// input = [v0 v1 ] (vi: 32-bit elements)
4434/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4435/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004436/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004437/// [k0 k1 k2 k3 ]
4438/// N1 =+[k1 k0 k3 k2 ]
4439/// [k0 k2 k1 k3 ]
4440/// N2 =+[k1 k3 k0 k2 ]
4441/// [k0 k2 k1 k3 ]
4442/// Extended =+[k1 k3 k0 k2 ]
4443/// [k0 k2 ]
4444/// Extracted=+[k1 k3 ]
4445///
4446static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4447 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004448 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004449
4450 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4451
4452 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4453 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4454 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4455 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4456 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4457
4458 if (VT.is64BitVector()) {
4459 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4460 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004461 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004462 } else {
4463 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004464 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004465 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4466 }
4467}
4468
4469static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4470 const ARMSubtarget *ST) {
4471 EVT VT = N->getValueType(0);
4472
4473 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004474 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4475 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004476 "Unexpected type for custom ctpop lowering");
4477
4478 if (VT.getVectorElementType() == MVT::i32)
4479 return lowerCTPOP32BitElements(N, DAG);
4480 else
4481 return lowerCTPOP16BitElements(N, DAG);
4482}
4483
Bob Wilson2e076c42009-06-22 23:27:02 +00004484static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4485 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004486 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004487 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004488
Bob Wilson7d471332010-11-18 21:16:28 +00004489 if (!VT.isVector())
4490 return SDValue();
4491
Bob Wilson2e076c42009-06-22 23:27:02 +00004492 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004493 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004494
Bob Wilson7d471332010-11-18 21:16:28 +00004495 // Left shifts translate directly to the vshiftu intrinsic.
4496 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004497 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004498 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4499 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004500 N->getOperand(0), N->getOperand(1));
4501
4502 assert((N->getOpcode() == ISD::SRA ||
4503 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4504
4505 // NEON uses the same intrinsics for both left and right shifts. For
4506 // right shifts, the shift amounts are negative, so negate the vector of
4507 // shift amounts.
4508 EVT ShiftVT = N->getOperand(1).getValueType();
4509 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4510 getZeroVector(ShiftVT, DAG, dl),
4511 N->getOperand(1));
4512 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4513 Intrinsic::arm_neon_vshifts :
4514 Intrinsic::arm_neon_vshiftu);
4515 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004516 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004517 N->getOperand(0), NegatedCount);
4518}
4519
4520static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4521 const ARMSubtarget *ST) {
4522 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004523 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004524
Eli Friedman682d8c12009-08-22 03:13:10 +00004525 // We can get here for a node like i32 = ISD::SHL i32, i64
4526 if (VT != MVT::i64)
4527 return SDValue();
4528
4529 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004530 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004531
Chris Lattnerf81d5882007-11-24 07:07:01 +00004532 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004533 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004534 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004535
Chris Lattnerf81d5882007-11-24 07:07:01 +00004536 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004537 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004538
Chris Lattnerf81d5882007-11-24 07:07:01 +00004539 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004540 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004541 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004542 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004543 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004544
Chris Lattnerf81d5882007-11-24 07:07:01 +00004545 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4546 // captures the result into a carry flag.
4547 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004548 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004549
Chris Lattnerf81d5882007-11-24 07:07:01 +00004550 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004551 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004552
Chris Lattnerf81d5882007-11-24 07:07:01 +00004553 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004554 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004555}
4556
Bob Wilson2e076c42009-06-22 23:27:02 +00004557static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4558 SDValue TmpOp0, TmpOp1;
4559 bool Invert = false;
4560 bool Swap = false;
4561 unsigned Opc = 0;
4562
4563 SDValue Op0 = Op.getOperand(0);
4564 SDValue Op1 = Op.getOperand(1);
4565 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004566 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004567 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004568 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004569 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004570
James Molloybf170092015-08-20 16:33:44 +00004571 if (CmpVT.getVectorElementType() == MVT::i64)
4572 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4573 // but it's possible that our operands are 64-bit but our result is 32-bit.
4574 // Bail in this case.
4575 return SDValue();
4576
Oliver Stannard51b1d462014-08-21 12:50:31 +00004577 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004578 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004579 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004580 case ISD::SETUNE:
4581 case ISD::SETNE: Invert = true; // Fallthrough
4582 case ISD::SETOEQ:
4583 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4584 case ISD::SETOLT:
4585 case ISD::SETLT: Swap = true; // Fallthrough
4586 case ISD::SETOGT:
4587 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4588 case ISD::SETOLE:
4589 case ISD::SETLE: Swap = true; // Fallthrough
4590 case ISD::SETOGE:
4591 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4592 case ISD::SETUGE: Swap = true; // Fallthrough
4593 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4594 case ISD::SETUGT: Swap = true; // Fallthrough
4595 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4596 case ISD::SETUEQ: Invert = true; // Fallthrough
4597 case ISD::SETONE:
4598 // Expand this to (OLT | OGT).
4599 TmpOp0 = Op0;
4600 TmpOp1 = Op1;
4601 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004602 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4603 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004604 break;
4605 case ISD::SETUO: Invert = true; // Fallthrough
4606 case ISD::SETO:
4607 // Expand this to (OLT | OGE).
4608 TmpOp0 = Op0;
4609 TmpOp1 = Op1;
4610 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004611 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4612 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004613 break;
4614 }
4615 } else {
4616 // Integer comparisons.
4617 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004618 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004619 case ISD::SETNE: Invert = true;
4620 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4621 case ISD::SETLT: Swap = true;
4622 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4623 case ISD::SETLE: Swap = true;
4624 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4625 case ISD::SETULT: Swap = true;
4626 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4627 case ISD::SETULE: Swap = true;
4628 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4629 }
4630
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004631 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004632 if (Opc == ARMISD::VCEQ) {
4633
4634 SDValue AndOp;
4635 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4636 AndOp = Op0;
4637 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4638 AndOp = Op1;
4639
4640 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004641 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004642 AndOp = AndOp.getOperand(0);
4643
4644 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4645 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004646 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4647 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004648 Invert = !Invert;
4649 }
4650 }
4651 }
4652
4653 if (Swap)
4654 std::swap(Op0, Op1);
4655
Owen Andersonc7baee32010-11-08 23:21:22 +00004656 // If one of the operands is a constant vector zero, attempt to fold the
4657 // comparison to a specialized compare-against-zero form.
4658 SDValue SingleOp;
4659 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4660 SingleOp = Op0;
4661 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4662 if (Opc == ARMISD::VCGE)
4663 Opc = ARMISD::VCLEZ;
4664 else if (Opc == ARMISD::VCGT)
4665 Opc = ARMISD::VCLTZ;
4666 SingleOp = Op1;
4667 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004668
Owen Andersonc7baee32010-11-08 23:21:22 +00004669 SDValue Result;
4670 if (SingleOp.getNode()) {
4671 switch (Opc) {
4672 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004673 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004674 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004675 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004676 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004677 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004678 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004679 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004680 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004681 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004682 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004683 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004684 }
4685 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004686 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004687 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004688
Tim Northover45aa89c2015-02-08 00:50:47 +00004689 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4690
Bob Wilson2e076c42009-06-22 23:27:02 +00004691 if (Invert)
4692 Result = DAG.getNOT(dl, Result, VT);
4693
4694 return Result;
4695}
4696
Bob Wilson5b2b5042010-06-14 22:19:57 +00004697/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4698/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004699/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004700static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4701 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004702 SDLoc dl, EVT &VT, bool is128Bits,
4703 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004704 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004705
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004706 // SplatBitSize is set to the smallest size that splats the vector, so a
4707 // zero vector will always have SplatBitSize == 8. However, NEON modified
4708 // immediate instructions others than VMOV do not support the 8-bit encoding
4709 // of a zero vector, and the default encoding of zero is supposed to be the
4710 // 32-bit version.
4711 if (SplatBits == 0)
4712 SplatBitSize = 32;
4713
Bob Wilson2e076c42009-06-22 23:27:02 +00004714 switch (SplatBitSize) {
4715 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004716 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004717 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004718 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004719 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004720 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004721 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004722 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004723 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004724
4725 case 16:
4726 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004727 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004728 if ((SplatBits & ~0xff) == 0) {
4729 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004730 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004731 Imm = SplatBits;
4732 break;
4733 }
4734 if ((SplatBits & ~0xff00) == 0) {
4735 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004736 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004737 Imm = SplatBits >> 8;
4738 break;
4739 }
4740 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004741
4742 case 32:
4743 // NEON's 32-bit VMOV supports splat values where:
4744 // * only one byte is nonzero, or
4745 // * the least significant byte is 0xff and the second byte is nonzero, or
4746 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004747 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004748 if ((SplatBits & ~0xff) == 0) {
4749 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004750 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004751 Imm = SplatBits;
4752 break;
4753 }
4754 if ((SplatBits & ~0xff00) == 0) {
4755 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004756 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004757 Imm = SplatBits >> 8;
4758 break;
4759 }
4760 if ((SplatBits & ~0xff0000) == 0) {
4761 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004762 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004763 Imm = SplatBits >> 16;
4764 break;
4765 }
4766 if ((SplatBits & ~0xff000000) == 0) {
4767 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004768 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004769 Imm = SplatBits >> 24;
4770 break;
4771 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004772
Owen Andersona4076922010-11-05 21:57:54 +00004773 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4774 if (type == OtherModImm) return SDValue();
4775
Bob Wilson2e076c42009-06-22 23:27:02 +00004776 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004777 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4778 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004779 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004780 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004781 break;
4782 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004783
4784 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004785 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4786 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004787 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004788 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004789 break;
4790 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004791
4792 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4793 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4794 // VMOV.I32. A (very) minor optimization would be to replicate the value
4795 // and fall through here to test for a valid 64-bit splat. But, then the
4796 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004797 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004798
4799 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004800 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004801 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004802 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004803 uint64_t BitMask = 0xff;
4804 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004805 unsigned ImmMask = 1;
4806 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004807 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004808 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004809 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004810 Imm |= ImmMask;
4811 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004812 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004813 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004814 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004815 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004816 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004817
Mehdi Aminiffc14022015-07-08 01:00:38 +00004818 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004819 // swap higher and lower 32 bit word
4820 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4821
Bob Wilson6eae5202010-06-11 21:34:50 +00004822 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004823 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004824 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004825 break;
4826 }
4827
Bob Wilson6eae5202010-06-11 21:34:50 +00004828 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004829 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004830 }
4831
Bob Wilsona3f19012010-07-13 21:16:48 +00004832 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004833 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004834}
4835
Lang Hames591cdaf2012-03-29 21:56:11 +00004836SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4837 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004838 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004839 return SDValue();
4840
Tim Northoverf79c3a52013-08-20 08:57:11 +00004841 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004842 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004843
Oliver Stannard51b1d462014-08-21 12:50:31 +00004844 // Use the default (constant pool) lowering for double constants when we have
4845 // an SP-only FPU
4846 if (IsDouble && Subtarget->isFPOnlySP())
4847 return SDValue();
4848
Lang Hames591cdaf2012-03-29 21:56:11 +00004849 // Try splatting with a VMOV.f32...
4850 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004851 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4852
Lang Hames591cdaf2012-03-29 21:56:11 +00004853 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004854 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4855 // We have code in place to select a valid ConstantFP already, no need to
4856 // do any mangling.
4857 return Op;
4858 }
4859
4860 // It's a float and we are trying to use NEON operations where
4861 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004862 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004863 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004864 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4865 NewVal);
4866 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004867 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004868 }
4869
Tim Northoverf79c3a52013-08-20 08:57:11 +00004870 // The rest of our options are NEON only, make sure that's allowed before
4871 // proceeding..
4872 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4873 return SDValue();
4874
Lang Hames591cdaf2012-03-29 21:56:11 +00004875 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004876 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4877
4878 // It wouldn't really be worth bothering for doubles except for one very
4879 // important value, which does happen to match: 0.0. So make sure we don't do
4880 // anything stupid.
4881 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4882 return SDValue();
4883
4884 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004885 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4886 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004887 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004888 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004889 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4890 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004891 if (IsDouble)
4892 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4893
4894 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004895 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4896 VecConstant);
4897 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004898 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004899 }
4900
4901 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004902 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004903 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004904 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004905 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004906 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004907
4908 if (IsDouble)
4909 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4910
4911 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004912 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4913 VecConstant);
4914 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004915 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004916 }
4917
4918 return SDValue();
4919}
4920
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004921// check if an VEXT instruction can handle the shuffle mask when the
4922// vector sources of the shuffle are the same.
4923static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4924 unsigned NumElts = VT.getVectorNumElements();
4925
4926 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4927 if (M[0] < 0)
4928 return false;
4929
4930 Imm = M[0];
4931
4932 // If this is a VEXT shuffle, the immediate value is the index of the first
4933 // element. The other shuffle indices must be the successive elements after
4934 // the first one.
4935 unsigned ExpectedElt = Imm;
4936 for (unsigned i = 1; i < NumElts; ++i) {
4937 // Increment the expected index. If it wraps around, just follow it
4938 // back to index zero and keep going.
4939 ++ExpectedElt;
4940 if (ExpectedElt == NumElts)
4941 ExpectedElt = 0;
4942
4943 if (M[i] < 0) continue; // ignore UNDEF indices
4944 if (ExpectedElt != static_cast<unsigned>(M[i]))
4945 return false;
4946 }
4947
4948 return true;
4949}
4950
Lang Hames591cdaf2012-03-29 21:56:11 +00004951
Benjamin Kramer339ced42012-01-15 13:16:05 +00004952static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004953 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004954 unsigned NumElts = VT.getVectorNumElements();
4955 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004956
4957 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4958 if (M[0] < 0)
4959 return false;
4960
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004961 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004962
4963 // If this is a VEXT shuffle, the immediate value is the index of the first
4964 // element. The other shuffle indices must be the successive elements after
4965 // the first one.
4966 unsigned ExpectedElt = Imm;
4967 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004968 // Increment the expected index. If it wraps around, it may still be
4969 // a VEXT but the source vectors must be swapped.
4970 ExpectedElt += 1;
4971 if (ExpectedElt == NumElts * 2) {
4972 ExpectedElt = 0;
4973 ReverseVEXT = true;
4974 }
4975
Bob Wilson411dfad2010-08-17 05:54:34 +00004976 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004977 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004978 return false;
4979 }
4980
4981 // Adjust the index value if the source operands will be swapped.
4982 if (ReverseVEXT)
4983 Imm -= NumElts;
4984
Bob Wilson32cd8552009-08-19 17:03:43 +00004985 return true;
4986}
4987
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004988/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4989/// instruction with the specified blocksize. (The order of the elements
4990/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004991static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004992 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4993 "Only possible block sizes for VREV are: 16, 32, 64");
4994
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004995 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004996 if (EltSz == 64)
4997 return false;
4998
4999 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005000 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005001 // If the first shuffle index is UNDEF, be optimistic.
5002 if (M[0] < 0)
5003 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005004
5005 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5006 return false;
5007
5008 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005009 if (M[i] < 0) continue; // ignore UNDEF indices
5010 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005011 return false;
5012 }
5013
5014 return true;
5015}
5016
Benjamin Kramer339ced42012-01-15 13:16:05 +00005017static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005018 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5019 // range, then 0 is placed into the resulting vector. So pretty much any mask
5020 // of 8 elements can work here.
5021 return VT == MVT::v8i8 && M.size() == 8;
5022}
5023
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005024// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5025// checking that pairs of elements in the shuffle mask represent the same index
5026// in each vector, incrementing the expected index by 2 at each step.
5027// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5028// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5029// v2={e,f,g,h}
5030// WhichResult gives the offset for each element in the mask based on which
5031// of the two results it belongs to.
5032//
5033// The transpose can be represented either as:
5034// result1 = shufflevector v1, v2, result1_shuffle_mask
5035// result2 = shufflevector v1, v2, result2_shuffle_mask
5036// where v1/v2 and the shuffle masks have the same number of elements
5037// (here WhichResult (see below) indicates which result is being checked)
5038//
5039// or as:
5040// results = shufflevector v1, v2, shuffle_mask
5041// where both results are returned in one vector and the shuffle mask has twice
5042// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5043// want to check the low half and high half of the shuffle mask as if it were
5044// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005045static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005046 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5047 if (EltSz == 64)
5048 return false;
5049
Bob Wilsona7062312009-08-21 20:54:19 +00005050 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005051 if (M.size() != NumElts && M.size() != NumElts*2)
5052 return false;
5053
James Molloy8c995a92015-09-10 08:42:28 +00005054 // If the mask is twice as long as the input vector then we need to check the
5055 // upper and lower parts of the mask with a matching value for WhichResult
5056 // FIXME: A mask with only even values will be rejected in case the first
5057 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5058 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005059 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005060 if (M.size() == NumElts * 2)
5061 WhichResult = i / NumElts;
5062 else
5063 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005064 for (unsigned j = 0; j < NumElts; j += 2) {
5065 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5066 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5067 return false;
5068 }
Bob Wilsona7062312009-08-21 20:54:19 +00005069 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005070
5071 if (M.size() == NumElts*2)
5072 WhichResult = 0;
5073
Bob Wilsona7062312009-08-21 20:54:19 +00005074 return true;
5075}
5076
Bob Wilson0bbd3072009-12-03 06:40:55 +00005077/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5078/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5079/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005080static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005081 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5082 if (EltSz == 64)
5083 return false;
5084
5085 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005086 if (M.size() != NumElts && M.size() != NumElts*2)
5087 return false;
5088
5089 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005090 if (M.size() == NumElts * 2)
5091 WhichResult = i / NumElts;
5092 else
5093 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005094 for (unsigned j = 0; j < NumElts; j += 2) {
5095 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5096 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5097 return false;
5098 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005099 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005100
5101 if (M.size() == NumElts*2)
5102 WhichResult = 0;
5103
Bob Wilson0bbd3072009-12-03 06:40:55 +00005104 return true;
5105}
5106
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005107// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5108// that the mask elements are either all even and in steps of size 2 or all odd
5109// and in steps of size 2.
5110// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5111// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5112// v2={e,f,g,h}
5113// Requires similar checks to that of isVTRNMask with
5114// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005115static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005116 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5117 if (EltSz == 64)
5118 return false;
5119
Bob Wilsona7062312009-08-21 20:54:19 +00005120 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005121 if (M.size() != NumElts && M.size() != NumElts*2)
5122 return false;
5123
5124 for (unsigned i = 0; i < M.size(); i += NumElts) {
5125 WhichResult = M[i] == 0 ? 0 : 1;
5126 for (unsigned j = 0; j < NumElts; ++j) {
5127 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5128 return false;
5129 }
Bob Wilsona7062312009-08-21 20:54:19 +00005130 }
5131
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005132 if (M.size() == NumElts*2)
5133 WhichResult = 0;
5134
Bob Wilsona7062312009-08-21 20:54:19 +00005135 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005136 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005137 return false;
5138
5139 return true;
5140}
5141
Bob Wilson0bbd3072009-12-03 06:40:55 +00005142/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5143/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5144/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005145static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005146 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5147 if (EltSz == 64)
5148 return false;
5149
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005150 unsigned NumElts = VT.getVectorNumElements();
5151 if (M.size() != NumElts && M.size() != NumElts*2)
5152 return false;
5153
5154 unsigned Half = NumElts / 2;
5155 for (unsigned i = 0; i < M.size(); i += NumElts) {
5156 WhichResult = M[i] == 0 ? 0 : 1;
5157 for (unsigned j = 0; j < NumElts; j += Half) {
5158 unsigned Idx = WhichResult;
5159 for (unsigned k = 0; k < Half; ++k) {
5160 int MIdx = M[i + j + k];
5161 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5162 return false;
5163 Idx += 2;
5164 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005165 }
5166 }
5167
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005168 if (M.size() == NumElts*2)
5169 WhichResult = 0;
5170
Bob Wilson0bbd3072009-12-03 06:40:55 +00005171 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5172 if (VT.is64BitVector() && EltSz == 32)
5173 return false;
5174
5175 return true;
5176}
5177
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005178// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5179// that pairs of elements of the shufflemask represent the same index in each
5180// vector incrementing sequentially through the vectors.
5181// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5182// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5183// v2={e,f,g,h}
5184// Requires similar checks to that of isVTRNMask with respect the how results
5185// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005186static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005187 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5188 if (EltSz == 64)
5189 return false;
5190
Bob Wilsona7062312009-08-21 20:54:19 +00005191 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005192 if (M.size() != NumElts && M.size() != NumElts*2)
5193 return false;
5194
5195 for (unsigned i = 0; i < M.size(); i += NumElts) {
5196 WhichResult = M[i] == 0 ? 0 : 1;
5197 unsigned Idx = WhichResult * NumElts / 2;
5198 for (unsigned j = 0; j < NumElts; j += 2) {
5199 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5200 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5201 return false;
5202 Idx += 1;
5203 }
Bob Wilsona7062312009-08-21 20:54:19 +00005204 }
5205
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005206 if (M.size() == NumElts*2)
5207 WhichResult = 0;
5208
Bob Wilsona7062312009-08-21 20:54:19 +00005209 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005210 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005211 return false;
5212
5213 return true;
5214}
5215
Bob Wilson0bbd3072009-12-03 06:40:55 +00005216/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5217/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5218/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005219static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005220 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5221 if (EltSz == 64)
5222 return false;
5223
5224 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005225 if (M.size() != NumElts && M.size() != NumElts*2)
5226 return false;
5227
5228 for (unsigned i = 0; i < M.size(); i += NumElts) {
5229 WhichResult = M[i] == 0 ? 0 : 1;
5230 unsigned Idx = WhichResult * NumElts / 2;
5231 for (unsigned j = 0; j < NumElts; j += 2) {
5232 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5233 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5234 return false;
5235 Idx += 1;
5236 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005237 }
5238
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005239 if (M.size() == NumElts*2)
5240 WhichResult = 0;
5241
Bob Wilson0bbd3072009-12-03 06:40:55 +00005242 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5243 if (VT.is64BitVector() && EltSz == 32)
5244 return false;
5245
5246 return true;
5247}
5248
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005249/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5250/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5251static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5252 unsigned &WhichResult,
5253 bool &isV_UNDEF) {
5254 isV_UNDEF = false;
5255 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5256 return ARMISD::VTRN;
5257 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5258 return ARMISD::VUZP;
5259 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5260 return ARMISD::VZIP;
5261
5262 isV_UNDEF = true;
5263 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5264 return ARMISD::VTRN;
5265 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5266 return ARMISD::VUZP;
5267 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5268 return ARMISD::VZIP;
5269
5270 return 0;
5271}
5272
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005273/// \return true if this is a reverse operation on an vector.
5274static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5275 unsigned NumElts = VT.getVectorNumElements();
5276 // Make sure the mask has the right size.
5277 if (NumElts != M.size())
5278 return false;
5279
5280 // Look for <15, ..., 3, -1, 1, 0>.
5281 for (unsigned i = 0; i != NumElts; ++i)
5282 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5283 return false;
5284
5285 return true;
5286}
5287
Dale Johannesen2bff5052010-07-29 20:10:08 +00005288// If N is an integer constant that can be moved into a register in one
5289// instruction, return an SDValue of such a constant (will become a MOV
5290// instruction). Otherwise return null.
5291static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005292 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005293 uint64_t Val;
5294 if (!isa<ConstantSDNode>(N))
5295 return SDValue();
5296 Val = cast<ConstantSDNode>(N)->getZExtValue();
5297
5298 if (ST->isThumb1Only()) {
5299 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005300 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005301 } else {
5302 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005303 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005304 }
5305 return SDValue();
5306}
5307
Bob Wilson2e076c42009-06-22 23:27:02 +00005308// If this is a case we can't handle, return null and let the default
5309// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005310SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5311 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005312 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005313 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005314 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005315
5316 APInt SplatBits, SplatUndef;
5317 unsigned SplatBitSize;
5318 bool HasAnyUndefs;
5319 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005320 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005321 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005322 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005323 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005324 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005325 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005326 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005327 if (Val.getNode()) {
5328 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005329 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005330 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005331
5332 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005333 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005334 Val = isNEONModifiedImm(NegatedImm,
5335 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005336 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005337 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005338 if (Val.getNode()) {
5339 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005340 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005341 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005342
5343 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005344 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005345 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005346 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005347 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005348 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5349 }
5350 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005351 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005352 }
5353
Bob Wilson91fdf682010-05-22 00:23:12 +00005354 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005355 //
5356 // As an optimisation, even if more than one value is used it may be more
5357 // profitable to splat with one value then change some lanes.
5358 //
5359 // Heuristically we decide to do this if the vector has a "dominant" value,
5360 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005361 unsigned NumElts = VT.getVectorNumElements();
5362 bool isOnlyLowElement = true;
5363 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005364 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005365 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005366
5367 // Map of the number of times a particular SDValue appears in the
5368 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005369 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005370 SDValue Value;
5371 for (unsigned i = 0; i < NumElts; ++i) {
5372 SDValue V = Op.getOperand(i);
5373 if (V.getOpcode() == ISD::UNDEF)
5374 continue;
5375 if (i > 0)
5376 isOnlyLowElement = false;
5377 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5378 isConstant = false;
5379
James Molloy49bdbce2012-09-06 09:55:02 +00005380 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005381 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005382
James Molloy49bdbce2012-09-06 09:55:02 +00005383 // Is this value dominant? (takes up more than half of the lanes)
5384 if (++Count > (NumElts / 2)) {
5385 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005386 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005387 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005388 }
James Molloy49bdbce2012-09-06 09:55:02 +00005389 if (ValueCounts.size() != 1)
5390 usesOnlyOneValue = false;
5391 if (!Value.getNode() && ValueCounts.size() > 0)
5392 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005393
James Molloy49bdbce2012-09-06 09:55:02 +00005394 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005395 return DAG.getUNDEF(VT);
5396
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005397 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5398 // Keep going if we are hitting this case.
5399 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005400 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5401
Dale Johannesen2bff5052010-07-29 20:10:08 +00005402 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5403
Dale Johannesen710a2d92010-10-19 20:00:17 +00005404 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5405 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005406 if (hasDominantValue && EltSize <= 32) {
5407 if (!isConstant) {
5408 SDValue N;
5409
5410 // If we are VDUPing a value that comes directly from a vector, that will
5411 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005412 // just use VDUPLANE. We can only do this if the lane being extracted
5413 // is at a constant index, as the VDUP from lane instructions only have
5414 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005415 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005416 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005417 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005418 // We need to create a new undef vector to use for the VDUPLANE if the
5419 // size of the vector from which we get the value is different than the
5420 // size of the vector that we need to create. We will insert the element
5421 // such that the register coalescer will remove unnecessary copies.
5422 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005423 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5424 VT.getVectorNumElements();
5425 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5426 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005427 Value, DAG.getConstant(index, dl, MVT::i32)),
5428 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005429 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005430 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005431 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005432 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005433 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5434
5435 if (!usesOnlyOneValue) {
5436 // The dominant value was splatted as 'N', but we now have to insert
5437 // all differing elements.
5438 for (unsigned I = 0; I < NumElts; ++I) {
5439 if (Op.getOperand(I) == Value)
5440 continue;
5441 SmallVector<SDValue, 3> Ops;
5442 Ops.push_back(N);
5443 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005444 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005445 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005446 }
5447 }
5448 return N;
5449 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005450 if (VT.getVectorElementType().isFloatingPoint()) {
5451 SmallVector<SDValue, 8> Ops;
5452 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005453 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005454 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005455 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005456 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005457 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5458 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005459 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005460 }
James Molloy49bdbce2012-09-06 09:55:02 +00005461 if (usesOnlyOneValue) {
5462 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5463 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005464 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005465 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005466 }
5467
5468 // If all elements are constants and the case above didn't get hit, fall back
5469 // to the default expansion, which will generate a load from the constant
5470 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005471 if (isConstant)
5472 return SDValue();
5473
Bob Wilson6f2b8962011-01-07 21:37:30 +00005474 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5475 if (NumElts >= 4) {
5476 SDValue shuffle = ReconstructShuffle(Op, DAG);
5477 if (shuffle != SDValue())
5478 return shuffle;
5479 }
5480
Bob Wilson91fdf682010-05-22 00:23:12 +00005481 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005482 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5483 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005484 if (EltSize >= 32) {
5485 // Do the expansion with floating-point types, since that is what the VFP
5486 // registers are defined to use, and since i64 is not legal.
5487 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5488 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005489 SmallVector<SDValue, 8> Ops;
5490 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005491 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005492 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005493 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005494 }
5495
Jim Grosbach24e102a2013-07-08 18:18:52 +00005496 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5497 // know the default expansion would otherwise fall back on something even
5498 // worse. For a vector with one or two non-undef values, that's
5499 // scalar_to_vector for the elements followed by a shuffle (provided the
5500 // shuffle is valid for the target) and materialization element by element
5501 // on the stack followed by a load for everything else.
5502 if (!isConstant && !usesOnlyOneValue) {
5503 SDValue Vec = DAG.getUNDEF(VT);
5504 for (unsigned i = 0 ; i < NumElts; ++i) {
5505 SDValue V = Op.getOperand(i);
5506 if (V.getOpcode() == ISD::UNDEF)
5507 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005508 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005509 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5510 }
5511 return Vec;
5512 }
5513
Bob Wilson2e076c42009-06-22 23:27:02 +00005514 return SDValue();
5515}
5516
Bob Wilson6f2b8962011-01-07 21:37:30 +00005517// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005518// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005519SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5520 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005521 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005522 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005523 EVT VT = Op.getValueType();
5524 unsigned NumElts = VT.getVectorNumElements();
5525
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005526 struct ShuffleSourceInfo {
5527 SDValue Vec;
5528 unsigned MinElt;
5529 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005530
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005531 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5532 // be compatible with the shuffle we intend to construct. As a result
5533 // ShuffleVec will be some sliding window into the original Vec.
5534 SDValue ShuffleVec;
5535
5536 // Code should guarantee that element i in Vec starts at element "WindowBase
5537 // + i * WindowScale in ShuffleVec".
5538 int WindowBase;
5539 int WindowScale;
5540
5541 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5542 ShuffleSourceInfo(SDValue Vec)
5543 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5544 WindowScale(1) {}
5545 };
5546
5547 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5548 // node.
5549 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005550 for (unsigned i = 0; i < NumElts; ++i) {
5551 SDValue V = Op.getOperand(i);
5552 if (V.getOpcode() == ISD::UNDEF)
5553 continue;
5554 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5555 // A shuffle can only come from building a vector from various
5556 // elements of other vectors.
5557 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005558 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5559 // Furthermore, shuffles require a constant mask, whereas extractelts
5560 // accept variable indices.
5561 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005562 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005563
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005564 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005565 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005566 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5567 if (Source == Sources.end())
5568 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005569
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005570 // Update the minimum and maximum lane number seen.
5571 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5572 Source->MinElt = std::min(Source->MinElt, EltNo);
5573 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005574 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005575
Bob Wilson6f2b8962011-01-07 21:37:30 +00005576 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005577 // are involved.
5578 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005579 return SDValue();
5580
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005581 // Find out the smallest element size among result and two sources, and use
5582 // it as element size to build the shuffle_vector.
5583 EVT SmallestEltTy = VT.getVectorElementType();
5584 for (auto &Source : Sources) {
5585 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5586 if (SrcEltTy.bitsLT(SmallestEltTy))
5587 SmallestEltTy = SrcEltTy;
5588 }
5589 unsigned ResMultiplier =
5590 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5591 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5592 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005593
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005594 // If the source vector is too wide or too narrow, we may nevertheless be able
5595 // to construct a compatible shuffle either by concatenating it with UNDEF or
5596 // extracting a suitable range of elements.
5597 for (auto &Src : Sources) {
5598 EVT SrcVT = Src.ShuffleVec.getValueType();
5599
5600 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005601 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005602
5603 // This stage of the search produces a source with the same element type as
5604 // the original, but with a total width matching the BUILD_VECTOR output.
5605 EVT EltVT = SrcVT.getVectorElementType();
5606 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5607 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5608
5609 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5610 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5611 return SDValue();
5612 // We can pad out the smaller vector for free, so if it's part of a
5613 // shuffle...
5614 Src.ShuffleVec =
5615 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5616 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5617 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005618 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005619
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005620 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5621 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005622
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005623 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005624 // Span too large for a VEXT to cope
5625 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005626 }
5627
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005628 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005629 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005630 Src.ShuffleVec =
5631 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5632 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5633 Src.WindowBase = -NumSrcElts;
5634 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005635 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005636 Src.ShuffleVec =
5637 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5638 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005639 } else {
5640 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005641 SDValue VEXTSrc1 =
5642 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5643 DAG.getConstant(0, dl, MVT::i32));
5644 SDValue VEXTSrc2 =
5645 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5646 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005647
5648 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5649 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005650 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005651 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005652 }
5653 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005654
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005655 // Another possible incompatibility occurs from the vector element types. We
5656 // can fix this by bitcasting the source vectors to the same type we intend
5657 // for the shuffle.
5658 for (auto &Src : Sources) {
5659 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5660 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005661 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005662 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5663 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5664 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5665 Src.WindowBase *= Src.WindowScale;
5666 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005667
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005668 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005669 DEBUG(
5670 for (auto Src : Sources)
5671 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5672 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005673
5674 // The stars all align, our next step is to produce the mask for the shuffle.
5675 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5676 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5677 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5678 SDValue Entry = Op.getOperand(i);
5679 if (Entry.getOpcode() == ISD::UNDEF)
5680 continue;
5681
5682 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5683 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5684
5685 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5686 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5687 // segment.
5688 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5689 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5690 VT.getVectorElementType().getSizeInBits());
5691 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5692
5693 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5694 // starting at the appropriate offset.
5695 int *LaneMask = &Mask[i * ResMultiplier];
5696
5697 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5698 ExtractBase += NumElts * (Src - Sources.begin());
5699 for (int j = 0; j < LanesDefined; ++j)
5700 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005701 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005702
Bob Wilson6f2b8962011-01-07 21:37:30 +00005703 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005704 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5705 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005706
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005707 // We can't handle more than two sources. This should have already
5708 // been checked before this point.
5709 assert(Sources.size() <= 2 && "Too many sources!");
5710
5711 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5712 for (unsigned i = 0; i < Sources.size(); ++i)
5713 ShuffleOps[i] = Sources[i].ShuffleVec;
5714
5715 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5716 ShuffleOps[1], &Mask[0]);
5717 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005718}
5719
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005720/// isShuffleMaskLegal - Targets can use this to indicate that they only
5721/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5722/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5723/// are assumed to be legal.
5724bool
5725ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5726 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005727 if (VT.getVectorNumElements() == 4 &&
5728 (VT.is128BitVector() || VT.is64BitVector())) {
5729 unsigned PFIndexes[4];
5730 for (unsigned i = 0; i != 4; ++i) {
5731 if (M[i] < 0)
5732 PFIndexes[i] = 8;
5733 else
5734 PFIndexes[i] = M[i];
5735 }
5736
5737 // Compute the index in the perfect shuffle table.
5738 unsigned PFTableIndex =
5739 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5740 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5741 unsigned Cost = (PFEntry >> 30);
5742
5743 if (Cost <= 4)
5744 return true;
5745 }
5746
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005747 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005748 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005749
Bob Wilson846bd792010-06-07 23:53:38 +00005750 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5751 return (EltSize >= 32 ||
5752 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005753 isVREVMask(M, VT, 64) ||
5754 isVREVMask(M, VT, 32) ||
5755 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005756 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005757 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005758 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005759 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005760}
5761
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005762/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5763/// the specified operations to build the shuffle.
5764static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5765 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005766 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005767 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5768 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5769 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5770
5771 enum {
5772 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5773 OP_VREV,
5774 OP_VDUP0,
5775 OP_VDUP1,
5776 OP_VDUP2,
5777 OP_VDUP3,
5778 OP_VEXT1,
5779 OP_VEXT2,
5780 OP_VEXT3,
5781 OP_VUZPL, // VUZP, left result
5782 OP_VUZPR, // VUZP, right result
5783 OP_VZIPL, // VZIP, left result
5784 OP_VZIPR, // VZIP, right result
5785 OP_VTRNL, // VTRN, left result
5786 OP_VTRNR // VTRN, right result
5787 };
5788
5789 if (OpNum == OP_COPY) {
5790 if (LHSID == (1*9+2)*9+3) return LHS;
5791 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5792 return RHS;
5793 }
5794
5795 SDValue OpLHS, OpRHS;
5796 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5797 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5798 EVT VT = OpLHS.getValueType();
5799
5800 switch (OpNum) {
5801 default: llvm_unreachable("Unknown shuffle opcode!");
5802 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005803 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005804 if (VT.getVectorElementType() == MVT::i32 ||
5805 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005806 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5807 // vrev <4 x i16> -> VREV32
5808 if (VT.getVectorElementType() == MVT::i16)
5809 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5810 // vrev <4 x i8> -> VREV16
5811 assert(VT.getVectorElementType() == MVT::i8);
5812 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005813 case OP_VDUP0:
5814 case OP_VDUP1:
5815 case OP_VDUP2:
5816 case OP_VDUP3:
5817 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005818 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005819 case OP_VEXT1:
5820 case OP_VEXT2:
5821 case OP_VEXT3:
5822 return DAG.getNode(ARMISD::VEXT, dl, VT,
5823 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005824 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005825 case OP_VUZPL:
5826 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005827 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005828 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5829 case OP_VZIPL:
5830 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005831 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005832 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5833 case OP_VTRNL:
5834 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005835 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5836 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005837 }
5838}
5839
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005840static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005841 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005842 SelectionDAG &DAG) {
5843 // Check to see if we can use the VTBL instruction.
5844 SDValue V1 = Op.getOperand(0);
5845 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005846 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005847
5848 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005849 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005850 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005851 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005852
5853 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5854 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005855 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005856
Owen Anderson77aa2662011-04-05 21:48:57 +00005857 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005858 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005859}
5860
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005861static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5862 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005863 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005864 SDValue OpLHS = Op.getOperand(0);
5865 EVT VT = OpLHS.getValueType();
5866
5867 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5868 "Expect an v8i16/v16i8 type");
5869 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5870 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5871 // extract the first 8 bytes into the top double word and the last 8 bytes
5872 // into the bottom double word. The v8i16 case is similar.
5873 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5874 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005875 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005876}
5877
Bob Wilson2e076c42009-06-22 23:27:02 +00005878static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005879 SDValue V1 = Op.getOperand(0);
5880 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005881 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005882 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005883 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005884
Bob Wilsonc6800b52009-08-13 02:13:04 +00005885 // Convert shuffles that are directly supported on NEON to target-specific
5886 // DAG nodes, instead of keeping them as shuffles and matching them again
5887 // during code selection. This is more efficient and avoids the possibility
5888 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005889 // FIXME: floating-point vectors should be canonicalized to integer vectors
5890 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005891 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005892
Bob Wilson846bd792010-06-07 23:53:38 +00005893 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5894 if (EltSize <= 32) {
5895 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5896 int Lane = SVN->getSplatIndex();
5897 // If this is undef splat, generate it via "just" vdup, if possible.
5898 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005899
Dan Gohman198b7ff2011-11-03 21:49:52 +00005900 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005901 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5902 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5903 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005904 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5905 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5906 // reaches it).
5907 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5908 !isa<ConstantSDNode>(V1.getOperand(0))) {
5909 bool IsScalarToVector = true;
5910 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5911 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5912 IsScalarToVector = false;
5913 break;
5914 }
5915 if (IsScalarToVector)
5916 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5917 }
Bob Wilson846bd792010-06-07 23:53:38 +00005918 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005919 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005920 }
Bob Wilson846bd792010-06-07 23:53:38 +00005921
5922 bool ReverseVEXT;
5923 unsigned Imm;
5924 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5925 if (ReverseVEXT)
5926 std::swap(V1, V2);
5927 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005928 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005929 }
5930
5931 if (isVREVMask(ShuffleMask, VT, 64))
5932 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5933 if (isVREVMask(ShuffleMask, VT, 32))
5934 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5935 if (isVREVMask(ShuffleMask, VT, 16))
5936 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5937
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005938 if (V2->getOpcode() == ISD::UNDEF &&
5939 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5940 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005941 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005942 }
5943
Bob Wilson846bd792010-06-07 23:53:38 +00005944 // Check for Neon shuffles that modify both input vectors in place.
5945 // If both results are used, i.e., if there are two shuffles with the same
5946 // source operands and with masks corresponding to both results of one of
5947 // these operations, DAG memoization will ensure that a single node is
5948 // used for both shuffles.
5949 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005950 bool isV_UNDEF;
5951 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5952 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5953 if (isV_UNDEF)
5954 V2 = V1;
5955 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5956 .getValue(WhichResult);
5957 }
Bob Wilson846bd792010-06-07 23:53:38 +00005958
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005959 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5960 // shuffles that produce a result larger than their operands with:
5961 // shuffle(concat(v1, undef), concat(v2, undef))
5962 // ->
5963 // shuffle(concat(v1, v2), undef)
5964 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5965 //
5966 // This is useful in the general case, but there are special cases where
5967 // native shuffles produce larger results: the two-result ops.
5968 //
5969 // Look through the concat when lowering them:
5970 // shuffle(concat(v1, v2), undef)
5971 // ->
5972 // concat(VZIP(v1, v2):0, :1)
5973 //
5974 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5975 V2->getOpcode() == ISD::UNDEF) {
5976 SDValue SubV1 = V1->getOperand(0);
5977 SDValue SubV2 = V1->getOperand(1);
5978 EVT SubVT = SubV1.getValueType();
5979
5980 // We expect these to have been canonicalized to -1.
5981 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5982 return i < (int)VT.getVectorNumElements();
5983 }) && "Unexpected shuffle index into UNDEF operand!");
5984
5985 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5986 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5987 if (isV_UNDEF)
5988 SubV2 = SubV1;
5989 assert((WhichResult == 0) &&
5990 "In-place shuffle of concat can only have one result!");
5991 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
5992 SubV1, SubV2);
5993 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
5994 Res.getValue(1));
5995 }
5996 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00005997 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005998
Bob Wilsona7062312009-08-21 20:54:19 +00005999 // If the shuffle is not directly supported and it has 4 elements, use
6000 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006001 unsigned NumElts = VT.getVectorNumElements();
6002 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006003 unsigned PFIndexes[4];
6004 for (unsigned i = 0; i != 4; ++i) {
6005 if (ShuffleMask[i] < 0)
6006 PFIndexes[i] = 8;
6007 else
6008 PFIndexes[i] = ShuffleMask[i];
6009 }
6010
6011 // Compute the index in the perfect shuffle table.
6012 unsigned PFTableIndex =
6013 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006014 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6015 unsigned Cost = (PFEntry >> 30);
6016
6017 if (Cost <= 4)
6018 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6019 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006020
Bob Wilsond8a9a042010-06-04 00:04:02 +00006021 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006022 if (EltSize >= 32) {
6023 // Do the expansion with floating-point types, since that is what the VFP
6024 // registers are defined to use, and since i64 is not legal.
6025 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6026 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006027 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6028 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006029 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006030 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006031 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006032 Ops.push_back(DAG.getUNDEF(EltVT));
6033 else
6034 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6035 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6036 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006037 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006038 }
Craig Topper48d114b2014-04-26 18:35:24 +00006039 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006040 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006041 }
6042
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006043 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6044 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6045
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006046 if (VT == MVT::v8i8) {
6047 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
6048 if (NewOp.getNode())
6049 return NewOp;
6050 }
6051
Bob Wilson6f34e272009-08-14 05:16:33 +00006052 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006053}
6054
Eli Friedmana5e244c2011-10-24 23:08:52 +00006055static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6056 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6057 SDValue Lane = Op.getOperand(2);
6058 if (!isa<ConstantSDNode>(Lane))
6059 return SDValue();
6060
6061 return Op;
6062}
6063
Bob Wilson2e076c42009-06-22 23:27:02 +00006064static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006065 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006066 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006067 if (!isa<ConstantSDNode>(Lane))
6068 return SDValue();
6069
6070 SDValue Vec = Op.getOperand(0);
6071 if (Op.getValueType() == MVT::i32 &&
6072 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006073 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006074 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6075 }
6076
6077 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006078}
6079
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006080static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6081 // The only time a CONCAT_VECTORS operation can have legal types is when
6082 // two 64-bit vectors are concatenated to a 128-bit vector.
6083 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6084 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006085 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006086 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006087 SDValue Op0 = Op.getOperand(0);
6088 SDValue Op1 = Op.getOperand(1);
6089 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006090 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006091 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006092 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006093 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006094 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006095 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006096 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006097 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006098}
6099
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006100/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6101/// element has been zero/sign-extended, depending on the isSigned parameter,
6102/// from an integer type half its size.
6103static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6104 bool isSigned) {
6105 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6106 EVT VT = N->getValueType(0);
6107 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6108 SDNode *BVN = N->getOperand(0).getNode();
6109 if (BVN->getValueType(0) != MVT::v4i32 ||
6110 BVN->getOpcode() != ISD::BUILD_VECTOR)
6111 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006112 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006113 unsigned HiElt = 1 - LoElt;
6114 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6115 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6116 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6117 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6118 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6119 return false;
6120 if (isSigned) {
6121 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6122 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6123 return true;
6124 } else {
6125 if (Hi0->isNullValue() && Hi1->isNullValue())
6126 return true;
6127 }
6128 return false;
6129 }
6130
6131 if (N->getOpcode() != ISD::BUILD_VECTOR)
6132 return false;
6133
6134 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6135 SDNode *Elt = N->getOperand(i).getNode();
6136 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6137 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6138 unsigned HalfSize = EltSize / 2;
6139 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006140 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006141 return false;
6142 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006143 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006144 return false;
6145 }
6146 continue;
6147 }
6148 return false;
6149 }
6150
6151 return true;
6152}
6153
6154/// isSignExtended - Check if a node is a vector value that is sign-extended
6155/// or a constant BUILD_VECTOR with sign-extended elements.
6156static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6157 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6158 return true;
6159 if (isExtendedBUILD_VECTOR(N, DAG, true))
6160 return true;
6161 return false;
6162}
6163
6164/// isZeroExtended - Check if a node is a vector value that is zero-extended
6165/// or a constant BUILD_VECTOR with zero-extended elements.
6166static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6167 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6168 return true;
6169 if (isExtendedBUILD_VECTOR(N, DAG, false))
6170 return true;
6171 return false;
6172}
6173
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006174static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6175 if (OrigVT.getSizeInBits() >= 64)
6176 return OrigVT;
6177
6178 assert(OrigVT.isSimple() && "Expecting a simple value type");
6179
6180 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6181 switch (OrigSimpleTy) {
6182 default: llvm_unreachable("Unexpected Vector Type");
6183 case MVT::v2i8:
6184 case MVT::v2i16:
6185 return MVT::v2i32;
6186 case MVT::v4i8:
6187 return MVT::v4i16;
6188 }
6189}
6190
Sebastian Popa204f722012-11-30 19:08:04 +00006191/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6192/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6193/// We insert the required extension here to get the vector to fill a D register.
6194static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6195 const EVT &OrigTy,
6196 const EVT &ExtTy,
6197 unsigned ExtOpcode) {
6198 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6199 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6200 // 64-bits we need to insert a new extension so that it will be 64-bits.
6201 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6202 if (OrigTy.getSizeInBits() >= 64)
6203 return N;
6204
6205 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006206 EVT NewVT = getExtensionTo64Bits(OrigTy);
6207
Andrew Trickef9de2a2013-05-25 02:42:55 +00006208 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006209}
6210
6211/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6212/// does not do any sign/zero extension. If the original vector is less
6213/// than 64 bits, an appropriate extension will be added after the load to
6214/// reach a total size of 64 bits. We have to add the extension separately
6215/// because ARM does not have a sign/zero extending load for vectors.
6216static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006217 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6218
6219 // The load already has the right type.
6220 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006221 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006222 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6223 LD->isNonTemporal(), LD->isInvariant(),
6224 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006225
6226 // We need to create a zextload/sextload. We cannot just create a load
6227 // followed by a zext/zext node because LowerMUL is also run during normal
6228 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006229 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006230 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006231 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006232 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006233}
6234
6235/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6236/// extending load, or BUILD_VECTOR with extended elements, return the
6237/// unextended value. The unextended vector should be 64 bits so that it can
6238/// be used as an operand to a VMULL instruction. If the original vector size
6239/// before extension is less than 64 bits we add a an extension to resize
6240/// the vector to 64 bits.
6241static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006242 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006243 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6244 N->getOperand(0)->getValueType(0),
6245 N->getValueType(0),
6246 N->getOpcode());
6247
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006248 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006249 return SkipLoadExtensionForVMULL(LD, DAG);
6250
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006251 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6252 // have been legalized as a BITCAST from v4i32.
6253 if (N->getOpcode() == ISD::BITCAST) {
6254 SDNode *BVN = N->getOperand(0).getNode();
6255 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6256 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006257 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006258 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006259 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6260 }
6261 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6262 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6263 EVT VT = N->getValueType(0);
6264 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6265 unsigned NumElts = VT.getVectorNumElements();
6266 MVT TruncVT = MVT::getIntegerVT(EltSize);
6267 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006268 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006269 for (unsigned i = 0; i != NumElts; ++i) {
6270 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6271 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006272 // Element types smaller than 32 bits are not legal, so use i32 elements.
6273 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006274 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006275 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006276 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006277 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006278}
6279
Evan Chenge2086e72011-03-29 01:56:09 +00006280static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6281 unsigned Opcode = N->getOpcode();
6282 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6283 SDNode *N0 = N->getOperand(0).getNode();
6284 SDNode *N1 = N->getOperand(1).getNode();
6285 return N0->hasOneUse() && N1->hasOneUse() &&
6286 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6287 }
6288 return false;
6289}
6290
6291static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6292 unsigned Opcode = N->getOpcode();
6293 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6294 SDNode *N0 = N->getOperand(0).getNode();
6295 SDNode *N1 = N->getOperand(1).getNode();
6296 return N0->hasOneUse() && N1->hasOneUse() &&
6297 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6298 }
6299 return false;
6300}
6301
Bob Wilson38ab35a2010-09-01 23:50:19 +00006302static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6303 // Multiplications are only custom-lowered for 128-bit vectors so that
6304 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6305 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006306 assert(VT.is128BitVector() && VT.isInteger() &&
6307 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006308 SDNode *N0 = Op.getOperand(0).getNode();
6309 SDNode *N1 = Op.getOperand(1).getNode();
6310 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006311 bool isMLA = false;
6312 bool isN0SExt = isSignExtended(N0, DAG);
6313 bool isN1SExt = isSignExtended(N1, DAG);
6314 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006315 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006316 else {
6317 bool isN0ZExt = isZeroExtended(N0, DAG);
6318 bool isN1ZExt = isZeroExtended(N1, DAG);
6319 if (isN0ZExt && isN1ZExt)
6320 NewOpc = ARMISD::VMULLu;
6321 else if (isN1SExt || isN1ZExt) {
6322 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6323 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6324 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6325 NewOpc = ARMISD::VMULLs;
6326 isMLA = true;
6327 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6328 NewOpc = ARMISD::VMULLu;
6329 isMLA = true;
6330 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6331 std::swap(N0, N1);
6332 NewOpc = ARMISD::VMULLu;
6333 isMLA = true;
6334 }
6335 }
6336
6337 if (!NewOpc) {
6338 if (VT == MVT::v2i64)
6339 // Fall through to expand this. It is not legal.
6340 return SDValue();
6341 else
6342 // Other vector multiplications are legal.
6343 return Op;
6344 }
6345 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006346
6347 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006348 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006349 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006350 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006351 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006352 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006353 assert(Op0.getValueType().is64BitVector() &&
6354 Op1.getValueType().is64BitVector() &&
6355 "unexpected types for extended operands to VMULL");
6356 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6357 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006358
Evan Chenge2086e72011-03-29 01:56:09 +00006359 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6360 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6361 // vmull q0, d4, d6
6362 // vmlal q0, d5, d6
6363 // is faster than
6364 // vaddl q0, d4, d5
6365 // vmovl q1, d6
6366 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006367 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6368 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006369 EVT Op1VT = Op1.getValueType();
6370 return DAG.getNode(N0->getOpcode(), DL, VT,
6371 DAG.getNode(NewOpc, DL, VT,
6372 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6373 DAG.getNode(NewOpc, DL, VT,
6374 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006375}
6376
Owen Anderson77aa2662011-04-05 21:48:57 +00006377static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006378LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006379 // TODO: Should this propagate fast-math-flags?
6380
Nate Begemanfa62d502011-02-11 20:53:29 +00006381 // Convert to float
6382 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6383 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6384 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6385 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6386 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6387 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6388 // Get reciprocal estimate.
6389 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006390 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006391 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6392 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006393 // Because char has a smaller range than uchar, we can actually get away
6394 // without any newton steps. This requires that we use a weird bias
6395 // of 0xb000, however (again, this has been exhaustively tested).
6396 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6397 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6398 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006399 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006400 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6401 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6402 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6403 // Convert back to short.
6404 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6405 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6406 return X;
6407}
6408
Owen Anderson77aa2662011-04-05 21:48:57 +00006409static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006410LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006411 // TODO: Should this propagate fast-math-flags?
6412
Nate Begemanfa62d502011-02-11 20:53:29 +00006413 SDValue N2;
6414 // Convert to float.
6415 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6416 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6417 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6418 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6419 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6420 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006421
Nate Begemanfa62d502011-02-11 20:53:29 +00006422 // Use reciprocal estimate and one refinement step.
6423 // float4 recip = vrecpeq_f32(yf);
6424 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006425 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006426 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6427 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006428 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006429 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006430 N1, N2);
6431 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6432 // Because short has a smaller range than ushort, we can actually get away
6433 // with only a single newton step. This requires that we use a weird bias
6434 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006435 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006436 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6437 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006438 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006439 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6440 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6441 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6442 // Convert back to integer and return.
6443 // return vmovn_s32(vcvt_s32_f32(result));
6444 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6445 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6446 return N0;
6447}
6448
6449static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6450 EVT VT = Op.getValueType();
6451 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6452 "unexpected type for custom-lowering ISD::SDIV");
6453
Andrew Trickef9de2a2013-05-25 02:42:55 +00006454 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006455 SDValue N0 = Op.getOperand(0);
6456 SDValue N1 = Op.getOperand(1);
6457 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006458
Nate Begemanfa62d502011-02-11 20:53:29 +00006459 if (VT == MVT::v8i8) {
6460 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6461 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006462
Nate Begemanfa62d502011-02-11 20:53:29 +00006463 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006464 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006465 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006466 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006467 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006468 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006469 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006470 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006471
6472 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6473 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6474
6475 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6476 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006477
Nate Begemanfa62d502011-02-11 20:53:29 +00006478 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6479 return N0;
6480 }
6481 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6482}
6483
6484static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006485 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006486 EVT VT = Op.getValueType();
6487 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6488 "unexpected type for custom-lowering ISD::UDIV");
6489
Andrew Trickef9de2a2013-05-25 02:42:55 +00006490 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006491 SDValue N0 = Op.getOperand(0);
6492 SDValue N1 = Op.getOperand(1);
6493 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006494
Nate Begemanfa62d502011-02-11 20:53:29 +00006495 if (VT == MVT::v8i8) {
6496 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6497 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006498
Nate Begemanfa62d502011-02-11 20:53:29 +00006499 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006500 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006501 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006502 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006503 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006504 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006505 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006506 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006507
Nate Begemanfa62d502011-02-11 20:53:29 +00006508 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6509 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006510
Nate Begemanfa62d502011-02-11 20:53:29 +00006511 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6512 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006513
6514 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006515 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6516 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006517 N0);
6518 return N0;
6519 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006520
Nate Begemanfa62d502011-02-11 20:53:29 +00006521 // v4i16 sdiv ... Convert to float.
6522 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6523 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6524 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6525 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6526 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006527 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006528
6529 // Use reciprocal estimate and two refinement steps.
6530 // float4 recip = vrecpeq_f32(yf);
6531 // recip *= vrecpsq_f32(yf, recip);
6532 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006533 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006534 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6535 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006536 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006537 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006538 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006539 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006540 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006541 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006542 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006543 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6544 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6545 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6546 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006547 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006548 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6549 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006550 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006551 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6552 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6553 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6554 // Convert back to integer and return.
6555 // return vmovn_u32(vcvt_s32_f32(result));
6556 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6557 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6558 return N0;
6559}
6560
Evan Chenge8916542011-08-30 01:34:54 +00006561static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6562 EVT VT = Op.getNode()->getValueType(0);
6563 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6564
6565 unsigned Opc;
6566 bool ExtraOp = false;
6567 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006568 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006569 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6570 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6571 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6572 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6573 }
6574
6575 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006576 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006577 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006578 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006579 Op.getOperand(1), Op.getOperand(2));
6580}
6581
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006582SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6583 assert(Subtarget->isTargetDarwin());
6584
6585 // For iOS, we want to call an alternative entry point: __sincos_stret,
6586 // return values are passed via sret.
6587 SDLoc dl(Op);
6588 SDValue Arg = Op.getOperand(0);
6589 EVT ArgVT = Arg.getValueType();
6590 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006591 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006592
6593 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006594 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006595
6596 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006597 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006598 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006599
6600 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006601 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6602 SDValue SRet;
6603 if (ShouldUseSRet) {
6604 // Create stack object for sret.
6605 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6606 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6607 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6608 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6609
6610 ArgListEntry Entry;
6611 Entry.Node = SRet;
6612 Entry.Ty = RetTy->getPointerTo();
6613 Entry.isSExt = false;
6614 Entry.isZExt = false;
6615 Entry.isSRet = true;
6616 Args.push_back(Entry);
6617 RetTy = Type::getVoidTy(*DAG.getContext());
6618 }
6619
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006620 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006621 Entry.Node = Arg;
6622 Entry.Ty = ArgTy;
6623 Entry.isSExt = false;
6624 Entry.isZExt = false;
6625 Args.push_back(Entry);
6626
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006627 const char *LibcallName =
6628 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006629 RTLIB::Libcall LC =
6630 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6631 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006632 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006633
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006634 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006635 CLI.setDebugLoc(dl)
6636 .setChain(DAG.getEntryNode())
6637 .setCallee(CC, RetTy, Callee, std::move(Args), 0)
6638 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006639 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6640
Tim Northover8b403662015-10-28 22:51:16 +00006641 if (!ShouldUseSRet)
6642 return CallResult.first;
6643
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006644 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6645 MachinePointerInfo(), false, false, false, 0);
6646
6647 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006648 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006649 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006650 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6651 MachinePointerInfo(), false, false, false, 0);
6652
6653 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6654 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6655 LoadSin.getValue(0), LoadCos.getValue(0));
6656}
6657
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006658SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006659 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006660 SDValue &Chain) const {
6661 EVT VT = Op.getValueType();
6662 assert((VT == MVT::i32 || VT == MVT::i64) &&
6663 "unexpected type for custom lowering DIV");
6664 SDLoc dl(Op);
6665
6666 const auto &DL = DAG.getDataLayout();
6667 const auto &TLI = DAG.getTargetLoweringInfo();
6668
6669 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006670 if (Signed)
6671 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6672 else
6673 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006674
6675 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6676
6677 ARMTargetLowering::ArgListTy Args;
6678
6679 for (auto AI : {1, 0}) {
6680 ArgListEntry Arg;
6681 Arg.Node = Op.getOperand(AI);
6682 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6683 Args.push_back(Arg);
6684 }
6685
6686 CallLoweringInfo CLI(DAG);
6687 CLI.setDebugLoc(dl)
6688 .setChain(Chain)
6689 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6690 ES, std::move(Args), 0);
6691
6692 return LowerCallTo(CLI).first;
6693}
6694
Martell Maloned1229242015-11-26 15:34:03 +00006695SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6696 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006697 assert(Op.getValueType() == MVT::i32 &&
6698 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006699 SDLoc dl(Op);
6700
6701 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6702 DAG.getEntryNode(), Op.getOperand(1));
6703
Martell Maloned1229242015-11-26 15:34:03 +00006704 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006705}
6706
6707void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00006708 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006709 SmallVectorImpl<SDValue> &Results) const {
6710 const auto &DL = DAG.getDataLayout();
6711 const auto &TLI = DAG.getTargetLoweringInfo();
6712
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006713 assert(Op.getValueType() == MVT::i64 &&
6714 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006715 SDLoc dl(Op);
6716
6717 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6718 DAG.getConstant(0, dl, MVT::i32));
6719 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6720 DAG.getConstant(1, dl, MVT::i32));
6721 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6722
6723 SDValue DBZCHK =
6724 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6725
Martell Maloned1229242015-11-26 15:34:03 +00006726 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006727
6728 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6729 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6730 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6731 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6732
6733 Results.push_back(Lower);
6734 Results.push_back(Upper);
6735}
6736
Eli Friedman10f9ce22011-09-15 22:26:18 +00006737static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006738 // Monotonic load/store is legal for all targets
6739 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6740 return Op;
6741
Alp Tokercb402912014-01-24 17:20:08 +00006742 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006743 // dmb or equivalent available.
6744 return SDValue();
6745}
6746
Tim Northoverbc933082013-05-23 19:11:20 +00006747static void ReplaceREADCYCLECOUNTER(SDNode *N,
6748 SmallVectorImpl<SDValue> &Results,
6749 SelectionDAG &DAG,
6750 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006751 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006752 // Under Power Management extensions, the cycle-count is:
6753 // mrc p15, #0, <Rt>, c9, c13, #0
6754 SDValue Ops[] = { N->getOperand(0), // Chain
6755 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6756 DAG.getConstant(15, DL, MVT::i32),
6757 DAG.getConstant(0, DL, MVT::i32),
6758 DAG.getConstant(9, DL, MVT::i32),
6759 DAG.getConstant(13, DL, MVT::i32),
6760 DAG.getConstant(0, DL, MVT::i32)
6761 };
Tim Northoverbc933082013-05-23 19:11:20 +00006762
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006763 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6764 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6765 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6766 DAG.getConstant(0, DL, MVT::i32)));
6767 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006768}
6769
Dan Gohman21cea8a2010-04-17 15:26:15 +00006770SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006771 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006772 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006773 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006774 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006775 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006776 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006777 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6778 default: llvm_unreachable("unknown object format");
6779 case Triple::COFF:
6780 return LowerGlobalAddressWindows(Op, DAG);
6781 case Triple::ELF:
6782 return LowerGlobalAddressELF(Op, DAG);
6783 case Triple::MachO:
6784 return LowerGlobalAddressDarwin(Op, DAG);
6785 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006786 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006787 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006788 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6789 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006790 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006791 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006792 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006793 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006794 case ISD::SINT_TO_FP:
6795 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6796 case ISD::FP_TO_SINT:
6797 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006798 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006799 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006800 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006801 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006802 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006803 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006804 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6805 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006806 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006807 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006808 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006809 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00006810 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
6811 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006812 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006813 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006814 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006815 case ISD::CTTZ:
6816 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006817 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006818 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006819 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006820 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006821 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006822 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006823 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006824 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006825 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006826 case ISD::MUL: return LowerMUL(Op, DAG);
Martell Malonea6b867e2015-11-23 13:11:39 +00006827 case ISD::SDIV: return LowerSDIV(Op, DAG);
Martell Maloned1229242015-11-26 15:34:03 +00006828 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006829 case ISD::ADDC:
6830 case ISD::ADDE:
6831 case ISD::SUBC:
6832 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006833 case ISD::SADDO:
6834 case ISD::UADDO:
6835 case ISD::SSUBO:
6836 case ISD::USUBO:
6837 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006838 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006839 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006840 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006841 case ISD::SDIVREM:
6842 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006843 case ISD::DYNAMIC_STACKALLOC:
6844 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6845 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6846 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006847 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6848 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006849 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00006850 }
Evan Cheng10043e22007-01-19 07:51:42 +00006851}
6852
Duncan Sands6ed40142008-12-01 11:39:25 +00006853/// ReplaceNodeResults - Replace the results of node with an illegal result
6854/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006855void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006856 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006857 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006858 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006859 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006860 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006861 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006862 case ISD::READ_REGISTER:
6863 ExpandREAD_REGISTER(N, Results, DAG);
6864 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006865 case ISD::BITCAST:
6866 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006867 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006868 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006869 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006870 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006871 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00006872 case ISD::SREM:
6873 case ISD::UREM:
6874 Res = LowerREM(N, DAG);
6875 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006876 case ISD::READCYCLECOUNTER:
6877 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6878 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006879 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00006880 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006881 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00006882 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
6883 Results);
Duncan Sands6ed40142008-12-01 11:39:25 +00006884 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006885 if (Res.getNode())
6886 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006887}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006888
Evan Cheng10043e22007-01-19 07:51:42 +00006889//===----------------------------------------------------------------------===//
6890// ARM Scheduler Hooks
6891//===----------------------------------------------------------------------===//
6892
Bill Wendling030b58e2011-10-06 22:18:16 +00006893/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6894/// registers the function context.
6895void ARMTargetLowering::
6896SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6897 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006898 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006899 DebugLoc dl = MI->getDebugLoc();
6900 MachineFunction *MF = MBB->getParent();
6901 MachineRegisterInfo *MRI = &MF->getRegInfo();
6902 MachineConstantPool *MCP = MF->getConstantPool();
6903 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6904 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006905
Bill Wendling374ee192011-10-03 21:25:38 +00006906 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006907 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006908
Bill Wendling374ee192011-10-03 21:25:38 +00006909 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006910 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006911 ARMConstantPoolValue *CPV =
6912 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6913 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6914
Craig Topper61e88f42014-11-21 05:58:21 +00006915 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6916 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006917
Bill Wendling030b58e2011-10-06 22:18:16 +00006918 // Grab constant pool and fixed stack memory operands.
6919 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006920 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
6921 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006922
6923 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006924 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
6925 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006926
6927 // Load the address of the dispatch MBB into the jump buffer.
6928 if (isThumb2) {
6929 // Incoming value: jbuf
6930 // ldr.n r5, LCPI1_1
6931 // orr r5, r5, #1
6932 // add r5, pc
6933 // str r5, [$jbuf, #+4] ; &jbuf[1]
6934 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6935 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6936 .addConstantPoolIndex(CPI)
6937 .addMemOperand(CPMMO));
6938 // Set the low bit because of thumb mode.
6939 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6940 AddDefaultCC(
6941 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6942 .addReg(NewVReg1, RegState::Kill)
6943 .addImm(0x01)));
6944 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6945 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6946 .addReg(NewVReg2, RegState::Kill)
6947 .addImm(PCLabelId);
6948 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6949 .addReg(NewVReg3, RegState::Kill)
6950 .addFrameIndex(FI)
6951 .addImm(36) // &jbuf[1] :: pc
6952 .addMemOperand(FIMMOSt));
6953 } else if (isThumb) {
6954 // Incoming value: jbuf
6955 // ldr.n r1, LCPI1_4
6956 // add r1, pc
6957 // mov r2, #1
6958 // orrs r1, r2
6959 // add r2, $jbuf, #+4 ; &jbuf[1]
6960 // str r1, [r2]
6961 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6962 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6963 .addConstantPoolIndex(CPI)
6964 .addMemOperand(CPMMO));
6965 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6966 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6967 .addReg(NewVReg1, RegState::Kill)
6968 .addImm(PCLabelId);
6969 // Set the low bit because of thumb mode.
6970 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6971 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6972 .addReg(ARM::CPSR, RegState::Define)
6973 .addImm(1));
6974 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6975 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6976 .addReg(ARM::CPSR, RegState::Define)
6977 .addReg(NewVReg2, RegState::Kill)
6978 .addReg(NewVReg3, RegState::Kill));
6979 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006980 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6981 .addFrameIndex(FI)
6982 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006983 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6984 .addReg(NewVReg4, RegState::Kill)
6985 .addReg(NewVReg5, RegState::Kill)
6986 .addImm(0)
6987 .addMemOperand(FIMMOSt));
6988 } else {
6989 // Incoming value: jbuf
6990 // ldr r1, LCPI1_1
6991 // add r1, pc, r1
6992 // str r1, [$jbuf, #+4] ; &jbuf[1]
6993 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6994 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6995 .addConstantPoolIndex(CPI)
6996 .addImm(0)
6997 .addMemOperand(CPMMO));
6998 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6999 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7000 .addReg(NewVReg1, RegState::Kill)
7001 .addImm(PCLabelId));
7002 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7003 .addReg(NewVReg2, RegState::Kill)
7004 .addFrameIndex(FI)
7005 .addImm(36) // &jbuf[1] :: pc
7006 .addMemOperand(FIMMOSt));
7007 }
7008}
7009
Matthias Brauneec4efc2015-04-28 00:37:05 +00007010void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
7011 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007012 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007013 DebugLoc dl = MI->getDebugLoc();
7014 MachineFunction *MF = MBB->getParent();
7015 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007016 MachineFrameInfo *MFI = MF->getFrameInfo();
7017 int FI = MFI->getFunctionContextIndex();
7018
Craig Topper61e88f42014-11-21 05:58:21 +00007019 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7020 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007021
Bill Wendling362c1b02011-10-06 21:29:56 +00007022 // Get a mapping of the call site numbers to all of the landing pads they're
7023 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007024 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7025 unsigned MaxCSNum = 0;
7026 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007027 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7028 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007029 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007030
7031 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7032 // pad.
7033 for (MachineBasicBlock::iterator
7034 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7035 if (!II->isEHLabel()) continue;
7036
7037 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007038 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007039
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007040 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7041 for (SmallVectorImpl<unsigned>::iterator
7042 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7043 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007044 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007045 MaxCSNum = std::max(MaxCSNum, *CSI);
7046 }
Bill Wendling202803e2011-10-05 00:02:33 +00007047 break;
7048 }
7049 }
7050
7051 // Get an ordered list of the machine basic blocks for the jump table.
7052 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00007053 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007054 LPadList.reserve(CallSiteNumToLPad.size());
7055 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7056 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7057 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007058 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007059 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007060 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7061 }
Bill Wendling202803e2011-10-05 00:02:33 +00007062 }
7063
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007064 assert(!LPadList.empty() &&
7065 "No landing pad destinations for the dispatch jump table!");
7066
Bill Wendling362c1b02011-10-06 21:29:56 +00007067 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007068 MachineJumpTableInfo *JTI =
7069 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7070 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007071 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007072
Bill Wendling362c1b02011-10-06 21:29:56 +00007073 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007074
7075 // Shove the dispatch's address into the return slot in the function context.
7076 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007077 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007078
Bill Wendling324be982011-10-05 00:39:32 +00007079 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007080 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007081 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007082 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007083 else
7084 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7085
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007086 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007087 DispatchBB->addSuccessor(TrapBB);
7088
7089 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7090 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007091
Bill Wendling510fbcd2011-10-17 21:32:56 +00007092 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007093 MF->insert(MF->end(), DispatchBB);
7094 MF->insert(MF->end(), DispContBB);
7095 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007096
Bill Wendling030b58e2011-10-06 22:18:16 +00007097 // Insert code into the entry block that creates and registers the function
7098 // context.
7099 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7100
Alex Lorenze40c8a22015-08-11 23:09:45 +00007101 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7102 MachinePointerInfo::getFixedStack(*MF, FI),
7103 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007104
Chad Rosier1ec8e402012-11-06 23:05:24 +00007105 MachineInstrBuilder MIB;
7106 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7107
7108 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7109 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7110
7111 // Add a register mask with no preserved registers. This results in all
7112 // registers being marked as clobbered.
7113 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007114
Bill Wendling85833f72011-10-18 22:49:07 +00007115 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007116 if (Subtarget->isThumb2()) {
7117 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7118 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7119 .addFrameIndex(FI)
7120 .addImm(4)
7121 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007122
Bill Wendling85833f72011-10-18 22:49:07 +00007123 if (NumLPads < 256) {
7124 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7125 .addReg(NewVReg1)
7126 .addImm(LPadList.size()));
7127 } else {
7128 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7129 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007130 .addImm(NumLPads & 0xFFFF));
7131
7132 unsigned VReg2 = VReg1;
7133 if ((NumLPads & 0xFFFF0000) != 0) {
7134 VReg2 = MRI->createVirtualRegister(TRC);
7135 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7136 .addReg(VReg1)
7137 .addImm(NumLPads >> 16));
7138 }
7139
Bill Wendling85833f72011-10-18 22:49:07 +00007140 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7141 .addReg(NewVReg1)
7142 .addReg(VReg2));
7143 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007144
Bill Wendling5626c662011-10-06 22:53:00 +00007145 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7146 .addMBB(TrapBB)
7147 .addImm(ARMCC::HI)
7148 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007149
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007150 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7151 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007152 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007153
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007154 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007155 AddDefaultCC(
7156 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007157 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7158 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007159 .addReg(NewVReg1)
7160 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7161
7162 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007163 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007164 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007165 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007166 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007167 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7168 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7169 .addFrameIndex(FI)
7170 .addImm(1)
7171 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007172
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007173 if (NumLPads < 256) {
7174 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7175 .addReg(NewVReg1)
7176 .addImm(NumLPads));
7177 } else {
7178 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007179 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7180 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7181
7182 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007183 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007184 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007185 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007186 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007187
7188 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7189 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7190 .addReg(VReg1, RegState::Define)
7191 .addConstantPoolIndex(Idx));
7192 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7193 .addReg(NewVReg1)
7194 .addReg(VReg1));
7195 }
7196
Bill Wendlingb3d46782011-10-06 23:37:36 +00007197 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7198 .addMBB(TrapBB)
7199 .addImm(ARMCC::HI)
7200 .addReg(ARM::CPSR);
7201
7202 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7203 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7204 .addReg(ARM::CPSR, RegState::Define)
7205 .addReg(NewVReg1)
7206 .addImm(2));
7207
7208 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007209 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007210 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007211
7212 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7213 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7214 .addReg(ARM::CPSR, RegState::Define)
7215 .addReg(NewVReg2, RegState::Kill)
7216 .addReg(NewVReg3));
7217
Alex Lorenze40c8a22015-08-11 23:09:45 +00007218 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7219 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007220
7221 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7222 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7223 .addReg(NewVReg4, RegState::Kill)
7224 .addImm(0)
7225 .addMemOperand(JTMMOLd));
7226
Chad Rosier96603432013-03-01 18:30:38 +00007227 unsigned NewVReg6 = NewVReg5;
7228 if (RelocM == Reloc::PIC_) {
7229 NewVReg6 = MRI->createVirtualRegister(TRC);
7230 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7231 .addReg(ARM::CPSR, RegState::Define)
7232 .addReg(NewVReg5, RegState::Kill)
7233 .addReg(NewVReg3));
7234 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007235
7236 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7237 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007238 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007239 } else {
7240 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7241 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7242 .addFrameIndex(FI)
7243 .addImm(4)
7244 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007245
Bill Wendling4969dcd2011-10-18 22:52:20 +00007246 if (NumLPads < 256) {
7247 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7248 .addReg(NewVReg1)
7249 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007250 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007251 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7252 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007253 .addImm(NumLPads & 0xFFFF));
7254
7255 unsigned VReg2 = VReg1;
7256 if ((NumLPads & 0xFFFF0000) != 0) {
7257 VReg2 = MRI->createVirtualRegister(TRC);
7258 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7259 .addReg(VReg1)
7260 .addImm(NumLPads >> 16));
7261 }
7262
Bill Wendling4969dcd2011-10-18 22:52:20 +00007263 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7264 .addReg(NewVReg1)
7265 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007266 } else {
7267 MachineConstantPool *ConstantPool = MF->getConstantPool();
7268 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7269 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7270
7271 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007272 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007273 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007274 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007275 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7276
7277 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7278 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7279 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007280 .addConstantPoolIndex(Idx)
7281 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007282 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7283 .addReg(NewVReg1)
7284 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007285 }
7286
Bill Wendling5626c662011-10-06 22:53:00 +00007287 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7288 .addMBB(TrapBB)
7289 .addImm(ARMCC::HI)
7290 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007291
Bill Wendling973c8172011-10-18 22:11:18 +00007292 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007293 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007294 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007295 .addReg(NewVReg1)
7296 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007297 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7298 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007299 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007300
Alex Lorenze40c8a22015-08-11 23:09:45 +00007301 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7302 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007303 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007304 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007305 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7306 .addReg(NewVReg3, RegState::Kill)
7307 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007308 .addImm(0)
7309 .addMemOperand(JTMMOLd));
7310
Chad Rosier96603432013-03-01 18:30:38 +00007311 if (RelocM == Reloc::PIC_) {
7312 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7313 .addReg(NewVReg5, RegState::Kill)
7314 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007315 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007316 } else {
7317 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7318 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007319 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007320 }
Bill Wendling5626c662011-10-06 22:53:00 +00007321 }
Bill Wendling202803e2011-10-05 00:02:33 +00007322
Bill Wendling324be982011-10-05 00:39:32 +00007323 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007324 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007325 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007326 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7327 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007328 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007329 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007330 }
7331
Bill Wendling26d27802011-10-17 05:25:09 +00007332 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007333 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007334 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007335 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007336
7337 // Remove the landing pad successor from the invoke block and replace it
7338 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007339 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7340 BB->succ_end());
7341 while (!Successors.empty()) {
7342 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007343 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007344 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007345 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007346 }
7347 }
7348
7349 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007350
7351 // Find the invoke call and mark all of the callee-saved registers as
7352 // 'implicit defined' so that they're spilled. This prevents code from
7353 // moving instructions to before the EH block, where they will never be
7354 // executed.
7355 for (MachineBasicBlock::reverse_iterator
7356 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007357 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007358
7359 DenseMap<unsigned, bool> DefRegs;
7360 for (MachineInstr::mop_iterator
7361 OI = II->operands_begin(), OE = II->operands_end();
7362 OI != OE; ++OI) {
7363 if (!OI->isReg()) continue;
7364 DefRegs[OI->getReg()] = true;
7365 }
7366
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007367 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007368
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007369 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007370 unsigned Reg = SavedRegs[i];
7371 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007372 !ARM::tGPRRegClass.contains(Reg) &&
7373 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007374 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007375 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007376 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007377 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007378 continue;
7379 if (!DefRegs[Reg])
7380 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007381 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007382
7383 break;
7384 }
Bill Wendling883ec972011-10-07 23:18:02 +00007385 }
Bill Wendling324be982011-10-05 00:39:32 +00007386
Bill Wendling617075f2011-10-18 18:30:49 +00007387 // Mark all former landing pads as non-landing pads. The dispatch is the only
7388 // landing pad now.
7389 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7390 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007391 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007392
Bill Wendling324be982011-10-05 00:39:32 +00007393 // The instruction is gone now.
7394 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007395}
7396
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007397static
7398MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7399 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7400 E = MBB->succ_end(); I != E; ++I)
7401 if (*I != Succ)
7402 return *I;
7403 llvm_unreachable("Expecting a BB with two successors!");
7404}
7405
Manman Renb504f492013-10-29 22:27:32 +00007406/// Return the load opcode for a given load size. If load size >= 8,
7407/// neon opcode will be returned.
7408static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7409 if (LdSize >= 8)
7410 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7411 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7412 if (IsThumb1)
7413 return LdSize == 4 ? ARM::tLDRi
7414 : LdSize == 2 ? ARM::tLDRHi
7415 : LdSize == 1 ? ARM::tLDRBi : 0;
7416 if (IsThumb2)
7417 return LdSize == 4 ? ARM::t2LDR_POST
7418 : LdSize == 2 ? ARM::t2LDRH_POST
7419 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7420 return LdSize == 4 ? ARM::LDR_POST_IMM
7421 : LdSize == 2 ? ARM::LDRH_POST
7422 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7423}
7424
7425/// Return the store opcode for a given store size. If store size >= 8,
7426/// neon opcode will be returned.
7427static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7428 if (StSize >= 8)
7429 return StSize == 16 ? ARM::VST1q32wb_fixed
7430 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7431 if (IsThumb1)
7432 return StSize == 4 ? ARM::tSTRi
7433 : StSize == 2 ? ARM::tSTRHi
7434 : StSize == 1 ? ARM::tSTRBi : 0;
7435 if (IsThumb2)
7436 return StSize == 4 ? ARM::t2STR_POST
7437 : StSize == 2 ? ARM::t2STRH_POST
7438 : StSize == 1 ? ARM::t2STRB_POST : 0;
7439 return StSize == 4 ? ARM::STR_POST_IMM
7440 : StSize == 2 ? ARM::STRH_POST
7441 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7442}
7443
7444/// Emit a post-increment load operation with given size. The instructions
7445/// will be added to BB at Pos.
7446static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7447 const TargetInstrInfo *TII, DebugLoc dl,
7448 unsigned LdSize, unsigned Data, unsigned AddrIn,
7449 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7450 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7451 assert(LdOpc != 0 && "Should have a load opcode");
7452 if (LdSize >= 8) {
7453 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7454 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7455 .addImm(0));
7456 } else if (IsThumb1) {
7457 // load + update AddrIn
7458 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7459 .addReg(AddrIn).addImm(0));
7460 MachineInstrBuilder MIB =
7461 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7462 MIB = AddDefaultT1CC(MIB);
7463 MIB.addReg(AddrIn).addImm(LdSize);
7464 AddDefaultPred(MIB);
7465 } else if (IsThumb2) {
7466 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7467 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7468 .addImm(LdSize));
7469 } else { // arm
7470 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7471 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7472 .addReg(0).addImm(LdSize));
7473 }
7474}
7475
7476/// Emit a post-increment store operation with given size. The instructions
7477/// will be added to BB at Pos.
7478static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7479 const TargetInstrInfo *TII, DebugLoc dl,
7480 unsigned StSize, unsigned Data, unsigned AddrIn,
7481 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7482 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7483 assert(StOpc != 0 && "Should have a store opcode");
7484 if (StSize >= 8) {
7485 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7486 .addReg(AddrIn).addImm(0).addReg(Data));
7487 } else if (IsThumb1) {
7488 // store + update AddrIn
7489 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7490 .addReg(AddrIn).addImm(0));
7491 MachineInstrBuilder MIB =
7492 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7493 MIB = AddDefaultT1CC(MIB);
7494 MIB.addReg(AddrIn).addImm(StSize);
7495 AddDefaultPred(MIB);
7496 } else if (IsThumb2) {
7497 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7498 .addReg(Data).addReg(AddrIn).addImm(StSize));
7499 } else { // arm
7500 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7501 .addReg(Data).addReg(AddrIn).addReg(0)
7502 .addImm(StSize));
7503 }
7504}
7505
David Peixottoc32e24a2013-10-17 19:49:22 +00007506MachineBasicBlock *
7507ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7508 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007509 // This pseudo instruction has 3 operands: dst, src, size
7510 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7511 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007512 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007513 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007514 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007515
7516 unsigned dest = MI->getOperand(0).getReg();
7517 unsigned src = MI->getOperand(1).getReg();
7518 unsigned SizeVal = MI->getOperand(2).getImm();
7519 unsigned Align = MI->getOperand(3).getImm();
7520 DebugLoc dl = MI->getDebugLoc();
7521
Manman Rene8735522012-06-01 19:33:18 +00007522 MachineFunction *MF = BB->getParent();
7523 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007524 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007525 const TargetRegisterClass *TRC = nullptr;
7526 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007527
7528 bool IsThumb1 = Subtarget->isThumb1Only();
7529 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007530
7531 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007532 UnitSize = 1;
7533 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007534 UnitSize = 2;
7535 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007536 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007537 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007538 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007539 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007540 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007541 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007542 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007543 }
7544 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007545 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007546 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007547 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007548
David Peixottob0653e532013-10-24 16:39:36 +00007549 // Select the correct opcode and register class for unit size load/store
7550 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007551 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007552 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007553 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7554 : UnitSize == 8 ? &ARM::DPRRegClass
7555 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007556
Manman Rene8735522012-06-01 19:33:18 +00007557 unsigned BytesLeft = SizeVal % UnitSize;
7558 unsigned LoopSize = SizeVal - BytesLeft;
7559
7560 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7561 // Use LDR and STR to copy.
7562 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7563 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7564 unsigned srcIn = src;
7565 unsigned destIn = dest;
7566 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007567 unsigned srcOut = MRI.createVirtualRegister(TRC);
7568 unsigned destOut = MRI.createVirtualRegister(TRC);
7569 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007570 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7571 IsThumb1, IsThumb2);
7572 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7573 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007574 srcIn = srcOut;
7575 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007576 }
7577
7578 // Handle the leftover bytes with LDRB and STRB.
7579 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7580 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007581 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007582 unsigned srcOut = MRI.createVirtualRegister(TRC);
7583 unsigned destOut = MRI.createVirtualRegister(TRC);
7584 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007585 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7586 IsThumb1, IsThumb2);
7587 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7588 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007589 srcIn = srcOut;
7590 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007591 }
7592 MI->eraseFromParent(); // The instruction is gone now.
7593 return BB;
7594 }
7595
7596 // Expand the pseudo op to a loop.
7597 // thisMBB:
7598 // ...
7599 // movw varEnd, # --> with thumb2
7600 // movt varEnd, #
7601 // ldrcp varEnd, idx --> without thumb2
7602 // fallthrough --> loopMBB
7603 // loopMBB:
7604 // PHI varPhi, varEnd, varLoop
7605 // PHI srcPhi, src, srcLoop
7606 // PHI destPhi, dst, destLoop
7607 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7608 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7609 // subs varLoop, varPhi, #UnitSize
7610 // bne loopMBB
7611 // fallthrough --> exitMBB
7612 // exitMBB:
7613 // epilogue to handle left-over bytes
7614 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7615 // [destOut] = STRB_POST(scratch, destLoop, 1)
7616 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7617 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7618 MF->insert(It, loopMBB);
7619 MF->insert(It, exitMBB);
7620
7621 // Transfer the remainder of BB and its successor edges to exitMBB.
7622 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007623 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007624 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7625
7626 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007627 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007628 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007629 unsigned Vtmp = varEnd;
7630 if ((LoopSize & 0xFFFF0000) != 0)
7631 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007632 AddDefaultPred(BuildMI(BB, dl,
7633 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7634 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007635
7636 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007637 AddDefaultPred(BuildMI(BB, dl,
7638 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7639 varEnd)
7640 .addReg(Vtmp)
7641 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007642 } else {
7643 MachineConstantPool *ConstantPool = MF->getConstantPool();
7644 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7645 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7646
7647 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007648 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007649 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007650 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007651 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7652
7653 if (IsThumb1)
7654 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7655 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7656 else
7657 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7658 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7659 }
Manman Rene8735522012-06-01 19:33:18 +00007660 BB->addSuccessor(loopMBB);
7661
7662 // Generate the loop body:
7663 // varPhi = PHI(varLoop, varEnd)
7664 // srcPhi = PHI(srcLoop, src)
7665 // destPhi = PHI(destLoop, dst)
7666 MachineBasicBlock *entryBB = BB;
7667 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007668 unsigned varLoop = MRI.createVirtualRegister(TRC);
7669 unsigned varPhi = MRI.createVirtualRegister(TRC);
7670 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7671 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7672 unsigned destLoop = MRI.createVirtualRegister(TRC);
7673 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007674
7675 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7676 .addReg(varLoop).addMBB(loopMBB)
7677 .addReg(varEnd).addMBB(entryBB);
7678 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7679 .addReg(srcLoop).addMBB(loopMBB)
7680 .addReg(src).addMBB(entryBB);
7681 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7682 .addReg(destLoop).addMBB(loopMBB)
7683 .addReg(dest).addMBB(entryBB);
7684
7685 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7686 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007687 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007688 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7689 IsThumb1, IsThumb2);
7690 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7691 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007692
7693 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007694 if (IsThumb1) {
7695 MachineInstrBuilder MIB =
7696 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7697 MIB = AddDefaultT1CC(MIB);
7698 MIB.addReg(varPhi).addImm(UnitSize);
7699 AddDefaultPred(MIB);
7700 } else {
7701 MachineInstrBuilder MIB =
7702 BuildMI(*BB, BB->end(), dl,
7703 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7704 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7705 MIB->getOperand(5).setReg(ARM::CPSR);
7706 MIB->getOperand(5).setIsDef(true);
7707 }
7708 BuildMI(*BB, BB->end(), dl,
7709 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7710 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007711
7712 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7713 BB->addSuccessor(loopMBB);
7714 BB->addSuccessor(exitMBB);
7715
7716 // Add epilogue to handle BytesLeft.
7717 BB = exitMBB;
7718 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007719
7720 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7721 // [destOut] = STRB_POST(scratch, destLoop, 1)
7722 unsigned srcIn = srcLoop;
7723 unsigned destIn = destLoop;
7724 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007725 unsigned srcOut = MRI.createVirtualRegister(TRC);
7726 unsigned destOut = MRI.createVirtualRegister(TRC);
7727 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007728 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7729 IsThumb1, IsThumb2);
7730 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7731 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007732 srcIn = srcOut;
7733 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007734 }
7735
7736 MI->eraseFromParent(); // The instruction is gone now.
7737 return BB;
7738}
7739
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007740MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007741ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7742 MachineBasicBlock *MBB) const {
7743 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007744 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007745 DebugLoc DL = MI->getDebugLoc();
7746
7747 assert(Subtarget->isTargetWindows() &&
7748 "__chkstk is only supported on Windows");
7749 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7750
7751 // __chkstk takes the number of words to allocate on the stack in R4, and
7752 // returns the stack adjustment in number of bytes in R4. This will not
7753 // clober any other registers (other than the obvious lr).
7754 //
7755 // Although, technically, IP should be considered a register which may be
7756 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7757 // thumb-2 environment, so there is no interworking required. As a result, we
7758 // do not expect a veneer to be emitted by the linker, clobbering IP.
7759 //
Alp Toker1d099d92014-06-19 19:41:26 +00007760 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007761 // required, again, ensuring that IP is not clobbered.
7762 //
7763 // Finally, although some linkers may theoretically provide a trampoline for
7764 // out of range calls (which is quite common due to a 32M range limitation of
7765 // branches for Thumb), we can generate the long-call version via
7766 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7767 // IP.
7768
7769 switch (TM.getCodeModel()) {
7770 case CodeModel::Small:
7771 case CodeModel::Medium:
7772 case CodeModel::Default:
7773 case CodeModel::Kernel:
7774 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7775 .addImm((unsigned)ARMCC::AL).addReg(0)
7776 .addExternalSymbol("__chkstk")
7777 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7778 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7779 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7780 break;
7781 case CodeModel::Large:
7782 case CodeModel::JITDefault: {
7783 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7784 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7785
7786 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7787 .addExternalSymbol("__chkstk");
7788 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7789 .addImm((unsigned)ARMCC::AL).addReg(0)
7790 .addReg(Reg, RegState::Kill)
7791 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7792 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7793 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7794 break;
7795 }
7796 }
7797
7798 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7799 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007800 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007801
7802 MI->eraseFromParent();
7803 return MBB;
7804}
7805
7806MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007807ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
7808 MachineBasicBlock *MBB) const {
7809 DebugLoc DL = MI->getDebugLoc();
7810 MachineFunction *MF = MBB->getParent();
7811 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
7812
7813 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
7814 MF->push_back(ContBB);
7815 ContBB->splice(ContBB->begin(), MBB,
7816 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
7817 MBB->addSuccessor(ContBB);
7818
7819 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
7820 MF->push_back(TrapBB);
7821 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
7822 MBB->addSuccessor(TrapBB);
7823
7824 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
7825 .addReg(MI->getOperand(0).getReg())
7826 .addMBB(TrapBB);
7827
7828 MI->eraseFromParent();
7829 return ContBB;
7830}
7831
7832MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007833ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007834 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007835 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007836 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007837 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007838 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007839 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007840 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007841 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007842 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007843 // The Thumb2 pre-indexed stores have the same MI operands, they just
7844 // define them differently in the .td files from the isel patterns, so
7845 // they need pseudos.
7846 case ARM::t2STR_preidx:
7847 MI->setDesc(TII->get(ARM::t2STR_PRE));
7848 return BB;
7849 case ARM::t2STRB_preidx:
7850 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7851 return BB;
7852 case ARM::t2STRH_preidx:
7853 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7854 return BB;
7855
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007856 case ARM::STRi_preidx:
7857 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007858 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007859 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7860 // Decode the offset.
7861 unsigned Offset = MI->getOperand(4).getImm();
7862 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7863 Offset = ARM_AM::getAM2Offset(Offset);
7864 if (isSub)
7865 Offset = -Offset;
7866
Jim Grosbachf402f692011-08-12 21:02:34 +00007867 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007868 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007869 .addOperand(MI->getOperand(0)) // Rn_wb
7870 .addOperand(MI->getOperand(1)) // Rt
7871 .addOperand(MI->getOperand(2)) // Rn
7872 .addImm(Offset) // offset (skip GPR==zero_reg)
7873 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007874 .addOperand(MI->getOperand(6))
7875 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007876 MI->eraseFromParent();
7877 return BB;
7878 }
7879 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007880 case ARM::STRBr_preidx:
7881 case ARM::STRH_preidx: {
7882 unsigned NewOpc;
7883 switch (MI->getOpcode()) {
7884 default: llvm_unreachable("unexpected opcode!");
7885 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7886 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7887 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7888 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007889 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7890 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7891 MIB.addOperand(MI->getOperand(i));
7892 MI->eraseFromParent();
7893 return BB;
7894 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007895
Evan Chengbb2af352009-08-12 05:17:19 +00007896 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007897 // To "insert" a SELECT_CC instruction, we actually have to insert the
7898 // diamond control-flow pattern. The incoming instruction knows the
7899 // destination vreg to set, the condition code register to branch on, the
7900 // true/false values to select between, and a branch opcode to use.
7901 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007902 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00007903
7904 // thisMBB:
7905 // ...
7906 // TrueVal = ...
7907 // cmpTY ccX, r1, r2
7908 // bCC copy1MBB
7909 // fallthrough --> copy0MBB
7910 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007911 MachineFunction *F = BB->getParent();
7912 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7913 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007914 F->insert(It, copy0MBB);
7915 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007916
7917 // Transfer the remainder of BB and its successor edges to sinkMBB.
7918 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007919 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007920 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7921
Dan Gohmanf4f04102010-07-06 15:49:48 +00007922 BB->addSuccessor(copy0MBB);
7923 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007924
Dan Gohman34396292010-07-06 20:24:04 +00007925 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7926 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7927
Evan Cheng10043e22007-01-19 07:51:42 +00007928 // copy0MBB:
7929 // %FalseValue = ...
7930 // # fallthrough to sinkMBB
7931 BB = copy0MBB;
7932
7933 // Update machine-CFG edges
7934 BB->addSuccessor(sinkMBB);
7935
7936 // sinkMBB:
7937 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7938 // ...
7939 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007940 BuildMI(*BB, BB->begin(), dl,
7941 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007942 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7943 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7944
Dan Gohman34396292010-07-06 20:24:04 +00007945 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007946 return BB;
7947 }
Evan Chengb972e562009-08-07 00:34:42 +00007948
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007949 case ARM::BCCi64:
7950 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007951 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007952 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007953
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007954 // Compare both parts that make up the double comparison separately for
7955 // equality.
7956 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7957
7958 unsigned LHS1 = MI->getOperand(1).getReg();
7959 unsigned LHS2 = MI->getOperand(2).getReg();
7960 if (RHSisZero) {
7961 AddDefaultPred(BuildMI(BB, dl,
7962 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7963 .addReg(LHS1).addImm(0));
7964 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7965 .addReg(LHS2).addImm(0)
7966 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7967 } else {
7968 unsigned RHS1 = MI->getOperand(3).getReg();
7969 unsigned RHS2 = MI->getOperand(4).getReg();
7970 AddDefaultPred(BuildMI(BB, dl,
7971 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7972 .addReg(LHS1).addReg(RHS1));
7973 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7974 .addReg(LHS2).addReg(RHS2)
7975 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7976 }
7977
7978 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7979 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7980 if (MI->getOperand(0).getImm() == ARMCC::NE)
7981 std::swap(destMBB, exitMBB);
7982
7983 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7984 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007985 if (isThumb2)
7986 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7987 else
7988 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007989
7990 MI->eraseFromParent(); // The pseudo instruction is gone now.
7991 return BB;
7992 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007993
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007994 case ARM::Int_eh_sjlj_setjmp:
7995 case ARM::Int_eh_sjlj_setjmp_nofp:
7996 case ARM::tInt_eh_sjlj_setjmp:
7997 case ARM::t2Int_eh_sjlj_setjmp:
7998 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00007999 return BB;
8000
8001 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008002 EmitSjLjDispatchBlock(MI, BB);
8003 return BB;
8004
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008005 case ARM::ABS:
8006 case ARM::t2ABS: {
8007 // To insert an ABS instruction, we have to insert the
8008 // diamond control-flow pattern. The incoming instruction knows the
8009 // source vreg to test against 0, the destination vreg to set,
8010 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008011 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008012 // It transforms
8013 // V1 = ABS V0
8014 // into
8015 // V2 = MOVS V0
8016 // BCC (branch to SinkBB if V0 >= 0)
8017 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008018 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008019 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008020 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008021 MachineFunction *Fn = BB->getParent();
8022 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8023 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8024 Fn->insert(BBI, RSBBB);
8025 Fn->insert(BBI, SinkBB);
8026
8027 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8028 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008029 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008030 bool isThumb2 = Subtarget->isThumb2();
8031 MachineRegisterInfo &MRI = Fn->getRegInfo();
8032 // In Thumb mode S must not be specified if source register is the SP or
8033 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008034 unsigned NewRsbDstReg =
8035 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008036
8037 // Transfer the remainder of BB and its successor edges to sinkMBB.
8038 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008039 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008040 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8041
8042 BB->addSuccessor(RSBBB);
8043 BB->addSuccessor(SinkBB);
8044
8045 // fall through to SinkMBB
8046 RSBBB->addSuccessor(SinkBB);
8047
Manman Rene0763c72012-06-15 21:32:12 +00008048 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008049 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008050 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8051 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008052
8053 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008054 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008055 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8056 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8057
8058 // insert rsbri in RSBBB
8059 // Note: BCC and rsbri will be converted into predicated rsbmi
8060 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008061 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008062 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008063 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008064 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8065
Andrew Trick3f07c422011-10-18 18:40:53 +00008066 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008067 // reuse ABSDstReg to not change uses of ABS instruction
8068 BuildMI(*SinkBB, SinkBB->begin(), dl,
8069 TII->get(ARM::PHI), ABSDstReg)
8070 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008071 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008072
8073 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008074 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008075
8076 // return last added BB
8077 return SinkBB;
8078 }
Manman Rene8735522012-06-01 19:33:18 +00008079 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008080 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008081 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008082 case ARM::WIN__CHKSTK:
8083 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008084 case ARM::WIN__DBZCHK:
8085 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008086 }
8087}
8088
Scott Douglass953f9082015-10-05 14:49:54 +00008089/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8090/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8091/// instead of as a custom inserter because we need the use list from the SDNode.
8092static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8093 MachineInstr *MI, const SDNode *Node) {
8094 bool isThumb1 = Subtarget->isThumb1Only();
8095
8096 DebugLoc DL = MI->getDebugLoc();
8097 MachineFunction *MF = MI->getParent()->getParent();
8098 MachineRegisterInfo &MRI = MF->getRegInfo();
8099 MachineInstrBuilder MIB(*MF, MI);
8100
8101 // If the new dst/src is unused mark it as dead.
8102 if (!Node->hasAnyUseOfValue(0)) {
8103 MI->getOperand(0).setIsDead(true);
8104 }
8105 if (!Node->hasAnyUseOfValue(1)) {
8106 MI->getOperand(1).setIsDead(true);
8107 }
8108
8109 // The MEMCPY both defines and kills the scratch registers.
8110 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8111 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8112 : &ARM::GPRRegClass);
8113 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8114 }
8115}
8116
Evan Chenge6fba772011-08-30 19:09:48 +00008117void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8118 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008119 if (MI->getOpcode() == ARM::MEMCPY) {
8120 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8121 return;
8122 }
8123
Evan Cheng7f8e5632011-12-07 07:15:52 +00008124 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008125 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8126 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8127 // operand is still set to noreg. If needed, set the optional operand's
8128 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008129 //
Andrew Trick88b24502011-10-18 19:18:52 +00008130 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008131
Andrew Trick924123a2011-09-21 02:20:46 +00008132 // Rename pseudo opcodes.
8133 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8134 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008135 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008136 MCID = &TII->get(NewOpc);
8137
8138 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8139 "converted opcode should be the same except for cc_out");
8140
8141 MI->setDesc(*MCID);
8142
8143 // Add the optional cc_out operand
8144 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008145 }
Andrew Trick88b24502011-10-18 19:18:52 +00008146 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008147
8148 // Any ARM instruction that sets the 's' bit should specify an optional
8149 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008150 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008151 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008152 return;
8153 }
Andrew Trick924123a2011-09-21 02:20:46 +00008154 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8155 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008156 bool definesCPSR = false;
8157 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008158 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008159 i != e; ++i) {
8160 const MachineOperand &MO = MI->getOperand(i);
8161 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8162 definesCPSR = true;
8163 if (MO.isDead())
8164 deadCPSR = true;
8165 MI->RemoveOperand(i);
8166 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008167 }
8168 }
Andrew Trick8586e622011-09-20 03:17:40 +00008169 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008170 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008171 return;
8172 }
8173 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008174 if (deadCPSR) {
8175 assert(!MI->getOperand(ccOutIdx).getReg() &&
8176 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008177 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008178 }
Andrew Trick8586e622011-09-20 03:17:40 +00008179
Andrew Trick924123a2011-09-21 02:20:46 +00008180 // If this instruction was defined with an optional CPSR def and its dag node
8181 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008182 MachineOperand &MO = MI->getOperand(ccOutIdx);
8183 MO.setReg(ARM::CPSR);
8184 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008185}
8186
Evan Cheng10043e22007-01-19 07:51:42 +00008187//===----------------------------------------------------------------------===//
8188// ARM Optimization Hooks
8189//===----------------------------------------------------------------------===//
8190
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008191// Helper function that checks if N is a null or all ones constant.
8192static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008193 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008194}
8195
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008196// Return true if N is conditionally 0 or all ones.
8197// Detects these expressions where cc is an i1 value:
8198//
8199// (select cc 0, y) [AllOnes=0]
8200// (select cc y, 0) [AllOnes=0]
8201// (zext cc) [AllOnes=0]
8202// (sext cc) [AllOnes=0/1]
8203// (select cc -1, y) [AllOnes=1]
8204// (select cc y, -1) [AllOnes=1]
8205//
8206// Invert is set when N is the null/all ones constant when CC is false.
8207// OtherOp is set to the alternative value of N.
8208static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8209 SDValue &CC, bool &Invert,
8210 SDValue &OtherOp,
8211 SelectionDAG &DAG) {
8212 switch (N->getOpcode()) {
8213 default: return false;
8214 case ISD::SELECT: {
8215 CC = N->getOperand(0);
8216 SDValue N1 = N->getOperand(1);
8217 SDValue N2 = N->getOperand(2);
8218 if (isZeroOrAllOnes(N1, AllOnes)) {
8219 Invert = false;
8220 OtherOp = N2;
8221 return true;
8222 }
8223 if (isZeroOrAllOnes(N2, AllOnes)) {
8224 Invert = true;
8225 OtherOp = N1;
8226 return true;
8227 }
8228 return false;
8229 }
8230 case ISD::ZERO_EXTEND:
8231 // (zext cc) can never be the all ones value.
8232 if (AllOnes)
8233 return false;
8234 // Fall through.
8235 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008236 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008237 EVT VT = N->getValueType(0);
8238 CC = N->getOperand(0);
8239 if (CC.getValueType() != MVT::i1)
8240 return false;
8241 Invert = !AllOnes;
8242 if (AllOnes)
8243 // When looking for an AllOnes constant, N is an sext, and the 'other'
8244 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008245 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008246 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8247 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008248 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008249 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008250 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8251 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008252 return true;
8253 }
8254 }
8255}
8256
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008257// Combine a constant select operand into its use:
8258//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008259// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8260// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8261// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8262// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8263// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008264//
8265// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008266// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008267//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008268// Also recognize sext/zext from i1:
8269//
8270// (add (zext cc), x) -> (select cc (add x, 1), x)
8271// (add (sext cc), x) -> (select cc (add x, -1), x)
8272//
8273// These transformations eventually create predicated instructions.
8274//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008275// @param N The node to transform.
8276// @param Slct The N operand that is a select.
8277// @param OtherOp The other N operand (x above).
8278// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008279// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008280// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008281static
8282SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008283 TargetLowering::DAGCombinerInfo &DCI,
8284 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008285 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008286 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008287 SDValue NonConstantVal;
8288 SDValue CCOp;
8289 bool SwapSelectOps;
8290 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8291 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008292 return SDValue();
8293
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008294 // Slct is now know to be the desired identity constant when CC is true.
8295 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008296 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008297 OtherOp, NonConstantVal);
8298 // Unless SwapSelectOps says CC should be false.
8299 if (SwapSelectOps)
8300 std::swap(TrueVal, FalseVal);
8301
Andrew Trickef9de2a2013-05-25 02:42:55 +00008302 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008303 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008304}
8305
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008306// Attempt combineSelectAndUse on each operand of a commutative operator N.
8307static
8308SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8309 TargetLowering::DAGCombinerInfo &DCI) {
8310 SDValue N0 = N->getOperand(0);
8311 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008312 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008313 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8314 if (Result.getNode())
8315 return Result;
8316 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008317 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008318 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8319 if (Result.getNode())
8320 return Result;
8321 }
8322 return SDValue();
8323}
8324
Eric Christopher1b8b94192011-06-29 21:10:36 +00008325// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008326// (only after legalization).
8327static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8328 TargetLowering::DAGCombinerInfo &DCI,
8329 const ARMSubtarget *Subtarget) {
8330
8331 // Only perform optimization if after legalize, and if NEON is available. We
8332 // also expected both operands to be BUILD_VECTORs.
8333 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8334 || N0.getOpcode() != ISD::BUILD_VECTOR
8335 || N1.getOpcode() != ISD::BUILD_VECTOR)
8336 return SDValue();
8337
8338 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8339 EVT VT = N->getValueType(0);
8340 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8341 return SDValue();
8342
8343 // Check that the vector operands are of the right form.
8344 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8345 // operands, where N is the size of the formed vector.
8346 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8347 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008348
8349 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008350 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008351 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008352 SDValue Vec = N0->getOperand(0)->getOperand(0);
8353 SDNode *V = Vec.getNode();
8354 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008355
Eric Christopher1b8b94192011-06-29 21:10:36 +00008356 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008357 // check to see if each of their operands are an EXTRACT_VECTOR with
8358 // the same vector and appropriate index.
8359 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8360 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8361 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008362
Tanya Lattnere9e67052011-06-14 23:48:48 +00008363 SDValue ExtVec0 = N0->getOperand(i);
8364 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008365
Tanya Lattnere9e67052011-06-14 23:48:48 +00008366 // First operand is the vector, verify its the same.
8367 if (V != ExtVec0->getOperand(0).getNode() ||
8368 V != ExtVec1->getOperand(0).getNode())
8369 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008370
Tanya Lattnere9e67052011-06-14 23:48:48 +00008371 // Second is the constant, verify its correct.
8372 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8373 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008374
Tanya Lattnere9e67052011-06-14 23:48:48 +00008375 // For the constant, we want to see all the even or all the odd.
8376 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8377 || C1->getZExtValue() != nextIndex+1)
8378 return SDValue();
8379
8380 // Increment index.
8381 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008382 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008383 return SDValue();
8384 }
8385
8386 // Create VPADDL node.
8387 SelectionDAG &DAG = DCI.DAG;
8388 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008389
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008390 SDLoc dl(N);
8391
Tanya Lattnere9e67052011-06-14 23:48:48 +00008392 // Build operand list.
8393 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008394 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008395 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008396
8397 // Input is the vector.
8398 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008399
Tanya Lattnere9e67052011-06-14 23:48:48 +00008400 // Get widened type and narrowed type.
8401 MVT widenType;
8402 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008403
Silviu Barangaa3106e62014-04-03 10:44:27 +00008404 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8405 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008406 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8407 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8408 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8409 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008410 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008411 }
8412
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008413 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008414 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008415 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008416}
8417
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008418static SDValue findMUL_LOHI(SDValue V) {
8419 if (V->getOpcode() == ISD::UMUL_LOHI ||
8420 V->getOpcode() == ISD::SMUL_LOHI)
8421 return V;
8422 return SDValue();
8423}
8424
8425static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8426 TargetLowering::DAGCombinerInfo &DCI,
8427 const ARMSubtarget *Subtarget) {
8428
8429 if (Subtarget->isThumb1Only()) return SDValue();
8430
8431 // Only perform the checks after legalize when the pattern is available.
8432 if (DCI.isBeforeLegalize()) return SDValue();
8433
8434 // Look for multiply add opportunities.
8435 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8436 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8437 // a glue link from the first add to the second add.
8438 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8439 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008440 // UMUL_LOHI
8441 // / :lo \ :hi
8442 // / \ [no multiline comment]
8443 // loAdd -> ADDE |
8444 // \ :glue /
8445 // \ /
8446 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008447 //
8448 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8449 SDValue AddcOp0 = AddcNode->getOperand(0);
8450 SDValue AddcOp1 = AddcNode->getOperand(1);
8451
8452 // Check if the two operands are from the same mul_lohi node.
8453 if (AddcOp0.getNode() == AddcOp1.getNode())
8454 return SDValue();
8455
8456 assert(AddcNode->getNumValues() == 2 &&
8457 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008458 "Expect ADDC with two result values. First: i32");
8459
8460 // Check that we have a glued ADDC node.
8461 if (AddcNode->getValueType(1) != MVT::Glue)
8462 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008463
8464 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8465 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8466 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8467 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8468 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8469 return SDValue();
8470
8471 // Look for the glued ADDE.
8472 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008473 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008474 return SDValue();
8475
8476 // Make sure it is really an ADDE.
8477 if (AddeNode->getOpcode() != ISD::ADDE)
8478 return SDValue();
8479
8480 assert(AddeNode->getNumOperands() == 3 &&
8481 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8482 "ADDE node has the wrong inputs");
8483
8484 // Check for the triangle shape.
8485 SDValue AddeOp0 = AddeNode->getOperand(0);
8486 SDValue AddeOp1 = AddeNode->getOperand(1);
8487
8488 // Make sure that the ADDE operands are not coming from the same node.
8489 if (AddeOp0.getNode() == AddeOp1.getNode())
8490 return SDValue();
8491
8492 // Find the MUL_LOHI node walking up ADDE's operands.
8493 bool IsLeftOperandMUL = false;
8494 SDValue MULOp = findMUL_LOHI(AddeOp0);
8495 if (MULOp == SDValue())
8496 MULOp = findMUL_LOHI(AddeOp1);
8497 else
8498 IsLeftOperandMUL = true;
8499 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008500 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008501
8502 // Figure out the right opcode.
8503 unsigned Opc = MULOp->getOpcode();
8504 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8505
8506 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008507 SDValue* HiAdd = nullptr;
8508 SDValue* LoMul = nullptr;
8509 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008510
Jyoti Allurf1d70502015-01-23 09:10:03 +00008511 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8512 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8513 return SDValue();
8514
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008515 if (IsLeftOperandMUL)
8516 HiAdd = &AddeOp1;
8517 else
8518 HiAdd = &AddeOp0;
8519
8520
Jyoti Allurf1d70502015-01-23 09:10:03 +00008521 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8522 // whose low result is fed to the ADDC we are checking.
8523
8524 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008525 LoMul = &AddcOp0;
8526 LowAdd = &AddcOp1;
8527 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008528 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008529 LoMul = &AddcOp1;
8530 LowAdd = &AddcOp0;
8531 }
8532
Craig Topper062a2ba2014-04-25 05:30:21 +00008533 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008534 return SDValue();
8535
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008536 // Create the merged node.
8537 SelectionDAG &DAG = DCI.DAG;
8538
8539 // Build operand list.
8540 SmallVector<SDValue, 8> Ops;
8541 Ops.push_back(LoMul->getOperand(0));
8542 Ops.push_back(LoMul->getOperand(1));
8543 Ops.push_back(*LowAdd);
8544 Ops.push_back(*HiAdd);
8545
Andrew Trickef9de2a2013-05-25 02:42:55 +00008546 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008547 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008548
8549 // Replace the ADDs' nodes uses by the MLA node's values.
8550 SDValue HiMLALResult(MLALNode.getNode(), 1);
8551 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8552
8553 SDValue LoMLALResult(MLALNode.getNode(), 0);
8554 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8555
8556 // Return original node to notify the driver to stop replacing.
8557 SDValue resNode(AddcNode, 0);
8558 return resNode;
8559}
8560
8561/// PerformADDCCombine - Target-specific dag combine transform from
8562/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8563static SDValue PerformADDCCombine(SDNode *N,
8564 TargetLowering::DAGCombinerInfo &DCI,
8565 const ARMSubtarget *Subtarget) {
8566
8567 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8568
8569}
8570
Bob Wilson728eb292010-07-29 20:34:14 +00008571/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8572/// operands N0 and N1. This is a helper for PerformADDCombine that is
8573/// called with the default operands, and if that fails, with commuted
8574/// operands.
8575static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008576 TargetLowering::DAGCombinerInfo &DCI,
8577 const ARMSubtarget *Subtarget){
8578
8579 // Attempt to create vpaddl for this add.
8580 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8581 if (Result.getNode())
8582 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008583
Chris Lattner4147f082009-03-12 06:52:53 +00008584 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008585 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008586 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8587 if (Result.getNode()) return Result;
8588 }
Chris Lattner4147f082009-03-12 06:52:53 +00008589 return SDValue();
8590}
8591
Bob Wilson728eb292010-07-29 20:34:14 +00008592/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8593///
8594static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008595 TargetLowering::DAGCombinerInfo &DCI,
8596 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008597 SDValue N0 = N->getOperand(0);
8598 SDValue N1 = N->getOperand(1);
8599
8600 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008601 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008602 if (Result.getNode())
8603 return Result;
8604
8605 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008606 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008607}
8608
Chris Lattner4147f082009-03-12 06:52:53 +00008609/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008610///
Chris Lattner4147f082009-03-12 06:52:53 +00008611static SDValue PerformSUBCombine(SDNode *N,
8612 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008613 SDValue N0 = N->getOperand(0);
8614 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008615
Chris Lattner4147f082009-03-12 06:52:53 +00008616 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008617 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008618 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8619 if (Result.getNode()) return Result;
8620 }
Bob Wilson7117a912009-03-20 22:42:55 +00008621
Chris Lattner4147f082009-03-12 06:52:53 +00008622 return SDValue();
8623}
8624
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008625/// PerformVMULCombine
8626/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8627/// special multiplier accumulator forwarding.
8628/// vmul d3, d0, d2
8629/// vmla d3, d1, d2
8630/// is faster than
8631/// vadd d3, d0, d1
8632/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008633// However, for (A + B) * (A + B),
8634// vadd d2, d0, d1
8635// vmul d3, d0, d2
8636// vmla d3, d1, d2
8637// is slower than
8638// vadd d2, d0, d1
8639// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008640static SDValue PerformVMULCombine(SDNode *N,
8641 TargetLowering::DAGCombinerInfo &DCI,
8642 const ARMSubtarget *Subtarget) {
8643 if (!Subtarget->hasVMLxForwarding())
8644 return SDValue();
8645
8646 SelectionDAG &DAG = DCI.DAG;
8647 SDValue N0 = N->getOperand(0);
8648 SDValue N1 = N->getOperand(1);
8649 unsigned Opcode = N0.getOpcode();
8650 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8651 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008652 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008653 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8654 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8655 return SDValue();
8656 std::swap(N0, N1);
8657 }
8658
Weiming Zhao2052f482013-09-25 23:12:06 +00008659 if (N0 == N1)
8660 return SDValue();
8661
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008662 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008663 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008664 SDValue N00 = N0->getOperand(0);
8665 SDValue N01 = N0->getOperand(1);
8666 return DAG.getNode(Opcode, DL, VT,
8667 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8668 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8669}
8670
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008671static SDValue PerformMULCombine(SDNode *N,
8672 TargetLowering::DAGCombinerInfo &DCI,
8673 const ARMSubtarget *Subtarget) {
8674 SelectionDAG &DAG = DCI.DAG;
8675
8676 if (Subtarget->isThumb1Only())
8677 return SDValue();
8678
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008679 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8680 return SDValue();
8681
8682 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008683 if (VT.is64BitVector() || VT.is128BitVector())
8684 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008685 if (VT != MVT::i32)
8686 return SDValue();
8687
8688 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8689 if (!C)
8690 return SDValue();
8691
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008692 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008693 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008694
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008695 ShiftAmt = ShiftAmt & (32 - 1);
8696 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008697 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008698
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008699 SDValue Res;
8700 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008701
8702 if (MulAmt >= 0) {
8703 if (isPowerOf2_32(MulAmt - 1)) {
8704 // (mul x, 2^N + 1) => (add (shl x, N), x)
8705 Res = DAG.getNode(ISD::ADD, DL, VT,
8706 V,
8707 DAG.getNode(ISD::SHL, DL, VT,
8708 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008709 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008710 MVT::i32)));
8711 } else if (isPowerOf2_32(MulAmt + 1)) {
8712 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8713 Res = DAG.getNode(ISD::SUB, DL, VT,
8714 DAG.getNode(ISD::SHL, DL, VT,
8715 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008716 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008717 MVT::i32)),
8718 V);
8719 } else
8720 return SDValue();
8721 } else {
8722 uint64_t MulAmtAbs = -MulAmt;
8723 if (isPowerOf2_32(MulAmtAbs + 1)) {
8724 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8725 Res = DAG.getNode(ISD::SUB, DL, VT,
8726 V,
8727 DAG.getNode(ISD::SHL, DL, VT,
8728 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008729 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008730 MVT::i32)));
8731 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8732 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8733 Res = DAG.getNode(ISD::ADD, DL, VT,
8734 V,
8735 DAG.getNode(ISD::SHL, DL, VT,
8736 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008737 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008738 MVT::i32)));
8739 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008740 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008741
8742 } else
8743 return SDValue();
8744 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008745
8746 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008747 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008748 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008749
8750 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008751 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008752 return SDValue();
8753}
8754
Owen Anderson30c48922010-11-05 19:27:46 +00008755static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008756 TargetLowering::DAGCombinerInfo &DCI,
8757 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008758
Owen Anderson30c48922010-11-05 19:27:46 +00008759 // Attempt to use immediate-form VBIC
8760 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008761 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008762 EVT VT = N->getValueType(0);
8763 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008764
Tanya Lattner266792a2011-04-07 15:24:20 +00008765 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8766 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008767
Owen Anderson30c48922010-11-05 19:27:46 +00008768 APInt SplatBits, SplatUndef;
8769 unsigned SplatBitSize;
8770 bool HasAnyUndefs;
8771 if (BVN &&
8772 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8773 if (SplatBitSize <= 64) {
8774 EVT VbicVT;
8775 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8776 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008777 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008778 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008779 if (Val.getNode()) {
8780 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008781 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008782 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008783 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008784 }
8785 }
8786 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008787
Evan Chenge87681c2012-02-23 01:19:06 +00008788 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008789 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8790 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8791 if (Result.getNode())
8792 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008793 }
8794
Owen Anderson30c48922010-11-05 19:27:46 +00008795 return SDValue();
8796}
8797
Jim Grosbach11013ed2010-07-16 23:05:05 +00008798/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8799static SDValue PerformORCombine(SDNode *N,
8800 TargetLowering::DAGCombinerInfo &DCI,
8801 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008802 // Attempt to use immediate-form VORR
8803 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008804 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008805 EVT VT = N->getValueType(0);
8806 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008807
Tanya Lattner266792a2011-04-07 15:24:20 +00008808 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8809 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008810
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008811 APInt SplatBits, SplatUndef;
8812 unsigned SplatBitSize;
8813 bool HasAnyUndefs;
8814 if (BVN && Subtarget->hasNEON() &&
8815 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8816 if (SplatBitSize <= 64) {
8817 EVT VorrVT;
8818 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8819 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008820 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008821 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008822 if (Val.getNode()) {
8823 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008824 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008825 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008826 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008827 }
8828 }
8829 }
8830
Evan Chenge87681c2012-02-23 01:19:06 +00008831 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008832 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8833 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8834 if (Result.getNode())
8835 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008836 }
8837
Nadav Rotem3a94c542012-08-13 18:52:44 +00008838 // The code below optimizes (or (and X, Y), Z).
8839 // The AND operand needs to have a single user to make these optimizations
8840 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008841 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008842 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008843 return SDValue();
8844 SDValue N1 = N->getOperand(1);
8845
8846 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8847 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8848 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8849 APInt SplatUndef;
8850 unsigned SplatBitSize;
8851 bool HasAnyUndefs;
8852
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008853 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008854 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008855 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8856 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008857 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008858 HasAnyUndefs) && !HasAnyUndefs) {
8859 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8860 HasAnyUndefs) && !HasAnyUndefs) {
8861 // Ensure that the bit width of the constants are the same and that
8862 // the splat arguments are logical inverses as per the pattern we
8863 // are trying to simplify.
8864 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8865 SplatBits0 == ~SplatBits1) {
8866 // Canonicalize the vector type to make instruction selection
8867 // simpler.
8868 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8869 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8870 N0->getOperand(1),
8871 N0->getOperand(0),
8872 N1->getOperand(0));
8873 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8874 }
8875 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008876 }
8877 }
8878
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008879 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8880 // reasonable.
8881
Jim Grosbach11013ed2010-07-16 23:05:05 +00008882 // BFI is only available on V6T2+
8883 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8884 return SDValue();
8885
Andrew Trickef9de2a2013-05-25 02:42:55 +00008886 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008887 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008888 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008889 //
8890 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008891 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008892 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008893 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008894 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008895 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008896
Jim Grosbach11013ed2010-07-16 23:05:05 +00008897 if (VT != MVT::i32)
8898 return SDValue();
8899
Evan Cheng2e51bb42010-12-13 20:32:54 +00008900 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008901
Jim Grosbach11013ed2010-07-16 23:05:05 +00008902 // The value and the mask need to be constants so we can verify this is
8903 // actually a bitfield set. If the mask is 0xffff, we can do better
8904 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008905 SDValue MaskOp = N0.getOperand(1);
8906 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8907 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008908 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008909 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008910 if (Mask == 0xffff)
8911 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008912 SDValue Res;
8913 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008914 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8915 if (N1C) {
8916 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008917 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008918 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008919
Evan Cheng34345752010-12-11 04:11:38 +00008920 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008921 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008922
Evan Cheng2e51bb42010-12-13 20:32:54 +00008923 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008924 DAG.getConstant(Val, DL, MVT::i32),
8925 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008926
8927 // Do not add new nodes to DAG combiner worklist.
8928 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008929 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008930 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008931 } else if (N1.getOpcode() == ISD::AND) {
8932 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008933 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8934 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008935 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008936 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008937
Eric Christopherd5530962011-03-26 01:21:03 +00008938 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8939 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008940 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008941 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008942 // The pack halfword instruction works better for masks that fit it,
8943 // so use that when it's available.
8944 if (Subtarget->hasT2ExtractPack() &&
8945 (Mask == 0xffff || Mask == 0xffff0000))
8946 return SDValue();
8947 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008948 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008949 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008950 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008951 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008952 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008953 // Do not add new nodes to DAG combiner worklist.
8954 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008955 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008956 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008957 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008958 // The pack halfword instruction works better for masks that fit it,
8959 // so use that when it's available.
8960 if (Subtarget->hasT2ExtractPack() &&
8961 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8962 return SDValue();
8963 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008964 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008965 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008966 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008967 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008968 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008969 // Do not add new nodes to DAG combiner worklist.
8970 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008971 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008972 }
8973 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008974
Evan Cheng2e51bb42010-12-13 20:32:54 +00008975 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8976 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8977 ARM::isBitFieldInvertedMask(~Mask)) {
8978 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8979 // where lsb(mask) == #shamt and masked bits of B are known zero.
8980 SDValue ShAmt = N00.getOperand(1);
8981 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008982 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008983 if (ShAmtC != LSB)
8984 return SDValue();
8985
8986 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008987 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008988
8989 // Do not add new nodes to DAG combiner worklist.
8990 DCI.CombineTo(N, Res, false);
8991 }
8992
Jim Grosbach11013ed2010-07-16 23:05:05 +00008993 return SDValue();
8994}
8995
Evan Chenge87681c2012-02-23 01:19:06 +00008996static SDValue PerformXORCombine(SDNode *N,
8997 TargetLowering::DAGCombinerInfo &DCI,
8998 const ARMSubtarget *Subtarget) {
8999 EVT VT = N->getValueType(0);
9000 SelectionDAG &DAG = DCI.DAG;
9001
9002 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9003 return SDValue();
9004
9005 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009006 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
9007 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
9008 if (Result.getNode())
9009 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009010 }
9011
9012 return SDValue();
9013}
9014
James Molloyce12c922015-11-11 15:40:40 +00009015// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9016// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9017// their position in "to" (Rd).
9018static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9019 assert(N->getOpcode() == ARMISD::BFI);
9020
9021 SDValue From = N->getOperand(1);
9022 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9023 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9024
9025 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9026 // #C in the base of the SHR.
9027 if (From->getOpcode() == ISD::SRL &&
9028 isa<ConstantSDNode>(From->getOperand(1))) {
9029 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9030 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9031 FromMask <<= Shift.getLimitedValue(31);
9032 From = From->getOperand(0);
9033 }
9034
9035 return From;
9036}
9037
9038// If A and B contain one contiguous set of bits, does A | B == A . B?
9039//
9040// Neither A nor B must be zero.
9041static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9042 unsigned LastActiveBitInA = A.countTrailingZeros();
9043 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9044 return LastActiveBitInA - 1 == FirstActiveBitInB;
9045}
9046
9047static SDValue FindBFIToCombineWith(SDNode *N) {
9048 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9049 // if one exists.
9050 APInt ToMask, FromMask;
9051 SDValue From = ParseBFI(N, ToMask, FromMask);
9052 SDValue To = N->getOperand(0);
9053
9054 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9055 // aren't compatible, but not if they set the same bit in their destination as
9056 // we do (or that of any BFI we're going to combine with).
9057 SDValue V = To;
9058 APInt CombinedToMask = ToMask;
9059 while (V.getOpcode() == ARMISD::BFI) {
9060 APInt NewToMask, NewFromMask;
9061 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9062 if (NewFrom != From) {
9063 // This BFI has a different base. Keep going.
9064 CombinedToMask |= NewToMask;
9065 V = V.getOperand(0);
9066 continue;
9067 }
9068
9069 // Do the written bits conflict with any we've seen so far?
9070 if ((NewToMask & CombinedToMask).getBoolValue())
9071 // Conflicting bits - bail out because going further is unsafe.
9072 return SDValue();
9073
9074 // Are the new bits contiguous when combined with the old bits?
9075 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9076 BitsProperlyConcatenate(FromMask, NewFromMask))
9077 return V;
9078 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9079 BitsProperlyConcatenate(NewFromMask, FromMask))
9080 return V;
9081
9082 // We've seen a write to some bits, so track it.
9083 CombinedToMask |= NewToMask;
9084 // Keep going...
9085 V = V.getOperand(0);
9086 }
9087
9088 return SDValue();
9089}
9090
Evan Chengc1778132010-12-14 03:22:07 +00009091static SDValue PerformBFICombine(SDNode *N,
9092 TargetLowering::DAGCombinerInfo &DCI) {
9093 SDValue N1 = N->getOperand(1);
9094 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009095 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9096 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009097 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9098 if (!N11C)
9099 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009100 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009101 unsigned LSB = countTrailingZeros(~InvMask);
9102 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009103 assert(Width <
9104 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009105 "undefined behavior");
9106 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009107 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009108 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009109 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009110 N->getOperand(0), N1.getOperand(0),
9111 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009112 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9113 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9114 // Keep track of any consecutive bits set that all come from the same base
9115 // value. We can combine these together into a single BFI.
9116 SDValue CombineBFI = FindBFIToCombineWith(N);
9117 if (CombineBFI == SDValue())
9118 return SDValue();
9119
9120 // We've found a BFI.
9121 APInt ToMask1, FromMask1;
9122 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9123
9124 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009125 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9126 assert(From1 == From2);
9127 (void)From2;
James Molloyce12c922015-11-11 15:40:40 +00009128
9129 // First, unlink CombineBFI.
9130 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9131 // Then create a new BFI, combining the two together.
9132 APInt NewFromMask = FromMask1 | FromMask2;
9133 APInt NewToMask = ToMask1 | ToMask2;
9134
9135 EVT VT = N->getValueType(0);
9136 SDLoc dl(N);
9137
9138 if (NewFromMask[0] == 0)
9139 From1 = DCI.DAG.getNode(
9140 ISD::SRL, dl, VT, From1,
9141 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9142 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9143 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009144 }
9145 return SDValue();
9146}
9147
Bob Wilson22806742010-09-22 22:09:21 +00009148/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9149/// ARMISD::VMOVRRD.
9150static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009151 TargetLowering::DAGCombinerInfo &DCI,
9152 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009153 // vmovrrd(vmovdrr x, y) -> x,y
9154 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009155 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009156 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009157
9158 // vmovrrd(load f64) -> (load i32), (load i32)
9159 SDNode *InNode = InDouble.getNode();
9160 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9161 InNode->getValueType(0) == MVT::f64 &&
9162 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9163 !cast<LoadSDNode>(InNode)->isVolatile()) {
9164 // TODO: Should this be done for non-FrameIndex operands?
9165 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9166
9167 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009168 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009169 SDValue BasePtr = LD->getBasePtr();
9170 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9171 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009172 LD->isNonTemporal(), LD->isInvariant(),
9173 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009174
9175 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009176 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009177 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9178 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009179 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009180 std::min(4U, LD->getAlignment() / 2));
9181
9182 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009183 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009184 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009185 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009186 return Result;
9187 }
9188
Bob Wilson22806742010-09-22 22:09:21 +00009189 return SDValue();
9190}
9191
9192/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9193/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9194static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9195 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9196 SDValue Op0 = N->getOperand(0);
9197 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009198 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009199 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009200 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009201 Op1 = Op1.getOperand(0);
9202 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9203 Op0.getNode() == Op1.getNode() &&
9204 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009205 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009206 N->getValueType(0), Op0.getOperand(0));
9207 return SDValue();
9208}
9209
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009210/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9211/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9212/// i64 vector to have f64 elements, since the value can then be loaded
9213/// directly into a VFP register.
9214static bool hasNormalLoadOperand(SDNode *N) {
9215 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9216 for (unsigned i = 0; i < NumElts; ++i) {
9217 SDNode *Elt = N->getOperand(i).getNode();
9218 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9219 return true;
9220 }
9221 return false;
9222}
9223
Bob Wilsoncb6db982010-09-17 22:59:05 +00009224/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9225/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009226static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009227 TargetLowering::DAGCombinerInfo &DCI,
9228 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009229 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9230 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9231 // into a pair of GPRs, which is fine when the value is used as a scalar,
9232 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009233 SelectionDAG &DAG = DCI.DAG;
9234 if (N->getNumOperands() == 2) {
9235 SDValue RV = PerformVMOVDRRCombine(N, DAG);
9236 if (RV.getNode())
9237 return RV;
9238 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00009239
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009240 // Load i64 elements as f64 values so that type legalization does not split
9241 // them up into i32 values.
9242 EVT VT = N->getValueType(0);
9243 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9244 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009245 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009246 SmallVector<SDValue, 8> Ops;
9247 unsigned NumElts = VT.getVectorNumElements();
9248 for (unsigned i = 0; i < NumElts; ++i) {
9249 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9250 Ops.push_back(V);
9251 // Make the DAGCombiner fold the bitcast.
9252 DCI.AddToWorklist(V.getNode());
9253 }
9254 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009255 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009256 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9257}
9258
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009259/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9260static SDValue
9261PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9262 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9263 // At that time, we may have inserted bitcasts from integer to float.
9264 // If these bitcasts have survived DAGCombine, change the lowering of this
9265 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9266 // force to use floating point types.
9267
9268 // Make sure we can change the type of the vector.
9269 // This is possible iff:
9270 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9271 // 1.1. Vector is used only once.
9272 // 1.2. Use is a bit convert to an integer type.
9273 // 2. The size of its operands are 32-bits (64-bits are not legal).
9274 EVT VT = N->getValueType(0);
9275 EVT EltVT = VT.getVectorElementType();
9276
9277 // Check 1.1. and 2.
9278 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9279 return SDValue();
9280
9281 // By construction, the input type must be float.
9282 assert(EltVT == MVT::f32 && "Unexpected type!");
9283
9284 // Check 1.2.
9285 SDNode *Use = *N->use_begin();
9286 if (Use->getOpcode() != ISD::BITCAST ||
9287 Use->getValueType(0).isFloatingPoint())
9288 return SDValue();
9289
9290 // Check profitability.
9291 // Model is, if more than half of the relevant operands are bitcast from
9292 // i32, turn the build_vector into a sequence of insert_vector_elt.
9293 // Relevant operands are everything that is not statically
9294 // (i.e., at compile time) bitcasted.
9295 unsigned NumOfBitCastedElts = 0;
9296 unsigned NumElts = VT.getVectorNumElements();
9297 unsigned NumOfRelevantElts = NumElts;
9298 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9299 SDValue Elt = N->getOperand(Idx);
9300 if (Elt->getOpcode() == ISD::BITCAST) {
9301 // Assume only bit cast to i32 will go away.
9302 if (Elt->getOperand(0).getValueType() == MVT::i32)
9303 ++NumOfBitCastedElts;
9304 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
9305 // Constants are statically casted, thus do not count them as
9306 // relevant operands.
9307 --NumOfRelevantElts;
9308 }
9309
9310 // Check if more than half of the elements require a non-free bitcast.
9311 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9312 return SDValue();
9313
9314 SelectionDAG &DAG = DCI.DAG;
9315 // Create the new vector type.
9316 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9317 // Check if the type is legal.
9318 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9319 if (!TLI.isTypeLegal(VecVT))
9320 return SDValue();
9321
9322 // Combine:
9323 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9324 // => BITCAST INSERT_VECTOR_ELT
9325 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9326 // (BITCAST EN), N.
9327 SDValue Vec = DAG.getUNDEF(VecVT);
9328 SDLoc dl(N);
9329 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9330 SDValue V = N->getOperand(Idx);
9331 if (V.getOpcode() == ISD::UNDEF)
9332 continue;
9333 if (V.getOpcode() == ISD::BITCAST &&
9334 V->getOperand(0).getValueType() == MVT::i32)
9335 // Fold obvious case.
9336 V = V.getOperand(0);
9337 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009338 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009339 // Make the DAGCombiner fold the bitcasts.
9340 DCI.AddToWorklist(V.getNode());
9341 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009342 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009343 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9344 }
9345 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9346 // Make the DAGCombiner fold the bitcasts.
9347 DCI.AddToWorklist(Vec.getNode());
9348 return Vec;
9349}
9350
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009351/// PerformInsertEltCombine - Target-specific dag combine xforms for
9352/// ISD::INSERT_VECTOR_ELT.
9353static SDValue PerformInsertEltCombine(SDNode *N,
9354 TargetLowering::DAGCombinerInfo &DCI) {
9355 // Bitcast an i64 load inserted into a vector to f64.
9356 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9357 EVT VT = N->getValueType(0);
9358 SDNode *Elt = N->getOperand(1).getNode();
9359 if (VT.getVectorElementType() != MVT::i64 ||
9360 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9361 return SDValue();
9362
9363 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009364 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009365 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9366 VT.getVectorNumElements());
9367 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9368 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9369 // Make the DAGCombiner fold the bitcasts.
9370 DCI.AddToWorklist(Vec.getNode());
9371 DCI.AddToWorklist(V.getNode());
9372 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9373 Vec, V, N->getOperand(2));
9374 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009375}
9376
Bob Wilsonc7334a12010-10-27 20:38:28 +00009377/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9378/// ISD::VECTOR_SHUFFLE.
9379static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9380 // The LLVM shufflevector instruction does not require the shuffle mask
9381 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9382 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9383 // operands do not match the mask length, they are extended by concatenating
9384 // them with undef vectors. That is probably the right thing for other
9385 // targets, but for NEON it is better to concatenate two double-register
9386 // size vector operands into a single quad-register size vector. Do that
9387 // transformation here:
9388 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9389 // shuffle(concat(v1, v2), undef)
9390 SDValue Op0 = N->getOperand(0);
9391 SDValue Op1 = N->getOperand(1);
9392 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9393 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9394 Op0.getNumOperands() != 2 ||
9395 Op1.getNumOperands() != 2)
9396 return SDValue();
9397 SDValue Concat0Op1 = Op0.getOperand(1);
9398 SDValue Concat1Op1 = Op1.getOperand(1);
9399 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9400 Concat1Op1.getOpcode() != ISD::UNDEF)
9401 return SDValue();
9402 // Skip the transformation if any of the types are illegal.
9403 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9404 EVT VT = N->getValueType(0);
9405 if (!TLI.isTypeLegal(VT) ||
9406 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9407 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9408 return SDValue();
9409
Andrew Trickef9de2a2013-05-25 02:42:55 +00009410 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009411 Op0.getOperand(0), Op1.getOperand(0));
9412 // Translate the shuffle mask.
9413 SmallVector<int, 16> NewMask;
9414 unsigned NumElts = VT.getVectorNumElements();
9415 unsigned HalfElts = NumElts/2;
9416 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9417 for (unsigned n = 0; n < NumElts; ++n) {
9418 int MaskElt = SVN->getMaskElt(n);
9419 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009420 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009421 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009422 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009423 NewElt = HalfElts + MaskElt - NumElts;
9424 NewMask.push_back(NewElt);
9425 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009426 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009427 DAG.getUNDEF(VT), NewMask.data());
9428}
9429
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009430/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9431/// NEON load/store intrinsics, and generic vector load/stores, to merge
9432/// base address updates.
9433/// For generic load/stores, the memory type is assumed to be a vector.
9434/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009435static SDValue CombineBaseUpdate(SDNode *N,
9436 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009437 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009438 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9439 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009440 const bool isStore = N->getOpcode() == ISD::STORE;
9441 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009442 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009443 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009444 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009445
9446 // Search for a use of the address operand that is an increment.
9447 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9448 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9449 SDNode *User = *UI;
9450 if (User->getOpcode() != ISD::ADD ||
9451 UI.getUse().getResNo() != Addr.getResNo())
9452 continue;
9453
9454 // Check that the add is independent of the load/store. Otherwise, folding
9455 // it would create a cycle.
9456 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9457 continue;
9458
9459 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009460 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009461 bool isLaneOp = false;
9462 unsigned NewOpc = 0;
9463 unsigned NumVecs = 0;
9464 if (isIntrinsic) {
9465 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9466 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009467 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009468 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9469 NumVecs = 1; break;
9470 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9471 NumVecs = 2; break;
9472 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9473 NumVecs = 3; break;
9474 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9475 NumVecs = 4; break;
9476 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9477 NumVecs = 2; isLaneOp = true; break;
9478 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9479 NumVecs = 3; isLaneOp = true; break;
9480 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9481 NumVecs = 4; isLaneOp = true; break;
9482 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009483 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009484 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009485 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009486 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009487 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009488 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009489 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009490 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009491 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009492 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009493 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009494 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009495 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009496 }
9497 } else {
9498 isLaneOp = true;
9499 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009500 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009501 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9502 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9503 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009504 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9505 NumVecs = 1; isLaneOp = false; break;
9506 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9507 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009508 }
9509 }
9510
9511 // Find the size of memory referenced by the load/store.
9512 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009513 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009514 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009515 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009516 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009517 } else {
9518 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9519 VecTy = N->getOperand(1).getValueType();
9520 }
9521
Bob Wilson06fce872011-02-07 17:43:21 +00009522 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9523 if (isLaneOp)
9524 NumBytes /= VecTy.getVectorNumElements();
9525
9526 // If the increment is a constant, it must match the memory ref size.
9527 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9528 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9529 uint64_t IncVal = CInc->getZExtValue();
9530 if (IncVal != NumBytes)
9531 continue;
9532 } else if (NumBytes >= 3 * 16) {
9533 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9534 // separate instructions that make it harder to use a non-constant update.
9535 continue;
9536 }
9537
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009538 // OK, we found an ADD we can fold into the base update.
9539 // Now, create a _UPD node, taking care of not breaking alignment.
9540
9541 EVT AlignedVecTy = VecTy;
9542 unsigned Alignment = MemN->getAlignment();
9543
9544 // If this is a less-than-standard-aligned load/store, change the type to
9545 // match the standard alignment.
9546 // The alignment is overlooked when selecting _UPD variants; and it's
9547 // easier to introduce bitcasts here than fix that.
9548 // There are 3 ways to get to this base-update combine:
9549 // - intrinsics: they are assumed to be properly aligned (to the standard
9550 // alignment of the memory type), so we don't need to do anything.
9551 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9552 // intrinsics, so, likewise, there's nothing to do.
9553 // - generic load/store instructions: the alignment is specified as an
9554 // explicit operand, rather than implicitly as the standard alignment
9555 // of the memory type (like the intrisics). We need to change the
9556 // memory type to match the explicit alignment. That way, we don't
9557 // generate non-standard-aligned ARMISD::VLDx nodes.
9558 if (isa<LSBaseSDNode>(N)) {
9559 if (Alignment == 0)
9560 Alignment = 1;
9561 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9562 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9563 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9564 assert(!isLaneOp && "Unexpected generic load/store lane.");
9565 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9566 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9567 }
9568 // Don't set an explicit alignment on regular load/stores that we want
9569 // to transform to VLD/VST 1_UPD nodes.
9570 // This matches the behavior of regular load/stores, which only get an
9571 // explicit alignment if the MMO alignment is larger than the standard
9572 // alignment of the memory type.
9573 // Intrinsics, however, always get an explicit alignment, set to the
9574 // alignment of the MMO.
9575 Alignment = 1;
9576 }
9577
Bob Wilson06fce872011-02-07 17:43:21 +00009578 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009579 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009580 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009581 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009582 unsigned n;
9583 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009584 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009585 Tys[n++] = MVT::i32;
9586 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009587 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009588
9589 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009590 SmallVector<SDValue, 8> Ops;
9591 Ops.push_back(N->getOperand(0)); // incoming chain
9592 Ops.push_back(N->getOperand(AddrOpIdx));
9593 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009594
9595 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9596 // Try to match the intrinsic's signature
9597 Ops.push_back(StN->getValue());
9598 } else {
9599 // Loads (and of course intrinsics) match the intrinsics' signature,
9600 // so just add all but the alignment operand.
9601 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9602 Ops.push_back(N->getOperand(i));
9603 }
9604
9605 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009606 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009607
9608 // If this is a non-standard-aligned STORE, the penultimate operand is the
9609 // stored value. Bitcast it to the aligned type.
9610 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9611 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009612 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009613 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009614
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009615 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009616 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009617 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009618
9619 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009620 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009621 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009622 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009623
9624 // If this is an non-standard-aligned LOAD, the first result is the loaded
9625 // value. Bitcast it to the expected result type.
9626 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9627 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009628 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009629 }
9630
Bob Wilson06fce872011-02-07 17:43:21 +00009631 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9632 DCI.CombineTo(N, NewResults);
9633 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9634
9635 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009636 }
Bob Wilson06fce872011-02-07 17:43:21 +00009637 return SDValue();
9638}
9639
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009640static SDValue PerformVLDCombine(SDNode *N,
9641 TargetLowering::DAGCombinerInfo &DCI) {
9642 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9643 return SDValue();
9644
9645 return CombineBaseUpdate(N, DCI);
9646}
9647
Bob Wilson2d790df2010-11-28 06:51:26 +00009648/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9649/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9650/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9651/// return true.
9652static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9653 SelectionDAG &DAG = DCI.DAG;
9654 EVT VT = N->getValueType(0);
9655 // vldN-dup instructions only support 64-bit vectors for N > 1.
9656 if (!VT.is64BitVector())
9657 return false;
9658
9659 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9660 SDNode *VLD = N->getOperand(0).getNode();
9661 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9662 return false;
9663 unsigned NumVecs = 0;
9664 unsigned NewOpc = 0;
9665 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9666 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9667 NumVecs = 2;
9668 NewOpc = ARMISD::VLD2DUP;
9669 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9670 NumVecs = 3;
9671 NewOpc = ARMISD::VLD3DUP;
9672 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9673 NumVecs = 4;
9674 NewOpc = ARMISD::VLD4DUP;
9675 } else {
9676 return false;
9677 }
9678
9679 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9680 // numbers match the load.
9681 unsigned VLDLaneNo =
9682 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9683 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9684 UI != UE; ++UI) {
9685 // Ignore uses of the chain result.
9686 if (UI.getUse().getResNo() == NumVecs)
9687 continue;
9688 SDNode *User = *UI;
9689 if (User->getOpcode() != ARMISD::VDUPLANE ||
9690 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9691 return false;
9692 }
9693
9694 // Create the vldN-dup node.
9695 EVT Tys[5];
9696 unsigned n;
9697 for (n = 0; n < NumVecs; ++n)
9698 Tys[n] = VT;
9699 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009700 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009701 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9702 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009703 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009704 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009705 VLDMemInt->getMemOperand());
9706
9707 // Update the uses.
9708 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9709 UI != UE; ++UI) {
9710 unsigned ResNo = UI.getUse().getResNo();
9711 // Ignore uses of the chain result.
9712 if (ResNo == NumVecs)
9713 continue;
9714 SDNode *User = *UI;
9715 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9716 }
9717
9718 // Now the vldN-lane intrinsic is dead except for its chain result.
9719 // Update uses of the chain.
9720 std::vector<SDValue> VLDDupResults;
9721 for (unsigned n = 0; n < NumVecs; ++n)
9722 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9723 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9724 DCI.CombineTo(VLD, VLDDupResults);
9725
9726 return true;
9727}
9728
Bob Wilson103a0dc2010-07-14 01:22:12 +00009729/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9730/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009731static SDValue PerformVDUPLANECombine(SDNode *N,
9732 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009733 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009734
Bob Wilson2d790df2010-11-28 06:51:26 +00009735 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9736 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9737 if (CombineVLDDUP(N, DCI))
9738 return SDValue(N, 0);
9739
9740 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9741 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009742 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009743 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009744 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009745 return SDValue();
9746
9747 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9748 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9749 // The canonical VMOV for a zero vector uses a 32-bit element size.
9750 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9751 unsigned EltBits;
9752 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9753 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009754 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009755 if (EltSize > VT.getVectorElementType().getSizeInBits())
9756 return SDValue();
9757
Andrew Trickef9de2a2013-05-25 02:42:55 +00009758 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009759}
9760
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009761static SDValue PerformLOADCombine(SDNode *N,
9762 TargetLowering::DAGCombinerInfo &DCI) {
9763 EVT VT = N->getValueType(0);
9764
9765 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9766 if (ISD::isNormalLoad(N) && VT.isVector() &&
9767 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9768 return CombineBaseUpdate(N, DCI);
9769
9770 return SDValue();
9771}
9772
Ahmed Bougacha23167462014-12-09 21:26:53 +00009773/// PerformSTORECombine - Target-specific dag combine xforms for
9774/// ISD::STORE.
9775static SDValue PerformSTORECombine(SDNode *N,
9776 TargetLowering::DAGCombinerInfo &DCI) {
9777 StoreSDNode *St = cast<StoreSDNode>(N);
9778 if (St->isVolatile())
9779 return SDValue();
9780
9781 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9782 // pack all of the elements in one place. Next, store to memory in fewer
9783 // chunks.
9784 SDValue StVal = St->getValue();
9785 EVT VT = StVal.getValueType();
9786 if (St->isTruncatingStore() && VT.isVector()) {
9787 SelectionDAG &DAG = DCI.DAG;
9788 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9789 EVT StVT = St->getMemoryVT();
9790 unsigned NumElems = VT.getVectorNumElements();
9791 assert(StVT != VT && "Cannot truncate to the same type");
9792 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9793 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9794
9795 // From, To sizes and ElemCount must be pow of two
9796 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9797
9798 // We are going to use the original vector elt for storing.
9799 // Accumulated smaller vector elements must be a multiple of the store size.
9800 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9801
9802 unsigned SizeRatio = FromEltSz / ToEltSz;
9803 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9804
9805 // Create a type on which we perform the shuffle.
9806 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9807 NumElems*SizeRatio);
9808 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9809
9810 SDLoc DL(St);
9811 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9812 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9813 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009814 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9815 ? (i + 1) * SizeRatio - 1
9816 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009817
9818 // Can't shuffle using an illegal type.
9819 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9820
9821 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9822 DAG.getUNDEF(WideVec.getValueType()),
9823 ShuffleVec.data());
9824 // At this point all of the data is stored at the bottom of the
9825 // register. We now need to save it to mem.
9826
9827 // Find the largest store unit
9828 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009829 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009830 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9831 StoreType = Tp;
9832 }
9833 // Didn't find a legal store type.
9834 if (!TLI.isTypeLegal(StoreType))
9835 return SDValue();
9836
9837 // Bitcast the original vector into a vector of store-size units
9838 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9839 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9840 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9841 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9842 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009843 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9844 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009845 SDValue BasePtr = St->getBasePtr();
9846
9847 // Perform one or more big stores into memory.
9848 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9849 for (unsigned I = 0; I < E; I++) {
9850 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9851 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009852 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009853 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9854 St->getPointerInfo(), St->isVolatile(),
9855 St->isNonTemporal(), St->getAlignment());
9856 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9857 Increment);
9858 Chains.push_back(Ch);
9859 }
9860 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9861 }
9862
9863 if (!ISD::isNormalStore(St))
9864 return SDValue();
9865
9866 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9867 // ARM stores of arguments in the same cache line.
9868 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9869 StVal.getNode()->hasOneUse()) {
9870 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009871 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009872 SDLoc DL(St);
9873 SDValue BasePtr = St->getBasePtr();
9874 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9875 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9876 BasePtr, St->getPointerInfo(), St->isVolatile(),
9877 St->isNonTemporal(), St->getAlignment());
9878
9879 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009880 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009881 return DAG.getStore(NewST1.getValue(0), DL,
9882 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9883 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9884 St->isNonTemporal(),
9885 std::min(4U, St->getAlignment() / 2));
9886 }
9887
9888 if (StVal.getValueType() == MVT::i64 &&
9889 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9890
9891 // Bitcast an i64 store extracted from a vector to f64.
9892 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9893 SelectionDAG &DAG = DCI.DAG;
9894 SDLoc dl(StVal);
9895 SDValue IntVec = StVal.getOperand(0);
9896 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9897 IntVec.getValueType().getVectorNumElements());
9898 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9899 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9900 Vec, StVal.getOperand(1));
9901 dl = SDLoc(N);
9902 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9903 // Make the DAGCombiner fold the bitcasts.
9904 DCI.AddToWorklist(Vec.getNode());
9905 DCI.AddToWorklist(ExtElt.getNode());
9906 DCI.AddToWorklist(V.getNode());
9907 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9908 St->getPointerInfo(), St->isVolatile(),
9909 St->isNonTemporal(), St->getAlignment(),
9910 St->getAAInfo());
9911 }
9912
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009913 // If this is a legal vector store, try to combine it into a VST1_UPD.
9914 if (ISD::isNormalStore(N) && VT.isVector() &&
9915 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9916 return CombineBaseUpdate(N, DCI);
9917
Ahmed Bougacha23167462014-12-09 21:26:53 +00009918 return SDValue();
9919}
9920
Chad Rosierfa8d8932011-06-24 19:23:04 +00009921/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9922/// can replace combinations of VMUL and VCVT (floating-point to integer)
9923/// when the VMUL has a constant operand that is a power of 2.
9924///
9925/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9926/// vmul.f32 d16, d17, d16
9927/// vcvt.s32.f32 d16, d16
9928/// becomes:
9929/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +00009930static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009931 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +00009932 if (!Subtarget->hasNEON())
9933 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +00009934
Chad Rosiera087fd22015-10-06 20:23:42 +00009935 SDValue Op = N->getOperand(0);
9936 if (!Op.getValueType().isVector() || Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +00009937 return SDValue();
9938
Chad Rosierfa8d8932011-06-24 19:23:04 +00009939 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +00009940 if (!isa<BuildVectorSDNode>(ConstVec))
9941 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +00009942
Tim Northover7cbc2152013-06-28 15:29:25 +00009943 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +00009944 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +00009945 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +00009946 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +00009947 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +00009948 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009949 // These instructions only exist converting from f32 to i32. We can handle
9950 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009951 // be lossy. We also can't handle more then 4 lanes, since these intructions
9952 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009953 return SDValue();
9954 }
9955
Chad Rosier169865f2015-10-07 17:28:58 +00009956 BitVector UndefElements;
9957 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
9958 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
9959 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +00009960 return SDValue();
9961
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009962 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +00009963 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009964 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9965 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +00009966 SDValue FixConv = DAG.getNode(
9967 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9968 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +00009969 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009970
Chad Rosier9df4aff2015-10-06 20:45:45 +00009971 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009972 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009973
9974 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009975}
9976
9977/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9978/// can replace combinations of VCVT (integer to floating-point) and VDIV
9979/// when the VDIV has a constant operand that is a power of 2.
9980///
9981/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9982/// vcvt.f32.s32 d16, d16
9983/// vdiv.f32 d16, d17, d16
9984/// becomes:
9985/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +00009986static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009987 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +00009988 if (!Subtarget->hasNEON())
9989 return SDValue();
9990
Chad Rosierfa8d8932011-06-24 19:23:04 +00009991 SDValue Op = N->getOperand(0);
9992 unsigned OpOpcode = Op.getNode()->getOpcode();
Chad Rosiera087fd22015-10-06 20:23:42 +00009993 if (!N->getValueType(0).isVector() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009994 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9995 return SDValue();
9996
Chad Rosierfa8d8932011-06-24 19:23:04 +00009997 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +00009998 if (!isa<BuildVectorSDNode>(ConstVec))
9999 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010000
Tim Northover7cbc2152013-06-28 15:29:25 +000010001 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010002 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010003 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010004 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010005 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10006 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010007 // These instructions only exist converting from i32 to f32. We can handle
10008 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010009 // be lossy. We also can't handle more then 4 lanes, since these intructions
10010 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010011 return SDValue();
10012 }
10013
Chad Rosier169865f2015-10-07 17:28:58 +000010014 BitVector UndefElements;
10015 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10016 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10017 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010018 return SDValue();
10019
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010020 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010021 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010022 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010023 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010024 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010025 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010026 ConvInput);
10027
Eric Christopher1b8b94192011-06-29 21:10:36 +000010028 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010029 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010030 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010031 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010032 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010033 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010034}
10035
10036/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010037/// operand of a vector shift operation, where all the elements of the
10038/// build_vector must have the same constant integer value.
10039static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10040 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010041 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010042 Op = Op.getOperand(0);
10043 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10044 APInt SplatBits, SplatUndef;
10045 unsigned SplatBitSize;
10046 bool HasAnyUndefs;
10047 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10048 HasAnyUndefs, ElementBits) ||
10049 SplatBitSize > ElementBits)
10050 return false;
10051 Cnt = SplatBits.getSExtValue();
10052 return true;
10053}
10054
10055/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10056/// operand of a vector shift left operation. That value must be in the range:
10057/// 0 <= Value < ElementBits for a left shift; or
10058/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010059static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010060 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010061 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010062 if (! getVShiftImm(Op, ElementBits, Cnt))
10063 return false;
10064 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10065}
10066
10067/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10068/// operand of a vector shift right operation. For a shift opcode, the value
10069/// is positive, but for an intrinsic the value count must be negative. The
10070/// absolute value must be in the range:
10071/// 1 <= |Value| <= ElementBits for a right shift; or
10072/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010073static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010074 int64_t &Cnt) {
10075 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010076 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010077 if (! getVShiftImm(Op, ElementBits, Cnt))
10078 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010079 if (!isIntrinsic)
10080 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10081 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010082 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010083 return true;
10084 }
10085 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010086}
10087
10088/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10089static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10090 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10091 switch (IntNo) {
10092 default:
10093 // Don't do anything for most intrinsics.
10094 break;
10095
James Molloya6702e22015-07-17 17:10:55 +000010096 case Intrinsic::arm_neon_vabds:
10097 if (!N->getValueType(0).isInteger())
10098 return SDValue();
10099 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0),
10100 N->getOperand(1), N->getOperand(2));
10101 case Intrinsic::arm_neon_vabdu:
10102 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0),
10103 N->getOperand(1), N->getOperand(2));
10104
Bob Wilson2e076c42009-06-22 23:27:02 +000010105 // Vector shifts: check for immediate versions and lower them.
10106 // Note: This is done during DAG combining instead of DAG legalizing because
10107 // the build_vectors for 64-bit vector element shift counts are generally
10108 // not legal, and it is hard to see their values after they get legalized to
10109 // loads from a constant pool.
10110 case Intrinsic::arm_neon_vshifts:
10111 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010112 case Intrinsic::arm_neon_vrshifts:
10113 case Intrinsic::arm_neon_vrshiftu:
10114 case Intrinsic::arm_neon_vrshiftn:
10115 case Intrinsic::arm_neon_vqshifts:
10116 case Intrinsic::arm_neon_vqshiftu:
10117 case Intrinsic::arm_neon_vqshiftsu:
10118 case Intrinsic::arm_neon_vqshiftns:
10119 case Intrinsic::arm_neon_vqshiftnu:
10120 case Intrinsic::arm_neon_vqshiftnsu:
10121 case Intrinsic::arm_neon_vqrshiftns:
10122 case Intrinsic::arm_neon_vqrshiftnu:
10123 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010124 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010125 int64_t Cnt;
10126 unsigned VShiftOpc = 0;
10127
10128 switch (IntNo) {
10129 case Intrinsic::arm_neon_vshifts:
10130 case Intrinsic::arm_neon_vshiftu:
10131 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10132 VShiftOpc = ARMISD::VSHL;
10133 break;
10134 }
10135 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10136 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10137 ARMISD::VSHRs : ARMISD::VSHRu);
10138 break;
10139 }
10140 return SDValue();
10141
Bob Wilson2e076c42009-06-22 23:27:02 +000010142 case Intrinsic::arm_neon_vrshifts:
10143 case Intrinsic::arm_neon_vrshiftu:
10144 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10145 break;
10146 return SDValue();
10147
10148 case Intrinsic::arm_neon_vqshifts:
10149 case Intrinsic::arm_neon_vqshiftu:
10150 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10151 break;
10152 return SDValue();
10153
10154 case Intrinsic::arm_neon_vqshiftsu:
10155 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10156 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010157 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010158
Bob Wilson2e076c42009-06-22 23:27:02 +000010159 case Intrinsic::arm_neon_vrshiftn:
10160 case Intrinsic::arm_neon_vqshiftns:
10161 case Intrinsic::arm_neon_vqshiftnu:
10162 case Intrinsic::arm_neon_vqshiftnsu:
10163 case Intrinsic::arm_neon_vqrshiftns:
10164 case Intrinsic::arm_neon_vqrshiftnu:
10165 case Intrinsic::arm_neon_vqrshiftnsu:
10166 // Narrowing shifts require an immediate right shift.
10167 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10168 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010169 llvm_unreachable("invalid shift count for narrowing vector shift "
10170 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010171
10172 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010173 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010174 }
10175
10176 switch (IntNo) {
10177 case Intrinsic::arm_neon_vshifts:
10178 case Intrinsic::arm_neon_vshiftu:
10179 // Opcode already set above.
10180 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010181 case Intrinsic::arm_neon_vrshifts:
10182 VShiftOpc = ARMISD::VRSHRs; break;
10183 case Intrinsic::arm_neon_vrshiftu:
10184 VShiftOpc = ARMISD::VRSHRu; break;
10185 case Intrinsic::arm_neon_vrshiftn:
10186 VShiftOpc = ARMISD::VRSHRN; break;
10187 case Intrinsic::arm_neon_vqshifts:
10188 VShiftOpc = ARMISD::VQSHLs; break;
10189 case Intrinsic::arm_neon_vqshiftu:
10190 VShiftOpc = ARMISD::VQSHLu; break;
10191 case Intrinsic::arm_neon_vqshiftsu:
10192 VShiftOpc = ARMISD::VQSHLsu; break;
10193 case Intrinsic::arm_neon_vqshiftns:
10194 VShiftOpc = ARMISD::VQSHRNs; break;
10195 case Intrinsic::arm_neon_vqshiftnu:
10196 VShiftOpc = ARMISD::VQSHRNu; break;
10197 case Intrinsic::arm_neon_vqshiftnsu:
10198 VShiftOpc = ARMISD::VQSHRNsu; break;
10199 case Intrinsic::arm_neon_vqrshiftns:
10200 VShiftOpc = ARMISD::VQRSHRNs; break;
10201 case Intrinsic::arm_neon_vqrshiftnu:
10202 VShiftOpc = ARMISD::VQRSHRNu; break;
10203 case Intrinsic::arm_neon_vqrshiftnsu:
10204 VShiftOpc = ARMISD::VQRSHRNsu; break;
10205 }
10206
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010207 SDLoc dl(N);
10208 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10209 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010210 }
10211
10212 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010213 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010214 int64_t Cnt;
10215 unsigned VShiftOpc = 0;
10216
10217 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10218 VShiftOpc = ARMISD::VSLI;
10219 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10220 VShiftOpc = ARMISD::VSRI;
10221 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010222 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010223 }
10224
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010225 SDLoc dl(N);
10226 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010227 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010228 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010229 }
10230
10231 case Intrinsic::arm_neon_vqrshifts:
10232 case Intrinsic::arm_neon_vqrshiftu:
10233 // No immediate versions of these to check for.
10234 break;
10235 }
10236
10237 return SDValue();
10238}
10239
10240/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10241/// lowers them. As with the vector shift intrinsics, this is done during DAG
10242/// combining instead of DAG legalizing because the build_vectors for 64-bit
10243/// vector element shift counts are generally not legal, and it is hard to see
10244/// their values after they get legalized to loads from a constant pool.
10245static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10246 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010247 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010248 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10249 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10250 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10251 SDValue N1 = N->getOperand(1);
10252 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10253 SDValue N0 = N->getOperand(0);
10254 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10255 DAG.MaskedValueIsZero(N0.getOperand(0),
10256 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010257 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010258 }
10259 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010260
10261 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010262 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10263 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010264 return SDValue();
10265
10266 assert(ST->hasNEON() && "unexpected vector shift");
10267 int64_t Cnt;
10268
10269 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010270 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010271
10272 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010273 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10274 SDLoc dl(N);
10275 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10276 DAG.getConstant(Cnt, dl, MVT::i32));
10277 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010278 break;
10279
10280 case ISD::SRA:
10281 case ISD::SRL:
10282 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10283 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10284 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010285 SDLoc dl(N);
10286 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10287 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010288 }
10289 }
10290 return SDValue();
10291}
10292
10293/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10294/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10295static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10296 const ARMSubtarget *ST) {
10297 SDValue N0 = N->getOperand(0);
10298
10299 // Check for sign- and zero-extensions of vector extract operations of 8-
10300 // and 16-bit vector elements. NEON supports these directly. They are
10301 // handled during DAG combining because type legalization will promote them
10302 // to 32-bit types and it is messy to recognize the operations after that.
10303 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10304 SDValue Vec = N0.getOperand(0);
10305 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010306 EVT VT = N->getValueType(0);
10307 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010308 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10309
Owen Anderson9f944592009-08-11 20:47:22 +000010310 if (VT == MVT::i32 &&
10311 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010312 TLI.isTypeLegal(Vec.getValueType()) &&
10313 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010314
10315 unsigned Opc = 0;
10316 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010317 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010318 case ISD::SIGN_EXTEND:
10319 Opc = ARMISD::VGETLANEs;
10320 break;
10321 case ISD::ZERO_EXTEND:
10322 case ISD::ANY_EXTEND:
10323 Opc = ARMISD::VGETLANEu;
10324 break;
10325 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010326 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010327 }
10328 }
10329
10330 return SDValue();
10331}
10332
James Molloy9d55f192015-11-10 14:22:05 +000010333static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10334 APInt &KnownOne) {
10335 if (Op.getOpcode() == ARMISD::BFI) {
10336 // Conservatively, we can recurse down the first operand
10337 // and just mask out all affected bits.
10338 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10339
10340 // The operand to BFI is already a mask suitable for removing the bits it
10341 // sets.
10342 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
10343 APInt Mask = CI->getAPIntValue();
10344 KnownZero &= Mask;
10345 KnownOne &= Mask;
10346 return;
10347 }
10348 if (Op.getOpcode() == ARMISD::CMOV) {
10349 APInt KZ2(KnownZero.getBitWidth(), 0);
10350 APInt KO2(KnownOne.getBitWidth(), 0);
10351 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10352 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10353
10354 KnownZero &= KZ2;
10355 KnownOne &= KO2;
10356 return;
10357 }
10358 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10359}
10360
10361SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10362 // If we have a CMOV, OR and AND combination such as:
10363 // if (x & CN)
10364 // y |= CM;
10365 //
10366 // And:
10367 // * CN is a single bit;
10368 // * All bits covered by CM are known zero in y
10369 //
10370 // Then we can convert this into a sequence of BFI instructions. This will
10371 // always be a win if CM is a single bit, will always be no worse than the
10372 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10373 // three bits (due to the extra IT instruction).
10374
10375 SDValue Op0 = CMOV->getOperand(0);
10376 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010377 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10378 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010379 SDValue CmpZ = CMOV->getOperand(4);
10380
James Molloy20180912015-11-16 10:49:25 +000010381 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010382 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010383 return SDValue();
10384
James Molloy9d55f192015-11-10 14:22:05 +000010385 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10386 SDValue And = CmpZ->getOperand(0);
10387 if (And->getOpcode() != ISD::AND)
10388 return SDValue();
10389 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10390 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10391 return SDValue();
10392 SDValue X = And->getOperand(0);
10393
James Molloy8e99e972015-11-12 13:49:17 +000010394 if (CC == ARMCC::EQ) {
10395 // We're performing an "equal to zero" compare. Swap the operands so we
10396 // canonicalize on a "not equal to zero" compare.
10397 std::swap(Op0, Op1);
10398 } else {
10399 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10400 }
10401
James Molloy9d55f192015-11-10 14:22:05 +000010402 if (Op1->getOpcode() != ISD::OR)
10403 return SDValue();
10404
10405 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10406 if (!OrC)
10407 return SDValue();
10408 SDValue Y = Op1->getOperand(0);
10409
10410 if (Op0 != Y)
10411 return SDValue();
10412
10413 // Now, is it profitable to continue?
10414 APInt OrCI = OrC->getAPIntValue();
10415 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10416 if (OrCI.countPopulation() > Heuristic)
10417 return SDValue();
10418
10419 // Lastly, can we determine that the bits defined by OrCI
10420 // are zero in Y?
10421 APInt KnownZero, KnownOne;
10422 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10423 if ((OrCI & KnownZero) != OrCI)
10424 return SDValue();
10425
10426 // OK, we can do the combine.
10427 SDValue V = Y;
10428 SDLoc dl(X);
10429 EVT VT = X.getValueType();
10430 unsigned BitInX = AndC->getAPIntValue().logBase2();
10431
10432 if (BitInX != 0) {
10433 // We must shift X first.
10434 X = DAG.getNode(ISD::SRL, dl, VT, X,
10435 DAG.getConstant(BitInX, dl, VT));
10436 }
10437
10438 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10439 BitInY < NumActiveBits; ++BitInY) {
10440 if (OrCI[BitInY] == 0)
10441 continue;
10442 APInt Mask(VT.getSizeInBits(), 0);
10443 Mask.setBit(BitInY);
10444 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10445 // Confusingly, the operand is an *inverted* mask.
10446 DAG.getConstant(~Mask, dl, VT));
10447 }
10448
10449 return V;
10450}
10451
Evan Chengf863e3f2011-07-13 00:42:17 +000010452/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10453SDValue
10454ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10455 SDValue Cmp = N->getOperand(4);
10456 if (Cmp.getOpcode() != ARMISD::CMPZ)
10457 // Only looking at EQ and NE cases.
10458 return SDValue();
10459
10460 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010461 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010462 SDValue LHS = Cmp.getOperand(0);
10463 SDValue RHS = Cmp.getOperand(1);
10464 SDValue FalseVal = N->getOperand(0);
10465 SDValue TrueVal = N->getOperand(1);
10466 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010467 ARMCC::CondCodes CC =
10468 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010469
James Molloy9d55f192015-11-10 14:22:05 +000010470 // BFI is only available on V6T2+.
10471 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10472 SDValue R = PerformCMOVToBFICombine(N, DAG);
10473 if (R)
10474 return R;
10475 }
10476
Evan Chengf863e3f2011-07-13 00:42:17 +000010477 // Simplify
10478 // mov r1, r0
10479 // cmp r1, x
10480 // mov r0, y
10481 // moveq r0, x
10482 // to
10483 // cmp r0, x
10484 // movne r0, y
10485 //
10486 // mov r1, r0
10487 // cmp r1, x
10488 // mov r0, x
10489 // movne r0, y
10490 // to
10491 // cmp r0, x
10492 // movne r0, y
10493 /// FIXME: Turn this into a target neutral optimization?
10494 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010495 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010496 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10497 N->getOperand(3), Cmp);
10498 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10499 SDValue ARMcc;
10500 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10501 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10502 N->getOperand(3), NewCmp);
10503 }
10504
10505 if (Res.getNode()) {
10506 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010507 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010508 // Capture demanded bits information that would be otherwise lost.
10509 if (KnownZero == 0xfffffffe)
10510 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10511 DAG.getValueType(MVT::i1));
10512 else if (KnownZero == 0xffffff00)
10513 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10514 DAG.getValueType(MVT::i8));
10515 else if (KnownZero == 0xffff0000)
10516 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10517 DAG.getValueType(MVT::i16));
10518 }
10519
10520 return Res;
10521}
10522
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010523SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010524 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010525 switch (N->getOpcode()) {
10526 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010527 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010528 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010529 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010530 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010531 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010532 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10533 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010534 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010535 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010536 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010537 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010538 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010539 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010540 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010541 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010542 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010543 case ISD::FP_TO_UINT:
10544 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10545 case ISD::FDIV:
10546 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010547 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010548 case ISD::SHL:
10549 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010550 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010551 case ISD::SIGN_EXTEND:
10552 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010553 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010554 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010555 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010556 case ARMISD::VLD2DUP:
10557 case ARMISD::VLD3DUP:
10558 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010559 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010560 case ARMISD::BUILD_VECTOR:
10561 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010562 case ISD::INTRINSIC_VOID:
10563 case ISD::INTRINSIC_W_CHAIN:
10564 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10565 case Intrinsic::arm_neon_vld1:
10566 case Intrinsic::arm_neon_vld2:
10567 case Intrinsic::arm_neon_vld3:
10568 case Intrinsic::arm_neon_vld4:
10569 case Intrinsic::arm_neon_vld2lane:
10570 case Intrinsic::arm_neon_vld3lane:
10571 case Intrinsic::arm_neon_vld4lane:
10572 case Intrinsic::arm_neon_vst1:
10573 case Intrinsic::arm_neon_vst2:
10574 case Intrinsic::arm_neon_vst3:
10575 case Intrinsic::arm_neon_vst4:
10576 case Intrinsic::arm_neon_vst2lane:
10577 case Intrinsic::arm_neon_vst3lane:
10578 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010579 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010580 default: break;
10581 }
10582 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010583 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010584 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010585}
10586
Evan Chengd42641c2011-02-02 01:06:55 +000010587bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10588 EVT VT) const {
10589 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10590}
10591
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010592bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10593 unsigned,
10594 unsigned,
10595 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010596 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010597 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010598
10599 switch (VT.getSimpleVT().SimpleTy) {
10600 default:
10601 return false;
10602 case MVT::i8:
10603 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010604 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010605 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010606 if (AllowsUnaligned) {
10607 if (Fast)
10608 *Fast = Subtarget->hasV7Ops();
10609 return true;
10610 }
10611 return false;
10612 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010613 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010614 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010615 // For any little-endian targets with neon, we can support unaligned ld/st
10616 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010617 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010618 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010619 if (Fast)
10620 *Fast = true;
10621 return true;
10622 }
10623 return false;
10624 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010625 }
10626}
10627
Lang Hames9929c422011-11-02 22:52:45 +000010628static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10629 unsigned AlignCheck) {
10630 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10631 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10632}
10633
10634EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10635 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010636 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010637 bool MemcpyStrSrc,
10638 MachineFunction &MF) const {
10639 const Function *F = MF.getFunction();
10640
10641 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010642 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10643 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010644 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010645 if (Size >= 16 &&
10646 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010647 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010648 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010649 } else if (Size >= 8 &&
10650 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010651 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10652 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010653 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010654 }
10655 }
10656
Lang Hamesb85fcd02011-11-08 18:56:23 +000010657 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010658 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010659 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010660 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010661 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010662
Lang Hames9929c422011-11-02 22:52:45 +000010663 // Let the target-independent logic figure it out.
10664 return MVT::Other;
10665}
10666
Evan Cheng9ec512d2012-12-06 19:13:27 +000010667bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10668 if (Val.getOpcode() != ISD::LOAD)
10669 return false;
10670
10671 EVT VT1 = Val.getValueType();
10672 if (!VT1.isSimple() || !VT1.isInteger() ||
10673 !VT2.isSimple() || !VT2.isInteger())
10674 return false;
10675
10676 switch (VT1.getSimpleVT().SimpleTy) {
10677 default: break;
10678 case MVT::i1:
10679 case MVT::i8:
10680 case MVT::i16:
10681 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10682 return true;
10683 }
10684
10685 return false;
10686}
10687
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010688bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10689 EVT VT = ExtVal.getValueType();
10690
10691 if (!isTypeLegal(VT))
10692 return false;
10693
10694 // Don't create a loadext if we can fold the extension into a wide/long
10695 // instruction.
10696 // If there's more than one user instruction, the loadext is desirable no
10697 // matter what. There can be two uses by the same instruction.
10698 if (ExtVal->use_empty() ||
10699 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10700 return true;
10701
10702 SDNode *U = *ExtVal->use_begin();
10703 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10704 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10705 return false;
10706
10707 return true;
10708}
10709
Tim Northovercc2e9032013-08-06 13:58:03 +000010710bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10711 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10712 return false;
10713
10714 if (!isTypeLegal(EVT::getEVT(Ty1)))
10715 return false;
10716
10717 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10718
10719 // Assuming the caller doesn't have a zeroext or signext return parameter,
10720 // truncation all the way down to i1 is valid.
10721 return true;
10722}
10723
10724
Evan Chengdc49a8d2009-08-14 20:09:37 +000010725static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10726 if (V < 0)
10727 return false;
10728
10729 unsigned Scale = 1;
10730 switch (VT.getSimpleVT().SimpleTy) {
10731 default: return false;
10732 case MVT::i1:
10733 case MVT::i8:
10734 // Scale == 1;
10735 break;
10736 case MVT::i16:
10737 // Scale == 2;
10738 Scale = 2;
10739 break;
10740 case MVT::i32:
10741 // Scale == 4;
10742 Scale = 4;
10743 break;
10744 }
10745
10746 if ((V & (Scale - 1)) != 0)
10747 return false;
10748 V /= Scale;
10749 return V == (V & ((1LL << 5) - 1));
10750}
10751
10752static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10753 const ARMSubtarget *Subtarget) {
10754 bool isNeg = false;
10755 if (V < 0) {
10756 isNeg = true;
10757 V = - V;
10758 }
10759
10760 switch (VT.getSimpleVT().SimpleTy) {
10761 default: return false;
10762 case MVT::i1:
10763 case MVT::i8:
10764 case MVT::i16:
10765 case MVT::i32:
10766 // + imm12 or - imm8
10767 if (isNeg)
10768 return V == (V & ((1LL << 8) - 1));
10769 return V == (V & ((1LL << 12) - 1));
10770 case MVT::f32:
10771 case MVT::f64:
10772 // Same as ARM mode. FIXME: NEON?
10773 if (!Subtarget->hasVFP2())
10774 return false;
10775 if ((V & 3) != 0)
10776 return false;
10777 V >>= 2;
10778 return V == (V & ((1LL << 8) - 1));
10779 }
10780}
10781
Evan Cheng2150b922007-03-12 23:30:29 +000010782/// isLegalAddressImmediate - Return true if the integer value can be used
10783/// as the offset of the target addressing mode for load / store of the
10784/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010785static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010786 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010787 if (V == 0)
10788 return true;
10789
Evan Chengce5dfb62009-03-09 19:15:00 +000010790 if (!VT.isSimple())
10791 return false;
10792
Evan Chengdc49a8d2009-08-14 20:09:37 +000010793 if (Subtarget->isThumb1Only())
10794 return isLegalT1AddressImmediate(V, VT);
10795 else if (Subtarget->isThumb2())
10796 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010797
Evan Chengdc49a8d2009-08-14 20:09:37 +000010798 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010799 if (V < 0)
10800 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010801 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010802 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010803 case MVT::i1:
10804 case MVT::i8:
10805 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010806 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010807 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010808 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010809 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010810 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010811 case MVT::f32:
10812 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010813 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010814 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010815 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010816 return false;
10817 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010818 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010819 }
Evan Cheng10043e22007-01-19 07:51:42 +000010820}
10821
Evan Chengdc49a8d2009-08-14 20:09:37 +000010822bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10823 EVT VT) const {
10824 int Scale = AM.Scale;
10825 if (Scale < 0)
10826 return false;
10827
10828 switch (VT.getSimpleVT().SimpleTy) {
10829 default: return false;
10830 case MVT::i1:
10831 case MVT::i8:
10832 case MVT::i16:
10833 case MVT::i32:
10834 if (Scale == 1)
10835 return true;
10836 // r + r << imm
10837 Scale = Scale & ~1;
10838 return Scale == 2 || Scale == 4 || Scale == 8;
10839 case MVT::i64:
10840 // r + r
10841 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10842 return true;
10843 return false;
10844 case MVT::isVoid:
10845 // Note, we allow "void" uses (basically, uses that aren't loads or
10846 // stores), because arm allows folding a scale into many arithmetic
10847 // operations. This should be made more precise and revisited later.
10848
10849 // Allow r << imm, but the imm has to be a multiple of two.
10850 if (Scale & 1) return false;
10851 return isPowerOf2_32(Scale);
10852 }
10853}
10854
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010855/// isLegalAddressingMode - Return true if the addressing mode represented
10856/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010857bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10858 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010859 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010860 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010861 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010862 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010863
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010864 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010865 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010866 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010867
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010868 switch (AM.Scale) {
10869 case 0: // no scale reg, must be "r+i" or "r", or "i".
10870 break;
10871 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010872 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010873 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010874 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010875 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010876 // ARM doesn't support any R+R*scale+imm addr modes.
10877 if (AM.BaseOffs)
10878 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010879
Bob Wilson866c1742009-04-08 17:55:28 +000010880 if (!VT.isSimple())
10881 return false;
10882
Evan Chengdc49a8d2009-08-14 20:09:37 +000010883 if (Subtarget->isThumb2())
10884 return isLegalT2ScaledAddressingMode(AM, VT);
10885
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010886 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010887 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010888 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010889 case MVT::i1:
10890 case MVT::i8:
10891 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010892 if (Scale < 0) Scale = -Scale;
10893 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010894 return true;
10895 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010896 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010897 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010898 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010899 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010900 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010901 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010902 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010903
Owen Anderson9f944592009-08-11 20:47:22 +000010904 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010905 // Note, we allow "void" uses (basically, uses that aren't loads or
10906 // stores), because arm allows folding a scale into many arithmetic
10907 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010908
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010909 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010910 if (Scale & 1) return false;
10911 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010912 }
Evan Cheng2150b922007-03-12 23:30:29 +000010913 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010914 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010915}
10916
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010917/// isLegalICmpImmediate - Return true if the specified immediate is legal
10918/// icmp immediate, that is the target has icmp instructions which can compare
10919/// a register against the immediate without having to materialize the
10920/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010921bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010922 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010923 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010924 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010925 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010926 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010927 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010928 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010929}
10930
Andrew Tricka22cdb72012-07-18 18:34:27 +000010931/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10932/// *or sub* immediate, that is the target has add or sub instructions which can
10933/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010934/// immediate into a register.
10935bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010936 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010937 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010938 if (!Subtarget->isThumb())
10939 return ARM_AM::getSOImmVal(AbsImm) != -1;
10940 if (Subtarget->isThumb2())
10941 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10942 // Thumb1 only has 8-bit unsigned immediate.
10943 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010944}
10945
Owen Anderson53aa7a92009-08-10 22:56:29 +000010946static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010947 bool isSEXTLoad, SDValue &Base,
10948 SDValue &Offset, bool &isInc,
10949 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010950 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10951 return false;
10952
Owen Anderson9f944592009-08-11 20:47:22 +000010953 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010954 // AddressingMode 3
10955 Base = Ptr->getOperand(0);
10956 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010957 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010958 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010959 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010960 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010961 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010962 return true;
10963 }
10964 }
10965 isInc = (Ptr->getOpcode() == ISD::ADD);
10966 Offset = Ptr->getOperand(1);
10967 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010968 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010969 // AddressingMode 2
10970 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010971 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010972 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010973 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010974 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010975 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010976 Base = Ptr->getOperand(0);
10977 return true;
10978 }
10979 }
10980
10981 if (Ptr->getOpcode() == ISD::ADD) {
10982 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010983 ARM_AM::ShiftOpc ShOpcVal=
10984 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010985 if (ShOpcVal != ARM_AM::no_shift) {
10986 Base = Ptr->getOperand(1);
10987 Offset = Ptr->getOperand(0);
10988 } else {
10989 Base = Ptr->getOperand(0);
10990 Offset = Ptr->getOperand(1);
10991 }
10992 return true;
10993 }
10994
10995 isInc = (Ptr->getOpcode() == ISD::ADD);
10996 Base = Ptr->getOperand(0);
10997 Offset = Ptr->getOperand(1);
10998 return true;
10999 }
11000
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011001 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011002 return false;
11003}
11004
Owen Anderson53aa7a92009-08-10 22:56:29 +000011005static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011006 bool isSEXTLoad, SDValue &Base,
11007 SDValue &Offset, bool &isInc,
11008 SelectionDAG &DAG) {
11009 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11010 return false;
11011
11012 Base = Ptr->getOperand(0);
11013 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11014 int RHSC = (int)RHS->getZExtValue();
11015 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11016 assert(Ptr->getOpcode() == ISD::ADD);
11017 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011018 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011019 return true;
11020 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11021 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011022 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011023 return true;
11024 }
11025 }
11026
11027 return false;
11028}
11029
Evan Cheng10043e22007-01-19 07:51:42 +000011030/// getPreIndexedAddressParts - returns true by value, base pointer and
11031/// offset pointer and addressing mode by reference if the node's address
11032/// can be legally represented as pre-indexed load / store address.
11033bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011034ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11035 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011036 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011037 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011038 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011039 return false;
11040
Owen Anderson53aa7a92009-08-10 22:56:29 +000011041 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011042 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011043 bool isSEXTLoad = false;
11044 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11045 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011046 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011047 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11048 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11049 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011050 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011051 } else
11052 return false;
11053
11054 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011055 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011056 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011057 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11058 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011059 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011060 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011061 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011062 if (!isLegal)
11063 return false;
11064
11065 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11066 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011067}
11068
11069/// getPostIndexedAddressParts - returns true by value, base pointer and
11070/// offset pointer and addressing mode by reference if this node can be
11071/// combined with a load / store to form a post-indexed load / store.
11072bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011073 SDValue &Base,
11074 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011075 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011076 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011077 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011078 return false;
11079
Owen Anderson53aa7a92009-08-10 22:56:29 +000011080 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011081 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011082 bool isSEXTLoad = false;
11083 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011084 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011085 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011086 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11087 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011088 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011089 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011090 } else
11091 return false;
11092
11093 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011094 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011095 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011096 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011097 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011098 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011099 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11100 isInc, DAG);
11101 if (!isLegal)
11102 return false;
11103
Evan Chengf19384d2010-05-18 21:31:17 +000011104 if (Ptr != Base) {
11105 // Swap base ptr and offset to catch more post-index load / store when
11106 // it's legal. In Thumb2 mode, offset must be an immediate.
11107 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11108 !Subtarget->isThumb2())
11109 std::swap(Base, Offset);
11110
11111 // Post-indexed load / store update the base pointer.
11112 if (Ptr != Base)
11113 return false;
11114 }
11115
Evan Cheng84c6cda2009-07-02 07:28:31 +000011116 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11117 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011118}
11119
Jay Foada0653a32014-05-14 21:14:37 +000011120void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11121 APInt &KnownZero,
11122 APInt &KnownOne,
11123 const SelectionDAG &DAG,
11124 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011125 unsigned BitWidth = KnownOne.getBitWidth();
11126 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011127 switch (Op.getOpcode()) {
11128 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011129 case ARMISD::ADDC:
11130 case ARMISD::ADDE:
11131 case ARMISD::SUBC:
11132 case ARMISD::SUBE:
11133 // These nodes' second result is a boolean
11134 if (Op.getResNo() == 0)
11135 break;
11136 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11137 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011138 case ARMISD::CMOV: {
11139 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011140 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011141 if (KnownZero == 0 && KnownOne == 0) return;
11142
Dan Gohmanf990faf2008-02-13 00:35:47 +000011143 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011144 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011145 KnownZero &= KnownZeroRHS;
11146 KnownOne &= KnownOneRHS;
11147 return;
11148 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011149 case ISD::INTRINSIC_W_CHAIN: {
11150 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11151 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11152 switch (IntID) {
11153 default: return;
11154 case Intrinsic::arm_ldaex:
11155 case Intrinsic::arm_ldrex: {
11156 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11157 unsigned MemBits = VT.getScalarType().getSizeInBits();
11158 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11159 return;
11160 }
11161 }
11162 }
Evan Cheng10043e22007-01-19 07:51:42 +000011163 }
11164}
11165
11166//===----------------------------------------------------------------------===//
11167// ARM Inline Assembly Support
11168//===----------------------------------------------------------------------===//
11169
Evan Cheng078b0b02011-01-08 01:24:27 +000011170bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11171 // Looking for "rev" which is V6+.
11172 if (!Subtarget->hasV6Ops())
11173 return false;
11174
11175 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11176 std::string AsmStr = IA->getAsmString();
11177 SmallVector<StringRef, 4> AsmPieces;
11178 SplitString(AsmStr, AsmPieces, ";\n");
11179
11180 switch (AsmPieces.size()) {
11181 default: return false;
11182 case 1:
11183 AsmStr = AsmPieces[0];
11184 AsmPieces.clear();
11185 SplitString(AsmStr, AsmPieces, " \t,");
11186
11187 // rev $0, $1
11188 if (AsmPieces.size() == 3 &&
11189 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11190 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011191 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011192 if (Ty && Ty->getBitWidth() == 32)
11193 return IntrinsicLowering::LowerToByteSwap(CI);
11194 }
11195 break;
11196 }
11197
11198 return false;
11199}
11200
Evan Cheng10043e22007-01-19 07:51:42 +000011201/// getConstraintType - Given a constraint letter, return the type of
11202/// constraint it is for this target.
11203ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011204ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011205 if (Constraint.size() == 1) {
11206 switch (Constraint[0]) {
11207 default: break;
11208 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011209 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011210 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011211 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011212 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011213 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011214 // An address with a single base register. Due to the way we
11215 // currently handle addresses it is the same as an 'r' memory constraint.
11216 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011217 }
Eric Christophere256cd02011-06-21 22:10:57 +000011218 } else if (Constraint.size() == 2) {
11219 switch (Constraint[0]) {
11220 default: break;
11221 // All 'U+' constraints are addresses.
11222 case 'U': return C_Memory;
11223 }
Evan Cheng10043e22007-01-19 07:51:42 +000011224 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011225 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011226}
11227
John Thompsone8360b72010-10-29 17:29:13 +000011228/// Examine constraint type and operand type and determine a weight value.
11229/// This object must already have been set up with the operand type
11230/// and the current alternative constraint selected.
11231TargetLowering::ConstraintWeight
11232ARMTargetLowering::getSingleConstraintMatchWeight(
11233 AsmOperandInfo &info, const char *constraint) const {
11234 ConstraintWeight weight = CW_Invalid;
11235 Value *CallOperandVal = info.CallOperandVal;
11236 // If we don't have a value, we can't do a match,
11237 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011238 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011239 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011240 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011241 // Look at the constraint type.
11242 switch (*constraint) {
11243 default:
11244 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11245 break;
11246 case 'l':
11247 if (type->isIntegerTy()) {
11248 if (Subtarget->isThumb())
11249 weight = CW_SpecificReg;
11250 else
11251 weight = CW_Register;
11252 }
11253 break;
11254 case 'w':
11255 if (type->isFloatingPointTy())
11256 weight = CW_Register;
11257 break;
11258 }
11259 return weight;
11260}
11261
Eric Christophercf2007c2011-06-30 23:50:52 +000011262typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011263RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11264 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011265 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011266 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011267 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011268 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011269 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011270 return RCPair(0U, &ARM::tGPRRegClass);
11271 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011272 case 'h': // High regs or no regs.
11273 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011274 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011275 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011276 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011277 if (Subtarget->isThumb1Only())
11278 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011279 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011280 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011281 if (VT == MVT::Other)
11282 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011283 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011284 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011285 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011286 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011287 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011288 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011289 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011290 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011291 if (VT == MVT::Other)
11292 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011293 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011294 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011295 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011296 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011297 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011298 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011299 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011300 case 't':
11301 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011302 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011303 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011304 }
11305 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011306 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011307 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011308
Eric Christopher11e4df72015-02-26 22:38:43 +000011309 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011310}
11311
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011312/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11313/// vector. If it is invalid, don't add anything to Ops.
11314void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011315 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011316 std::vector<SDValue>&Ops,
11317 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011318 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011319
Eric Christopherde9399b2011-06-02 23:16:42 +000011320 // Currently only support length 1 constraints.
11321 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011322
Eric Christopherde9399b2011-06-02 23:16:42 +000011323 char ConstraintLetter = Constraint[0];
11324 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011325 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011326 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011327 case 'I': case 'J': case 'K': case 'L':
11328 case 'M': case 'N': case 'O':
11329 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11330 if (!C)
11331 return;
11332
11333 int64_t CVal64 = C->getSExtValue();
11334 int CVal = (int) CVal64;
11335 // None of these constraints allow values larger than 32 bits. Check
11336 // that the value fits in an int.
11337 if (CVal != CVal64)
11338 return;
11339
Eric Christopherde9399b2011-06-02 23:16:42 +000011340 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011341 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011342 // Constant suitable for movw, must be between 0 and
11343 // 65535.
11344 if (Subtarget->hasV6T2Ops())
11345 if (CVal >= 0 && CVal <= 65535)
11346 break;
11347 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011348 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011349 if (Subtarget->isThumb1Only()) {
11350 // This must be a constant between 0 and 255, for ADD
11351 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011352 if (CVal >= 0 && CVal <= 255)
11353 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011354 } else if (Subtarget->isThumb2()) {
11355 // A constant that can be used as an immediate value in a
11356 // data-processing instruction.
11357 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11358 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011359 } else {
11360 // A constant that can be used as an immediate value in a
11361 // data-processing instruction.
11362 if (ARM_AM::getSOImmVal(CVal) != -1)
11363 break;
11364 }
11365 return;
11366
11367 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000011368 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011369 // This must be a constant between -255 and -1, for negated ADD
11370 // immediates. This can be used in GCC with an "n" modifier that
11371 // prints the negated value, for use with SUB instructions. It is
11372 // not useful otherwise but is implemented for compatibility.
11373 if (CVal >= -255 && CVal <= -1)
11374 break;
11375 } else {
11376 // This must be a constant between -4095 and 4095. It is not clear
11377 // what this constraint is intended for. Implemented for
11378 // compatibility with GCC.
11379 if (CVal >= -4095 && CVal <= 4095)
11380 break;
11381 }
11382 return;
11383
11384 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011385 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011386 // A 32-bit value where only one byte has a nonzero value. Exclude
11387 // zero to match GCC. This constraint is used by GCC internally for
11388 // constants that can be loaded with a move/shift combination.
11389 // It is not useful otherwise but is implemented for compatibility.
11390 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11391 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011392 } else if (Subtarget->isThumb2()) {
11393 // A constant whose bitwise inverse can be used as an immediate
11394 // value in a data-processing instruction. This can be used in GCC
11395 // with a "B" modifier that prints the inverted value, for use with
11396 // BIC and MVN instructions. It is not useful otherwise but is
11397 // implemented for compatibility.
11398 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11399 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011400 } else {
11401 // A constant whose bitwise inverse can be used as an immediate
11402 // value in a data-processing instruction. This can be used in GCC
11403 // with a "B" modifier that prints the inverted value, for use with
11404 // BIC and MVN instructions. It is not useful otherwise but is
11405 // implemented for compatibility.
11406 if (ARM_AM::getSOImmVal(~CVal) != -1)
11407 break;
11408 }
11409 return;
11410
11411 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011412 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011413 // This must be a constant between -7 and 7,
11414 // for 3-operand ADD/SUB immediate instructions.
11415 if (CVal >= -7 && CVal < 7)
11416 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011417 } else if (Subtarget->isThumb2()) {
11418 // A constant whose negation can be used as an immediate value in a
11419 // data-processing instruction. This can be used in GCC with an "n"
11420 // modifier that prints the negated value, for use with SUB
11421 // instructions. It is not useful otherwise but is implemented for
11422 // compatibility.
11423 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11424 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011425 } else {
11426 // A constant whose negation can be used as an immediate value in a
11427 // data-processing instruction. This can be used in GCC with an "n"
11428 // modifier that prints the negated value, for use with SUB
11429 // instructions. It is not useful otherwise but is implemented for
11430 // compatibility.
11431 if (ARM_AM::getSOImmVal(-CVal) != -1)
11432 break;
11433 }
11434 return;
11435
11436 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000011437 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011438 // This must be a multiple of 4 between 0 and 1020, for
11439 // ADD sp + immediate.
11440 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11441 break;
11442 } else {
11443 // A power of two or a constant between 0 and 32. This is used in
11444 // GCC for the shift amount on shifted register operands, but it is
11445 // useful in general for any shift amounts.
11446 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11447 break;
11448 }
11449 return;
11450
11451 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011452 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011453 // This must be a constant between 0 and 31, for shift amounts.
11454 if (CVal >= 0 && CVal <= 31)
11455 break;
11456 }
11457 return;
11458
11459 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011460 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011461 // This must be a multiple of 4 between -508 and 508, for
11462 // ADD/SUB sp = sp + immediate.
11463 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11464 break;
11465 }
11466 return;
11467 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011468 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011469 break;
11470 }
11471
11472 if (Result.getNode()) {
11473 Ops.push_back(Result);
11474 return;
11475 }
Dale Johannesence97d552010-06-25 21:55:36 +000011476 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011477}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011478
Scott Douglassd2974a62015-08-24 09:17:11 +000011479static RTLIB::Libcall getDivRemLibcall(
11480 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011481 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11482 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011483 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011484 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11485 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011486 RTLIB::Libcall LC;
11487 switch (SVT) {
11488 default: llvm_unreachable("Unexpected request for libcall!");
11489 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11490 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11491 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11492 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11493 }
11494 return LC;
11495}
11496
11497static TargetLowering::ArgListTy getDivRemArgList(
11498 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011499 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11500 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011501 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011502 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11503 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011504 TargetLowering::ArgListTy Args;
11505 TargetLowering::ArgListEntry Entry;
11506 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11507 EVT ArgVT = N->getOperand(i).getValueType();
11508 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11509 Entry.Node = N->getOperand(i);
11510 Entry.Ty = ArgTy;
11511 Entry.isSExt = isSigned;
11512 Entry.isZExt = !isSigned;
11513 Args.push_back(Entry);
11514 }
11515 return Args;
11516}
11517
Renato Golin87610692013-07-16 09:32:17 +000011518SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011519 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) &&
11520 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011521 unsigned Opcode = Op->getOpcode();
11522 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011523 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011524 bool isSigned = (Opcode == ISD::SDIVREM);
11525 EVT VT = Op->getValueType(0);
11526 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11527
Scott Douglassd2974a62015-08-24 09:17:11 +000011528 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11529 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011530 SDValue InChain = DAG.getEntryNode();
11531
Scott Douglassd2974a62015-08-24 09:17:11 +000011532 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11533 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011534
11535 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011536 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011537
Reid Kleckner343c3952014-11-20 23:51:47 +000011538 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011539
11540 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011541 TargetLowering::CallLoweringInfo CLI(DAG);
11542 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011543 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011544 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011545
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011546 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011547 return CallInfo.first;
11548}
11549
Scott Douglassbdef6042015-08-24 09:17:18 +000011550// Lowers REM using divmod helpers
11551// see RTABI section 4.2/4.3
11552SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11553 // Build return types (div and rem)
11554 std::vector<Type*> RetTyParams;
11555 Type *RetTyElement;
11556
11557 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11558 default: llvm_unreachable("Unexpected request for libcall!");
11559 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11560 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11561 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11562 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11563 }
11564
11565 RetTyParams.push_back(RetTyElement);
11566 RetTyParams.push_back(RetTyElement);
11567 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11568 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11569
11570 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11571 SimpleTy);
11572 SDValue InChain = DAG.getEntryNode();
11573 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11574 bool isSigned = N->getOpcode() == ISD::SREM;
11575 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11576 getPointerTy(DAG.getDataLayout()));
11577
11578 // Lower call
11579 CallLoweringInfo CLI(DAG);
11580 CLI.setChain(InChain)
11581 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11582 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11583 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11584
11585 // Return second (rem) result operand (first contains div)
11586 SDNode *ResNode = CallResult.first.getNode();
11587 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11588 return ResNode->getOperand(1);
11589}
11590
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011591SDValue
11592ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11593 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11594 SDLoc DL(Op);
11595
11596 // Get the inputs.
11597 SDValue Chain = Op.getOperand(0);
11598 SDValue Size = Op.getOperand(1);
11599
11600 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011601 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011602
11603 SDValue Flag;
11604 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11605 Flag = Chain.getValue(1);
11606
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011607 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011608 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11609
11610 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11611 Chain = NewSP.getValue(1);
11612
11613 SDValue Ops[2] = { NewSP, Chain };
11614 return DAG.getMergeValues(Ops, DL);
11615}
11616
Oliver Stannard51b1d462014-08-21 12:50:31 +000011617SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11618 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11619 "Unexpected type for custom-lowering FP_EXTEND");
11620
11621 RTLIB::Libcall LC;
11622 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11623
11624 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011625 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11626 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011627}
11628
11629SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11630 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11631 Subtarget->isFPOnlySP() &&
11632 "Unexpected type for custom-lowering FP_ROUND");
11633
11634 RTLIB::Libcall LC;
11635 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11636
11637 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011638 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11639 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011640}
11641
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011642bool
11643ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11644 // The ARM target isn't yet aware of offsets.
11645 return false;
11646}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011647
Jim Grosbach11013ed2010-07-16 23:05:05 +000011648bool ARM::isBitFieldInvertedMask(unsigned v) {
11649 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011650 return false;
11651
Jim Grosbach11013ed2010-07-16 23:05:05 +000011652 // there can be 1's on either or both "outsides", all the "inside"
11653 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011654 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011655}
11656
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011657/// isFPImmLegal - Returns true if the target can instruction select the
11658/// specified FP immediate natively. If false, the legalizer will
11659/// materialize the FP immediate as a load from a constant pool.
11660bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11661 if (!Subtarget->hasVFP3())
11662 return false;
11663 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011664 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011665 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011666 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011667 return false;
11668}
Bob Wilson5549d492010-09-21 17:56:22 +000011669
Wesley Peck527da1b2010-11-23 03:31:01 +000011670/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011671/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11672/// specified in the intrinsic calls.
11673bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11674 const CallInst &I,
11675 unsigned Intrinsic) const {
11676 switch (Intrinsic) {
11677 case Intrinsic::arm_neon_vld1:
11678 case Intrinsic::arm_neon_vld2:
11679 case Intrinsic::arm_neon_vld3:
11680 case Intrinsic::arm_neon_vld4:
11681 case Intrinsic::arm_neon_vld2lane:
11682 case Intrinsic::arm_neon_vld3lane:
11683 case Intrinsic::arm_neon_vld4lane: {
11684 Info.opc = ISD::INTRINSIC_W_CHAIN;
11685 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011686 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11687 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011688 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11689 Info.ptrVal = I.getArgOperand(0);
11690 Info.offset = 0;
11691 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11692 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11693 Info.vol = false; // volatile loads with NEON intrinsics not supported
11694 Info.readMem = true;
11695 Info.writeMem = false;
11696 return true;
11697 }
11698 case Intrinsic::arm_neon_vst1:
11699 case Intrinsic::arm_neon_vst2:
11700 case Intrinsic::arm_neon_vst3:
11701 case Intrinsic::arm_neon_vst4:
11702 case Intrinsic::arm_neon_vst2lane:
11703 case Intrinsic::arm_neon_vst3lane:
11704 case Intrinsic::arm_neon_vst4lane: {
11705 Info.opc = ISD::INTRINSIC_VOID;
11706 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011707 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011708 unsigned NumElts = 0;
11709 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011710 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011711 if (!ArgTy->isVectorTy())
11712 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011713 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011714 }
11715 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11716 Info.ptrVal = I.getArgOperand(0);
11717 Info.offset = 0;
11718 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11719 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11720 Info.vol = false; // volatile stores with NEON intrinsics not supported
11721 Info.readMem = false;
11722 Info.writeMem = true;
11723 return true;
11724 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011725 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011726 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011727 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011728 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11729 Info.opc = ISD::INTRINSIC_W_CHAIN;
11730 Info.memVT = MVT::getVT(PtrTy->getElementType());
11731 Info.ptrVal = I.getArgOperand(0);
11732 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011733 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011734 Info.vol = true;
11735 Info.readMem = true;
11736 Info.writeMem = false;
11737 return true;
11738 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011739 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011740 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011741 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011742 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11743 Info.opc = ISD::INTRINSIC_W_CHAIN;
11744 Info.memVT = MVT::getVT(PtrTy->getElementType());
11745 Info.ptrVal = I.getArgOperand(1);
11746 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011747 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011748 Info.vol = true;
11749 Info.readMem = false;
11750 Info.writeMem = true;
11751 return true;
11752 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011753 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011754 case Intrinsic::arm_strexd: {
11755 Info.opc = ISD::INTRINSIC_W_CHAIN;
11756 Info.memVT = MVT::i64;
11757 Info.ptrVal = I.getArgOperand(2);
11758 Info.offset = 0;
11759 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011760 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011761 Info.readMem = false;
11762 Info.writeMem = true;
11763 return true;
11764 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011765 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011766 case Intrinsic::arm_ldrexd: {
11767 Info.opc = ISD::INTRINSIC_W_CHAIN;
11768 Info.memVT = MVT::i64;
11769 Info.ptrVal = I.getArgOperand(0);
11770 Info.offset = 0;
11771 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011772 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011773 Info.readMem = true;
11774 Info.writeMem = false;
11775 return true;
11776 }
Bob Wilson5549d492010-09-21 17:56:22 +000011777 default:
11778 break;
11779 }
11780
11781 return false;
11782}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011783
11784/// \brief Returns true if it is beneficial to convert a load of a constant
11785/// to just the constant itself.
11786bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11787 Type *Ty) const {
11788 assert(Ty->isIntegerTy());
11789
11790 unsigned Bits = Ty->getPrimitiveSizeInBits();
11791 if (Bits == 0 || Bits > 32)
11792 return false;
11793 return true;
11794}
Tim Northover037f26f22014-04-17 18:22:47 +000011795
Robin Morisset5349e8e2014-09-18 18:56:04 +000011796Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11797 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011798 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011799
11800 // First, if the target has no DMB, see what fallback we can use.
11801 if (!Subtarget->hasDataBarrier()) {
11802 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11803 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11804 // here.
11805 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11806 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11807 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11808 Builder.getInt32(0), Builder.getInt32(7),
11809 Builder.getInt32(10), Builder.getInt32(5)};
11810 return Builder.CreateCall(MCR, args);
11811 } else {
11812 // Instead of using barriers, atomic accesses on these subtargets use
11813 // libcalls.
11814 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11815 }
11816 } else {
11817 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11818 // Only a full system barrier exists in the M-class architectures.
11819 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11820 Constant *CDomain = Builder.getInt32(Domain);
11821 return Builder.CreateCall(DMB, CDomain);
11822 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011823}
11824
11825// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011826Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011827 AtomicOrdering Ord, bool IsStore,
11828 bool IsLoad) const {
11829 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011830 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011831
11832 switch (Ord) {
11833 case NotAtomic:
11834 case Unordered:
11835 llvm_unreachable("Invalid fence: unordered/non-atomic");
11836 case Monotonic:
11837 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011838 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011839 case SequentiallyConsistent:
11840 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011841 return nullptr; // Nothing to do
11842 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011843 case Release:
11844 case AcquireRelease:
11845 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011846 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011847 // FIXME: add a comment with a link to documentation justifying this.
11848 else
Robin Morissetdedef332014-09-23 20:31:14 +000011849 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011850 }
Robin Morissetdedef332014-09-23 20:31:14 +000011851 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011852}
11853
Robin Morissetdedef332014-09-23 20:31:14 +000011854Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011855 AtomicOrdering Ord, bool IsStore,
11856 bool IsLoad) const {
11857 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011858 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011859
11860 switch (Ord) {
11861 case NotAtomic:
11862 case Unordered:
11863 llvm_unreachable("Invalid fence: unordered/not-atomic");
11864 case Monotonic:
11865 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011866 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011867 case Acquire:
11868 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011869 case SequentiallyConsistent:
11870 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011871 }
Robin Morissetdedef332014-09-23 20:31:14 +000011872 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011873}
11874
Robin Morisseted3d48f2014-09-03 21:29:59 +000011875// Loads and stores less than 64-bits are already atomic; ones above that
11876// are doomed anyway, so defer to the default libcall and blame the OS when
11877// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11878// anything for those.
11879bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11880 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11881 return (Size == 64) && !Subtarget->isMClass();
11882}
Tim Northover037f26f22014-04-17 18:22:47 +000011883
Robin Morisseted3d48f2014-09-03 21:29:59 +000011884// Loads and stores less than 64-bits are already atomic; ones above that
11885// are doomed anyway, so defer to the default libcall and blame the OS when
11886// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11887// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011888// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11889// guarantee, see DDI0406C ARM architecture reference manual,
11890// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000011891TargetLowering::AtomicExpansionKind
11892ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011893 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Ahmed Bougacha52468672015-09-11 17:08:28 +000011894 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLSC
11895 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000011896}
11897
11898// For the real atomic operations, we have ldrex/strex up to 32 bits,
11899// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000011900TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000011901ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011902 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011903 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000011904 ? AtomicExpansionKind::LLSC
11905 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011906}
11907
Ahmed Bougacha52468672015-09-11 17:08:28 +000011908bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
11909 AtomicCmpXchgInst *AI) const {
11910 return true;
11911}
11912
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011913// This has so far only been implemented for MachO.
11914bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011915 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011916}
11917
Quentin Colombetc32615d2014-10-31 17:52:53 +000011918bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11919 unsigned &Cost) const {
11920 // If we do not have NEON, vector types are not natively supported.
11921 if (!Subtarget->hasNEON())
11922 return false;
11923
11924 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000011925 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000011926 // better to leave at float as we have more freedom in the addressing mode for
11927 // those.
11928 if (VectorTy->isFPOrFPVectorTy())
11929 return false;
11930
11931 // If the index is unknown at compile time, this is very expensive to lower
11932 // and it is not possible to combine the store with the extract.
11933 if (!isa<ConstantInt>(Idx))
11934 return false;
11935
11936 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11937 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11938 // We can do a store + vector extract on any vector that fits perfectly in a D
11939 // or Q register.
11940 if (BitWidth == 64 || BitWidth == 128) {
11941 Cost = 0;
11942 return true;
11943 }
11944 return false;
11945}
11946
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000011947bool ARMTargetLowering::isCheapToSpeculateCttz() const {
11948 return Subtarget->hasV6T2Ops();
11949}
11950
11951bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
11952 return Subtarget->hasV6T2Ops();
11953}
11954
Tim Northover037f26f22014-04-17 18:22:47 +000011955Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11956 AtomicOrdering Ord) const {
11957 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11958 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011959 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011960
11961 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11962 // intrinsic must return {i32, i32} and we have to recombine them into a
11963 // single i64 here.
11964 if (ValTy->getPrimitiveSizeInBits() == 64) {
11965 Intrinsic::ID Int =
11966 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11967 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11968
11969 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11970 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11971
11972 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11973 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011974 if (!Subtarget->isLittle())
11975 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011976 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11977 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11978 return Builder.CreateOr(
11979 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11980 }
11981
11982 Type *Tys[] = { Addr->getType() };
11983 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11984 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11985
11986 return Builder.CreateTruncOrBitCast(
11987 Builder.CreateCall(Ldrex, Addr),
11988 cast<PointerType>(Addr->getType())->getElementType());
11989}
11990
Ahmed Bougacha81616a72015-09-22 17:22:58 +000011991void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
11992 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000011993 if (!Subtarget->hasV7Ops())
11994 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000011995 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11996 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
11997}
11998
Tim Northover037f26f22014-04-17 18:22:47 +000011999Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12000 Value *Addr,
12001 AtomicOrdering Ord) const {
12002 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000012003 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012004
12005 // Since the intrinsics must have legal type, the i64 intrinsics take two
12006 // parameters: "i32, i32". We must marshal Val into the appropriate form
12007 // before the call.
12008 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12009 Intrinsic::ID Int =
12010 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12011 Function *Strex = Intrinsic::getDeclaration(M, Int);
12012 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12013
12014 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12015 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012016 if (!Subtarget->isLittle())
12017 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012018 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012019 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012020 }
12021
12022 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12023 Type *Tys[] = { Addr->getType() };
12024 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12025
David Blaikieff6409d2015-05-18 22:13:54 +000012026 return Builder.CreateCall(
12027 Strex, {Builder.CreateZExtOrBitCast(
12028 Val, Strex->getFunctionType()->getParamType(0)),
12029 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012030}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012031
Hao Liu2cd34bb2015-06-26 02:45:36 +000012032/// \brief Lower an interleaved load into a vldN intrinsic.
12033///
12034/// E.g. Lower an interleaved load (Factor = 2):
12035/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12036/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12037/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12038///
12039/// Into:
12040/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12041/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12042/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12043bool ARMTargetLowering::lowerInterleavedLoad(
12044 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12045 ArrayRef<unsigned> Indices, unsigned Factor) const {
12046 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12047 "Invalid interleave factor");
12048 assert(!Shuffles.empty() && "Empty shufflevector input");
12049 assert(Shuffles.size() == Indices.size() &&
12050 "Unmatched number of shufflevectors and indices");
12051
12052 VectorType *VecTy = Shuffles[0]->getType();
12053 Type *EltTy = VecTy->getVectorElementType();
12054
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012055 const DataLayout &DL = LI->getModule()->getDataLayout();
12056 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
12057 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012058
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012059 // Skip if we do not have NEON and skip illegal vector types and vector types
12060 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12061 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012062 return false;
12063
12064 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12065 // load integer vectors first and then convert to pointer vectors.
12066 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012067 VecTy =
12068 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012069
12070 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12071 Intrinsic::arm_neon_vld3,
12072 Intrinsic::arm_neon_vld4};
12073
Hao Liu2cd34bb2015-06-26 02:45:36 +000012074 IRBuilder<> Builder(LI);
12075 SmallVector<Value *, 2> Ops;
12076
12077 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12078 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12079 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12080
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012081 Type *Tys[] = { VecTy, Int8Ptr };
12082 Function *VldnFunc =
12083 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012084 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12085
12086 // Replace uses of each shufflevector with the corresponding vector loaded
12087 // by ldN.
12088 for (unsigned i = 0; i < Shuffles.size(); i++) {
12089 ShuffleVectorInst *SV = Shuffles[i];
12090 unsigned Index = Indices[i];
12091
12092 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12093
12094 // Convert the integer vector to pointer vector if the element is pointer.
12095 if (EltTy->isPointerTy())
12096 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12097
12098 SV->replaceAllUsesWith(SubVec);
12099 }
12100
12101 return true;
12102}
12103
12104/// \brief Get a mask consisting of sequential integers starting from \p Start.
12105///
12106/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12107static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12108 unsigned NumElts) {
12109 SmallVector<Constant *, 16> Mask;
12110 for (unsigned i = 0; i < NumElts; i++)
12111 Mask.push_back(Builder.getInt32(Start + i));
12112
12113 return ConstantVector::get(Mask);
12114}
12115
12116/// \brief Lower an interleaved store into a vstN intrinsic.
12117///
12118/// E.g. Lower an interleaved store (Factor = 3):
12119/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12120/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12121/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12122///
12123/// Into:
12124/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12125/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12126/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12127/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12128///
12129/// Note that the new shufflevectors will be removed and we'll only generate one
12130/// vst3 instruction in CodeGen.
12131bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12132 ShuffleVectorInst *SVI,
12133 unsigned Factor) const {
12134 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12135 "Invalid interleave factor");
12136
12137 VectorType *VecTy = SVI->getType();
12138 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12139 "Invalid interleaved store");
12140
12141 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12142 Type *EltTy = VecTy->getVectorElementType();
12143 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12144
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012145 const DataLayout &DL = SI->getModule()->getDataLayout();
12146 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
12147 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012148
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012149 // Skip if we do not have NEON and skip illegal vector types and vector types
12150 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12151 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12152 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012153 return false;
12154
12155 Value *Op0 = SVI->getOperand(0);
12156 Value *Op1 = SVI->getOperand(1);
12157 IRBuilder<> Builder(SI);
12158
12159 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12160 // vectors to integer vectors.
12161 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012162 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012163
12164 // Convert to the corresponding integer vector.
12165 Type *IntVecTy =
12166 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12167 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12168 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12169
12170 SubVecTy = VectorType::get(IntTy, NumSubElts);
12171 }
12172
Craig Topper26260942015-10-18 05:15:34 +000012173 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12174 Intrinsic::arm_neon_vst3,
12175 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012176 SmallVector<Value *, 6> Ops;
12177
12178 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12179 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12180
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012181 Type *Tys[] = { Int8Ptr, SubVecTy };
12182 Function *VstNFunc = Intrinsic::getDeclaration(
12183 SI->getModule(), StoreInts[Factor - 2], Tys);
12184
Hao Liu2cd34bb2015-06-26 02:45:36 +000012185 // Split the shufflevector operands into sub vectors for the new vstN call.
12186 for (unsigned i = 0; i < Factor; i++)
12187 Ops.push_back(Builder.CreateShuffleVector(
12188 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12189
12190 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12191 Builder.CreateCall(VstNFunc, Ops);
12192 return true;
12193}
12194
Oliver Stannardc24f2172014-05-09 14:01:47 +000012195enum HABaseType {
12196 HA_UNKNOWN = 0,
12197 HA_FLOAT,
12198 HA_DOUBLE,
12199 HA_VECT64,
12200 HA_VECT128
12201};
12202
12203static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12204 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012205 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012206 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12207 uint64_t SubMembers = 0;
12208 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12209 return false;
12210 Members += SubMembers;
12211 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012212 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012213 uint64_t SubMembers = 0;
12214 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12215 return false;
12216 Members += SubMembers * AT->getNumElements();
12217 } else if (Ty->isFloatTy()) {
12218 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12219 return false;
12220 Members = 1;
12221 Base = HA_FLOAT;
12222 } else if (Ty->isDoubleTy()) {
12223 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12224 return false;
12225 Members = 1;
12226 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012227 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012228 Members = 1;
12229 switch (Base) {
12230 case HA_FLOAT:
12231 case HA_DOUBLE:
12232 return false;
12233 case HA_VECT64:
12234 return VT->getBitWidth() == 64;
12235 case HA_VECT128:
12236 return VT->getBitWidth() == 128;
12237 case HA_UNKNOWN:
12238 switch (VT->getBitWidth()) {
12239 case 64:
12240 Base = HA_VECT64;
12241 return true;
12242 case 128:
12243 Base = HA_VECT128;
12244 return true;
12245 default:
12246 return false;
12247 }
12248 }
12249 }
12250
12251 return (Members > 0 && Members <= 4);
12252}
12253
Tim Northovere95c5b32015-02-24 17:22:34 +000012254/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12255/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12256/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012257bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12258 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012259 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12260 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012261 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012262
12263 HABaseType Base = HA_UNKNOWN;
12264 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012265 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12266 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12267
12268 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12269 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012270}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012271
12272unsigned ARMTargetLowering::getExceptionPointerRegister(
12273 const Constant *PersonalityFn) const {
12274 // Platforms which do not use SjLj EH may return values in these registers
12275 // via the personality function.
12276 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12277}
12278
12279unsigned ARMTargetLowering::getExceptionSelectorRegister(
12280 const Constant *PersonalityFn) const {
12281 // Platforms which do not use SjLj EH may return values in these registers
12282 // via the personality function.
12283 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12284}