blob: 3c66d56b6a3a30b72d3ed755897bfa45fce97c9d [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 }
Evan Cheng10043e22007-01-19 07:51:42 +0000245 }
246
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000247 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000248 setLibcallName(RTLIB::SHL_I128, nullptr);
249 setLibcallName(RTLIB::SRL_I128, nullptr);
250 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000251
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000252 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
253 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000254 static const struct {
255 const RTLIB::Libcall Op;
256 const char * const Name;
257 const CallingConv::ID CC;
258 const ISD::CondCode Cond;
259 } LibraryCalls[] = {
260 // Double-precision floating-point arithmetic helper functions
261 // RTABI chapter 4.1.2, Table 2
262 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
263 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
264 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
265 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000266
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000267 // Double-precision floating-point comparison helper functions
268 // RTABI chapter 4.1.2, Table 3
269 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
270 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
271 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
272 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000277
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000278 // Single-precision floating-point arithmetic helper functions
279 // RTABI chapter 4.1.2, Table 4
280 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
281 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
282 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
283 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000284
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000285 // Single-precision floating-point comparison helper functions
286 // RTABI chapter 4.1.2, Table 5
287 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
288 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
289 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
290 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000295
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000296 // Floating-point to integer conversions.
297 // RTABI chapter 4.1.2, Table 6
298 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000306
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000307 // Conversions between floating types.
308 // RTABI chapter 4.1.2, Table 7
309 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000310 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000311 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000312
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000313 // Integer to floating-point conversions.
314 // RTABI chapter 4.1.2, Table 8
315 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000323
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000324 // Long long helper functions
325 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000326 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
329 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000330
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000331 // Integer division functions
332 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000333 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000341
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000342 // Memory operations
343 // RTABI chapter 4.3.4
344 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
347 };
348
349 for (const auto &LC : LibraryCalls) {
350 setLibcallName(LC.Op, LC.Name);
351 setLibcallCallingConv(LC.Op, LC.CC);
352 if (LC.Cond != ISD::SETCC_INVALID)
353 setCmpLibcallCC(LC.Op, LC.Cond);
354 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000355 }
356
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000357 if (Subtarget->isTargetWindows()) {
358 static const struct {
359 const RTLIB::Libcall Op;
360 const char * const Name;
361 const CallingConv::ID CC;
362 } LibraryCalls[] = {
363 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool0a2672b2015-08-04 03:57:56 +0000371
372 { RTLIB::SDIV_I32, "__rt_sdiv", CallingConv::ARM_AAPCS_VFP },
373 { RTLIB::UDIV_I32, "__rt_udiv", CallingConv::ARM_AAPCS_VFP },
374 { RTLIB::SDIV_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS_VFP },
375 { RTLIB::UDIV_I64, "__rt_udiv64", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000376 };
377
378 for (const auto &LC : LibraryCalls) {
379 setLibcallName(LC.Op, LC.Name);
380 setLibcallCallingConv(LC.Op, LC.CC);
381 }
382 }
383
Bob Wilsonbc158992011-10-07 16:59:21 +0000384 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000385 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000386 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
387 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
388 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
389 }
390
Oliver Stannard11790b22014-08-11 09:12:32 +0000391 // The half <-> float conversion functions are always soft-float, but are
392 // needed for some targets which use a hard-float calling convention by
393 // default.
394 if (Subtarget->isAAPCS_ABI()) {
395 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
396 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
397 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
398 } else {
399 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
400 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
401 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
402 }
403
David Goodwin22c2fba2009-07-08 23:10:31 +0000404 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000405 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000406 else
Craig Topperc7242e02012-04-20 07:30:17 +0000407 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000408 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000409 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000410 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000411 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000412 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000413
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000414 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000415 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000416 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000417 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
418 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
419 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
420 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000421
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000422 setOperationAction(ISD::MULHS, VT, Expand);
423 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
424 setOperationAction(ISD::MULHU, VT, Expand);
425 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000426
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000427 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000428 }
429
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000430 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000431 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000432
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000433 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
434 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
435
Bob Wilson2e076c42009-06-22 23:27:02 +0000436 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000437 addDRTypeForNEON(MVT::v2f32);
438 addDRTypeForNEON(MVT::v8i8);
439 addDRTypeForNEON(MVT::v4i16);
440 addDRTypeForNEON(MVT::v2i32);
441 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000442
Owen Anderson9f944592009-08-11 20:47:22 +0000443 addQRTypeForNEON(MVT::v4f32);
444 addQRTypeForNEON(MVT::v2f64);
445 addQRTypeForNEON(MVT::v16i8);
446 addQRTypeForNEON(MVT::v8i16);
447 addQRTypeForNEON(MVT::v4i32);
448 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000449
Bob Wilson194a2512009-09-15 23:55:57 +0000450 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
451 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000452 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
453 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000454 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
455 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
456 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000457 // FIXME: Code duplication: FDIV and FREM are expanded always, see
458 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000459 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
460 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000461 // FIXME: Create unittest.
462 // In another words, find a way when "copysign" appears in DAG with vector
463 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000464 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000465 // FIXME: Code duplication: SETCC has custom operation action, see
466 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000467 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000468 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000469 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
470 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
471 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
472 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
473 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
474 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
475 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
476 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
477 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
478 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
479 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
480 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000481 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000482 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
483 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
484 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
485 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
486 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000487 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000488
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000489 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
490 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
491 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
492 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
493 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
494 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
495 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
496 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
497 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
498 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000499 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
500 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
501 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
502 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000503 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000504
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000505 // Mark v2f32 intrinsics.
506 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
507 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
508 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
509 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
510 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
511 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
512 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
513 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
514 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
515 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
516 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
517 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
518 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
519 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
520 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
521
Bob Wilson6cc46572009-09-16 00:32:15 +0000522 // Neon does not support some operations on v1i64 and v2i64 types.
523 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000524 // Custom handling for some quad-vector types to detect VMULL.
525 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
526 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
527 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000528 // Custom handling for some vector types to avoid expensive expansions
529 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
530 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
531 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
532 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000533 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
534 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000535 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000536 // a destination type that is wider than the source, and nor does
537 // it have a FP_TO_[SU]INT instruction with a narrower destination than
538 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000539 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
540 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000541 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
542 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000543
Eli Friedmane6385e62012-11-15 22:44:27 +0000544 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000545 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000546
Evan Chengb4eae132012-12-04 22:41:50 +0000547 // NEON does not have single instruction CTPOP for vectors with element
548 // types wider than 8-bits. However, custom lowering can leverage the
549 // v8i8/v16i8 vcnt instruction.
550 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
551 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
552 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
553 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
554
Logan Chien0a43abc2015-07-13 15:37:30 +0000555 // NEON does not have single instruction CTTZ for vectors.
556 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
557 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
558 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
559 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
560
561 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
562 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
563 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
564 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
565
566 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
567 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
568 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
569 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
570
571 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
572 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
573 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
574 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
575
Jim Grosbach5f215872013-02-27 21:31:12 +0000576 // NEON only has FMA instructions as of VFP4.
577 if (!Subtarget->hasVFP4()) {
578 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
579 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
580 }
581
Bob Wilson06fce872011-02-07 17:43:21 +0000582 setTargetDAGCombine(ISD::INTRINSIC_VOID);
583 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000584 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
585 setTargetDAGCombine(ISD::SHL);
586 setTargetDAGCombine(ISD::SRL);
587 setTargetDAGCombine(ISD::SRA);
588 setTargetDAGCombine(ISD::SIGN_EXTEND);
589 setTargetDAGCombine(ISD::ZERO_EXTEND);
590 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000591 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000592 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000593 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
594 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000595 setTargetDAGCombine(ISD::FP_TO_SINT);
596 setTargetDAGCombine(ISD::FP_TO_UINT);
597 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000598 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000599
James Molloy547d4c02012-02-20 09:24:05 +0000600 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000601 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
602 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000603 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000604 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
605 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
606 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000607 }
James Molloy547d4c02012-02-20 09:24:05 +0000608 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000609 }
610
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000611 // ARM and Thumb2 support UMLAL/SMLAL.
612 if (!Subtarget->isThumb1Only())
613 setTargetDAGCombine(ISD::ADDC);
614
Oliver Stannard51b1d462014-08-21 12:50:31 +0000615 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000616 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000617 // operations, f64 is legal for the few double-precision instructions which
618 // are present However, no double-precision operations other than moves,
619 // loads and stores are provided by the hardware.
620 setOperationAction(ISD::FADD, MVT::f64, Expand);
621 setOperationAction(ISD::FSUB, MVT::f64, Expand);
622 setOperationAction(ISD::FMUL, MVT::f64, Expand);
623 setOperationAction(ISD::FMA, MVT::f64, Expand);
624 setOperationAction(ISD::FDIV, MVT::f64, Expand);
625 setOperationAction(ISD::FREM, MVT::f64, Expand);
626 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
627 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
628 setOperationAction(ISD::FNEG, MVT::f64, Expand);
629 setOperationAction(ISD::FABS, MVT::f64, Expand);
630 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
631 setOperationAction(ISD::FSIN, MVT::f64, Expand);
632 setOperationAction(ISD::FCOS, MVT::f64, Expand);
633 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
634 setOperationAction(ISD::FPOW, MVT::f64, Expand);
635 setOperationAction(ISD::FLOG, MVT::f64, Expand);
636 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
637 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
638 setOperationAction(ISD::FEXP, MVT::f64, Expand);
639 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
640 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
641 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
642 setOperationAction(ISD::FRINT, MVT::f64, Expand);
643 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
644 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000645 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
646 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
647 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
648 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
649 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
650 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000651 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
652 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
653 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000654
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000655 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000656
Tim Northover4e80b582014-07-18 13:01:19 +0000657 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000658 for (MVT VT : MVT::fp_valuetypes()) {
659 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
660 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
661 }
Tim Northover4e80b582014-07-18 13:01:19 +0000662
663 // ... or truncating stores
664 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
665 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
666 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000667
Duncan Sands95d46ef2008-01-23 20:39:46 +0000668 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000669 for (MVT VT : MVT::integer_valuetypes())
670 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000671
Evan Cheng10043e22007-01-19 07:51:42 +0000672 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000673 if (!Subtarget->isThumb1Only()) {
674 for (unsigned im = (unsigned)ISD::PRE_INC;
675 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000676 setIndexedLoadAction(im, MVT::i1, Legal);
677 setIndexedLoadAction(im, MVT::i8, Legal);
678 setIndexedLoadAction(im, MVT::i16, Legal);
679 setIndexedLoadAction(im, MVT::i32, Legal);
680 setIndexedStoreAction(im, MVT::i1, Legal);
681 setIndexedStoreAction(im, MVT::i8, Legal);
682 setIndexedStoreAction(im, MVT::i16, Legal);
683 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000684 }
Evan Cheng10043e22007-01-19 07:51:42 +0000685 }
686
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000687 setOperationAction(ISD::SADDO, MVT::i32, Custom);
688 setOperationAction(ISD::UADDO, MVT::i32, Custom);
689 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
690 setOperationAction(ISD::USUBO, MVT::i32, Custom);
691
Evan Cheng10043e22007-01-19 07:51:42 +0000692 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000693 setOperationAction(ISD::MUL, MVT::i64, Expand);
694 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000695 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000696 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
697 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000698 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000699 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
700 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000701 setOperationAction(ISD::MULHS, MVT::i32, Expand);
702
Jim Grosbach5d994042009-10-31 19:38:01 +0000703 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000704 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000705 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000706 setOperationAction(ISD::SRL, MVT::i64, Custom);
707 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000708
Evan Chenge8916542011-08-30 01:34:54 +0000709 if (!Subtarget->isThumb1Only()) {
710 // FIXME: We should do this for Thumb1 as well.
711 setOperationAction(ISD::ADDC, MVT::i32, Custom);
712 setOperationAction(ISD::ADDE, MVT::i32, Custom);
713 setOperationAction(ISD::SUBC, MVT::i32, Custom);
714 setOperationAction(ISD::SUBE, MVT::i32, Custom);
715 }
716
Evan Cheng10043e22007-01-19 07:51:42 +0000717 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000718 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000719 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000720 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000721 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000722 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000723
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000724 // These just redirect to CTTZ and CTLZ on ARM.
725 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
726 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
727
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000728 // @llvm.readcyclecounter requires the Performance Monitors extension.
729 // Default to the 0 expansion on unsupported platforms.
730 // FIXME: Technically there are older ARM CPUs that have
731 // implementation-specific ways of obtaining this information.
732 if (Subtarget->hasPerfMon())
733 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000734
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000735 // Only ARMv6 has BSWAP.
736 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000737 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000738
Bob Wilsone8a549c2012-09-29 21:43:49 +0000739 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
740 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
741 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000742 setOperationAction(ISD::SDIV, MVT::i32, Expand);
743 setOperationAction(ISD::UDIV, MVT::i32, Expand);
744 }
Renato Golin87610692013-07-16 09:32:17 +0000745
Chad Rosierad7c9102014-08-23 18:29:43 +0000746 setOperationAction(ISD::SREM, MVT::i32, Expand);
747 setOperationAction(ISD::UREM, MVT::i32, Expand);
748 // Register based DivRem for AEABI (RTABI 4.2)
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000749 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000750 setOperationAction(ISD::SREM, MVT::i64, Custom);
751 setOperationAction(ISD::UREM, MVT::i64, Custom);
752
Chad Rosierad7c9102014-08-23 18:29:43 +0000753 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
754 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
755 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
756 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
757 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
758 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
759 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
760 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
761
762 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
763 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
764 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
765 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
766 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
767 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
768 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
769 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
770
771 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
772 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
773 } else {
Renato Golin87610692013-07-16 09:32:17 +0000774 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
775 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
776 }
Bob Wilson7117a912009-03-20 22:42:55 +0000777
Owen Anderson9f944592009-08-11 20:47:22 +0000778 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
779 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
780 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
781 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000782 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000783
Evan Cheng74d92c12011-04-08 21:37:21 +0000784 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000785
Evan Cheng10043e22007-01-19 07:51:42 +0000786 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000787 setOperationAction(ISD::VASTART, MVT::Other, Custom);
788 setOperationAction(ISD::VAARG, MVT::Other, Expand);
789 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
790 setOperationAction(ISD::VAEND, MVT::Other, Expand);
791 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
792 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000793
Tim Northoverd6a729b2014-01-06 14:28:05 +0000794 if (!Subtarget->isTargetMachO()) {
795 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000796 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000797 setExceptionPointerRegister(ARM::R0);
798 setExceptionSelectorRegister(ARM::R1);
799 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000800
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000801 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
802 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
803 else
804 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
805
Evan Cheng6e809de2010-08-11 06:22:01 +0000806 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000807 // the default expansion. If we are targeting a single threaded system,
808 // then set them all for expand so we can lower them later into their
809 // non-atomic form.
810 if (TM.Options.ThreadModel == ThreadModel::Single)
811 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
812 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000813 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
814 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000815 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000816
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000817 // On v8, we have particularly efficient implementations of atomic fences
818 // if they can be combined with nearby atomic loads and stores.
819 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000820 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000821 setInsertFencesForAtomic(true);
822 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000823 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000824 // If there's anything we can use as a barrier, go through custom lowering
825 // for ATOMIC_FENCE.
826 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
827 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
828
Jim Grosbach6860bb72010-06-18 22:35:32 +0000829 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000830 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000831 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000832 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000833 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000834 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000835 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000836 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000837 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000838 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000839 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000840 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000841 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000842 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
843 // Unordered/Monotonic case.
844 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
845 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000846 }
Evan Cheng10043e22007-01-19 07:51:42 +0000847
Evan Cheng21acf9f2010-11-04 05:19:35 +0000848 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000849
Eli Friedman8cfa7712010-06-26 04:36:50 +0000850 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
851 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000852 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
853 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000854 }
Owen Anderson9f944592009-08-11 20:47:22 +0000855 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000856
Eric Christopher824f42f2015-05-12 01:26:05 +0000857 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000858 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000859 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000860 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000861 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000862 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
863 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000864
865 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000866 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000867 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
868 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
869 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
870 if (Subtarget->isTargetDarwin())
John McCall7d84ece2011-05-29 19:50:32 +0000871 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000872
Owen Anderson9f944592009-08-11 20:47:22 +0000873 setOperationAction(ISD::SETCC, MVT::i32, Expand);
874 setOperationAction(ISD::SETCC, MVT::f32, Expand);
875 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000876 setOperationAction(ISD::SELECT, MVT::i32, Custom);
877 setOperationAction(ISD::SELECT, MVT::f32, Custom);
878 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000879 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
880 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
881 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000882
Owen Anderson9f944592009-08-11 20:47:22 +0000883 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
884 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
885 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
886 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
887 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000888
Dan Gohman482732a2007-10-11 23:21:31 +0000889 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000890 setOperationAction(ISD::FSIN, MVT::f64, Expand);
891 setOperationAction(ISD::FSIN, MVT::f32, Expand);
892 setOperationAction(ISD::FCOS, MVT::f32, Expand);
893 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000894 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
895 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000896 setOperationAction(ISD::FREM, MVT::f64, Expand);
897 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000898 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000899 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000900 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
901 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000902 }
Owen Anderson9f944592009-08-11 20:47:22 +0000903 setOperationAction(ISD::FPOW, MVT::f64, Expand);
904 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000905
Evan Chengd0007f32012-04-10 21:40:28 +0000906 if (!Subtarget->hasVFP4()) {
907 setOperationAction(ISD::FMA, MVT::f64, Expand);
908 setOperationAction(ISD::FMA, MVT::f32, Expand);
909 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000910
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000911 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000912 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000913 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
914 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000915 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
916 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
917 }
918
919 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000920 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000921 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
922 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000923 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000924 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000925
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000926 // Combine sin / cos into one node or libcall if possible.
927 if (Subtarget->hasSinCos()) {
928 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
929 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000930 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000931 // For iOS, we don't want to the normal expansion of a libcall to
932 // sincos. We want to issue a libcall to __sincos_stret.
933 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
934 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
935 }
936 }
Evan Cheng10043e22007-01-19 07:51:42 +0000937
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000938 // FP-ARMv8 implements a lot of rounding-like FP operations.
939 if (Subtarget->hasFPARMv8()) {
940 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
941 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
942 setOperationAction(ISD::FROUND, MVT::f32, Legal);
943 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
944 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
945 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000946 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
947 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000948 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
949 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
950 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
951 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
952
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000953 if (!Subtarget->isFPOnlySP()) {
954 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
955 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
956 setOperationAction(ISD::FROUND, MVT::f64, Legal);
957 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
958 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
959 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000960 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
961 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000962 }
963 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000964
James Molloy974838f2015-08-17 19:37:12 +0000965 if (Subtarget->hasNEON()) {
966 // vmin and vmax aren't available in a scalar form, so we use
967 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +0000968 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
969 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +0000970 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
971 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
972 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
973 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
974 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000975
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000976 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000977 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000978 setTargetDAGCombine(ISD::ADD);
979 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000980 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000981 setTargetDAGCombine(ISD::AND);
982 setTargetDAGCombine(ISD::OR);
983 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000984
Evan Chengf258a152012-02-23 02:58:19 +0000985 if (Subtarget->hasV6Ops())
986 setTargetDAGCombine(ISD::SRL);
987
Evan Cheng10043e22007-01-19 07:51:42 +0000988 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000989
Eric Christopher824f42f2015-05-12 01:26:05 +0000990 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000991 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000992 setSchedulingPreference(Sched::RegPressure);
993 else
994 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000995
Evan Cheng3ae2b792011-01-06 06:52:41 +0000996 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000997 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +0000998 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000999 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001000 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001001 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001002 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001003
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001004 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1005 // are at least 4 bytes aligned.
1006 setMinStackArgumentAlignment(4);
1007
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001008 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001009 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001010
Eli Friedman2518f832011-05-06 20:34:06 +00001011 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001012}
1013
Eric Christopher824f42f2015-05-12 01:26:05 +00001014bool ARMTargetLowering::useSoftFloat() const {
1015 return Subtarget->useSoftFloat();
1016}
1017
Andrew Trick43f25632011-01-19 02:35:27 +00001018// FIXME: It might make sense to define the representative register class as the
1019// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1020// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1021// SPR's representative would be DPR_VFP2. This should work well if register
1022// pressure tracking were modified such that a register use would increment the
1023// pressure of the register class's representative and all of it's super
1024// classes' representatives transitively. We have not implemented this because
1025// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001026// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001027// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001028std::pair<const TargetRegisterClass *, uint8_t>
1029ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1030 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001031 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001032 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001033 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001034 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001035 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001036 // Use DPR as representative register class for all floating point
1037 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1038 // the cost is 1 for both f32 and f64.
1039 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001040 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001041 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001042 // When NEON is used for SP, only half of the register file is available
1043 // because operations that define both SP and DP results will be constrained
1044 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1045 // coalescing by double-counting the SP regs. See the FIXME above.
1046 if (Subtarget->useNEONForSinglePrecisionFP())
1047 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001048 break;
1049 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1050 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001051 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001052 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001053 break;
1054 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001055 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001056 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001057 break;
1058 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001059 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001060 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001061 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001062 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001063 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001064}
1065
Evan Cheng10043e22007-01-19 07:51:42 +00001066const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001067 switch ((ARMISD::NodeType)Opcode) {
1068 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001069 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001070 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001071 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001072 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001073 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001074 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001075 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1076 case ARMISD::tCALL: return "ARMISD::tCALL";
1077 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1078 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001079 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001080 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001081 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001082 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1083 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001084 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001085 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001086 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1087 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001088 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001089 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001090
Evan Cheng10043e22007-01-19 07:51:42 +00001091 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001092
Jim Grosbach8546ec92010-01-18 19:58:49 +00001093 case ARMISD::RBIT: return "ARMISD::RBIT";
1094
Evan Cheng10043e22007-01-19 07:51:42 +00001095 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1096 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1097 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001098
Evan Chenge8916542011-08-30 01:34:54 +00001099 case ARMISD::ADDC: return "ARMISD::ADDC";
1100 case ARMISD::ADDE: return "ARMISD::ADDE";
1101 case ARMISD::SUBC: return "ARMISD::SUBC";
1102 case ARMISD::SUBE: return "ARMISD::SUBE";
1103
Bob Wilson22806742010-09-22 22:09:21 +00001104 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1105 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001106
Evan Chengec6d7c92009-10-28 06:55:03 +00001107 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001108 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1109 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001110
Dale Johannesend679ff72010-06-03 21:09:53 +00001111 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001112
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001113 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001114
Evan Chengb972e562009-08-07 00:34:42 +00001115 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1116
Bob Wilson7ed59712010-10-30 00:54:37 +00001117 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001118
Evan Cheng8740ee32010-11-03 06:34:55 +00001119 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1120
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001121 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1122
Bob Wilson2e076c42009-06-22 23:27:02 +00001123 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001124 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001125 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001126 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1127 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001128 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1129 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001130 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1131 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001132 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1133 case ARMISD::VTST: return "ARMISD::VTST";
1134
1135 case ARMISD::VSHL: return "ARMISD::VSHL";
1136 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1137 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001138 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1139 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1140 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1141 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1142 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1143 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1144 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1145 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1146 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1147 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1148 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1149 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001150 case ARMISD::VSLI: return "ARMISD::VSLI";
1151 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001152 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1153 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001154 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001155 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001156 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001157 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001158 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001159 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001160 case ARMISD::VREV64: return "ARMISD::VREV64";
1161 case ARMISD::VREV32: return "ARMISD::VREV32";
1162 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001163 case ARMISD::VZIP: return "ARMISD::VZIP";
1164 case ARMISD::VUZP: return "ARMISD::VUZP";
1165 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001166 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1167 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001168 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1169 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001170 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1171 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001172 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001173 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001174 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1175 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001176 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001177 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1178 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1179 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001180 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1181 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1182 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1183 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1184 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1185 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1186 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1187 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1188 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1189 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1190 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1191 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1192 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1193 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1194 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1195 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1196 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001197 }
Matthias Braund04893f2015-05-07 21:33:59 +00001198 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001199}
1200
Mehdi Amini44ede332015-07-09 02:09:04 +00001201EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1202 EVT VT) const {
1203 if (!VT.isVector())
1204 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001205 return VT.changeVectorElementTypeToInteger();
1206}
1207
Evan Cheng4cad68e2010-05-15 02:18:07 +00001208/// getRegClassFor - Return the register class that should be used for the
1209/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001210const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001211 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1212 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1213 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001214 if (Subtarget->hasNEON()) {
1215 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001216 return &ARM::QQPRRegClass;
1217 if (VT == MVT::v8i64)
1218 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001219 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001220 return TargetLowering::getRegClassFor(VT);
1221}
1222
John Brawn0dbcd652015-03-18 12:01:59 +00001223// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1224// source/dest is aligned and the copy size is large enough. We therefore want
1225// to align such objects passed to memory intrinsics.
1226bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1227 unsigned &PrefAlign) const {
1228 if (!isa<MemIntrinsic>(CI))
1229 return false;
1230 MinSize = 8;
1231 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1232 // cycle faster than 4-byte aligned LDM.
1233 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1234 return true;
1235}
1236
Eric Christopher84bdfd82010-07-21 22:26:11 +00001237// Create a fast isel object.
1238FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001239ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1240 const TargetLibraryInfo *libInfo) const {
1241 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001242}
1243
Evan Cheng4401f882010-05-20 23:26:43 +00001244Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001245 unsigned NumVals = N->getNumValues();
1246 if (!NumVals)
1247 return Sched::RegPressure;
1248
1249 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001250 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001251 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001252 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001253 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001254 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001255 }
Evan Chengbf914992010-05-28 23:25:23 +00001256
1257 if (!N->isMachineOpcode())
1258 return Sched::RegPressure;
1259
1260 // Load are scheduled for latency even if there instruction itinerary
1261 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001262 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001263 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001264
Evan Cheng6cc775f2011-06-28 19:10:37 +00001265 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001266 return Sched::RegPressure;
1267 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001268 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001269 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001270
Evan Cheng4401f882010-05-20 23:26:43 +00001271 return Sched::RegPressure;
1272}
1273
Evan Cheng10043e22007-01-19 07:51:42 +00001274//===----------------------------------------------------------------------===//
1275// Lowering Code
1276//===----------------------------------------------------------------------===//
1277
Evan Cheng10043e22007-01-19 07:51:42 +00001278/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1279static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1280 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001281 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001282 case ISD::SETNE: return ARMCC::NE;
1283 case ISD::SETEQ: return ARMCC::EQ;
1284 case ISD::SETGT: return ARMCC::GT;
1285 case ISD::SETGE: return ARMCC::GE;
1286 case ISD::SETLT: return ARMCC::LT;
1287 case ISD::SETLE: return ARMCC::LE;
1288 case ISD::SETUGT: return ARMCC::HI;
1289 case ISD::SETUGE: return ARMCC::HS;
1290 case ISD::SETULT: return ARMCC::LO;
1291 case ISD::SETULE: return ARMCC::LS;
1292 }
1293}
1294
Bob Wilsona2e83332009-09-09 23:14:54 +00001295/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1296static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001297 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001298 CondCode2 = ARMCC::AL;
1299 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001300 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001301 case ISD::SETEQ:
1302 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1303 case ISD::SETGT:
1304 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1305 case ISD::SETGE:
1306 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1307 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001308 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001309 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1310 case ISD::SETO: CondCode = ARMCC::VC; break;
1311 case ISD::SETUO: CondCode = ARMCC::VS; break;
1312 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1313 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1314 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1315 case ISD::SETLT:
1316 case ISD::SETULT: CondCode = ARMCC::LT; break;
1317 case ISD::SETLE:
1318 case ISD::SETULE: CondCode = ARMCC::LE; break;
1319 case ISD::SETNE:
1320 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1321 }
Evan Cheng10043e22007-01-19 07:51:42 +00001322}
1323
Bob Wilsona4c22902009-04-17 19:07:39 +00001324//===----------------------------------------------------------------------===//
1325// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001326//===----------------------------------------------------------------------===//
1327
1328#include "ARMGenCallingConv.inc"
1329
Oliver Stannardc24f2172014-05-09 14:01:47 +00001330/// getEffectiveCallingConv - Get the effective calling convention, taking into
1331/// account presence of floating point hardware and calling convention
1332/// limitations, such as support for variadic functions.
1333CallingConv::ID
1334ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1335 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001336 switch (CC) {
1337 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001338 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001339 case CallingConv::ARM_AAPCS:
1340 case CallingConv::ARM_APCS:
1341 case CallingConv::GHC:
1342 return CC;
1343 case CallingConv::ARM_AAPCS_VFP:
1344 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1345 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001346 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001347 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001348 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001349 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1350 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001351 return CallingConv::ARM_AAPCS_VFP;
1352 else
1353 return CallingConv::ARM_AAPCS;
1354 case CallingConv::Fast:
1355 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001356 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001357 return CallingConv::Fast;
1358 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001359 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001360 return CallingConv::ARM_AAPCS_VFP;
1361 else
1362 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001363 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001364}
1365
1366/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1367/// CallingConvention.
1368CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1369 bool Return,
1370 bool isVarArg) const {
1371 switch (getEffectiveCallingConv(CC, isVarArg)) {
1372 default:
1373 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001374 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001375 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001376 case CallingConv::ARM_AAPCS:
1377 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1378 case CallingConv::ARM_AAPCS_VFP:
1379 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1380 case CallingConv::Fast:
1381 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001382 case CallingConv::GHC:
1383 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001384 }
1385}
1386
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001387/// LowerCallResult - Lower the result values of a call into the
1388/// appropriate copies out of appropriate physical registers.
1389SDValue
1390ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001391 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001392 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001393 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001394 SmallVectorImpl<SDValue> &InVals,
1395 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001396
Bob Wilsona4c22902009-04-17 19:07:39 +00001397 // Assign locations to each value returned by this call.
1398 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001399 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1400 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001401 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001402 CCAssignFnForNode(CallConv, /* Return*/ true,
1403 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001404
1405 // Copy all of the result registers out of their specified physreg.
1406 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1407 CCValAssign VA = RVLocs[i];
1408
Stephen Linb8bd2322013-04-20 05:14:40 +00001409 // Pass 'this' value directly from the argument to return value, to avoid
1410 // reg unit interference
1411 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001412 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1413 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001414 InVals.push_back(ThisVal);
1415 continue;
1416 }
1417
Bob Wilson0041bd32009-04-25 00:33:20 +00001418 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001419 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001420 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001421 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001422 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001423 Chain = Lo.getValue(1);
1424 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001425 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001426 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001427 InFlag);
1428 Chain = Hi.getValue(1);
1429 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001430 if (!Subtarget->isLittle())
1431 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001432 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001433
Owen Anderson9f944592009-08-11 20:47:22 +00001434 if (VA.getLocVT() == MVT::v2f64) {
1435 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1436 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001437 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001438
1439 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001440 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001441 Chain = Lo.getValue(1);
1442 InFlag = Lo.getValue(2);
1443 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001444 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001445 Chain = Hi.getValue(1);
1446 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001447 if (!Subtarget->isLittle())
1448 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001449 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001450 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001451 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001452 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001453 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001454 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1455 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001456 Chain = Val.getValue(1);
1457 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001458 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001459
1460 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001461 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001462 case CCValAssign::Full: break;
1463 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001464 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001465 break;
1466 }
1467
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001468 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001469 }
1470
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001471 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001472}
1473
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001474/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001475SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001476ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1477 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001478 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001479 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001480 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001481 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001482 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001483 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1484 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001485 return DAG.getStore(
1486 Chain, dl, Arg, PtrOff,
1487 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1488 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001489}
1490
Andrew Trickef9de2a2013-05-25 02:42:55 +00001491void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001492 SDValue Chain, SDValue &Arg,
1493 RegsToPassVector &RegsToPass,
1494 CCValAssign &VA, CCValAssign &NextVA,
1495 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001496 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001497 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001498
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001499 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001500 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001501 unsigned id = Subtarget->isLittle() ? 0 : 1;
1502 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001503
1504 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001505 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001506 else {
1507 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001508 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001509 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1510 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001511
Christian Pirkerb5728192014-05-08 14:06:24 +00001512 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001513 dl, DAG, NextVA,
1514 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001515 }
1516}
1517
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001518/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001519/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1520/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001521SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001522ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001523 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001524 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001525 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001526 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1527 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1528 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001529 SDValue Chain = CLI.Chain;
1530 SDValue Callee = CLI.Callee;
1531 bool &isTailCall = CLI.IsTailCall;
1532 CallingConv::ID CallConv = CLI.CallConv;
1533 bool doesNotRet = CLI.DoesNotReturn;
1534 bool isVarArg = CLI.IsVarArg;
1535
Dale Johannesend679ff72010-06-03 21:09:53 +00001536 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001537 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1538 bool isThisReturn = false;
1539 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001540 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001541
Bob Wilson8decdc42011-10-07 17:17:49 +00001542 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001543 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001544 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001545
Dale Johannesend679ff72010-06-03 21:09:53 +00001546 if (isTailCall) {
1547 // Check if it's really possible to do a tail call.
1548 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001549 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001550 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001551 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1552 report_fatal_error("failed to perform tail call elimination on a call "
1553 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001554 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1555 // detected sibcalls.
1556 if (isTailCall) {
1557 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001558 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001559 }
1560 }
Evan Cheng10043e22007-01-19 07:51:42 +00001561
Bob Wilsona4c22902009-04-17 19:07:39 +00001562 // Analyze operands of the call, assigning locations to each operand.
1563 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001564 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1565 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001566 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001567 CCAssignFnForNode(CallConv, /* Return*/ false,
1568 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001569
Bob Wilsona4c22902009-04-17 19:07:39 +00001570 // Get a count of how many bytes are to be pushed on the stack.
1571 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001572
Dale Johannesend679ff72010-06-03 21:09:53 +00001573 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001574 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001575 NumBytes = 0;
1576
Evan Cheng10043e22007-01-19 07:51:42 +00001577 // Adjust the stack pointer for the new arguments...
1578 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001579 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001580 Chain = DAG.getCALLSEQ_START(Chain,
1581 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001582
Mehdi Amini44ede332015-07-09 02:09:04 +00001583 SDValue StackPtr =
1584 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001585
Bob Wilson2e076c42009-06-22 23:27:02 +00001586 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001587 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001588
Bob Wilsona4c22902009-04-17 19:07:39 +00001589 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001590 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001591 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1592 i != e;
1593 ++i, ++realArgIdx) {
1594 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001595 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001596 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001597 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001598
Bob Wilsona4c22902009-04-17 19:07:39 +00001599 // Promote the value if needed.
1600 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001601 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001602 case CCValAssign::Full: break;
1603 case CCValAssign::SExt:
1604 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1605 break;
1606 case CCValAssign::ZExt:
1607 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1608 break;
1609 case CCValAssign::AExt:
1610 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1611 break;
1612 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001613 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001614 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001615 }
1616
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001617 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001618 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001619 if (VA.getLocVT() == MVT::v2f64) {
1620 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001621 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001622 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001623 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001624
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001625 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001626 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1627
1628 VA = ArgLocs[++i]; // skip ahead to next loc
1629 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001630 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001631 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1632 } else {
1633 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001634
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001635 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1636 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001637 }
1638 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001639 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001640 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001641 }
1642 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001643 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1644 assert(VA.getLocVT() == MVT::i32 &&
1645 "unexpected calling convention register assignment");
1646 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001647 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001648 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001649 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001650 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001651 } else if (isByVal) {
1652 assert(VA.isMemLoc());
1653 unsigned offset = 0;
1654
1655 // True if this byval aggregate will be split between registers
1656 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001657 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001658 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001659
1660 if (CurByValIdx < ByValArgsCount) {
1661
1662 unsigned RegBegin, RegEnd;
1663 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1664
Mehdi Amini44ede332015-07-09 02:09:04 +00001665 EVT PtrVT =
1666 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001667 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001668 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001669 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001670 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1671 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1672 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001673 false, false, false,
1674 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001675 MemOpChains.push_back(Load.getValue(1));
1676 RegsToPass.push_back(std::make_pair(j, Load));
1677 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001678
1679 // If parameter size outsides register area, "offset" value
1680 // helps us to calculate stack slot for remained part properly.
1681 offset = RegEnd - RegBegin;
1682
1683 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001684 }
1685
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001686 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001687 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001688 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001689 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001690 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001691 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001692 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001693 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001694 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001695 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1696 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001697
Manman Ren9f911162012-06-01 02:44:42 +00001698 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001699 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001700 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001701 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001702 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001703 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001704 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001705
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001706 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1707 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001708 }
Evan Cheng10043e22007-01-19 07:51:42 +00001709 }
1710
1711 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001712 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001713
1714 // Build a sequence of copy-to-reg nodes chained together with token chain
1715 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001716 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001717 // Tail call byval lowering might overwrite argument registers so in case of
1718 // tail call optimization the copies to registers are lowered later.
1719 if (!isTailCall)
1720 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1721 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1722 RegsToPass[i].second, InFlag);
1723 InFlag = Chain.getValue(1);
1724 }
Evan Cheng10043e22007-01-19 07:51:42 +00001725
Dale Johannesend679ff72010-06-03 21:09:53 +00001726 // For tail calls lower the arguments to the 'real' stack slot.
1727 if (isTailCall) {
1728 // Force all the incoming stack arguments to be loaded from the stack
1729 // before any new outgoing arguments are stored to the stack, because the
1730 // outgoing stack slots may alias the incoming argument stack slots, and
1731 // the alias isn't otherwise explicit. This is slightly more conservative
1732 // than necessary, because it means that each store effectively depends
1733 // on every argument instead of just those arguments it would clobber.
1734
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001735 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001736 InFlag = SDValue();
1737 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1738 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1739 RegsToPass[i].second, InFlag);
1740 InFlag = Chain.getValue(1);
1741 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001742 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001743 }
1744
Bill Wendling24c79f22008-09-16 21:48:12 +00001745 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1746 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1747 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001748 bool isDirect = false;
1749 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001750 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001751 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001752 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001753
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001754 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001755 assert((Subtarget->isTargetWindows() ||
1756 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1757 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001758 // Handle a global address or an external symbol. If it's not one of
1759 // those, the target's already in a register, so we don't need to do
1760 // anything extra.
1761 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001762 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001763 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001764 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001765 ARMConstantPoolValue *CPV =
1766 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1767
Jim Grosbach32bb3622010-04-14 22:28:31 +00001768 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001769 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001770 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001771 Callee = DAG.getLoad(
1772 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1773 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1774 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001775 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1776 const char *Sym = S->getSymbol();
1777
1778 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001779 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001780 ARMConstantPoolValue *CPV =
1781 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1782 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001783 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001784 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001785 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001786 Callee = DAG.getLoad(
1787 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1788 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1789 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001790 }
1791 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001792 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001793 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001794 bool isDef = GV->isStrongDefinitionForLinker();
1795 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001796 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001797 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001798 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001799 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001800 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001801 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001802 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001803 Callee = DAG.getNode(
1804 ARMISD::WrapperPIC, dl, PtrVt,
1805 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1806 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001807 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1808 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001809 } else if (Subtarget->isTargetCOFF()) {
1810 assert(Subtarget->isTargetWindows() &&
1811 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001812 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1813 ? ARMII::MO_DLLIMPORT
1814 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001815 Callee =
1816 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001817 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001818 Callee =
1819 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1820 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001821 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1822 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001823 } else {
1824 // On ELF targets for PIC code, direct calls should go through the PLT
1825 unsigned OpFlags = 0;
1826 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001827 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001828 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001829 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001830 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001831 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001832 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001833 bool isStub = 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 Cheng83f35172007-01-30 20:37:08 +00001836 // tBX takes a register source operand.
1837 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001838 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001839 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001840 ARMConstantPoolValue *CPV =
1841 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1842 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001843 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001844 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001845 Callee = DAG.getLoad(
1846 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1847 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1848 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001849 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001850 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001851 } else {
1852 unsigned OpFlags = 0;
1853 // On ELF targets for PIC code, direct calls should go through the PLT
1854 if (Subtarget->isTargetELF() &&
1855 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1856 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001857 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001858 }
Evan Cheng10043e22007-01-19 07:51:42 +00001859 }
1860
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001861 // FIXME: handle tail calls differently.
1862 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001863 if (Subtarget->isThumb()) {
1864 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001865 CallOpc = ARMISD::CALL_NOLINK;
1866 else
1867 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1868 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001869 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001870 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001871 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001872 // Emit regular call when code size is the priority
1873 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001874 // "mov lr, pc; b _foo" to avoid confusing the RSP
1875 CallOpc = ARMISD::CALL_NOLINK;
1876 else
1877 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001878 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001879
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001880 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001881 Ops.push_back(Chain);
1882 Ops.push_back(Callee);
1883
1884 // Add argument registers to the end of the list so that they are known live
1885 // into the call.
1886 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1887 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1888 RegsToPass[i].second.getValueType()));
1889
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001890 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001891 if (!isTailCall) {
1892 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001893 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001894 if (isThisReturn) {
1895 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001896 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001897 if (!Mask) {
1898 // Set isThisReturn to false if the calling convention is not one that
1899 // allows 'returned' to be modeled in this way, so LowerCallResult does
1900 // not try to pass 'this' straight through
1901 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001902 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001903 }
1904 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001905 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001906
Matthias Braunc22630e2013-10-04 16:52:54 +00001907 assert(Mask && "Missing call preserved mask for calling convention");
1908 Ops.push_back(DAG.getRegisterMask(Mask));
1909 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001910
Gabor Greiff304a7a2008-08-28 21:40:38 +00001911 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001912 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001913
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001914 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001915 if (isTailCall) {
1916 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001917 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001918 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001919
Duncan Sands739a0542008-07-02 17:40:58 +00001920 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001921 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001922 InFlag = Chain.getValue(1);
1923
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001924 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1925 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001926 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001927 InFlag = Chain.getValue(1);
1928
Bob Wilsona4c22902009-04-17 19:07:39 +00001929 // Handle result values, copying them out of physregs into vregs that we
1930 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001931 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001932 InVals, isThisReturn,
1933 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001934}
1935
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001936/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001937/// on the stack. Remember the next parameter register to allocate,
1938/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001939/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001940void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1941 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001942 assert((State->getCallOrPrologue() == Prologue ||
1943 State->getCallOrPrologue() == Call) &&
1944 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001945
Tim Northover8cda34f2015-03-11 18:54:22 +00001946 // Byval (as with any stack) slots are always at least 4 byte aligned.
1947 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001948
Tim Northover8cda34f2015-03-11 18:54:22 +00001949 unsigned Reg = State->AllocateReg(GPRArgRegs);
1950 if (!Reg)
1951 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001952
Tim Northover8cda34f2015-03-11 18:54:22 +00001953 unsigned AlignInRegs = Align / 4;
1954 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1955 for (unsigned i = 0; i < Waste; ++i)
1956 Reg = State->AllocateReg(GPRArgRegs);
1957
1958 if (!Reg)
1959 return;
1960
1961 unsigned Excess = 4 * (ARM::R4 - Reg);
1962
1963 // Special case when NSAA != SP and parameter size greater than size of
1964 // all remained GPR regs. In that case we can't split parameter, we must
1965 // send it to stack. We also must set NCRN to R4, so waste all
1966 // remained registers.
1967 const unsigned NSAAOffset = State->getNextStackOffset();
1968 if (NSAAOffset != 0 && Size > Excess) {
1969 while (State->AllocateReg(GPRArgRegs))
1970 ;
1971 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001972 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001973
1974 // First register for byval parameter is the first register that wasn't
1975 // allocated before this method call, so it would be "reg".
1976 // If parameter is small enough to be saved in range [reg, r4), then
1977 // the end (first after last) register would be reg + param-size-in-regs,
1978 // else parameter would be splitted between registers and stack,
1979 // end register would be r4 in this case.
1980 unsigned ByValRegBegin = Reg;
1981 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1982 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1983 // Note, first register is allocated in the beginning of function already,
1984 // allocate remained amount of registers we need.
1985 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1986 State->AllocateReg(GPRArgRegs);
1987 // A byval parameter that is split between registers and memory needs its
1988 // size truncated here.
1989 // In the case where the entire structure fits in registers, we set the
1990 // size in memory to zero.
1991 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001992}
1993
Dale Johannesend679ff72010-06-03 21:09:53 +00001994/// MatchingStackOffset - Return true if the given stack call argument is
1995/// already available in the same position (relatively) of the caller's
1996/// incoming argument stack.
1997static
1998bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1999 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002000 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002001 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2002 int FI = INT_MAX;
2003 if (Arg.getOpcode() == ISD::CopyFromReg) {
2004 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002005 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002006 return false;
2007 MachineInstr *Def = MRI->getVRegDef(VR);
2008 if (!Def)
2009 return false;
2010 if (!Flags.isByVal()) {
2011 if (!TII->isLoadFromStackSlot(Def, FI))
2012 return false;
2013 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002014 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002015 }
2016 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2017 if (Flags.isByVal())
2018 // ByVal argument is passed in as a pointer but it's now being
2019 // dereferenced. e.g.
2020 // define @foo(%struct.X* %A) {
2021 // tail call @bar(%struct.X* byval %A)
2022 // }
2023 return false;
2024 SDValue Ptr = Ld->getBasePtr();
2025 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2026 if (!FINode)
2027 return false;
2028 FI = FINode->getIndex();
2029 } else
2030 return false;
2031
2032 assert(FI != INT_MAX);
2033 if (!MFI->isFixedObjectIndex(FI))
2034 return false;
2035 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2036}
2037
2038/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2039/// for tail call optimization. Targets which want to do tail call
2040/// optimization should implement this function.
2041bool
2042ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2043 CallingConv::ID CalleeCC,
2044 bool isVarArg,
2045 bool isCalleeStructRet,
2046 bool isCallerStructRet,
2047 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002048 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002049 const SmallVectorImpl<ISD::InputArg> &Ins,
2050 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002051 const Function *CallerF = DAG.getMachineFunction().getFunction();
2052 CallingConv::ID CallerCC = CallerF->getCallingConv();
2053 bool CCMatch = CallerCC == CalleeCC;
2054
2055 // Look for obvious safe cases to perform tail call optimization that do not
2056 // require ABI changes. This is what gcc calls sibcall.
2057
Jim Grosbache3864cc2010-06-16 23:45:49 +00002058 // Do not sibcall optimize vararg calls unless the call site is not passing
2059 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002060 if (isVarArg && !Outs.empty())
2061 return false;
2062
Tim Northoverd8407452013-10-01 14:33:28 +00002063 // Exception-handling functions need a special set of instructions to indicate
2064 // a return to the hardware. Tail-calling another function would probably
2065 // break this.
2066 if (CallerF->hasFnAttribute("interrupt"))
2067 return false;
2068
Dale Johannesend679ff72010-06-03 21:09:53 +00002069 // Also avoid sibcall optimization if either caller or callee uses struct
2070 // return semantics.
2071 if (isCalleeStructRet || isCallerStructRet)
2072 return false;
2073
Eric Christopherae326492015-03-12 22:48:50 +00002074 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002075 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2076 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2077 // support in the assembler and linker to be used. This would need to be
2078 // fixed to fully support tail calls in Thumb1.
2079 //
Dale Johannesene2289282010-07-08 01:18:23 +00002080 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2081 // LR. This means if we need to reload LR, it takes an extra instructions,
2082 // which outweighs the value of the tail call; but here we don't know yet
2083 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002084 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002085 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002086
2087 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2088 // but we need to make sure there are enough registers; the only valid
2089 // registers are the 4 used for parameters. We don't currently do this
2090 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002091 if (Subtarget->isThumb1Only())
2092 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002093
Oliver Stannard12993dd2014-08-18 12:42:15 +00002094 // Externally-defined functions with weak linkage should not be
2095 // tail-called on ARM when the OS does not support dynamic
2096 // pre-emption of symbols, as the AAELF spec requires normal calls
2097 // to undefined weak functions to be replaced with a NOP or jump to the
2098 // next instruction. The behaviour of branch instructions in this
2099 // situation (as used for tail calls) is implementation-defined, so we
2100 // cannot rely on the linker replacing the tail call with a return.
2101 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2102 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002103 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002104 if (GV->hasExternalWeakLinkage() &&
2105 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002106 return false;
2107 }
2108
Dale Johannesend679ff72010-06-03 21:09:53 +00002109 // If the calling conventions do not match, then we'd better make sure the
2110 // results are returned in the same way as what the caller expects.
2111 if (!CCMatch) {
2112 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002113 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2114 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002115 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2116
2117 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002118 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2119 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002120 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2121
2122 if (RVLocs1.size() != RVLocs2.size())
2123 return false;
2124 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2125 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2126 return false;
2127 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2128 return false;
2129 if (RVLocs1[i].isRegLoc()) {
2130 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2131 return false;
2132 } else {
2133 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2134 return false;
2135 }
2136 }
2137 }
2138
Manman Ren7e48b252012-10-12 23:39:43 +00002139 // If Caller's vararg or byval argument has been split between registers and
2140 // stack, do not perform tail call, since part of the argument is in caller's
2141 // local frame.
2142 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2143 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002144 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002145 return false;
2146
Dale Johannesend679ff72010-06-03 21:09:53 +00002147 // If the callee takes no arguments then go on to check the results of the
2148 // call.
2149 if (!Outs.empty()) {
2150 // Check if stack adjustment is needed. For now, do not do this if any
2151 // argument is passed on the stack.
2152 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002153 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2154 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002155 CCInfo.AnalyzeCallOperands(Outs,
2156 CCAssignFnForNode(CalleeCC, false, isVarArg));
2157 if (CCInfo.getNextStackOffset()) {
2158 MachineFunction &MF = DAG.getMachineFunction();
2159
2160 // Check if the arguments are already laid out in the right way as
2161 // the caller's fixed stack objects.
2162 MachineFrameInfo *MFI = MF.getFrameInfo();
2163 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002164 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002165 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2166 i != e;
2167 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002168 CCValAssign &VA = ArgLocs[i];
2169 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002170 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002171 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002172 if (VA.getLocInfo() == CCValAssign::Indirect)
2173 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002174 if (VA.needsCustom()) {
2175 // f64 and vector types are split into multiple registers or
2176 // register/stack-slot combinations. The types will not match
2177 // the registers; give up on memory f64 refs until we figure
2178 // out what to do about this.
2179 if (!VA.isRegLoc())
2180 return false;
2181 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002182 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002183 if (RegVT == MVT::v2f64) {
2184 if (!ArgLocs[++i].isRegLoc())
2185 return false;
2186 if (!ArgLocs[++i].isRegLoc())
2187 return false;
2188 }
2189 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002190 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2191 MFI, MRI, TII))
2192 return false;
2193 }
2194 }
2195 }
2196 }
2197
2198 return true;
2199}
2200
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002201bool
2202ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2203 MachineFunction &MF, bool isVarArg,
2204 const SmallVectorImpl<ISD::OutputArg> &Outs,
2205 LLVMContext &Context) const {
2206 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002207 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002208 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2209 isVarArg));
2210}
2211
Tim Northoverd8407452013-10-01 14:33:28 +00002212static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2213 SDLoc DL, SelectionDAG &DAG) {
2214 const MachineFunction &MF = DAG.getMachineFunction();
2215 const Function *F = MF.getFunction();
2216
2217 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2218
2219 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2220 // version of the "preferred return address". These offsets affect the return
2221 // instruction if this is a return from PL1 without hypervisor extensions.
2222 // IRQ/FIQ: +4 "subs pc, lr, #4"
2223 // SWI: 0 "subs pc, lr, #0"
2224 // ABORT: +4 "subs pc, lr, #4"
2225 // UNDEF: +4/+2 "subs pc, lr, #0"
2226 // UNDEF varies depending on where the exception came from ARM or Thumb
2227 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2228
2229 int64_t LROffset;
2230 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2231 IntKind == "ABORT")
2232 LROffset = 4;
2233 else if (IntKind == "SWI" || IntKind == "UNDEF")
2234 LROffset = 0;
2235 else
2236 report_fatal_error("Unsupported interrupt attribute. If present, value "
2237 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2238
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002239 RetOps.insert(RetOps.begin() + 1,
2240 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002241
Craig Topper48d114b2014-04-26 18:35:24 +00002242 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002243}
2244
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002245SDValue
2246ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002247 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002248 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002249 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002250 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002251
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002252 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002253 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002254
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002255 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002256 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2257 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002258
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002259 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002260 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2261 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002262
Bob Wilsona4c22902009-04-17 19:07:39 +00002263 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002264 SmallVector<SDValue, 4> RetOps;
2265 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002266 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002267
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002268 MachineFunction &MF = DAG.getMachineFunction();
2269 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2270 AFI->setReturnRegsCount(RVLocs.size());
2271
Bob Wilsona4c22902009-04-17 19:07:39 +00002272 // Copy the result values into the output registers.
2273 for (unsigned i = 0, realRVLocIdx = 0;
2274 i != RVLocs.size();
2275 ++i, ++realRVLocIdx) {
2276 CCValAssign &VA = RVLocs[i];
2277 assert(VA.isRegLoc() && "Can only return in registers!");
2278
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002279 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002280
2281 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002282 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002283 case CCValAssign::Full: break;
2284 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002285 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002286 break;
2287 }
2288
Bob Wilsona4c22902009-04-17 19:07:39 +00002289 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002290 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002291 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002292 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002293 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002294 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002295 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002296
Christian Pirkerb5728192014-05-08 14:06:24 +00002297 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2298 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2299 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002300 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002301 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002302 VA = RVLocs[++i]; // skip ahead to next loc
2303 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002304 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2305 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002306 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002307 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002308 VA = RVLocs[++i]; // skip ahead to next loc
2309
2310 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002311 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002312 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002313 }
2314 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2315 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002316 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002317 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002318 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2319 fmrrd.getValue(isLittleEndian ? 0 : 1),
2320 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002321 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002322 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002323 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002324 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2325 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002326 Flag);
2327 } else
2328 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2329
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002330 // Guarantee that all emitted copies are
2331 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002332 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002333 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002334 }
2335
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002336 // Update chain and glue.
2337 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002338 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002339 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002340
Tim Northoverd8407452013-10-01 14:33:28 +00002341 // CPUs which aren't M-class use a special sequence to return from
2342 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2343 // though we use "subs pc, lr, #N").
2344 //
2345 // M-class CPUs actually use a normal return sequence with a special
2346 // (hardware-provided) value in LR, so the normal code path works.
2347 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2348 !Subtarget->isMClass()) {
2349 if (Subtarget->isThumb1Only())
2350 report_fatal_error("interrupt attribute is not supported in Thumb1");
2351 return LowerInterruptReturn(RetOps, dl, DAG);
2352 }
2353
Craig Topper48d114b2014-04-26 18:35:24 +00002354 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002355}
2356
Evan Chengf8bad082012-04-10 01:51:00 +00002357bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002358 if (N->getNumValues() != 1)
2359 return false;
2360 if (!N->hasNUsesOfValue(1, 0))
2361 return false;
2362
Evan Chengf8bad082012-04-10 01:51:00 +00002363 SDValue TCChain = Chain;
2364 SDNode *Copy = *N->use_begin();
2365 if (Copy->getOpcode() == ISD::CopyToReg) {
2366 // If the copy has a glue operand, we conservatively assume it isn't safe to
2367 // perform a tail call.
2368 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2369 return false;
2370 TCChain = Copy->getOperand(0);
2371 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2372 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002373 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002374 SmallPtrSet<SDNode*, 2> Copies;
2375 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002376 UI != UE; ++UI) {
2377 if (UI->getOpcode() != ISD::CopyToReg)
2378 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002379 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002380 }
Evan Chengf8bad082012-04-10 01:51:00 +00002381 if (Copies.size() > 2)
2382 return false;
2383
2384 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2385 UI != UE; ++UI) {
2386 SDValue UseChain = UI->getOperand(0);
2387 if (Copies.count(UseChain.getNode()))
2388 // Second CopyToReg
2389 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002390 else {
2391 // We are at the top of this chain.
2392 // If the copy has a glue operand, we conservatively assume it
2393 // isn't safe to perform a tail call.
2394 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2395 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002396 // First CopyToReg
2397 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002398 }
Evan Chengf8bad082012-04-10 01:51:00 +00002399 }
2400 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002401 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002402 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002403 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002404 Copy = *Copy->use_begin();
2405 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002406 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002407 // If the copy has a glue operand, we conservatively assume it isn't safe to
2408 // perform a tail call.
2409 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2410 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002411 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002412 } else {
2413 return false;
2414 }
2415
Evan Cheng419ea282010-12-01 22:59:46 +00002416 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002417 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2418 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002419 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2420 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002421 return false;
2422 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002423 }
2424
Evan Chengf8bad082012-04-10 01:51:00 +00002425 if (!HasRet)
2426 return false;
2427
2428 Chain = TCChain;
2429 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002430}
2431
Evan Cheng0663f232011-03-21 01:19:09 +00002432bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002433 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002434 return false;
2435
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002436 auto Attr =
2437 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2438 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002439 return false;
2440
2441 return !Subtarget->isThumb1Only();
2442}
2443
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002444// Trying to write a 64 bit value so need to split into two 32 bit values first,
2445// and pass the lower and high parts through.
2446static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2447 SDLoc DL(Op);
2448 SDValue WriteValue = Op->getOperand(2);
2449
2450 // This function is only supposed to be called for i64 type argument.
2451 assert(WriteValue.getValueType() == MVT::i64
2452 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2453
2454 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2455 DAG.getConstant(0, DL, MVT::i32));
2456 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2457 DAG.getConstant(1, DL, MVT::i32));
2458 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2459 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2460}
2461
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002462// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2463// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2464// one of the above mentioned nodes. It has to be wrapped because otherwise
2465// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2466// be used to form addressing mode. These wrapped nodes will be selected
2467// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002468static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002469 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002470 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002471 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002472 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002473 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002474 if (CP->isMachineConstantPoolEntry())
2475 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2476 CP->getAlignment());
2477 else
2478 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2479 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002480 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002481}
2482
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002483unsigned ARMTargetLowering::getJumpTableEncoding() const {
2484 return MachineJumpTableInfo::EK_Inline;
2485}
2486
Dan Gohman21cea8a2010-04-17 15:26:15 +00002487SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2488 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002489 MachineFunction &MF = DAG.getMachineFunction();
2490 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2491 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002492 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002493 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002494 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002495 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2496 SDValue CPAddr;
2497 if (RelocM == Reloc::Static) {
2498 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2499 } else {
2500 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002501 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002502 ARMConstantPoolValue *CPV =
2503 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2504 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002505 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2506 }
2507 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002508 SDValue Result =
2509 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2510 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2511 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002512 if (RelocM == Reloc::Static)
2513 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002514 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002515 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002516}
2517
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002518// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002519SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002520ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002521 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002522 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002523 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002524 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002525 MachineFunction &MF = DAG.getMachineFunction();
2526 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002527 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002528 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002529 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2530 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002531 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002532 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002533 Argument =
2534 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2535 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2536 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002537 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002538
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002539 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002540 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002541
2542 // call __tls_get_addr.
2543 ArgListTy Args;
2544 ArgListEntry Entry;
2545 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002546 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002547 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002548
Dale Johannesen555a3752009-01-30 23:10:59 +00002549 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002550 TargetLowering::CallLoweringInfo CLI(DAG);
2551 CLI.setDebugLoc(dl).setChain(Chain)
2552 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002553 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2554 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002555
Justin Holewinskiaa583972012-05-25 16:35:28 +00002556 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002557 return CallResult.first;
2558}
2559
2560// Lower ISD::GlobalTLSAddress using the "initial exec" or
2561// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002562SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002563ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002564 SelectionDAG &DAG,
2565 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002566 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002567 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002568 SDValue Offset;
2569 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002570 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002571 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002572 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002573
Hans Wennborgaea41202012-05-04 09:40:39 +00002574 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002575 MachineFunction &MF = DAG.getMachineFunction();
2576 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002577 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002578 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002579 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2580 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002581 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2582 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2583 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002584 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002585 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002586 Offset = DAG.getLoad(
2587 PtrVT, dl, Chain, Offset,
2588 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2589 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002590 Chain = Offset.getValue(1);
2591
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002592 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002593 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002594
Alex Lorenze40c8a22015-08-11 23:09:45 +00002595 Offset = DAG.getLoad(
2596 PtrVT, dl, Chain, Offset,
2597 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2598 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002599 } else {
2600 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002601 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002602 ARMConstantPoolValue *CPV =
2603 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
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 }
2611
2612 // The address of the thread local variable is the add of the thread
2613 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002614 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002615}
2616
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002617SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002618ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002619 // TODO: implement the "local dynamic" model
2620 assert(Subtarget->isTargetELF() &&
2621 "TLS not implemented for non-ELF targets");
2622 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002623 if (DAG.getTarget().Options.EmulatedTLS)
2624 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002625
2626 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2627
2628 switch (model) {
2629 case TLSModel::GeneralDynamic:
2630 case TLSModel::LocalDynamic:
2631 return LowerToTLSGeneralDynamicModel(GA, DAG);
2632 case TLSModel::InitialExec:
2633 case TLSModel::LocalExec:
2634 return LowerToTLSExecModels(GA, DAG, model);
2635 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002636 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002637}
2638
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002639SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002640 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002641 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002643 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002644 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002645 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002646 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002647 ARMConstantPoolConstant::Create(GV,
2648 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002649 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002650 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002651 SDValue Result = DAG.getLoad(
2652 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2653 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2654 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002655 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002656 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002657 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002658 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002659 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002660 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002661 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002662 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002663 }
2664
2665 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002666 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002667 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002668 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002669 // FIXME: Once remat is capable of dealing with instructions with register
2670 // operands, expand this into two nodes.
2671 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2672 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002673 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002674 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2675 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002676 return DAG.getLoad(
2677 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2678 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2679 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002680 }
2681}
2682
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002683SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002684 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002685 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002686 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002687 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002688 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002689
Eric Christopherc1058df2014-07-04 01:55:26 +00002690 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002691 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002692
Tim Northover72360d22013-12-02 10:35:41 +00002693 // FIXME: Once remat is capable of dealing with instructions with register
2694 // operands, expand this into multiple nodes
2695 unsigned Wrapper =
2696 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002697
Tim Northover72360d22013-12-02 10:35:41 +00002698 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2699 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002700
Evan Cheng1b389522009-09-03 07:04:02 +00002701 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002702 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002703 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2704 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002705 return Result;
2706}
2707
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002708SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2709 SelectionDAG &DAG) const {
2710 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002711 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2712 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002713
2714 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002715 const ARMII::TOF TargetFlags =
2716 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002717 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002718 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002719 SDLoc DL(Op);
2720
2721 ++NumMovwMovt;
2722
2723 // FIXME: Once remat is capable of dealing with instructions with register
2724 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002725 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2726 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002727 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002728 if (GV->hasDLLImportStorageClass())
2729 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002730 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2731 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002732 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002733}
2734
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002735SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002736 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002737 assert(Subtarget->isTargetELF() &&
2738 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002739 MachineFunction &MF = DAG.getMachineFunction();
2740 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002741 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002742 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002743 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002744 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002745 ARMConstantPoolValue *CPV =
2746 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2747 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002748 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002749 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002750 SDValue Result =
2751 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2752 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2753 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002754 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002755 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002756}
2757
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002758SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002759ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002760 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002761 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002762 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2763 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002764 Op.getOperand(1), Val);
2765}
2766
2767SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002768ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002769 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002770 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002771 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002772}
2773
Matthias Braun3cd00c12015-07-16 22:34:16 +00002774SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2775 SelectionDAG &DAG) const {
2776 SDLoc dl(Op);
2777 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2778 Op.getOperand(0));
2779}
2780
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002781SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002782ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002783 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002784 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002785 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002786 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002787 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002788 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002789 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002790 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002791 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002792 }
Bob Wilson17f88782009-08-04 00:25:01 +00002793 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002794 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002795 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2796 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002797 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002798 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002799 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002800 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002801 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002802 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2803 SDValue CPAddr;
2804 unsigned PCAdj = (RelocM != Reloc::PIC_)
2805 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002806 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002807 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2808 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002809 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002810 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002811 SDValue Result = DAG.getLoad(
2812 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2813 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2814 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002815
2816 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002818 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2819 }
2820 return Result;
2821 }
Evan Cheng18381b42011-03-29 23:06:19 +00002822 case Intrinsic::arm_neon_vmulls:
2823 case Intrinsic::arm_neon_vmullu: {
2824 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2825 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002826 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002827 Op.getOperand(1), Op.getOperand(2));
2828 }
James Molloyee868b22015-08-11 12:06:25 +00002829 case Intrinsic::arm_neon_vminnm:
2830 case Intrinsic::arm_neon_vmaxnm: {
2831 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2832 ? ISD::FMINNUM : ISD::FMAXNUM;
2833 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2834 Op.getOperand(1), Op.getOperand(2));
2835 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002836 case Intrinsic::arm_neon_vminu:
2837 case Intrinsic::arm_neon_vmaxu: {
2838 if (Op.getValueType().isFloatingPoint())
2839 return SDValue();
2840 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2841 ? ISD::UMIN : ISD::UMAX;
2842 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2843 Op.getOperand(1), Op.getOperand(2));
2844 }
James Molloyd616c642015-08-11 12:06:28 +00002845 case Intrinsic::arm_neon_vmins:
2846 case Intrinsic::arm_neon_vmaxs: {
2847 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002848 if (!Op.getValueType().isFloatingPoint()) {
2849 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2850 ? ISD::SMIN : ISD::SMAX;
2851 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2852 Op.getOperand(1), Op.getOperand(2));
2853 }
James Molloyd616c642015-08-11 12:06:28 +00002854 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2855 ? ISD::FMINNAN : ISD::FMAXNAN;
2856 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2857 Op.getOperand(1), Op.getOperand(2));
2858 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002859 }
2860}
2861
Eli Friedman30a49e92011-08-03 21:06:02 +00002862static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2863 const ARMSubtarget *Subtarget) {
2864 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002865 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002866 if (!Subtarget->hasDataBarrier()) {
2867 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2868 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2869 // here.
2870 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002871 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002872 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002873 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002874 }
2875
Tim Northover36b24172013-07-03 09:20:36 +00002876 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2877 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002878 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002879 if (Subtarget->isMClass()) {
2880 // Only a full system barrier exists in the M-class architectures.
2881 Domain = ARM_MB::SY;
2882 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002883 // Swift happens to implement ISHST barriers in a way that's compatible with
2884 // Release semantics but weaker than ISH so we'd be fools not to use
2885 // it. Beware: other processors probably don't!
2886 Domain = ARM_MB::ISHST;
2887 }
2888
Joey Gouly926d3f52013-09-05 15:35:24 +00002889 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002890 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2891 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002892}
2893
Evan Cheng8740ee32010-11-03 06:34:55 +00002894static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2895 const ARMSubtarget *Subtarget) {
2896 // ARM pre v5TE and Thumb1 does not have preload instructions.
2897 if (!(Subtarget->isThumb2() ||
2898 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2899 // Just preserve the chain.
2900 return Op.getOperand(0);
2901
Andrew Trickef9de2a2013-05-25 02:42:55 +00002902 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002903 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2904 if (!isRead &&
2905 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2906 // ARMv7 with MP extension has PLDW.
2907 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002908
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002909 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2910 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002911 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002912 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002913 isData = ~isData & 1;
2914 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002915
2916 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002917 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2918 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002919}
2920
Dan Gohman31ae5862010-04-17 14:41:14 +00002921static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2922 MachineFunction &MF = DAG.getMachineFunction();
2923 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2924
Evan Cheng10043e22007-01-19 07:51:42 +00002925 // vastart just stores the address of the VarArgsFrameIndex slot into the
2926 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002927 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002928 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002929 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002930 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002931 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2932 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002933}
2934
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002935SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002936ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2937 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002938 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002939 MachineFunction &MF = DAG.getMachineFunction();
2940 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2941
Craig Topper760b1342012-02-22 05:59:10 +00002942 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002943 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002944 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002945 else
Craig Topperc7242e02012-04-20 07:30:17 +00002946 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002947
2948 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002949 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002950 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002951
2952 SDValue ArgValue2;
2953 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002954 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002955 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002956
2957 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002958 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00002959 ArgValue2 = DAG.getLoad(
2960 MVT::i32, dl, Root, FIN,
2961 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
2962 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002963 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002964 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002965 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002966 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002967 if (!Subtarget->isLittle())
2968 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002969 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002970}
2971
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002972// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002973// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002974// byval). Either way, we allocate stack slots adjacent to the data
2975// provided by our caller, and store the unallocated registers there.
2976// If this is a variadic function, the va_list pointer will begin with
2977// these values; otherwise, this reassembles a (byval) structure that
2978// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002979// Return: The frame index registers were stored into.
2980int
2981ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002982 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002983 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002984 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002985 int ArgOffset,
2986 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002987 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002988 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002989 // Setup first unallocated register as first byval register;
2990 // eat all remained registers
2991 // (these two actions are performed by HandleByVal method).
2992 // Then, here, we initialize stack frame with
2993 // "store-reg" instructions.
2994 // Case #2. Var-args function, that doesn't contain byval parameters.
2995 // The same: eat all remained unallocated registers,
2996 // initialize stack frame.
2997
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002998 MachineFunction &MF = DAG.getMachineFunction();
2999 MachineFrameInfo *MFI = MF.getFrameInfo();
3000 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003001 unsigned RBegin, REnd;
3002 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3003 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003004 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003005 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003006 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003007 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003008 }
3009
Tim Northover8cda34f2015-03-11 18:54:22 +00003010 if (REnd != RBegin)
3011 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003012
Mehdi Amini44ede332015-07-09 02:09:04 +00003013 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003014 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003015 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003016
Tim Northover8cda34f2015-03-11 18:54:22 +00003017 SmallVector<SDValue, 4> MemOps;
3018 const TargetRegisterClass *RC =
3019 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003020
Tim Northover8cda34f2015-03-11 18:54:22 +00003021 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3022 unsigned VReg = MF.addLiveIn(Reg, RC);
3023 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3024 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003025 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003026 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3027 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003028 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003029 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003030
3031 if (!MemOps.empty())
3032 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3033 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003034}
3035
3036// Setup stack frame, the va_list pointer will start from.
3037void
3038ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003039 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003040 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003041 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003042 bool ForceMutable) const {
3043 MachineFunction &MF = DAG.getMachineFunction();
3044 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3045
3046 // Try to store any remaining integer argument regs
3047 // to their spots on the stack so that they may be loaded by deferencing
3048 // the result of va_next.
3049 // If there is no regs to be stored, just point address after last
3050 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003051 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3052 CCInfo.getInRegsParamsCount(),
3053 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003054 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003055}
3056
Bob Wilson2e076c42009-06-22 23:27:02 +00003057SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003058ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003059 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003060 const SmallVectorImpl<ISD::InputArg>
3061 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003062 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003063 SmallVectorImpl<SDValue> &InVals)
3064 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003065 MachineFunction &MF = DAG.getMachineFunction();
3066 MachineFrameInfo *MFI = MF.getFrameInfo();
3067
Bob Wilsona4c22902009-04-17 19:07:39 +00003068 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3069
3070 // Assign locations to all of the incoming arguments.
3071 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003072 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3073 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003074 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003075 CCAssignFnForNode(CallConv, /* Return*/ false,
3076 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003077
Bob Wilsona4c22902009-04-17 19:07:39 +00003078 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003079 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003080 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3081 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003082
3083 // Initially ArgRegsSaveSize is zero.
3084 // Then we increase this value each time we meet byval parameter.
3085 // We also increase this value in case of varargs function.
3086 AFI->setArgRegsSaveSize(0);
3087
Oliver Stannardd55e1152014-03-05 15:25:27 +00003088 // Calculate the amount of stack space that we need to allocate to store
3089 // byval and variadic arguments that are passed in registers.
3090 // We need to know this before we allocate the first byval or variadic
3091 // argument, as they will be allocated a stack slot below the CFA (Canonical
3092 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003093 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003094 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003095 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3096 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003097
Tim Northover8cda34f2015-03-11 18:54:22 +00003098 CCValAssign &VA = ArgLocs[i];
3099 unsigned Index = VA.getValNo();
3100 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3101 if (!Flags.isByVal())
3102 continue;
3103
3104 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3105 unsigned RBegin, REnd;
3106 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3107 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3108
3109 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003110 }
3111 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003112
3113 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003114 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003115 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3116 if (RegIdx != array_lengthof(GPRArgRegs))
3117 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003118 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003119
3120 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3121 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003122 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003123
Bob Wilsona4c22902009-04-17 19:07:39 +00003124 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3125 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003126 if (Ins[VA.getValNo()].isOrigArg()) {
3127 std::advance(CurOrigArg,
3128 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3129 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3130 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003131 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003132 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003133 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003134
Bob Wilsona4c22902009-04-17 19:07:39 +00003135 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003136 // f64 and vector types are split up into multiple registers or
3137 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003138 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003139 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003140 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003141 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003142 SDValue ArgValue2;
3143 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003144 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003145 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003146 ArgValue2 = DAG.getLoad(
3147 MVT::f64, dl, Chain, FIN,
3148 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3149 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003150 } else {
3151 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3152 Chain, DAG, dl);
3153 }
Owen Anderson9f944592009-08-11 20:47:22 +00003154 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3155 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003156 ArgValue, ArgValue1,
3157 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003158 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003159 ArgValue, ArgValue2,
3160 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003161 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003162 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003163
Bob Wilson2e076c42009-06-22 23:27:02 +00003164 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003165 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003166
Owen Anderson9f944592009-08-11 20:47:22 +00003167 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003168 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003169 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003170 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003171 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003172 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003173 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003174 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3175 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003176 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003177 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003178
3179 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003180 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003181 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003182 }
3183
3184 // If this is an 8 or 16-bit value, it is really passed promoted
3185 // to 32 bits. Insert an assert[sz]ext to capture this, then
3186 // truncate to the right size.
3187 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003188 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003189 case CCValAssign::Full: break;
3190 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003191 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003192 break;
3193 case CCValAssign::SExt:
3194 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3195 DAG.getValueType(VA.getValVT()));
3196 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3197 break;
3198 case CCValAssign::ZExt:
3199 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3200 DAG.getValueType(VA.getValVT()));
3201 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3202 break;
3203 }
3204
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003205 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003206
3207 } else { // VA.isRegLoc()
3208
3209 // sanity check
3210 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003211 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003212
Andrew Trick05938a52015-02-16 18:10:47 +00003213 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003214
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003215 // Some Ins[] entries become multiple ArgLoc[] entries.
3216 // Process them only once.
3217 if (index != lastInsIndex)
3218 {
3219 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003220 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003221 // This can be changed with more analysis.
3222 // In case of tail call optimization mark all arguments mutable.
3223 // Since they could be overwritten by lowering of arguments in case of
3224 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003225 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003226 assert(Ins[index].isOrigArg() &&
3227 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003228 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003229
Tim Northover8cda34f2015-03-11 18:54:22 +00003230 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3231 CurByValIndex, VA.getLocMemOffset(),
3232 Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003233 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003234 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003235 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003236 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003237 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003238 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003239
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003240 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003241 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003242 InVals.push_back(DAG.getLoad(
3243 VA.getValVT(), dl, Chain, FIN,
3244 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3245 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003246 }
3247 lastInsIndex = index;
3248 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003249 }
3250 }
3251
3252 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003253 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003254 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003255 CCInfo.getNextStackOffset(),
3256 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003257
Oliver Stannardb14c6252014-04-02 16:10:33 +00003258 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3259
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003260 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003261}
3262
3263/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003264static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003265 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003266 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003267 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003268 // Maybe this has already been legalized into the constant pool?
3269 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003270 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003271 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003272 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003273 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003274 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003275 } else if (Op->getOpcode() == ISD::BITCAST &&
3276 Op->getValueType(0) == MVT::f64) {
3277 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3278 // created by LowerConstantFP().
3279 SDValue BitcastOp = Op->getOperand(0);
3280 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3281 SDValue MoveOp = BitcastOp->getOperand(0);
3282 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3283 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3284 return true;
3285 }
3286 }
Evan Cheng10043e22007-01-19 07:51:42 +00003287 }
3288 return false;
3289}
3290
Evan Cheng10043e22007-01-19 07:51:42 +00003291/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3292/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003293SDValue
3294ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003295 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003296 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003297 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003298 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003299 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003300 // Constant does not fit, try adjusting it by one?
3301 switch (CC) {
3302 default: break;
3303 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003304 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003305 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003306 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003307 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003308 }
3309 break;
3310 case ISD::SETULT:
3311 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003312 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003313 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003314 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003315 }
3316 break;
3317 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003318 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003319 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003320 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003321 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003322 }
3323 break;
3324 case ISD::SETULE:
3325 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003326 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003327 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003328 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003329 }
3330 break;
3331 }
3332 }
3333 }
3334
3335 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003336 ARMISD::NodeType CompareType;
3337 switch (CondCode) {
3338 default:
3339 CompareType = ARMISD::CMP;
3340 break;
3341 case ARMCC::EQ:
3342 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003343 // Uses only Z Flag
3344 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003345 break;
3346 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003347 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003348 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003349}
3350
3351/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003352SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003353ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003354 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003355 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003356 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003357 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003358 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003359 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003360 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3361 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003362}
3363
Bob Wilson45acbd02011-03-08 01:17:20 +00003364/// duplicateCmp - Glue values can have only one use, so this function
3365/// duplicates a comparison node.
3366SDValue
3367ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3368 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003369 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003370 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3371 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3372
3373 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3374 Cmp = Cmp.getOperand(0);
3375 Opc = Cmp.getOpcode();
3376 if (Opc == ARMISD::CMPFP)
3377 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3378 else {
3379 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3380 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3381 }
3382 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3383}
3384
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003385std::pair<SDValue, SDValue>
3386ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3387 SDValue &ARMcc) const {
3388 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3389
3390 SDValue Value, OverflowCmp;
3391 SDValue LHS = Op.getOperand(0);
3392 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003393 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003394
3395 // FIXME: We are currently always generating CMPs because we don't support
3396 // generating CMN through the backend. This is not as good as the natural
3397 // CMP case because it causes a register dependency and cannot be folded
3398 // later.
3399
3400 switch (Op.getOpcode()) {
3401 default:
3402 llvm_unreachable("Unknown overflow instruction!");
3403 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003404 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3405 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3406 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003407 break;
3408 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003409 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3410 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3411 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003412 break;
3413 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003414 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3415 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3416 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003417 break;
3418 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003419 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3420 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3421 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003422 break;
3423 } // switch (...)
3424
3425 return std::make_pair(Value, OverflowCmp);
3426}
3427
3428
3429SDValue
3430ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3431 // Let legalize expand this if it isn't a legal type yet.
3432 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3433 return SDValue();
3434
3435 SDValue Value, OverflowCmp;
3436 SDValue ARMcc;
3437 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3438 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003439 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003440 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003441 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3442 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003443 EVT VT = Op.getValueType();
3444
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003445 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003446 ARMcc, CCR, OverflowCmp);
3447
3448 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003449 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003450}
3451
3452
Bill Wendling6a981312010-08-11 08:43:16 +00003453SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3454 SDValue Cond = Op.getOperand(0);
3455 SDValue SelectTrue = Op.getOperand(1);
3456 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003457 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003458 unsigned Opc = Cond.getOpcode();
3459
3460 if (Cond.getResNo() == 1 &&
3461 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3462 Opc == ISD::USUBO)) {
3463 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3464 return SDValue();
3465
3466 SDValue Value, OverflowCmp;
3467 SDValue ARMcc;
3468 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3469 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3470 EVT VT = Op.getValueType();
3471
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003472 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003473 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003474 }
Bill Wendling6a981312010-08-11 08:43:16 +00003475
3476 // Convert:
3477 //
3478 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3479 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3480 //
3481 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3482 const ConstantSDNode *CMOVTrue =
3483 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3484 const ConstantSDNode *CMOVFalse =
3485 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3486
3487 if (CMOVTrue && CMOVFalse) {
3488 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3489 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3490
3491 SDValue True;
3492 SDValue False;
3493 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3494 True = SelectTrue;
3495 False = SelectFalse;
3496 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3497 True = SelectFalse;
3498 False = SelectTrue;
3499 }
3500
3501 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003502 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003503 SDValue ARMcc = Cond.getOperand(2);
3504 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003505 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003506 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003507 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003508 }
3509 }
3510 }
3511
Dan Gohmand4a77c42012-02-24 00:09:36 +00003512 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3513 // undefined bits before doing a full-word comparison with zero.
3514 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003515 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003516
Bill Wendling6a981312010-08-11 08:43:16 +00003517 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003518 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003519 SelectTrue, SelectFalse, ISD::SETNE);
3520}
3521
Joey Gouly881eab52013-08-22 15:29:11 +00003522static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3523 bool &swpCmpOps, bool &swpVselOps) {
3524 // Start by selecting the GE condition code for opcodes that return true for
3525 // 'equality'
3526 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3527 CC == ISD::SETULE)
3528 CondCode = ARMCC::GE;
3529
3530 // and GT for opcodes that return false for 'equality'.
3531 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3532 CC == ISD::SETULT)
3533 CondCode = ARMCC::GT;
3534
3535 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3536 // to swap the compare operands.
3537 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3538 CC == ISD::SETULT)
3539 swpCmpOps = true;
3540
3541 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3542 // If we have an unordered opcode, we need to swap the operands to the VSEL
3543 // instruction (effectively negating the condition).
3544 //
3545 // This also has the effect of swapping which one of 'less' or 'greater'
3546 // returns true, so we also swap the compare operands. It also switches
3547 // whether we return true for 'equality', so we compensate by picking the
3548 // opposite condition code to our original choice.
3549 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3550 CC == ISD::SETUGT) {
3551 swpCmpOps = !swpCmpOps;
3552 swpVselOps = !swpVselOps;
3553 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3554 }
3555
3556 // 'ordered' is 'anything but unordered', so use the VS condition code and
3557 // swap the VSEL operands.
3558 if (CC == ISD::SETO) {
3559 CondCode = ARMCC::VS;
3560 swpVselOps = true;
3561 }
3562
3563 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3564 // code and swap the VSEL operands.
3565 if (CC == ISD::SETUNE) {
3566 CondCode = ARMCC::EQ;
3567 swpVselOps = true;
3568 }
3569}
3570
Oliver Stannard51b1d462014-08-21 12:50:31 +00003571SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3572 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3573 SDValue Cmp, SelectionDAG &DAG) const {
3574 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3575 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3576 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3577 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3578 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3579
3580 SDValue TrueLow = TrueVal.getValue(0);
3581 SDValue TrueHigh = TrueVal.getValue(1);
3582 SDValue FalseLow = FalseVal.getValue(0);
3583 SDValue FalseHigh = FalseVal.getValue(1);
3584
3585 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3586 ARMcc, CCR, Cmp);
3587 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3588 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3589
3590 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3591 } else {
3592 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3593 Cmp);
3594 }
3595}
3596
Dan Gohman21cea8a2010-04-17 15:26:15 +00003597SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003598 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003599 SDValue LHS = Op.getOperand(0);
3600 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003601 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003602 SDValue TrueVal = Op.getOperand(2);
3603 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003604 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003605
Oliver Stannard51b1d462014-08-21 12:50:31 +00003606 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3607 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3608 dl);
3609
3610 // If softenSetCCOperands only returned one value, we should compare it to
3611 // zero.
3612 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003613 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003614 CC = ISD::SETNE;
3615 }
3616 }
3617
Owen Anderson9f944592009-08-11 20:47:22 +00003618 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003619 // Try to generate VSEL on ARMv8.
3620 // The VSEL instruction can't use all the usual ARM condition
3621 // codes: it only has two bits to select the condition code, so it's
3622 // constrained to use only GE, GT, VS and EQ.
3623 //
3624 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3625 // swap the operands of the previous compare instruction (effectively
3626 // inverting the compare condition, swapping 'less' and 'greater') and
3627 // sometimes need to swap the operands to the VSEL (which inverts the
3628 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003629 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3630 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003631 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3632 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3633 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003634 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003635 std::swap(TrueVal, FalseVal);
3636 }
3637 }
3638
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003639 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003640 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003641 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003642 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003643 }
3644
3645 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003646 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003647
Scott Douglass7ad77922015-04-08 17:18:28 +00003648 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003649 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3650 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003651 bool swpCmpOps = false;
3652 bool swpVselOps = false;
3653 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3654
3655 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3656 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3657 if (swpCmpOps)
3658 std::swap(LHS, RHS);
3659 if (swpVselOps)
3660 std::swap(TrueVal, FalseVal);
3661 }
3662 }
3663
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003664 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003665 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003666 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003667 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003668 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003669 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003670 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003671 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003672 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003673 }
3674 return Result;
3675}
3676
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003677/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3678/// to morph to an integer compare sequence.
3679static bool canChangeToInt(SDValue Op, bool &SeenZero,
3680 const ARMSubtarget *Subtarget) {
3681 SDNode *N = Op.getNode();
3682 if (!N->hasOneUse())
3683 // Otherwise it requires moving the value from fp to integer registers.
3684 return false;
3685 if (!N->getNumValues())
3686 return false;
3687 EVT VT = Op.getValueType();
3688 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3689 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3690 // vmrs are very slow, e.g. cortex-a8.
3691 return false;
3692
3693 if (isFloatingPointZero(Op)) {
3694 SeenZero = true;
3695 return true;
3696 }
3697 return ISD::isNormalLoad(N);
3698}
3699
3700static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3701 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003702 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003703
3704 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003705 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003706 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003707 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003708 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003709
3710 llvm_unreachable("Unknown VFP cmp argument!");
3711}
3712
3713static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3714 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003715 SDLoc dl(Op);
3716
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003717 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003718 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3719 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003720 return;
3721 }
3722
3723 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3724 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003725 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003726 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003727 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003728 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003729 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003730
3731 EVT PtrType = Ptr.getValueType();
3732 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003733 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3734 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3735 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003736 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003737 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003738 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003739 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003740 return;
3741 }
3742
3743 llvm_unreachable("Unknown VFP cmp argument!");
3744}
3745
3746/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3747/// f32 and even f64 comparisons to integer ones.
3748SDValue
3749ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3750 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003751 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003752 SDValue LHS = Op.getOperand(2);
3753 SDValue RHS = Op.getOperand(3);
3754 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003755 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003756
Evan Chengd12af5d2012-03-01 23:27:13 +00003757 bool LHSSeenZero = false;
3758 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3759 bool RHSSeenZero = false;
3760 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3761 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003762 // If unsafe fp math optimization is enabled and there are no other uses of
3763 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003764 // to an integer comparison.
3765 if (CC == ISD::SETOEQ)
3766 CC = ISD::SETEQ;
3767 else if (CC == ISD::SETUNE)
3768 CC = ISD::SETNE;
3769
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003770 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003771 SDValue ARMcc;
3772 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003773 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3774 bitcastf32Toi32(LHS, DAG), Mask);
3775 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3776 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003777 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3778 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3779 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3780 Chain, Dest, ARMcc, CCR, Cmp);
3781 }
3782
3783 SDValue LHS1, LHS2;
3784 SDValue RHS1, RHS2;
3785 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3786 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003787 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3788 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003789 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003790 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003791 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003792 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003793 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003794 }
3795
3796 return SDValue();
3797}
3798
3799SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3800 SDValue Chain = Op.getOperand(0);
3801 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3802 SDValue LHS = Op.getOperand(2);
3803 SDValue RHS = Op.getOperand(3);
3804 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003805 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003806
Oliver Stannard51b1d462014-08-21 12:50:31 +00003807 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3808 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3809 dl);
3810
3811 // If softenSetCCOperands only returned one value, we should compare it to
3812 // zero.
3813 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003814 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003815 CC = ISD::SETNE;
3816 }
3817 }
3818
Owen Anderson9f944592009-08-11 20:47:22 +00003819 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003820 SDValue ARMcc;
3821 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003822 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003823 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003824 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003825 }
3826
Owen Anderson9f944592009-08-11 20:47:22 +00003827 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003828
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003829 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003830 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3831 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3832 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3833 if (Result.getNode())
3834 return Result;
3835 }
3836
Evan Cheng10043e22007-01-19 07:51:42 +00003837 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003838 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003839
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003840 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003841 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003842 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003843 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003844 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003845 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003846 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003847 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003848 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003849 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003850 }
3851 return Res;
3852}
3853
Dan Gohman21cea8a2010-04-17 15:26:15 +00003854SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003855 SDValue Chain = Op.getOperand(0);
3856 SDValue Table = Op.getOperand(1);
3857 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003858 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003859
Mehdi Amini44ede332015-07-09 02:09:04 +00003860 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003861 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003862 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003863 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003864 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003865 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003866 if (Subtarget->isThumb2()) {
3867 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3868 // which does another jump to the destination. This also makes it easier
3869 // to translate it to TBB / TBH later.
3870 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003871 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003872 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003873 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003874 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003875 Addr =
3876 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
3877 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3878 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003879 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003880 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003881 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003882 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003883 Addr =
3884 DAG.getLoad(PTy, dl, Chain, Addr,
3885 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3886 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003887 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003888 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003889 }
Evan Cheng10043e22007-01-19 07:51:42 +00003890}
3891
Eli Friedman2d4055b2011-11-09 23:36:02 +00003892static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003893 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003894 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003895
James Molloy547d4c02012-02-20 09:24:05 +00003896 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3897 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3898 return Op;
3899 return DAG.UnrollVectorOp(Op.getNode());
3900 }
3901
3902 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3903 "Invalid type for custom lowering!");
3904 if (VT != MVT::v4i16)
3905 return DAG.UnrollVectorOp(Op.getNode());
3906
3907 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3908 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003909}
3910
Oliver Stannard51b1d462014-08-21 12:50:31 +00003911SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003912 EVT VT = Op.getValueType();
3913 if (VT.isVector())
3914 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003915 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3916 RTLIB::Libcall LC;
3917 if (Op.getOpcode() == ISD::FP_TO_SINT)
3918 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3919 Op.getValueType());
3920 else
3921 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3922 Op.getValueType());
3923 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3924 /*isSigned*/ false, SDLoc(Op)).first;
3925 }
3926
James Molloyfa041152015-03-23 16:15:16 +00003927 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003928}
3929
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003930static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3931 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003932 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003933
Eli Friedman2d4055b2011-11-09 23:36:02 +00003934 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3935 if (VT.getVectorElementType() == MVT::f32)
3936 return Op;
3937 return DAG.UnrollVectorOp(Op.getNode());
3938 }
3939
Duncan Sandsa41634e2011-08-12 14:54:45 +00003940 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3941 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003942 if (VT != MVT::v4f32)
3943 return DAG.UnrollVectorOp(Op.getNode());
3944
3945 unsigned CastOpc;
3946 unsigned Opc;
3947 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003948 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003949 case ISD::SINT_TO_FP:
3950 CastOpc = ISD::SIGN_EXTEND;
3951 Opc = ISD::SINT_TO_FP;
3952 break;
3953 case ISD::UINT_TO_FP:
3954 CastOpc = ISD::ZERO_EXTEND;
3955 Opc = ISD::UINT_TO_FP;
3956 break;
3957 }
3958
3959 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3960 return DAG.getNode(Opc, dl, VT, Op);
3961}
3962
Oliver Stannard51b1d462014-08-21 12:50:31 +00003963SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003964 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003965 if (VT.isVector())
3966 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003967 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3968 RTLIB::Libcall LC;
3969 if (Op.getOpcode() == ISD::SINT_TO_FP)
3970 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3971 Op.getValueType());
3972 else
3973 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3974 Op.getValueType());
3975 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3976 /*isSigned*/ false, SDLoc(Op)).first;
3977 }
3978
James Molloyfa041152015-03-23 16:15:16 +00003979 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003980}
3981
Evan Cheng25f93642010-07-08 02:08:50 +00003982SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003983 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003984 SDValue Tmp0 = Op.getOperand(0);
3985 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003986 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003987 EVT VT = Op.getValueType();
3988 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003989 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3990 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3991 bool UseNEON = !InGPR && Subtarget->hasNEON();
3992
3993 if (UseNEON) {
3994 // Use VBSL to copy the sign bit.
3995 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3996 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003997 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003998 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3999 if (VT == MVT::f64)
4000 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4001 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004002 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004003 else /*if (VT == MVT::f32)*/
4004 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4005 if (SrcVT == MVT::f32) {
4006 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4007 if (VT == MVT::f64)
4008 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4009 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004010 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004011 } else if (VT == MVT::f32)
4012 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4013 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004014 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004015 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4016 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4017
4018 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004019 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004020 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4021 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4022 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004023
Evan Chengd6b641e2011-02-23 02:24:55 +00004024 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4025 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4026 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004027 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004028 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4029 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004030 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004031 } else {
4032 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4033 }
4034
4035 return Res;
4036 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004037
4038 // Bitcast operand 1 to i32.
4039 if (SrcVT == MVT::f64)
4040 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004041 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004042 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4043
Evan Chengd6b641e2011-02-23 02:24:55 +00004044 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004045 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4046 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004047 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4048 if (VT == MVT::f32) {
4049 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4050 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4051 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4052 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004053 }
4054
Evan Chengd6b641e2011-02-23 02:24:55 +00004055 // f64: Or the high part with signbit and then combine two parts.
4056 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004057 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004058 SDValue Lo = Tmp0.getValue(0);
4059 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4060 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4061 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004062}
4063
Evan Cheng168ced92010-05-22 01:47:14 +00004064SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4065 MachineFunction &MF = DAG.getMachineFunction();
4066 MachineFrameInfo *MFI = MF.getFrameInfo();
4067 MFI->setReturnAddressIsTaken(true);
4068
Bill Wendling908bf812014-01-06 00:43:20 +00004069 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004070 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004071
Evan Cheng168ced92010-05-22 01:47:14 +00004072 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004073 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004074 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4075 if (Depth) {
4076 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004077 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004078 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4079 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004080 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004081 }
4082
4083 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004084 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004085 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4086}
4087
Dan Gohman21cea8a2010-04-17 15:26:15 +00004088SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004089 const ARMBaseRegisterInfo &ARI =
4090 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4091 MachineFunction &MF = DAG.getMachineFunction();
4092 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004093 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004094
Owen Anderson53aa7a92009-08-10 22:56:29 +00004095 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004096 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004097 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004098 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004099 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4100 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004101 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4102 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004103 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004104 return FrameAddr;
4105}
4106
Renato Golinc7aea402014-05-06 16:51:25 +00004107// FIXME? Maybe this could be a TableGen attribute on some registers and
4108// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004109unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4110 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004111 unsigned Reg = StringSwitch<unsigned>(RegName)
4112 .Case("sp", ARM::SP)
4113 .Default(0);
4114 if (Reg)
4115 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004116 report_fatal_error(Twine("Invalid register name \""
4117 + StringRef(RegName) + "\"."));
4118}
4119
4120// Result is 64 bit value so split into two 32 bit values and return as a
4121// pair of values.
4122static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4123 SelectionDAG &DAG) {
4124 SDLoc DL(N);
4125
4126 // This function is only supposed to be called for i64 type destination.
4127 assert(N->getValueType(0) == MVT::i64
4128 && "ExpandREAD_REGISTER called for non-i64 type result.");
4129
4130 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4131 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4132 N->getOperand(0),
4133 N->getOperand(1));
4134
4135 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4136 Read.getValue(1)));
4137 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004138}
4139
Wesley Peck527da1b2010-11-23 03:31:01 +00004140/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004141/// expand a bit convert where either the source or destination type is i64 to
4142/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4143/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4144/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004145static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004146 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004147 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004148 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004149
Bob Wilson59b70ea2010-04-17 05:30:19 +00004150 // This function is only supposed to be called for i64 types, either as the
4151 // source or destination of the bit convert.
4152 EVT SrcVT = Op.getValueType();
4153 EVT DstVT = N->getValueType(0);
4154 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004155 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004156
Bob Wilson59b70ea2010-04-17 05:30:19 +00004157 // Turn i64->f64 into VMOVDRR.
4158 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004159 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004160 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004161 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004162 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004163 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004164 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004165 }
Bob Wilson7117a912009-03-20 22:42:55 +00004166
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004167 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004168 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004169 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004170 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004171 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004172 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4173 DAG.getVTList(MVT::i32, MVT::i32),
4174 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4175 else
4176 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4177 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004178 // Merge the pieces into a single i64 value.
4179 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4180 }
Bob Wilson7117a912009-03-20 22:42:55 +00004181
Bob Wilson59b70ea2010-04-17 05:30:19 +00004182 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004183}
4184
Bob Wilson2e076c42009-06-22 23:27:02 +00004185/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004186/// Zero vectors are used to represent vector negation and in those cases
4187/// will be implemented with the NEON VNEG instruction. However, VNEG does
4188/// not support i64 elements, so sometimes the zero vectors will need to be
4189/// explicitly constructed. Regardless, use a canonical VMOV to create the
4190/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004191static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004192 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004193 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004194 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004195 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4196 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004197 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004198}
4199
Jim Grosbach624fcb22009-10-31 21:00:56 +00004200/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4201/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004202SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4203 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004204 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4205 EVT VT = Op.getValueType();
4206 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004207 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004208 SDValue ShOpLo = Op.getOperand(0);
4209 SDValue ShOpHi = Op.getOperand(1);
4210 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004211 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004212 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004213
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004214 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4215
Jim Grosbach624fcb22009-10-31 21:00:56 +00004216 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004217 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004218 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4219 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004220 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004221 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4222 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004223 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004224
4225 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004226 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4227 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004228 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004229 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004230 CCR, Cmp);
4231
4232 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004233 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004234}
4235
Jim Grosbach5d994042009-10-31 19:38:01 +00004236/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4237/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004238SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4239 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004240 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4241 EVT VT = Op.getValueType();
4242 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004243 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004244 SDValue ShOpLo = Op.getOperand(0);
4245 SDValue ShOpHi = Op.getOperand(1);
4246 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004247 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004248
4249 assert(Op.getOpcode() == ISD::SHL_PARTS);
4250 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004251 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004252 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4253 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004254 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004255 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4256 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4257
4258 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4259 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004260 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4261 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004262 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004263 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004264 CCR, Cmp);
4265
4266 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004267 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004268}
4269
Jim Grosbach535d3b42010-09-08 03:54:02 +00004270SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004271 SelectionDAG &DAG) const {
4272 // The rounding mode is in bits 23:22 of the FPSCR.
4273 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4274 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4275 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004276 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004277 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004278 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004279 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004280 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004281 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004282 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004283 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004284 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004285 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004286}
4287
Jim Grosbach8546ec92010-01-18 19:58:49 +00004288static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4289 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004290 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004291 EVT VT = N->getValueType(0);
4292 if (VT.isVector()) {
4293 assert(ST->hasNEON());
4294
4295 // Compute the least significant set bit: LSB = X & -X
4296 SDValue X = N->getOperand(0);
4297 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4298 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4299
4300 EVT ElemTy = VT.getVectorElementType();
4301
4302 if (ElemTy == MVT::i8) {
4303 // Compute with: cttz(x) = ctpop(lsb - 1)
4304 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4305 DAG.getTargetConstant(1, dl, ElemTy));
4306 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4307 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4308 }
4309
4310 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4311 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4312 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4313 unsigned NumBits = ElemTy.getSizeInBits();
4314 SDValue WidthMinus1 =
4315 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4316 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4317 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4318 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4319 }
4320
4321 // Compute with: cttz(x) = ctpop(lsb - 1)
4322
4323 // Since we can only compute the number of bits in a byte with vcnt.8, we
4324 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4325 // and i64.
4326
4327 // Compute LSB - 1.
4328 SDValue Bits;
4329 if (ElemTy == MVT::i64) {
4330 // Load constant 0xffff'ffff'ffff'ffff to register.
4331 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4332 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4333 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4334 } else {
4335 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4336 DAG.getTargetConstant(1, dl, ElemTy));
4337 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4338 }
4339
4340 // Count #bits with vcnt.8.
4341 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4342 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4343 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4344
4345 // Gather the #bits with vpaddl (pairwise add.)
4346 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4347 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4348 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4349 Cnt8);
4350 if (ElemTy == MVT::i16)
4351 return Cnt16;
4352
4353 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4354 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4355 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4356 Cnt16);
4357 if (ElemTy == MVT::i32)
4358 return Cnt32;
4359
4360 assert(ElemTy == MVT::i64);
4361 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4362 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4363 Cnt32);
4364 return Cnt64;
4365 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004366
4367 if (!ST->hasV6T2Ops())
4368 return SDValue();
4369
4370 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4371 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4372}
4373
Evan Chengb4eae132012-12-04 22:41:50 +00004374/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4375/// for each 16-bit element from operand, repeated. The basic idea is to
4376/// leverage vcnt to get the 8-bit counts, gather and add the results.
4377///
4378/// Trace for v4i16:
4379/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4380/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4381/// 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 +00004382/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004383/// [b0 b1 b2 b3 b4 b5 b6 b7]
4384/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4385/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4386/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4387static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4388 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004389 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004390
4391 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4392 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4393 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4394 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4395 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4396 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4397}
4398
4399/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4400/// bit-count for each 16-bit element from the operand. We need slightly
4401/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4402/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004403///
Evan Chengb4eae132012-12-04 22:41:50 +00004404/// Trace for v4i16:
4405/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4406/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4407/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4408/// v4i16:Extracted = [k0 k1 k2 k3 ]
4409static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4410 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004411 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004412
4413 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4414 if (VT.is64BitVector()) {
4415 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4416 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004417 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004418 } else {
4419 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004420 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004421 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4422 }
4423}
4424
4425/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4426/// bit-count for each 32-bit element from the operand. The idea here is
4427/// to split the vector into 16-bit elements, leverage the 16-bit count
4428/// routine, and then combine the results.
4429///
4430/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4431/// input = [v0 v1 ] (vi: 32-bit elements)
4432/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4433/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004434/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004435/// [k0 k1 k2 k3 ]
4436/// N1 =+[k1 k0 k3 k2 ]
4437/// [k0 k2 k1 k3 ]
4438/// N2 =+[k1 k3 k0 k2 ]
4439/// [k0 k2 k1 k3 ]
4440/// Extended =+[k1 k3 k0 k2 ]
4441/// [k0 k2 ]
4442/// Extracted=+[k1 k3 ]
4443///
4444static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4445 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004446 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004447
4448 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4449
4450 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4451 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4452 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4453 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4454 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4455
4456 if (VT.is64BitVector()) {
4457 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4458 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004459 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004460 } else {
4461 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004462 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004463 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4464 }
4465}
4466
4467static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4468 const ARMSubtarget *ST) {
4469 EVT VT = N->getValueType(0);
4470
4471 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004472 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4473 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004474 "Unexpected type for custom ctpop lowering");
4475
4476 if (VT.getVectorElementType() == MVT::i32)
4477 return lowerCTPOP32BitElements(N, DAG);
4478 else
4479 return lowerCTPOP16BitElements(N, DAG);
4480}
4481
Bob Wilson2e076c42009-06-22 23:27:02 +00004482static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4483 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004484 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004485 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004486
Bob Wilson7d471332010-11-18 21:16:28 +00004487 if (!VT.isVector())
4488 return SDValue();
4489
Bob Wilson2e076c42009-06-22 23:27:02 +00004490 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004491 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004492
Bob Wilson7d471332010-11-18 21:16:28 +00004493 // Left shifts translate directly to the vshiftu intrinsic.
4494 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004495 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004496 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4497 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004498 N->getOperand(0), N->getOperand(1));
4499
4500 assert((N->getOpcode() == ISD::SRA ||
4501 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4502
4503 // NEON uses the same intrinsics for both left and right shifts. For
4504 // right shifts, the shift amounts are negative, so negate the vector of
4505 // shift amounts.
4506 EVT ShiftVT = N->getOperand(1).getValueType();
4507 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4508 getZeroVector(ShiftVT, DAG, dl),
4509 N->getOperand(1));
4510 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4511 Intrinsic::arm_neon_vshifts :
4512 Intrinsic::arm_neon_vshiftu);
4513 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004514 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004515 N->getOperand(0), NegatedCount);
4516}
4517
4518static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4519 const ARMSubtarget *ST) {
4520 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004521 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004522
Eli Friedman682d8c12009-08-22 03:13:10 +00004523 // We can get here for a node like i32 = ISD::SHL i32, i64
4524 if (VT != MVT::i64)
4525 return SDValue();
4526
4527 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004528 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004529
Chris Lattnerf81d5882007-11-24 07:07:01 +00004530 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4531 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004532 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004533 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004534
Chris Lattnerf81d5882007-11-24 07:07:01 +00004535 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004536 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004537
Chris Lattnerf81d5882007-11-24 07:07:01 +00004538 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004539 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004540 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004541 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004542 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004543
Chris Lattnerf81d5882007-11-24 07:07:01 +00004544 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4545 // captures the result into a carry flag.
4546 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004547 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004548
Chris Lattnerf81d5882007-11-24 07:07:01 +00004549 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004550 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004551
Chris Lattnerf81d5882007-11-24 07:07:01 +00004552 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004553 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004554}
4555
Bob Wilson2e076c42009-06-22 23:27:02 +00004556static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4557 SDValue TmpOp0, TmpOp1;
4558 bool Invert = false;
4559 bool Swap = false;
4560 unsigned Opc = 0;
4561
4562 SDValue Op0 = Op.getOperand(0);
4563 SDValue Op1 = Op.getOperand(1);
4564 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004565 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004566 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004567 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004568 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004569
James Molloybf170092015-08-20 16:33:44 +00004570 if (CmpVT.getVectorElementType() == MVT::i64)
4571 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4572 // but it's possible that our operands are 64-bit but our result is 32-bit.
4573 // Bail in this case.
4574 return SDValue();
4575
Oliver Stannard51b1d462014-08-21 12:50:31 +00004576 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004577 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004578 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004579 case ISD::SETUNE:
4580 case ISD::SETNE: Invert = true; // Fallthrough
4581 case ISD::SETOEQ:
4582 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4583 case ISD::SETOLT:
4584 case ISD::SETLT: Swap = true; // Fallthrough
4585 case ISD::SETOGT:
4586 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4587 case ISD::SETOLE:
4588 case ISD::SETLE: Swap = true; // Fallthrough
4589 case ISD::SETOGE:
4590 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4591 case ISD::SETUGE: Swap = true; // Fallthrough
4592 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4593 case ISD::SETUGT: Swap = true; // Fallthrough
4594 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4595 case ISD::SETUEQ: Invert = true; // Fallthrough
4596 case ISD::SETONE:
4597 // Expand this to (OLT | OGT).
4598 TmpOp0 = Op0;
4599 TmpOp1 = Op1;
4600 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004601 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4602 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004603 break;
4604 case ISD::SETUO: Invert = true; // Fallthrough
4605 case ISD::SETO:
4606 // Expand this to (OLT | OGE).
4607 TmpOp0 = Op0;
4608 TmpOp1 = Op1;
4609 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004610 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4611 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004612 break;
4613 }
4614 } else {
4615 // Integer comparisons.
4616 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004617 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004618 case ISD::SETNE: Invert = true;
4619 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4620 case ISD::SETLT: Swap = true;
4621 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4622 case ISD::SETLE: Swap = true;
4623 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4624 case ISD::SETULT: Swap = true;
4625 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4626 case ISD::SETULE: Swap = true;
4627 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4628 }
4629
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004630 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004631 if (Opc == ARMISD::VCEQ) {
4632
4633 SDValue AndOp;
4634 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4635 AndOp = Op0;
4636 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4637 AndOp = Op1;
4638
4639 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004640 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004641 AndOp = AndOp.getOperand(0);
4642
4643 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4644 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004645 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4646 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004647 Invert = !Invert;
4648 }
4649 }
4650 }
4651
4652 if (Swap)
4653 std::swap(Op0, Op1);
4654
Owen Andersonc7baee32010-11-08 23:21:22 +00004655 // If one of the operands is a constant vector zero, attempt to fold the
4656 // comparison to a specialized compare-against-zero form.
4657 SDValue SingleOp;
4658 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4659 SingleOp = Op0;
4660 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4661 if (Opc == ARMISD::VCGE)
4662 Opc = ARMISD::VCLEZ;
4663 else if (Opc == ARMISD::VCGT)
4664 Opc = ARMISD::VCLTZ;
4665 SingleOp = Op1;
4666 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004667
Owen Andersonc7baee32010-11-08 23:21:22 +00004668 SDValue Result;
4669 if (SingleOp.getNode()) {
4670 switch (Opc) {
4671 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004672 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004673 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004674 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004675 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004676 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004677 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004678 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004679 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004680 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004681 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004682 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004683 }
4684 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004685 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004686 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004687
Tim Northover45aa89c2015-02-08 00:50:47 +00004688 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4689
Bob Wilson2e076c42009-06-22 23:27:02 +00004690 if (Invert)
4691 Result = DAG.getNOT(dl, Result, VT);
4692
4693 return Result;
4694}
4695
Bob Wilson5b2b5042010-06-14 22:19:57 +00004696/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4697/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004698/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004699static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4700 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004701 SDLoc dl, EVT &VT, bool is128Bits,
4702 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004703 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004704
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004705 // SplatBitSize is set to the smallest size that splats the vector, so a
4706 // zero vector will always have SplatBitSize == 8. However, NEON modified
4707 // immediate instructions others than VMOV do not support the 8-bit encoding
4708 // of a zero vector, and the default encoding of zero is supposed to be the
4709 // 32-bit version.
4710 if (SplatBits == 0)
4711 SplatBitSize = 32;
4712
Bob Wilson2e076c42009-06-22 23:27:02 +00004713 switch (SplatBitSize) {
4714 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004715 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004716 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004717 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004718 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004719 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004720 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004721 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004722 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004723
4724 case 16:
4725 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004726 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004727 if ((SplatBits & ~0xff) == 0) {
4728 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004729 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004730 Imm = SplatBits;
4731 break;
4732 }
4733 if ((SplatBits & ~0xff00) == 0) {
4734 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004735 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004736 Imm = SplatBits >> 8;
4737 break;
4738 }
4739 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004740
4741 case 32:
4742 // NEON's 32-bit VMOV supports splat values where:
4743 // * only one byte is nonzero, or
4744 // * the least significant byte is 0xff and the second byte is nonzero, or
4745 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004746 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004747 if ((SplatBits & ~0xff) == 0) {
4748 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004749 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004750 Imm = SplatBits;
4751 break;
4752 }
4753 if ((SplatBits & ~0xff00) == 0) {
4754 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004755 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004756 Imm = SplatBits >> 8;
4757 break;
4758 }
4759 if ((SplatBits & ~0xff0000) == 0) {
4760 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004761 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004762 Imm = SplatBits >> 16;
4763 break;
4764 }
4765 if ((SplatBits & ~0xff000000) == 0) {
4766 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004767 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004768 Imm = SplatBits >> 24;
4769 break;
4770 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004771
Owen Andersona4076922010-11-05 21:57:54 +00004772 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4773 if (type == OtherModImm) return SDValue();
4774
Bob Wilson2e076c42009-06-22 23:27:02 +00004775 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004776 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4777 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004778 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004779 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004780 break;
4781 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004782
4783 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004784 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4785 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004786 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004787 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004788 break;
4789 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004790
4791 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4792 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4793 // VMOV.I32. A (very) minor optimization would be to replicate the value
4794 // and fall through here to test for a valid 64-bit splat. But, then the
4795 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004796 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004797
4798 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004799 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004800 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004801 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004802 uint64_t BitMask = 0xff;
4803 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004804 unsigned ImmMask = 1;
4805 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004806 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004807 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004808 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004809 Imm |= ImmMask;
4810 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004811 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004812 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004813 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004814 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004815 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004816
Mehdi Aminiffc14022015-07-08 01:00:38 +00004817 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004818 // swap higher and lower 32 bit word
4819 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4820
Bob Wilson6eae5202010-06-11 21:34:50 +00004821 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004822 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004823 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004824 break;
4825 }
4826
Bob Wilson6eae5202010-06-11 21:34:50 +00004827 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004828 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004829 }
4830
Bob Wilsona3f19012010-07-13 21:16:48 +00004831 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004832 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004833}
4834
Lang Hames591cdaf2012-03-29 21:56:11 +00004835SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4836 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004837 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004838 return SDValue();
4839
Tim Northoverf79c3a52013-08-20 08:57:11 +00004840 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004841 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004842
Oliver Stannard51b1d462014-08-21 12:50:31 +00004843 // Use the default (constant pool) lowering for double constants when we have
4844 // an SP-only FPU
4845 if (IsDouble && Subtarget->isFPOnlySP())
4846 return SDValue();
4847
Lang Hames591cdaf2012-03-29 21:56:11 +00004848 // Try splatting with a VMOV.f32...
4849 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004850 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4851
Lang Hames591cdaf2012-03-29 21:56:11 +00004852 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004853 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4854 // We have code in place to select a valid ConstantFP already, no need to
4855 // do any mangling.
4856 return Op;
4857 }
4858
4859 // It's a float and we are trying to use NEON operations where
4860 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004861 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004862 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004863 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4864 NewVal);
4865 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004866 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004867 }
4868
Tim Northoverf79c3a52013-08-20 08:57:11 +00004869 // The rest of our options are NEON only, make sure that's allowed before
4870 // proceeding..
4871 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4872 return SDValue();
4873
Lang Hames591cdaf2012-03-29 21:56:11 +00004874 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004875 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4876
4877 // It wouldn't really be worth bothering for doubles except for one very
4878 // important value, which does happen to match: 0.0. So make sure we don't do
4879 // anything stupid.
4880 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4881 return SDValue();
4882
4883 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004884 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4885 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004886 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004887 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004888 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4889 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004890 if (IsDouble)
4891 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4892
4893 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004894 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4895 VecConstant);
4896 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004897 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004898 }
4899
4900 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004901 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004902 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004903 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004904 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004905 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004906
4907 if (IsDouble)
4908 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4909
4910 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004911 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4912 VecConstant);
4913 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004914 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004915 }
4916
4917 return SDValue();
4918}
4919
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004920// check if an VEXT instruction can handle the shuffle mask when the
4921// vector sources of the shuffle are the same.
4922static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4923 unsigned NumElts = VT.getVectorNumElements();
4924
4925 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4926 if (M[0] < 0)
4927 return false;
4928
4929 Imm = M[0];
4930
4931 // If this is a VEXT shuffle, the immediate value is the index of the first
4932 // element. The other shuffle indices must be the successive elements after
4933 // the first one.
4934 unsigned ExpectedElt = Imm;
4935 for (unsigned i = 1; i < NumElts; ++i) {
4936 // Increment the expected index. If it wraps around, just follow it
4937 // back to index zero and keep going.
4938 ++ExpectedElt;
4939 if (ExpectedElt == NumElts)
4940 ExpectedElt = 0;
4941
4942 if (M[i] < 0) continue; // ignore UNDEF indices
4943 if (ExpectedElt != static_cast<unsigned>(M[i]))
4944 return false;
4945 }
4946
4947 return true;
4948}
4949
Lang Hames591cdaf2012-03-29 21:56:11 +00004950
Benjamin Kramer339ced42012-01-15 13:16:05 +00004951static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004952 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004953 unsigned NumElts = VT.getVectorNumElements();
4954 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004955
4956 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4957 if (M[0] < 0)
4958 return false;
4959
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004960 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004961
4962 // If this is a VEXT shuffle, the immediate value is the index of the first
4963 // element. The other shuffle indices must be the successive elements after
4964 // the first one.
4965 unsigned ExpectedElt = Imm;
4966 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004967 // Increment the expected index. If it wraps around, it may still be
4968 // a VEXT but the source vectors must be swapped.
4969 ExpectedElt += 1;
4970 if (ExpectedElt == NumElts * 2) {
4971 ExpectedElt = 0;
4972 ReverseVEXT = true;
4973 }
4974
Bob Wilson411dfad2010-08-17 05:54:34 +00004975 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004976 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004977 return false;
4978 }
4979
4980 // Adjust the index value if the source operands will be swapped.
4981 if (ReverseVEXT)
4982 Imm -= NumElts;
4983
Bob Wilson32cd8552009-08-19 17:03:43 +00004984 return true;
4985}
4986
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004987/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4988/// instruction with the specified blocksize. (The order of the elements
4989/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004990static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004991 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4992 "Only possible block sizes for VREV are: 16, 32, 64");
4993
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004994 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004995 if (EltSz == 64)
4996 return false;
4997
4998 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004999 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005000 // If the first shuffle index is UNDEF, be optimistic.
5001 if (M[0] < 0)
5002 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005003
5004 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5005 return false;
5006
5007 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005008 if (M[i] < 0) continue; // ignore UNDEF indices
5009 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005010 return false;
5011 }
5012
5013 return true;
5014}
5015
Benjamin Kramer339ced42012-01-15 13:16:05 +00005016static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005017 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5018 // range, then 0 is placed into the resulting vector. So pretty much any mask
5019 // of 8 elements can work here.
5020 return VT == MVT::v8i8 && M.size() == 8;
5021}
5022
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005023// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5024// checking that pairs of elements in the shuffle mask represent the same index
5025// in each vector, incrementing the expected index by 2 at each step.
5026// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5027// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5028// v2={e,f,g,h}
5029// WhichResult gives the offset for each element in the mask based on which
5030// of the two results it belongs to.
5031//
5032// The transpose can be represented either as:
5033// result1 = shufflevector v1, v2, result1_shuffle_mask
5034// result2 = shufflevector v1, v2, result2_shuffle_mask
5035// where v1/v2 and the shuffle masks have the same number of elements
5036// (here WhichResult (see below) indicates which result is being checked)
5037//
5038// or as:
5039// results = shufflevector v1, v2, shuffle_mask
5040// where both results are returned in one vector and the shuffle mask has twice
5041// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5042// want to check the low half and high half of the shuffle mask as if it were
5043// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005044static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005045 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5046 if (EltSz == 64)
5047 return false;
5048
Bob Wilsona7062312009-08-21 20:54:19 +00005049 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005050 if (M.size() != NumElts && M.size() != NumElts*2)
5051 return false;
5052
James Molloy8c995a92015-09-10 08:42:28 +00005053 // If the mask is twice as long as the input vector then we need to check the
5054 // upper and lower parts of the mask with a matching value for WhichResult
5055 // FIXME: A mask with only even values will be rejected in case the first
5056 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5057 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005058 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005059 if (M.size() == NumElts * 2)
5060 WhichResult = i / NumElts;
5061 else
5062 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005063 for (unsigned j = 0; j < NumElts; j += 2) {
5064 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5065 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5066 return false;
5067 }
Bob Wilsona7062312009-08-21 20:54:19 +00005068 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005069
5070 if (M.size() == NumElts*2)
5071 WhichResult = 0;
5072
Bob Wilsona7062312009-08-21 20:54:19 +00005073 return true;
5074}
5075
Bob Wilson0bbd3072009-12-03 06:40:55 +00005076/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5077/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5078/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005079static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005080 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5081 if (EltSz == 64)
5082 return false;
5083
5084 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005085 if (M.size() != NumElts && M.size() != NumElts*2)
5086 return false;
5087
5088 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005089 if (M.size() == NumElts * 2)
5090 WhichResult = i / NumElts;
5091 else
5092 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005093 for (unsigned j = 0; j < NumElts; j += 2) {
5094 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5095 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5096 return false;
5097 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005098 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005099
5100 if (M.size() == NumElts*2)
5101 WhichResult = 0;
5102
Bob Wilson0bbd3072009-12-03 06:40:55 +00005103 return true;
5104}
5105
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005106// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5107// that the mask elements are either all even and in steps of size 2 or all odd
5108// and in steps of size 2.
5109// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5110// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5111// v2={e,f,g,h}
5112// Requires similar checks to that of isVTRNMask with
5113// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005114static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005115 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5116 if (EltSz == 64)
5117 return false;
5118
Bob Wilsona7062312009-08-21 20:54:19 +00005119 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005120 if (M.size() != NumElts && M.size() != NumElts*2)
5121 return false;
5122
5123 for (unsigned i = 0; i < M.size(); i += NumElts) {
5124 WhichResult = M[i] == 0 ? 0 : 1;
5125 for (unsigned j = 0; j < NumElts; ++j) {
5126 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5127 return false;
5128 }
Bob Wilsona7062312009-08-21 20:54:19 +00005129 }
5130
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005131 if (M.size() == NumElts*2)
5132 WhichResult = 0;
5133
Bob Wilsona7062312009-08-21 20:54:19 +00005134 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005135 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005136 return false;
5137
5138 return true;
5139}
5140
Bob Wilson0bbd3072009-12-03 06:40:55 +00005141/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5142/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5143/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005144static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005145 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5146 if (EltSz == 64)
5147 return false;
5148
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005149 unsigned NumElts = VT.getVectorNumElements();
5150 if (M.size() != NumElts && M.size() != NumElts*2)
5151 return false;
5152
5153 unsigned Half = NumElts / 2;
5154 for (unsigned i = 0; i < M.size(); i += NumElts) {
5155 WhichResult = M[i] == 0 ? 0 : 1;
5156 for (unsigned j = 0; j < NumElts; j += Half) {
5157 unsigned Idx = WhichResult;
5158 for (unsigned k = 0; k < Half; ++k) {
5159 int MIdx = M[i + j + k];
5160 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5161 return false;
5162 Idx += 2;
5163 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005164 }
5165 }
5166
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005167 if (M.size() == NumElts*2)
5168 WhichResult = 0;
5169
Bob Wilson0bbd3072009-12-03 06:40:55 +00005170 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5171 if (VT.is64BitVector() && EltSz == 32)
5172 return false;
5173
5174 return true;
5175}
5176
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005177// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5178// that pairs of elements of the shufflemask represent the same index in each
5179// vector incrementing sequentially through the vectors.
5180// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5181// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5182// v2={e,f,g,h}
5183// Requires similar checks to that of isVTRNMask with respect the how results
5184// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005185static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005186 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5187 if (EltSz == 64)
5188 return false;
5189
Bob Wilsona7062312009-08-21 20:54:19 +00005190 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005191 if (M.size() != NumElts && M.size() != NumElts*2)
5192 return false;
5193
5194 for (unsigned i = 0; i < M.size(); i += NumElts) {
5195 WhichResult = M[i] == 0 ? 0 : 1;
5196 unsigned Idx = WhichResult * NumElts / 2;
5197 for (unsigned j = 0; j < NumElts; j += 2) {
5198 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5199 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5200 return false;
5201 Idx += 1;
5202 }
Bob Wilsona7062312009-08-21 20:54:19 +00005203 }
5204
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005205 if (M.size() == NumElts*2)
5206 WhichResult = 0;
5207
Bob Wilsona7062312009-08-21 20:54:19 +00005208 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005209 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005210 return false;
5211
5212 return true;
5213}
5214
Bob Wilson0bbd3072009-12-03 06:40:55 +00005215/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5216/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5217/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005218static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005219 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5220 if (EltSz == 64)
5221 return false;
5222
5223 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005224 if (M.size() != NumElts && M.size() != NumElts*2)
5225 return false;
5226
5227 for (unsigned i = 0; i < M.size(); i += NumElts) {
5228 WhichResult = M[i] == 0 ? 0 : 1;
5229 unsigned Idx = WhichResult * NumElts / 2;
5230 for (unsigned j = 0; j < NumElts; j += 2) {
5231 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5232 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5233 return false;
5234 Idx += 1;
5235 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005236 }
5237
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005238 if (M.size() == NumElts*2)
5239 WhichResult = 0;
5240
Bob Wilson0bbd3072009-12-03 06:40:55 +00005241 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5242 if (VT.is64BitVector() && EltSz == 32)
5243 return false;
5244
5245 return true;
5246}
5247
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005248/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5249/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5250static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5251 unsigned &WhichResult,
5252 bool &isV_UNDEF) {
5253 isV_UNDEF = false;
5254 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5255 return ARMISD::VTRN;
5256 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5257 return ARMISD::VUZP;
5258 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5259 return ARMISD::VZIP;
5260
5261 isV_UNDEF = true;
5262 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5263 return ARMISD::VTRN;
5264 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5265 return ARMISD::VUZP;
5266 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5267 return ARMISD::VZIP;
5268
5269 return 0;
5270}
5271
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005272/// \return true if this is a reverse operation on an vector.
5273static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5274 unsigned NumElts = VT.getVectorNumElements();
5275 // Make sure the mask has the right size.
5276 if (NumElts != M.size())
5277 return false;
5278
5279 // Look for <15, ..., 3, -1, 1, 0>.
5280 for (unsigned i = 0; i != NumElts; ++i)
5281 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5282 return false;
5283
5284 return true;
5285}
5286
Dale Johannesen2bff5052010-07-29 20:10:08 +00005287// If N is an integer constant that can be moved into a register in one
5288// instruction, return an SDValue of such a constant (will become a MOV
5289// instruction). Otherwise return null.
5290static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005291 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005292 uint64_t Val;
5293 if (!isa<ConstantSDNode>(N))
5294 return SDValue();
5295 Val = cast<ConstantSDNode>(N)->getZExtValue();
5296
5297 if (ST->isThumb1Only()) {
5298 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005299 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005300 } else {
5301 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005302 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005303 }
5304 return SDValue();
5305}
5306
Bob Wilson2e076c42009-06-22 23:27:02 +00005307// If this is a case we can't handle, return null and let the default
5308// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005309SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5310 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005311 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005312 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005313 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005314
5315 APInt SplatBits, SplatUndef;
5316 unsigned SplatBitSize;
5317 bool HasAnyUndefs;
5318 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005319 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005320 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005321 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005322 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005323 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005324 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005325 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005326 if (Val.getNode()) {
5327 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005328 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005329 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005330
5331 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005332 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005333 Val = isNEONModifiedImm(NegatedImm,
5334 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005335 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005336 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005337 if (Val.getNode()) {
5338 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005339 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005340 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005341
5342 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005343 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005344 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005345 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005346 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005347 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5348 }
5349 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005350 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005351 }
5352
Bob Wilson91fdf682010-05-22 00:23:12 +00005353 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005354 //
5355 // As an optimisation, even if more than one value is used it may be more
5356 // profitable to splat with one value then change some lanes.
5357 //
5358 // Heuristically we decide to do this if the vector has a "dominant" value,
5359 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005360 unsigned NumElts = VT.getVectorNumElements();
5361 bool isOnlyLowElement = true;
5362 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005363 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005364 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005365
5366 // Map of the number of times a particular SDValue appears in the
5367 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005368 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005369 SDValue Value;
5370 for (unsigned i = 0; i < NumElts; ++i) {
5371 SDValue V = Op.getOperand(i);
5372 if (V.getOpcode() == ISD::UNDEF)
5373 continue;
5374 if (i > 0)
5375 isOnlyLowElement = false;
5376 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5377 isConstant = false;
5378
James Molloy49bdbce2012-09-06 09:55:02 +00005379 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005380 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005381
James Molloy49bdbce2012-09-06 09:55:02 +00005382 // Is this value dominant? (takes up more than half of the lanes)
5383 if (++Count > (NumElts / 2)) {
5384 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005385 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005386 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005387 }
James Molloy49bdbce2012-09-06 09:55:02 +00005388 if (ValueCounts.size() != 1)
5389 usesOnlyOneValue = false;
5390 if (!Value.getNode() && ValueCounts.size() > 0)
5391 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005392
James Molloy49bdbce2012-09-06 09:55:02 +00005393 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005394 return DAG.getUNDEF(VT);
5395
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005396 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5397 // Keep going if we are hitting this case.
5398 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005399 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5400
Dale Johannesen2bff5052010-07-29 20:10:08 +00005401 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5402
Dale Johannesen710a2d92010-10-19 20:00:17 +00005403 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5404 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005405 if (hasDominantValue && EltSize <= 32) {
5406 if (!isConstant) {
5407 SDValue N;
5408
5409 // If we are VDUPing a value that comes directly from a vector, that will
5410 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005411 // just use VDUPLANE. We can only do this if the lane being extracted
5412 // is at a constant index, as the VDUP from lane instructions only have
5413 // constant-index forms.
5414 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5415 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005416 // We need to create a new undef vector to use for the VDUPLANE if the
5417 // size of the vector from which we get the value is different than the
5418 // size of the vector that we need to create. We will insert the element
5419 // such that the register coalescer will remove unnecessary copies.
5420 if (VT != Value->getOperand(0).getValueType()) {
5421 ConstantSDNode *constIndex;
5422 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5423 assert(constIndex && "The index is not a constant!");
5424 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5425 VT.getVectorNumElements();
5426 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5427 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005428 Value, DAG.getConstant(index, dl, MVT::i32)),
5429 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005430 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005431 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005432 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005433 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005434 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5435
5436 if (!usesOnlyOneValue) {
5437 // The dominant value was splatted as 'N', but we now have to insert
5438 // all differing elements.
5439 for (unsigned I = 0; I < NumElts; ++I) {
5440 if (Op.getOperand(I) == Value)
5441 continue;
5442 SmallVector<SDValue, 3> Ops;
5443 Ops.push_back(N);
5444 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005445 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005446 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005447 }
5448 }
5449 return N;
5450 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005451 if (VT.getVectorElementType().isFloatingPoint()) {
5452 SmallVector<SDValue, 8> Ops;
5453 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005454 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005455 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005456 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005457 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005458 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5459 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005460 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005461 }
James Molloy49bdbce2012-09-06 09:55:02 +00005462 if (usesOnlyOneValue) {
5463 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5464 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005465 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005466 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005467 }
5468
5469 // If all elements are constants and the case above didn't get hit, fall back
5470 // to the default expansion, which will generate a load from the constant
5471 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005472 if (isConstant)
5473 return SDValue();
5474
Bob Wilson6f2b8962011-01-07 21:37:30 +00005475 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5476 if (NumElts >= 4) {
5477 SDValue shuffle = ReconstructShuffle(Op, DAG);
5478 if (shuffle != SDValue())
5479 return shuffle;
5480 }
5481
Bob Wilson91fdf682010-05-22 00:23:12 +00005482 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005483 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5484 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005485 if (EltSize >= 32) {
5486 // Do the expansion with floating-point types, since that is what the VFP
5487 // registers are defined to use, and since i64 is not legal.
5488 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5489 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005490 SmallVector<SDValue, 8> Ops;
5491 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005492 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005493 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005494 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005495 }
5496
Jim Grosbach24e102a2013-07-08 18:18:52 +00005497 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5498 // know the default expansion would otherwise fall back on something even
5499 // worse. For a vector with one or two non-undef values, that's
5500 // scalar_to_vector for the elements followed by a shuffle (provided the
5501 // shuffle is valid for the target) and materialization element by element
5502 // on the stack followed by a load for everything else.
5503 if (!isConstant && !usesOnlyOneValue) {
5504 SDValue Vec = DAG.getUNDEF(VT);
5505 for (unsigned i = 0 ; i < NumElts; ++i) {
5506 SDValue V = Op.getOperand(i);
5507 if (V.getOpcode() == ISD::UNDEF)
5508 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005509 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005510 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5511 }
5512 return Vec;
5513 }
5514
Bob Wilson2e076c42009-06-22 23:27:02 +00005515 return SDValue();
5516}
5517
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005518/// getExtFactor - Determine the adjustment factor for the position when
5519/// generating an "extract from vector registers" instruction.
5520static unsigned getExtFactor(SDValue &V) {
5521 EVT EltType = V.getValueType().getVectorElementType();
5522 return EltType.getSizeInBits() / 8;
5523}
5524
Bob Wilson6f2b8962011-01-07 21:37:30 +00005525// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005526// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005527SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5528 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005529 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005530 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005531 EVT VT = Op.getValueType();
5532 unsigned NumElts = VT.getVectorNumElements();
5533
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005534 struct ShuffleSourceInfo {
5535 SDValue Vec;
5536 unsigned MinElt;
5537 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005538
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005539 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5540 // be compatible with the shuffle we intend to construct. As a result
5541 // ShuffleVec will be some sliding window into the original Vec.
5542 SDValue ShuffleVec;
5543
5544 // Code should guarantee that element i in Vec starts at element "WindowBase
5545 // + i * WindowScale in ShuffleVec".
5546 int WindowBase;
5547 int WindowScale;
5548
5549 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5550 ShuffleSourceInfo(SDValue Vec)
5551 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5552 WindowScale(1) {}
5553 };
5554
5555 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5556 // node.
5557 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005558 for (unsigned i = 0; i < NumElts; ++i) {
5559 SDValue V = Op.getOperand(i);
5560 if (V.getOpcode() == ISD::UNDEF)
5561 continue;
5562 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5563 // A shuffle can only come from building a vector from various
5564 // elements of other vectors.
5565 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005566 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5567 // Furthermore, shuffles require a constant mask, whereas extractelts
5568 // accept variable indices.
5569 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005570 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005571
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005572 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005573 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005574 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5575 if (Source == Sources.end())
5576 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005577
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005578 // Update the minimum and maximum lane number seen.
5579 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5580 Source->MinElt = std::min(Source->MinElt, EltNo);
5581 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005582 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005583
Bob Wilson6f2b8962011-01-07 21:37:30 +00005584 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005585 // are involved.
5586 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005587 return SDValue();
5588
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005589 // Find out the smallest element size among result and two sources, and use
5590 // it as element size to build the shuffle_vector.
5591 EVT SmallestEltTy = VT.getVectorElementType();
5592 for (auto &Source : Sources) {
5593 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5594 if (SrcEltTy.bitsLT(SmallestEltTy))
5595 SmallestEltTy = SrcEltTy;
5596 }
5597 unsigned ResMultiplier =
5598 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5599 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5600 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005601
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005602 // If the source vector is too wide or too narrow, we may nevertheless be able
5603 // to construct a compatible shuffle either by concatenating it with UNDEF or
5604 // extracting a suitable range of elements.
5605 for (auto &Src : Sources) {
5606 EVT SrcVT = Src.ShuffleVec.getValueType();
5607
5608 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005609 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005610
5611 // This stage of the search produces a source with the same element type as
5612 // the original, but with a total width matching the BUILD_VECTOR output.
5613 EVT EltVT = SrcVT.getVectorElementType();
5614 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5615 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5616
5617 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5618 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5619 return SDValue();
5620 // We can pad out the smaller vector for free, so if it's part of a
5621 // shuffle...
5622 Src.ShuffleVec =
5623 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5624 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5625 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005626 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005627
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005628 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5629 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005630
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005631 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005632 // Span too large for a VEXT to cope
5633 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005634 }
5635
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005636 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005637 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005638 Src.ShuffleVec =
5639 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5640 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5641 Src.WindowBase = -NumSrcElts;
5642 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005643 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005644 Src.ShuffleVec =
5645 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5646 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005647 } else {
5648 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005649 SDValue VEXTSrc1 =
5650 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5651 DAG.getConstant(0, dl, MVT::i32));
5652 SDValue VEXTSrc2 =
5653 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5654 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5655 unsigned Imm = Src.MinElt * getExtFactor(VEXTSrc1);
5656
5657 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5658 VEXTSrc2,
5659 DAG.getConstant(Imm, dl, MVT::i32));
5660 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005661 }
5662 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005663
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005664 // Another possible incompatibility occurs from the vector element types. We
5665 // can fix this by bitcasting the source vectors to the same type we intend
5666 // for the shuffle.
5667 for (auto &Src : Sources) {
5668 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5669 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005670 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005671 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5672 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5673 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5674 Src.WindowBase *= Src.WindowScale;
5675 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005676
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005677 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005678 DEBUG(
5679 for (auto Src : Sources)
5680 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5681 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005682
5683 // The stars all align, our next step is to produce the mask for the shuffle.
5684 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5685 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5686 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5687 SDValue Entry = Op.getOperand(i);
5688 if (Entry.getOpcode() == ISD::UNDEF)
5689 continue;
5690
5691 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5692 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5693
5694 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5695 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5696 // segment.
5697 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5698 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5699 VT.getVectorElementType().getSizeInBits());
5700 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5701
5702 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5703 // starting at the appropriate offset.
5704 int *LaneMask = &Mask[i * ResMultiplier];
5705
5706 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5707 ExtractBase += NumElts * (Src - Sources.begin());
5708 for (int j = 0; j < LanesDefined; ++j)
5709 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005710 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005711
Bob Wilson6f2b8962011-01-07 21:37:30 +00005712 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005713 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5714 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005715
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005716 // We can't handle more than two sources. This should have already
5717 // been checked before this point.
5718 assert(Sources.size() <= 2 && "Too many sources!");
5719
5720 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5721 for (unsigned i = 0; i < Sources.size(); ++i)
5722 ShuffleOps[i] = Sources[i].ShuffleVec;
5723
5724 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5725 ShuffleOps[1], &Mask[0]);
5726 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005727}
5728
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005729/// isShuffleMaskLegal - Targets can use this to indicate that they only
5730/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5731/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5732/// are assumed to be legal.
5733bool
5734ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5735 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005736 if (VT.getVectorNumElements() == 4 &&
5737 (VT.is128BitVector() || VT.is64BitVector())) {
5738 unsigned PFIndexes[4];
5739 for (unsigned i = 0; i != 4; ++i) {
5740 if (M[i] < 0)
5741 PFIndexes[i] = 8;
5742 else
5743 PFIndexes[i] = M[i];
5744 }
5745
5746 // Compute the index in the perfect shuffle table.
5747 unsigned PFTableIndex =
5748 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5749 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5750 unsigned Cost = (PFEntry >> 30);
5751
5752 if (Cost <= 4)
5753 return true;
5754 }
5755
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005756 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005757 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005758
Bob Wilson846bd792010-06-07 23:53:38 +00005759 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5760 return (EltSize >= 32 ||
5761 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005762 isVREVMask(M, VT, 64) ||
5763 isVREVMask(M, VT, 32) ||
5764 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005765 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005766 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005767 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005768 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005769}
5770
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005771/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5772/// the specified operations to build the shuffle.
5773static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5774 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005775 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005776 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5777 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5778 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5779
5780 enum {
5781 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5782 OP_VREV,
5783 OP_VDUP0,
5784 OP_VDUP1,
5785 OP_VDUP2,
5786 OP_VDUP3,
5787 OP_VEXT1,
5788 OP_VEXT2,
5789 OP_VEXT3,
5790 OP_VUZPL, // VUZP, left result
5791 OP_VUZPR, // VUZP, right result
5792 OP_VZIPL, // VZIP, left result
5793 OP_VZIPR, // VZIP, right result
5794 OP_VTRNL, // VTRN, left result
5795 OP_VTRNR // VTRN, right result
5796 };
5797
5798 if (OpNum == OP_COPY) {
5799 if (LHSID == (1*9+2)*9+3) return LHS;
5800 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5801 return RHS;
5802 }
5803
5804 SDValue OpLHS, OpRHS;
5805 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5806 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5807 EVT VT = OpLHS.getValueType();
5808
5809 switch (OpNum) {
5810 default: llvm_unreachable("Unknown shuffle opcode!");
5811 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005812 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005813 if (VT.getVectorElementType() == MVT::i32 ||
5814 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005815 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5816 // vrev <4 x i16> -> VREV32
5817 if (VT.getVectorElementType() == MVT::i16)
5818 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5819 // vrev <4 x i8> -> VREV16
5820 assert(VT.getVectorElementType() == MVT::i8);
5821 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005822 case OP_VDUP0:
5823 case OP_VDUP1:
5824 case OP_VDUP2:
5825 case OP_VDUP3:
5826 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005827 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005828 case OP_VEXT1:
5829 case OP_VEXT2:
5830 case OP_VEXT3:
5831 return DAG.getNode(ARMISD::VEXT, dl, VT,
5832 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005833 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005834 case OP_VUZPL:
5835 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005836 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005837 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5838 case OP_VZIPL:
5839 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005840 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005841 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5842 case OP_VTRNL:
5843 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005844 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5845 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005846 }
5847}
5848
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005849static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005850 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005851 SelectionDAG &DAG) {
5852 // Check to see if we can use the VTBL instruction.
5853 SDValue V1 = Op.getOperand(0);
5854 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005855 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005856
5857 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005858 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005859 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005860 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005861
5862 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5863 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005864 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005865
Owen Anderson77aa2662011-04-05 21:48:57 +00005866 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005867 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005868}
5869
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005870static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5871 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005872 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005873 SDValue OpLHS = Op.getOperand(0);
5874 EVT VT = OpLHS.getValueType();
5875
5876 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5877 "Expect an v8i16/v16i8 type");
5878 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5879 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5880 // extract the first 8 bytes into the top double word and the last 8 bytes
5881 // into the bottom double word. The v8i16 case is similar.
5882 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5883 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005884 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005885}
5886
Bob Wilson2e076c42009-06-22 23:27:02 +00005887static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005888 SDValue V1 = Op.getOperand(0);
5889 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005890 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005891 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005892 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005893
Bob Wilsonc6800b52009-08-13 02:13:04 +00005894 // Convert shuffles that are directly supported on NEON to target-specific
5895 // DAG nodes, instead of keeping them as shuffles and matching them again
5896 // during code selection. This is more efficient and avoids the possibility
5897 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005898 // FIXME: floating-point vectors should be canonicalized to integer vectors
5899 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005900 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005901
Bob Wilson846bd792010-06-07 23:53:38 +00005902 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5903 if (EltSize <= 32) {
5904 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5905 int Lane = SVN->getSplatIndex();
5906 // If this is undef splat, generate it via "just" vdup, if possible.
5907 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005908
Dan Gohman198b7ff2011-11-03 21:49:52 +00005909 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005910 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5911 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5912 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005913 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5914 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5915 // reaches it).
5916 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5917 !isa<ConstantSDNode>(V1.getOperand(0))) {
5918 bool IsScalarToVector = true;
5919 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5920 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5921 IsScalarToVector = false;
5922 break;
5923 }
5924 if (IsScalarToVector)
5925 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5926 }
Bob Wilson846bd792010-06-07 23:53:38 +00005927 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005928 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005929 }
Bob Wilson846bd792010-06-07 23:53:38 +00005930
5931 bool ReverseVEXT;
5932 unsigned Imm;
5933 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5934 if (ReverseVEXT)
5935 std::swap(V1, V2);
5936 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005937 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005938 }
5939
5940 if (isVREVMask(ShuffleMask, VT, 64))
5941 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5942 if (isVREVMask(ShuffleMask, VT, 32))
5943 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5944 if (isVREVMask(ShuffleMask, VT, 16))
5945 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5946
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005947 if (V2->getOpcode() == ISD::UNDEF &&
5948 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5949 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005950 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005951 }
5952
Bob Wilson846bd792010-06-07 23:53:38 +00005953 // Check for Neon shuffles that modify both input vectors in place.
5954 // If both results are used, i.e., if there are two shuffles with the same
5955 // source operands and with masks corresponding to both results of one of
5956 // these operations, DAG memoization will ensure that a single node is
5957 // used for both shuffles.
5958 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005959 bool isV_UNDEF;
5960 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5961 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5962 if (isV_UNDEF)
5963 V2 = V1;
5964 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5965 .getValue(WhichResult);
5966 }
Bob Wilson846bd792010-06-07 23:53:38 +00005967
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005968 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5969 // shuffles that produce a result larger than their operands with:
5970 // shuffle(concat(v1, undef), concat(v2, undef))
5971 // ->
5972 // shuffle(concat(v1, v2), undef)
5973 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5974 //
5975 // This is useful in the general case, but there are special cases where
5976 // native shuffles produce larger results: the two-result ops.
5977 //
5978 // Look through the concat when lowering them:
5979 // shuffle(concat(v1, v2), undef)
5980 // ->
5981 // concat(VZIP(v1, v2):0, :1)
5982 //
5983 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5984 V2->getOpcode() == ISD::UNDEF) {
5985 SDValue SubV1 = V1->getOperand(0);
5986 SDValue SubV2 = V1->getOperand(1);
5987 EVT SubVT = SubV1.getValueType();
5988
5989 // We expect these to have been canonicalized to -1.
5990 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5991 return i < (int)VT.getVectorNumElements();
5992 }) && "Unexpected shuffle index into UNDEF operand!");
5993
5994 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5995 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5996 if (isV_UNDEF)
5997 SubV2 = SubV1;
5998 assert((WhichResult == 0) &&
5999 "In-place shuffle of concat can only have one result!");
6000 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6001 SubV1, SubV2);
6002 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6003 Res.getValue(1));
6004 }
6005 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006006 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006007
Bob Wilsona7062312009-08-21 20:54:19 +00006008 // If the shuffle is not directly supported and it has 4 elements, use
6009 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006010 unsigned NumElts = VT.getVectorNumElements();
6011 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006012 unsigned PFIndexes[4];
6013 for (unsigned i = 0; i != 4; ++i) {
6014 if (ShuffleMask[i] < 0)
6015 PFIndexes[i] = 8;
6016 else
6017 PFIndexes[i] = ShuffleMask[i];
6018 }
6019
6020 // Compute the index in the perfect shuffle table.
6021 unsigned PFTableIndex =
6022 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006023 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6024 unsigned Cost = (PFEntry >> 30);
6025
6026 if (Cost <= 4)
6027 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6028 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006029
Bob Wilsond8a9a042010-06-04 00:04:02 +00006030 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006031 if (EltSize >= 32) {
6032 // Do the expansion with floating-point types, since that is what the VFP
6033 // registers are defined to use, and since i64 is not legal.
6034 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6035 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006036 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6037 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006038 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006039 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006040 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006041 Ops.push_back(DAG.getUNDEF(EltVT));
6042 else
6043 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6044 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6045 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006046 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006047 }
Craig Topper48d114b2014-04-26 18:35:24 +00006048 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006049 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006050 }
6051
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006052 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6053 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6054
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006055 if (VT == MVT::v8i8) {
6056 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
6057 if (NewOp.getNode())
6058 return NewOp;
6059 }
6060
Bob Wilson6f34e272009-08-14 05:16:33 +00006061 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006062}
6063
Eli Friedmana5e244c2011-10-24 23:08:52 +00006064static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6065 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6066 SDValue Lane = Op.getOperand(2);
6067 if (!isa<ConstantSDNode>(Lane))
6068 return SDValue();
6069
6070 return Op;
6071}
6072
Bob Wilson2e076c42009-06-22 23:27:02 +00006073static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006074 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006075 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006076 if (!isa<ConstantSDNode>(Lane))
6077 return SDValue();
6078
6079 SDValue Vec = Op.getOperand(0);
6080 if (Op.getValueType() == MVT::i32 &&
6081 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006082 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006083 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6084 }
6085
6086 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006087}
6088
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006089static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6090 // The only time a CONCAT_VECTORS operation can have legal types is when
6091 // two 64-bit vectors are concatenated to a 128-bit vector.
6092 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6093 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006094 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006095 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006096 SDValue Op0 = Op.getOperand(0);
6097 SDValue Op1 = Op.getOperand(1);
6098 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006099 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006100 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006101 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006102 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006103 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006104 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006105 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006106 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006107}
6108
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006109/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6110/// element has been zero/sign-extended, depending on the isSigned parameter,
6111/// from an integer type half its size.
6112static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6113 bool isSigned) {
6114 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6115 EVT VT = N->getValueType(0);
6116 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6117 SDNode *BVN = N->getOperand(0).getNode();
6118 if (BVN->getValueType(0) != MVT::v4i32 ||
6119 BVN->getOpcode() != ISD::BUILD_VECTOR)
6120 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006121 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006122 unsigned HiElt = 1 - LoElt;
6123 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6124 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6125 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6126 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6127 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6128 return false;
6129 if (isSigned) {
6130 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6131 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6132 return true;
6133 } else {
6134 if (Hi0->isNullValue() && Hi1->isNullValue())
6135 return true;
6136 }
6137 return false;
6138 }
6139
6140 if (N->getOpcode() != ISD::BUILD_VECTOR)
6141 return false;
6142
6143 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6144 SDNode *Elt = N->getOperand(i).getNode();
6145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6146 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6147 unsigned HalfSize = EltSize / 2;
6148 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006149 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006150 return false;
6151 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006152 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006153 return false;
6154 }
6155 continue;
6156 }
6157 return false;
6158 }
6159
6160 return true;
6161}
6162
6163/// isSignExtended - Check if a node is a vector value that is sign-extended
6164/// or a constant BUILD_VECTOR with sign-extended elements.
6165static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6166 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6167 return true;
6168 if (isExtendedBUILD_VECTOR(N, DAG, true))
6169 return true;
6170 return false;
6171}
6172
6173/// isZeroExtended - Check if a node is a vector value that is zero-extended
6174/// or a constant BUILD_VECTOR with zero-extended elements.
6175static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6176 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6177 return true;
6178 if (isExtendedBUILD_VECTOR(N, DAG, false))
6179 return true;
6180 return false;
6181}
6182
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006183static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6184 if (OrigVT.getSizeInBits() >= 64)
6185 return OrigVT;
6186
6187 assert(OrigVT.isSimple() && "Expecting a simple value type");
6188
6189 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6190 switch (OrigSimpleTy) {
6191 default: llvm_unreachable("Unexpected Vector Type");
6192 case MVT::v2i8:
6193 case MVT::v2i16:
6194 return MVT::v2i32;
6195 case MVT::v4i8:
6196 return MVT::v4i16;
6197 }
6198}
6199
Sebastian Popa204f722012-11-30 19:08:04 +00006200/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6201/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6202/// We insert the required extension here to get the vector to fill a D register.
6203static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6204 const EVT &OrigTy,
6205 const EVT &ExtTy,
6206 unsigned ExtOpcode) {
6207 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6208 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6209 // 64-bits we need to insert a new extension so that it will be 64-bits.
6210 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6211 if (OrigTy.getSizeInBits() >= 64)
6212 return N;
6213
6214 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006215 EVT NewVT = getExtensionTo64Bits(OrigTy);
6216
Andrew Trickef9de2a2013-05-25 02:42:55 +00006217 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006218}
6219
6220/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6221/// does not do any sign/zero extension. If the original vector is less
6222/// than 64 bits, an appropriate extension will be added after the load to
6223/// reach a total size of 64 bits. We have to add the extension separately
6224/// because ARM does not have a sign/zero extending load for vectors.
6225static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006226 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6227
6228 // The load already has the right type.
6229 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006230 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006231 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6232 LD->isNonTemporal(), LD->isInvariant(),
6233 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006234
6235 // We need to create a zextload/sextload. We cannot just create a load
6236 // followed by a zext/zext node because LowerMUL is also run during normal
6237 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006238 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006239 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006240 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006241 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006242}
6243
6244/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6245/// extending load, or BUILD_VECTOR with extended elements, return the
6246/// unextended value. The unextended vector should be 64 bits so that it can
6247/// be used as an operand to a VMULL instruction. If the original vector size
6248/// before extension is less than 64 bits we add a an extension to resize
6249/// the vector to 64 bits.
6250static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006251 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006252 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6253 N->getOperand(0)->getValueType(0),
6254 N->getValueType(0),
6255 N->getOpcode());
6256
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006257 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006258 return SkipLoadExtensionForVMULL(LD, DAG);
6259
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006260 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6261 // have been legalized as a BITCAST from v4i32.
6262 if (N->getOpcode() == ISD::BITCAST) {
6263 SDNode *BVN = N->getOperand(0).getNode();
6264 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6265 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006266 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006267 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006268 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6269 }
6270 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6271 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6272 EVT VT = N->getValueType(0);
6273 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6274 unsigned NumElts = VT.getVectorNumElements();
6275 MVT TruncVT = MVT::getIntegerVT(EltSize);
6276 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006277 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006278 for (unsigned i = 0; i != NumElts; ++i) {
6279 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6280 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006281 // Element types smaller than 32 bits are not legal, so use i32 elements.
6282 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006283 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006284 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006285 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006286 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006287}
6288
Evan Chenge2086e72011-03-29 01:56:09 +00006289static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6290 unsigned Opcode = N->getOpcode();
6291 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6292 SDNode *N0 = N->getOperand(0).getNode();
6293 SDNode *N1 = N->getOperand(1).getNode();
6294 return N0->hasOneUse() && N1->hasOneUse() &&
6295 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6296 }
6297 return false;
6298}
6299
6300static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6301 unsigned Opcode = N->getOpcode();
6302 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6303 SDNode *N0 = N->getOperand(0).getNode();
6304 SDNode *N1 = N->getOperand(1).getNode();
6305 return N0->hasOneUse() && N1->hasOneUse() &&
6306 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6307 }
6308 return false;
6309}
6310
Bob Wilson38ab35a2010-09-01 23:50:19 +00006311static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6312 // Multiplications are only custom-lowered for 128-bit vectors so that
6313 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6314 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006315 assert(VT.is128BitVector() && VT.isInteger() &&
6316 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006317 SDNode *N0 = Op.getOperand(0).getNode();
6318 SDNode *N1 = Op.getOperand(1).getNode();
6319 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006320 bool isMLA = false;
6321 bool isN0SExt = isSignExtended(N0, DAG);
6322 bool isN1SExt = isSignExtended(N1, DAG);
6323 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006324 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006325 else {
6326 bool isN0ZExt = isZeroExtended(N0, DAG);
6327 bool isN1ZExt = isZeroExtended(N1, DAG);
6328 if (isN0ZExt && isN1ZExt)
6329 NewOpc = ARMISD::VMULLu;
6330 else if (isN1SExt || isN1ZExt) {
6331 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6332 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6333 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6334 NewOpc = ARMISD::VMULLs;
6335 isMLA = true;
6336 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6337 NewOpc = ARMISD::VMULLu;
6338 isMLA = true;
6339 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6340 std::swap(N0, N1);
6341 NewOpc = ARMISD::VMULLu;
6342 isMLA = true;
6343 }
6344 }
6345
6346 if (!NewOpc) {
6347 if (VT == MVT::v2i64)
6348 // Fall through to expand this. It is not legal.
6349 return SDValue();
6350 else
6351 // Other vector multiplications are legal.
6352 return Op;
6353 }
6354 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006355
6356 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006357 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006358 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006359 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006360 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006361 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006362 assert(Op0.getValueType().is64BitVector() &&
6363 Op1.getValueType().is64BitVector() &&
6364 "unexpected types for extended operands to VMULL");
6365 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6366 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006367
Evan Chenge2086e72011-03-29 01:56:09 +00006368 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6369 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6370 // vmull q0, d4, d6
6371 // vmlal q0, d5, d6
6372 // is faster than
6373 // vaddl q0, d4, d5
6374 // vmovl q1, d6
6375 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006376 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6377 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006378 EVT Op1VT = Op1.getValueType();
6379 return DAG.getNode(N0->getOpcode(), DL, VT,
6380 DAG.getNode(NewOpc, DL, VT,
6381 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6382 DAG.getNode(NewOpc, DL, VT,
6383 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006384}
6385
Owen Anderson77aa2662011-04-05 21:48:57 +00006386static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006387LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006388 // TODO: Should this propagate fast-math-flags?
6389
Nate Begemanfa62d502011-02-11 20:53:29 +00006390 // Convert to float
6391 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6392 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6393 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6394 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6395 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6396 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6397 // Get reciprocal estimate.
6398 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006399 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006400 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6401 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006402 // Because char has a smaller range than uchar, we can actually get away
6403 // without any newton steps. This requires that we use a weird bias
6404 // of 0xb000, however (again, this has been exhaustively tested).
6405 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6406 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6407 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006408 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006409 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6410 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6411 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6412 // Convert back to short.
6413 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6414 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6415 return X;
6416}
6417
Owen Anderson77aa2662011-04-05 21:48:57 +00006418static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006419LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006420 // TODO: Should this propagate fast-math-flags?
6421
Nate Begemanfa62d502011-02-11 20:53:29 +00006422 SDValue N2;
6423 // Convert to float.
6424 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6425 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6426 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6427 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6428 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6429 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006430
Nate Begemanfa62d502011-02-11 20:53:29 +00006431 // Use reciprocal estimate and one refinement step.
6432 // float4 recip = vrecpeq_f32(yf);
6433 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006434 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006435 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6436 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006437 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006438 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006439 N1, N2);
6440 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6441 // Because short has a smaller range than ushort, we can actually get away
6442 // with only a single newton step. This requires that we use a weird bias
6443 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006444 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006445 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6446 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006447 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006448 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6449 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6450 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6451 // Convert back to integer and return.
6452 // return vmovn_s32(vcvt_s32_f32(result));
6453 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6454 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6455 return N0;
6456}
6457
6458static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6459 EVT VT = Op.getValueType();
6460 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6461 "unexpected type for custom-lowering ISD::SDIV");
6462
Andrew Trickef9de2a2013-05-25 02:42:55 +00006463 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006464 SDValue N0 = Op.getOperand(0);
6465 SDValue N1 = Op.getOperand(1);
6466 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006467
Nate Begemanfa62d502011-02-11 20:53:29 +00006468 if (VT == MVT::v8i8) {
6469 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6470 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006471
Nate Begemanfa62d502011-02-11 20:53:29 +00006472 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006473 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006474 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006475 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006476 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006477 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006478 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006479 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006480
6481 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6482 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6483
6484 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6485 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006486
Nate Begemanfa62d502011-02-11 20:53:29 +00006487 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6488 return N0;
6489 }
6490 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6491}
6492
6493static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006494 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006495 EVT VT = Op.getValueType();
6496 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6497 "unexpected type for custom-lowering ISD::UDIV");
6498
Andrew Trickef9de2a2013-05-25 02:42:55 +00006499 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006500 SDValue N0 = Op.getOperand(0);
6501 SDValue N1 = Op.getOperand(1);
6502 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006503
Nate Begemanfa62d502011-02-11 20:53:29 +00006504 if (VT == MVT::v8i8) {
6505 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6506 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006507
Nate Begemanfa62d502011-02-11 20:53:29 +00006508 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006509 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006510 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006511 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006512 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006513 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006514 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006515 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006516
Nate Begemanfa62d502011-02-11 20:53:29 +00006517 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6518 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006519
Nate Begemanfa62d502011-02-11 20:53:29 +00006520 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6521 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006522
6523 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006524 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6525 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006526 N0);
6527 return N0;
6528 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006529
Nate Begemanfa62d502011-02-11 20:53:29 +00006530 // v4i16 sdiv ... Convert to float.
6531 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6532 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6533 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6534 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6535 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006536 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006537
6538 // Use reciprocal estimate and two refinement steps.
6539 // float4 recip = vrecpeq_f32(yf);
6540 // recip *= vrecpsq_f32(yf, recip);
6541 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006542 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006543 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6544 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006545 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006546 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006547 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006548 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006549 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006550 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006551 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006552 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6553 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6554 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6555 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006556 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006557 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6558 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006559 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006560 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6561 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6562 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6563 // Convert back to integer and return.
6564 // return vmovn_u32(vcvt_s32_f32(result));
6565 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6566 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6567 return N0;
6568}
6569
Evan Chenge8916542011-08-30 01:34:54 +00006570static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6571 EVT VT = Op.getNode()->getValueType(0);
6572 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6573
6574 unsigned Opc;
6575 bool ExtraOp = false;
6576 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006577 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006578 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6579 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6580 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6581 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6582 }
6583
6584 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006585 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006586 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006587 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006588 Op.getOperand(1), Op.getOperand(2));
6589}
6590
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006591SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6592 assert(Subtarget->isTargetDarwin());
6593
6594 // For iOS, we want to call an alternative entry point: __sincos_stret,
6595 // return values are passed via sret.
6596 SDLoc dl(Op);
6597 SDValue Arg = Op.getOperand(0);
6598 EVT ArgVT = Arg.getValueType();
6599 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006600 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006601
6602 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006603
6604 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006605 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006606
6607 // Create stack object for sret.
Mehdi Amini44ede332015-07-09 02:09:04 +00006608 auto &DL = DAG.getDataLayout();
6609 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6610 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006611 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00006612 SDValue SRet = DAG.getFrameIndex(FrameIdx, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006613
6614 ArgListTy Args;
6615 ArgListEntry Entry;
6616
6617 Entry.Node = SRet;
6618 Entry.Ty = RetTy->getPointerTo();
6619 Entry.isSExt = false;
6620 Entry.isZExt = false;
6621 Entry.isSRet = true;
6622 Args.push_back(Entry);
6623
6624 Entry.Node = Arg;
6625 Entry.Ty = ArgTy;
6626 Entry.isSExt = false;
6627 Entry.isZExt = false;
6628 Args.push_back(Entry);
6629
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006630 const char *LibcallName =
6631 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
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);
6635 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6636 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006637 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006638 .setDiscardResult();
6639
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006640 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6641
6642 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6643 MachinePointerInfo(), false, false, false, 0);
6644
6645 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006646 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006647 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006648 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6649 MachinePointerInfo(), false, false, false, 0);
6650
6651 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6652 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6653 LoadSin.getValue(0), LoadCos.getValue(0));
6654}
6655
Eli Friedman10f9ce22011-09-15 22:26:18 +00006656static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006657 // Monotonic load/store is legal for all targets
6658 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6659 return Op;
6660
Alp Tokercb402912014-01-24 17:20:08 +00006661 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006662 // dmb or equivalent available.
6663 return SDValue();
6664}
6665
Tim Northoverbc933082013-05-23 19:11:20 +00006666static void ReplaceREADCYCLECOUNTER(SDNode *N,
6667 SmallVectorImpl<SDValue> &Results,
6668 SelectionDAG &DAG,
6669 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006670 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006671 // Under Power Management extensions, the cycle-count is:
6672 // mrc p15, #0, <Rt>, c9, c13, #0
6673 SDValue Ops[] = { N->getOperand(0), // Chain
6674 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6675 DAG.getConstant(15, DL, MVT::i32),
6676 DAG.getConstant(0, DL, MVT::i32),
6677 DAG.getConstant(9, DL, MVT::i32),
6678 DAG.getConstant(13, DL, MVT::i32),
6679 DAG.getConstant(0, DL, MVT::i32)
6680 };
Tim Northoverbc933082013-05-23 19:11:20 +00006681
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006682 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6683 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6684 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6685 DAG.getConstant(0, DL, MVT::i32)));
6686 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006687}
6688
Dan Gohman21cea8a2010-04-17 15:26:15 +00006689SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006690 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006691 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006692 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006693 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006694 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006695 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006696 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6697 default: llvm_unreachable("unknown object format");
6698 case Triple::COFF:
6699 return LowerGlobalAddressWindows(Op, DAG);
6700 case Triple::ELF:
6701 return LowerGlobalAddressELF(Op, DAG);
6702 case Triple::MachO:
6703 return LowerGlobalAddressDarwin(Op, DAG);
6704 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006705 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006706 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006707 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6708 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006709 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006710 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006711 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006712 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006713 case ISD::SINT_TO_FP:
6714 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6715 case ISD::FP_TO_SINT:
6716 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006717 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006718 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006719 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006720 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006721 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006722 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006723 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006724 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6725 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006726 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006727 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006728 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006729 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00006730 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
6731 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006732 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006733 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006734 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006735 case ISD::CTTZ:
6736 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006737 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006738 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006739 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006740 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006741 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006742 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006743 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006744 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006745 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006746 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006747 case ISD::SDIV: return LowerSDIV(Op, DAG);
6748 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006749 case ISD::ADDC:
6750 case ISD::ADDE:
6751 case ISD::SUBC:
6752 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006753 case ISD::SADDO:
6754 case ISD::UADDO:
6755 case ISD::SSUBO:
6756 case ISD::USUBO:
6757 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006758 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006759 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006760 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006761 case ISD::SDIVREM:
6762 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006763 case ISD::DYNAMIC_STACKALLOC:
6764 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6765 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6766 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006767 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6768 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006769 }
Evan Cheng10043e22007-01-19 07:51:42 +00006770}
6771
Duncan Sands6ed40142008-12-01 11:39:25 +00006772/// ReplaceNodeResults - Replace the results of node with an illegal result
6773/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006774void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6775 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006776 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006777 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006778 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006779 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006780 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006781 case ISD::READ_REGISTER:
6782 ExpandREAD_REGISTER(N, Results, DAG);
6783 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006784 case ISD::BITCAST:
6785 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006786 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006787 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006788 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006789 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006790 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00006791 case ISD::SREM:
6792 case ISD::UREM:
6793 Res = LowerREM(N, DAG);
6794 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006795 case ISD::READCYCLECOUNTER:
6796 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6797 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006798 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006799 if (Res.getNode())
6800 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006801}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006802
Evan Cheng10043e22007-01-19 07:51:42 +00006803//===----------------------------------------------------------------------===//
6804// ARM Scheduler Hooks
6805//===----------------------------------------------------------------------===//
6806
Bill Wendling030b58e2011-10-06 22:18:16 +00006807/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6808/// registers the function context.
6809void ARMTargetLowering::
6810SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6811 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006812 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006813 DebugLoc dl = MI->getDebugLoc();
6814 MachineFunction *MF = MBB->getParent();
6815 MachineRegisterInfo *MRI = &MF->getRegInfo();
6816 MachineConstantPool *MCP = MF->getConstantPool();
6817 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6818 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006819
Bill Wendling374ee192011-10-03 21:25:38 +00006820 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006821 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006822
Bill Wendling374ee192011-10-03 21:25:38 +00006823 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006824 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006825 ARMConstantPoolValue *CPV =
6826 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6827 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6828
Craig Topper61e88f42014-11-21 05:58:21 +00006829 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6830 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006831
Bill Wendling030b58e2011-10-06 22:18:16 +00006832 // Grab constant pool and fixed stack memory operands.
6833 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006834 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
6835 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006836
6837 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006838 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
6839 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006840
6841 // Load the address of the dispatch MBB into the jump buffer.
6842 if (isThumb2) {
6843 // Incoming value: jbuf
6844 // ldr.n r5, LCPI1_1
6845 // orr r5, r5, #1
6846 // add r5, pc
6847 // str r5, [$jbuf, #+4] ; &jbuf[1]
6848 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6849 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6850 .addConstantPoolIndex(CPI)
6851 .addMemOperand(CPMMO));
6852 // Set the low bit because of thumb mode.
6853 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6854 AddDefaultCC(
6855 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6856 .addReg(NewVReg1, RegState::Kill)
6857 .addImm(0x01)));
6858 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6859 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6860 .addReg(NewVReg2, RegState::Kill)
6861 .addImm(PCLabelId);
6862 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6863 .addReg(NewVReg3, RegState::Kill)
6864 .addFrameIndex(FI)
6865 .addImm(36) // &jbuf[1] :: pc
6866 .addMemOperand(FIMMOSt));
6867 } else if (isThumb) {
6868 // Incoming value: jbuf
6869 // ldr.n r1, LCPI1_4
6870 // add r1, pc
6871 // mov r2, #1
6872 // orrs r1, r2
6873 // add r2, $jbuf, #+4 ; &jbuf[1]
6874 // str r1, [r2]
6875 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6876 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6877 .addConstantPoolIndex(CPI)
6878 .addMemOperand(CPMMO));
6879 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6880 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6881 .addReg(NewVReg1, RegState::Kill)
6882 .addImm(PCLabelId);
6883 // Set the low bit because of thumb mode.
6884 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6885 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6886 .addReg(ARM::CPSR, RegState::Define)
6887 .addImm(1));
6888 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6889 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6890 .addReg(ARM::CPSR, RegState::Define)
6891 .addReg(NewVReg2, RegState::Kill)
6892 .addReg(NewVReg3, RegState::Kill));
6893 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006894 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6895 .addFrameIndex(FI)
6896 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006897 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6898 .addReg(NewVReg4, RegState::Kill)
6899 .addReg(NewVReg5, RegState::Kill)
6900 .addImm(0)
6901 .addMemOperand(FIMMOSt));
6902 } else {
6903 // Incoming value: jbuf
6904 // ldr r1, LCPI1_1
6905 // add r1, pc, r1
6906 // str r1, [$jbuf, #+4] ; &jbuf[1]
6907 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6908 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6909 .addConstantPoolIndex(CPI)
6910 .addImm(0)
6911 .addMemOperand(CPMMO));
6912 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6913 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6914 .addReg(NewVReg1, RegState::Kill)
6915 .addImm(PCLabelId));
6916 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6917 .addReg(NewVReg2, RegState::Kill)
6918 .addFrameIndex(FI)
6919 .addImm(36) // &jbuf[1] :: pc
6920 .addMemOperand(FIMMOSt));
6921 }
6922}
6923
Matthias Brauneec4efc2015-04-28 00:37:05 +00006924void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6925 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006926 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006927 DebugLoc dl = MI->getDebugLoc();
6928 MachineFunction *MF = MBB->getParent();
6929 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006930 MachineFrameInfo *MFI = MF->getFrameInfo();
6931 int FI = MFI->getFunctionContextIndex();
6932
Craig Topper61e88f42014-11-21 05:58:21 +00006933 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6934 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006935
Bill Wendling362c1b02011-10-06 21:29:56 +00006936 // Get a mapping of the call site numbers to all of the landing pads they're
6937 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006938 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6939 unsigned MaxCSNum = 0;
6940 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006941 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6942 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00006943 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006944
6945 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6946 // pad.
6947 for (MachineBasicBlock::iterator
6948 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6949 if (!II->isEHLabel()) continue;
6950
6951 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006952 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006953
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006954 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6955 for (SmallVectorImpl<unsigned>::iterator
6956 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6957 CSI != CSE; ++CSI) {
6958 CallSiteNumToLPad[*CSI].push_back(BB);
6959 MaxCSNum = std::max(MaxCSNum, *CSI);
6960 }
Bill Wendling202803e2011-10-05 00:02:33 +00006961 break;
6962 }
6963 }
6964
6965 // Get an ordered list of the machine basic blocks for the jump table.
6966 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006967 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006968 LPadList.reserve(CallSiteNumToLPad.size());
6969 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6970 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6971 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006972 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006973 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006974 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6975 }
Bill Wendling202803e2011-10-05 00:02:33 +00006976 }
6977
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006978 assert(!LPadList.empty() &&
6979 "No landing pad destinations for the dispatch jump table!");
6980
Bill Wendling362c1b02011-10-06 21:29:56 +00006981 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006982 MachineJumpTableInfo *JTI =
6983 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6984 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00006985 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006986
Bill Wendling362c1b02011-10-06 21:29:56 +00006987 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006988
6989 // Shove the dispatch's address into the return slot in the function context.
6990 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00006991 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006992
Bill Wendling324be982011-10-05 00:39:32 +00006993 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006994 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006995 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006996 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006997 else
6998 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6999
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007000 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007001 DispatchBB->addSuccessor(TrapBB);
7002
7003 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7004 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007005
Bill Wendling510fbcd2011-10-17 21:32:56 +00007006 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007007 MF->insert(MF->end(), DispatchBB);
7008 MF->insert(MF->end(), DispContBB);
7009 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007010
Bill Wendling030b58e2011-10-06 22:18:16 +00007011 // Insert code into the entry block that creates and registers the function
7012 // context.
7013 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7014
Alex Lorenze40c8a22015-08-11 23:09:45 +00007015 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7016 MachinePointerInfo::getFixedStack(*MF, FI),
7017 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007018
Chad Rosier1ec8e402012-11-06 23:05:24 +00007019 MachineInstrBuilder MIB;
7020 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7021
7022 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7023 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7024
7025 // Add a register mask with no preserved registers. This results in all
7026 // registers being marked as clobbered.
7027 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007028
Bill Wendling85833f72011-10-18 22:49:07 +00007029 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007030 if (Subtarget->isThumb2()) {
7031 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7032 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7033 .addFrameIndex(FI)
7034 .addImm(4)
7035 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007036
Bill Wendling85833f72011-10-18 22:49:07 +00007037 if (NumLPads < 256) {
7038 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7039 .addReg(NewVReg1)
7040 .addImm(LPadList.size()));
7041 } else {
7042 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7043 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007044 .addImm(NumLPads & 0xFFFF));
7045
7046 unsigned VReg2 = VReg1;
7047 if ((NumLPads & 0xFFFF0000) != 0) {
7048 VReg2 = MRI->createVirtualRegister(TRC);
7049 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7050 .addReg(VReg1)
7051 .addImm(NumLPads >> 16));
7052 }
7053
Bill Wendling85833f72011-10-18 22:49:07 +00007054 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7055 .addReg(NewVReg1)
7056 .addReg(VReg2));
7057 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007058
Bill Wendling5626c662011-10-06 22:53:00 +00007059 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7060 .addMBB(TrapBB)
7061 .addImm(ARMCC::HI)
7062 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007063
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007064 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7065 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007066 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007067
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007068 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007069 AddDefaultCC(
7070 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007071 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7072 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007073 .addReg(NewVReg1)
7074 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7075
7076 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007077 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007078 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007079 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007080 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007081 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7082 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7083 .addFrameIndex(FI)
7084 .addImm(1)
7085 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007086
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007087 if (NumLPads < 256) {
7088 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7089 .addReg(NewVReg1)
7090 .addImm(NumLPads));
7091 } else {
7092 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007093 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7094 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7095
7096 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007097 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007098 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007099 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007100 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007101
7102 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7103 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7104 .addReg(VReg1, RegState::Define)
7105 .addConstantPoolIndex(Idx));
7106 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7107 .addReg(NewVReg1)
7108 .addReg(VReg1));
7109 }
7110
Bill Wendlingb3d46782011-10-06 23:37:36 +00007111 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7112 .addMBB(TrapBB)
7113 .addImm(ARMCC::HI)
7114 .addReg(ARM::CPSR);
7115
7116 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7117 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7118 .addReg(ARM::CPSR, RegState::Define)
7119 .addReg(NewVReg1)
7120 .addImm(2));
7121
7122 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007123 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007124 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007125
7126 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7127 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7128 .addReg(ARM::CPSR, RegState::Define)
7129 .addReg(NewVReg2, RegState::Kill)
7130 .addReg(NewVReg3));
7131
Alex Lorenze40c8a22015-08-11 23:09:45 +00007132 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7133 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007134
7135 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7136 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7137 .addReg(NewVReg4, RegState::Kill)
7138 .addImm(0)
7139 .addMemOperand(JTMMOLd));
7140
Chad Rosier96603432013-03-01 18:30:38 +00007141 unsigned NewVReg6 = NewVReg5;
7142 if (RelocM == Reloc::PIC_) {
7143 NewVReg6 = MRI->createVirtualRegister(TRC);
7144 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7145 .addReg(ARM::CPSR, RegState::Define)
7146 .addReg(NewVReg5, RegState::Kill)
7147 .addReg(NewVReg3));
7148 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007149
7150 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7151 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007152 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007153 } else {
7154 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7155 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7156 .addFrameIndex(FI)
7157 .addImm(4)
7158 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007159
Bill Wendling4969dcd2011-10-18 22:52:20 +00007160 if (NumLPads < 256) {
7161 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7162 .addReg(NewVReg1)
7163 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007164 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007165 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7166 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007167 .addImm(NumLPads & 0xFFFF));
7168
7169 unsigned VReg2 = VReg1;
7170 if ((NumLPads & 0xFFFF0000) != 0) {
7171 VReg2 = MRI->createVirtualRegister(TRC);
7172 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7173 .addReg(VReg1)
7174 .addImm(NumLPads >> 16));
7175 }
7176
Bill Wendling4969dcd2011-10-18 22:52:20 +00007177 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7178 .addReg(NewVReg1)
7179 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007180 } else {
7181 MachineConstantPool *ConstantPool = MF->getConstantPool();
7182 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7183 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7184
7185 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007186 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007187 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007188 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007189 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7190
7191 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7192 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7193 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007194 .addConstantPoolIndex(Idx)
7195 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007196 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7197 .addReg(NewVReg1)
7198 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007199 }
7200
Bill Wendling5626c662011-10-06 22:53:00 +00007201 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7202 .addMBB(TrapBB)
7203 .addImm(ARMCC::HI)
7204 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007205
Bill Wendling973c8172011-10-18 22:11:18 +00007206 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007207 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007208 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007209 .addReg(NewVReg1)
7210 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007211 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7212 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007213 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007214
Alex Lorenze40c8a22015-08-11 23:09:45 +00007215 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7216 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007217 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007218 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007219 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7220 .addReg(NewVReg3, RegState::Kill)
7221 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007222 .addImm(0)
7223 .addMemOperand(JTMMOLd));
7224
Chad Rosier96603432013-03-01 18:30:38 +00007225 if (RelocM == Reloc::PIC_) {
7226 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7227 .addReg(NewVReg5, RegState::Kill)
7228 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007229 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007230 } else {
7231 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7232 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007233 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007234 }
Bill Wendling5626c662011-10-06 22:53:00 +00007235 }
Bill Wendling202803e2011-10-05 00:02:33 +00007236
Bill Wendling324be982011-10-05 00:39:32 +00007237 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007238 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007239 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007240 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7241 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007242 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007243 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007244 }
7245
Bill Wendling26d27802011-10-17 05:25:09 +00007246 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007247 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007248 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007249 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007250
7251 // Remove the landing pad successor from the invoke block and replace it
7252 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007253 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7254 BB->succ_end());
7255 while (!Successors.empty()) {
7256 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007257 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007258 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007259 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007260 }
7261 }
7262
7263 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007264
7265 // Find the invoke call and mark all of the callee-saved registers as
7266 // 'implicit defined' so that they're spilled. This prevents code from
7267 // moving instructions to before the EH block, where they will never be
7268 // executed.
7269 for (MachineBasicBlock::reverse_iterator
7270 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007271 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007272
7273 DenseMap<unsigned, bool> DefRegs;
7274 for (MachineInstr::mop_iterator
7275 OI = II->operands_begin(), OE = II->operands_end();
7276 OI != OE; ++OI) {
7277 if (!OI->isReg()) continue;
7278 DefRegs[OI->getReg()] = true;
7279 }
7280
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007281 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007282
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007283 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007284 unsigned Reg = SavedRegs[i];
7285 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007286 !ARM::tGPRRegClass.contains(Reg) &&
7287 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007288 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007289 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007290 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007291 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007292 continue;
7293 if (!DefRegs[Reg])
7294 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007295 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007296
7297 break;
7298 }
Bill Wendling883ec972011-10-07 23:18:02 +00007299 }
Bill Wendling324be982011-10-05 00:39:32 +00007300
Bill Wendling617075f2011-10-18 18:30:49 +00007301 // Mark all former landing pads as non-landing pads. The dispatch is the only
7302 // landing pad now.
7303 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7304 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007305 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007306
Bill Wendling324be982011-10-05 00:39:32 +00007307 // The instruction is gone now.
7308 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007309}
7310
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007311static
7312MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7313 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7314 E = MBB->succ_end(); I != E; ++I)
7315 if (*I != Succ)
7316 return *I;
7317 llvm_unreachable("Expecting a BB with two successors!");
7318}
7319
Manman Renb504f492013-10-29 22:27:32 +00007320/// Return the load opcode for a given load size. If load size >= 8,
7321/// neon opcode will be returned.
7322static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7323 if (LdSize >= 8)
7324 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7325 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7326 if (IsThumb1)
7327 return LdSize == 4 ? ARM::tLDRi
7328 : LdSize == 2 ? ARM::tLDRHi
7329 : LdSize == 1 ? ARM::tLDRBi : 0;
7330 if (IsThumb2)
7331 return LdSize == 4 ? ARM::t2LDR_POST
7332 : LdSize == 2 ? ARM::t2LDRH_POST
7333 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7334 return LdSize == 4 ? ARM::LDR_POST_IMM
7335 : LdSize == 2 ? ARM::LDRH_POST
7336 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7337}
7338
7339/// Return the store opcode for a given store size. If store size >= 8,
7340/// neon opcode will be returned.
7341static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7342 if (StSize >= 8)
7343 return StSize == 16 ? ARM::VST1q32wb_fixed
7344 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7345 if (IsThumb1)
7346 return StSize == 4 ? ARM::tSTRi
7347 : StSize == 2 ? ARM::tSTRHi
7348 : StSize == 1 ? ARM::tSTRBi : 0;
7349 if (IsThumb2)
7350 return StSize == 4 ? ARM::t2STR_POST
7351 : StSize == 2 ? ARM::t2STRH_POST
7352 : StSize == 1 ? ARM::t2STRB_POST : 0;
7353 return StSize == 4 ? ARM::STR_POST_IMM
7354 : StSize == 2 ? ARM::STRH_POST
7355 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7356}
7357
7358/// Emit a post-increment load operation with given size. The instructions
7359/// will be added to BB at Pos.
7360static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7361 const TargetInstrInfo *TII, DebugLoc dl,
7362 unsigned LdSize, unsigned Data, unsigned AddrIn,
7363 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7364 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7365 assert(LdOpc != 0 && "Should have a load opcode");
7366 if (LdSize >= 8) {
7367 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7368 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7369 .addImm(0));
7370 } else if (IsThumb1) {
7371 // load + update AddrIn
7372 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7373 .addReg(AddrIn).addImm(0));
7374 MachineInstrBuilder MIB =
7375 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7376 MIB = AddDefaultT1CC(MIB);
7377 MIB.addReg(AddrIn).addImm(LdSize);
7378 AddDefaultPred(MIB);
7379 } else if (IsThumb2) {
7380 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7381 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7382 .addImm(LdSize));
7383 } else { // arm
7384 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7385 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7386 .addReg(0).addImm(LdSize));
7387 }
7388}
7389
7390/// Emit a post-increment store operation with given size. The instructions
7391/// will be added to BB at Pos.
7392static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7393 const TargetInstrInfo *TII, DebugLoc dl,
7394 unsigned StSize, unsigned Data, unsigned AddrIn,
7395 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7396 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7397 assert(StOpc != 0 && "Should have a store opcode");
7398 if (StSize >= 8) {
7399 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7400 .addReg(AddrIn).addImm(0).addReg(Data));
7401 } else if (IsThumb1) {
7402 // store + update AddrIn
7403 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7404 .addReg(AddrIn).addImm(0));
7405 MachineInstrBuilder MIB =
7406 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7407 MIB = AddDefaultT1CC(MIB);
7408 MIB.addReg(AddrIn).addImm(StSize);
7409 AddDefaultPred(MIB);
7410 } else if (IsThumb2) {
7411 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7412 .addReg(Data).addReg(AddrIn).addImm(StSize));
7413 } else { // arm
7414 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7415 .addReg(Data).addReg(AddrIn).addReg(0)
7416 .addImm(StSize));
7417 }
7418}
7419
David Peixottoc32e24a2013-10-17 19:49:22 +00007420MachineBasicBlock *
7421ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7422 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007423 // This pseudo instruction has 3 operands: dst, src, size
7424 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7425 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007426 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007427 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7428 MachineFunction::iterator It = BB;
7429 ++It;
7430
7431 unsigned dest = MI->getOperand(0).getReg();
7432 unsigned src = MI->getOperand(1).getReg();
7433 unsigned SizeVal = MI->getOperand(2).getImm();
7434 unsigned Align = MI->getOperand(3).getImm();
7435 DebugLoc dl = MI->getDebugLoc();
7436
Manman Rene8735522012-06-01 19:33:18 +00007437 MachineFunction *MF = BB->getParent();
7438 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007439 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007440 const TargetRegisterClass *TRC = nullptr;
7441 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007442
7443 bool IsThumb1 = Subtarget->isThumb1Only();
7444 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007445
7446 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007447 UnitSize = 1;
7448 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007449 UnitSize = 2;
7450 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007451 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007452 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007453 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007454 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007455 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007456 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007457 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007458 }
7459 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007460 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007461 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007462 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007463
David Peixottob0653e532013-10-24 16:39:36 +00007464 // Select the correct opcode and register class for unit size load/store
7465 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007466 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007467 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007468 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7469 : UnitSize == 8 ? &ARM::DPRRegClass
7470 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007471
Manman Rene8735522012-06-01 19:33:18 +00007472 unsigned BytesLeft = SizeVal % UnitSize;
7473 unsigned LoopSize = SizeVal - BytesLeft;
7474
7475 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7476 // Use LDR and STR to copy.
7477 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7478 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7479 unsigned srcIn = src;
7480 unsigned destIn = dest;
7481 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007482 unsigned srcOut = MRI.createVirtualRegister(TRC);
7483 unsigned destOut = MRI.createVirtualRegister(TRC);
7484 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007485 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7486 IsThumb1, IsThumb2);
7487 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7488 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007489 srcIn = srcOut;
7490 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007491 }
7492
7493 // Handle the leftover bytes with LDRB and STRB.
7494 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7495 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007496 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007497 unsigned srcOut = MRI.createVirtualRegister(TRC);
7498 unsigned destOut = MRI.createVirtualRegister(TRC);
7499 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007500 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7501 IsThumb1, IsThumb2);
7502 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7503 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007504 srcIn = srcOut;
7505 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007506 }
7507 MI->eraseFromParent(); // The instruction is gone now.
7508 return BB;
7509 }
7510
7511 // Expand the pseudo op to a loop.
7512 // thisMBB:
7513 // ...
7514 // movw varEnd, # --> with thumb2
7515 // movt varEnd, #
7516 // ldrcp varEnd, idx --> without thumb2
7517 // fallthrough --> loopMBB
7518 // loopMBB:
7519 // PHI varPhi, varEnd, varLoop
7520 // PHI srcPhi, src, srcLoop
7521 // PHI destPhi, dst, destLoop
7522 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7523 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7524 // subs varLoop, varPhi, #UnitSize
7525 // bne loopMBB
7526 // fallthrough --> exitMBB
7527 // exitMBB:
7528 // epilogue to handle left-over bytes
7529 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7530 // [destOut] = STRB_POST(scratch, destLoop, 1)
7531 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7532 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7533 MF->insert(It, loopMBB);
7534 MF->insert(It, exitMBB);
7535
7536 // Transfer the remainder of BB and its successor edges to exitMBB.
7537 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007538 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007539 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7540
7541 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007542 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007543 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007544 unsigned Vtmp = varEnd;
7545 if ((LoopSize & 0xFFFF0000) != 0)
7546 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007547 AddDefaultPred(BuildMI(BB, dl,
7548 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7549 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007550
7551 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007552 AddDefaultPred(BuildMI(BB, dl,
7553 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7554 varEnd)
7555 .addReg(Vtmp)
7556 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007557 } else {
7558 MachineConstantPool *ConstantPool = MF->getConstantPool();
7559 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7560 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7561
7562 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007563 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007564 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007565 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007566 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7567
7568 if (IsThumb1)
7569 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7570 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7571 else
7572 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7573 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7574 }
Manman Rene8735522012-06-01 19:33:18 +00007575 BB->addSuccessor(loopMBB);
7576
7577 // Generate the loop body:
7578 // varPhi = PHI(varLoop, varEnd)
7579 // srcPhi = PHI(srcLoop, src)
7580 // destPhi = PHI(destLoop, dst)
7581 MachineBasicBlock *entryBB = BB;
7582 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007583 unsigned varLoop = MRI.createVirtualRegister(TRC);
7584 unsigned varPhi = MRI.createVirtualRegister(TRC);
7585 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7586 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7587 unsigned destLoop = MRI.createVirtualRegister(TRC);
7588 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007589
7590 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7591 .addReg(varLoop).addMBB(loopMBB)
7592 .addReg(varEnd).addMBB(entryBB);
7593 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7594 .addReg(srcLoop).addMBB(loopMBB)
7595 .addReg(src).addMBB(entryBB);
7596 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7597 .addReg(destLoop).addMBB(loopMBB)
7598 .addReg(dest).addMBB(entryBB);
7599
7600 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7601 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007602 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007603 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7604 IsThumb1, IsThumb2);
7605 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7606 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007607
7608 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007609 if (IsThumb1) {
7610 MachineInstrBuilder MIB =
7611 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7612 MIB = AddDefaultT1CC(MIB);
7613 MIB.addReg(varPhi).addImm(UnitSize);
7614 AddDefaultPred(MIB);
7615 } else {
7616 MachineInstrBuilder MIB =
7617 BuildMI(*BB, BB->end(), dl,
7618 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7619 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7620 MIB->getOperand(5).setReg(ARM::CPSR);
7621 MIB->getOperand(5).setIsDef(true);
7622 }
7623 BuildMI(*BB, BB->end(), dl,
7624 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7625 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007626
7627 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7628 BB->addSuccessor(loopMBB);
7629 BB->addSuccessor(exitMBB);
7630
7631 // Add epilogue to handle BytesLeft.
7632 BB = exitMBB;
7633 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007634
7635 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7636 // [destOut] = STRB_POST(scratch, destLoop, 1)
7637 unsigned srcIn = srcLoop;
7638 unsigned destIn = destLoop;
7639 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007640 unsigned srcOut = MRI.createVirtualRegister(TRC);
7641 unsigned destOut = MRI.createVirtualRegister(TRC);
7642 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007643 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7644 IsThumb1, IsThumb2);
7645 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7646 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007647 srcIn = srcOut;
7648 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007649 }
7650
7651 MI->eraseFromParent(); // The instruction is gone now.
7652 return BB;
7653}
7654
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007655MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007656ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7657 MachineBasicBlock *MBB) const {
7658 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007659 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007660 DebugLoc DL = MI->getDebugLoc();
7661
7662 assert(Subtarget->isTargetWindows() &&
7663 "__chkstk is only supported on Windows");
7664 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7665
7666 // __chkstk takes the number of words to allocate on the stack in R4, and
7667 // returns the stack adjustment in number of bytes in R4. This will not
7668 // clober any other registers (other than the obvious lr).
7669 //
7670 // Although, technically, IP should be considered a register which may be
7671 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7672 // thumb-2 environment, so there is no interworking required. As a result, we
7673 // do not expect a veneer to be emitted by the linker, clobbering IP.
7674 //
Alp Toker1d099d92014-06-19 19:41:26 +00007675 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007676 // required, again, ensuring that IP is not clobbered.
7677 //
7678 // Finally, although some linkers may theoretically provide a trampoline for
7679 // out of range calls (which is quite common due to a 32M range limitation of
7680 // branches for Thumb), we can generate the long-call version via
7681 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7682 // IP.
7683
7684 switch (TM.getCodeModel()) {
7685 case CodeModel::Small:
7686 case CodeModel::Medium:
7687 case CodeModel::Default:
7688 case CodeModel::Kernel:
7689 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7690 .addImm((unsigned)ARMCC::AL).addReg(0)
7691 .addExternalSymbol("__chkstk")
7692 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7693 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7694 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7695 break;
7696 case CodeModel::Large:
7697 case CodeModel::JITDefault: {
7698 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7699 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7700
7701 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7702 .addExternalSymbol("__chkstk");
7703 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7704 .addImm((unsigned)ARMCC::AL).addReg(0)
7705 .addReg(Reg, RegState::Kill)
7706 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7707 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7708 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7709 break;
7710 }
7711 }
7712
7713 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7714 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007715 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007716
7717 MI->eraseFromParent();
7718 return MBB;
7719}
7720
7721MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007722ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007723 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007724 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007725 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007726 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007727 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007728 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007729 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007730 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007731 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007732 // The Thumb2 pre-indexed stores have the same MI operands, they just
7733 // define them differently in the .td files from the isel patterns, so
7734 // they need pseudos.
7735 case ARM::t2STR_preidx:
7736 MI->setDesc(TII->get(ARM::t2STR_PRE));
7737 return BB;
7738 case ARM::t2STRB_preidx:
7739 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7740 return BB;
7741 case ARM::t2STRH_preidx:
7742 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7743 return BB;
7744
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007745 case ARM::STRi_preidx:
7746 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007747 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007748 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7749 // Decode the offset.
7750 unsigned Offset = MI->getOperand(4).getImm();
7751 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7752 Offset = ARM_AM::getAM2Offset(Offset);
7753 if (isSub)
7754 Offset = -Offset;
7755
Jim Grosbachf402f692011-08-12 21:02:34 +00007756 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007757 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007758 .addOperand(MI->getOperand(0)) // Rn_wb
7759 .addOperand(MI->getOperand(1)) // Rt
7760 .addOperand(MI->getOperand(2)) // Rn
7761 .addImm(Offset) // offset (skip GPR==zero_reg)
7762 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007763 .addOperand(MI->getOperand(6))
7764 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007765 MI->eraseFromParent();
7766 return BB;
7767 }
7768 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007769 case ARM::STRBr_preidx:
7770 case ARM::STRH_preidx: {
7771 unsigned NewOpc;
7772 switch (MI->getOpcode()) {
7773 default: llvm_unreachable("unexpected opcode!");
7774 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7775 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7776 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7777 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007778 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7779 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7780 MIB.addOperand(MI->getOperand(i));
7781 MI->eraseFromParent();
7782 return BB;
7783 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007784
Evan Chengbb2af352009-08-12 05:17:19 +00007785 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007786 // To "insert" a SELECT_CC instruction, we actually have to insert the
7787 // diamond control-flow pattern. The incoming instruction knows the
7788 // destination vreg to set, the condition code register to branch on, the
7789 // true/false values to select between, and a branch opcode to use.
7790 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007791 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007792 ++It;
7793
7794 // thisMBB:
7795 // ...
7796 // TrueVal = ...
7797 // cmpTY ccX, r1, r2
7798 // bCC copy1MBB
7799 // fallthrough --> copy0MBB
7800 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007801 MachineFunction *F = BB->getParent();
7802 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7803 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007804 F->insert(It, copy0MBB);
7805 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007806
7807 // Transfer the remainder of BB and its successor edges to sinkMBB.
7808 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007809 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007810 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7811
Dan Gohmanf4f04102010-07-06 15:49:48 +00007812 BB->addSuccessor(copy0MBB);
7813 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007814
Dan Gohman34396292010-07-06 20:24:04 +00007815 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7816 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7817
Evan Cheng10043e22007-01-19 07:51:42 +00007818 // copy0MBB:
7819 // %FalseValue = ...
7820 // # fallthrough to sinkMBB
7821 BB = copy0MBB;
7822
7823 // Update machine-CFG edges
7824 BB->addSuccessor(sinkMBB);
7825
7826 // sinkMBB:
7827 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7828 // ...
7829 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007830 BuildMI(*BB, BB->begin(), dl,
7831 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007832 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7833 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7834
Dan Gohman34396292010-07-06 20:24:04 +00007835 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007836 return BB;
7837 }
Evan Chengb972e562009-08-07 00:34:42 +00007838
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007839 case ARM::BCCi64:
7840 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007841 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007842 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007843
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007844 // Compare both parts that make up the double comparison separately for
7845 // equality.
7846 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7847
7848 unsigned LHS1 = MI->getOperand(1).getReg();
7849 unsigned LHS2 = MI->getOperand(2).getReg();
7850 if (RHSisZero) {
7851 AddDefaultPred(BuildMI(BB, dl,
7852 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7853 .addReg(LHS1).addImm(0));
7854 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7855 .addReg(LHS2).addImm(0)
7856 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7857 } else {
7858 unsigned RHS1 = MI->getOperand(3).getReg();
7859 unsigned RHS2 = MI->getOperand(4).getReg();
7860 AddDefaultPred(BuildMI(BB, dl,
7861 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7862 .addReg(LHS1).addReg(RHS1));
7863 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7864 .addReg(LHS2).addReg(RHS2)
7865 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7866 }
7867
7868 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7869 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7870 if (MI->getOperand(0).getImm() == ARMCC::NE)
7871 std::swap(destMBB, exitMBB);
7872
7873 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7874 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007875 if (isThumb2)
7876 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7877 else
7878 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007879
7880 MI->eraseFromParent(); // The pseudo instruction is gone now.
7881 return BB;
7882 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007883
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007884 case ARM::Int_eh_sjlj_setjmp:
7885 case ARM::Int_eh_sjlj_setjmp_nofp:
7886 case ARM::tInt_eh_sjlj_setjmp:
7887 case ARM::t2Int_eh_sjlj_setjmp:
7888 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00007889 return BB;
7890
7891 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007892 EmitSjLjDispatchBlock(MI, BB);
7893 return BB;
7894
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007895 case ARM::ABS:
7896 case ARM::t2ABS: {
7897 // To insert an ABS instruction, we have to insert the
7898 // diamond control-flow pattern. The incoming instruction knows the
7899 // source vreg to test against 0, the destination vreg to set,
7900 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007901 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007902 // It transforms
7903 // V1 = ABS V0
7904 // into
7905 // V2 = MOVS V0
7906 // BCC (branch to SinkBB if V0 >= 0)
7907 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007908 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007909 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7910 MachineFunction::iterator BBI = BB;
7911 ++BBI;
7912 MachineFunction *Fn = BB->getParent();
7913 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7914 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7915 Fn->insert(BBI, RSBBB);
7916 Fn->insert(BBI, SinkBB);
7917
7918 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7919 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00007920 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007921 bool isThumb2 = Subtarget->isThumb2();
7922 MachineRegisterInfo &MRI = Fn->getRegInfo();
7923 // In Thumb mode S must not be specified if source register is the SP or
7924 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007925 unsigned NewRsbDstReg =
7926 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007927
7928 // Transfer the remainder of BB and its successor edges to sinkMBB.
7929 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007930 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007931 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7932
7933 BB->addSuccessor(RSBBB);
7934 BB->addSuccessor(SinkBB);
7935
7936 // fall through to SinkMBB
7937 RSBBB->addSuccessor(SinkBB);
7938
Manman Rene0763c72012-06-15 21:32:12 +00007939 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007940 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007941 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7942 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007943
7944 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007945 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007946 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7947 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7948
7949 // insert rsbri in RSBBB
7950 // Note: BCC and rsbri will be converted into predicated rsbmi
7951 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007952 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007953 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00007954 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007955 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7956
Andrew Trick3f07c422011-10-18 18:40:53 +00007957 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007958 // reuse ABSDstReg to not change uses of ABS instruction
7959 BuildMI(*SinkBB, SinkBB->begin(), dl,
7960 TII->get(ARM::PHI), ABSDstReg)
7961 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007962 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007963
7964 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007965 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007966
7967 // return last added BB
7968 return SinkBB;
7969 }
Manman Rene8735522012-06-01 19:33:18 +00007970 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007971 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007972 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007973 case ARM::WIN__CHKSTK:
7974 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007975 }
7976}
7977
Evan Chenge6fba772011-08-30 19:09:48 +00007978void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7979 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007980 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007981 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7982 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7983 // operand is still set to noreg. If needed, set the optional operand's
7984 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007985 //
Andrew Trick88b24502011-10-18 19:18:52 +00007986 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007987
Andrew Trick924123a2011-09-21 02:20:46 +00007988 // Rename pseudo opcodes.
7989 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7990 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007991 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007992 MCID = &TII->get(NewOpc);
7993
7994 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7995 "converted opcode should be the same except for cc_out");
7996
7997 MI->setDesc(*MCID);
7998
7999 // Add the optional cc_out operand
8000 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008001 }
Andrew Trick88b24502011-10-18 19:18:52 +00008002 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008003
8004 // Any ARM instruction that sets the 's' bit should specify an optional
8005 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008006 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008007 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008008 return;
8009 }
Andrew Trick924123a2011-09-21 02:20:46 +00008010 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8011 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008012 bool definesCPSR = false;
8013 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008014 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008015 i != e; ++i) {
8016 const MachineOperand &MO = MI->getOperand(i);
8017 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8018 definesCPSR = true;
8019 if (MO.isDead())
8020 deadCPSR = true;
8021 MI->RemoveOperand(i);
8022 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008023 }
8024 }
Andrew Trick8586e622011-09-20 03:17:40 +00008025 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008026 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008027 return;
8028 }
8029 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008030 if (deadCPSR) {
8031 assert(!MI->getOperand(ccOutIdx).getReg() &&
8032 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008033 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008034 }
Andrew Trick8586e622011-09-20 03:17:40 +00008035
Andrew Trick924123a2011-09-21 02:20:46 +00008036 // If this instruction was defined with an optional CPSR def and its dag node
8037 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008038 MachineOperand &MO = MI->getOperand(ccOutIdx);
8039 MO.setReg(ARM::CPSR);
8040 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008041}
8042
Evan Cheng10043e22007-01-19 07:51:42 +00008043//===----------------------------------------------------------------------===//
8044// ARM Optimization Hooks
8045//===----------------------------------------------------------------------===//
8046
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008047// Helper function that checks if N is a null or all ones constant.
8048static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
8049 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
8050 if (!C)
8051 return false;
8052 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
8053}
8054
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008055// Return true if N is conditionally 0 or all ones.
8056// Detects these expressions where cc is an i1 value:
8057//
8058// (select cc 0, y) [AllOnes=0]
8059// (select cc y, 0) [AllOnes=0]
8060// (zext cc) [AllOnes=0]
8061// (sext cc) [AllOnes=0/1]
8062// (select cc -1, y) [AllOnes=1]
8063// (select cc y, -1) [AllOnes=1]
8064//
8065// Invert is set when N is the null/all ones constant when CC is false.
8066// OtherOp is set to the alternative value of N.
8067static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8068 SDValue &CC, bool &Invert,
8069 SDValue &OtherOp,
8070 SelectionDAG &DAG) {
8071 switch (N->getOpcode()) {
8072 default: return false;
8073 case ISD::SELECT: {
8074 CC = N->getOperand(0);
8075 SDValue N1 = N->getOperand(1);
8076 SDValue N2 = N->getOperand(2);
8077 if (isZeroOrAllOnes(N1, AllOnes)) {
8078 Invert = false;
8079 OtherOp = N2;
8080 return true;
8081 }
8082 if (isZeroOrAllOnes(N2, AllOnes)) {
8083 Invert = true;
8084 OtherOp = N1;
8085 return true;
8086 }
8087 return false;
8088 }
8089 case ISD::ZERO_EXTEND:
8090 // (zext cc) can never be the all ones value.
8091 if (AllOnes)
8092 return false;
8093 // Fall through.
8094 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008095 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008096 EVT VT = N->getValueType(0);
8097 CC = N->getOperand(0);
8098 if (CC.getValueType() != MVT::i1)
8099 return false;
8100 Invert = !AllOnes;
8101 if (AllOnes)
8102 // When looking for an AllOnes constant, N is an sext, and the 'other'
8103 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008104 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008105 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8106 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008107 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008108 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008109 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8110 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008111 return true;
8112 }
8113 }
8114}
8115
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008116// Combine a constant select operand into its use:
8117//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008118// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8119// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8120// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8121// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8122// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008123//
8124// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008125// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008126//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008127// Also recognize sext/zext from i1:
8128//
8129// (add (zext cc), x) -> (select cc (add x, 1), x)
8130// (add (sext cc), x) -> (select cc (add x, -1), x)
8131//
8132// These transformations eventually create predicated instructions.
8133//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008134// @param N The node to transform.
8135// @param Slct The N operand that is a select.
8136// @param OtherOp The other N operand (x above).
8137// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008138// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008139// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008140static
8141SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008142 TargetLowering::DAGCombinerInfo &DCI,
8143 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008144 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008145 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008146 SDValue NonConstantVal;
8147 SDValue CCOp;
8148 bool SwapSelectOps;
8149 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8150 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008151 return SDValue();
8152
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008153 // Slct is now know to be the desired identity constant when CC is true.
8154 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008155 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008156 OtherOp, NonConstantVal);
8157 // Unless SwapSelectOps says CC should be false.
8158 if (SwapSelectOps)
8159 std::swap(TrueVal, FalseVal);
8160
Andrew Trickef9de2a2013-05-25 02:42:55 +00008161 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008162 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008163}
8164
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008165// Attempt combineSelectAndUse on each operand of a commutative operator N.
8166static
8167SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8168 TargetLowering::DAGCombinerInfo &DCI) {
8169 SDValue N0 = N->getOperand(0);
8170 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008171 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008172 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8173 if (Result.getNode())
8174 return Result;
8175 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008176 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008177 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8178 if (Result.getNode())
8179 return Result;
8180 }
8181 return SDValue();
8182}
8183
Eric Christopher1b8b94192011-06-29 21:10:36 +00008184// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008185// (only after legalization).
8186static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8187 TargetLowering::DAGCombinerInfo &DCI,
8188 const ARMSubtarget *Subtarget) {
8189
8190 // Only perform optimization if after legalize, and if NEON is available. We
8191 // also expected both operands to be BUILD_VECTORs.
8192 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8193 || N0.getOpcode() != ISD::BUILD_VECTOR
8194 || N1.getOpcode() != ISD::BUILD_VECTOR)
8195 return SDValue();
8196
8197 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8198 EVT VT = N->getValueType(0);
8199 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8200 return SDValue();
8201
8202 // Check that the vector operands are of the right form.
8203 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8204 // operands, where N is the size of the formed vector.
8205 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8206 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008207
8208 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008209 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008210 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008211 SDValue Vec = N0->getOperand(0)->getOperand(0);
8212 SDNode *V = Vec.getNode();
8213 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008214
Eric Christopher1b8b94192011-06-29 21:10:36 +00008215 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008216 // check to see if each of their operands are an EXTRACT_VECTOR with
8217 // the same vector and appropriate index.
8218 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8219 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8220 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008221
Tanya Lattnere9e67052011-06-14 23:48:48 +00008222 SDValue ExtVec0 = N0->getOperand(i);
8223 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008224
Tanya Lattnere9e67052011-06-14 23:48:48 +00008225 // First operand is the vector, verify its the same.
8226 if (V != ExtVec0->getOperand(0).getNode() ||
8227 V != ExtVec1->getOperand(0).getNode())
8228 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008229
Tanya Lattnere9e67052011-06-14 23:48:48 +00008230 // Second is the constant, verify its correct.
8231 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8232 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008233
Tanya Lattnere9e67052011-06-14 23:48:48 +00008234 // For the constant, we want to see all the even or all the odd.
8235 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8236 || C1->getZExtValue() != nextIndex+1)
8237 return SDValue();
8238
8239 // Increment index.
8240 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008241 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008242 return SDValue();
8243 }
8244
8245 // Create VPADDL node.
8246 SelectionDAG &DAG = DCI.DAG;
8247 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008248
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008249 SDLoc dl(N);
8250
Tanya Lattnere9e67052011-06-14 23:48:48 +00008251 // Build operand list.
8252 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008253 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008254 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008255
8256 // Input is the vector.
8257 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008258
Tanya Lattnere9e67052011-06-14 23:48:48 +00008259 // Get widened type and narrowed type.
8260 MVT widenType;
8261 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008262
Silviu Barangaa3106e62014-04-03 10:44:27 +00008263 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8264 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008265 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8266 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8267 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8268 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008269 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008270 }
8271
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008272 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008273 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008274 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008275}
8276
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008277static SDValue findMUL_LOHI(SDValue V) {
8278 if (V->getOpcode() == ISD::UMUL_LOHI ||
8279 V->getOpcode() == ISD::SMUL_LOHI)
8280 return V;
8281 return SDValue();
8282}
8283
8284static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8285 TargetLowering::DAGCombinerInfo &DCI,
8286 const ARMSubtarget *Subtarget) {
8287
8288 if (Subtarget->isThumb1Only()) return SDValue();
8289
8290 // Only perform the checks after legalize when the pattern is available.
8291 if (DCI.isBeforeLegalize()) return SDValue();
8292
8293 // Look for multiply add opportunities.
8294 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8295 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8296 // a glue link from the first add to the second add.
8297 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8298 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008299 // UMUL_LOHI
8300 // / :lo \ :hi
8301 // / \ [no multiline comment]
8302 // loAdd -> ADDE |
8303 // \ :glue /
8304 // \ /
8305 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008306 //
8307 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8308 SDValue AddcOp0 = AddcNode->getOperand(0);
8309 SDValue AddcOp1 = AddcNode->getOperand(1);
8310
8311 // Check if the two operands are from the same mul_lohi node.
8312 if (AddcOp0.getNode() == AddcOp1.getNode())
8313 return SDValue();
8314
8315 assert(AddcNode->getNumValues() == 2 &&
8316 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008317 "Expect ADDC with two result values. First: i32");
8318
8319 // Check that we have a glued ADDC node.
8320 if (AddcNode->getValueType(1) != MVT::Glue)
8321 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008322
8323 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8324 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8325 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8326 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8327 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8328 return SDValue();
8329
8330 // Look for the glued ADDE.
8331 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008332 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008333 return SDValue();
8334
8335 // Make sure it is really an ADDE.
8336 if (AddeNode->getOpcode() != ISD::ADDE)
8337 return SDValue();
8338
8339 assert(AddeNode->getNumOperands() == 3 &&
8340 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8341 "ADDE node has the wrong inputs");
8342
8343 // Check for the triangle shape.
8344 SDValue AddeOp0 = AddeNode->getOperand(0);
8345 SDValue AddeOp1 = AddeNode->getOperand(1);
8346
8347 // Make sure that the ADDE operands are not coming from the same node.
8348 if (AddeOp0.getNode() == AddeOp1.getNode())
8349 return SDValue();
8350
8351 // Find the MUL_LOHI node walking up ADDE's operands.
8352 bool IsLeftOperandMUL = false;
8353 SDValue MULOp = findMUL_LOHI(AddeOp0);
8354 if (MULOp == SDValue())
8355 MULOp = findMUL_LOHI(AddeOp1);
8356 else
8357 IsLeftOperandMUL = true;
8358 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008359 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008360
8361 // Figure out the right opcode.
8362 unsigned Opc = MULOp->getOpcode();
8363 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8364
8365 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008366 SDValue* HiAdd = nullptr;
8367 SDValue* LoMul = nullptr;
8368 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008369
Jyoti Allurf1d70502015-01-23 09:10:03 +00008370 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8371 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8372 return SDValue();
8373
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008374 if (IsLeftOperandMUL)
8375 HiAdd = &AddeOp1;
8376 else
8377 HiAdd = &AddeOp0;
8378
8379
Jyoti Allurf1d70502015-01-23 09:10:03 +00008380 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8381 // whose low result is fed to the ADDC we are checking.
8382
8383 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008384 LoMul = &AddcOp0;
8385 LowAdd = &AddcOp1;
8386 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008387 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008388 LoMul = &AddcOp1;
8389 LowAdd = &AddcOp0;
8390 }
8391
Craig Topper062a2ba2014-04-25 05:30:21 +00008392 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008393 return SDValue();
8394
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008395 // Create the merged node.
8396 SelectionDAG &DAG = DCI.DAG;
8397
8398 // Build operand list.
8399 SmallVector<SDValue, 8> Ops;
8400 Ops.push_back(LoMul->getOperand(0));
8401 Ops.push_back(LoMul->getOperand(1));
8402 Ops.push_back(*LowAdd);
8403 Ops.push_back(*HiAdd);
8404
Andrew Trickef9de2a2013-05-25 02:42:55 +00008405 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008406 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008407
8408 // Replace the ADDs' nodes uses by the MLA node's values.
8409 SDValue HiMLALResult(MLALNode.getNode(), 1);
8410 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8411
8412 SDValue LoMLALResult(MLALNode.getNode(), 0);
8413 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8414
8415 // Return original node to notify the driver to stop replacing.
8416 SDValue resNode(AddcNode, 0);
8417 return resNode;
8418}
8419
8420/// PerformADDCCombine - Target-specific dag combine transform from
8421/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8422static SDValue PerformADDCCombine(SDNode *N,
8423 TargetLowering::DAGCombinerInfo &DCI,
8424 const ARMSubtarget *Subtarget) {
8425
8426 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8427
8428}
8429
Bob Wilson728eb292010-07-29 20:34:14 +00008430/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8431/// operands N0 and N1. This is a helper for PerformADDCombine that is
8432/// called with the default operands, and if that fails, with commuted
8433/// operands.
8434static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008435 TargetLowering::DAGCombinerInfo &DCI,
8436 const ARMSubtarget *Subtarget){
8437
8438 // Attempt to create vpaddl for this add.
8439 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8440 if (Result.getNode())
8441 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008442
Chris Lattner4147f082009-03-12 06:52:53 +00008443 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008444 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008445 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8446 if (Result.getNode()) return Result;
8447 }
Chris Lattner4147f082009-03-12 06:52:53 +00008448 return SDValue();
8449}
8450
Bob Wilson728eb292010-07-29 20:34:14 +00008451/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8452///
8453static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008454 TargetLowering::DAGCombinerInfo &DCI,
8455 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008456 SDValue N0 = N->getOperand(0);
8457 SDValue N1 = N->getOperand(1);
8458
8459 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008460 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008461 if (Result.getNode())
8462 return Result;
8463
8464 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008465 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008466}
8467
Chris Lattner4147f082009-03-12 06:52:53 +00008468/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008469///
Chris Lattner4147f082009-03-12 06:52:53 +00008470static SDValue PerformSUBCombine(SDNode *N,
8471 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008472 SDValue N0 = N->getOperand(0);
8473 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008474
Chris Lattner4147f082009-03-12 06:52:53 +00008475 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008476 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008477 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8478 if (Result.getNode()) return Result;
8479 }
Bob Wilson7117a912009-03-20 22:42:55 +00008480
Chris Lattner4147f082009-03-12 06:52:53 +00008481 return SDValue();
8482}
8483
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008484/// PerformVMULCombine
8485/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8486/// special multiplier accumulator forwarding.
8487/// vmul d3, d0, d2
8488/// vmla d3, d1, d2
8489/// is faster than
8490/// vadd d3, d0, d1
8491/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008492// However, for (A + B) * (A + B),
8493// vadd d2, d0, d1
8494// vmul d3, d0, d2
8495// vmla d3, d1, d2
8496// is slower than
8497// vadd d2, d0, d1
8498// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008499static SDValue PerformVMULCombine(SDNode *N,
8500 TargetLowering::DAGCombinerInfo &DCI,
8501 const ARMSubtarget *Subtarget) {
8502 if (!Subtarget->hasVMLxForwarding())
8503 return SDValue();
8504
8505 SelectionDAG &DAG = DCI.DAG;
8506 SDValue N0 = N->getOperand(0);
8507 SDValue N1 = N->getOperand(1);
8508 unsigned Opcode = N0.getOpcode();
8509 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8510 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008511 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008512 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8513 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8514 return SDValue();
8515 std::swap(N0, N1);
8516 }
8517
Weiming Zhao2052f482013-09-25 23:12:06 +00008518 if (N0 == N1)
8519 return SDValue();
8520
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008521 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008522 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008523 SDValue N00 = N0->getOperand(0);
8524 SDValue N01 = N0->getOperand(1);
8525 return DAG.getNode(Opcode, DL, VT,
8526 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8527 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8528}
8529
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008530static SDValue PerformMULCombine(SDNode *N,
8531 TargetLowering::DAGCombinerInfo &DCI,
8532 const ARMSubtarget *Subtarget) {
8533 SelectionDAG &DAG = DCI.DAG;
8534
8535 if (Subtarget->isThumb1Only())
8536 return SDValue();
8537
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008538 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8539 return SDValue();
8540
8541 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008542 if (VT.is64BitVector() || VT.is128BitVector())
8543 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008544 if (VT != MVT::i32)
8545 return SDValue();
8546
8547 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8548 if (!C)
8549 return SDValue();
8550
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008551 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008552 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008553
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008554 ShiftAmt = ShiftAmt & (32 - 1);
8555 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008556 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008557
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008558 SDValue Res;
8559 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008560
8561 if (MulAmt >= 0) {
8562 if (isPowerOf2_32(MulAmt - 1)) {
8563 // (mul x, 2^N + 1) => (add (shl x, N), x)
8564 Res = DAG.getNode(ISD::ADD, DL, VT,
8565 V,
8566 DAG.getNode(ISD::SHL, DL, VT,
8567 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008568 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008569 MVT::i32)));
8570 } else if (isPowerOf2_32(MulAmt + 1)) {
8571 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8572 Res = DAG.getNode(ISD::SUB, DL, VT,
8573 DAG.getNode(ISD::SHL, DL, VT,
8574 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008575 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008576 MVT::i32)),
8577 V);
8578 } else
8579 return SDValue();
8580 } else {
8581 uint64_t MulAmtAbs = -MulAmt;
8582 if (isPowerOf2_32(MulAmtAbs + 1)) {
8583 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8584 Res = DAG.getNode(ISD::SUB, DL, VT,
8585 V,
8586 DAG.getNode(ISD::SHL, DL, VT,
8587 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008588 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008589 MVT::i32)));
8590 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8591 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8592 Res = DAG.getNode(ISD::ADD, DL, VT,
8593 V,
8594 DAG.getNode(ISD::SHL, DL, VT,
8595 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008596 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008597 MVT::i32)));
8598 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008599 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008600
8601 } else
8602 return SDValue();
8603 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008604
8605 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008606 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008607 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008608
8609 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008610 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008611 return SDValue();
8612}
8613
Owen Anderson30c48922010-11-05 19:27:46 +00008614static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008615 TargetLowering::DAGCombinerInfo &DCI,
8616 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008617
Owen Anderson30c48922010-11-05 19:27:46 +00008618 // Attempt to use immediate-form VBIC
8619 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008620 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008621 EVT VT = N->getValueType(0);
8622 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008623
Tanya Lattner266792a2011-04-07 15:24:20 +00008624 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8625 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008626
Owen Anderson30c48922010-11-05 19:27:46 +00008627 APInt SplatBits, SplatUndef;
8628 unsigned SplatBitSize;
8629 bool HasAnyUndefs;
8630 if (BVN &&
8631 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8632 if (SplatBitSize <= 64) {
8633 EVT VbicVT;
8634 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8635 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008636 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008637 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008638 if (Val.getNode()) {
8639 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008640 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008641 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008642 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008643 }
8644 }
8645 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008646
Evan Chenge87681c2012-02-23 01:19:06 +00008647 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008648 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8649 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8650 if (Result.getNode())
8651 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008652 }
8653
Owen Anderson30c48922010-11-05 19:27:46 +00008654 return SDValue();
8655}
8656
Jim Grosbach11013ed2010-07-16 23:05:05 +00008657/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8658static SDValue PerformORCombine(SDNode *N,
8659 TargetLowering::DAGCombinerInfo &DCI,
8660 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008661 // Attempt to use immediate-form VORR
8662 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008663 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008664 EVT VT = N->getValueType(0);
8665 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008666
Tanya Lattner266792a2011-04-07 15:24:20 +00008667 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8668 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008669
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008670 APInt SplatBits, SplatUndef;
8671 unsigned SplatBitSize;
8672 bool HasAnyUndefs;
8673 if (BVN && Subtarget->hasNEON() &&
8674 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8675 if (SplatBitSize <= 64) {
8676 EVT VorrVT;
8677 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8678 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008679 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008680 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008681 if (Val.getNode()) {
8682 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008683 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008684 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008685 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008686 }
8687 }
8688 }
8689
Evan Chenge87681c2012-02-23 01:19:06 +00008690 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008691 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8692 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8693 if (Result.getNode())
8694 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008695 }
8696
Nadav Rotem3a94c542012-08-13 18:52:44 +00008697 // The code below optimizes (or (and X, Y), Z).
8698 // The AND operand needs to have a single user to make these optimizations
8699 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008700 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008701 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008702 return SDValue();
8703 SDValue N1 = N->getOperand(1);
8704
8705 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8706 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8707 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8708 APInt SplatUndef;
8709 unsigned SplatBitSize;
8710 bool HasAnyUndefs;
8711
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008712 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008713 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008714 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8715 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008716 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008717 HasAnyUndefs) && !HasAnyUndefs) {
8718 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8719 HasAnyUndefs) && !HasAnyUndefs) {
8720 // Ensure that the bit width of the constants are the same and that
8721 // the splat arguments are logical inverses as per the pattern we
8722 // are trying to simplify.
8723 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8724 SplatBits0 == ~SplatBits1) {
8725 // Canonicalize the vector type to make instruction selection
8726 // simpler.
8727 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8728 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8729 N0->getOperand(1),
8730 N0->getOperand(0),
8731 N1->getOperand(0));
8732 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8733 }
8734 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008735 }
8736 }
8737
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008738 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8739 // reasonable.
8740
Jim Grosbach11013ed2010-07-16 23:05:05 +00008741 // BFI is only available on V6T2+
8742 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8743 return SDValue();
8744
Andrew Trickef9de2a2013-05-25 02:42:55 +00008745 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008746 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008747 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008748 //
8749 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008750 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008751 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008752 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008753 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008754 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008755
Jim Grosbach11013ed2010-07-16 23:05:05 +00008756 if (VT != MVT::i32)
8757 return SDValue();
8758
Evan Cheng2e51bb42010-12-13 20:32:54 +00008759 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008760
Jim Grosbach11013ed2010-07-16 23:05:05 +00008761 // The value and the mask need to be constants so we can verify this is
8762 // actually a bitfield set. If the mask is 0xffff, we can do better
8763 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008764 SDValue MaskOp = N0.getOperand(1);
8765 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8766 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008767 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008768 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008769 if (Mask == 0xffff)
8770 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008771 SDValue Res;
8772 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008773 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8774 if (N1C) {
8775 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008776 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008777 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008778
Evan Cheng34345752010-12-11 04:11:38 +00008779 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008780 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008781
Evan Cheng2e51bb42010-12-13 20:32:54 +00008782 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008783 DAG.getConstant(Val, DL, MVT::i32),
8784 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008785
8786 // Do not add new nodes to DAG combiner worklist.
8787 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008788 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008789 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008790 } else if (N1.getOpcode() == ISD::AND) {
8791 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008792 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8793 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008794 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008795 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008796
Eric Christopherd5530962011-03-26 01:21:03 +00008797 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8798 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008799 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008800 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008801 // The pack halfword instruction works better for masks that fit it,
8802 // so use that when it's available.
8803 if (Subtarget->hasT2ExtractPack() &&
8804 (Mask == 0xffff || Mask == 0xffff0000))
8805 return SDValue();
8806 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008807 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008808 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008809 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008810 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008811 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008812 // Do not add new nodes to DAG combiner worklist.
8813 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008814 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008815 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008816 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008817 // The pack halfword instruction works better for masks that fit it,
8818 // so use that when it's available.
8819 if (Subtarget->hasT2ExtractPack() &&
8820 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8821 return SDValue();
8822 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008823 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008824 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008825 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008826 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008827 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008828 // Do not add new nodes to DAG combiner worklist.
8829 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008830 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008831 }
8832 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008833
Evan Cheng2e51bb42010-12-13 20:32:54 +00008834 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8835 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8836 ARM::isBitFieldInvertedMask(~Mask)) {
8837 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8838 // where lsb(mask) == #shamt and masked bits of B are known zero.
8839 SDValue ShAmt = N00.getOperand(1);
8840 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008841 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008842 if (ShAmtC != LSB)
8843 return SDValue();
8844
8845 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008846 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008847
8848 // Do not add new nodes to DAG combiner worklist.
8849 DCI.CombineTo(N, Res, false);
8850 }
8851
Jim Grosbach11013ed2010-07-16 23:05:05 +00008852 return SDValue();
8853}
8854
Evan Chenge87681c2012-02-23 01:19:06 +00008855static SDValue PerformXORCombine(SDNode *N,
8856 TargetLowering::DAGCombinerInfo &DCI,
8857 const ARMSubtarget *Subtarget) {
8858 EVT VT = N->getValueType(0);
8859 SelectionDAG &DAG = DCI.DAG;
8860
8861 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8862 return SDValue();
8863
8864 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008865 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8866 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8867 if (Result.getNode())
8868 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008869 }
8870
8871 return SDValue();
8872}
8873
Evan Cheng6d02d902011-06-15 01:12:31 +00008874/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8875/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008876static SDValue PerformBFICombine(SDNode *N,
8877 TargetLowering::DAGCombinerInfo &DCI) {
8878 SDValue N1 = N->getOperand(1);
8879 if (N1.getOpcode() == ISD::AND) {
8880 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8881 if (!N11C)
8882 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008883 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008884 unsigned LSB = countTrailingZeros(~InvMask);
8885 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008886 assert(Width <
8887 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008888 "undefined behavior");
8889 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008890 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008891 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008892 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008893 N->getOperand(0), N1.getOperand(0),
8894 N->getOperand(2));
8895 }
8896 return SDValue();
8897}
8898
Bob Wilson22806742010-09-22 22:09:21 +00008899/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8900/// ARMISD::VMOVRRD.
8901static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008902 TargetLowering::DAGCombinerInfo &DCI,
8903 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008904 // vmovrrd(vmovdrr x, y) -> x,y
8905 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008906 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008907 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008908
8909 // vmovrrd(load f64) -> (load i32), (load i32)
8910 SDNode *InNode = InDouble.getNode();
8911 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8912 InNode->getValueType(0) == MVT::f64 &&
8913 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8914 !cast<LoadSDNode>(InNode)->isVolatile()) {
8915 // TODO: Should this be done for non-FrameIndex operands?
8916 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8917
8918 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008919 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008920 SDValue BasePtr = LD->getBasePtr();
8921 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8922 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008923 LD->isNonTemporal(), LD->isInvariant(),
8924 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008925
8926 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008927 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008928 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8929 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008930 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008931 std::min(4U, LD->getAlignment() / 2));
8932
8933 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00008934 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00008935 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008936 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008937 return Result;
8938 }
8939
Bob Wilson22806742010-09-22 22:09:21 +00008940 return SDValue();
8941}
8942
8943/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8944/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8945static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8946 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8947 SDValue Op0 = N->getOperand(0);
8948 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008949 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008950 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008951 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008952 Op1 = Op1.getOperand(0);
8953 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8954 Op0.getNode() == Op1.getNode() &&
8955 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008956 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008957 N->getValueType(0), Op0.getOperand(0));
8958 return SDValue();
8959}
8960
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008961/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8962/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8963/// i64 vector to have f64 elements, since the value can then be loaded
8964/// directly into a VFP register.
8965static bool hasNormalLoadOperand(SDNode *N) {
8966 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8967 for (unsigned i = 0; i < NumElts; ++i) {
8968 SDNode *Elt = N->getOperand(i).getNode();
8969 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8970 return true;
8971 }
8972 return false;
8973}
8974
Bob Wilsoncb6db982010-09-17 22:59:05 +00008975/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8976/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008977static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008978 TargetLowering::DAGCombinerInfo &DCI,
8979 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008980 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8981 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8982 // into a pair of GPRs, which is fine when the value is used as a scalar,
8983 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008984 SelectionDAG &DAG = DCI.DAG;
8985 if (N->getNumOperands() == 2) {
8986 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8987 if (RV.getNode())
8988 return RV;
8989 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008990
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008991 // Load i64 elements as f64 values so that type legalization does not split
8992 // them up into i32 values.
8993 EVT VT = N->getValueType(0);
8994 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8995 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008996 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008997 SmallVector<SDValue, 8> Ops;
8998 unsigned NumElts = VT.getVectorNumElements();
8999 for (unsigned i = 0; i < NumElts; ++i) {
9000 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9001 Ops.push_back(V);
9002 // Make the DAGCombiner fold the bitcast.
9003 DCI.AddToWorklist(V.getNode());
9004 }
9005 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009006 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009007 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9008}
9009
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009010/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9011static SDValue
9012PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9013 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9014 // At that time, we may have inserted bitcasts from integer to float.
9015 // If these bitcasts have survived DAGCombine, change the lowering of this
9016 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9017 // force to use floating point types.
9018
9019 // Make sure we can change the type of the vector.
9020 // This is possible iff:
9021 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9022 // 1.1. Vector is used only once.
9023 // 1.2. Use is a bit convert to an integer type.
9024 // 2. The size of its operands are 32-bits (64-bits are not legal).
9025 EVT VT = N->getValueType(0);
9026 EVT EltVT = VT.getVectorElementType();
9027
9028 // Check 1.1. and 2.
9029 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9030 return SDValue();
9031
9032 // By construction, the input type must be float.
9033 assert(EltVT == MVT::f32 && "Unexpected type!");
9034
9035 // Check 1.2.
9036 SDNode *Use = *N->use_begin();
9037 if (Use->getOpcode() != ISD::BITCAST ||
9038 Use->getValueType(0).isFloatingPoint())
9039 return SDValue();
9040
9041 // Check profitability.
9042 // Model is, if more than half of the relevant operands are bitcast from
9043 // i32, turn the build_vector into a sequence of insert_vector_elt.
9044 // Relevant operands are everything that is not statically
9045 // (i.e., at compile time) bitcasted.
9046 unsigned NumOfBitCastedElts = 0;
9047 unsigned NumElts = VT.getVectorNumElements();
9048 unsigned NumOfRelevantElts = NumElts;
9049 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9050 SDValue Elt = N->getOperand(Idx);
9051 if (Elt->getOpcode() == ISD::BITCAST) {
9052 // Assume only bit cast to i32 will go away.
9053 if (Elt->getOperand(0).getValueType() == MVT::i32)
9054 ++NumOfBitCastedElts;
9055 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
9056 // Constants are statically casted, thus do not count them as
9057 // relevant operands.
9058 --NumOfRelevantElts;
9059 }
9060
9061 // Check if more than half of the elements require a non-free bitcast.
9062 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9063 return SDValue();
9064
9065 SelectionDAG &DAG = DCI.DAG;
9066 // Create the new vector type.
9067 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9068 // Check if the type is legal.
9069 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9070 if (!TLI.isTypeLegal(VecVT))
9071 return SDValue();
9072
9073 // Combine:
9074 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9075 // => BITCAST INSERT_VECTOR_ELT
9076 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9077 // (BITCAST EN), N.
9078 SDValue Vec = DAG.getUNDEF(VecVT);
9079 SDLoc dl(N);
9080 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9081 SDValue V = N->getOperand(Idx);
9082 if (V.getOpcode() == ISD::UNDEF)
9083 continue;
9084 if (V.getOpcode() == ISD::BITCAST &&
9085 V->getOperand(0).getValueType() == MVT::i32)
9086 // Fold obvious case.
9087 V = V.getOperand(0);
9088 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009089 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009090 // Make the DAGCombiner fold the bitcasts.
9091 DCI.AddToWorklist(V.getNode());
9092 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009093 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009094 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9095 }
9096 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9097 // Make the DAGCombiner fold the bitcasts.
9098 DCI.AddToWorklist(Vec.getNode());
9099 return Vec;
9100}
9101
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009102/// PerformInsertEltCombine - Target-specific dag combine xforms for
9103/// ISD::INSERT_VECTOR_ELT.
9104static SDValue PerformInsertEltCombine(SDNode *N,
9105 TargetLowering::DAGCombinerInfo &DCI) {
9106 // Bitcast an i64 load inserted into a vector to f64.
9107 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9108 EVT VT = N->getValueType(0);
9109 SDNode *Elt = N->getOperand(1).getNode();
9110 if (VT.getVectorElementType() != MVT::i64 ||
9111 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9112 return SDValue();
9113
9114 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009115 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009116 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9117 VT.getVectorNumElements());
9118 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9119 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9120 // Make the DAGCombiner fold the bitcasts.
9121 DCI.AddToWorklist(Vec.getNode());
9122 DCI.AddToWorklist(V.getNode());
9123 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9124 Vec, V, N->getOperand(2));
9125 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009126}
9127
Bob Wilsonc7334a12010-10-27 20:38:28 +00009128/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9129/// ISD::VECTOR_SHUFFLE.
9130static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9131 // The LLVM shufflevector instruction does not require the shuffle mask
9132 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9133 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9134 // operands do not match the mask length, they are extended by concatenating
9135 // them with undef vectors. That is probably the right thing for other
9136 // targets, but for NEON it is better to concatenate two double-register
9137 // size vector operands into a single quad-register size vector. Do that
9138 // transformation here:
9139 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9140 // shuffle(concat(v1, v2), undef)
9141 SDValue Op0 = N->getOperand(0);
9142 SDValue Op1 = N->getOperand(1);
9143 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9144 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9145 Op0.getNumOperands() != 2 ||
9146 Op1.getNumOperands() != 2)
9147 return SDValue();
9148 SDValue Concat0Op1 = Op0.getOperand(1);
9149 SDValue Concat1Op1 = Op1.getOperand(1);
9150 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9151 Concat1Op1.getOpcode() != ISD::UNDEF)
9152 return SDValue();
9153 // Skip the transformation if any of the types are illegal.
9154 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9155 EVT VT = N->getValueType(0);
9156 if (!TLI.isTypeLegal(VT) ||
9157 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9158 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9159 return SDValue();
9160
Andrew Trickef9de2a2013-05-25 02:42:55 +00009161 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009162 Op0.getOperand(0), Op1.getOperand(0));
9163 // Translate the shuffle mask.
9164 SmallVector<int, 16> NewMask;
9165 unsigned NumElts = VT.getVectorNumElements();
9166 unsigned HalfElts = NumElts/2;
9167 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9168 for (unsigned n = 0; n < NumElts; ++n) {
9169 int MaskElt = SVN->getMaskElt(n);
9170 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009171 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009172 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009173 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009174 NewElt = HalfElts + MaskElt - NumElts;
9175 NewMask.push_back(NewElt);
9176 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009177 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009178 DAG.getUNDEF(VT), NewMask.data());
9179}
9180
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009181/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9182/// NEON load/store intrinsics, and generic vector load/stores, to merge
9183/// base address updates.
9184/// For generic load/stores, the memory type is assumed to be a vector.
9185/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009186static SDValue CombineBaseUpdate(SDNode *N,
9187 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009188 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009189 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9190 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009191 const bool isStore = N->getOpcode() == ISD::STORE;
9192 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009193 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009194 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009195 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009196
9197 // Search for a use of the address operand that is an increment.
9198 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9199 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9200 SDNode *User = *UI;
9201 if (User->getOpcode() != ISD::ADD ||
9202 UI.getUse().getResNo() != Addr.getResNo())
9203 continue;
9204
9205 // Check that the add is independent of the load/store. Otherwise, folding
9206 // it would create a cycle.
9207 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9208 continue;
9209
9210 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009211 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009212 bool isLaneOp = false;
9213 unsigned NewOpc = 0;
9214 unsigned NumVecs = 0;
9215 if (isIntrinsic) {
9216 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9217 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009218 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009219 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9220 NumVecs = 1; break;
9221 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9222 NumVecs = 2; break;
9223 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9224 NumVecs = 3; break;
9225 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9226 NumVecs = 4; break;
9227 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9228 NumVecs = 2; isLaneOp = true; break;
9229 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9230 NumVecs = 3; isLaneOp = true; break;
9231 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9232 NumVecs = 4; isLaneOp = true; break;
9233 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009234 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009235 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009236 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009237 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009238 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009239 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009240 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009241 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009242 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009243 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009244 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009245 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009246 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009247 }
9248 } else {
9249 isLaneOp = true;
9250 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009251 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009252 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9253 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9254 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009255 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9256 NumVecs = 1; isLaneOp = false; break;
9257 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9258 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009259 }
9260 }
9261
9262 // Find the size of memory referenced by the load/store.
9263 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009264 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009265 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009266 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009267 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009268 } else {
9269 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9270 VecTy = N->getOperand(1).getValueType();
9271 }
9272
Bob Wilson06fce872011-02-07 17:43:21 +00009273 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9274 if (isLaneOp)
9275 NumBytes /= VecTy.getVectorNumElements();
9276
9277 // If the increment is a constant, it must match the memory ref size.
9278 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9279 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9280 uint64_t IncVal = CInc->getZExtValue();
9281 if (IncVal != NumBytes)
9282 continue;
9283 } else if (NumBytes >= 3 * 16) {
9284 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9285 // separate instructions that make it harder to use a non-constant update.
9286 continue;
9287 }
9288
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009289 // OK, we found an ADD we can fold into the base update.
9290 // Now, create a _UPD node, taking care of not breaking alignment.
9291
9292 EVT AlignedVecTy = VecTy;
9293 unsigned Alignment = MemN->getAlignment();
9294
9295 // If this is a less-than-standard-aligned load/store, change the type to
9296 // match the standard alignment.
9297 // The alignment is overlooked when selecting _UPD variants; and it's
9298 // easier to introduce bitcasts here than fix that.
9299 // There are 3 ways to get to this base-update combine:
9300 // - intrinsics: they are assumed to be properly aligned (to the standard
9301 // alignment of the memory type), so we don't need to do anything.
9302 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9303 // intrinsics, so, likewise, there's nothing to do.
9304 // - generic load/store instructions: the alignment is specified as an
9305 // explicit operand, rather than implicitly as the standard alignment
9306 // of the memory type (like the intrisics). We need to change the
9307 // memory type to match the explicit alignment. That way, we don't
9308 // generate non-standard-aligned ARMISD::VLDx nodes.
9309 if (isa<LSBaseSDNode>(N)) {
9310 if (Alignment == 0)
9311 Alignment = 1;
9312 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9313 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9314 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9315 assert(!isLaneOp && "Unexpected generic load/store lane.");
9316 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9317 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9318 }
9319 // Don't set an explicit alignment on regular load/stores that we want
9320 // to transform to VLD/VST 1_UPD nodes.
9321 // This matches the behavior of regular load/stores, which only get an
9322 // explicit alignment if the MMO alignment is larger than the standard
9323 // alignment of the memory type.
9324 // Intrinsics, however, always get an explicit alignment, set to the
9325 // alignment of the MMO.
9326 Alignment = 1;
9327 }
9328
Bob Wilson06fce872011-02-07 17:43:21 +00009329 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009330 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009331 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009332 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009333 unsigned n;
9334 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009335 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009336 Tys[n++] = MVT::i32;
9337 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009338 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009339
9340 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009341 SmallVector<SDValue, 8> Ops;
9342 Ops.push_back(N->getOperand(0)); // incoming chain
9343 Ops.push_back(N->getOperand(AddrOpIdx));
9344 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009345
9346 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9347 // Try to match the intrinsic's signature
9348 Ops.push_back(StN->getValue());
9349 } else {
9350 // Loads (and of course intrinsics) match the intrinsics' signature,
9351 // so just add all but the alignment operand.
9352 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9353 Ops.push_back(N->getOperand(i));
9354 }
9355
9356 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009357 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009358
9359 // If this is a non-standard-aligned STORE, the penultimate operand is the
9360 // stored value. Bitcast it to the aligned type.
9361 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9362 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009363 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009364 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009365
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009366 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009367 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009368 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009369
9370 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009371 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009372 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009373 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009374
9375 // If this is an non-standard-aligned LOAD, the first result is the loaded
9376 // value. Bitcast it to the expected result type.
9377 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9378 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009379 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009380 }
9381
Bob Wilson06fce872011-02-07 17:43:21 +00009382 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9383 DCI.CombineTo(N, NewResults);
9384 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9385
9386 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009387 }
Bob Wilson06fce872011-02-07 17:43:21 +00009388 return SDValue();
9389}
9390
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009391static SDValue PerformVLDCombine(SDNode *N,
9392 TargetLowering::DAGCombinerInfo &DCI) {
9393 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9394 return SDValue();
9395
9396 return CombineBaseUpdate(N, DCI);
9397}
9398
Bob Wilson2d790df2010-11-28 06:51:26 +00009399/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9400/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9401/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9402/// return true.
9403static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9404 SelectionDAG &DAG = DCI.DAG;
9405 EVT VT = N->getValueType(0);
9406 // vldN-dup instructions only support 64-bit vectors for N > 1.
9407 if (!VT.is64BitVector())
9408 return false;
9409
9410 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9411 SDNode *VLD = N->getOperand(0).getNode();
9412 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9413 return false;
9414 unsigned NumVecs = 0;
9415 unsigned NewOpc = 0;
9416 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9417 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9418 NumVecs = 2;
9419 NewOpc = ARMISD::VLD2DUP;
9420 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9421 NumVecs = 3;
9422 NewOpc = ARMISD::VLD3DUP;
9423 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9424 NumVecs = 4;
9425 NewOpc = ARMISD::VLD4DUP;
9426 } else {
9427 return false;
9428 }
9429
9430 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9431 // numbers match the load.
9432 unsigned VLDLaneNo =
9433 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9434 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9435 UI != UE; ++UI) {
9436 // Ignore uses of the chain result.
9437 if (UI.getUse().getResNo() == NumVecs)
9438 continue;
9439 SDNode *User = *UI;
9440 if (User->getOpcode() != ARMISD::VDUPLANE ||
9441 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9442 return false;
9443 }
9444
9445 // Create the vldN-dup node.
9446 EVT Tys[5];
9447 unsigned n;
9448 for (n = 0; n < NumVecs; ++n)
9449 Tys[n] = VT;
9450 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009451 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009452 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9453 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009454 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009455 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009456 VLDMemInt->getMemOperand());
9457
9458 // Update the uses.
9459 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9460 UI != UE; ++UI) {
9461 unsigned ResNo = UI.getUse().getResNo();
9462 // Ignore uses of the chain result.
9463 if (ResNo == NumVecs)
9464 continue;
9465 SDNode *User = *UI;
9466 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9467 }
9468
9469 // Now the vldN-lane intrinsic is dead except for its chain result.
9470 // Update uses of the chain.
9471 std::vector<SDValue> VLDDupResults;
9472 for (unsigned n = 0; n < NumVecs; ++n)
9473 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9474 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9475 DCI.CombineTo(VLD, VLDDupResults);
9476
9477 return true;
9478}
9479
Bob Wilson103a0dc2010-07-14 01:22:12 +00009480/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9481/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009482static SDValue PerformVDUPLANECombine(SDNode *N,
9483 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009484 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009485
Bob Wilson2d790df2010-11-28 06:51:26 +00009486 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9487 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9488 if (CombineVLDDUP(N, DCI))
9489 return SDValue(N, 0);
9490
9491 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9492 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009493 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009494 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009495 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009496 return SDValue();
9497
9498 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9499 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9500 // The canonical VMOV for a zero vector uses a 32-bit element size.
9501 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9502 unsigned EltBits;
9503 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9504 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009505 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009506 if (EltSize > VT.getVectorElementType().getSizeInBits())
9507 return SDValue();
9508
Andrew Trickef9de2a2013-05-25 02:42:55 +00009509 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009510}
9511
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009512static SDValue PerformLOADCombine(SDNode *N,
9513 TargetLowering::DAGCombinerInfo &DCI) {
9514 EVT VT = N->getValueType(0);
9515
9516 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9517 if (ISD::isNormalLoad(N) && VT.isVector() &&
9518 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9519 return CombineBaseUpdate(N, DCI);
9520
9521 return SDValue();
9522}
9523
Ahmed Bougacha23167462014-12-09 21:26:53 +00009524/// PerformSTORECombine - Target-specific dag combine xforms for
9525/// ISD::STORE.
9526static SDValue PerformSTORECombine(SDNode *N,
9527 TargetLowering::DAGCombinerInfo &DCI) {
9528 StoreSDNode *St = cast<StoreSDNode>(N);
9529 if (St->isVolatile())
9530 return SDValue();
9531
9532 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9533 // pack all of the elements in one place. Next, store to memory in fewer
9534 // chunks.
9535 SDValue StVal = St->getValue();
9536 EVT VT = StVal.getValueType();
9537 if (St->isTruncatingStore() && VT.isVector()) {
9538 SelectionDAG &DAG = DCI.DAG;
9539 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9540 EVT StVT = St->getMemoryVT();
9541 unsigned NumElems = VT.getVectorNumElements();
9542 assert(StVT != VT && "Cannot truncate to the same type");
9543 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9544 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9545
9546 // From, To sizes and ElemCount must be pow of two
9547 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9548
9549 // We are going to use the original vector elt for storing.
9550 // Accumulated smaller vector elements must be a multiple of the store size.
9551 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9552
9553 unsigned SizeRatio = FromEltSz / ToEltSz;
9554 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9555
9556 // Create a type on which we perform the shuffle.
9557 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9558 NumElems*SizeRatio);
9559 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9560
9561 SDLoc DL(St);
9562 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9563 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9564 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009565 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9566 ? (i + 1) * SizeRatio - 1
9567 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009568
9569 // Can't shuffle using an illegal type.
9570 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9571
9572 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9573 DAG.getUNDEF(WideVec.getValueType()),
9574 ShuffleVec.data());
9575 // At this point all of the data is stored at the bottom of the
9576 // register. We now need to save it to mem.
9577
9578 // Find the largest store unit
9579 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009580 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009581 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9582 StoreType = Tp;
9583 }
9584 // Didn't find a legal store type.
9585 if (!TLI.isTypeLegal(StoreType))
9586 return SDValue();
9587
9588 // Bitcast the original vector into a vector of store-size units
9589 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9590 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9591 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9592 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9593 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009594 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9595 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009596 SDValue BasePtr = St->getBasePtr();
9597
9598 // Perform one or more big stores into memory.
9599 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9600 for (unsigned I = 0; I < E; I++) {
9601 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9602 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009603 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009604 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9605 St->getPointerInfo(), St->isVolatile(),
9606 St->isNonTemporal(), St->getAlignment());
9607 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9608 Increment);
9609 Chains.push_back(Ch);
9610 }
9611 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9612 }
9613
9614 if (!ISD::isNormalStore(St))
9615 return SDValue();
9616
9617 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9618 // ARM stores of arguments in the same cache line.
9619 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9620 StVal.getNode()->hasOneUse()) {
9621 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009622 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009623 SDLoc DL(St);
9624 SDValue BasePtr = St->getBasePtr();
9625 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9626 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9627 BasePtr, St->getPointerInfo(), St->isVolatile(),
9628 St->isNonTemporal(), St->getAlignment());
9629
9630 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009631 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009632 return DAG.getStore(NewST1.getValue(0), DL,
9633 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9634 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9635 St->isNonTemporal(),
9636 std::min(4U, St->getAlignment() / 2));
9637 }
9638
9639 if (StVal.getValueType() == MVT::i64 &&
9640 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9641
9642 // Bitcast an i64 store extracted from a vector to f64.
9643 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9644 SelectionDAG &DAG = DCI.DAG;
9645 SDLoc dl(StVal);
9646 SDValue IntVec = StVal.getOperand(0);
9647 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9648 IntVec.getValueType().getVectorNumElements());
9649 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9650 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9651 Vec, StVal.getOperand(1));
9652 dl = SDLoc(N);
9653 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9654 // Make the DAGCombiner fold the bitcasts.
9655 DCI.AddToWorklist(Vec.getNode());
9656 DCI.AddToWorklist(ExtElt.getNode());
9657 DCI.AddToWorklist(V.getNode());
9658 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9659 St->getPointerInfo(), St->isVolatile(),
9660 St->isNonTemporal(), St->getAlignment(),
9661 St->getAAInfo());
9662 }
9663
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009664 // If this is a legal vector store, try to combine it into a VST1_UPD.
9665 if (ISD::isNormalStore(N) && VT.isVector() &&
9666 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9667 return CombineBaseUpdate(N, DCI);
9668
Ahmed Bougacha23167462014-12-09 21:26:53 +00009669 return SDValue();
9670}
9671
Eric Christopher1b8b94192011-06-29 21:10:36 +00009672// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009673// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9674static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9675{
Chad Rosier6b610b32011-06-28 17:26:57 +00009676 integerPart cN;
9677 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009678 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9679 I != E; I++) {
9680 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9681 if (!C)
9682 return false;
9683
Eric Christopher1b8b94192011-06-29 21:10:36 +00009684 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009685 APFloat APF = C->getValueAPF();
9686 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9687 != APFloat::opOK || !isExact)
9688 return false;
9689
9690 c0 = (I == 0) ? cN : c0;
9691 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9692 return false;
9693 }
9694 C = c0;
9695 return true;
9696}
9697
9698/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9699/// can replace combinations of VMUL and VCVT (floating-point to integer)
9700/// when the VMUL has a constant operand that is a power of 2.
9701///
9702/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9703/// vmul.f32 d16, d17, d16
9704/// vcvt.s32.f32 d16, d16
9705/// becomes:
9706/// vcvt.s32.f32 d16, d16, #3
9707static SDValue PerformVCVTCombine(SDNode *N,
9708 TargetLowering::DAGCombinerInfo &DCI,
9709 const ARMSubtarget *Subtarget) {
9710 SelectionDAG &DAG = DCI.DAG;
9711 SDValue Op = N->getOperand(0);
9712
9713 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9714 Op.getOpcode() != ISD::FMUL)
9715 return SDValue();
9716
9717 uint64_t C;
9718 SDValue N0 = Op->getOperand(0);
9719 SDValue ConstVec = Op->getOperand(1);
9720 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9721
Eric Christopher1b8b94192011-06-29 21:10:36 +00009722 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009723 !isConstVecPow2(ConstVec, isSigned, C))
9724 return SDValue();
9725
Tim Northover7cbc2152013-06-28 15:29:25 +00009726 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9727 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009728 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9729 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9730 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009731 // These instructions only exist converting from f32 to i32. We can handle
9732 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009733 // be lossy. We also can't handle more then 4 lanes, since these intructions
9734 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009735 return SDValue();
9736 }
9737
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009738 SDLoc dl(N);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009739 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9740 Intrinsic::arm_neon_vcvtfp2fxu;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009741 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Tim Northover7cbc2152013-06-28 15:29:25 +00009742 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009743 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9744 N0,
9745 DAG.getConstant(Log2_64(C), dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009746
9747 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009748 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009749
9750 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009751}
9752
9753/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9754/// can replace combinations of VCVT (integer to floating-point) and VDIV
9755/// when the VDIV has a constant operand that is a power of 2.
9756///
9757/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9758/// vcvt.f32.s32 d16, d16
9759/// vdiv.f32 d16, d17, d16
9760/// becomes:
9761/// vcvt.f32.s32 d16, d16, #3
9762static SDValue PerformVDIVCombine(SDNode *N,
9763 TargetLowering::DAGCombinerInfo &DCI,
9764 const ARMSubtarget *Subtarget) {
9765 SelectionDAG &DAG = DCI.DAG;
9766 SDValue Op = N->getOperand(0);
9767 unsigned OpOpcode = Op.getNode()->getOpcode();
9768
9769 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9770 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9771 return SDValue();
9772
9773 uint64_t C;
9774 SDValue ConstVec = N->getOperand(1);
9775 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9776
9777 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9778 !isConstVecPow2(ConstVec, isSigned, C))
9779 return SDValue();
9780
Tim Northover7cbc2152013-06-28 15:29:25 +00009781 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9782 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9783 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9784 // These instructions only exist converting from i32 to f32. We can handle
9785 // smaller integers by generating an extra extend, but larger ones would
9786 // be lossy.
9787 return SDValue();
9788 }
9789
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009790 SDLoc dl(N);
Tim Northover7cbc2152013-06-28 15:29:25 +00009791 SDValue ConvInput = Op.getOperand(0);
9792 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9793 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9794 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009795 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009796 ConvInput);
9797
Eric Christopher1b8b94192011-06-29 21:10:36 +00009798 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009799 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009800 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009801 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009802 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9803 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009804}
9805
9806/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009807/// operand of a vector shift operation, where all the elements of the
9808/// build_vector must have the same constant integer value.
9809static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9810 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009811 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009812 Op = Op.getOperand(0);
9813 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9814 APInt SplatBits, SplatUndef;
9815 unsigned SplatBitSize;
9816 bool HasAnyUndefs;
9817 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9818 HasAnyUndefs, ElementBits) ||
9819 SplatBitSize > ElementBits)
9820 return false;
9821 Cnt = SplatBits.getSExtValue();
9822 return true;
9823}
9824
9825/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9826/// operand of a vector shift left operation. That value must be in the range:
9827/// 0 <= Value < ElementBits for a left shift; or
9828/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009829static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009830 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009831 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009832 if (! getVShiftImm(Op, ElementBits, Cnt))
9833 return false;
9834 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9835}
9836
9837/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9838/// operand of a vector shift right operation. For a shift opcode, the value
9839/// is positive, but for an intrinsic the value count must be negative. The
9840/// absolute value must be in the range:
9841/// 1 <= |Value| <= ElementBits for a right shift; or
9842/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009843static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009844 int64_t &Cnt) {
9845 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009846 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009847 if (! getVShiftImm(Op, ElementBits, Cnt))
9848 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009849 if (!isIntrinsic)
9850 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9851 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009852 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009853 return true;
9854 }
9855 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +00009856}
9857
9858/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9859static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9860 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9861 switch (IntNo) {
9862 default:
9863 // Don't do anything for most intrinsics.
9864 break;
9865
James Molloya6702e22015-07-17 17:10:55 +00009866 case Intrinsic::arm_neon_vabds:
9867 if (!N->getValueType(0).isInteger())
9868 return SDValue();
9869 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0),
9870 N->getOperand(1), N->getOperand(2));
9871 case Intrinsic::arm_neon_vabdu:
9872 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0),
9873 N->getOperand(1), N->getOperand(2));
9874
Bob Wilson2e076c42009-06-22 23:27:02 +00009875 // Vector shifts: check for immediate versions and lower them.
9876 // Note: This is done during DAG combining instead of DAG legalizing because
9877 // the build_vectors for 64-bit vector element shift counts are generally
9878 // not legal, and it is hard to see their values after they get legalized to
9879 // loads from a constant pool.
9880 case Intrinsic::arm_neon_vshifts:
9881 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009882 case Intrinsic::arm_neon_vrshifts:
9883 case Intrinsic::arm_neon_vrshiftu:
9884 case Intrinsic::arm_neon_vrshiftn:
9885 case Intrinsic::arm_neon_vqshifts:
9886 case Intrinsic::arm_neon_vqshiftu:
9887 case Intrinsic::arm_neon_vqshiftsu:
9888 case Intrinsic::arm_neon_vqshiftns:
9889 case Intrinsic::arm_neon_vqshiftnu:
9890 case Intrinsic::arm_neon_vqshiftnsu:
9891 case Intrinsic::arm_neon_vqrshiftns:
9892 case Intrinsic::arm_neon_vqrshiftnu:
9893 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009894 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009895 int64_t Cnt;
9896 unsigned VShiftOpc = 0;
9897
9898 switch (IntNo) {
9899 case Intrinsic::arm_neon_vshifts:
9900 case Intrinsic::arm_neon_vshiftu:
9901 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9902 VShiftOpc = ARMISD::VSHL;
9903 break;
9904 }
9905 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9906 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9907 ARMISD::VSHRs : ARMISD::VSHRu);
9908 break;
9909 }
9910 return SDValue();
9911
Bob Wilson2e076c42009-06-22 23:27:02 +00009912 case Intrinsic::arm_neon_vrshifts:
9913 case Intrinsic::arm_neon_vrshiftu:
9914 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9915 break;
9916 return SDValue();
9917
9918 case Intrinsic::arm_neon_vqshifts:
9919 case Intrinsic::arm_neon_vqshiftu:
9920 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9921 break;
9922 return SDValue();
9923
9924 case Intrinsic::arm_neon_vqshiftsu:
9925 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9926 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009927 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009928
Bob Wilson2e076c42009-06-22 23:27:02 +00009929 case Intrinsic::arm_neon_vrshiftn:
9930 case Intrinsic::arm_neon_vqshiftns:
9931 case Intrinsic::arm_neon_vqshiftnu:
9932 case Intrinsic::arm_neon_vqshiftnsu:
9933 case Intrinsic::arm_neon_vqrshiftns:
9934 case Intrinsic::arm_neon_vqrshiftnu:
9935 case Intrinsic::arm_neon_vqrshiftnsu:
9936 // Narrowing shifts require an immediate right shift.
9937 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9938 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009939 llvm_unreachable("invalid shift count for narrowing vector shift "
9940 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009941
9942 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009943 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009944 }
9945
9946 switch (IntNo) {
9947 case Intrinsic::arm_neon_vshifts:
9948 case Intrinsic::arm_neon_vshiftu:
9949 // Opcode already set above.
9950 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009951 case Intrinsic::arm_neon_vrshifts:
9952 VShiftOpc = ARMISD::VRSHRs; break;
9953 case Intrinsic::arm_neon_vrshiftu:
9954 VShiftOpc = ARMISD::VRSHRu; break;
9955 case Intrinsic::arm_neon_vrshiftn:
9956 VShiftOpc = ARMISD::VRSHRN; break;
9957 case Intrinsic::arm_neon_vqshifts:
9958 VShiftOpc = ARMISD::VQSHLs; break;
9959 case Intrinsic::arm_neon_vqshiftu:
9960 VShiftOpc = ARMISD::VQSHLu; break;
9961 case Intrinsic::arm_neon_vqshiftsu:
9962 VShiftOpc = ARMISD::VQSHLsu; break;
9963 case Intrinsic::arm_neon_vqshiftns:
9964 VShiftOpc = ARMISD::VQSHRNs; break;
9965 case Intrinsic::arm_neon_vqshiftnu:
9966 VShiftOpc = ARMISD::VQSHRNu; break;
9967 case Intrinsic::arm_neon_vqshiftnsu:
9968 VShiftOpc = ARMISD::VQSHRNsu; break;
9969 case Intrinsic::arm_neon_vqrshiftns:
9970 VShiftOpc = ARMISD::VQRSHRNs; break;
9971 case Intrinsic::arm_neon_vqrshiftnu:
9972 VShiftOpc = ARMISD::VQRSHRNu; break;
9973 case Intrinsic::arm_neon_vqrshiftnsu:
9974 VShiftOpc = ARMISD::VQRSHRNsu; break;
9975 }
9976
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009977 SDLoc dl(N);
9978 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
9979 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009980 }
9981
9982 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009983 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009984 int64_t Cnt;
9985 unsigned VShiftOpc = 0;
9986
9987 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9988 VShiftOpc = ARMISD::VSLI;
9989 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9990 VShiftOpc = ARMISD::VSRI;
9991 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009992 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009993 }
9994
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009995 SDLoc dl(N);
9996 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009997 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009998 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009999 }
10000
10001 case Intrinsic::arm_neon_vqrshifts:
10002 case Intrinsic::arm_neon_vqrshiftu:
10003 // No immediate versions of these to check for.
10004 break;
10005 }
10006
10007 return SDValue();
10008}
10009
10010/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10011/// lowers them. As with the vector shift intrinsics, this is done during DAG
10012/// combining instead of DAG legalizing because the build_vectors for 64-bit
10013/// vector element shift counts are generally not legal, and it is hard to see
10014/// their values after they get legalized to loads from a constant pool.
10015static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10016 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010017 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010018 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10019 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10020 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10021 SDValue N1 = N->getOperand(1);
10022 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10023 SDValue N0 = N->getOperand(0);
10024 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10025 DAG.MaskedValueIsZero(N0.getOperand(0),
10026 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010027 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010028 }
10029 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010030
10031 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010032 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10033 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010034 return SDValue();
10035
10036 assert(ST->hasNEON() && "unexpected vector shift");
10037 int64_t Cnt;
10038
10039 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010040 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010041
10042 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010043 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10044 SDLoc dl(N);
10045 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10046 DAG.getConstant(Cnt, dl, MVT::i32));
10047 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010048 break;
10049
10050 case ISD::SRA:
10051 case ISD::SRL:
10052 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10053 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10054 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010055 SDLoc dl(N);
10056 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10057 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010058 }
10059 }
10060 return SDValue();
10061}
10062
10063/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10064/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10065static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10066 const ARMSubtarget *ST) {
10067 SDValue N0 = N->getOperand(0);
10068
10069 // Check for sign- and zero-extensions of vector extract operations of 8-
10070 // and 16-bit vector elements. NEON supports these directly. They are
10071 // handled during DAG combining because type legalization will promote them
10072 // to 32-bit types and it is messy to recognize the operations after that.
10073 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10074 SDValue Vec = N0.getOperand(0);
10075 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010076 EVT VT = N->getValueType(0);
10077 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010078 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10079
Owen Anderson9f944592009-08-11 20:47:22 +000010080 if (VT == MVT::i32 &&
10081 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010082 TLI.isTypeLegal(Vec.getValueType()) &&
10083 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010084
10085 unsigned Opc = 0;
10086 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010087 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010088 case ISD::SIGN_EXTEND:
10089 Opc = ARMISD::VGETLANEs;
10090 break;
10091 case ISD::ZERO_EXTEND:
10092 case ISD::ANY_EXTEND:
10093 Opc = ARMISD::VGETLANEu;
10094 break;
10095 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010096 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010097 }
10098 }
10099
10100 return SDValue();
10101}
10102
Evan Chengf863e3f2011-07-13 00:42:17 +000010103/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10104SDValue
10105ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10106 SDValue Cmp = N->getOperand(4);
10107 if (Cmp.getOpcode() != ARMISD::CMPZ)
10108 // Only looking at EQ and NE cases.
10109 return SDValue();
10110
10111 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010112 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010113 SDValue LHS = Cmp.getOperand(0);
10114 SDValue RHS = Cmp.getOperand(1);
10115 SDValue FalseVal = N->getOperand(0);
10116 SDValue TrueVal = N->getOperand(1);
10117 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010118 ARMCC::CondCodes CC =
10119 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010120
10121 // Simplify
10122 // mov r1, r0
10123 // cmp r1, x
10124 // mov r0, y
10125 // moveq r0, x
10126 // to
10127 // cmp r0, x
10128 // movne r0, y
10129 //
10130 // mov r1, r0
10131 // cmp r1, x
10132 // mov r0, x
10133 // movne r0, y
10134 // to
10135 // cmp r0, x
10136 // movne r0, y
10137 /// FIXME: Turn this into a target neutral optimization?
10138 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010139 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010140 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10141 N->getOperand(3), Cmp);
10142 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10143 SDValue ARMcc;
10144 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10145 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10146 N->getOperand(3), NewCmp);
10147 }
10148
10149 if (Res.getNode()) {
10150 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010151 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010152 // Capture demanded bits information that would be otherwise lost.
10153 if (KnownZero == 0xfffffffe)
10154 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10155 DAG.getValueType(MVT::i1));
10156 else if (KnownZero == 0xffffff00)
10157 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10158 DAG.getValueType(MVT::i8));
10159 else if (KnownZero == 0xffff0000)
10160 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10161 DAG.getValueType(MVT::i16));
10162 }
10163
10164 return Res;
10165}
10166
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010167SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010168 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010169 switch (N->getOpcode()) {
10170 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010171 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010172 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010173 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010174 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010175 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010176 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10177 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010178 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010179 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010180 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010181 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010182 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010183 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010184 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010185 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010186 case ISD::FP_TO_SINT:
10187 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
10188 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010189 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010190 case ISD::SHL:
10191 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010192 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010193 case ISD::SIGN_EXTEND:
10194 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010195 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010196 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010197 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010198 case ARMISD::VLD2DUP:
10199 case ARMISD::VLD3DUP:
10200 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010201 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010202 case ARMISD::BUILD_VECTOR:
10203 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010204 case ISD::INTRINSIC_VOID:
10205 case ISD::INTRINSIC_W_CHAIN:
10206 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10207 case Intrinsic::arm_neon_vld1:
10208 case Intrinsic::arm_neon_vld2:
10209 case Intrinsic::arm_neon_vld3:
10210 case Intrinsic::arm_neon_vld4:
10211 case Intrinsic::arm_neon_vld2lane:
10212 case Intrinsic::arm_neon_vld3lane:
10213 case Intrinsic::arm_neon_vld4lane:
10214 case Intrinsic::arm_neon_vst1:
10215 case Intrinsic::arm_neon_vst2:
10216 case Intrinsic::arm_neon_vst3:
10217 case Intrinsic::arm_neon_vst4:
10218 case Intrinsic::arm_neon_vst2lane:
10219 case Intrinsic::arm_neon_vst3lane:
10220 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010221 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010222 default: break;
10223 }
10224 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010225 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010226 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010227}
10228
Evan Chengd42641c2011-02-02 01:06:55 +000010229bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10230 EVT VT) const {
10231 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10232}
10233
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010234bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10235 unsigned,
10236 unsigned,
10237 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010238 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010239 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010240
10241 switch (VT.getSimpleVT().SimpleTy) {
10242 default:
10243 return false;
10244 case MVT::i8:
10245 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010246 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010247 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010248 if (AllowsUnaligned) {
10249 if (Fast)
10250 *Fast = Subtarget->hasV7Ops();
10251 return true;
10252 }
10253 return false;
10254 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010255 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010256 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010257 // For any little-endian targets with neon, we can support unaligned ld/st
10258 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010259 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010260 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010261 if (Fast)
10262 *Fast = true;
10263 return true;
10264 }
10265 return false;
10266 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010267 }
10268}
10269
Lang Hames9929c422011-11-02 22:52:45 +000010270static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10271 unsigned AlignCheck) {
10272 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10273 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10274}
10275
10276EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10277 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010278 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010279 bool MemcpyStrSrc,
10280 MachineFunction &MF) const {
10281 const Function *F = MF.getFunction();
10282
10283 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010284 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10285 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010286 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010287 if (Size >= 16 &&
10288 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010289 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010290 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010291 } else if (Size >= 8 &&
10292 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010293 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10294 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010295 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010296 }
10297 }
10298
Lang Hamesb85fcd02011-11-08 18:56:23 +000010299 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010300 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010301 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010302 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010303 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010304
Lang Hames9929c422011-11-02 22:52:45 +000010305 // Let the target-independent logic figure it out.
10306 return MVT::Other;
10307}
10308
Evan Cheng9ec512d2012-12-06 19:13:27 +000010309bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10310 if (Val.getOpcode() != ISD::LOAD)
10311 return false;
10312
10313 EVT VT1 = Val.getValueType();
10314 if (!VT1.isSimple() || !VT1.isInteger() ||
10315 !VT2.isSimple() || !VT2.isInteger())
10316 return false;
10317
10318 switch (VT1.getSimpleVT().SimpleTy) {
10319 default: break;
10320 case MVT::i1:
10321 case MVT::i8:
10322 case MVT::i16:
10323 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10324 return true;
10325 }
10326
10327 return false;
10328}
10329
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010330bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10331 EVT VT = ExtVal.getValueType();
10332
10333 if (!isTypeLegal(VT))
10334 return false;
10335
10336 // Don't create a loadext if we can fold the extension into a wide/long
10337 // instruction.
10338 // If there's more than one user instruction, the loadext is desirable no
10339 // matter what. There can be two uses by the same instruction.
10340 if (ExtVal->use_empty() ||
10341 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10342 return true;
10343
10344 SDNode *U = *ExtVal->use_begin();
10345 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10346 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10347 return false;
10348
10349 return true;
10350}
10351
Tim Northovercc2e9032013-08-06 13:58:03 +000010352bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10353 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10354 return false;
10355
10356 if (!isTypeLegal(EVT::getEVT(Ty1)))
10357 return false;
10358
10359 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10360
10361 // Assuming the caller doesn't have a zeroext or signext return parameter,
10362 // truncation all the way down to i1 is valid.
10363 return true;
10364}
10365
10366
Evan Chengdc49a8d2009-08-14 20:09:37 +000010367static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10368 if (V < 0)
10369 return false;
10370
10371 unsigned Scale = 1;
10372 switch (VT.getSimpleVT().SimpleTy) {
10373 default: return false;
10374 case MVT::i1:
10375 case MVT::i8:
10376 // Scale == 1;
10377 break;
10378 case MVT::i16:
10379 // Scale == 2;
10380 Scale = 2;
10381 break;
10382 case MVT::i32:
10383 // Scale == 4;
10384 Scale = 4;
10385 break;
10386 }
10387
10388 if ((V & (Scale - 1)) != 0)
10389 return false;
10390 V /= Scale;
10391 return V == (V & ((1LL << 5) - 1));
10392}
10393
10394static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10395 const ARMSubtarget *Subtarget) {
10396 bool isNeg = false;
10397 if (V < 0) {
10398 isNeg = true;
10399 V = - V;
10400 }
10401
10402 switch (VT.getSimpleVT().SimpleTy) {
10403 default: return false;
10404 case MVT::i1:
10405 case MVT::i8:
10406 case MVT::i16:
10407 case MVT::i32:
10408 // + imm12 or - imm8
10409 if (isNeg)
10410 return V == (V & ((1LL << 8) - 1));
10411 return V == (V & ((1LL << 12) - 1));
10412 case MVT::f32:
10413 case MVT::f64:
10414 // Same as ARM mode. FIXME: NEON?
10415 if (!Subtarget->hasVFP2())
10416 return false;
10417 if ((V & 3) != 0)
10418 return false;
10419 V >>= 2;
10420 return V == (V & ((1LL << 8) - 1));
10421 }
10422}
10423
Evan Cheng2150b922007-03-12 23:30:29 +000010424/// isLegalAddressImmediate - Return true if the integer value can be used
10425/// as the offset of the target addressing mode for load / store of the
10426/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010427static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010428 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010429 if (V == 0)
10430 return true;
10431
Evan Chengce5dfb62009-03-09 19:15:00 +000010432 if (!VT.isSimple())
10433 return false;
10434
Evan Chengdc49a8d2009-08-14 20:09:37 +000010435 if (Subtarget->isThumb1Only())
10436 return isLegalT1AddressImmediate(V, VT);
10437 else if (Subtarget->isThumb2())
10438 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010439
Evan Chengdc49a8d2009-08-14 20:09:37 +000010440 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010441 if (V < 0)
10442 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010443 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010444 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010445 case MVT::i1:
10446 case MVT::i8:
10447 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010448 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010449 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010450 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010451 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010452 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010453 case MVT::f32:
10454 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010455 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010456 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010457 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010458 return false;
10459 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010460 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010461 }
Evan Cheng10043e22007-01-19 07:51:42 +000010462}
10463
Evan Chengdc49a8d2009-08-14 20:09:37 +000010464bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10465 EVT VT) const {
10466 int Scale = AM.Scale;
10467 if (Scale < 0)
10468 return false;
10469
10470 switch (VT.getSimpleVT().SimpleTy) {
10471 default: return false;
10472 case MVT::i1:
10473 case MVT::i8:
10474 case MVT::i16:
10475 case MVT::i32:
10476 if (Scale == 1)
10477 return true;
10478 // r + r << imm
10479 Scale = Scale & ~1;
10480 return Scale == 2 || Scale == 4 || Scale == 8;
10481 case MVT::i64:
10482 // r + r
10483 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10484 return true;
10485 return false;
10486 case MVT::isVoid:
10487 // Note, we allow "void" uses (basically, uses that aren't loads or
10488 // stores), because arm allows folding a scale into many arithmetic
10489 // operations. This should be made more precise and revisited later.
10490
10491 // Allow r << imm, but the imm has to be a multiple of two.
10492 if (Scale & 1) return false;
10493 return isPowerOf2_32(Scale);
10494 }
10495}
10496
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010497/// isLegalAddressingMode - Return true if the addressing mode represented
10498/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010499bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10500 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010501 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010502 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010503 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010504 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010505
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010506 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010507 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010508 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010509
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010510 switch (AM.Scale) {
10511 case 0: // no scale reg, must be "r+i" or "r", or "i".
10512 break;
10513 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010514 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010515 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010516 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010517 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010518 // ARM doesn't support any R+R*scale+imm addr modes.
10519 if (AM.BaseOffs)
10520 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010521
Bob Wilson866c1742009-04-08 17:55:28 +000010522 if (!VT.isSimple())
10523 return false;
10524
Evan Chengdc49a8d2009-08-14 20:09:37 +000010525 if (Subtarget->isThumb2())
10526 return isLegalT2ScaledAddressingMode(AM, VT);
10527
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010528 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010529 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010530 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010531 case MVT::i1:
10532 case MVT::i8:
10533 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010534 if (Scale < 0) Scale = -Scale;
10535 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010536 return true;
10537 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010538 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010539 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010540 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010541 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010542 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010543 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010544 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010545
Owen Anderson9f944592009-08-11 20:47:22 +000010546 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010547 // Note, we allow "void" uses (basically, uses that aren't loads or
10548 // stores), because arm allows folding a scale into many arithmetic
10549 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010550
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010551 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010552 if (Scale & 1) return false;
10553 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010554 }
Evan Cheng2150b922007-03-12 23:30:29 +000010555 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010556 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010557}
10558
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010559/// isLegalICmpImmediate - Return true if the specified immediate is legal
10560/// icmp immediate, that is the target has icmp instructions which can compare
10561/// a register against the immediate without having to materialize the
10562/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010563bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010564 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010565 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010566 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010567 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010568 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010569 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010570 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010571}
10572
Andrew Tricka22cdb72012-07-18 18:34:27 +000010573/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10574/// *or sub* immediate, that is the target has add or sub instructions which can
10575/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010576/// immediate into a register.
10577bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010578 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010579 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010580 if (!Subtarget->isThumb())
10581 return ARM_AM::getSOImmVal(AbsImm) != -1;
10582 if (Subtarget->isThumb2())
10583 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10584 // Thumb1 only has 8-bit unsigned immediate.
10585 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010586}
10587
Owen Anderson53aa7a92009-08-10 22:56:29 +000010588static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010589 bool isSEXTLoad, SDValue &Base,
10590 SDValue &Offset, bool &isInc,
10591 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010592 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10593 return false;
10594
Owen Anderson9f944592009-08-11 20:47:22 +000010595 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010596 // AddressingMode 3
10597 Base = Ptr->getOperand(0);
10598 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010599 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010600 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010601 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010602 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010603 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010604 return true;
10605 }
10606 }
10607 isInc = (Ptr->getOpcode() == ISD::ADD);
10608 Offset = Ptr->getOperand(1);
10609 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010610 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010611 // AddressingMode 2
10612 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010613 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010614 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010615 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010616 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010617 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010618 Base = Ptr->getOperand(0);
10619 return true;
10620 }
10621 }
10622
10623 if (Ptr->getOpcode() == ISD::ADD) {
10624 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010625 ARM_AM::ShiftOpc ShOpcVal=
10626 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010627 if (ShOpcVal != ARM_AM::no_shift) {
10628 Base = Ptr->getOperand(1);
10629 Offset = Ptr->getOperand(0);
10630 } else {
10631 Base = Ptr->getOperand(0);
10632 Offset = Ptr->getOperand(1);
10633 }
10634 return true;
10635 }
10636
10637 isInc = (Ptr->getOpcode() == ISD::ADD);
10638 Base = Ptr->getOperand(0);
10639 Offset = Ptr->getOperand(1);
10640 return true;
10641 }
10642
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010643 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010644 return false;
10645}
10646
Owen Anderson53aa7a92009-08-10 22:56:29 +000010647static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010648 bool isSEXTLoad, SDValue &Base,
10649 SDValue &Offset, bool &isInc,
10650 SelectionDAG &DAG) {
10651 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10652 return false;
10653
10654 Base = Ptr->getOperand(0);
10655 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10656 int RHSC = (int)RHS->getZExtValue();
10657 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10658 assert(Ptr->getOpcode() == ISD::ADD);
10659 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010660 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010661 return true;
10662 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10663 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010664 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010665 return true;
10666 }
10667 }
10668
10669 return false;
10670}
10671
Evan Cheng10043e22007-01-19 07:51:42 +000010672/// getPreIndexedAddressParts - returns true by value, base pointer and
10673/// offset pointer and addressing mode by reference if the node's address
10674/// can be legally represented as pre-indexed load / store address.
10675bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010676ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10677 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010678 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010679 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010680 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010681 return false;
10682
Owen Anderson53aa7a92009-08-10 22:56:29 +000010683 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010684 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010685 bool isSEXTLoad = false;
10686 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10687 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010688 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010689 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10690 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10691 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010692 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010693 } else
10694 return false;
10695
10696 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010697 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010698 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010699 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10700 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010701 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010702 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010703 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010704 if (!isLegal)
10705 return false;
10706
10707 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10708 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010709}
10710
10711/// getPostIndexedAddressParts - returns true by value, base pointer and
10712/// offset pointer and addressing mode by reference if this node can be
10713/// combined with a load / store to form a post-indexed load / store.
10714bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010715 SDValue &Base,
10716 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010717 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010718 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010719 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010720 return false;
10721
Owen Anderson53aa7a92009-08-10 22:56:29 +000010722 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010723 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010724 bool isSEXTLoad = false;
10725 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010726 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010727 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010728 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10729 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010730 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010731 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010732 } else
10733 return false;
10734
10735 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010736 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010737 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010738 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010739 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010740 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010741 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10742 isInc, DAG);
10743 if (!isLegal)
10744 return false;
10745
Evan Chengf19384d2010-05-18 21:31:17 +000010746 if (Ptr != Base) {
10747 // Swap base ptr and offset to catch more post-index load / store when
10748 // it's legal. In Thumb2 mode, offset must be an immediate.
10749 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10750 !Subtarget->isThumb2())
10751 std::swap(Base, Offset);
10752
10753 // Post-indexed load / store update the base pointer.
10754 if (Ptr != Base)
10755 return false;
10756 }
10757
Evan Cheng84c6cda2009-07-02 07:28:31 +000010758 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10759 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010760}
10761
Jay Foada0653a32014-05-14 21:14:37 +000010762void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10763 APInt &KnownZero,
10764 APInt &KnownOne,
10765 const SelectionDAG &DAG,
10766 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010767 unsigned BitWidth = KnownOne.getBitWidth();
10768 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010769 switch (Op.getOpcode()) {
10770 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010771 case ARMISD::ADDC:
10772 case ARMISD::ADDE:
10773 case ARMISD::SUBC:
10774 case ARMISD::SUBE:
10775 // These nodes' second result is a boolean
10776 if (Op.getResNo() == 0)
10777 break;
10778 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10779 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010780 case ARMISD::CMOV: {
10781 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010782 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010783 if (KnownZero == 0 && KnownOne == 0) return;
10784
Dan Gohmanf990faf2008-02-13 00:35:47 +000010785 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010786 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010787 KnownZero &= KnownZeroRHS;
10788 KnownOne &= KnownOneRHS;
10789 return;
10790 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010791 case ISD::INTRINSIC_W_CHAIN: {
10792 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10793 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10794 switch (IntID) {
10795 default: return;
10796 case Intrinsic::arm_ldaex:
10797 case Intrinsic::arm_ldrex: {
10798 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10799 unsigned MemBits = VT.getScalarType().getSizeInBits();
10800 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10801 return;
10802 }
10803 }
10804 }
Evan Cheng10043e22007-01-19 07:51:42 +000010805 }
10806}
10807
10808//===----------------------------------------------------------------------===//
10809// ARM Inline Assembly Support
10810//===----------------------------------------------------------------------===//
10811
Evan Cheng078b0b02011-01-08 01:24:27 +000010812bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10813 // Looking for "rev" which is V6+.
10814 if (!Subtarget->hasV6Ops())
10815 return false;
10816
10817 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10818 std::string AsmStr = IA->getAsmString();
10819 SmallVector<StringRef, 4> AsmPieces;
10820 SplitString(AsmStr, AsmPieces, ";\n");
10821
10822 switch (AsmPieces.size()) {
10823 default: return false;
10824 case 1:
10825 AsmStr = AsmPieces[0];
10826 AsmPieces.clear();
10827 SplitString(AsmStr, AsmPieces, " \t,");
10828
10829 // rev $0, $1
10830 if (AsmPieces.size() == 3 &&
10831 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10832 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010833 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010834 if (Ty && Ty->getBitWidth() == 32)
10835 return IntrinsicLowering::LowerToByteSwap(CI);
10836 }
10837 break;
10838 }
10839
10840 return false;
10841}
10842
Evan Cheng10043e22007-01-19 07:51:42 +000010843/// getConstraintType - Given a constraint letter, return the type of
10844/// constraint it is for this target.
10845ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010846ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000010847 if (Constraint.size() == 1) {
10848 switch (Constraint[0]) {
10849 default: break;
10850 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010851 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010852 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010853 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010854 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010855 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010856 // An address with a single base register. Due to the way we
10857 // currently handle addresses it is the same as an 'r' memory constraint.
10858 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010859 }
Eric Christophere256cd02011-06-21 22:10:57 +000010860 } else if (Constraint.size() == 2) {
10861 switch (Constraint[0]) {
10862 default: break;
10863 // All 'U+' constraints are addresses.
10864 case 'U': return C_Memory;
10865 }
Evan Cheng10043e22007-01-19 07:51:42 +000010866 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010867 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010868}
10869
John Thompsone8360b72010-10-29 17:29:13 +000010870/// Examine constraint type and operand type and determine a weight value.
10871/// This object must already have been set up with the operand type
10872/// and the current alternative constraint selected.
10873TargetLowering::ConstraintWeight
10874ARMTargetLowering::getSingleConstraintMatchWeight(
10875 AsmOperandInfo &info, const char *constraint) const {
10876 ConstraintWeight weight = CW_Invalid;
10877 Value *CallOperandVal = info.CallOperandVal;
10878 // If we don't have a value, we can't do a match,
10879 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010880 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010881 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010882 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010883 // Look at the constraint type.
10884 switch (*constraint) {
10885 default:
10886 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10887 break;
10888 case 'l':
10889 if (type->isIntegerTy()) {
10890 if (Subtarget->isThumb())
10891 weight = CW_SpecificReg;
10892 else
10893 weight = CW_Register;
10894 }
10895 break;
10896 case 'w':
10897 if (type->isFloatingPointTy())
10898 weight = CW_Register;
10899 break;
10900 }
10901 return weight;
10902}
10903
Eric Christophercf2007c2011-06-30 23:50:52 +000010904typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010905RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
10906 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010907 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010908 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010909 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010910 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010911 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010912 return RCPair(0U, &ARM::tGPRRegClass);
10913 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010914 case 'h': // High regs or no regs.
10915 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010916 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010917 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010918 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010919 if (Subtarget->isThumb1Only())
10920 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010921 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010922 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010923 if (VT == MVT::Other)
10924 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010925 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010926 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010927 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010928 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010929 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010930 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010931 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010932 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010933 if (VT == MVT::Other)
10934 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010935 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010936 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010937 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010938 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010939 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010940 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010941 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010942 case 't':
10943 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010944 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010945 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010946 }
10947 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010948 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010949 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010950
Eric Christopher11e4df72015-02-26 22:38:43 +000010951 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010952}
10953
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010954/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10955/// vector. If it is invalid, don't add anything to Ops.
10956void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010957 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010958 std::vector<SDValue>&Ops,
10959 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010960 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010961
Eric Christopherde9399b2011-06-02 23:16:42 +000010962 // Currently only support length 1 constraints.
10963 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010964
Eric Christopherde9399b2011-06-02 23:16:42 +000010965 char ConstraintLetter = Constraint[0];
10966 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010967 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010968 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010969 case 'I': case 'J': case 'K': case 'L':
10970 case 'M': case 'N': case 'O':
10971 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10972 if (!C)
10973 return;
10974
10975 int64_t CVal64 = C->getSExtValue();
10976 int CVal = (int) CVal64;
10977 // None of these constraints allow values larger than 32 bits. Check
10978 // that the value fits in an int.
10979 if (CVal != CVal64)
10980 return;
10981
Eric Christopherde9399b2011-06-02 23:16:42 +000010982 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010983 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010984 // Constant suitable for movw, must be between 0 and
10985 // 65535.
10986 if (Subtarget->hasV6T2Ops())
10987 if (CVal >= 0 && CVal <= 65535)
10988 break;
10989 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010990 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010991 if (Subtarget->isThumb1Only()) {
10992 // This must be a constant between 0 and 255, for ADD
10993 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010994 if (CVal >= 0 && CVal <= 255)
10995 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010996 } else if (Subtarget->isThumb2()) {
10997 // A constant that can be used as an immediate value in a
10998 // data-processing instruction.
10999 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11000 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011001 } else {
11002 // A constant that can be used as an immediate value in a
11003 // data-processing instruction.
11004 if (ARM_AM::getSOImmVal(CVal) != -1)
11005 break;
11006 }
11007 return;
11008
11009 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000011010 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011011 // This must be a constant between -255 and -1, for negated ADD
11012 // immediates. This can be used in GCC with an "n" modifier that
11013 // prints the negated value, for use with SUB instructions. It is
11014 // not useful otherwise but is implemented for compatibility.
11015 if (CVal >= -255 && CVal <= -1)
11016 break;
11017 } else {
11018 // This must be a constant between -4095 and 4095. It is not clear
11019 // what this constraint is intended for. Implemented for
11020 // compatibility with GCC.
11021 if (CVal >= -4095 && CVal <= 4095)
11022 break;
11023 }
11024 return;
11025
11026 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011027 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011028 // A 32-bit value where only one byte has a nonzero value. Exclude
11029 // zero to match GCC. This constraint is used by GCC internally for
11030 // constants that can be loaded with a move/shift combination.
11031 // It is not useful otherwise but is implemented for compatibility.
11032 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11033 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011034 } else if (Subtarget->isThumb2()) {
11035 // A constant whose bitwise inverse can be used as an immediate
11036 // value in a data-processing instruction. This can be used in GCC
11037 // with a "B" modifier that prints the inverted value, for use with
11038 // BIC and MVN instructions. It is not useful otherwise but is
11039 // implemented for compatibility.
11040 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11041 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011042 } else {
11043 // A constant whose bitwise inverse can be used as an immediate
11044 // value in a data-processing instruction. This can be used in GCC
11045 // with a "B" modifier that prints the inverted value, for use with
11046 // BIC and MVN instructions. It is not useful otherwise but is
11047 // implemented for compatibility.
11048 if (ARM_AM::getSOImmVal(~CVal) != -1)
11049 break;
11050 }
11051 return;
11052
11053 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011054 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011055 // This must be a constant between -7 and 7,
11056 // for 3-operand ADD/SUB immediate instructions.
11057 if (CVal >= -7 && CVal < 7)
11058 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011059 } else if (Subtarget->isThumb2()) {
11060 // A constant whose negation can be used as an immediate value in a
11061 // data-processing instruction. This can be used in GCC with an "n"
11062 // modifier that prints the negated value, for use with SUB
11063 // instructions. It is not useful otherwise but is implemented for
11064 // compatibility.
11065 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11066 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011067 } else {
11068 // A constant whose negation can be used as an immediate value in a
11069 // data-processing instruction. This can be used in GCC with an "n"
11070 // modifier that prints the negated value, for use with SUB
11071 // instructions. It is not useful otherwise but is implemented for
11072 // compatibility.
11073 if (ARM_AM::getSOImmVal(-CVal) != -1)
11074 break;
11075 }
11076 return;
11077
11078 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000011079 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011080 // This must be a multiple of 4 between 0 and 1020, for
11081 // ADD sp + immediate.
11082 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11083 break;
11084 } else {
11085 // A power of two or a constant between 0 and 32. This is used in
11086 // GCC for the shift amount on shifted register operands, but it is
11087 // useful in general for any shift amounts.
11088 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11089 break;
11090 }
11091 return;
11092
11093 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011094 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011095 // This must be a constant between 0 and 31, for shift amounts.
11096 if (CVal >= 0 && CVal <= 31)
11097 break;
11098 }
11099 return;
11100
11101 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011102 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011103 // This must be a multiple of 4 between -508 and 508, for
11104 // ADD/SUB sp = sp + immediate.
11105 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11106 break;
11107 }
11108 return;
11109 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011110 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011111 break;
11112 }
11113
11114 if (Result.getNode()) {
11115 Ops.push_back(Result);
11116 return;
11117 }
Dale Johannesence97d552010-06-25 21:55:36 +000011118 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011119}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011120
Scott Douglassd2974a62015-08-24 09:17:11 +000011121static RTLIB::Libcall getDivRemLibcall(
11122 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011123 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11124 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011125 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011126 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11127 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011128 RTLIB::Libcall LC;
11129 switch (SVT) {
11130 default: llvm_unreachable("Unexpected request for libcall!");
11131 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11132 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11133 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11134 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11135 }
11136 return LC;
11137}
11138
11139static TargetLowering::ArgListTy getDivRemArgList(
11140 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011141 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11142 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011143 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011144 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11145 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011146 TargetLowering::ArgListTy Args;
11147 TargetLowering::ArgListEntry Entry;
11148 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11149 EVT ArgVT = N->getOperand(i).getValueType();
11150 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11151 Entry.Node = N->getOperand(i);
11152 Entry.Ty = ArgTy;
11153 Entry.isSExt = isSigned;
11154 Entry.isZExt = !isSigned;
11155 Args.push_back(Entry);
11156 }
11157 return Args;
11158}
11159
Renato Golin87610692013-07-16 09:32:17 +000011160SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011161 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) &&
11162 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011163 unsigned Opcode = Op->getOpcode();
11164 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011165 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011166 bool isSigned = (Opcode == ISD::SDIVREM);
11167 EVT VT = Op->getValueType(0);
11168 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11169
Scott Douglassd2974a62015-08-24 09:17:11 +000011170 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11171 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011172 SDValue InChain = DAG.getEntryNode();
11173
Scott Douglassd2974a62015-08-24 09:17:11 +000011174 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11175 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011176
11177 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011178 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011179
Reid Kleckner343c3952014-11-20 23:51:47 +000011180 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011181
11182 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011183 TargetLowering::CallLoweringInfo CLI(DAG);
11184 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011185 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011186 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011187
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011188 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011189 return CallInfo.first;
11190}
11191
Scott Douglassbdef6042015-08-24 09:17:18 +000011192// Lowers REM using divmod helpers
11193// see RTABI section 4.2/4.3
11194SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11195 // Build return types (div and rem)
11196 std::vector<Type*> RetTyParams;
11197 Type *RetTyElement;
11198
11199 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11200 default: llvm_unreachable("Unexpected request for libcall!");
11201 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11202 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11203 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11204 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11205 }
11206
11207 RetTyParams.push_back(RetTyElement);
11208 RetTyParams.push_back(RetTyElement);
11209 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11210 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11211
11212 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11213 SimpleTy);
11214 SDValue InChain = DAG.getEntryNode();
11215 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11216 bool isSigned = N->getOpcode() == ISD::SREM;
11217 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11218 getPointerTy(DAG.getDataLayout()));
11219
11220 // Lower call
11221 CallLoweringInfo CLI(DAG);
11222 CLI.setChain(InChain)
11223 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11224 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11225 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11226
11227 // Return second (rem) result operand (first contains div)
11228 SDNode *ResNode = CallResult.first.getNode();
11229 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11230 return ResNode->getOperand(1);
11231}
11232
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011233SDValue
11234ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11235 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11236 SDLoc DL(Op);
11237
11238 // Get the inputs.
11239 SDValue Chain = Op.getOperand(0);
11240 SDValue Size = Op.getOperand(1);
11241
11242 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011243 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011244
11245 SDValue Flag;
11246 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11247 Flag = Chain.getValue(1);
11248
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011249 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011250 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11251
11252 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11253 Chain = NewSP.getValue(1);
11254
11255 SDValue Ops[2] = { NewSP, Chain };
11256 return DAG.getMergeValues(Ops, DL);
11257}
11258
Oliver Stannard51b1d462014-08-21 12:50:31 +000011259SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11260 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11261 "Unexpected type for custom-lowering FP_EXTEND");
11262
11263 RTLIB::Libcall LC;
11264 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11265
11266 SDValue SrcVal = Op.getOperand(0);
11267 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11268 /*isSigned*/ false, SDLoc(Op)).first;
11269}
11270
11271SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11272 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11273 Subtarget->isFPOnlySP() &&
11274 "Unexpected type for custom-lowering FP_ROUND");
11275
11276 RTLIB::Libcall LC;
11277 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11278
11279 SDValue SrcVal = Op.getOperand(0);
11280 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11281 /*isSigned*/ false, SDLoc(Op)).first;
11282}
11283
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011284bool
11285ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11286 // The ARM target isn't yet aware of offsets.
11287 return false;
11288}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011289
Jim Grosbach11013ed2010-07-16 23:05:05 +000011290bool ARM::isBitFieldInvertedMask(unsigned v) {
11291 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011292 return false;
11293
Jim Grosbach11013ed2010-07-16 23:05:05 +000011294 // there can be 1's on either or both "outsides", all the "inside"
11295 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011296 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011297}
11298
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011299/// isFPImmLegal - Returns true if the target can instruction select the
11300/// specified FP immediate natively. If false, the legalizer will
11301/// materialize the FP immediate as a load from a constant pool.
11302bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11303 if (!Subtarget->hasVFP3())
11304 return false;
11305 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011306 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011307 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011308 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011309 return false;
11310}
Bob Wilson5549d492010-09-21 17:56:22 +000011311
Wesley Peck527da1b2010-11-23 03:31:01 +000011312/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011313/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11314/// specified in the intrinsic calls.
11315bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11316 const CallInst &I,
11317 unsigned Intrinsic) const {
11318 switch (Intrinsic) {
11319 case Intrinsic::arm_neon_vld1:
11320 case Intrinsic::arm_neon_vld2:
11321 case Intrinsic::arm_neon_vld3:
11322 case Intrinsic::arm_neon_vld4:
11323 case Intrinsic::arm_neon_vld2lane:
11324 case Intrinsic::arm_neon_vld3lane:
11325 case Intrinsic::arm_neon_vld4lane: {
11326 Info.opc = ISD::INTRINSIC_W_CHAIN;
11327 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011328 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11329 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011330 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11331 Info.ptrVal = I.getArgOperand(0);
11332 Info.offset = 0;
11333 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11334 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11335 Info.vol = false; // volatile loads with NEON intrinsics not supported
11336 Info.readMem = true;
11337 Info.writeMem = false;
11338 return true;
11339 }
11340 case Intrinsic::arm_neon_vst1:
11341 case Intrinsic::arm_neon_vst2:
11342 case Intrinsic::arm_neon_vst3:
11343 case Intrinsic::arm_neon_vst4:
11344 case Intrinsic::arm_neon_vst2lane:
11345 case Intrinsic::arm_neon_vst3lane:
11346 case Intrinsic::arm_neon_vst4lane: {
11347 Info.opc = ISD::INTRINSIC_VOID;
11348 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011349 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011350 unsigned NumElts = 0;
11351 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011352 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011353 if (!ArgTy->isVectorTy())
11354 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011355 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011356 }
11357 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11358 Info.ptrVal = I.getArgOperand(0);
11359 Info.offset = 0;
11360 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11361 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11362 Info.vol = false; // volatile stores with NEON intrinsics not supported
11363 Info.readMem = false;
11364 Info.writeMem = true;
11365 return true;
11366 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011367 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011368 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011369 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011370 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11371 Info.opc = ISD::INTRINSIC_W_CHAIN;
11372 Info.memVT = MVT::getVT(PtrTy->getElementType());
11373 Info.ptrVal = I.getArgOperand(0);
11374 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011375 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011376 Info.vol = true;
11377 Info.readMem = true;
11378 Info.writeMem = false;
11379 return true;
11380 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011381 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011382 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011383 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011384 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11385 Info.opc = ISD::INTRINSIC_W_CHAIN;
11386 Info.memVT = MVT::getVT(PtrTy->getElementType());
11387 Info.ptrVal = I.getArgOperand(1);
11388 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011389 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011390 Info.vol = true;
11391 Info.readMem = false;
11392 Info.writeMem = true;
11393 return true;
11394 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011395 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011396 case Intrinsic::arm_strexd: {
11397 Info.opc = ISD::INTRINSIC_W_CHAIN;
11398 Info.memVT = MVT::i64;
11399 Info.ptrVal = I.getArgOperand(2);
11400 Info.offset = 0;
11401 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011402 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011403 Info.readMem = false;
11404 Info.writeMem = true;
11405 return true;
11406 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011407 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011408 case Intrinsic::arm_ldrexd: {
11409 Info.opc = ISD::INTRINSIC_W_CHAIN;
11410 Info.memVT = MVT::i64;
11411 Info.ptrVal = I.getArgOperand(0);
11412 Info.offset = 0;
11413 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011414 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011415 Info.readMem = true;
11416 Info.writeMem = false;
11417 return true;
11418 }
Bob Wilson5549d492010-09-21 17:56:22 +000011419 default:
11420 break;
11421 }
11422
11423 return false;
11424}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011425
11426/// \brief Returns true if it is beneficial to convert a load of a constant
11427/// to just the constant itself.
11428bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11429 Type *Ty) const {
11430 assert(Ty->isIntegerTy());
11431
11432 unsigned Bits = Ty->getPrimitiveSizeInBits();
11433 if (Bits == 0 || Bits > 32)
11434 return false;
11435 return true;
11436}
Tim Northover037f26f22014-04-17 18:22:47 +000011437
Robin Morisset5349e8e2014-09-18 18:56:04 +000011438Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11439 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011440 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011441
11442 // First, if the target has no DMB, see what fallback we can use.
11443 if (!Subtarget->hasDataBarrier()) {
11444 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11445 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11446 // here.
11447 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11448 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11449 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11450 Builder.getInt32(0), Builder.getInt32(7),
11451 Builder.getInt32(10), Builder.getInt32(5)};
11452 return Builder.CreateCall(MCR, args);
11453 } else {
11454 // Instead of using barriers, atomic accesses on these subtargets use
11455 // libcalls.
11456 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11457 }
11458 } else {
11459 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11460 // Only a full system barrier exists in the M-class architectures.
11461 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11462 Constant *CDomain = Builder.getInt32(Domain);
11463 return Builder.CreateCall(DMB, CDomain);
11464 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011465}
11466
11467// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011468Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011469 AtomicOrdering Ord, bool IsStore,
11470 bool IsLoad) const {
11471 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011472 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011473
11474 switch (Ord) {
11475 case NotAtomic:
11476 case Unordered:
11477 llvm_unreachable("Invalid fence: unordered/non-atomic");
11478 case Monotonic:
11479 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011480 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011481 case SequentiallyConsistent:
11482 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011483 return nullptr; // Nothing to do
11484 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011485 case Release:
11486 case AcquireRelease:
11487 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011488 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011489 // FIXME: add a comment with a link to documentation justifying this.
11490 else
Robin Morissetdedef332014-09-23 20:31:14 +000011491 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011492 }
Robin Morissetdedef332014-09-23 20:31:14 +000011493 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011494}
11495
Robin Morissetdedef332014-09-23 20:31:14 +000011496Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011497 AtomicOrdering Ord, bool IsStore,
11498 bool IsLoad) const {
11499 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011500 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011501
11502 switch (Ord) {
11503 case NotAtomic:
11504 case Unordered:
11505 llvm_unreachable("Invalid fence: unordered/not-atomic");
11506 case Monotonic:
11507 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011508 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011509 case Acquire:
11510 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011511 case SequentiallyConsistent:
11512 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011513 }
Robin Morissetdedef332014-09-23 20:31:14 +000011514 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011515}
11516
Robin Morisseted3d48f2014-09-03 21:29:59 +000011517// Loads and stores less than 64-bits are already atomic; ones above that
11518// are doomed anyway, so defer to the default libcall and blame the OS when
11519// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11520// anything for those.
11521bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11522 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11523 return (Size == 64) && !Subtarget->isMClass();
11524}
Tim Northover037f26f22014-04-17 18:22:47 +000011525
Robin Morisseted3d48f2014-09-03 21:29:59 +000011526// Loads and stores less than 64-bits are already atomic; ones above that
11527// are doomed anyway, so defer to the default libcall and blame the OS when
11528// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11529// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011530// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11531// guarantee, see DDI0406C ARM architecture reference manual,
11532// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000011533TargetLowering::AtomicExpansionKind
11534ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011535 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Ahmed Bougacha52468672015-09-11 17:08:28 +000011536 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLSC
11537 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000011538}
11539
11540// For the real atomic operations, we have ldrex/strex up to 32 bits,
11541// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000011542TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000011543ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011544 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011545 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000011546 ? AtomicExpansionKind::LLSC
11547 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011548}
11549
Ahmed Bougacha52468672015-09-11 17:08:28 +000011550bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
11551 AtomicCmpXchgInst *AI) const {
11552 return true;
11553}
11554
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011555// This has so far only been implemented for MachO.
11556bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011557 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011558}
11559
Quentin Colombetc32615d2014-10-31 17:52:53 +000011560bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11561 unsigned &Cost) const {
11562 // If we do not have NEON, vector types are not natively supported.
11563 if (!Subtarget->hasNEON())
11564 return false;
11565
11566 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000011567 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000011568 // better to leave at float as we have more freedom in the addressing mode for
11569 // those.
11570 if (VectorTy->isFPOrFPVectorTy())
11571 return false;
11572
11573 // If the index is unknown at compile time, this is very expensive to lower
11574 // and it is not possible to combine the store with the extract.
11575 if (!isa<ConstantInt>(Idx))
11576 return false;
11577
11578 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11579 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11580 // We can do a store + vector extract on any vector that fits perfectly in a D
11581 // or Q register.
11582 if (BitWidth == 64 || BitWidth == 128) {
11583 Cost = 0;
11584 return true;
11585 }
11586 return false;
11587}
11588
Tim Northover037f26f22014-04-17 18:22:47 +000011589Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11590 AtomicOrdering Ord) const {
11591 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11592 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011593 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011594
11595 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11596 // intrinsic must return {i32, i32} and we have to recombine them into a
11597 // single i64 here.
11598 if (ValTy->getPrimitiveSizeInBits() == 64) {
11599 Intrinsic::ID Int =
11600 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11601 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11602
11603 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11604 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11605
11606 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11607 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011608 if (!Subtarget->isLittle())
11609 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011610 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11611 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11612 return Builder.CreateOr(
11613 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11614 }
11615
11616 Type *Tys[] = { Addr->getType() };
11617 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11618 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11619
11620 return Builder.CreateTruncOrBitCast(
11621 Builder.CreateCall(Ldrex, Addr),
11622 cast<PointerType>(Addr->getType())->getElementType());
11623}
11624
11625Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11626 Value *Addr,
11627 AtomicOrdering Ord) const {
11628 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011629 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011630
11631 // Since the intrinsics must have legal type, the i64 intrinsics take two
11632 // parameters: "i32, i32". We must marshal Val into the appropriate form
11633 // before the call.
11634 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11635 Intrinsic::ID Int =
11636 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11637 Function *Strex = Intrinsic::getDeclaration(M, Int);
11638 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11639
11640 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11641 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011642 if (!Subtarget->isLittle())
11643 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011644 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011645 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011646 }
11647
11648 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11649 Type *Tys[] = { Addr->getType() };
11650 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11651
David Blaikieff6409d2015-05-18 22:13:54 +000011652 return Builder.CreateCall(
11653 Strex, {Builder.CreateZExtOrBitCast(
11654 Val, Strex->getFunctionType()->getParamType(0)),
11655 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011656}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011657
Hao Liu2cd34bb2015-06-26 02:45:36 +000011658/// \brief Lower an interleaved load into a vldN intrinsic.
11659///
11660/// E.g. Lower an interleaved load (Factor = 2):
11661/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
11662/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
11663/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
11664///
11665/// Into:
11666/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
11667/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
11668/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
11669bool ARMTargetLowering::lowerInterleavedLoad(
11670 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
11671 ArrayRef<unsigned> Indices, unsigned Factor) const {
11672 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11673 "Invalid interleave factor");
11674 assert(!Shuffles.empty() && "Empty shufflevector input");
11675 assert(Shuffles.size() == Indices.size() &&
11676 "Unmatched number of shufflevectors and indices");
11677
11678 VectorType *VecTy = Shuffles[0]->getType();
11679 Type *EltTy = VecTy->getVectorElementType();
11680
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011681 const DataLayout &DL = LI->getModule()->getDataLayout();
11682 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
11683 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011684
11685 // Skip illegal vector types and vector types of i64/f64 element (vldN doesn't
11686 // support i64/f64 element).
11687 if ((VecSize != 64 && VecSize != 128) || EltIs64Bits)
11688 return false;
11689
11690 // A pointer vector can not be the return type of the ldN intrinsics. Need to
11691 // load integer vectors first and then convert to pointer vectors.
11692 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011693 VecTy =
11694 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000011695
11696 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
11697 Intrinsic::arm_neon_vld3,
11698 Intrinsic::arm_neon_vld4};
11699
11700 Function *VldnFunc =
11701 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], VecTy);
11702
11703 IRBuilder<> Builder(LI);
11704 SmallVector<Value *, 2> Ops;
11705
11706 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
11707 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
11708 Ops.push_back(Builder.getInt32(LI->getAlignment()));
11709
11710 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
11711
11712 // Replace uses of each shufflevector with the corresponding vector loaded
11713 // by ldN.
11714 for (unsigned i = 0; i < Shuffles.size(); i++) {
11715 ShuffleVectorInst *SV = Shuffles[i];
11716 unsigned Index = Indices[i];
11717
11718 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
11719
11720 // Convert the integer vector to pointer vector if the element is pointer.
11721 if (EltTy->isPointerTy())
11722 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
11723
11724 SV->replaceAllUsesWith(SubVec);
11725 }
11726
11727 return true;
11728}
11729
11730/// \brief Get a mask consisting of sequential integers starting from \p Start.
11731///
11732/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
11733static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
11734 unsigned NumElts) {
11735 SmallVector<Constant *, 16> Mask;
11736 for (unsigned i = 0; i < NumElts; i++)
11737 Mask.push_back(Builder.getInt32(Start + i));
11738
11739 return ConstantVector::get(Mask);
11740}
11741
11742/// \brief Lower an interleaved store into a vstN intrinsic.
11743///
11744/// E.g. Lower an interleaved store (Factor = 3):
11745/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
11746/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
11747/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
11748///
11749/// Into:
11750/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
11751/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
11752/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
11753/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
11754///
11755/// Note that the new shufflevectors will be removed and we'll only generate one
11756/// vst3 instruction in CodeGen.
11757bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
11758 ShuffleVectorInst *SVI,
11759 unsigned Factor) const {
11760 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11761 "Invalid interleave factor");
11762
11763 VectorType *VecTy = SVI->getType();
11764 assert(VecTy->getVectorNumElements() % Factor == 0 &&
11765 "Invalid interleaved store");
11766
11767 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
11768 Type *EltTy = VecTy->getVectorElementType();
11769 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
11770
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011771 const DataLayout &DL = SI->getModule()->getDataLayout();
11772 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
11773 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011774
11775 // Skip illegal sub vector types and vector types of i64/f64 element (vstN
11776 // doesn't support i64/f64 element).
11777 if ((SubVecSize != 64 && SubVecSize != 128) || EltIs64Bits)
11778 return false;
11779
11780 Value *Op0 = SVI->getOperand(0);
11781 Value *Op1 = SVI->getOperand(1);
11782 IRBuilder<> Builder(SI);
11783
11784 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
11785 // vectors to integer vectors.
11786 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011787 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011788
11789 // Convert to the corresponding integer vector.
11790 Type *IntVecTy =
11791 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
11792 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
11793 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
11794
11795 SubVecTy = VectorType::get(IntTy, NumSubElts);
11796 }
11797
11798 static Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
11799 Intrinsic::arm_neon_vst3,
11800 Intrinsic::arm_neon_vst4};
11801 Function *VstNFunc = Intrinsic::getDeclaration(
11802 SI->getModule(), StoreInts[Factor - 2], SubVecTy);
11803
11804 SmallVector<Value *, 6> Ops;
11805
11806 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
11807 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
11808
11809 // Split the shufflevector operands into sub vectors for the new vstN call.
11810 for (unsigned i = 0; i < Factor; i++)
11811 Ops.push_back(Builder.CreateShuffleVector(
11812 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
11813
11814 Ops.push_back(Builder.getInt32(SI->getAlignment()));
11815 Builder.CreateCall(VstNFunc, Ops);
11816 return true;
11817}
11818
Oliver Stannardc24f2172014-05-09 14:01:47 +000011819enum HABaseType {
11820 HA_UNKNOWN = 0,
11821 HA_FLOAT,
11822 HA_DOUBLE,
11823 HA_VECT64,
11824 HA_VECT128
11825};
11826
11827static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11828 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000011829 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011830 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11831 uint64_t SubMembers = 0;
11832 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11833 return false;
11834 Members += SubMembers;
11835 }
Craig Toppere3dcce92015-08-01 22:20:21 +000011836 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011837 uint64_t SubMembers = 0;
11838 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11839 return false;
11840 Members += SubMembers * AT->getNumElements();
11841 } else if (Ty->isFloatTy()) {
11842 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11843 return false;
11844 Members = 1;
11845 Base = HA_FLOAT;
11846 } else if (Ty->isDoubleTy()) {
11847 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11848 return false;
11849 Members = 1;
11850 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000011851 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011852 Members = 1;
11853 switch (Base) {
11854 case HA_FLOAT:
11855 case HA_DOUBLE:
11856 return false;
11857 case HA_VECT64:
11858 return VT->getBitWidth() == 64;
11859 case HA_VECT128:
11860 return VT->getBitWidth() == 128;
11861 case HA_UNKNOWN:
11862 switch (VT->getBitWidth()) {
11863 case 64:
11864 Base = HA_VECT64;
11865 return true;
11866 case 128:
11867 Base = HA_VECT128;
11868 return true;
11869 default:
11870 return false;
11871 }
11872 }
11873 }
11874
11875 return (Members > 0 && Members <= 4);
11876}
11877
Tim Northovere95c5b32015-02-24 17:22:34 +000011878/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11879/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11880/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011881bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11882 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011883 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11884 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011885 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011886
11887 HABaseType Base = HA_UNKNOWN;
11888 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011889 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11890 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11891
11892 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11893 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011894}