blob: 91edc12dc1166710234d782f21a9ffafb6e0bcb9 [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 },
371 };
372
373 for (const auto &LC : LibraryCalls) {
374 setLibcallName(LC.Op, LC.Name);
375 setLibcallCallingConv(LC.Op, LC.CC);
376 }
377 }
378
Bob Wilsonbc158992011-10-07 16:59:21 +0000379 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000380 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000381 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
382 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
383 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
384 }
385
Oliver Stannard11790b22014-08-11 09:12:32 +0000386 // The half <-> float conversion functions are always soft-float, but are
387 // needed for some targets which use a hard-float calling convention by
388 // default.
389 if (Subtarget->isAAPCS_ABI()) {
390 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
393 } else {
394 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
395 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
396 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
397 }
398
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000399 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
400 // a __gnu_ prefix (which is the default).
401 if (Subtarget->isTargetAEABI()) {
402 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
403 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
404 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
405 }
406
David Goodwin22c2fba2009-07-08 23:10:31 +0000407 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000408 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000409 else
Craig Topperc7242e02012-04-20 07:30:17 +0000410 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000411 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000412 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000413 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000414 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000415 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000416
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000417 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000418 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000419 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000420 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
421 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
422 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
423 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000424
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000425 setOperationAction(ISD::MULHS, VT, Expand);
426 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
427 setOperationAction(ISD::MULHU, VT, Expand);
428 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000429
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000430 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000431 }
432
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000433 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000434 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000435
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000436 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
437 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
438
Bob Wilson2e076c42009-06-22 23:27:02 +0000439 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000440 addDRTypeForNEON(MVT::v2f32);
441 addDRTypeForNEON(MVT::v8i8);
442 addDRTypeForNEON(MVT::v4i16);
443 addDRTypeForNEON(MVT::v2i32);
444 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000445
Owen Anderson9f944592009-08-11 20:47:22 +0000446 addQRTypeForNEON(MVT::v4f32);
447 addQRTypeForNEON(MVT::v2f64);
448 addQRTypeForNEON(MVT::v16i8);
449 addQRTypeForNEON(MVT::v8i16);
450 addQRTypeForNEON(MVT::v4i32);
451 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000452
Bob Wilson194a2512009-09-15 23:55:57 +0000453 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
454 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000455 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
456 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000457 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
458 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
459 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000460 // FIXME: Code duplication: FDIV and FREM are expanded always, see
461 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000462 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
463 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000464 // FIXME: Create unittest.
465 // In another words, find a way when "copysign" appears in DAG with vector
466 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000467 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000468 // FIXME: Code duplication: SETCC has custom operation action, see
469 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000470 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000471 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000472 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
473 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
474 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
475 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
476 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
477 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
478 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
479 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
480 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
481 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
482 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
483 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000484 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000485 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
486 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
487 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
488 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
489 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000490 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000491
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000492 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
493 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
494 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
495 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
496 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
497 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
498 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
499 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
500 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
501 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000502 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
503 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
504 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
505 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000506 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000507
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000508 // Mark v2f32 intrinsics.
509 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
510 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
511 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
512 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
513 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
514 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
515 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
516 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
517 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
518 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
519 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
520 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
521 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
522 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
523 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
524
Bob Wilson6cc46572009-09-16 00:32:15 +0000525 // Neon does not support some operations on v1i64 and v2i64 types.
526 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000527 // Custom handling for some quad-vector types to detect VMULL.
528 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
529 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
530 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000531 // Custom handling for some vector types to avoid expensive expansions
532 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
533 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
534 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
535 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000536 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
537 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000538 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000539 // a destination type that is wider than the source, and nor does
540 // it have a FP_TO_[SU]INT instruction with a narrower destination than
541 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000542 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
543 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000544 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
545 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000546
Eli Friedmane6385e62012-11-15 22:44:27 +0000547 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000548 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000549
Evan Chengb4eae132012-12-04 22:41:50 +0000550 // NEON does not have single instruction CTPOP for vectors with element
551 // types wider than 8-bits. However, custom lowering can leverage the
552 // v8i8/v16i8 vcnt instruction.
553 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
554 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
555 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
556 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
557
Logan Chien0a43abc2015-07-13 15:37:30 +0000558 // NEON does not have single instruction CTTZ for vectors.
559 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
560 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
561 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
562 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
563
564 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
565 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
566 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
567 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
568
569 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
570 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
571 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
572 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
573
574 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
575 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
576 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
577 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
578
Jim Grosbach5f215872013-02-27 21:31:12 +0000579 // NEON only has FMA instructions as of VFP4.
580 if (!Subtarget->hasVFP4()) {
581 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
582 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
583 }
584
Bob Wilson06fce872011-02-07 17:43:21 +0000585 setTargetDAGCombine(ISD::INTRINSIC_VOID);
586 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000587 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
588 setTargetDAGCombine(ISD::SHL);
589 setTargetDAGCombine(ISD::SRL);
590 setTargetDAGCombine(ISD::SRA);
591 setTargetDAGCombine(ISD::SIGN_EXTEND);
592 setTargetDAGCombine(ISD::ZERO_EXTEND);
593 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000594 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000595 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000596 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
597 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000598 setTargetDAGCombine(ISD::FP_TO_SINT);
599 setTargetDAGCombine(ISD::FP_TO_UINT);
600 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000601 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000602
James Molloy547d4c02012-02-20 09:24:05 +0000603 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000604 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
605 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000606 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000607 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
608 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
609 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000610 }
James Molloy547d4c02012-02-20 09:24:05 +0000611 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000612 }
613
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000614 // ARM and Thumb2 support UMLAL/SMLAL.
615 if (!Subtarget->isThumb1Only())
616 setTargetDAGCombine(ISD::ADDC);
617
Oliver Stannard51b1d462014-08-21 12:50:31 +0000618 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000619 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000620 // operations, f64 is legal for the few double-precision instructions which
621 // are present However, no double-precision operations other than moves,
622 // loads and stores are provided by the hardware.
623 setOperationAction(ISD::FADD, MVT::f64, Expand);
624 setOperationAction(ISD::FSUB, MVT::f64, Expand);
625 setOperationAction(ISD::FMUL, MVT::f64, Expand);
626 setOperationAction(ISD::FMA, MVT::f64, Expand);
627 setOperationAction(ISD::FDIV, MVT::f64, Expand);
628 setOperationAction(ISD::FREM, MVT::f64, Expand);
629 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
630 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
631 setOperationAction(ISD::FNEG, MVT::f64, Expand);
632 setOperationAction(ISD::FABS, MVT::f64, Expand);
633 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
634 setOperationAction(ISD::FSIN, MVT::f64, Expand);
635 setOperationAction(ISD::FCOS, MVT::f64, Expand);
636 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
637 setOperationAction(ISD::FPOW, MVT::f64, Expand);
638 setOperationAction(ISD::FLOG, MVT::f64, Expand);
639 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
640 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
641 setOperationAction(ISD::FEXP, MVT::f64, Expand);
642 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
643 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
644 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
645 setOperationAction(ISD::FRINT, MVT::f64, Expand);
646 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
647 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000648 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
649 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
650 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
651 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
652 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
653 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000654 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
655 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
656 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000657
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000658 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000659
Tim Northover4e80b582014-07-18 13:01:19 +0000660 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000661 for (MVT VT : MVT::fp_valuetypes()) {
662 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
663 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
664 }
Tim Northover4e80b582014-07-18 13:01:19 +0000665
666 // ... or truncating stores
667 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
668 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
669 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000670
Duncan Sands95d46ef2008-01-23 20:39:46 +0000671 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000672 for (MVT VT : MVT::integer_valuetypes())
673 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000674
Evan Cheng10043e22007-01-19 07:51:42 +0000675 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000676 if (!Subtarget->isThumb1Only()) {
677 for (unsigned im = (unsigned)ISD::PRE_INC;
678 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000679 setIndexedLoadAction(im, MVT::i1, Legal);
680 setIndexedLoadAction(im, MVT::i8, Legal);
681 setIndexedLoadAction(im, MVT::i16, Legal);
682 setIndexedLoadAction(im, MVT::i32, Legal);
683 setIndexedStoreAction(im, MVT::i1, Legal);
684 setIndexedStoreAction(im, MVT::i8, Legal);
685 setIndexedStoreAction(im, MVT::i16, Legal);
686 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000687 }
Evan Cheng10043e22007-01-19 07:51:42 +0000688 }
689
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000690 setOperationAction(ISD::SADDO, MVT::i32, Custom);
691 setOperationAction(ISD::UADDO, MVT::i32, Custom);
692 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
693 setOperationAction(ISD::USUBO, MVT::i32, Custom);
694
Evan Cheng10043e22007-01-19 07:51:42 +0000695 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000696 setOperationAction(ISD::MUL, MVT::i64, Expand);
697 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000698 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000699 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
700 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000701 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000702 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000703 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000704 setOperationAction(ISD::MULHS, MVT::i32, Expand);
705
Jim Grosbach5d994042009-10-31 19:38:01 +0000706 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000707 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000708 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000709 setOperationAction(ISD::SRL, MVT::i64, Custom);
710 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000711
Evan Chenge8916542011-08-30 01:34:54 +0000712 if (!Subtarget->isThumb1Only()) {
713 // FIXME: We should do this for Thumb1 as well.
714 setOperationAction(ISD::ADDC, MVT::i32, Custom);
715 setOperationAction(ISD::ADDE, MVT::i32, Custom);
716 setOperationAction(ISD::SUBC, MVT::i32, Custom);
717 setOperationAction(ISD::SUBE, MVT::i32, Custom);
718 }
719
Evan Cheng10043e22007-01-19 07:51:42 +0000720 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000721 setOperationAction(ISD::ROTL, MVT::i32, Expand);
722 for (MVT VT : MVT::vector_valuetypes()) {
723 setOperationAction(ISD::ROTL, VT, Expand);
724 setOperationAction(ISD::ROTR, VT, Expand);
725 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000726 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000727 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000728 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000729 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000730
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000731 // These just redirect to CTTZ and CTLZ on ARM.
732 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
733 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
734
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000735 // @llvm.readcyclecounter requires the Performance Monitors extension.
736 // Default to the 0 expansion on unsupported platforms.
737 // FIXME: Technically there are older ARM CPUs that have
738 // implementation-specific ways of obtaining this information.
739 if (Subtarget->hasPerfMon())
740 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000741
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000742 // Only ARMv6 has BSWAP.
743 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000744 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000745
Bob Wilsone8a549c2012-09-29 21:43:49 +0000746 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
747 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
748 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000749 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
750 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000751 }
Renato Golin87610692013-07-16 09:32:17 +0000752
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +0000753 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
754 setOperationAction(ISD::SDIV, MVT::i32, Custom);
755 setOperationAction(ISD::UDIV, MVT::i32, Custom);
756
757 setOperationAction(ISD::SDIV, MVT::i64, Custom);
758 setOperationAction(ISD::UDIV, MVT::i64, Custom);
759 }
760
Chad Rosierad7c9102014-08-23 18:29:43 +0000761 setOperationAction(ISD::SREM, MVT::i32, Expand);
762 setOperationAction(ISD::UREM, MVT::i32, Expand);
763 // Register based DivRem for AEABI (RTABI 4.2)
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000764 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000765 setOperationAction(ISD::SREM, MVT::i64, Custom);
766 setOperationAction(ISD::UREM, MVT::i64, Custom);
767
Chad Rosierad7c9102014-08-23 18:29:43 +0000768 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
769 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
770 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
771 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
772 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
773 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
774 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
775 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
776
777 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
778 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
779 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
780 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
781 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
782 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
783 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
784 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
785
786 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
787 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
788 } else {
Renato Golin87610692013-07-16 09:32:17 +0000789 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
790 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
791 }
Bob Wilson7117a912009-03-20 22:42:55 +0000792
Owen Anderson9f944592009-08-11 20:47:22 +0000793 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
794 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000795 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000796 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000797
Evan Cheng74d92c12011-04-08 21:37:21 +0000798 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000799
Evan Cheng10043e22007-01-19 07:51:42 +0000800 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000801 setOperationAction(ISD::VASTART, MVT::Other, Custom);
802 setOperationAction(ISD::VAARG, MVT::Other, Expand);
803 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
804 setOperationAction(ISD::VAEND, MVT::Other, Expand);
805 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
806 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000807
Tim Northoverd6a729b2014-01-06 14:28:05 +0000808 if (!Subtarget->isTargetMachO()) {
809 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000810 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000811 setExceptionPointerRegister(ARM::R0);
812 setExceptionSelectorRegister(ARM::R1);
813 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000814
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000815 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
816 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
817 else
818 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
819
Evan Cheng6e809de2010-08-11 06:22:01 +0000820 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000821 // the default expansion. If we are targeting a single threaded system,
822 // then set them all for expand so we can lower them later into their
823 // non-atomic form.
824 if (TM.Options.ThreadModel == ThreadModel::Single)
825 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
826 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000827 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
828 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000829 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000830
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000831 // On v8, we have particularly efficient implementations of atomic fences
832 // if they can be combined with nearby atomic loads and stores.
833 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000834 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000835 setInsertFencesForAtomic(true);
836 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000837 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000838 // If there's anything we can use as a barrier, go through custom lowering
839 // for ATOMIC_FENCE.
840 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
841 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
842
Jim Grosbach6860bb72010-06-18 22:35:32 +0000843 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000844 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000845 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000846 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000847 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000848 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000849 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000850 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000851 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000852 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000853 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000854 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000855 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000856 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
857 // Unordered/Monotonic case.
858 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
859 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000860 }
Evan Cheng10043e22007-01-19 07:51:42 +0000861
Evan Cheng21acf9f2010-11-04 05:19:35 +0000862 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000863
Eli Friedman8cfa7712010-06-26 04:36:50 +0000864 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
865 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000866 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
867 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000868 }
Owen Anderson9f944592009-08-11 20:47:22 +0000869 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000870
Eric Christopher824f42f2015-05-12 01:26:05 +0000871 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000872 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000873 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000874 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000875 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000876 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
877 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000878
879 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000880 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000881 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
882 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
883 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
884 if (Subtarget->isTargetDarwin())
John McCall7d84ece2011-05-29 19:50:32 +0000885 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000886
Owen Anderson9f944592009-08-11 20:47:22 +0000887 setOperationAction(ISD::SETCC, MVT::i32, Expand);
888 setOperationAction(ISD::SETCC, MVT::f32, Expand);
889 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000890 setOperationAction(ISD::SELECT, MVT::i32, Custom);
891 setOperationAction(ISD::SELECT, MVT::f32, Custom);
892 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000893 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
894 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
895 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000896
Owen Anderson9f944592009-08-11 20:47:22 +0000897 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
898 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
899 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
900 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
901 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000902
Dan Gohman482732a2007-10-11 23:21:31 +0000903 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000904 setOperationAction(ISD::FSIN, MVT::f64, Expand);
905 setOperationAction(ISD::FSIN, MVT::f32, Expand);
906 setOperationAction(ISD::FCOS, MVT::f32, Expand);
907 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000908 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
909 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000910 setOperationAction(ISD::FREM, MVT::f64, Expand);
911 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000912 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000913 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000914 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
915 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000916 }
Owen Anderson9f944592009-08-11 20:47:22 +0000917 setOperationAction(ISD::FPOW, MVT::f64, Expand);
918 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000919
Evan Chengd0007f32012-04-10 21:40:28 +0000920 if (!Subtarget->hasVFP4()) {
921 setOperationAction(ISD::FMA, MVT::f64, Expand);
922 setOperationAction(ISD::FMA, MVT::f32, Expand);
923 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000924
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000925 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000926 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000927 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
928 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000929 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
930 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
931 }
932
933 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000934 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000935 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
936 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000937 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000938 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000939
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000940 // Combine sin / cos into one node or libcall if possible.
941 if (Subtarget->hasSinCos()) {
942 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
943 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000944 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000945 // For iOS, we don't want to the normal expansion of a libcall to
946 // sincos. We want to issue a libcall to __sincos_stret.
947 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
948 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
949 }
950 }
Evan Cheng10043e22007-01-19 07:51:42 +0000951
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000952 // FP-ARMv8 implements a lot of rounding-like FP operations.
953 if (Subtarget->hasFPARMv8()) {
954 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
955 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
956 setOperationAction(ISD::FROUND, MVT::f32, Legal);
957 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
958 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
959 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000960 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
961 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000962 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
963 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
964 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
965 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
966
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000967 if (!Subtarget->isFPOnlySP()) {
968 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
969 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
970 setOperationAction(ISD::FROUND, MVT::f64, Legal);
971 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
972 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
973 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000974 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
975 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000976 }
977 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000978
James Molloy974838f2015-08-17 19:37:12 +0000979 if (Subtarget->hasNEON()) {
980 // vmin and vmax aren't available in a scalar form, so we use
981 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +0000982 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
983 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +0000984 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
985 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
986 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
987 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
988 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000989
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000990 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000991 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000992 setTargetDAGCombine(ISD::ADD);
993 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000994 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000995 setTargetDAGCombine(ISD::AND);
996 setTargetDAGCombine(ISD::OR);
997 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000998
Evan Chengf258a152012-02-23 02:58:19 +0000999 if (Subtarget->hasV6Ops())
1000 setTargetDAGCombine(ISD::SRL);
1001
Evan Cheng10043e22007-01-19 07:51:42 +00001002 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001003
Eric Christopher824f42f2015-05-12 01:26:05 +00001004 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001005 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001006 setSchedulingPreference(Sched::RegPressure);
1007 else
1008 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001009
Evan Cheng3ae2b792011-01-06 06:52:41 +00001010 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001011 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001012 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001013 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001014 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001015 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001016 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001017
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001018 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1019 // are at least 4 bytes aligned.
1020 setMinStackArgumentAlignment(4);
1021
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001022 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001023 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001024
Eli Friedman2518f832011-05-06 20:34:06 +00001025 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001026}
1027
Eric Christopher824f42f2015-05-12 01:26:05 +00001028bool ARMTargetLowering::useSoftFloat() const {
1029 return Subtarget->useSoftFloat();
1030}
1031
Andrew Trick43f25632011-01-19 02:35:27 +00001032// FIXME: It might make sense to define the representative register class as the
1033// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1034// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1035// SPR's representative would be DPR_VFP2. This should work well if register
1036// pressure tracking were modified such that a register use would increment the
1037// pressure of the register class's representative and all of it's super
1038// classes' representatives transitively. We have not implemented this because
1039// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001040// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001041// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001042std::pair<const TargetRegisterClass *, uint8_t>
1043ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1044 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001045 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001046 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001047 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001048 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001049 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001050 // Use DPR as representative register class for all floating point
1051 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1052 // the cost is 1 for both f32 and f64.
1053 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001054 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001055 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001056 // When NEON is used for SP, only half of the register file is available
1057 // because operations that define both SP and DP results will be constrained
1058 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1059 // coalescing by double-counting the SP regs. See the FIXME above.
1060 if (Subtarget->useNEONForSinglePrecisionFP())
1061 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001062 break;
1063 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1064 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001065 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001066 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001067 break;
1068 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001069 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001070 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001071 break;
1072 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001073 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001074 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001075 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001076 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001077 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001078}
1079
Evan Cheng10043e22007-01-19 07:51:42 +00001080const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001081 switch ((ARMISD::NodeType)Opcode) {
1082 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001083 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001084 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001085 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001086 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001087 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001088 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001089 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1090 case ARMISD::tCALL: return "ARMISD::tCALL";
1091 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1092 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001093 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001094 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001095 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001096 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1097 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001098 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001099 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001100 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1101 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001102 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001103 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001104
Evan Cheng10043e22007-01-19 07:51:42 +00001105 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001106
Jim Grosbach8546ec92010-01-18 19:58:49 +00001107 case ARMISD::RBIT: return "ARMISD::RBIT";
1108
Evan Cheng10043e22007-01-19 07:51:42 +00001109 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1110 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1111 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001112
Evan Chenge8916542011-08-30 01:34:54 +00001113 case ARMISD::ADDC: return "ARMISD::ADDC";
1114 case ARMISD::ADDE: return "ARMISD::ADDE";
1115 case ARMISD::SUBC: return "ARMISD::SUBC";
1116 case ARMISD::SUBE: return "ARMISD::SUBE";
1117
Bob Wilson22806742010-09-22 22:09:21 +00001118 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1119 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001120
Evan Chengec6d7c92009-10-28 06:55:03 +00001121 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001122 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1123 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001124
Dale Johannesend679ff72010-06-03 21:09:53 +00001125 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001126
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001127 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001128
Evan Chengb972e562009-08-07 00:34:42 +00001129 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1130
Bob Wilson7ed59712010-10-30 00:54:37 +00001131 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001132
Evan Cheng8740ee32010-11-03 06:34:55 +00001133 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1134
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001135 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001136 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001137
Bob Wilson2e076c42009-06-22 23:27:02 +00001138 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001139 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001140 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001141 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1142 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001143 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1144 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001145 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1146 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001147 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1148 case ARMISD::VTST: return "ARMISD::VTST";
1149
1150 case ARMISD::VSHL: return "ARMISD::VSHL";
1151 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1152 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001153 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1154 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1155 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1156 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1157 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1158 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1159 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1160 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1161 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1162 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1163 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1164 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001165 case ARMISD::VSLI: return "ARMISD::VSLI";
1166 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001167 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1168 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001169 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001170 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001171 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001172 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001173 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001174 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001175 case ARMISD::VREV64: return "ARMISD::VREV64";
1176 case ARMISD::VREV32: return "ARMISD::VREV32";
1177 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001178 case ARMISD::VZIP: return "ARMISD::VZIP";
1179 case ARMISD::VUZP: return "ARMISD::VUZP";
1180 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001181 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1182 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001183 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1184 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001185 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1186 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001187 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001188 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001189 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1190 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001191 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001192 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001193 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1194 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1195 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001196 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1197 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1198 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1199 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1200 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1201 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1202 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1203 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1204 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1205 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1206 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1207 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1208 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1209 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1210 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1211 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1212 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001213 }
Matthias Braund04893f2015-05-07 21:33:59 +00001214 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001215}
1216
Mehdi Amini44ede332015-07-09 02:09:04 +00001217EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1218 EVT VT) const {
1219 if (!VT.isVector())
1220 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001221 return VT.changeVectorElementTypeToInteger();
1222}
1223
Evan Cheng4cad68e2010-05-15 02:18:07 +00001224/// getRegClassFor - Return the register class that should be used for the
1225/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001226const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001227 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1228 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1229 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001230 if (Subtarget->hasNEON()) {
1231 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001232 return &ARM::QQPRRegClass;
1233 if (VT == MVT::v8i64)
1234 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001235 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001236 return TargetLowering::getRegClassFor(VT);
1237}
1238
John Brawn0dbcd652015-03-18 12:01:59 +00001239// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1240// source/dest is aligned and the copy size is large enough. We therefore want
1241// to align such objects passed to memory intrinsics.
1242bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1243 unsigned &PrefAlign) const {
1244 if (!isa<MemIntrinsic>(CI))
1245 return false;
1246 MinSize = 8;
1247 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1248 // cycle faster than 4-byte aligned LDM.
1249 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1250 return true;
1251}
1252
Eric Christopher84bdfd82010-07-21 22:26:11 +00001253// Create a fast isel object.
1254FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001255ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1256 const TargetLibraryInfo *libInfo) const {
1257 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001258}
1259
Evan Cheng4401f882010-05-20 23:26:43 +00001260Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001261 unsigned NumVals = N->getNumValues();
1262 if (!NumVals)
1263 return Sched::RegPressure;
1264
1265 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001266 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001267 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001268 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001269 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001270 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001271 }
Evan Chengbf914992010-05-28 23:25:23 +00001272
1273 if (!N->isMachineOpcode())
1274 return Sched::RegPressure;
1275
1276 // Load are scheduled for latency even if there instruction itinerary
1277 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001278 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001279 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001280
Evan Cheng6cc775f2011-06-28 19:10:37 +00001281 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001282 return Sched::RegPressure;
1283 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001284 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001285 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001286
Evan Cheng4401f882010-05-20 23:26:43 +00001287 return Sched::RegPressure;
1288}
1289
Evan Cheng10043e22007-01-19 07:51:42 +00001290//===----------------------------------------------------------------------===//
1291// Lowering Code
1292//===----------------------------------------------------------------------===//
1293
Evan Cheng10043e22007-01-19 07:51:42 +00001294/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1295static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1296 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001297 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001298 case ISD::SETNE: return ARMCC::NE;
1299 case ISD::SETEQ: return ARMCC::EQ;
1300 case ISD::SETGT: return ARMCC::GT;
1301 case ISD::SETGE: return ARMCC::GE;
1302 case ISD::SETLT: return ARMCC::LT;
1303 case ISD::SETLE: return ARMCC::LE;
1304 case ISD::SETUGT: return ARMCC::HI;
1305 case ISD::SETUGE: return ARMCC::HS;
1306 case ISD::SETULT: return ARMCC::LO;
1307 case ISD::SETULE: return ARMCC::LS;
1308 }
1309}
1310
Bob Wilsona2e83332009-09-09 23:14:54 +00001311/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1312static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001313 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001314 CondCode2 = ARMCC::AL;
1315 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001316 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001317 case ISD::SETEQ:
1318 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1319 case ISD::SETGT:
1320 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1321 case ISD::SETGE:
1322 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1323 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001324 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001325 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1326 case ISD::SETO: CondCode = ARMCC::VC; break;
1327 case ISD::SETUO: CondCode = ARMCC::VS; break;
1328 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1329 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1330 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1331 case ISD::SETLT:
1332 case ISD::SETULT: CondCode = ARMCC::LT; break;
1333 case ISD::SETLE:
1334 case ISD::SETULE: CondCode = ARMCC::LE; break;
1335 case ISD::SETNE:
1336 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1337 }
Evan Cheng10043e22007-01-19 07:51:42 +00001338}
1339
Bob Wilsona4c22902009-04-17 19:07:39 +00001340//===----------------------------------------------------------------------===//
1341// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001342//===----------------------------------------------------------------------===//
1343
1344#include "ARMGenCallingConv.inc"
1345
Oliver Stannardc24f2172014-05-09 14:01:47 +00001346/// getEffectiveCallingConv - Get the effective calling convention, taking into
1347/// account presence of floating point hardware and calling convention
1348/// limitations, such as support for variadic functions.
1349CallingConv::ID
1350ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1351 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001352 switch (CC) {
1353 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001354 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001355 case CallingConv::ARM_AAPCS:
1356 case CallingConv::ARM_APCS:
1357 case CallingConv::GHC:
1358 return CC;
1359 case CallingConv::ARM_AAPCS_VFP:
1360 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1361 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001362 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001363 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001364 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001365 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1366 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001367 return CallingConv::ARM_AAPCS_VFP;
1368 else
1369 return CallingConv::ARM_AAPCS;
1370 case CallingConv::Fast:
1371 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001372 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001373 return CallingConv::Fast;
1374 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001375 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001376 return CallingConv::ARM_AAPCS_VFP;
1377 else
1378 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001379 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001380}
1381
1382/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1383/// CallingConvention.
1384CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1385 bool Return,
1386 bool isVarArg) const {
1387 switch (getEffectiveCallingConv(CC, isVarArg)) {
1388 default:
1389 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001390 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001391 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001392 case CallingConv::ARM_AAPCS:
1393 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1394 case CallingConv::ARM_AAPCS_VFP:
1395 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1396 case CallingConv::Fast:
1397 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001398 case CallingConv::GHC:
1399 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001400 }
1401}
1402
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001403/// LowerCallResult - Lower the result values of a call into the
1404/// appropriate copies out of appropriate physical registers.
1405SDValue
1406ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001407 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001408 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001409 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001410 SmallVectorImpl<SDValue> &InVals,
1411 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001412
Bob Wilsona4c22902009-04-17 19:07:39 +00001413 // Assign locations to each value returned by this call.
1414 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001415 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1416 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001417 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001418 CCAssignFnForNode(CallConv, /* Return*/ true,
1419 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001420
1421 // Copy all of the result registers out of their specified physreg.
1422 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1423 CCValAssign VA = RVLocs[i];
1424
Stephen Linb8bd2322013-04-20 05:14:40 +00001425 // Pass 'this' value directly from the argument to return value, to avoid
1426 // reg unit interference
1427 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001428 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1429 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001430 InVals.push_back(ThisVal);
1431 continue;
1432 }
1433
Bob Wilson0041bd32009-04-25 00:33:20 +00001434 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001435 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001436 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001437 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001438 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001439 Chain = Lo.getValue(1);
1440 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001441 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001442 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001443 InFlag);
1444 Chain = Hi.getValue(1);
1445 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001446 if (!Subtarget->isLittle())
1447 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001448 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001449
Owen Anderson9f944592009-08-11 20:47:22 +00001450 if (VA.getLocVT() == MVT::v2f64) {
1451 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1452 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001453 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001454
1455 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001456 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001457 Chain = Lo.getValue(1);
1458 InFlag = Lo.getValue(2);
1459 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001460 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001461 Chain = Hi.getValue(1);
1462 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001463 if (!Subtarget->isLittle())
1464 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001465 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001466 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001467 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001468 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001469 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001470 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1471 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001472 Chain = Val.getValue(1);
1473 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001474 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001475
1476 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001477 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001478 case CCValAssign::Full: break;
1479 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001480 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001481 break;
1482 }
1483
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001484 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001485 }
1486
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001487 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001488}
1489
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001490/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001491SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001492ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1493 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001494 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001495 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001496 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001497 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001498 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001499 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1500 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001501 return DAG.getStore(
1502 Chain, dl, Arg, PtrOff,
1503 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1504 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001505}
1506
Andrew Trickef9de2a2013-05-25 02:42:55 +00001507void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001508 SDValue Chain, SDValue &Arg,
1509 RegsToPassVector &RegsToPass,
1510 CCValAssign &VA, CCValAssign &NextVA,
1511 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001512 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001513 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001514
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001515 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001516 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001517 unsigned id = Subtarget->isLittle() ? 0 : 1;
1518 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001519
1520 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001521 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001522 else {
1523 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001524 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001525 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1526 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001527
Christian Pirkerb5728192014-05-08 14:06:24 +00001528 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001529 dl, DAG, NextVA,
1530 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001531 }
1532}
1533
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001534/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001535/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1536/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001537SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001538ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001539 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001540 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001541 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001542 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1543 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1544 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001545 SDValue Chain = CLI.Chain;
1546 SDValue Callee = CLI.Callee;
1547 bool &isTailCall = CLI.IsTailCall;
1548 CallingConv::ID CallConv = CLI.CallConv;
1549 bool doesNotRet = CLI.DoesNotReturn;
1550 bool isVarArg = CLI.IsVarArg;
1551
Dale Johannesend679ff72010-06-03 21:09:53 +00001552 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001553 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1554 bool isThisReturn = false;
1555 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001556 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001557
Bob Wilson8decdc42011-10-07 17:17:49 +00001558 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001559 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001560 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001561
Dale Johannesend679ff72010-06-03 21:09:53 +00001562 if (isTailCall) {
1563 // Check if it's really possible to do a tail call.
1564 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001565 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001566 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001567 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1568 report_fatal_error("failed to perform tail call elimination on a call "
1569 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001570 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1571 // detected sibcalls.
1572 if (isTailCall) {
1573 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001574 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001575 }
1576 }
Evan Cheng10043e22007-01-19 07:51:42 +00001577
Bob Wilsona4c22902009-04-17 19:07:39 +00001578 // Analyze operands of the call, assigning locations to each operand.
1579 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001580 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1581 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001582 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001583 CCAssignFnForNode(CallConv, /* Return*/ false,
1584 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001585
Bob Wilsona4c22902009-04-17 19:07:39 +00001586 // Get a count of how many bytes are to be pushed on the stack.
1587 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001588
Dale Johannesend679ff72010-06-03 21:09:53 +00001589 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001590 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001591 NumBytes = 0;
1592
Evan Cheng10043e22007-01-19 07:51:42 +00001593 // Adjust the stack pointer for the new arguments...
1594 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001595 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001596 Chain = DAG.getCALLSEQ_START(Chain,
1597 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001598
Mehdi Amini44ede332015-07-09 02:09:04 +00001599 SDValue StackPtr =
1600 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001601
Bob Wilson2e076c42009-06-22 23:27:02 +00001602 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001603 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001604
Bob Wilsona4c22902009-04-17 19:07:39 +00001605 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001606 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001607 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1608 i != e;
1609 ++i, ++realArgIdx) {
1610 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001611 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001612 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001613 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001614
Bob Wilsona4c22902009-04-17 19:07:39 +00001615 // Promote the value if needed.
1616 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001617 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001618 case CCValAssign::Full: break;
1619 case CCValAssign::SExt:
1620 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1621 break;
1622 case CCValAssign::ZExt:
1623 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1624 break;
1625 case CCValAssign::AExt:
1626 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1627 break;
1628 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001629 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001630 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001631 }
1632
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001633 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001634 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001635 if (VA.getLocVT() == MVT::v2f64) {
1636 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001637 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001638 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001639 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001640
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001641 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001642 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1643
1644 VA = ArgLocs[++i]; // skip ahead to next loc
1645 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001646 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001647 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1648 } else {
1649 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001650
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001651 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1652 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001653 }
1654 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001655 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001656 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001657 }
1658 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001659 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1660 assert(VA.getLocVT() == MVT::i32 &&
1661 "unexpected calling convention register assignment");
1662 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001663 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001664 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001665 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001666 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001667 } else if (isByVal) {
1668 assert(VA.isMemLoc());
1669 unsigned offset = 0;
1670
1671 // True if this byval aggregate will be split between registers
1672 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001673 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001674 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001675
1676 if (CurByValIdx < ByValArgsCount) {
1677
1678 unsigned RegBegin, RegEnd;
1679 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1680
Mehdi Amini44ede332015-07-09 02:09:04 +00001681 EVT PtrVT =
1682 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001683 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001684 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001685 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001686 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1687 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1688 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001689 false, false, false,
1690 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001691 MemOpChains.push_back(Load.getValue(1));
1692 RegsToPass.push_back(std::make_pair(j, Load));
1693 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001694
1695 // If parameter size outsides register area, "offset" value
1696 // helps us to calculate stack slot for remained part properly.
1697 offset = RegEnd - RegBegin;
1698
1699 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001700 }
1701
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001702 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001703 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001704 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001705 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001706 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001707 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001708 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001709 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001710 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001711 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1712 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001713
Manman Ren9f911162012-06-01 02:44:42 +00001714 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001715 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001716 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001717 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001718 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001719 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001720 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001721
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001722 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1723 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001724 }
Evan Cheng10043e22007-01-19 07:51:42 +00001725 }
1726
1727 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001728 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001729
1730 // Build a sequence of copy-to-reg nodes chained together with token chain
1731 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001732 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001733 // Tail call byval lowering might overwrite argument registers so in case of
1734 // tail call optimization the copies to registers are lowered later.
1735 if (!isTailCall)
1736 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1737 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1738 RegsToPass[i].second, InFlag);
1739 InFlag = Chain.getValue(1);
1740 }
Evan Cheng10043e22007-01-19 07:51:42 +00001741
Dale Johannesend679ff72010-06-03 21:09:53 +00001742 // For tail calls lower the arguments to the 'real' stack slot.
1743 if (isTailCall) {
1744 // Force all the incoming stack arguments to be loaded from the stack
1745 // before any new outgoing arguments are stored to the stack, because the
1746 // outgoing stack slots may alias the incoming argument stack slots, and
1747 // the alias isn't otherwise explicit. This is slightly more conservative
1748 // than necessary, because it means that each store effectively depends
1749 // on every argument instead of just those arguments it would clobber.
1750
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001751 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001752 InFlag = SDValue();
1753 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1754 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1755 RegsToPass[i].second, InFlag);
1756 InFlag = Chain.getValue(1);
1757 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001758 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001759 }
1760
Bill Wendling24c79f22008-09-16 21:48:12 +00001761 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1762 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1763 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001764 bool isDirect = false;
1765 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001766 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001767 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001768 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001769
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001770 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001771 assert((Subtarget->isTargetWindows() ||
1772 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1773 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001774 // Handle a global address or an external symbol. If it's not one of
1775 // those, the target's already in a register, so we don't need to do
1776 // anything extra.
1777 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001778 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001779 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001780 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001781 ARMConstantPoolValue *CPV =
1782 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1783
Jim Grosbach32bb3622010-04-14 22:28:31 +00001784 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001785 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001786 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001787 Callee = DAG.getLoad(
1788 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1789 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1790 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001791 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1792 const char *Sym = S->getSymbol();
1793
1794 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001795 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001796 ARMConstantPoolValue *CPV =
1797 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1798 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001799 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001800 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001801 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001802 Callee = DAG.getLoad(
1803 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1804 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1805 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001806 }
1807 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001808 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001809 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001810 bool isDef = GV->isStrongDefinitionForLinker();
1811 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001812 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001813 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001814 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001815 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001816 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001817 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001818 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001819 Callee = DAG.getNode(
1820 ARMISD::WrapperPIC, dl, PtrVt,
1821 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1822 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001823 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1824 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001825 } else if (Subtarget->isTargetCOFF()) {
1826 assert(Subtarget->isTargetWindows() &&
1827 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001828 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1829 ? ARMII::MO_DLLIMPORT
1830 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001831 Callee =
1832 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001833 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001834 Callee =
1835 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1836 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001837 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1838 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001839 } else {
1840 // On ELF targets for PIC code, direct calls should go through the PLT
1841 unsigned OpFlags = 0;
1842 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001843 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001844 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001845 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001846 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001847 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001848 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001849 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001850 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001851 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001852 // tBX takes a register source operand.
1853 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001854 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001855 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001856 ARMConstantPoolValue *CPV =
1857 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1858 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001859 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001860 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001861 Callee = DAG.getLoad(
1862 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1863 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1864 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001865 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001866 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001867 } else {
1868 unsigned OpFlags = 0;
1869 // On ELF targets for PIC code, direct calls should go through the PLT
1870 if (Subtarget->isTargetELF() &&
1871 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1872 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001873 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001874 }
Evan Cheng10043e22007-01-19 07:51:42 +00001875 }
1876
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001877 // FIXME: handle tail calls differently.
1878 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001879 if (Subtarget->isThumb()) {
1880 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001881 CallOpc = ARMISD::CALL_NOLINK;
1882 else
1883 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1884 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001885 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001886 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001887 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001888 // Emit regular call when code size is the priority
1889 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001890 // "mov lr, pc; b _foo" to avoid confusing the RSP
1891 CallOpc = ARMISD::CALL_NOLINK;
1892 else
1893 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001894 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001895
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001896 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001897 Ops.push_back(Chain);
1898 Ops.push_back(Callee);
1899
1900 // Add argument registers to the end of the list so that they are known live
1901 // into the call.
1902 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1903 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1904 RegsToPass[i].second.getValueType()));
1905
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001906 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001907 if (!isTailCall) {
1908 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001909 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001910 if (isThisReturn) {
1911 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001912 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001913 if (!Mask) {
1914 // Set isThisReturn to false if the calling convention is not one that
1915 // allows 'returned' to be modeled in this way, so LowerCallResult does
1916 // not try to pass 'this' straight through
1917 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001918 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001919 }
1920 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001921 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001922
Matthias Braunc22630e2013-10-04 16:52:54 +00001923 assert(Mask && "Missing call preserved mask for calling convention");
1924 Ops.push_back(DAG.getRegisterMask(Mask));
1925 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001926
Gabor Greiff304a7a2008-08-28 21:40:38 +00001927 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001928 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001929
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001930 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001931 if (isTailCall) {
1932 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001933 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001934 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001935
Duncan Sands739a0542008-07-02 17:40:58 +00001936 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001937 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001938 InFlag = Chain.getValue(1);
1939
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001940 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1941 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001942 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001943 InFlag = Chain.getValue(1);
1944
Bob Wilsona4c22902009-04-17 19:07:39 +00001945 // Handle result values, copying them out of physregs into vregs that we
1946 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001947 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001948 InVals, isThisReturn,
1949 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001950}
1951
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001952/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001953/// on the stack. Remember the next parameter register to allocate,
1954/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001955/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001956void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1957 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001958 assert((State->getCallOrPrologue() == Prologue ||
1959 State->getCallOrPrologue() == Call) &&
1960 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001961
Tim Northover8cda34f2015-03-11 18:54:22 +00001962 // Byval (as with any stack) slots are always at least 4 byte aligned.
1963 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001964
Tim Northover8cda34f2015-03-11 18:54:22 +00001965 unsigned Reg = State->AllocateReg(GPRArgRegs);
1966 if (!Reg)
1967 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001968
Tim Northover8cda34f2015-03-11 18:54:22 +00001969 unsigned AlignInRegs = Align / 4;
1970 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1971 for (unsigned i = 0; i < Waste; ++i)
1972 Reg = State->AllocateReg(GPRArgRegs);
1973
1974 if (!Reg)
1975 return;
1976
1977 unsigned Excess = 4 * (ARM::R4 - Reg);
1978
1979 // Special case when NSAA != SP and parameter size greater than size of
1980 // all remained GPR regs. In that case we can't split parameter, we must
1981 // send it to stack. We also must set NCRN to R4, so waste all
1982 // remained registers.
1983 const unsigned NSAAOffset = State->getNextStackOffset();
1984 if (NSAAOffset != 0 && Size > Excess) {
1985 while (State->AllocateReg(GPRArgRegs))
1986 ;
1987 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001988 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001989
1990 // First register for byval parameter is the first register that wasn't
1991 // allocated before this method call, so it would be "reg".
1992 // If parameter is small enough to be saved in range [reg, r4), then
1993 // the end (first after last) register would be reg + param-size-in-regs,
1994 // else parameter would be splitted between registers and stack,
1995 // end register would be r4 in this case.
1996 unsigned ByValRegBegin = Reg;
1997 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1998 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1999 // Note, first register is allocated in the beginning of function already,
2000 // allocate remained amount of registers we need.
2001 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2002 State->AllocateReg(GPRArgRegs);
2003 // A byval parameter that is split between registers and memory needs its
2004 // size truncated here.
2005 // In the case where the entire structure fits in registers, we set the
2006 // size in memory to zero.
2007 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002008}
2009
Dale Johannesend679ff72010-06-03 21:09:53 +00002010/// MatchingStackOffset - Return true if the given stack call argument is
2011/// already available in the same position (relatively) of the caller's
2012/// incoming argument stack.
2013static
2014bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2015 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002016 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002017 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2018 int FI = INT_MAX;
2019 if (Arg.getOpcode() == ISD::CopyFromReg) {
2020 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002021 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002022 return false;
2023 MachineInstr *Def = MRI->getVRegDef(VR);
2024 if (!Def)
2025 return false;
2026 if (!Flags.isByVal()) {
2027 if (!TII->isLoadFromStackSlot(Def, FI))
2028 return false;
2029 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002030 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002031 }
2032 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2033 if (Flags.isByVal())
2034 // ByVal argument is passed in as a pointer but it's now being
2035 // dereferenced. e.g.
2036 // define @foo(%struct.X* %A) {
2037 // tail call @bar(%struct.X* byval %A)
2038 // }
2039 return false;
2040 SDValue Ptr = Ld->getBasePtr();
2041 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2042 if (!FINode)
2043 return false;
2044 FI = FINode->getIndex();
2045 } else
2046 return false;
2047
2048 assert(FI != INT_MAX);
2049 if (!MFI->isFixedObjectIndex(FI))
2050 return false;
2051 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2052}
2053
2054/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2055/// for tail call optimization. Targets which want to do tail call
2056/// optimization should implement this function.
2057bool
2058ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2059 CallingConv::ID CalleeCC,
2060 bool isVarArg,
2061 bool isCalleeStructRet,
2062 bool isCallerStructRet,
2063 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002064 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002065 const SmallVectorImpl<ISD::InputArg> &Ins,
2066 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002067 const Function *CallerF = DAG.getMachineFunction().getFunction();
2068 CallingConv::ID CallerCC = CallerF->getCallingConv();
2069 bool CCMatch = CallerCC == CalleeCC;
2070
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002071 assert(Subtarget->supportsTailCall());
2072
Dale Johannesend679ff72010-06-03 21:09:53 +00002073 // Look for obvious safe cases to perform tail call optimization that do not
2074 // require ABI changes. This is what gcc calls sibcall.
2075
Jim Grosbache3864cc2010-06-16 23:45:49 +00002076 // Do not sibcall optimize vararg calls unless the call site is not passing
2077 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002078 if (isVarArg && !Outs.empty())
2079 return false;
2080
Tim Northoverd8407452013-10-01 14:33:28 +00002081 // Exception-handling functions need a special set of instructions to indicate
2082 // a return to the hardware. Tail-calling another function would probably
2083 // break this.
2084 if (CallerF->hasFnAttribute("interrupt"))
2085 return false;
2086
Dale Johannesend679ff72010-06-03 21:09:53 +00002087 // Also avoid sibcall optimization if either caller or callee uses struct
2088 // return semantics.
2089 if (isCalleeStructRet || isCallerStructRet)
2090 return false;
2091
Oliver Stannard12993dd2014-08-18 12:42:15 +00002092 // Externally-defined functions with weak linkage should not be
2093 // tail-called on ARM when the OS does not support dynamic
2094 // pre-emption of symbols, as the AAELF spec requires normal calls
2095 // to undefined weak functions to be replaced with a NOP or jump to the
2096 // next instruction. The behaviour of branch instructions in this
2097 // situation (as used for tail calls) is implementation-defined, so we
2098 // cannot rely on the linker replacing the tail call with a return.
2099 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2100 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002101 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002102 if (GV->hasExternalWeakLinkage() &&
2103 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002104 return false;
2105 }
2106
Dale Johannesend679ff72010-06-03 21:09:53 +00002107 // If the calling conventions do not match, then we'd better make sure the
2108 // results are returned in the same way as what the caller expects.
2109 if (!CCMatch) {
2110 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002111 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2112 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002113 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2114
2115 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002116 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2117 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002118 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2119
2120 if (RVLocs1.size() != RVLocs2.size())
2121 return false;
2122 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2123 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2124 return false;
2125 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2126 return false;
2127 if (RVLocs1[i].isRegLoc()) {
2128 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2129 return false;
2130 } else {
2131 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2132 return false;
2133 }
2134 }
2135 }
2136
Manman Ren7e48b252012-10-12 23:39:43 +00002137 // If Caller's vararg or byval argument has been split between registers and
2138 // stack, do not perform tail call, since part of the argument is in caller's
2139 // local frame.
2140 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2141 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002142 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002143 return false;
2144
Dale Johannesend679ff72010-06-03 21:09:53 +00002145 // If the callee takes no arguments then go on to check the results of the
2146 // call.
2147 if (!Outs.empty()) {
2148 // Check if stack adjustment is needed. For now, do not do this if any
2149 // argument is passed on the stack.
2150 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002151 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2152 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002153 CCInfo.AnalyzeCallOperands(Outs,
2154 CCAssignFnForNode(CalleeCC, false, isVarArg));
2155 if (CCInfo.getNextStackOffset()) {
2156 MachineFunction &MF = DAG.getMachineFunction();
2157
2158 // Check if the arguments are already laid out in the right way as
2159 // the caller's fixed stack objects.
2160 MachineFrameInfo *MFI = MF.getFrameInfo();
2161 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002162 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002163 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2164 i != e;
2165 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002166 CCValAssign &VA = ArgLocs[i];
2167 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002168 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002169 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002170 if (VA.getLocInfo() == CCValAssign::Indirect)
2171 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002172 if (VA.needsCustom()) {
2173 // f64 and vector types are split into multiple registers or
2174 // register/stack-slot combinations. The types will not match
2175 // the registers; give up on memory f64 refs until we figure
2176 // out what to do about this.
2177 if (!VA.isRegLoc())
2178 return false;
2179 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002180 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002181 if (RegVT == MVT::v2f64) {
2182 if (!ArgLocs[++i].isRegLoc())
2183 return false;
2184 if (!ArgLocs[++i].isRegLoc())
2185 return false;
2186 }
2187 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002188 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2189 MFI, MRI, TII))
2190 return false;
2191 }
2192 }
2193 }
2194 }
2195
2196 return true;
2197}
2198
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002199bool
2200ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2201 MachineFunction &MF, bool isVarArg,
2202 const SmallVectorImpl<ISD::OutputArg> &Outs,
2203 LLVMContext &Context) const {
2204 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002205 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002206 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2207 isVarArg));
2208}
2209
Tim Northoverd8407452013-10-01 14:33:28 +00002210static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2211 SDLoc DL, SelectionDAG &DAG) {
2212 const MachineFunction &MF = DAG.getMachineFunction();
2213 const Function *F = MF.getFunction();
2214
2215 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2216
2217 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2218 // version of the "preferred return address". These offsets affect the return
2219 // instruction if this is a return from PL1 without hypervisor extensions.
2220 // IRQ/FIQ: +4 "subs pc, lr, #4"
2221 // SWI: 0 "subs pc, lr, #0"
2222 // ABORT: +4 "subs pc, lr, #4"
2223 // UNDEF: +4/+2 "subs pc, lr, #0"
2224 // UNDEF varies depending on where the exception came from ARM or Thumb
2225 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2226
2227 int64_t LROffset;
2228 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2229 IntKind == "ABORT")
2230 LROffset = 4;
2231 else if (IntKind == "SWI" || IntKind == "UNDEF")
2232 LROffset = 0;
2233 else
2234 report_fatal_error("Unsupported interrupt attribute. If present, value "
2235 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2236
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002237 RetOps.insert(RetOps.begin() + 1,
2238 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002239
Craig Topper48d114b2014-04-26 18:35:24 +00002240 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002241}
2242
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002243SDValue
2244ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002245 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002246 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002247 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002248 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002249
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002250 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002251 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002252
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002253 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002254 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2255 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002256
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002257 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002258 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2259 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002260
Bob Wilsona4c22902009-04-17 19:07:39 +00002261 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002262 SmallVector<SDValue, 4> RetOps;
2263 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002264 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002265
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002266 MachineFunction &MF = DAG.getMachineFunction();
2267 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2268 AFI->setReturnRegsCount(RVLocs.size());
2269
Bob Wilsona4c22902009-04-17 19:07:39 +00002270 // Copy the result values into the output registers.
2271 for (unsigned i = 0, realRVLocIdx = 0;
2272 i != RVLocs.size();
2273 ++i, ++realRVLocIdx) {
2274 CCValAssign &VA = RVLocs[i];
2275 assert(VA.isRegLoc() && "Can only return in registers!");
2276
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002277 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002278
2279 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002280 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002281 case CCValAssign::Full: break;
2282 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002283 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002284 break;
2285 }
2286
Bob Wilsona4c22902009-04-17 19:07:39 +00002287 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002288 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002289 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002290 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002291 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002292 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002293 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002294
Christian Pirkerb5728192014-05-08 14:06:24 +00002295 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2296 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2297 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002298 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002299 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002300 VA = RVLocs[++i]; // skip ahead to next loc
2301 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002302 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2303 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002304 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002305 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002306 VA = RVLocs[++i]; // skip ahead to next loc
2307
2308 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002309 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002310 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002311 }
2312 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2313 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002314 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002315 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002316 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2317 fmrrd.getValue(isLittleEndian ? 0 : 1),
2318 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002319 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002320 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002321 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002322 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2323 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002324 Flag);
2325 } else
2326 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2327
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002328 // Guarantee that all emitted copies are
2329 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002330 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002331 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002332 }
2333
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002334 // Update chain and glue.
2335 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002336 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002337 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002338
Tim Northoverd8407452013-10-01 14:33:28 +00002339 // CPUs which aren't M-class use a special sequence to return from
2340 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2341 // though we use "subs pc, lr, #N").
2342 //
2343 // M-class CPUs actually use a normal return sequence with a special
2344 // (hardware-provided) value in LR, so the normal code path works.
2345 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2346 !Subtarget->isMClass()) {
2347 if (Subtarget->isThumb1Only())
2348 report_fatal_error("interrupt attribute is not supported in Thumb1");
2349 return LowerInterruptReturn(RetOps, dl, DAG);
2350 }
2351
Craig Topper48d114b2014-04-26 18:35:24 +00002352 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002353}
2354
Evan Chengf8bad082012-04-10 01:51:00 +00002355bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002356 if (N->getNumValues() != 1)
2357 return false;
2358 if (!N->hasNUsesOfValue(1, 0))
2359 return false;
2360
Evan Chengf8bad082012-04-10 01:51:00 +00002361 SDValue TCChain = Chain;
2362 SDNode *Copy = *N->use_begin();
2363 if (Copy->getOpcode() == ISD::CopyToReg) {
2364 // If the copy has a glue operand, we conservatively assume it isn't safe to
2365 // perform a tail call.
2366 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2367 return false;
2368 TCChain = Copy->getOperand(0);
2369 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2370 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002371 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002372 SmallPtrSet<SDNode*, 2> Copies;
2373 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002374 UI != UE; ++UI) {
2375 if (UI->getOpcode() != ISD::CopyToReg)
2376 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002377 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002378 }
Evan Chengf8bad082012-04-10 01:51:00 +00002379 if (Copies.size() > 2)
2380 return false;
2381
2382 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2383 UI != UE; ++UI) {
2384 SDValue UseChain = UI->getOperand(0);
2385 if (Copies.count(UseChain.getNode()))
2386 // Second CopyToReg
2387 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002388 else {
2389 // We are at the top of this chain.
2390 // If the copy has a glue operand, we conservatively assume it
2391 // isn't safe to perform a tail call.
2392 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2393 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002394 // First CopyToReg
2395 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002396 }
Evan Chengf8bad082012-04-10 01:51:00 +00002397 }
2398 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002399 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002400 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002401 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002402 Copy = *Copy->use_begin();
2403 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002404 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002405 // If the copy has a glue operand, we conservatively assume it isn't safe to
2406 // perform a tail call.
2407 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2408 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002409 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002410 } else {
2411 return false;
2412 }
2413
Evan Cheng419ea282010-12-01 22:59:46 +00002414 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002415 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2416 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002417 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2418 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002419 return false;
2420 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002421 }
2422
Evan Chengf8bad082012-04-10 01:51:00 +00002423 if (!HasRet)
2424 return false;
2425
2426 Chain = TCChain;
2427 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002428}
2429
Evan Cheng0663f232011-03-21 01:19:09 +00002430bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002431 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002432 return false;
2433
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002434 auto Attr =
2435 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2436 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002437 return false;
2438
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002439 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002440}
2441
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002442// Trying to write a 64 bit value so need to split into two 32 bit values first,
2443// and pass the lower and high parts through.
2444static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2445 SDLoc DL(Op);
2446 SDValue WriteValue = Op->getOperand(2);
2447
2448 // This function is only supposed to be called for i64 type argument.
2449 assert(WriteValue.getValueType() == MVT::i64
2450 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2451
2452 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2453 DAG.getConstant(0, DL, MVT::i32));
2454 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2455 DAG.getConstant(1, DL, MVT::i32));
2456 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2457 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2458}
2459
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002460// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2461// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2462// one of the above mentioned nodes. It has to be wrapped because otherwise
2463// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2464// be used to form addressing mode. These wrapped nodes will be selected
2465// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002466static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002467 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002468 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002469 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002470 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002471 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002472 if (CP->isMachineConstantPoolEntry())
2473 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2474 CP->getAlignment());
2475 else
2476 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2477 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002478 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002479}
2480
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002481unsigned ARMTargetLowering::getJumpTableEncoding() const {
2482 return MachineJumpTableInfo::EK_Inline;
2483}
2484
Dan Gohman21cea8a2010-04-17 15:26:15 +00002485SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2486 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002487 MachineFunction &MF = DAG.getMachineFunction();
2488 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2489 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002490 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002491 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002492 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002493 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2494 SDValue CPAddr;
2495 if (RelocM == Reloc::Static) {
2496 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2497 } else {
2498 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002499 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002500 ARMConstantPoolValue *CPV =
2501 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2502 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002503 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2504 }
2505 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002506 SDValue Result =
2507 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2508 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2509 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002510 if (RelocM == Reloc::Static)
2511 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002512 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002513 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002514}
2515
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002516// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002517SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002518ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002519 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002520 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002521 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002522 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002523 MachineFunction &MF = DAG.getMachineFunction();
2524 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002525 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002526 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002527 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2528 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002529 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002530 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002531 Argument =
2532 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2533 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2534 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002535 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002536
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002537 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002538 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002539
2540 // call __tls_get_addr.
2541 ArgListTy Args;
2542 ArgListEntry Entry;
2543 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002544 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002545 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002546
Dale Johannesen555a3752009-01-30 23:10:59 +00002547 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002548 TargetLowering::CallLoweringInfo CLI(DAG);
2549 CLI.setDebugLoc(dl).setChain(Chain)
2550 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002551 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2552 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002553
Justin Holewinskiaa583972012-05-25 16:35:28 +00002554 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002555 return CallResult.first;
2556}
2557
2558// Lower ISD::GlobalTLSAddress using the "initial exec" or
2559// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002560SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002561ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002562 SelectionDAG &DAG,
2563 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002564 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002565 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002566 SDValue Offset;
2567 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002568 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002569 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002570 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002571
Hans Wennborgaea41202012-05-04 09:40:39 +00002572 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002573 MachineFunction &MF = DAG.getMachineFunction();
2574 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002575 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002576 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002577 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2578 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002579 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2580 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2581 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002582 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002583 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002584 Offset = DAG.getLoad(
2585 PtrVT, dl, Chain, Offset,
2586 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2587 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002588 Chain = Offset.getValue(1);
2589
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002590 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002591 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002592
Alex Lorenze40c8a22015-08-11 23:09:45 +00002593 Offset = DAG.getLoad(
2594 PtrVT, dl, Chain, Offset,
2595 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2596 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002597 } else {
2598 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002599 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002600 ARMConstantPoolValue *CPV =
2601 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002602 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002603 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002604 Offset = DAG.getLoad(
2605 PtrVT, dl, Chain, Offset,
2606 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2607 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002608 }
2609
2610 // The address of the thread local variable is the add of the thread
2611 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002612 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002613}
2614
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002615SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002616ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002617 // TODO: implement the "local dynamic" model
2618 assert(Subtarget->isTargetELF() &&
2619 "TLS not implemented for non-ELF targets");
2620 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002621 if (DAG.getTarget().Options.EmulatedTLS)
2622 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002623
2624 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2625
2626 switch (model) {
2627 case TLSModel::GeneralDynamic:
2628 case TLSModel::LocalDynamic:
2629 return LowerToTLSGeneralDynamicModel(GA, DAG);
2630 case TLSModel::InitialExec:
2631 case TLSModel::LocalExec:
2632 return LowerToTLSExecModels(GA, DAG, model);
2633 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002634 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002635}
2636
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002637SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002638 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002639 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002640 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002641 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002642 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002643 bool UseGOT_PREL =
Peter Collingbourne99fac802015-10-26 20:46:44 +00002644 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage());
Peter Collingbourne97aae402015-10-26 18:23:16 +00002645
2646 MachineFunction &MF = DAG.getMachineFunction();
2647 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2648 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2649 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2650 SDLoc dl(Op);
2651 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2652 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2653 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2654 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2655 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002656 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002657 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002658 SDValue Result = DAG.getLoad(
2659 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2660 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2661 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002662 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002663 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2664 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2665 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002666 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002667 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002668 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002669 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002670 }
2671
2672 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002673 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002674 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002675 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002676 // FIXME: Once remat is capable of dealing with instructions with register
2677 // operands, expand this into two nodes.
2678 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2679 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002680 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002681 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2682 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002683 return DAG.getLoad(
2684 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2685 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2686 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002687 }
2688}
2689
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002690SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002691 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002692 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002693 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002694 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002695 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002696
Eric Christopherc1058df2014-07-04 01:55:26 +00002697 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002698 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002699
Tim Northover72360d22013-12-02 10:35:41 +00002700 // FIXME: Once remat is capable of dealing with instructions with register
2701 // operands, expand this into multiple nodes
2702 unsigned Wrapper =
2703 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002704
Tim Northover72360d22013-12-02 10:35:41 +00002705 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2706 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002707
Evan Cheng1b389522009-09-03 07:04:02 +00002708 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002709 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002710 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2711 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002712 return Result;
2713}
2714
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002715SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2716 SelectionDAG &DAG) const {
2717 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002718 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2719 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002720
2721 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002722 const ARMII::TOF TargetFlags =
2723 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002724 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002725 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002726 SDLoc DL(Op);
2727
2728 ++NumMovwMovt;
2729
2730 // FIXME: Once remat is capable of dealing with instructions with register
2731 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002732 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2733 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002734 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002735 if (GV->hasDLLImportStorageClass())
2736 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002737 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2738 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002739 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002740}
2741
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002742SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002743ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002744 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002745 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002746 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2747 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002748 Op.getOperand(1), Val);
2749}
2750
2751SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002752ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002753 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002754 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002755 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002756}
2757
Matthias Braun3cd00c12015-07-16 22:34:16 +00002758SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2759 SelectionDAG &DAG) const {
2760 SDLoc dl(Op);
2761 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2762 Op.getOperand(0));
2763}
2764
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002765SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002766ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002767 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002768 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002769 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002770 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002771 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002772 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002773 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002774 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002775 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002776 }
Bob Wilson17f88782009-08-04 00:25:01 +00002777 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002778 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002779 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2780 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002781 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002782 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002783 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002784 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002785 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002786 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2787 SDValue CPAddr;
2788 unsigned PCAdj = (RelocM != Reloc::PIC_)
2789 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002790 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002791 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2792 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002793 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002794 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002795 SDValue Result = DAG.getLoad(
2796 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2797 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2798 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002799
2800 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002801 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002802 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2803 }
2804 return Result;
2805 }
Evan Cheng18381b42011-03-29 23:06:19 +00002806 case Intrinsic::arm_neon_vmulls:
2807 case Intrinsic::arm_neon_vmullu: {
2808 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2809 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002810 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002811 Op.getOperand(1), Op.getOperand(2));
2812 }
James Molloyee868b22015-08-11 12:06:25 +00002813 case Intrinsic::arm_neon_vminnm:
2814 case Intrinsic::arm_neon_vmaxnm: {
2815 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2816 ? ISD::FMINNUM : ISD::FMAXNUM;
2817 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2818 Op.getOperand(1), Op.getOperand(2));
2819 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002820 case Intrinsic::arm_neon_vminu:
2821 case Intrinsic::arm_neon_vmaxu: {
2822 if (Op.getValueType().isFloatingPoint())
2823 return SDValue();
2824 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2825 ? ISD::UMIN : ISD::UMAX;
2826 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2827 Op.getOperand(1), Op.getOperand(2));
2828 }
James Molloyd616c642015-08-11 12:06:28 +00002829 case Intrinsic::arm_neon_vmins:
2830 case Intrinsic::arm_neon_vmaxs: {
2831 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002832 if (!Op.getValueType().isFloatingPoint()) {
2833 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2834 ? ISD::SMIN : ISD::SMAX;
2835 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2836 Op.getOperand(1), Op.getOperand(2));
2837 }
James Molloyd616c642015-08-11 12:06:28 +00002838 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2839 ? ISD::FMINNAN : ISD::FMAXNAN;
2840 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2841 Op.getOperand(1), Op.getOperand(2));
2842 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002843 }
2844}
2845
Eli Friedman30a49e92011-08-03 21:06:02 +00002846static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2847 const ARMSubtarget *Subtarget) {
2848 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002849 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002850 if (!Subtarget->hasDataBarrier()) {
2851 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2852 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2853 // here.
2854 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002855 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002856 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002857 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002858 }
2859
Tim Northover36b24172013-07-03 09:20:36 +00002860 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2861 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002862 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002863 if (Subtarget->isMClass()) {
2864 // Only a full system barrier exists in the M-class architectures.
2865 Domain = ARM_MB::SY;
2866 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002867 // Swift happens to implement ISHST barriers in a way that's compatible with
2868 // Release semantics but weaker than ISH so we'd be fools not to use
2869 // it. Beware: other processors probably don't!
2870 Domain = ARM_MB::ISHST;
2871 }
2872
Joey Gouly926d3f52013-09-05 15:35:24 +00002873 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002874 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2875 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002876}
2877
Evan Cheng8740ee32010-11-03 06:34:55 +00002878static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2879 const ARMSubtarget *Subtarget) {
2880 // ARM pre v5TE and Thumb1 does not have preload instructions.
2881 if (!(Subtarget->isThumb2() ||
2882 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2883 // Just preserve the chain.
2884 return Op.getOperand(0);
2885
Andrew Trickef9de2a2013-05-25 02:42:55 +00002886 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002887 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2888 if (!isRead &&
2889 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2890 // ARMv7 with MP extension has PLDW.
2891 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002892
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002893 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2894 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002895 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002896 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002897 isData = ~isData & 1;
2898 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002899
2900 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002901 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2902 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002903}
2904
Dan Gohman31ae5862010-04-17 14:41:14 +00002905static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2906 MachineFunction &MF = DAG.getMachineFunction();
2907 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2908
Evan Cheng10043e22007-01-19 07:51:42 +00002909 // vastart just stores the address of the VarArgsFrameIndex slot into the
2910 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002911 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002912 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002913 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002914 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002915 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2916 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002917}
2918
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002919SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002920ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2921 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002922 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002923 MachineFunction &MF = DAG.getMachineFunction();
2924 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2925
Craig Topper760b1342012-02-22 05:59:10 +00002926 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002927 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002928 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002929 else
Craig Topperc7242e02012-04-20 07:30:17 +00002930 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002931
2932 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002933 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002934 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002935
2936 SDValue ArgValue2;
2937 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002938 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002939 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002940
2941 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002942 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00002943 ArgValue2 = DAG.getLoad(
2944 MVT::i32, dl, Root, FIN,
2945 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
2946 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002947 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002948 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002949 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002950 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002951 if (!Subtarget->isLittle())
2952 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002953 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002954}
2955
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002956// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002957// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002958// byval). Either way, we allocate stack slots adjacent to the data
2959// provided by our caller, and store the unallocated registers there.
2960// If this is a variadic function, the va_list pointer will begin with
2961// these values; otherwise, this reassembles a (byval) structure that
2962// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002963// Return: The frame index registers were stored into.
2964int
2965ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002966 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002967 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002968 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002969 int ArgOffset,
2970 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002971 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002972 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002973 // Setup first unallocated register as first byval register;
2974 // eat all remained registers
2975 // (these two actions are performed by HandleByVal method).
2976 // Then, here, we initialize stack frame with
2977 // "store-reg" instructions.
2978 // Case #2. Var-args function, that doesn't contain byval parameters.
2979 // The same: eat all remained unallocated registers,
2980 // initialize stack frame.
2981
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002982 MachineFunction &MF = DAG.getMachineFunction();
2983 MachineFrameInfo *MFI = MF.getFrameInfo();
2984 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002985 unsigned RBegin, REnd;
2986 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2987 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002988 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002989 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00002990 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00002991 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002992 }
2993
Tim Northover8cda34f2015-03-11 18:54:22 +00002994 if (REnd != RBegin)
2995 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002996
Mehdi Amini44ede332015-07-09 02:09:04 +00002997 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00002998 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002999 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003000
Tim Northover8cda34f2015-03-11 18:54:22 +00003001 SmallVector<SDValue, 4> MemOps;
3002 const TargetRegisterClass *RC =
3003 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003004
Tim Northover8cda34f2015-03-11 18:54:22 +00003005 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3006 unsigned VReg = MF.addLiveIn(Reg, RC);
3007 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3008 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003009 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003010 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3011 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003012 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003013 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003014
3015 if (!MemOps.empty())
3016 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3017 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003018}
3019
3020// Setup stack frame, the va_list pointer will start from.
3021void
3022ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003023 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003024 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003025 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003026 bool ForceMutable) const {
3027 MachineFunction &MF = DAG.getMachineFunction();
3028 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3029
3030 // Try to store any remaining integer argument regs
3031 // to their spots on the stack so that they may be loaded by deferencing
3032 // the result of va_next.
3033 // If there is no regs to be stored, just point address after last
3034 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003035 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3036 CCInfo.getInRegsParamsCount(),
3037 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003038 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003039}
3040
Bob Wilson2e076c42009-06-22 23:27:02 +00003041SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003042ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003043 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003044 const SmallVectorImpl<ISD::InputArg>
3045 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003046 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003047 SmallVectorImpl<SDValue> &InVals)
3048 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003049 MachineFunction &MF = DAG.getMachineFunction();
3050 MachineFrameInfo *MFI = MF.getFrameInfo();
3051
Bob Wilsona4c22902009-04-17 19:07:39 +00003052 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3053
3054 // Assign locations to all of the incoming arguments.
3055 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003056 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3057 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003058 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003059 CCAssignFnForNode(CallConv, /* Return*/ false,
3060 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003061
Bob Wilsona4c22902009-04-17 19:07:39 +00003062 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003063 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003064 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3065 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003066
3067 // Initially ArgRegsSaveSize is zero.
3068 // Then we increase this value each time we meet byval parameter.
3069 // We also increase this value in case of varargs function.
3070 AFI->setArgRegsSaveSize(0);
3071
Oliver Stannardd55e1152014-03-05 15:25:27 +00003072 // Calculate the amount of stack space that we need to allocate to store
3073 // byval and variadic arguments that are passed in registers.
3074 // We need to know this before we allocate the first byval or variadic
3075 // argument, as they will be allocated a stack slot below the CFA (Canonical
3076 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003077 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003078 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003079 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3080 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003081
Tim Northover8cda34f2015-03-11 18:54:22 +00003082 CCValAssign &VA = ArgLocs[i];
3083 unsigned Index = VA.getValNo();
3084 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3085 if (!Flags.isByVal())
3086 continue;
3087
3088 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3089 unsigned RBegin, REnd;
3090 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3091 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3092
3093 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003094 }
3095 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003096
3097 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003098 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003099 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3100 if (RegIdx != array_lengthof(GPRArgRegs))
3101 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003102 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003103
3104 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3105 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003106 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003107
Bob Wilsona4c22902009-04-17 19:07:39 +00003108 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3109 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003110 if (Ins[VA.getValNo()].isOrigArg()) {
3111 std::advance(CurOrigArg,
3112 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3113 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3114 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003115 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003116 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003117 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003118
Bob Wilsona4c22902009-04-17 19:07:39 +00003119 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003120 // f64 and vector types are split up into multiple registers or
3121 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003122 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003123 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003124 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003125 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003126 SDValue ArgValue2;
3127 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003128 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003129 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003130 ArgValue2 = DAG.getLoad(
3131 MVT::f64, dl, Chain, FIN,
3132 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3133 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003134 } else {
3135 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3136 Chain, DAG, dl);
3137 }
Owen Anderson9f944592009-08-11 20:47:22 +00003138 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3139 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003140 ArgValue, ArgValue1,
3141 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003142 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003143 ArgValue, ArgValue2,
3144 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003145 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003146 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003147
Bob Wilson2e076c42009-06-22 23:27:02 +00003148 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003149 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003150
Owen Anderson9f944592009-08-11 20:47:22 +00003151 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003152 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003153 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003154 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003155 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003156 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003157 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003158 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3159 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003160 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003161 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003162
3163 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003164 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003165 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003166 }
3167
3168 // If this is an 8 or 16-bit value, it is really passed promoted
3169 // to 32 bits. Insert an assert[sz]ext to capture this, then
3170 // truncate to the right size.
3171 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003172 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003173 case CCValAssign::Full: break;
3174 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003175 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003176 break;
3177 case CCValAssign::SExt:
3178 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3179 DAG.getValueType(VA.getValVT()));
3180 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3181 break;
3182 case CCValAssign::ZExt:
3183 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3184 DAG.getValueType(VA.getValVT()));
3185 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3186 break;
3187 }
3188
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003189 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003190
3191 } else { // VA.isRegLoc()
3192
3193 // sanity check
3194 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003195 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003196
Andrew Trick05938a52015-02-16 18:10:47 +00003197 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003198
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003199 // Some Ins[] entries become multiple ArgLoc[] entries.
3200 // Process them only once.
3201 if (index != lastInsIndex)
3202 {
3203 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003204 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003205 // This can be changed with more analysis.
3206 // In case of tail call optimization mark all arguments mutable.
3207 // Since they could be overwritten by lowering of arguments in case of
3208 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003209 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003210 assert(Ins[index].isOrigArg() &&
3211 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003212 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003213
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003214 int FrameIndex = StoreByValRegs(
3215 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3216 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003217 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003218 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003219 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003220 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003221 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003222 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003223
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003224 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003225 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003226 InVals.push_back(DAG.getLoad(
3227 VA.getValVT(), dl, Chain, FIN,
3228 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3229 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003230 }
3231 lastInsIndex = index;
3232 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003233 }
3234 }
3235
3236 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003237 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003238 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003239 CCInfo.getNextStackOffset(),
3240 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003241
Oliver Stannardb14c6252014-04-02 16:10:33 +00003242 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3243
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003244 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003245}
3246
3247/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003248static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003249 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003250 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003251 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003252 // Maybe this has already been legalized into the constant pool?
3253 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003254 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003255 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003256 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003257 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003258 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003259 } else if (Op->getOpcode() == ISD::BITCAST &&
3260 Op->getValueType(0) == MVT::f64) {
3261 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3262 // created by LowerConstantFP().
3263 SDValue BitcastOp = Op->getOperand(0);
3264 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3265 SDValue MoveOp = BitcastOp->getOperand(0);
3266 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3267 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3268 return true;
3269 }
3270 }
Evan Cheng10043e22007-01-19 07:51:42 +00003271 }
3272 return false;
3273}
3274
Evan Cheng10043e22007-01-19 07:51:42 +00003275/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3276/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003277SDValue
3278ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003279 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003280 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003281 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003282 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003283 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003284 // Constant does not fit, try adjusting it by one?
3285 switch (CC) {
3286 default: break;
3287 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003288 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003289 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003290 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003291 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003292 }
3293 break;
3294 case ISD::SETULT:
3295 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003296 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003297 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003298 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003299 }
3300 break;
3301 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003302 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003303 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003304 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003305 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003306 }
3307 break;
3308 case ISD::SETULE:
3309 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003310 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003311 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003312 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003313 }
3314 break;
3315 }
3316 }
3317 }
3318
3319 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003320 ARMISD::NodeType CompareType;
3321 switch (CondCode) {
3322 default:
3323 CompareType = ARMISD::CMP;
3324 break;
3325 case ARMCC::EQ:
3326 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003327 // Uses only Z Flag
3328 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003329 break;
3330 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003331 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003332 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003333}
3334
3335/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003336SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003337ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003338 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003339 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003340 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003341 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003342 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003343 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003344 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3345 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003346}
3347
Bob Wilson45acbd02011-03-08 01:17:20 +00003348/// duplicateCmp - Glue values can have only one use, so this function
3349/// duplicates a comparison node.
3350SDValue
3351ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3352 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003353 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003354 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3355 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3356
3357 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3358 Cmp = Cmp.getOperand(0);
3359 Opc = Cmp.getOpcode();
3360 if (Opc == ARMISD::CMPFP)
3361 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3362 else {
3363 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3364 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3365 }
3366 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3367}
3368
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003369std::pair<SDValue, SDValue>
3370ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3371 SDValue &ARMcc) const {
3372 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3373
3374 SDValue Value, OverflowCmp;
3375 SDValue LHS = Op.getOperand(0);
3376 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003377 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003378
3379 // FIXME: We are currently always generating CMPs because we don't support
3380 // generating CMN through the backend. This is not as good as the natural
3381 // CMP case because it causes a register dependency and cannot be folded
3382 // later.
3383
3384 switch (Op.getOpcode()) {
3385 default:
3386 llvm_unreachable("Unknown overflow instruction!");
3387 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003388 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3389 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3390 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003391 break;
3392 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003393 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3394 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3395 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003396 break;
3397 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003398 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3399 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3400 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003401 break;
3402 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003403 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3404 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3405 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003406 break;
3407 } // switch (...)
3408
3409 return std::make_pair(Value, OverflowCmp);
3410}
3411
3412
3413SDValue
3414ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3415 // Let legalize expand this if it isn't a legal type yet.
3416 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3417 return SDValue();
3418
3419 SDValue Value, OverflowCmp;
3420 SDValue ARMcc;
3421 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3422 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003423 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003424 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003425 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3426 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003427 EVT VT = Op.getValueType();
3428
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003429 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003430 ARMcc, CCR, OverflowCmp);
3431
3432 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003433 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003434}
3435
3436
Bill Wendling6a981312010-08-11 08:43:16 +00003437SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3438 SDValue Cond = Op.getOperand(0);
3439 SDValue SelectTrue = Op.getOperand(1);
3440 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003441 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003442 unsigned Opc = Cond.getOpcode();
3443
3444 if (Cond.getResNo() == 1 &&
3445 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3446 Opc == ISD::USUBO)) {
3447 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3448 return SDValue();
3449
3450 SDValue Value, OverflowCmp;
3451 SDValue ARMcc;
3452 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3453 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3454 EVT VT = Op.getValueType();
3455
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003456 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003457 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003458 }
Bill Wendling6a981312010-08-11 08:43:16 +00003459
3460 // Convert:
3461 //
3462 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3463 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3464 //
3465 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3466 const ConstantSDNode *CMOVTrue =
3467 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3468 const ConstantSDNode *CMOVFalse =
3469 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3470
3471 if (CMOVTrue && CMOVFalse) {
3472 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3473 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3474
3475 SDValue True;
3476 SDValue False;
3477 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3478 True = SelectTrue;
3479 False = SelectFalse;
3480 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3481 True = SelectFalse;
3482 False = SelectTrue;
3483 }
3484
3485 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003486 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003487 SDValue ARMcc = Cond.getOperand(2);
3488 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003489 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003490 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003491 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003492 }
3493 }
3494 }
3495
Dan Gohmand4a77c42012-02-24 00:09:36 +00003496 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3497 // undefined bits before doing a full-word comparison with zero.
3498 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003499 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003500
Bill Wendling6a981312010-08-11 08:43:16 +00003501 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003502 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003503 SelectTrue, SelectFalse, ISD::SETNE);
3504}
3505
Joey Gouly881eab52013-08-22 15:29:11 +00003506static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3507 bool &swpCmpOps, bool &swpVselOps) {
3508 // Start by selecting the GE condition code for opcodes that return true for
3509 // 'equality'
3510 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3511 CC == ISD::SETULE)
3512 CondCode = ARMCC::GE;
3513
3514 // and GT for opcodes that return false for 'equality'.
3515 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3516 CC == ISD::SETULT)
3517 CondCode = ARMCC::GT;
3518
3519 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3520 // to swap the compare operands.
3521 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3522 CC == ISD::SETULT)
3523 swpCmpOps = true;
3524
3525 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3526 // If we have an unordered opcode, we need to swap the operands to the VSEL
3527 // instruction (effectively negating the condition).
3528 //
3529 // This also has the effect of swapping which one of 'less' or 'greater'
3530 // returns true, so we also swap the compare operands. It also switches
3531 // whether we return true for 'equality', so we compensate by picking the
3532 // opposite condition code to our original choice.
3533 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3534 CC == ISD::SETUGT) {
3535 swpCmpOps = !swpCmpOps;
3536 swpVselOps = !swpVselOps;
3537 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3538 }
3539
3540 // 'ordered' is 'anything but unordered', so use the VS condition code and
3541 // swap the VSEL operands.
3542 if (CC == ISD::SETO) {
3543 CondCode = ARMCC::VS;
3544 swpVselOps = true;
3545 }
3546
3547 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3548 // code and swap the VSEL operands.
3549 if (CC == ISD::SETUNE) {
3550 CondCode = ARMCC::EQ;
3551 swpVselOps = true;
3552 }
3553}
3554
Oliver Stannard51b1d462014-08-21 12:50:31 +00003555SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3556 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3557 SDValue Cmp, SelectionDAG &DAG) const {
3558 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3559 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3560 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3561 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3562 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3563
3564 SDValue TrueLow = TrueVal.getValue(0);
3565 SDValue TrueHigh = TrueVal.getValue(1);
3566 SDValue FalseLow = FalseVal.getValue(0);
3567 SDValue FalseHigh = FalseVal.getValue(1);
3568
3569 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3570 ARMcc, CCR, Cmp);
3571 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3572 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3573
3574 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3575 } else {
3576 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3577 Cmp);
3578 }
3579}
3580
Dan Gohman21cea8a2010-04-17 15:26:15 +00003581SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003582 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003583 SDValue LHS = Op.getOperand(0);
3584 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003585 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003586 SDValue TrueVal = Op.getOperand(2);
3587 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003588 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003589
Oliver Stannard51b1d462014-08-21 12:50:31 +00003590 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3591 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3592 dl);
3593
3594 // If softenSetCCOperands only returned one value, we should compare it to
3595 // zero.
3596 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003597 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003598 CC = ISD::SETNE;
3599 }
3600 }
3601
Owen Anderson9f944592009-08-11 20:47:22 +00003602 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003603 // Try to generate VSEL on ARMv8.
3604 // The VSEL instruction can't use all the usual ARM condition
3605 // codes: it only has two bits to select the condition code, so it's
3606 // constrained to use only GE, GT, VS and EQ.
3607 //
3608 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3609 // swap the operands of the previous compare instruction (effectively
3610 // inverting the compare condition, swapping 'less' and 'greater') and
3611 // sometimes need to swap the operands to the VSEL (which inverts the
3612 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003613 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3614 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003615 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3616 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3617 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003618 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003619 std::swap(TrueVal, FalseVal);
3620 }
3621 }
3622
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003623 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003624 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003625 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003626 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003627 }
3628
3629 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003630 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003631
Scott Douglass7ad77922015-04-08 17:18:28 +00003632 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003633 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3634 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003635 bool swpCmpOps = false;
3636 bool swpVselOps = false;
3637 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3638
3639 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3640 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3641 if (swpCmpOps)
3642 std::swap(LHS, RHS);
3643 if (swpVselOps)
3644 std::swap(TrueVal, FalseVal);
3645 }
3646 }
3647
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003648 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003649 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003650 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003651 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003652 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003653 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003654 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003655 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003656 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003657 }
3658 return Result;
3659}
3660
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003661/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3662/// to morph to an integer compare sequence.
3663static bool canChangeToInt(SDValue Op, bool &SeenZero,
3664 const ARMSubtarget *Subtarget) {
3665 SDNode *N = Op.getNode();
3666 if (!N->hasOneUse())
3667 // Otherwise it requires moving the value from fp to integer registers.
3668 return false;
3669 if (!N->getNumValues())
3670 return false;
3671 EVT VT = Op.getValueType();
3672 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3673 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3674 // vmrs are very slow, e.g. cortex-a8.
3675 return false;
3676
3677 if (isFloatingPointZero(Op)) {
3678 SeenZero = true;
3679 return true;
3680 }
3681 return ISD::isNormalLoad(N);
3682}
3683
3684static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3685 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003686 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003687
3688 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003689 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003690 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003691 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003692 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003693
3694 llvm_unreachable("Unknown VFP cmp argument!");
3695}
3696
3697static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3698 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003699 SDLoc dl(Op);
3700
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003701 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003702 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3703 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003704 return;
3705 }
3706
3707 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3708 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003709 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003710 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003711 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003712 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003713 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003714
3715 EVT PtrType = Ptr.getValueType();
3716 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003717 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3718 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3719 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003720 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003721 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003722 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003723 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003724 return;
3725 }
3726
3727 llvm_unreachable("Unknown VFP cmp argument!");
3728}
3729
3730/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3731/// f32 and even f64 comparisons to integer ones.
3732SDValue
3733ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3734 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003735 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003736 SDValue LHS = Op.getOperand(2);
3737 SDValue RHS = Op.getOperand(3);
3738 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003739 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003740
Evan Chengd12af5d2012-03-01 23:27:13 +00003741 bool LHSSeenZero = false;
3742 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3743 bool RHSSeenZero = false;
3744 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3745 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003746 // If unsafe fp math optimization is enabled and there are no other uses of
3747 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003748 // to an integer comparison.
3749 if (CC == ISD::SETOEQ)
3750 CC = ISD::SETEQ;
3751 else if (CC == ISD::SETUNE)
3752 CC = ISD::SETNE;
3753
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003754 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003755 SDValue ARMcc;
3756 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003757 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3758 bitcastf32Toi32(LHS, DAG), Mask);
3759 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3760 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003761 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3762 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3763 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3764 Chain, Dest, ARMcc, CCR, Cmp);
3765 }
3766
3767 SDValue LHS1, LHS2;
3768 SDValue RHS1, RHS2;
3769 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3770 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003771 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3772 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003773 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003774 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003775 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003776 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003777 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003778 }
3779
3780 return SDValue();
3781}
3782
3783SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3784 SDValue Chain = Op.getOperand(0);
3785 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3786 SDValue LHS = Op.getOperand(2);
3787 SDValue RHS = Op.getOperand(3);
3788 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003789 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003790
Oliver Stannard51b1d462014-08-21 12:50:31 +00003791 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3792 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3793 dl);
3794
3795 // If softenSetCCOperands only returned one value, we should compare it to
3796 // zero.
3797 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003798 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003799 CC = ISD::SETNE;
3800 }
3801 }
3802
Owen Anderson9f944592009-08-11 20:47:22 +00003803 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003804 SDValue ARMcc;
3805 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003806 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003807 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003808 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003809 }
3810
Owen Anderson9f944592009-08-11 20:47:22 +00003811 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003812
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003813 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003814 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3815 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3816 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3817 if (Result.getNode())
3818 return Result;
3819 }
3820
Evan Cheng10043e22007-01-19 07:51:42 +00003821 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003822 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003823
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003824 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003825 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003826 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003827 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003828 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003829 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003830 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003831 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003832 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003833 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003834 }
3835 return Res;
3836}
3837
Dan Gohman21cea8a2010-04-17 15:26:15 +00003838SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003839 SDValue Chain = Op.getOperand(0);
3840 SDValue Table = Op.getOperand(1);
3841 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003842 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003843
Mehdi Amini44ede332015-07-09 02:09:04 +00003844 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003845 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003846 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003847 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003848 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003849 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003850 if (Subtarget->isThumb2()) {
3851 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3852 // which does another jump to the destination. This also makes it easier
3853 // to translate it to TBB / TBH later.
3854 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003855 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003856 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003857 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003858 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003859 Addr =
3860 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
3861 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3862 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003863 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003864 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003865 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003866 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003867 Addr =
3868 DAG.getLoad(PTy, dl, Chain, Addr,
3869 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3870 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003871 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003872 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003873 }
Evan Cheng10043e22007-01-19 07:51:42 +00003874}
3875
Eli Friedman2d4055b2011-11-09 23:36:02 +00003876static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003877 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003878 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003879
James Molloy547d4c02012-02-20 09:24:05 +00003880 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3881 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3882 return Op;
3883 return DAG.UnrollVectorOp(Op.getNode());
3884 }
3885
3886 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3887 "Invalid type for custom lowering!");
3888 if (VT != MVT::v4i16)
3889 return DAG.UnrollVectorOp(Op.getNode());
3890
3891 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3892 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003893}
3894
Oliver Stannard51b1d462014-08-21 12:50:31 +00003895SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003896 EVT VT = Op.getValueType();
3897 if (VT.isVector())
3898 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003899 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3900 RTLIB::Libcall LC;
3901 if (Op.getOpcode() == ISD::FP_TO_SINT)
3902 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3903 Op.getValueType());
3904 else
3905 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3906 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00003907 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00003908 /*isSigned*/ false, SDLoc(Op)).first;
3909 }
3910
James Molloyfa041152015-03-23 16:15:16 +00003911 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003912}
3913
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003914static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3915 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003916 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003917
Eli Friedman2d4055b2011-11-09 23:36:02 +00003918 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3919 if (VT.getVectorElementType() == MVT::f32)
3920 return Op;
3921 return DAG.UnrollVectorOp(Op.getNode());
3922 }
3923
Duncan Sandsa41634e2011-08-12 14:54:45 +00003924 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3925 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003926 if (VT != MVT::v4f32)
3927 return DAG.UnrollVectorOp(Op.getNode());
3928
3929 unsigned CastOpc;
3930 unsigned Opc;
3931 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003932 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003933 case ISD::SINT_TO_FP:
3934 CastOpc = ISD::SIGN_EXTEND;
3935 Opc = ISD::SINT_TO_FP;
3936 break;
3937 case ISD::UINT_TO_FP:
3938 CastOpc = ISD::ZERO_EXTEND;
3939 Opc = ISD::UINT_TO_FP;
3940 break;
3941 }
3942
3943 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3944 return DAG.getNode(Opc, dl, VT, Op);
3945}
3946
Oliver Stannard51b1d462014-08-21 12:50:31 +00003947SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003948 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003949 if (VT.isVector())
3950 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003951 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3952 RTLIB::Libcall LC;
3953 if (Op.getOpcode() == ISD::SINT_TO_FP)
3954 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3955 Op.getValueType());
3956 else
3957 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3958 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00003959 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00003960 /*isSigned*/ false, SDLoc(Op)).first;
3961 }
3962
James Molloyfa041152015-03-23 16:15:16 +00003963 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003964}
3965
Evan Cheng25f93642010-07-08 02:08:50 +00003966SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003967 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003968 SDValue Tmp0 = Op.getOperand(0);
3969 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003970 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003971 EVT VT = Op.getValueType();
3972 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003973 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3974 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3975 bool UseNEON = !InGPR && Subtarget->hasNEON();
3976
3977 if (UseNEON) {
3978 // Use VBSL to copy the sign bit.
3979 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3980 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003981 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003982 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3983 if (VT == MVT::f64)
3984 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3985 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003986 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003987 else /*if (VT == MVT::f32)*/
3988 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3989 if (SrcVT == MVT::f32) {
3990 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3991 if (VT == MVT::f64)
3992 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3993 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003994 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003995 } else if (VT == MVT::f32)
3996 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3997 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003998 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003999 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4000 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4001
4002 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004003 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004004 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4005 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4006 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004007
Evan Chengd6b641e2011-02-23 02:24:55 +00004008 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4009 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4010 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004011 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004012 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4013 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004014 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004015 } else {
4016 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4017 }
4018
4019 return Res;
4020 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004021
4022 // Bitcast operand 1 to i32.
4023 if (SrcVT == MVT::f64)
4024 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004025 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004026 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4027
Evan Chengd6b641e2011-02-23 02:24:55 +00004028 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004029 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4030 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004031 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4032 if (VT == MVT::f32) {
4033 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4034 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4035 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4036 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004037 }
4038
Evan Chengd6b641e2011-02-23 02:24:55 +00004039 // f64: Or the high part with signbit and then combine two parts.
4040 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004041 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004042 SDValue Lo = Tmp0.getValue(0);
4043 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4044 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4045 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004046}
4047
Evan Cheng168ced92010-05-22 01:47:14 +00004048SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4049 MachineFunction &MF = DAG.getMachineFunction();
4050 MachineFrameInfo *MFI = MF.getFrameInfo();
4051 MFI->setReturnAddressIsTaken(true);
4052
Bill Wendling908bf812014-01-06 00:43:20 +00004053 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004054 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004055
Evan Cheng168ced92010-05-22 01:47:14 +00004056 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004057 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004058 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4059 if (Depth) {
4060 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004061 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004062 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4063 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004064 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004065 }
4066
4067 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004068 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004069 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4070}
4071
Dan Gohman21cea8a2010-04-17 15:26:15 +00004072SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004073 const ARMBaseRegisterInfo &ARI =
4074 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4075 MachineFunction &MF = DAG.getMachineFunction();
4076 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004077 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004078
Owen Anderson53aa7a92009-08-10 22:56:29 +00004079 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004080 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004081 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004082 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004083 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4084 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004085 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4086 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004087 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004088 return FrameAddr;
4089}
4090
Renato Golinc7aea402014-05-06 16:51:25 +00004091// FIXME? Maybe this could be a TableGen attribute on some registers and
4092// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004093unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4094 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004095 unsigned Reg = StringSwitch<unsigned>(RegName)
4096 .Case("sp", ARM::SP)
4097 .Default(0);
4098 if (Reg)
4099 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004100 report_fatal_error(Twine("Invalid register name \""
4101 + StringRef(RegName) + "\"."));
4102}
4103
4104// Result is 64 bit value so split into two 32 bit values and return as a
4105// pair of values.
4106static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4107 SelectionDAG &DAG) {
4108 SDLoc DL(N);
4109
4110 // This function is only supposed to be called for i64 type destination.
4111 assert(N->getValueType(0) == MVT::i64
4112 && "ExpandREAD_REGISTER called for non-i64 type result.");
4113
4114 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4115 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4116 N->getOperand(0),
4117 N->getOperand(1));
4118
4119 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4120 Read.getValue(1)));
4121 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004122}
4123
Wesley Peck527da1b2010-11-23 03:31:01 +00004124/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004125/// expand a bit convert where either the source or destination type is i64 to
4126/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4127/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4128/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004129static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004130 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004131 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004132 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004133
Bob Wilson59b70ea2010-04-17 05:30:19 +00004134 // This function is only supposed to be called for i64 types, either as the
4135 // source or destination of the bit convert.
4136 EVT SrcVT = Op.getValueType();
4137 EVT DstVT = N->getValueType(0);
4138 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004139 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004140
Bob Wilson59b70ea2010-04-17 05:30:19 +00004141 // Turn i64->f64 into VMOVDRR.
4142 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004143 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004144 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004145 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004146 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004147 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004148 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004149 }
Bob Wilson7117a912009-03-20 22:42:55 +00004150
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004151 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004152 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004153 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004154 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004155 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004156 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4157 DAG.getVTList(MVT::i32, MVT::i32),
4158 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4159 else
4160 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4161 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004162 // Merge the pieces into a single i64 value.
4163 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4164 }
Bob Wilson7117a912009-03-20 22:42:55 +00004165
Bob Wilson59b70ea2010-04-17 05:30:19 +00004166 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004167}
4168
Bob Wilson2e076c42009-06-22 23:27:02 +00004169/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004170/// Zero vectors are used to represent vector negation and in those cases
4171/// will be implemented with the NEON VNEG instruction. However, VNEG does
4172/// not support i64 elements, so sometimes the zero vectors will need to be
4173/// explicitly constructed. Regardless, use a canonical VMOV to create the
4174/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004175static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004176 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004177 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004178 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004179 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4180 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004181 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004182}
4183
Jim Grosbach624fcb22009-10-31 21:00:56 +00004184/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4185/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004186SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4187 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004188 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4189 EVT VT = Op.getValueType();
4190 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004191 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004192 SDValue ShOpLo = Op.getOperand(0);
4193 SDValue ShOpHi = Op.getOperand(1);
4194 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004195 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004196 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004197
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004198 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4199
Jim Grosbach624fcb22009-10-31 21:00:56 +00004200 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004201 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004202 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4203 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004204 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004205 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4206 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004207 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004208
4209 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004210 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4211 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004212 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004213 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004214 CCR, Cmp);
4215
4216 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004217 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004218}
4219
Jim Grosbach5d994042009-10-31 19:38:01 +00004220/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4221/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004222SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4223 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004224 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4225 EVT VT = Op.getValueType();
4226 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004227 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004228 SDValue ShOpLo = Op.getOperand(0);
4229 SDValue ShOpHi = Op.getOperand(1);
4230 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004231 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004232
4233 assert(Op.getOpcode() == ISD::SHL_PARTS);
4234 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004235 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004236 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4237 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004238 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004239 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4240 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4241
4242 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4243 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004244 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4245 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004246 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004247 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004248 CCR, Cmp);
4249
4250 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004251 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004252}
4253
Jim Grosbach535d3b42010-09-08 03:54:02 +00004254SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004255 SelectionDAG &DAG) const {
4256 // The rounding mode is in bits 23:22 of the FPSCR.
4257 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4258 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4259 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004260 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004261 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004262 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004263 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004264 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004265 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004266 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004267 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004268 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004269 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004270}
4271
Jim Grosbach8546ec92010-01-18 19:58:49 +00004272static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4273 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004274 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004275 EVT VT = N->getValueType(0);
4276 if (VT.isVector()) {
4277 assert(ST->hasNEON());
4278
4279 // Compute the least significant set bit: LSB = X & -X
4280 SDValue X = N->getOperand(0);
4281 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4282 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4283
4284 EVT ElemTy = VT.getVectorElementType();
4285
4286 if (ElemTy == MVT::i8) {
4287 // Compute with: cttz(x) = ctpop(lsb - 1)
4288 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4289 DAG.getTargetConstant(1, dl, ElemTy));
4290 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4291 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4292 }
4293
4294 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4295 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4296 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4297 unsigned NumBits = ElemTy.getSizeInBits();
4298 SDValue WidthMinus1 =
4299 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4300 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4301 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4302 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4303 }
4304
4305 // Compute with: cttz(x) = ctpop(lsb - 1)
4306
4307 // Since we can only compute the number of bits in a byte with vcnt.8, we
4308 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4309 // and i64.
4310
4311 // Compute LSB - 1.
4312 SDValue Bits;
4313 if (ElemTy == MVT::i64) {
4314 // Load constant 0xffff'ffff'ffff'ffff to register.
4315 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4316 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4317 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4318 } else {
4319 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4320 DAG.getTargetConstant(1, dl, ElemTy));
4321 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4322 }
4323
4324 // Count #bits with vcnt.8.
4325 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4326 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4327 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4328
4329 // Gather the #bits with vpaddl (pairwise add.)
4330 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4331 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4332 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4333 Cnt8);
4334 if (ElemTy == MVT::i16)
4335 return Cnt16;
4336
4337 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4338 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4339 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4340 Cnt16);
4341 if (ElemTy == MVT::i32)
4342 return Cnt32;
4343
4344 assert(ElemTy == MVT::i64);
4345 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4346 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4347 Cnt32);
4348 return Cnt64;
4349 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004350
4351 if (!ST->hasV6T2Ops())
4352 return SDValue();
4353
4354 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4355 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4356}
4357
Evan Chengb4eae132012-12-04 22:41:50 +00004358/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4359/// for each 16-bit element from operand, repeated. The basic idea is to
4360/// leverage vcnt to get the 8-bit counts, gather and add the results.
4361///
4362/// Trace for v4i16:
4363/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4364/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4365/// 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 +00004366/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004367/// [b0 b1 b2 b3 b4 b5 b6 b7]
4368/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4369/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4370/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4371static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4372 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004373 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004374
4375 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4376 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4377 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4378 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4379 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4380 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4381}
4382
4383/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4384/// bit-count for each 16-bit element from the operand. We need slightly
4385/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4386/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004387///
Evan Chengb4eae132012-12-04 22:41:50 +00004388/// Trace for v4i16:
4389/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4390/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4391/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4392/// v4i16:Extracted = [k0 k1 k2 k3 ]
4393static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4394 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004395 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004396
4397 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4398 if (VT.is64BitVector()) {
4399 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4400 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004401 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004402 } else {
4403 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004404 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004405 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4406 }
4407}
4408
4409/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4410/// bit-count for each 32-bit element from the operand. The idea here is
4411/// to split the vector into 16-bit elements, leverage the 16-bit count
4412/// routine, and then combine the results.
4413///
4414/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4415/// input = [v0 v1 ] (vi: 32-bit elements)
4416/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4417/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004418/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004419/// [k0 k1 k2 k3 ]
4420/// N1 =+[k1 k0 k3 k2 ]
4421/// [k0 k2 k1 k3 ]
4422/// N2 =+[k1 k3 k0 k2 ]
4423/// [k0 k2 k1 k3 ]
4424/// Extended =+[k1 k3 k0 k2 ]
4425/// [k0 k2 ]
4426/// Extracted=+[k1 k3 ]
4427///
4428static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4429 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004430 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004431
4432 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4433
4434 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4435 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4436 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4437 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4438 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4439
4440 if (VT.is64BitVector()) {
4441 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4442 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004443 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004444 } else {
4445 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004446 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004447 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4448 }
4449}
4450
4451static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4452 const ARMSubtarget *ST) {
4453 EVT VT = N->getValueType(0);
4454
4455 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004456 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4457 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004458 "Unexpected type for custom ctpop lowering");
4459
4460 if (VT.getVectorElementType() == MVT::i32)
4461 return lowerCTPOP32BitElements(N, DAG);
4462 else
4463 return lowerCTPOP16BitElements(N, DAG);
4464}
4465
Bob Wilson2e076c42009-06-22 23:27:02 +00004466static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4467 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004468 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004469 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004470
Bob Wilson7d471332010-11-18 21:16:28 +00004471 if (!VT.isVector())
4472 return SDValue();
4473
Bob Wilson2e076c42009-06-22 23:27:02 +00004474 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004475 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004476
Bob Wilson7d471332010-11-18 21:16:28 +00004477 // Left shifts translate directly to the vshiftu intrinsic.
4478 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004479 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004480 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4481 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004482 N->getOperand(0), N->getOperand(1));
4483
4484 assert((N->getOpcode() == ISD::SRA ||
4485 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4486
4487 // NEON uses the same intrinsics for both left and right shifts. For
4488 // right shifts, the shift amounts are negative, so negate the vector of
4489 // shift amounts.
4490 EVT ShiftVT = N->getOperand(1).getValueType();
4491 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4492 getZeroVector(ShiftVT, DAG, dl),
4493 N->getOperand(1));
4494 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4495 Intrinsic::arm_neon_vshifts :
4496 Intrinsic::arm_neon_vshiftu);
4497 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004498 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004499 N->getOperand(0), NegatedCount);
4500}
4501
4502static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4503 const ARMSubtarget *ST) {
4504 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004505 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004506
Eli Friedman682d8c12009-08-22 03:13:10 +00004507 // We can get here for a node like i32 = ISD::SHL i32, i64
4508 if (VT != MVT::i64)
4509 return SDValue();
4510
4511 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004512 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004513
Chris Lattnerf81d5882007-11-24 07:07:01 +00004514 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4515 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004516 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004517 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004518
Chris Lattnerf81d5882007-11-24 07:07:01 +00004519 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004520 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004521
Chris Lattnerf81d5882007-11-24 07:07:01 +00004522 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004523 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004524 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004525 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004526 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004527
Chris Lattnerf81d5882007-11-24 07:07:01 +00004528 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4529 // captures the result into a carry flag.
4530 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004531 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004532
Chris Lattnerf81d5882007-11-24 07:07:01 +00004533 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004534 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004535
Chris Lattnerf81d5882007-11-24 07:07:01 +00004536 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004537 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004538}
4539
Bob Wilson2e076c42009-06-22 23:27:02 +00004540static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4541 SDValue TmpOp0, TmpOp1;
4542 bool Invert = false;
4543 bool Swap = false;
4544 unsigned Opc = 0;
4545
4546 SDValue Op0 = Op.getOperand(0);
4547 SDValue Op1 = Op.getOperand(1);
4548 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004549 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004550 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004551 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004552 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004553
James Molloybf170092015-08-20 16:33:44 +00004554 if (CmpVT.getVectorElementType() == MVT::i64)
4555 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4556 // but it's possible that our operands are 64-bit but our result is 32-bit.
4557 // Bail in this case.
4558 return SDValue();
4559
Oliver Stannard51b1d462014-08-21 12:50:31 +00004560 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004561 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004562 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004563 case ISD::SETUNE:
4564 case ISD::SETNE: Invert = true; // Fallthrough
4565 case ISD::SETOEQ:
4566 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4567 case ISD::SETOLT:
4568 case ISD::SETLT: Swap = true; // Fallthrough
4569 case ISD::SETOGT:
4570 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4571 case ISD::SETOLE:
4572 case ISD::SETLE: Swap = true; // Fallthrough
4573 case ISD::SETOGE:
4574 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4575 case ISD::SETUGE: Swap = true; // Fallthrough
4576 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4577 case ISD::SETUGT: Swap = true; // Fallthrough
4578 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4579 case ISD::SETUEQ: Invert = true; // Fallthrough
4580 case ISD::SETONE:
4581 // Expand this to (OLT | OGT).
4582 TmpOp0 = Op0;
4583 TmpOp1 = Op1;
4584 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004585 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4586 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004587 break;
4588 case ISD::SETUO: Invert = true; // Fallthrough
4589 case ISD::SETO:
4590 // Expand this to (OLT | OGE).
4591 TmpOp0 = Op0;
4592 TmpOp1 = Op1;
4593 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004594 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4595 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004596 break;
4597 }
4598 } else {
4599 // Integer comparisons.
4600 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004601 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004602 case ISD::SETNE: Invert = true;
4603 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4604 case ISD::SETLT: Swap = true;
4605 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4606 case ISD::SETLE: Swap = true;
4607 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4608 case ISD::SETULT: Swap = true;
4609 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4610 case ISD::SETULE: Swap = true;
4611 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4612 }
4613
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004614 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004615 if (Opc == ARMISD::VCEQ) {
4616
4617 SDValue AndOp;
4618 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4619 AndOp = Op0;
4620 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4621 AndOp = Op1;
4622
4623 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004624 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004625 AndOp = AndOp.getOperand(0);
4626
4627 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4628 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004629 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4630 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004631 Invert = !Invert;
4632 }
4633 }
4634 }
4635
4636 if (Swap)
4637 std::swap(Op0, Op1);
4638
Owen Andersonc7baee32010-11-08 23:21:22 +00004639 // If one of the operands is a constant vector zero, attempt to fold the
4640 // comparison to a specialized compare-against-zero form.
4641 SDValue SingleOp;
4642 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4643 SingleOp = Op0;
4644 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4645 if (Opc == ARMISD::VCGE)
4646 Opc = ARMISD::VCLEZ;
4647 else if (Opc == ARMISD::VCGT)
4648 Opc = ARMISD::VCLTZ;
4649 SingleOp = Op1;
4650 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004651
Owen Andersonc7baee32010-11-08 23:21:22 +00004652 SDValue Result;
4653 if (SingleOp.getNode()) {
4654 switch (Opc) {
4655 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004656 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004657 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004658 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004659 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004660 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004661 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004662 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004663 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004664 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004665 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004666 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004667 }
4668 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004669 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004670 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004671
Tim Northover45aa89c2015-02-08 00:50:47 +00004672 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4673
Bob Wilson2e076c42009-06-22 23:27:02 +00004674 if (Invert)
4675 Result = DAG.getNOT(dl, Result, VT);
4676
4677 return Result;
4678}
4679
Bob Wilson5b2b5042010-06-14 22:19:57 +00004680/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4681/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004682/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004683static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4684 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004685 SDLoc dl, EVT &VT, bool is128Bits,
4686 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004687 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004688
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004689 // SplatBitSize is set to the smallest size that splats the vector, so a
4690 // zero vector will always have SplatBitSize == 8. However, NEON modified
4691 // immediate instructions others than VMOV do not support the 8-bit encoding
4692 // of a zero vector, and the default encoding of zero is supposed to be the
4693 // 32-bit version.
4694 if (SplatBits == 0)
4695 SplatBitSize = 32;
4696
Bob Wilson2e076c42009-06-22 23:27:02 +00004697 switch (SplatBitSize) {
4698 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004699 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004700 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004701 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004702 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004703 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004704 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004705 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004706 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004707
4708 case 16:
4709 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004710 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004711 if ((SplatBits & ~0xff) == 0) {
4712 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004713 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004714 Imm = SplatBits;
4715 break;
4716 }
4717 if ((SplatBits & ~0xff00) == 0) {
4718 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004719 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004720 Imm = SplatBits >> 8;
4721 break;
4722 }
4723 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004724
4725 case 32:
4726 // NEON's 32-bit VMOV supports splat values where:
4727 // * only one byte is nonzero, or
4728 // * the least significant byte is 0xff and the second byte is nonzero, or
4729 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004730 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004731 if ((SplatBits & ~0xff) == 0) {
4732 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004733 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004734 Imm = SplatBits;
4735 break;
4736 }
4737 if ((SplatBits & ~0xff00) == 0) {
4738 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004739 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004740 Imm = SplatBits >> 8;
4741 break;
4742 }
4743 if ((SplatBits & ~0xff0000) == 0) {
4744 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004745 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004746 Imm = SplatBits >> 16;
4747 break;
4748 }
4749 if ((SplatBits & ~0xff000000) == 0) {
4750 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004751 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004752 Imm = SplatBits >> 24;
4753 break;
4754 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004755
Owen Andersona4076922010-11-05 21:57:54 +00004756 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4757 if (type == OtherModImm) return SDValue();
4758
Bob Wilson2e076c42009-06-22 23:27:02 +00004759 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004760 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4761 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004762 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004763 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004764 break;
4765 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004766
4767 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004768 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4769 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004770 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004771 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004772 break;
4773 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004774
4775 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4776 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4777 // VMOV.I32. A (very) minor optimization would be to replicate the value
4778 // and fall through here to test for a valid 64-bit splat. But, then the
4779 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004780 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004781
4782 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004783 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004784 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004785 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004786 uint64_t BitMask = 0xff;
4787 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004788 unsigned ImmMask = 1;
4789 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004790 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004791 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004792 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004793 Imm |= ImmMask;
4794 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004795 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004796 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004797 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004798 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004799 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004800
Mehdi Aminiffc14022015-07-08 01:00:38 +00004801 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004802 // swap higher and lower 32 bit word
4803 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4804
Bob Wilson6eae5202010-06-11 21:34:50 +00004805 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004806 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004807 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004808 break;
4809 }
4810
Bob Wilson6eae5202010-06-11 21:34:50 +00004811 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004812 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004813 }
4814
Bob Wilsona3f19012010-07-13 21:16:48 +00004815 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004816 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004817}
4818
Lang Hames591cdaf2012-03-29 21:56:11 +00004819SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4820 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004821 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004822 return SDValue();
4823
Tim Northoverf79c3a52013-08-20 08:57:11 +00004824 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004825 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004826
Oliver Stannard51b1d462014-08-21 12:50:31 +00004827 // Use the default (constant pool) lowering for double constants when we have
4828 // an SP-only FPU
4829 if (IsDouble && Subtarget->isFPOnlySP())
4830 return SDValue();
4831
Lang Hames591cdaf2012-03-29 21:56:11 +00004832 // Try splatting with a VMOV.f32...
4833 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004834 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4835
Lang Hames591cdaf2012-03-29 21:56:11 +00004836 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004837 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4838 // We have code in place to select a valid ConstantFP already, no need to
4839 // do any mangling.
4840 return Op;
4841 }
4842
4843 // It's a float and we are trying to use NEON operations where
4844 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004845 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004846 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004847 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4848 NewVal);
4849 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004850 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004851 }
4852
Tim Northoverf79c3a52013-08-20 08:57:11 +00004853 // The rest of our options are NEON only, make sure that's allowed before
4854 // proceeding..
4855 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4856 return SDValue();
4857
Lang Hames591cdaf2012-03-29 21:56:11 +00004858 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004859 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4860
4861 // It wouldn't really be worth bothering for doubles except for one very
4862 // important value, which does happen to match: 0.0. So make sure we don't do
4863 // anything stupid.
4864 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4865 return SDValue();
4866
4867 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004868 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4869 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004870 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004871 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004872 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4873 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004874 if (IsDouble)
4875 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4876
4877 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004878 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4879 VecConstant);
4880 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004881 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004882 }
4883
4884 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004885 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004886 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004887 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004888 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004889 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004890
4891 if (IsDouble)
4892 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4893
4894 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004895 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4896 VecConstant);
4897 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004898 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004899 }
4900
4901 return SDValue();
4902}
4903
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004904// check if an VEXT instruction can handle the shuffle mask when the
4905// vector sources of the shuffle are the same.
4906static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4907 unsigned NumElts = VT.getVectorNumElements();
4908
4909 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4910 if (M[0] < 0)
4911 return false;
4912
4913 Imm = M[0];
4914
4915 // If this is a VEXT shuffle, the immediate value is the index of the first
4916 // element. The other shuffle indices must be the successive elements after
4917 // the first one.
4918 unsigned ExpectedElt = Imm;
4919 for (unsigned i = 1; i < NumElts; ++i) {
4920 // Increment the expected index. If it wraps around, just follow it
4921 // back to index zero and keep going.
4922 ++ExpectedElt;
4923 if (ExpectedElt == NumElts)
4924 ExpectedElt = 0;
4925
4926 if (M[i] < 0) continue; // ignore UNDEF indices
4927 if (ExpectedElt != static_cast<unsigned>(M[i]))
4928 return false;
4929 }
4930
4931 return true;
4932}
4933
Lang Hames591cdaf2012-03-29 21:56:11 +00004934
Benjamin Kramer339ced42012-01-15 13:16:05 +00004935static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004936 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004937 unsigned NumElts = VT.getVectorNumElements();
4938 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004939
4940 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4941 if (M[0] < 0)
4942 return false;
4943
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004944 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004945
4946 // If this is a VEXT shuffle, the immediate value is the index of the first
4947 // element. The other shuffle indices must be the successive elements after
4948 // the first one.
4949 unsigned ExpectedElt = Imm;
4950 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004951 // Increment the expected index. If it wraps around, it may still be
4952 // a VEXT but the source vectors must be swapped.
4953 ExpectedElt += 1;
4954 if (ExpectedElt == NumElts * 2) {
4955 ExpectedElt = 0;
4956 ReverseVEXT = true;
4957 }
4958
Bob Wilson411dfad2010-08-17 05:54:34 +00004959 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004960 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004961 return false;
4962 }
4963
4964 // Adjust the index value if the source operands will be swapped.
4965 if (ReverseVEXT)
4966 Imm -= NumElts;
4967
Bob Wilson32cd8552009-08-19 17:03:43 +00004968 return true;
4969}
4970
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004971/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4972/// instruction with the specified blocksize. (The order of the elements
4973/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004974static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004975 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4976 "Only possible block sizes for VREV are: 16, 32, 64");
4977
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004978 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004979 if (EltSz == 64)
4980 return false;
4981
4982 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004983 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004984 // If the first shuffle index is UNDEF, be optimistic.
4985 if (M[0] < 0)
4986 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004987
4988 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4989 return false;
4990
4991 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004992 if (M[i] < 0) continue; // ignore UNDEF indices
4993 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004994 return false;
4995 }
4996
4997 return true;
4998}
4999
Benjamin Kramer339ced42012-01-15 13:16:05 +00005000static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005001 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5002 // range, then 0 is placed into the resulting vector. So pretty much any mask
5003 // of 8 elements can work here.
5004 return VT == MVT::v8i8 && M.size() == 8;
5005}
5006
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005007// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5008// checking that pairs of elements in the shuffle mask represent the same index
5009// in each vector, incrementing the expected index by 2 at each step.
5010// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5011// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5012// v2={e,f,g,h}
5013// WhichResult gives the offset for each element in the mask based on which
5014// of the two results it belongs to.
5015//
5016// The transpose can be represented either as:
5017// result1 = shufflevector v1, v2, result1_shuffle_mask
5018// result2 = shufflevector v1, v2, result2_shuffle_mask
5019// where v1/v2 and the shuffle masks have the same number of elements
5020// (here WhichResult (see below) indicates which result is being checked)
5021//
5022// or as:
5023// results = shufflevector v1, v2, shuffle_mask
5024// where both results are returned in one vector and the shuffle mask has twice
5025// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5026// want to check the low half and high half of the shuffle mask as if it were
5027// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005028static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005029 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5030 if (EltSz == 64)
5031 return false;
5032
Bob Wilsona7062312009-08-21 20:54:19 +00005033 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005034 if (M.size() != NumElts && M.size() != NumElts*2)
5035 return false;
5036
James Molloy8c995a92015-09-10 08:42:28 +00005037 // If the mask is twice as long as the input vector then we need to check the
5038 // upper and lower parts of the mask with a matching value for WhichResult
5039 // FIXME: A mask with only even values will be rejected in case the first
5040 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5041 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005042 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005043 if (M.size() == NumElts * 2)
5044 WhichResult = i / NumElts;
5045 else
5046 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005047 for (unsigned j = 0; j < NumElts; j += 2) {
5048 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5049 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5050 return false;
5051 }
Bob Wilsona7062312009-08-21 20:54:19 +00005052 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005053
5054 if (M.size() == NumElts*2)
5055 WhichResult = 0;
5056
Bob Wilsona7062312009-08-21 20:54:19 +00005057 return true;
5058}
5059
Bob Wilson0bbd3072009-12-03 06:40:55 +00005060/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5061/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5062/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005063static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005064 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5065 if (EltSz == 64)
5066 return false;
5067
5068 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005069 if (M.size() != NumElts && M.size() != NumElts*2)
5070 return false;
5071
5072 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005073 if (M.size() == NumElts * 2)
5074 WhichResult = i / NumElts;
5075 else
5076 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005077 for (unsigned j = 0; j < NumElts; j += 2) {
5078 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5079 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5080 return false;
5081 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005082 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005083
5084 if (M.size() == NumElts*2)
5085 WhichResult = 0;
5086
Bob Wilson0bbd3072009-12-03 06:40:55 +00005087 return true;
5088}
5089
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005090// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5091// that the mask elements are either all even and in steps of size 2 or all odd
5092// and in steps of size 2.
5093// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5094// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5095// v2={e,f,g,h}
5096// Requires similar checks to that of isVTRNMask with
5097// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005098static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005099 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5100 if (EltSz == 64)
5101 return false;
5102
Bob Wilsona7062312009-08-21 20:54:19 +00005103 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005104 if (M.size() != NumElts && M.size() != NumElts*2)
5105 return false;
5106
5107 for (unsigned i = 0; i < M.size(); i += NumElts) {
5108 WhichResult = M[i] == 0 ? 0 : 1;
5109 for (unsigned j = 0; j < NumElts; ++j) {
5110 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5111 return false;
5112 }
Bob Wilsona7062312009-08-21 20:54:19 +00005113 }
5114
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005115 if (M.size() == NumElts*2)
5116 WhichResult = 0;
5117
Bob Wilsona7062312009-08-21 20:54:19 +00005118 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005119 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005120 return false;
5121
5122 return true;
5123}
5124
Bob Wilson0bbd3072009-12-03 06:40:55 +00005125/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5126/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5127/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005128static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005129 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5130 if (EltSz == 64)
5131 return false;
5132
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005133 unsigned NumElts = VT.getVectorNumElements();
5134 if (M.size() != NumElts && M.size() != NumElts*2)
5135 return false;
5136
5137 unsigned Half = NumElts / 2;
5138 for (unsigned i = 0; i < M.size(); i += NumElts) {
5139 WhichResult = M[i] == 0 ? 0 : 1;
5140 for (unsigned j = 0; j < NumElts; j += Half) {
5141 unsigned Idx = WhichResult;
5142 for (unsigned k = 0; k < Half; ++k) {
5143 int MIdx = M[i + j + k];
5144 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5145 return false;
5146 Idx += 2;
5147 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005148 }
5149 }
5150
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005151 if (M.size() == NumElts*2)
5152 WhichResult = 0;
5153
Bob Wilson0bbd3072009-12-03 06:40:55 +00005154 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5155 if (VT.is64BitVector() && EltSz == 32)
5156 return false;
5157
5158 return true;
5159}
5160
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005161// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5162// that pairs of elements of the shufflemask represent the same index in each
5163// vector incrementing sequentially through the vectors.
5164// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5165// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5166// v2={e,f,g,h}
5167// Requires similar checks to that of isVTRNMask with respect the how results
5168// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005169static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005170 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5171 if (EltSz == 64)
5172 return false;
5173
Bob Wilsona7062312009-08-21 20:54:19 +00005174 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005175 if (M.size() != NumElts && M.size() != NumElts*2)
5176 return false;
5177
5178 for (unsigned i = 0; i < M.size(); i += NumElts) {
5179 WhichResult = M[i] == 0 ? 0 : 1;
5180 unsigned Idx = WhichResult * NumElts / 2;
5181 for (unsigned j = 0; j < NumElts; j += 2) {
5182 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5183 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5184 return false;
5185 Idx += 1;
5186 }
Bob Wilsona7062312009-08-21 20:54:19 +00005187 }
5188
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005189 if (M.size() == NumElts*2)
5190 WhichResult = 0;
5191
Bob Wilsona7062312009-08-21 20:54:19 +00005192 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005193 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005194 return false;
5195
5196 return true;
5197}
5198
Bob Wilson0bbd3072009-12-03 06:40:55 +00005199/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5200/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5201/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005202static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005203 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5204 if (EltSz == 64)
5205 return false;
5206
5207 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005208 if (M.size() != NumElts && M.size() != NumElts*2)
5209 return false;
5210
5211 for (unsigned i = 0; i < M.size(); i += NumElts) {
5212 WhichResult = M[i] == 0 ? 0 : 1;
5213 unsigned Idx = WhichResult * NumElts / 2;
5214 for (unsigned j = 0; j < NumElts; j += 2) {
5215 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5216 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5217 return false;
5218 Idx += 1;
5219 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005220 }
5221
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005222 if (M.size() == NumElts*2)
5223 WhichResult = 0;
5224
Bob Wilson0bbd3072009-12-03 06:40:55 +00005225 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5226 if (VT.is64BitVector() && EltSz == 32)
5227 return false;
5228
5229 return true;
5230}
5231
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005232/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5233/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5234static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5235 unsigned &WhichResult,
5236 bool &isV_UNDEF) {
5237 isV_UNDEF = false;
5238 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5239 return ARMISD::VTRN;
5240 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5241 return ARMISD::VUZP;
5242 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5243 return ARMISD::VZIP;
5244
5245 isV_UNDEF = true;
5246 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5247 return ARMISD::VTRN;
5248 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5249 return ARMISD::VUZP;
5250 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5251 return ARMISD::VZIP;
5252
5253 return 0;
5254}
5255
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005256/// \return true if this is a reverse operation on an vector.
5257static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5258 unsigned NumElts = VT.getVectorNumElements();
5259 // Make sure the mask has the right size.
5260 if (NumElts != M.size())
5261 return false;
5262
5263 // Look for <15, ..., 3, -1, 1, 0>.
5264 for (unsigned i = 0; i != NumElts; ++i)
5265 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5266 return false;
5267
5268 return true;
5269}
5270
Dale Johannesen2bff5052010-07-29 20:10:08 +00005271// If N is an integer constant that can be moved into a register in one
5272// instruction, return an SDValue of such a constant (will become a MOV
5273// instruction). Otherwise return null.
5274static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005275 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005276 uint64_t Val;
5277 if (!isa<ConstantSDNode>(N))
5278 return SDValue();
5279 Val = cast<ConstantSDNode>(N)->getZExtValue();
5280
5281 if (ST->isThumb1Only()) {
5282 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005283 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005284 } else {
5285 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005286 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005287 }
5288 return SDValue();
5289}
5290
Bob Wilson2e076c42009-06-22 23:27:02 +00005291// If this is a case we can't handle, return null and let the default
5292// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005293SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5294 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005295 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005296 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005297 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005298
5299 APInt SplatBits, SplatUndef;
5300 unsigned SplatBitSize;
5301 bool HasAnyUndefs;
5302 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005303 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005304 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005305 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005306 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005307 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005308 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005309 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005310 if (Val.getNode()) {
5311 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005312 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005313 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005314
5315 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005316 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005317 Val = isNEONModifiedImm(NegatedImm,
5318 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005319 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005320 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005321 if (Val.getNode()) {
5322 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005323 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005324 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005325
5326 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005327 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005328 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005329 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005330 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005331 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5332 }
5333 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005334 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005335 }
5336
Bob Wilson91fdf682010-05-22 00:23:12 +00005337 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005338 //
5339 // As an optimisation, even if more than one value is used it may be more
5340 // profitable to splat with one value then change some lanes.
5341 //
5342 // Heuristically we decide to do this if the vector has a "dominant" value,
5343 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005344 unsigned NumElts = VT.getVectorNumElements();
5345 bool isOnlyLowElement = true;
5346 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005347 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005348 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005349
5350 // Map of the number of times a particular SDValue appears in the
5351 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005352 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005353 SDValue Value;
5354 for (unsigned i = 0; i < NumElts; ++i) {
5355 SDValue V = Op.getOperand(i);
5356 if (V.getOpcode() == ISD::UNDEF)
5357 continue;
5358 if (i > 0)
5359 isOnlyLowElement = false;
5360 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5361 isConstant = false;
5362
James Molloy49bdbce2012-09-06 09:55:02 +00005363 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005364 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005365
James Molloy49bdbce2012-09-06 09:55:02 +00005366 // Is this value dominant? (takes up more than half of the lanes)
5367 if (++Count > (NumElts / 2)) {
5368 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005369 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005370 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005371 }
James Molloy49bdbce2012-09-06 09:55:02 +00005372 if (ValueCounts.size() != 1)
5373 usesOnlyOneValue = false;
5374 if (!Value.getNode() && ValueCounts.size() > 0)
5375 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005376
James Molloy49bdbce2012-09-06 09:55:02 +00005377 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005378 return DAG.getUNDEF(VT);
5379
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005380 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5381 // Keep going if we are hitting this case.
5382 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005383 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5384
Dale Johannesen2bff5052010-07-29 20:10:08 +00005385 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5386
Dale Johannesen710a2d92010-10-19 20:00:17 +00005387 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5388 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005389 if (hasDominantValue && EltSize <= 32) {
5390 if (!isConstant) {
5391 SDValue N;
5392
5393 // If we are VDUPing a value that comes directly from a vector, that will
5394 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005395 // just use VDUPLANE. We can only do this if the lane being extracted
5396 // is at a constant index, as the VDUP from lane instructions only have
5397 // constant-index forms.
5398 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5399 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005400 // We need to create a new undef vector to use for the VDUPLANE if the
5401 // size of the vector from which we get the value is different than the
5402 // size of the vector that we need to create. We will insert the element
5403 // such that the register coalescer will remove unnecessary copies.
5404 if (VT != Value->getOperand(0).getValueType()) {
5405 ConstantSDNode *constIndex;
5406 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5407 assert(constIndex && "The index is not a constant!");
5408 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5409 VT.getVectorNumElements();
5410 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5411 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005412 Value, DAG.getConstant(index, dl, MVT::i32)),
5413 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005414 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005415 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005416 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005417 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005418 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5419
5420 if (!usesOnlyOneValue) {
5421 // The dominant value was splatted as 'N', but we now have to insert
5422 // all differing elements.
5423 for (unsigned I = 0; I < NumElts; ++I) {
5424 if (Op.getOperand(I) == Value)
5425 continue;
5426 SmallVector<SDValue, 3> Ops;
5427 Ops.push_back(N);
5428 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005429 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005430 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005431 }
5432 }
5433 return N;
5434 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005435 if (VT.getVectorElementType().isFloatingPoint()) {
5436 SmallVector<SDValue, 8> Ops;
5437 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005438 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005439 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005440 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005441 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005442 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5443 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005444 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005445 }
James Molloy49bdbce2012-09-06 09:55:02 +00005446 if (usesOnlyOneValue) {
5447 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5448 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005449 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005450 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005451 }
5452
5453 // If all elements are constants and the case above didn't get hit, fall back
5454 // to the default expansion, which will generate a load from the constant
5455 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005456 if (isConstant)
5457 return SDValue();
5458
Bob Wilson6f2b8962011-01-07 21:37:30 +00005459 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5460 if (NumElts >= 4) {
5461 SDValue shuffle = ReconstructShuffle(Op, DAG);
5462 if (shuffle != SDValue())
5463 return shuffle;
5464 }
5465
Bob Wilson91fdf682010-05-22 00:23:12 +00005466 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005467 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5468 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005469 if (EltSize >= 32) {
5470 // Do the expansion with floating-point types, since that is what the VFP
5471 // registers are defined to use, and since i64 is not legal.
5472 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5473 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005474 SmallVector<SDValue, 8> Ops;
5475 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005476 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005477 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005478 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005479 }
5480
Jim Grosbach24e102a2013-07-08 18:18:52 +00005481 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5482 // know the default expansion would otherwise fall back on something even
5483 // worse. For a vector with one or two non-undef values, that's
5484 // scalar_to_vector for the elements followed by a shuffle (provided the
5485 // shuffle is valid for the target) and materialization element by element
5486 // on the stack followed by a load for everything else.
5487 if (!isConstant && !usesOnlyOneValue) {
5488 SDValue Vec = DAG.getUNDEF(VT);
5489 for (unsigned i = 0 ; i < NumElts; ++i) {
5490 SDValue V = Op.getOperand(i);
5491 if (V.getOpcode() == ISD::UNDEF)
5492 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005493 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005494 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5495 }
5496 return Vec;
5497 }
5498
Bob Wilson2e076c42009-06-22 23:27:02 +00005499 return SDValue();
5500}
5501
Bob Wilson6f2b8962011-01-07 21:37:30 +00005502// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005503// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005504SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5505 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005506 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005507 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005508 EVT VT = Op.getValueType();
5509 unsigned NumElts = VT.getVectorNumElements();
5510
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005511 struct ShuffleSourceInfo {
5512 SDValue Vec;
5513 unsigned MinElt;
5514 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005515
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005516 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5517 // be compatible with the shuffle we intend to construct. As a result
5518 // ShuffleVec will be some sliding window into the original Vec.
5519 SDValue ShuffleVec;
5520
5521 // Code should guarantee that element i in Vec starts at element "WindowBase
5522 // + i * WindowScale in ShuffleVec".
5523 int WindowBase;
5524 int WindowScale;
5525
5526 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5527 ShuffleSourceInfo(SDValue Vec)
5528 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5529 WindowScale(1) {}
5530 };
5531
5532 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5533 // node.
5534 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005535 for (unsigned i = 0; i < NumElts; ++i) {
5536 SDValue V = Op.getOperand(i);
5537 if (V.getOpcode() == ISD::UNDEF)
5538 continue;
5539 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5540 // A shuffle can only come from building a vector from various
5541 // elements of other vectors.
5542 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005543 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5544 // Furthermore, shuffles require a constant mask, whereas extractelts
5545 // accept variable indices.
5546 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005547 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005548
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005549 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005550 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005551 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5552 if (Source == Sources.end())
5553 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005554
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005555 // Update the minimum and maximum lane number seen.
5556 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5557 Source->MinElt = std::min(Source->MinElt, EltNo);
5558 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005559 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005560
Bob Wilson6f2b8962011-01-07 21:37:30 +00005561 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005562 // are involved.
5563 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005564 return SDValue();
5565
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005566 // Find out the smallest element size among result and two sources, and use
5567 // it as element size to build the shuffle_vector.
5568 EVT SmallestEltTy = VT.getVectorElementType();
5569 for (auto &Source : Sources) {
5570 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5571 if (SrcEltTy.bitsLT(SmallestEltTy))
5572 SmallestEltTy = SrcEltTy;
5573 }
5574 unsigned ResMultiplier =
5575 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5576 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5577 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005578
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005579 // If the source vector is too wide or too narrow, we may nevertheless be able
5580 // to construct a compatible shuffle either by concatenating it with UNDEF or
5581 // extracting a suitable range of elements.
5582 for (auto &Src : Sources) {
5583 EVT SrcVT = Src.ShuffleVec.getValueType();
5584
5585 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005586 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005587
5588 // This stage of the search produces a source with the same element type as
5589 // the original, but with a total width matching the BUILD_VECTOR output.
5590 EVT EltVT = SrcVT.getVectorElementType();
5591 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5592 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5593
5594 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5595 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5596 return SDValue();
5597 // We can pad out the smaller vector for free, so if it's part of a
5598 // shuffle...
5599 Src.ShuffleVec =
5600 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5601 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5602 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005603 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005604
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005605 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5606 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005607
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005608 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005609 // Span too large for a VEXT to cope
5610 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005611 }
5612
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005613 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005614 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005615 Src.ShuffleVec =
5616 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5617 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5618 Src.WindowBase = -NumSrcElts;
5619 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005620 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005621 Src.ShuffleVec =
5622 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5623 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005624 } else {
5625 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005626 SDValue VEXTSrc1 =
5627 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5628 DAG.getConstant(0, dl, MVT::i32));
5629 SDValue VEXTSrc2 =
5630 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5631 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005632
5633 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5634 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005635 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005636 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005637 }
5638 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005639
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005640 // Another possible incompatibility occurs from the vector element types. We
5641 // can fix this by bitcasting the source vectors to the same type we intend
5642 // for the shuffle.
5643 for (auto &Src : Sources) {
5644 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5645 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005646 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005647 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5648 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5649 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5650 Src.WindowBase *= Src.WindowScale;
5651 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005652
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005653 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005654 DEBUG(
5655 for (auto Src : Sources)
5656 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5657 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005658
5659 // The stars all align, our next step is to produce the mask for the shuffle.
5660 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5661 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5662 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5663 SDValue Entry = Op.getOperand(i);
5664 if (Entry.getOpcode() == ISD::UNDEF)
5665 continue;
5666
5667 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5668 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5669
5670 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5671 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5672 // segment.
5673 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5674 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5675 VT.getVectorElementType().getSizeInBits());
5676 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5677
5678 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5679 // starting at the appropriate offset.
5680 int *LaneMask = &Mask[i * ResMultiplier];
5681
5682 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5683 ExtractBase += NumElts * (Src - Sources.begin());
5684 for (int j = 0; j < LanesDefined; ++j)
5685 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005686 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005687
Bob Wilson6f2b8962011-01-07 21:37:30 +00005688 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005689 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5690 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005691
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005692 // We can't handle more than two sources. This should have already
5693 // been checked before this point.
5694 assert(Sources.size() <= 2 && "Too many sources!");
5695
5696 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5697 for (unsigned i = 0; i < Sources.size(); ++i)
5698 ShuffleOps[i] = Sources[i].ShuffleVec;
5699
5700 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5701 ShuffleOps[1], &Mask[0]);
5702 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005703}
5704
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005705/// isShuffleMaskLegal - Targets can use this to indicate that they only
5706/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5707/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5708/// are assumed to be legal.
5709bool
5710ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5711 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005712 if (VT.getVectorNumElements() == 4 &&
5713 (VT.is128BitVector() || VT.is64BitVector())) {
5714 unsigned PFIndexes[4];
5715 for (unsigned i = 0; i != 4; ++i) {
5716 if (M[i] < 0)
5717 PFIndexes[i] = 8;
5718 else
5719 PFIndexes[i] = M[i];
5720 }
5721
5722 // Compute the index in the perfect shuffle table.
5723 unsigned PFTableIndex =
5724 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5725 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5726 unsigned Cost = (PFEntry >> 30);
5727
5728 if (Cost <= 4)
5729 return true;
5730 }
5731
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005732 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005733 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005734
Bob Wilson846bd792010-06-07 23:53:38 +00005735 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5736 return (EltSize >= 32 ||
5737 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005738 isVREVMask(M, VT, 64) ||
5739 isVREVMask(M, VT, 32) ||
5740 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005741 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005742 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005743 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005744 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005745}
5746
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005747/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5748/// the specified operations to build the shuffle.
5749static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5750 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005751 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005752 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5753 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5754 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5755
5756 enum {
5757 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5758 OP_VREV,
5759 OP_VDUP0,
5760 OP_VDUP1,
5761 OP_VDUP2,
5762 OP_VDUP3,
5763 OP_VEXT1,
5764 OP_VEXT2,
5765 OP_VEXT3,
5766 OP_VUZPL, // VUZP, left result
5767 OP_VUZPR, // VUZP, right result
5768 OP_VZIPL, // VZIP, left result
5769 OP_VZIPR, // VZIP, right result
5770 OP_VTRNL, // VTRN, left result
5771 OP_VTRNR // VTRN, right result
5772 };
5773
5774 if (OpNum == OP_COPY) {
5775 if (LHSID == (1*9+2)*9+3) return LHS;
5776 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5777 return RHS;
5778 }
5779
5780 SDValue OpLHS, OpRHS;
5781 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5782 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5783 EVT VT = OpLHS.getValueType();
5784
5785 switch (OpNum) {
5786 default: llvm_unreachable("Unknown shuffle opcode!");
5787 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005788 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005789 if (VT.getVectorElementType() == MVT::i32 ||
5790 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005791 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5792 // vrev <4 x i16> -> VREV32
5793 if (VT.getVectorElementType() == MVT::i16)
5794 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5795 // vrev <4 x i8> -> VREV16
5796 assert(VT.getVectorElementType() == MVT::i8);
5797 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005798 case OP_VDUP0:
5799 case OP_VDUP1:
5800 case OP_VDUP2:
5801 case OP_VDUP3:
5802 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005803 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005804 case OP_VEXT1:
5805 case OP_VEXT2:
5806 case OP_VEXT3:
5807 return DAG.getNode(ARMISD::VEXT, dl, VT,
5808 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005809 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005810 case OP_VUZPL:
5811 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005812 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005813 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5814 case OP_VZIPL:
5815 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005816 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005817 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5818 case OP_VTRNL:
5819 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005820 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5821 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005822 }
5823}
5824
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005825static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005826 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005827 SelectionDAG &DAG) {
5828 // Check to see if we can use the VTBL instruction.
5829 SDValue V1 = Op.getOperand(0);
5830 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005831 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005832
5833 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005834 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005835 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005836 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005837
5838 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5839 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005840 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005841
Owen Anderson77aa2662011-04-05 21:48:57 +00005842 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005843 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005844}
5845
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005846static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5847 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005848 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005849 SDValue OpLHS = Op.getOperand(0);
5850 EVT VT = OpLHS.getValueType();
5851
5852 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5853 "Expect an v8i16/v16i8 type");
5854 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5855 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5856 // extract the first 8 bytes into the top double word and the last 8 bytes
5857 // into the bottom double word. The v8i16 case is similar.
5858 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5859 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005860 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005861}
5862
Bob Wilson2e076c42009-06-22 23:27:02 +00005863static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005864 SDValue V1 = Op.getOperand(0);
5865 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005866 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005867 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005868 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005869
Bob Wilsonc6800b52009-08-13 02:13:04 +00005870 // Convert shuffles that are directly supported on NEON to target-specific
5871 // DAG nodes, instead of keeping them as shuffles and matching them again
5872 // during code selection. This is more efficient and avoids the possibility
5873 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005874 // FIXME: floating-point vectors should be canonicalized to integer vectors
5875 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005876 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005877
Bob Wilson846bd792010-06-07 23:53:38 +00005878 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5879 if (EltSize <= 32) {
5880 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5881 int Lane = SVN->getSplatIndex();
5882 // If this is undef splat, generate it via "just" vdup, if possible.
5883 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005884
Dan Gohman198b7ff2011-11-03 21:49:52 +00005885 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005886 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5887 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5888 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005889 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5890 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5891 // reaches it).
5892 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5893 !isa<ConstantSDNode>(V1.getOperand(0))) {
5894 bool IsScalarToVector = true;
5895 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5896 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5897 IsScalarToVector = false;
5898 break;
5899 }
5900 if (IsScalarToVector)
5901 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5902 }
Bob Wilson846bd792010-06-07 23:53:38 +00005903 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005904 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005905 }
Bob Wilson846bd792010-06-07 23:53:38 +00005906
5907 bool ReverseVEXT;
5908 unsigned Imm;
5909 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5910 if (ReverseVEXT)
5911 std::swap(V1, V2);
5912 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005913 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005914 }
5915
5916 if (isVREVMask(ShuffleMask, VT, 64))
5917 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5918 if (isVREVMask(ShuffleMask, VT, 32))
5919 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5920 if (isVREVMask(ShuffleMask, VT, 16))
5921 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5922
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005923 if (V2->getOpcode() == ISD::UNDEF &&
5924 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5925 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005926 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005927 }
5928
Bob Wilson846bd792010-06-07 23:53:38 +00005929 // Check for Neon shuffles that modify both input vectors in place.
5930 // If both results are used, i.e., if there are two shuffles with the same
5931 // source operands and with masks corresponding to both results of one of
5932 // these operations, DAG memoization will ensure that a single node is
5933 // used for both shuffles.
5934 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005935 bool isV_UNDEF;
5936 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5937 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5938 if (isV_UNDEF)
5939 V2 = V1;
5940 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5941 .getValue(WhichResult);
5942 }
Bob Wilson846bd792010-06-07 23:53:38 +00005943
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005944 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5945 // shuffles that produce a result larger than their operands with:
5946 // shuffle(concat(v1, undef), concat(v2, undef))
5947 // ->
5948 // shuffle(concat(v1, v2), undef)
5949 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5950 //
5951 // This is useful in the general case, but there are special cases where
5952 // native shuffles produce larger results: the two-result ops.
5953 //
5954 // Look through the concat when lowering them:
5955 // shuffle(concat(v1, v2), undef)
5956 // ->
5957 // concat(VZIP(v1, v2):0, :1)
5958 //
5959 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5960 V2->getOpcode() == ISD::UNDEF) {
5961 SDValue SubV1 = V1->getOperand(0);
5962 SDValue SubV2 = V1->getOperand(1);
5963 EVT SubVT = SubV1.getValueType();
5964
5965 // We expect these to have been canonicalized to -1.
5966 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5967 return i < (int)VT.getVectorNumElements();
5968 }) && "Unexpected shuffle index into UNDEF operand!");
5969
5970 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5971 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5972 if (isV_UNDEF)
5973 SubV2 = SubV1;
5974 assert((WhichResult == 0) &&
5975 "In-place shuffle of concat can only have one result!");
5976 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
5977 SubV1, SubV2);
5978 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
5979 Res.getValue(1));
5980 }
5981 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00005982 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005983
Bob Wilsona7062312009-08-21 20:54:19 +00005984 // If the shuffle is not directly supported and it has 4 elements, use
5985 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005986 unsigned NumElts = VT.getVectorNumElements();
5987 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005988 unsigned PFIndexes[4];
5989 for (unsigned i = 0; i != 4; ++i) {
5990 if (ShuffleMask[i] < 0)
5991 PFIndexes[i] = 8;
5992 else
5993 PFIndexes[i] = ShuffleMask[i];
5994 }
5995
5996 // Compute the index in the perfect shuffle table.
5997 unsigned PFTableIndex =
5998 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005999 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6000 unsigned Cost = (PFEntry >> 30);
6001
6002 if (Cost <= 4)
6003 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6004 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006005
Bob Wilsond8a9a042010-06-04 00:04:02 +00006006 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006007 if (EltSize >= 32) {
6008 // Do the expansion with floating-point types, since that is what the VFP
6009 // registers are defined to use, and since i64 is not legal.
6010 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6011 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006012 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6013 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006014 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006015 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006016 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006017 Ops.push_back(DAG.getUNDEF(EltVT));
6018 else
6019 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6020 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6021 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006022 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006023 }
Craig Topper48d114b2014-04-26 18:35:24 +00006024 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006025 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006026 }
6027
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006028 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6029 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6030
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006031 if (VT == MVT::v8i8) {
6032 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
6033 if (NewOp.getNode())
6034 return NewOp;
6035 }
6036
Bob Wilson6f34e272009-08-14 05:16:33 +00006037 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006038}
6039
Eli Friedmana5e244c2011-10-24 23:08:52 +00006040static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6041 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6042 SDValue Lane = Op.getOperand(2);
6043 if (!isa<ConstantSDNode>(Lane))
6044 return SDValue();
6045
6046 return Op;
6047}
6048
Bob Wilson2e076c42009-06-22 23:27:02 +00006049static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006050 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006051 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006052 if (!isa<ConstantSDNode>(Lane))
6053 return SDValue();
6054
6055 SDValue Vec = Op.getOperand(0);
6056 if (Op.getValueType() == MVT::i32 &&
6057 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006058 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006059 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6060 }
6061
6062 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006063}
6064
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006065static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6066 // The only time a CONCAT_VECTORS operation can have legal types is when
6067 // two 64-bit vectors are concatenated to a 128-bit vector.
6068 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6069 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006070 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006071 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006072 SDValue Op0 = Op.getOperand(0);
6073 SDValue Op1 = Op.getOperand(1);
6074 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006075 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006076 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006077 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006078 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006079 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006080 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006081 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006082 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006083}
6084
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006085/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6086/// element has been zero/sign-extended, depending on the isSigned parameter,
6087/// from an integer type half its size.
6088static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6089 bool isSigned) {
6090 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6091 EVT VT = N->getValueType(0);
6092 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6093 SDNode *BVN = N->getOperand(0).getNode();
6094 if (BVN->getValueType(0) != MVT::v4i32 ||
6095 BVN->getOpcode() != ISD::BUILD_VECTOR)
6096 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006097 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006098 unsigned HiElt = 1 - LoElt;
6099 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6100 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6101 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6102 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6103 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6104 return false;
6105 if (isSigned) {
6106 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6107 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6108 return true;
6109 } else {
6110 if (Hi0->isNullValue() && Hi1->isNullValue())
6111 return true;
6112 }
6113 return false;
6114 }
6115
6116 if (N->getOpcode() != ISD::BUILD_VECTOR)
6117 return false;
6118
6119 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6120 SDNode *Elt = N->getOperand(i).getNode();
6121 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6122 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6123 unsigned HalfSize = EltSize / 2;
6124 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006125 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006126 return false;
6127 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006128 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006129 return false;
6130 }
6131 continue;
6132 }
6133 return false;
6134 }
6135
6136 return true;
6137}
6138
6139/// isSignExtended - Check if a node is a vector value that is sign-extended
6140/// or a constant BUILD_VECTOR with sign-extended elements.
6141static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6142 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6143 return true;
6144 if (isExtendedBUILD_VECTOR(N, DAG, true))
6145 return true;
6146 return false;
6147}
6148
6149/// isZeroExtended - Check if a node is a vector value that is zero-extended
6150/// or a constant BUILD_VECTOR with zero-extended elements.
6151static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6152 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6153 return true;
6154 if (isExtendedBUILD_VECTOR(N, DAG, false))
6155 return true;
6156 return false;
6157}
6158
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006159static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6160 if (OrigVT.getSizeInBits() >= 64)
6161 return OrigVT;
6162
6163 assert(OrigVT.isSimple() && "Expecting a simple value type");
6164
6165 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6166 switch (OrigSimpleTy) {
6167 default: llvm_unreachable("Unexpected Vector Type");
6168 case MVT::v2i8:
6169 case MVT::v2i16:
6170 return MVT::v2i32;
6171 case MVT::v4i8:
6172 return MVT::v4i16;
6173 }
6174}
6175
Sebastian Popa204f722012-11-30 19:08:04 +00006176/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6177/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6178/// We insert the required extension here to get the vector to fill a D register.
6179static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6180 const EVT &OrigTy,
6181 const EVT &ExtTy,
6182 unsigned ExtOpcode) {
6183 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6184 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6185 // 64-bits we need to insert a new extension so that it will be 64-bits.
6186 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6187 if (OrigTy.getSizeInBits() >= 64)
6188 return N;
6189
6190 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006191 EVT NewVT = getExtensionTo64Bits(OrigTy);
6192
Andrew Trickef9de2a2013-05-25 02:42:55 +00006193 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006194}
6195
6196/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6197/// does not do any sign/zero extension. If the original vector is less
6198/// than 64 bits, an appropriate extension will be added after the load to
6199/// reach a total size of 64 bits. We have to add the extension separately
6200/// because ARM does not have a sign/zero extending load for vectors.
6201static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006202 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6203
6204 // The load already has the right type.
6205 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006206 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006207 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6208 LD->isNonTemporal(), LD->isInvariant(),
6209 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006210
6211 // We need to create a zextload/sextload. We cannot just create a load
6212 // followed by a zext/zext node because LowerMUL is also run during normal
6213 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006214 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006215 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006216 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006217 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006218}
6219
6220/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6221/// extending load, or BUILD_VECTOR with extended elements, return the
6222/// unextended value. The unextended vector should be 64 bits so that it can
6223/// be used as an operand to a VMULL instruction. If the original vector size
6224/// before extension is less than 64 bits we add a an extension to resize
6225/// the vector to 64 bits.
6226static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006227 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006228 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6229 N->getOperand(0)->getValueType(0),
6230 N->getValueType(0),
6231 N->getOpcode());
6232
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006233 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006234 return SkipLoadExtensionForVMULL(LD, DAG);
6235
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006236 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6237 // have been legalized as a BITCAST from v4i32.
6238 if (N->getOpcode() == ISD::BITCAST) {
6239 SDNode *BVN = N->getOperand(0).getNode();
6240 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6241 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006242 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006243 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006244 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6245 }
6246 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6247 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6248 EVT VT = N->getValueType(0);
6249 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6250 unsigned NumElts = VT.getVectorNumElements();
6251 MVT TruncVT = MVT::getIntegerVT(EltSize);
6252 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006253 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006254 for (unsigned i = 0; i != NumElts; ++i) {
6255 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6256 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006257 // Element types smaller than 32 bits are not legal, so use i32 elements.
6258 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006259 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006260 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006261 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006262 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006263}
6264
Evan Chenge2086e72011-03-29 01:56:09 +00006265static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6266 unsigned Opcode = N->getOpcode();
6267 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6268 SDNode *N0 = N->getOperand(0).getNode();
6269 SDNode *N1 = N->getOperand(1).getNode();
6270 return N0->hasOneUse() && N1->hasOneUse() &&
6271 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6272 }
6273 return false;
6274}
6275
6276static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6277 unsigned Opcode = N->getOpcode();
6278 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6279 SDNode *N0 = N->getOperand(0).getNode();
6280 SDNode *N1 = N->getOperand(1).getNode();
6281 return N0->hasOneUse() && N1->hasOneUse() &&
6282 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6283 }
6284 return false;
6285}
6286
Bob Wilson38ab35a2010-09-01 23:50:19 +00006287static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6288 // Multiplications are only custom-lowered for 128-bit vectors so that
6289 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6290 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006291 assert(VT.is128BitVector() && VT.isInteger() &&
6292 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006293 SDNode *N0 = Op.getOperand(0).getNode();
6294 SDNode *N1 = Op.getOperand(1).getNode();
6295 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006296 bool isMLA = false;
6297 bool isN0SExt = isSignExtended(N0, DAG);
6298 bool isN1SExt = isSignExtended(N1, DAG);
6299 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006300 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006301 else {
6302 bool isN0ZExt = isZeroExtended(N0, DAG);
6303 bool isN1ZExt = isZeroExtended(N1, DAG);
6304 if (isN0ZExt && isN1ZExt)
6305 NewOpc = ARMISD::VMULLu;
6306 else if (isN1SExt || isN1ZExt) {
6307 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6308 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6309 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6310 NewOpc = ARMISD::VMULLs;
6311 isMLA = true;
6312 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6313 NewOpc = ARMISD::VMULLu;
6314 isMLA = true;
6315 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6316 std::swap(N0, N1);
6317 NewOpc = ARMISD::VMULLu;
6318 isMLA = true;
6319 }
6320 }
6321
6322 if (!NewOpc) {
6323 if (VT == MVT::v2i64)
6324 // Fall through to expand this. It is not legal.
6325 return SDValue();
6326 else
6327 // Other vector multiplications are legal.
6328 return Op;
6329 }
6330 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006331
6332 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006333 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006334 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006335 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006336 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006337 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006338 assert(Op0.getValueType().is64BitVector() &&
6339 Op1.getValueType().is64BitVector() &&
6340 "unexpected types for extended operands to VMULL");
6341 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6342 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006343
Evan Chenge2086e72011-03-29 01:56:09 +00006344 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6345 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6346 // vmull q0, d4, d6
6347 // vmlal q0, d5, d6
6348 // is faster than
6349 // vaddl q0, d4, d5
6350 // vmovl q1, d6
6351 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006352 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6353 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006354 EVT Op1VT = Op1.getValueType();
6355 return DAG.getNode(N0->getOpcode(), DL, VT,
6356 DAG.getNode(NewOpc, DL, VT,
6357 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6358 DAG.getNode(NewOpc, DL, VT,
6359 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006360}
6361
Owen Anderson77aa2662011-04-05 21:48:57 +00006362static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006363LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006364 // TODO: Should this propagate fast-math-flags?
6365
Nate Begemanfa62d502011-02-11 20:53:29 +00006366 // Convert to float
6367 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6368 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6369 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6370 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6371 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6372 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6373 // Get reciprocal estimate.
6374 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006375 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006376 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6377 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006378 // Because char has a smaller range than uchar, we can actually get away
6379 // without any newton steps. This requires that we use a weird bias
6380 // of 0xb000, however (again, this has been exhaustively tested).
6381 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6382 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6383 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006384 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006385 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6386 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6387 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6388 // Convert back to short.
6389 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6390 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6391 return X;
6392}
6393
Owen Anderson77aa2662011-04-05 21:48:57 +00006394static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006395LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006396 // TODO: Should this propagate fast-math-flags?
6397
Nate Begemanfa62d502011-02-11 20:53:29 +00006398 SDValue N2;
6399 // Convert to float.
6400 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6401 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6402 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6403 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6404 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6405 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006406
Nate Begemanfa62d502011-02-11 20:53:29 +00006407 // Use reciprocal estimate and one refinement step.
6408 // float4 recip = vrecpeq_f32(yf);
6409 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006410 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006411 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6412 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006413 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006414 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006415 N1, N2);
6416 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6417 // Because short has a smaller range than ushort, we can actually get away
6418 // with only a single newton step. This requires that we use a weird bias
6419 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006420 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006421 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6422 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006423 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006424 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6425 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6426 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6427 // Convert back to integer and return.
6428 // return vmovn_s32(vcvt_s32_f32(result));
6429 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6430 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6431 return N0;
6432}
6433
6434static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6435 EVT VT = Op.getValueType();
6436 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6437 "unexpected type for custom-lowering ISD::SDIV");
6438
Andrew Trickef9de2a2013-05-25 02:42:55 +00006439 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006440 SDValue N0 = Op.getOperand(0);
6441 SDValue N1 = Op.getOperand(1);
6442 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006443
Nate Begemanfa62d502011-02-11 20:53:29 +00006444 if (VT == MVT::v8i8) {
6445 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6446 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006447
Nate Begemanfa62d502011-02-11 20:53:29 +00006448 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006449 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006450 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006451 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006452 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006453 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006454 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006455 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006456
6457 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6458 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6459
6460 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6461 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006462
Nate Begemanfa62d502011-02-11 20:53:29 +00006463 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6464 return N0;
6465 }
6466 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6467}
6468
6469static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006470 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006471 EVT VT = Op.getValueType();
6472 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6473 "unexpected type for custom-lowering ISD::UDIV");
6474
Andrew Trickef9de2a2013-05-25 02:42:55 +00006475 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006476 SDValue N0 = Op.getOperand(0);
6477 SDValue N1 = Op.getOperand(1);
6478 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006479
Nate Begemanfa62d502011-02-11 20:53:29 +00006480 if (VT == MVT::v8i8) {
6481 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6482 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006483
Nate Begemanfa62d502011-02-11 20:53:29 +00006484 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006485 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006486 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006487 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006488 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006489 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006490 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006491 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006492
Nate Begemanfa62d502011-02-11 20:53:29 +00006493 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6494 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006495
Nate Begemanfa62d502011-02-11 20:53:29 +00006496 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6497 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006498
6499 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006500 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6501 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006502 N0);
6503 return N0;
6504 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006505
Nate Begemanfa62d502011-02-11 20:53:29 +00006506 // v4i16 sdiv ... Convert to float.
6507 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6508 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6509 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6510 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6511 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006512 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006513
6514 // Use reciprocal estimate and two refinement steps.
6515 // float4 recip = vrecpeq_f32(yf);
6516 // recip *= vrecpsq_f32(yf, recip);
6517 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006518 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006519 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6520 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006521 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006522 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006523 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006524 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006525 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006526 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006527 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006528 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6529 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6530 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6531 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006532 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006533 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6534 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006535 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006536 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6537 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6538 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6539 // Convert back to integer and return.
6540 // return vmovn_u32(vcvt_s32_f32(result));
6541 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6542 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6543 return N0;
6544}
6545
Evan Chenge8916542011-08-30 01:34:54 +00006546static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6547 EVT VT = Op.getNode()->getValueType(0);
6548 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6549
6550 unsigned Opc;
6551 bool ExtraOp = false;
6552 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006553 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006554 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6555 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6556 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6557 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6558 }
6559
6560 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006561 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006562 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006563 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006564 Op.getOperand(1), Op.getOperand(2));
6565}
6566
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006567SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6568 assert(Subtarget->isTargetDarwin());
6569
6570 // For iOS, we want to call an alternative entry point: __sincos_stret,
6571 // return values are passed via sret.
6572 SDLoc dl(Op);
6573 SDValue Arg = Op.getOperand(0);
6574 EVT ArgVT = Arg.getValueType();
6575 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006576 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006577
6578 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006579
6580 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006581 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006582
6583 // Create stack object for sret.
Mehdi Amini44ede332015-07-09 02:09:04 +00006584 auto &DL = DAG.getDataLayout();
6585 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6586 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006587 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00006588 SDValue SRet = DAG.getFrameIndex(FrameIdx, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006589
6590 ArgListTy Args;
6591 ArgListEntry Entry;
6592
6593 Entry.Node = SRet;
6594 Entry.Ty = RetTy->getPointerTo();
6595 Entry.isSExt = false;
6596 Entry.isZExt = false;
6597 Entry.isSRet = true;
6598 Args.push_back(Entry);
6599
6600 Entry.Node = Arg;
6601 Entry.Ty = ArgTy;
6602 Entry.isSExt = false;
6603 Entry.isZExt = false;
6604 Args.push_back(Entry);
6605
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006606 const char *LibcallName =
6607 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Mehdi Amini44ede332015-07-09 02:09:04 +00006608 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006609
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006610 TargetLowering::CallLoweringInfo CLI(DAG);
6611 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6612 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006613 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006614 .setDiscardResult();
6615
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006616 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6617
6618 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6619 MachinePointerInfo(), false, false, false, 0);
6620
6621 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006622 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006623 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006624 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6625 MachinePointerInfo(), false, false, false, 0);
6626
6627 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6628 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6629 LoadSin.getValue(0), LoadCos.getValue(0));
6630}
6631
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006632SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
6633 bool Signed,
6634 SDValue &Chain) const {
6635 EVT VT = Op.getValueType();
6636 assert((VT == MVT::i32 || VT == MVT::i64) &&
6637 "unexpected type for custom lowering DIV");
6638 SDLoc dl(Op);
6639
6640 const auto &DL = DAG.getDataLayout();
6641 const auto &TLI = DAG.getTargetLoweringInfo();
6642
6643 const char *Name = nullptr;
6644 if (Signed)
6645 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6646 else
6647 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
6648
6649 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6650
6651 ARMTargetLowering::ArgListTy Args;
6652
6653 for (auto AI : {1, 0}) {
6654 ArgListEntry Arg;
6655 Arg.Node = Op.getOperand(AI);
6656 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6657 Args.push_back(Arg);
6658 }
6659
6660 CallLoweringInfo CLI(DAG);
6661 CLI.setDebugLoc(dl)
6662 .setChain(Chain)
6663 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6664 ES, std::move(Args), 0);
6665
6666 return LowerCallTo(CLI).first;
6667}
6668
6669SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6670 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006671 assert(Op.getValueType() == MVT::i32 &&
6672 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006673 SDLoc dl(Op);
6674
6675 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6676 DAG.getEntryNode(), Op.getOperand(1));
6677
6678 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
6679}
6680
6681void ARMTargetLowering::ExpandDIV_Windows(
6682 SDValue Op, SelectionDAG &DAG, bool Signed,
6683 SmallVectorImpl<SDValue> &Results) const {
6684 const auto &DL = DAG.getDataLayout();
6685 const auto &TLI = DAG.getTargetLoweringInfo();
6686
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006687 assert(Op.getValueType() == MVT::i64 &&
6688 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006689 SDLoc dl(Op);
6690
6691 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6692 DAG.getConstant(0, dl, MVT::i32));
6693 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6694 DAG.getConstant(1, dl, MVT::i32));
6695 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6696
6697 SDValue DBZCHK =
6698 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6699
6700 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
6701
6702 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6703 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6704 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6705 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6706
6707 Results.push_back(Lower);
6708 Results.push_back(Upper);
6709}
6710
Eli Friedman10f9ce22011-09-15 22:26:18 +00006711static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006712 // Monotonic load/store is legal for all targets
6713 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6714 return Op;
6715
Alp Tokercb402912014-01-24 17:20:08 +00006716 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006717 // dmb or equivalent available.
6718 return SDValue();
6719}
6720
Tim Northoverbc933082013-05-23 19:11:20 +00006721static void ReplaceREADCYCLECOUNTER(SDNode *N,
6722 SmallVectorImpl<SDValue> &Results,
6723 SelectionDAG &DAG,
6724 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006725 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006726 // Under Power Management extensions, the cycle-count is:
6727 // mrc p15, #0, <Rt>, c9, c13, #0
6728 SDValue Ops[] = { N->getOperand(0), // Chain
6729 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6730 DAG.getConstant(15, DL, MVT::i32),
6731 DAG.getConstant(0, DL, MVT::i32),
6732 DAG.getConstant(9, DL, MVT::i32),
6733 DAG.getConstant(13, DL, MVT::i32),
6734 DAG.getConstant(0, DL, MVT::i32)
6735 };
Tim Northoverbc933082013-05-23 19:11:20 +00006736
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006737 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6738 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6739 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6740 DAG.getConstant(0, DL, MVT::i32)));
6741 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006742}
6743
Dan Gohman21cea8a2010-04-17 15:26:15 +00006744SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006745 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006746 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006747 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006748 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006749 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006750 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006751 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6752 default: llvm_unreachable("unknown object format");
6753 case Triple::COFF:
6754 return LowerGlobalAddressWindows(Op, DAG);
6755 case Triple::ELF:
6756 return LowerGlobalAddressELF(Op, DAG);
6757 case Triple::MachO:
6758 return LowerGlobalAddressDarwin(Op, DAG);
6759 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006760 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006761 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006762 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6763 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006764 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006765 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006766 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006767 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006768 case ISD::SINT_TO_FP:
6769 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6770 case ISD::FP_TO_SINT:
6771 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006772 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006773 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006774 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006775 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006776 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006777 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006778 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6779 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006780 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006781 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006782 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006783 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00006784 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
6785 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006786 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006787 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006788 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006789 case ISD::CTTZ:
6790 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006791 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006792 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006793 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006794 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006795 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006796 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006797 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006798 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006799 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006800 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006801 case ISD::SDIV:
6802 if (Subtarget->isTargetWindows())
6803 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
6804 return LowerSDIV(Op, DAG);
6805 case ISD::UDIV:
6806 if (Subtarget->isTargetWindows())
6807 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
6808 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006809 case ISD::ADDC:
6810 case ISD::ADDE:
6811 case ISD::SUBC:
6812 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006813 case ISD::SADDO:
6814 case ISD::UADDO:
6815 case ISD::SSUBO:
6816 case ISD::USUBO:
6817 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006818 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006819 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006820 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006821 case ISD::SDIVREM:
6822 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006823 case ISD::DYNAMIC_STACKALLOC:
6824 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6825 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6826 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006827 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6828 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006829 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00006830 }
Evan Cheng10043e22007-01-19 07:51:42 +00006831}
6832
Duncan Sands6ed40142008-12-01 11:39:25 +00006833/// ReplaceNodeResults - Replace the results of node with an illegal result
6834/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006835void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006836 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006837 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006838 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006839 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006840 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006841 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006842 case ISD::READ_REGISTER:
6843 ExpandREAD_REGISTER(N, Results, DAG);
6844 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006845 case ISD::BITCAST:
6846 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006847 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006848 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006849 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006850 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006851 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00006852 case ISD::SREM:
6853 case ISD::UREM:
6854 Res = LowerREM(N, DAG);
6855 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006856 case ISD::READCYCLECOUNTER:
6857 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6858 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006859 case ISD::UDIV:
6860 case ISD::SDIV:
6861 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
6862 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
6863 Results);
Duncan Sands6ed40142008-12-01 11:39:25 +00006864 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006865 if (Res.getNode())
6866 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006867}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006868
Evan Cheng10043e22007-01-19 07:51:42 +00006869//===----------------------------------------------------------------------===//
6870// ARM Scheduler Hooks
6871//===----------------------------------------------------------------------===//
6872
Bill Wendling030b58e2011-10-06 22:18:16 +00006873/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6874/// registers the function context.
6875void ARMTargetLowering::
6876SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6877 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006878 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006879 DebugLoc dl = MI->getDebugLoc();
6880 MachineFunction *MF = MBB->getParent();
6881 MachineRegisterInfo *MRI = &MF->getRegInfo();
6882 MachineConstantPool *MCP = MF->getConstantPool();
6883 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6884 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006885
Bill Wendling374ee192011-10-03 21:25:38 +00006886 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006887 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006888
Bill Wendling374ee192011-10-03 21:25:38 +00006889 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006890 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006891 ARMConstantPoolValue *CPV =
6892 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6893 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6894
Craig Topper61e88f42014-11-21 05:58:21 +00006895 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6896 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006897
Bill Wendling030b58e2011-10-06 22:18:16 +00006898 // Grab constant pool and fixed stack memory operands.
6899 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006900 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
6901 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006902
6903 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006904 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
6905 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006906
6907 // Load the address of the dispatch MBB into the jump buffer.
6908 if (isThumb2) {
6909 // Incoming value: jbuf
6910 // ldr.n r5, LCPI1_1
6911 // orr r5, r5, #1
6912 // add r5, pc
6913 // str r5, [$jbuf, #+4] ; &jbuf[1]
6914 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6915 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6916 .addConstantPoolIndex(CPI)
6917 .addMemOperand(CPMMO));
6918 // Set the low bit because of thumb mode.
6919 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6920 AddDefaultCC(
6921 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6922 .addReg(NewVReg1, RegState::Kill)
6923 .addImm(0x01)));
6924 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6925 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6926 .addReg(NewVReg2, RegState::Kill)
6927 .addImm(PCLabelId);
6928 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6929 .addReg(NewVReg3, RegState::Kill)
6930 .addFrameIndex(FI)
6931 .addImm(36) // &jbuf[1] :: pc
6932 .addMemOperand(FIMMOSt));
6933 } else if (isThumb) {
6934 // Incoming value: jbuf
6935 // ldr.n r1, LCPI1_4
6936 // add r1, pc
6937 // mov r2, #1
6938 // orrs r1, r2
6939 // add r2, $jbuf, #+4 ; &jbuf[1]
6940 // str r1, [r2]
6941 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6942 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6943 .addConstantPoolIndex(CPI)
6944 .addMemOperand(CPMMO));
6945 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6946 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6947 .addReg(NewVReg1, RegState::Kill)
6948 .addImm(PCLabelId);
6949 // Set the low bit because of thumb mode.
6950 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6951 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6952 .addReg(ARM::CPSR, RegState::Define)
6953 .addImm(1));
6954 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6955 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6956 .addReg(ARM::CPSR, RegState::Define)
6957 .addReg(NewVReg2, RegState::Kill)
6958 .addReg(NewVReg3, RegState::Kill));
6959 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006960 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6961 .addFrameIndex(FI)
6962 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006963 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6964 .addReg(NewVReg4, RegState::Kill)
6965 .addReg(NewVReg5, RegState::Kill)
6966 .addImm(0)
6967 .addMemOperand(FIMMOSt));
6968 } else {
6969 // Incoming value: jbuf
6970 // ldr r1, LCPI1_1
6971 // add r1, pc, r1
6972 // str r1, [$jbuf, #+4] ; &jbuf[1]
6973 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6974 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6975 .addConstantPoolIndex(CPI)
6976 .addImm(0)
6977 .addMemOperand(CPMMO));
6978 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6979 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6980 .addReg(NewVReg1, RegState::Kill)
6981 .addImm(PCLabelId));
6982 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6983 .addReg(NewVReg2, RegState::Kill)
6984 .addFrameIndex(FI)
6985 .addImm(36) // &jbuf[1] :: pc
6986 .addMemOperand(FIMMOSt));
6987 }
6988}
6989
Matthias Brauneec4efc2015-04-28 00:37:05 +00006990void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6991 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006992 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006993 DebugLoc dl = MI->getDebugLoc();
6994 MachineFunction *MF = MBB->getParent();
6995 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006996 MachineFrameInfo *MFI = MF->getFrameInfo();
6997 int FI = MFI->getFunctionContextIndex();
6998
Craig Topper61e88f42014-11-21 05:58:21 +00006999 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7000 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007001
Bill Wendling362c1b02011-10-06 21:29:56 +00007002 // Get a mapping of the call site numbers to all of the landing pads they're
7003 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007004 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7005 unsigned MaxCSNum = 0;
7006 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007007 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7008 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007009 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007010
7011 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7012 // pad.
7013 for (MachineBasicBlock::iterator
7014 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7015 if (!II->isEHLabel()) continue;
7016
7017 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007018 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007019
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007020 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7021 for (SmallVectorImpl<unsigned>::iterator
7022 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7023 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007024 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007025 MaxCSNum = std::max(MaxCSNum, *CSI);
7026 }
Bill Wendling202803e2011-10-05 00:02:33 +00007027 break;
7028 }
7029 }
7030
7031 // Get an ordered list of the machine basic blocks for the jump table.
7032 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00007033 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007034 LPadList.reserve(CallSiteNumToLPad.size());
7035 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7036 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7037 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007038 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007039 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007040 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7041 }
Bill Wendling202803e2011-10-05 00:02:33 +00007042 }
7043
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007044 assert(!LPadList.empty() &&
7045 "No landing pad destinations for the dispatch jump table!");
7046
Bill Wendling362c1b02011-10-06 21:29:56 +00007047 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007048 MachineJumpTableInfo *JTI =
7049 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7050 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007051 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007052
Bill Wendling362c1b02011-10-06 21:29:56 +00007053 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007054
7055 // Shove the dispatch's address into the return slot in the function context.
7056 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007057 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007058
Bill Wendling324be982011-10-05 00:39:32 +00007059 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007060 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007061 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007062 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007063 else
7064 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7065
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007066 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007067 DispatchBB->addSuccessor(TrapBB);
7068
7069 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7070 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007071
Bill Wendling510fbcd2011-10-17 21:32:56 +00007072 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007073 MF->insert(MF->end(), DispatchBB);
7074 MF->insert(MF->end(), DispContBB);
7075 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007076
Bill Wendling030b58e2011-10-06 22:18:16 +00007077 // Insert code into the entry block that creates and registers the function
7078 // context.
7079 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7080
Alex Lorenze40c8a22015-08-11 23:09:45 +00007081 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7082 MachinePointerInfo::getFixedStack(*MF, FI),
7083 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007084
Chad Rosier1ec8e402012-11-06 23:05:24 +00007085 MachineInstrBuilder MIB;
7086 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7087
7088 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7089 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7090
7091 // Add a register mask with no preserved registers. This results in all
7092 // registers being marked as clobbered.
7093 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007094
Bill Wendling85833f72011-10-18 22:49:07 +00007095 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007096 if (Subtarget->isThumb2()) {
7097 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7098 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7099 .addFrameIndex(FI)
7100 .addImm(4)
7101 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007102
Bill Wendling85833f72011-10-18 22:49:07 +00007103 if (NumLPads < 256) {
7104 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7105 .addReg(NewVReg1)
7106 .addImm(LPadList.size()));
7107 } else {
7108 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7109 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007110 .addImm(NumLPads & 0xFFFF));
7111
7112 unsigned VReg2 = VReg1;
7113 if ((NumLPads & 0xFFFF0000) != 0) {
7114 VReg2 = MRI->createVirtualRegister(TRC);
7115 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7116 .addReg(VReg1)
7117 .addImm(NumLPads >> 16));
7118 }
7119
Bill Wendling85833f72011-10-18 22:49:07 +00007120 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7121 .addReg(NewVReg1)
7122 .addReg(VReg2));
7123 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007124
Bill Wendling5626c662011-10-06 22:53:00 +00007125 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7126 .addMBB(TrapBB)
7127 .addImm(ARMCC::HI)
7128 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007129
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007130 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7131 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007132 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007133
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007134 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007135 AddDefaultCC(
7136 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007137 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7138 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007139 .addReg(NewVReg1)
7140 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7141
7142 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007143 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007144 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007145 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007146 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007147 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7148 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7149 .addFrameIndex(FI)
7150 .addImm(1)
7151 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007152
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007153 if (NumLPads < 256) {
7154 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7155 .addReg(NewVReg1)
7156 .addImm(NumLPads));
7157 } else {
7158 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007159 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7160 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7161
7162 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007163 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007164 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007165 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007166 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007167
7168 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7169 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7170 .addReg(VReg1, RegState::Define)
7171 .addConstantPoolIndex(Idx));
7172 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7173 .addReg(NewVReg1)
7174 .addReg(VReg1));
7175 }
7176
Bill Wendlingb3d46782011-10-06 23:37:36 +00007177 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7178 .addMBB(TrapBB)
7179 .addImm(ARMCC::HI)
7180 .addReg(ARM::CPSR);
7181
7182 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7183 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7184 .addReg(ARM::CPSR, RegState::Define)
7185 .addReg(NewVReg1)
7186 .addImm(2));
7187
7188 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007189 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007190 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007191
7192 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7193 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7194 .addReg(ARM::CPSR, RegState::Define)
7195 .addReg(NewVReg2, RegState::Kill)
7196 .addReg(NewVReg3));
7197
Alex Lorenze40c8a22015-08-11 23:09:45 +00007198 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7199 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007200
7201 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7202 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7203 .addReg(NewVReg4, RegState::Kill)
7204 .addImm(0)
7205 .addMemOperand(JTMMOLd));
7206
Chad Rosier96603432013-03-01 18:30:38 +00007207 unsigned NewVReg6 = NewVReg5;
7208 if (RelocM == Reloc::PIC_) {
7209 NewVReg6 = MRI->createVirtualRegister(TRC);
7210 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7211 .addReg(ARM::CPSR, RegState::Define)
7212 .addReg(NewVReg5, RegState::Kill)
7213 .addReg(NewVReg3));
7214 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007215
7216 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7217 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007218 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007219 } else {
7220 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7221 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7222 .addFrameIndex(FI)
7223 .addImm(4)
7224 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007225
Bill Wendling4969dcd2011-10-18 22:52:20 +00007226 if (NumLPads < 256) {
7227 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7228 .addReg(NewVReg1)
7229 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007230 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007231 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7232 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007233 .addImm(NumLPads & 0xFFFF));
7234
7235 unsigned VReg2 = VReg1;
7236 if ((NumLPads & 0xFFFF0000) != 0) {
7237 VReg2 = MRI->createVirtualRegister(TRC);
7238 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7239 .addReg(VReg1)
7240 .addImm(NumLPads >> 16));
7241 }
7242
Bill Wendling4969dcd2011-10-18 22:52:20 +00007243 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7244 .addReg(NewVReg1)
7245 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007246 } else {
7247 MachineConstantPool *ConstantPool = MF->getConstantPool();
7248 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7249 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7250
7251 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007252 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007253 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007254 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007255 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7256
7257 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7258 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7259 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007260 .addConstantPoolIndex(Idx)
7261 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007262 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7263 .addReg(NewVReg1)
7264 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007265 }
7266
Bill Wendling5626c662011-10-06 22:53:00 +00007267 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7268 .addMBB(TrapBB)
7269 .addImm(ARMCC::HI)
7270 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007271
Bill Wendling973c8172011-10-18 22:11:18 +00007272 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007273 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007274 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007275 .addReg(NewVReg1)
7276 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007277 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7278 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007279 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007280
Alex Lorenze40c8a22015-08-11 23:09:45 +00007281 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7282 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007283 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007284 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007285 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7286 .addReg(NewVReg3, RegState::Kill)
7287 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007288 .addImm(0)
7289 .addMemOperand(JTMMOLd));
7290
Chad Rosier96603432013-03-01 18:30:38 +00007291 if (RelocM == Reloc::PIC_) {
7292 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7293 .addReg(NewVReg5, RegState::Kill)
7294 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007295 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007296 } else {
7297 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7298 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007299 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007300 }
Bill Wendling5626c662011-10-06 22:53:00 +00007301 }
Bill Wendling202803e2011-10-05 00:02:33 +00007302
Bill Wendling324be982011-10-05 00:39:32 +00007303 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007304 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007305 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007306 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7307 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007308 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007309 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007310 }
7311
Bill Wendling26d27802011-10-17 05:25:09 +00007312 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007313 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007314 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007315 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007316
7317 // Remove the landing pad successor from the invoke block and replace it
7318 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007319 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7320 BB->succ_end());
7321 while (!Successors.empty()) {
7322 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007323 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007324 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007325 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007326 }
7327 }
7328
7329 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007330
7331 // Find the invoke call and mark all of the callee-saved registers as
7332 // 'implicit defined' so that they're spilled. This prevents code from
7333 // moving instructions to before the EH block, where they will never be
7334 // executed.
7335 for (MachineBasicBlock::reverse_iterator
7336 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007337 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007338
7339 DenseMap<unsigned, bool> DefRegs;
7340 for (MachineInstr::mop_iterator
7341 OI = II->operands_begin(), OE = II->operands_end();
7342 OI != OE; ++OI) {
7343 if (!OI->isReg()) continue;
7344 DefRegs[OI->getReg()] = true;
7345 }
7346
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007347 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007348
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007349 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007350 unsigned Reg = SavedRegs[i];
7351 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007352 !ARM::tGPRRegClass.contains(Reg) &&
7353 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007354 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007355 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007356 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007357 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007358 continue;
7359 if (!DefRegs[Reg])
7360 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007361 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007362
7363 break;
7364 }
Bill Wendling883ec972011-10-07 23:18:02 +00007365 }
Bill Wendling324be982011-10-05 00:39:32 +00007366
Bill Wendling617075f2011-10-18 18:30:49 +00007367 // Mark all former landing pads as non-landing pads. The dispatch is the only
7368 // landing pad now.
7369 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7370 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007371 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007372
Bill Wendling324be982011-10-05 00:39:32 +00007373 // The instruction is gone now.
7374 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007375}
7376
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007377static
7378MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7379 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7380 E = MBB->succ_end(); I != E; ++I)
7381 if (*I != Succ)
7382 return *I;
7383 llvm_unreachable("Expecting a BB with two successors!");
7384}
7385
Manman Renb504f492013-10-29 22:27:32 +00007386/// Return the load opcode for a given load size. If load size >= 8,
7387/// neon opcode will be returned.
7388static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7389 if (LdSize >= 8)
7390 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7391 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7392 if (IsThumb1)
7393 return LdSize == 4 ? ARM::tLDRi
7394 : LdSize == 2 ? ARM::tLDRHi
7395 : LdSize == 1 ? ARM::tLDRBi : 0;
7396 if (IsThumb2)
7397 return LdSize == 4 ? ARM::t2LDR_POST
7398 : LdSize == 2 ? ARM::t2LDRH_POST
7399 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7400 return LdSize == 4 ? ARM::LDR_POST_IMM
7401 : LdSize == 2 ? ARM::LDRH_POST
7402 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7403}
7404
7405/// Return the store opcode for a given store size. If store size >= 8,
7406/// neon opcode will be returned.
7407static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7408 if (StSize >= 8)
7409 return StSize == 16 ? ARM::VST1q32wb_fixed
7410 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7411 if (IsThumb1)
7412 return StSize == 4 ? ARM::tSTRi
7413 : StSize == 2 ? ARM::tSTRHi
7414 : StSize == 1 ? ARM::tSTRBi : 0;
7415 if (IsThumb2)
7416 return StSize == 4 ? ARM::t2STR_POST
7417 : StSize == 2 ? ARM::t2STRH_POST
7418 : StSize == 1 ? ARM::t2STRB_POST : 0;
7419 return StSize == 4 ? ARM::STR_POST_IMM
7420 : StSize == 2 ? ARM::STRH_POST
7421 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7422}
7423
7424/// Emit a post-increment load operation with given size. The instructions
7425/// will be added to BB at Pos.
7426static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7427 const TargetInstrInfo *TII, DebugLoc dl,
7428 unsigned LdSize, unsigned Data, unsigned AddrIn,
7429 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7430 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7431 assert(LdOpc != 0 && "Should have a load opcode");
7432 if (LdSize >= 8) {
7433 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7434 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7435 .addImm(0));
7436 } else if (IsThumb1) {
7437 // load + update AddrIn
7438 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7439 .addReg(AddrIn).addImm(0));
7440 MachineInstrBuilder MIB =
7441 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7442 MIB = AddDefaultT1CC(MIB);
7443 MIB.addReg(AddrIn).addImm(LdSize);
7444 AddDefaultPred(MIB);
7445 } else if (IsThumb2) {
7446 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7447 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7448 .addImm(LdSize));
7449 } else { // arm
7450 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7451 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7452 .addReg(0).addImm(LdSize));
7453 }
7454}
7455
7456/// Emit a post-increment store operation with given size. The instructions
7457/// will be added to BB at Pos.
7458static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7459 const TargetInstrInfo *TII, DebugLoc dl,
7460 unsigned StSize, unsigned Data, unsigned AddrIn,
7461 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7462 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7463 assert(StOpc != 0 && "Should have a store opcode");
7464 if (StSize >= 8) {
7465 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7466 .addReg(AddrIn).addImm(0).addReg(Data));
7467 } else if (IsThumb1) {
7468 // store + update AddrIn
7469 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7470 .addReg(AddrIn).addImm(0));
7471 MachineInstrBuilder MIB =
7472 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7473 MIB = AddDefaultT1CC(MIB);
7474 MIB.addReg(AddrIn).addImm(StSize);
7475 AddDefaultPred(MIB);
7476 } else if (IsThumb2) {
7477 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7478 .addReg(Data).addReg(AddrIn).addImm(StSize));
7479 } else { // arm
7480 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7481 .addReg(Data).addReg(AddrIn).addReg(0)
7482 .addImm(StSize));
7483 }
7484}
7485
David Peixottoc32e24a2013-10-17 19:49:22 +00007486MachineBasicBlock *
7487ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7488 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007489 // This pseudo instruction has 3 operands: dst, src, size
7490 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7491 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007492 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007493 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007494 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007495
7496 unsigned dest = MI->getOperand(0).getReg();
7497 unsigned src = MI->getOperand(1).getReg();
7498 unsigned SizeVal = MI->getOperand(2).getImm();
7499 unsigned Align = MI->getOperand(3).getImm();
7500 DebugLoc dl = MI->getDebugLoc();
7501
Manman Rene8735522012-06-01 19:33:18 +00007502 MachineFunction *MF = BB->getParent();
7503 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007504 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007505 const TargetRegisterClass *TRC = nullptr;
7506 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007507
7508 bool IsThumb1 = Subtarget->isThumb1Only();
7509 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007510
7511 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007512 UnitSize = 1;
7513 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007514 UnitSize = 2;
7515 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007516 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007517 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007518 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007519 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007520 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007521 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007522 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007523 }
7524 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007525 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007526 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007527 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007528
David Peixottob0653e532013-10-24 16:39:36 +00007529 // Select the correct opcode and register class for unit size load/store
7530 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007531 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007532 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007533 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7534 : UnitSize == 8 ? &ARM::DPRRegClass
7535 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007536
Manman Rene8735522012-06-01 19:33:18 +00007537 unsigned BytesLeft = SizeVal % UnitSize;
7538 unsigned LoopSize = SizeVal - BytesLeft;
7539
7540 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7541 // Use LDR and STR to copy.
7542 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7543 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7544 unsigned srcIn = src;
7545 unsigned destIn = dest;
7546 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007547 unsigned srcOut = MRI.createVirtualRegister(TRC);
7548 unsigned destOut = MRI.createVirtualRegister(TRC);
7549 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007550 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7551 IsThumb1, IsThumb2);
7552 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7553 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007554 srcIn = srcOut;
7555 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007556 }
7557
7558 // Handle the leftover bytes with LDRB and STRB.
7559 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7560 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007561 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007562 unsigned srcOut = MRI.createVirtualRegister(TRC);
7563 unsigned destOut = MRI.createVirtualRegister(TRC);
7564 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007565 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7566 IsThumb1, IsThumb2);
7567 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7568 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007569 srcIn = srcOut;
7570 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007571 }
7572 MI->eraseFromParent(); // The instruction is gone now.
7573 return BB;
7574 }
7575
7576 // Expand the pseudo op to a loop.
7577 // thisMBB:
7578 // ...
7579 // movw varEnd, # --> with thumb2
7580 // movt varEnd, #
7581 // ldrcp varEnd, idx --> without thumb2
7582 // fallthrough --> loopMBB
7583 // loopMBB:
7584 // PHI varPhi, varEnd, varLoop
7585 // PHI srcPhi, src, srcLoop
7586 // PHI destPhi, dst, destLoop
7587 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7588 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7589 // subs varLoop, varPhi, #UnitSize
7590 // bne loopMBB
7591 // fallthrough --> exitMBB
7592 // exitMBB:
7593 // epilogue to handle left-over bytes
7594 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7595 // [destOut] = STRB_POST(scratch, destLoop, 1)
7596 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7597 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7598 MF->insert(It, loopMBB);
7599 MF->insert(It, exitMBB);
7600
7601 // Transfer the remainder of BB and its successor edges to exitMBB.
7602 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007603 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007604 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7605
7606 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007607 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007608 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007609 unsigned Vtmp = varEnd;
7610 if ((LoopSize & 0xFFFF0000) != 0)
7611 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007612 AddDefaultPred(BuildMI(BB, dl,
7613 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7614 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007615
7616 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007617 AddDefaultPred(BuildMI(BB, dl,
7618 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7619 varEnd)
7620 .addReg(Vtmp)
7621 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007622 } else {
7623 MachineConstantPool *ConstantPool = MF->getConstantPool();
7624 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7625 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7626
7627 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007628 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007629 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007630 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007631 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7632
7633 if (IsThumb1)
7634 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7635 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7636 else
7637 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7638 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7639 }
Manman Rene8735522012-06-01 19:33:18 +00007640 BB->addSuccessor(loopMBB);
7641
7642 // Generate the loop body:
7643 // varPhi = PHI(varLoop, varEnd)
7644 // srcPhi = PHI(srcLoop, src)
7645 // destPhi = PHI(destLoop, dst)
7646 MachineBasicBlock *entryBB = BB;
7647 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007648 unsigned varLoop = MRI.createVirtualRegister(TRC);
7649 unsigned varPhi = MRI.createVirtualRegister(TRC);
7650 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7651 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7652 unsigned destLoop = MRI.createVirtualRegister(TRC);
7653 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007654
7655 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7656 .addReg(varLoop).addMBB(loopMBB)
7657 .addReg(varEnd).addMBB(entryBB);
7658 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7659 .addReg(srcLoop).addMBB(loopMBB)
7660 .addReg(src).addMBB(entryBB);
7661 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7662 .addReg(destLoop).addMBB(loopMBB)
7663 .addReg(dest).addMBB(entryBB);
7664
7665 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7666 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007667 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007668 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7669 IsThumb1, IsThumb2);
7670 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7671 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007672
7673 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007674 if (IsThumb1) {
7675 MachineInstrBuilder MIB =
7676 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7677 MIB = AddDefaultT1CC(MIB);
7678 MIB.addReg(varPhi).addImm(UnitSize);
7679 AddDefaultPred(MIB);
7680 } else {
7681 MachineInstrBuilder MIB =
7682 BuildMI(*BB, BB->end(), dl,
7683 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7684 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7685 MIB->getOperand(5).setReg(ARM::CPSR);
7686 MIB->getOperand(5).setIsDef(true);
7687 }
7688 BuildMI(*BB, BB->end(), dl,
7689 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7690 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007691
7692 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7693 BB->addSuccessor(loopMBB);
7694 BB->addSuccessor(exitMBB);
7695
7696 // Add epilogue to handle BytesLeft.
7697 BB = exitMBB;
7698 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007699
7700 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7701 // [destOut] = STRB_POST(scratch, destLoop, 1)
7702 unsigned srcIn = srcLoop;
7703 unsigned destIn = destLoop;
7704 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007705 unsigned srcOut = MRI.createVirtualRegister(TRC);
7706 unsigned destOut = MRI.createVirtualRegister(TRC);
7707 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007708 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7709 IsThumb1, IsThumb2);
7710 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7711 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007712 srcIn = srcOut;
7713 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007714 }
7715
7716 MI->eraseFromParent(); // The instruction is gone now.
7717 return BB;
7718}
7719
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007720MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007721ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7722 MachineBasicBlock *MBB) const {
7723 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007724 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007725 DebugLoc DL = MI->getDebugLoc();
7726
7727 assert(Subtarget->isTargetWindows() &&
7728 "__chkstk is only supported on Windows");
7729 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7730
7731 // __chkstk takes the number of words to allocate on the stack in R4, and
7732 // returns the stack adjustment in number of bytes in R4. This will not
7733 // clober any other registers (other than the obvious lr).
7734 //
7735 // Although, technically, IP should be considered a register which may be
7736 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7737 // thumb-2 environment, so there is no interworking required. As a result, we
7738 // do not expect a veneer to be emitted by the linker, clobbering IP.
7739 //
Alp Toker1d099d92014-06-19 19:41:26 +00007740 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007741 // required, again, ensuring that IP is not clobbered.
7742 //
7743 // Finally, although some linkers may theoretically provide a trampoline for
7744 // out of range calls (which is quite common due to a 32M range limitation of
7745 // branches for Thumb), we can generate the long-call version via
7746 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7747 // IP.
7748
7749 switch (TM.getCodeModel()) {
7750 case CodeModel::Small:
7751 case CodeModel::Medium:
7752 case CodeModel::Default:
7753 case CodeModel::Kernel:
7754 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7755 .addImm((unsigned)ARMCC::AL).addReg(0)
7756 .addExternalSymbol("__chkstk")
7757 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7758 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7759 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7760 break;
7761 case CodeModel::Large:
7762 case CodeModel::JITDefault: {
7763 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7764 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7765
7766 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7767 .addExternalSymbol("__chkstk");
7768 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7769 .addImm((unsigned)ARMCC::AL).addReg(0)
7770 .addReg(Reg, RegState::Kill)
7771 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7772 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7773 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7774 break;
7775 }
7776 }
7777
7778 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7779 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007780 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007781
7782 MI->eraseFromParent();
7783 return MBB;
7784}
7785
7786MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007787ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
7788 MachineBasicBlock *MBB) const {
7789 DebugLoc DL = MI->getDebugLoc();
7790 MachineFunction *MF = MBB->getParent();
7791 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
7792
7793 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
7794 MF->push_back(ContBB);
7795 ContBB->splice(ContBB->begin(), MBB,
7796 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
7797 MBB->addSuccessor(ContBB);
7798
7799 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
7800 MF->push_back(TrapBB);
7801 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
7802 MBB->addSuccessor(TrapBB);
7803
7804 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
7805 .addReg(MI->getOperand(0).getReg())
7806 .addMBB(TrapBB);
7807
7808 MI->eraseFromParent();
7809 return ContBB;
7810}
7811
7812MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007813ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007814 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007815 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007816 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007817 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007818 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007819 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007820 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007821 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007822 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007823 // The Thumb2 pre-indexed stores have the same MI operands, they just
7824 // define them differently in the .td files from the isel patterns, so
7825 // they need pseudos.
7826 case ARM::t2STR_preidx:
7827 MI->setDesc(TII->get(ARM::t2STR_PRE));
7828 return BB;
7829 case ARM::t2STRB_preidx:
7830 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7831 return BB;
7832 case ARM::t2STRH_preidx:
7833 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7834 return BB;
7835
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007836 case ARM::STRi_preidx:
7837 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007838 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007839 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7840 // Decode the offset.
7841 unsigned Offset = MI->getOperand(4).getImm();
7842 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7843 Offset = ARM_AM::getAM2Offset(Offset);
7844 if (isSub)
7845 Offset = -Offset;
7846
Jim Grosbachf402f692011-08-12 21:02:34 +00007847 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007848 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007849 .addOperand(MI->getOperand(0)) // Rn_wb
7850 .addOperand(MI->getOperand(1)) // Rt
7851 .addOperand(MI->getOperand(2)) // Rn
7852 .addImm(Offset) // offset (skip GPR==zero_reg)
7853 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007854 .addOperand(MI->getOperand(6))
7855 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007856 MI->eraseFromParent();
7857 return BB;
7858 }
7859 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007860 case ARM::STRBr_preidx:
7861 case ARM::STRH_preidx: {
7862 unsigned NewOpc;
7863 switch (MI->getOpcode()) {
7864 default: llvm_unreachable("unexpected opcode!");
7865 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7866 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7867 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7868 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007869 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7870 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7871 MIB.addOperand(MI->getOperand(i));
7872 MI->eraseFromParent();
7873 return BB;
7874 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007875
Evan Chengbb2af352009-08-12 05:17:19 +00007876 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007877 // To "insert" a SELECT_CC instruction, we actually have to insert the
7878 // diamond control-flow pattern. The incoming instruction knows the
7879 // destination vreg to set, the condition code register to branch on, the
7880 // true/false values to select between, and a branch opcode to use.
7881 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007882 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00007883
7884 // thisMBB:
7885 // ...
7886 // TrueVal = ...
7887 // cmpTY ccX, r1, r2
7888 // bCC copy1MBB
7889 // fallthrough --> copy0MBB
7890 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007891 MachineFunction *F = BB->getParent();
7892 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7893 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007894 F->insert(It, copy0MBB);
7895 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007896
7897 // Transfer the remainder of BB and its successor edges to sinkMBB.
7898 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007899 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007900 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7901
Dan Gohmanf4f04102010-07-06 15:49:48 +00007902 BB->addSuccessor(copy0MBB);
7903 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007904
Dan Gohman34396292010-07-06 20:24:04 +00007905 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7906 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7907
Evan Cheng10043e22007-01-19 07:51:42 +00007908 // copy0MBB:
7909 // %FalseValue = ...
7910 // # fallthrough to sinkMBB
7911 BB = copy0MBB;
7912
7913 // Update machine-CFG edges
7914 BB->addSuccessor(sinkMBB);
7915
7916 // sinkMBB:
7917 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7918 // ...
7919 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007920 BuildMI(*BB, BB->begin(), dl,
7921 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007922 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7923 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7924
Dan Gohman34396292010-07-06 20:24:04 +00007925 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007926 return BB;
7927 }
Evan Chengb972e562009-08-07 00:34:42 +00007928
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007929 case ARM::BCCi64:
7930 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007931 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007932 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007933
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007934 // Compare both parts that make up the double comparison separately for
7935 // equality.
7936 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7937
7938 unsigned LHS1 = MI->getOperand(1).getReg();
7939 unsigned LHS2 = MI->getOperand(2).getReg();
7940 if (RHSisZero) {
7941 AddDefaultPred(BuildMI(BB, dl,
7942 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7943 .addReg(LHS1).addImm(0));
7944 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7945 .addReg(LHS2).addImm(0)
7946 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7947 } else {
7948 unsigned RHS1 = MI->getOperand(3).getReg();
7949 unsigned RHS2 = MI->getOperand(4).getReg();
7950 AddDefaultPred(BuildMI(BB, dl,
7951 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7952 .addReg(LHS1).addReg(RHS1));
7953 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7954 .addReg(LHS2).addReg(RHS2)
7955 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7956 }
7957
7958 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7959 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7960 if (MI->getOperand(0).getImm() == ARMCC::NE)
7961 std::swap(destMBB, exitMBB);
7962
7963 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7964 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007965 if (isThumb2)
7966 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7967 else
7968 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007969
7970 MI->eraseFromParent(); // The pseudo instruction is gone now.
7971 return BB;
7972 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007973
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007974 case ARM::Int_eh_sjlj_setjmp:
7975 case ARM::Int_eh_sjlj_setjmp_nofp:
7976 case ARM::tInt_eh_sjlj_setjmp:
7977 case ARM::t2Int_eh_sjlj_setjmp:
7978 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00007979 return BB;
7980
7981 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007982 EmitSjLjDispatchBlock(MI, BB);
7983 return BB;
7984
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007985 case ARM::ABS:
7986 case ARM::t2ABS: {
7987 // To insert an ABS instruction, we have to insert the
7988 // diamond control-flow pattern. The incoming instruction knows the
7989 // source vreg to test against 0, the destination vreg to set,
7990 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007991 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007992 // It transforms
7993 // V1 = ABS V0
7994 // into
7995 // V2 = MOVS V0
7996 // BCC (branch to SinkBB if V0 >= 0)
7997 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007998 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007999 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008000 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008001 MachineFunction *Fn = BB->getParent();
8002 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8003 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8004 Fn->insert(BBI, RSBBB);
8005 Fn->insert(BBI, SinkBB);
8006
8007 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8008 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008009 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008010 bool isThumb2 = Subtarget->isThumb2();
8011 MachineRegisterInfo &MRI = Fn->getRegInfo();
8012 // In Thumb mode S must not be specified if source register is the SP or
8013 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008014 unsigned NewRsbDstReg =
8015 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008016
8017 // Transfer the remainder of BB and its successor edges to sinkMBB.
8018 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008019 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008020 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8021
8022 BB->addSuccessor(RSBBB);
8023 BB->addSuccessor(SinkBB);
8024
8025 // fall through to SinkMBB
8026 RSBBB->addSuccessor(SinkBB);
8027
Manman Rene0763c72012-06-15 21:32:12 +00008028 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008029 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008030 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8031 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008032
8033 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008034 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008035 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8036 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8037
8038 // insert rsbri in RSBBB
8039 // Note: BCC and rsbri will be converted into predicated rsbmi
8040 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008041 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008042 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008043 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008044 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8045
Andrew Trick3f07c422011-10-18 18:40:53 +00008046 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008047 // reuse ABSDstReg to not change uses of ABS instruction
8048 BuildMI(*SinkBB, SinkBB->begin(), dl,
8049 TII->get(ARM::PHI), ABSDstReg)
8050 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008051 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008052
8053 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008054 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008055
8056 // return last added BB
8057 return SinkBB;
8058 }
Manman Rene8735522012-06-01 19:33:18 +00008059 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008060 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008061 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008062 case ARM::WIN__CHKSTK:
8063 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008064 case ARM::WIN__DBZCHK:
8065 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008066 }
8067}
8068
Scott Douglass953f9082015-10-05 14:49:54 +00008069/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8070/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8071/// instead of as a custom inserter because we need the use list from the SDNode.
8072static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8073 MachineInstr *MI, const SDNode *Node) {
8074 bool isThumb1 = Subtarget->isThumb1Only();
8075
8076 DebugLoc DL = MI->getDebugLoc();
8077 MachineFunction *MF = MI->getParent()->getParent();
8078 MachineRegisterInfo &MRI = MF->getRegInfo();
8079 MachineInstrBuilder MIB(*MF, MI);
8080
8081 // If the new dst/src is unused mark it as dead.
8082 if (!Node->hasAnyUseOfValue(0)) {
8083 MI->getOperand(0).setIsDead(true);
8084 }
8085 if (!Node->hasAnyUseOfValue(1)) {
8086 MI->getOperand(1).setIsDead(true);
8087 }
8088
8089 // The MEMCPY both defines and kills the scratch registers.
8090 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8091 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8092 : &ARM::GPRRegClass);
8093 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8094 }
8095}
8096
Evan Chenge6fba772011-08-30 19:09:48 +00008097void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8098 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008099 if (MI->getOpcode() == ARM::MEMCPY) {
8100 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8101 return;
8102 }
8103
Evan Cheng7f8e5632011-12-07 07:15:52 +00008104 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008105 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8106 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8107 // operand is still set to noreg. If needed, set the optional operand's
8108 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008109 //
Andrew Trick88b24502011-10-18 19:18:52 +00008110 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008111
Andrew Trick924123a2011-09-21 02:20:46 +00008112 // Rename pseudo opcodes.
8113 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8114 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008115 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008116 MCID = &TII->get(NewOpc);
8117
8118 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8119 "converted opcode should be the same except for cc_out");
8120
8121 MI->setDesc(*MCID);
8122
8123 // Add the optional cc_out operand
8124 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008125 }
Andrew Trick88b24502011-10-18 19:18:52 +00008126 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008127
8128 // Any ARM instruction that sets the 's' bit should specify an optional
8129 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008130 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008131 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008132 return;
8133 }
Andrew Trick924123a2011-09-21 02:20:46 +00008134 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8135 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008136 bool definesCPSR = false;
8137 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008138 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008139 i != e; ++i) {
8140 const MachineOperand &MO = MI->getOperand(i);
8141 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8142 definesCPSR = true;
8143 if (MO.isDead())
8144 deadCPSR = true;
8145 MI->RemoveOperand(i);
8146 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008147 }
8148 }
Andrew Trick8586e622011-09-20 03:17:40 +00008149 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008150 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008151 return;
8152 }
8153 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008154 if (deadCPSR) {
8155 assert(!MI->getOperand(ccOutIdx).getReg() &&
8156 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008157 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008158 }
Andrew Trick8586e622011-09-20 03:17:40 +00008159
Andrew Trick924123a2011-09-21 02:20:46 +00008160 // If this instruction was defined with an optional CPSR def and its dag node
8161 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008162 MachineOperand &MO = MI->getOperand(ccOutIdx);
8163 MO.setReg(ARM::CPSR);
8164 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008165}
8166
Evan Cheng10043e22007-01-19 07:51:42 +00008167//===----------------------------------------------------------------------===//
8168// ARM Optimization Hooks
8169//===----------------------------------------------------------------------===//
8170
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008171// Helper function that checks if N is a null or all ones constant.
8172static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
8173 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
8174 if (!C)
8175 return false;
8176 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
8177}
8178
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008179// Return true if N is conditionally 0 or all ones.
8180// Detects these expressions where cc is an i1 value:
8181//
8182// (select cc 0, y) [AllOnes=0]
8183// (select cc y, 0) [AllOnes=0]
8184// (zext cc) [AllOnes=0]
8185// (sext cc) [AllOnes=0/1]
8186// (select cc -1, y) [AllOnes=1]
8187// (select cc y, -1) [AllOnes=1]
8188//
8189// Invert is set when N is the null/all ones constant when CC is false.
8190// OtherOp is set to the alternative value of N.
8191static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8192 SDValue &CC, bool &Invert,
8193 SDValue &OtherOp,
8194 SelectionDAG &DAG) {
8195 switch (N->getOpcode()) {
8196 default: return false;
8197 case ISD::SELECT: {
8198 CC = N->getOperand(0);
8199 SDValue N1 = N->getOperand(1);
8200 SDValue N2 = N->getOperand(2);
8201 if (isZeroOrAllOnes(N1, AllOnes)) {
8202 Invert = false;
8203 OtherOp = N2;
8204 return true;
8205 }
8206 if (isZeroOrAllOnes(N2, AllOnes)) {
8207 Invert = true;
8208 OtherOp = N1;
8209 return true;
8210 }
8211 return false;
8212 }
8213 case ISD::ZERO_EXTEND:
8214 // (zext cc) can never be the all ones value.
8215 if (AllOnes)
8216 return false;
8217 // Fall through.
8218 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008219 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008220 EVT VT = N->getValueType(0);
8221 CC = N->getOperand(0);
8222 if (CC.getValueType() != MVT::i1)
8223 return false;
8224 Invert = !AllOnes;
8225 if (AllOnes)
8226 // When looking for an AllOnes constant, N is an sext, and the 'other'
8227 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008228 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008229 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8230 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008231 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008232 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008233 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8234 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008235 return true;
8236 }
8237 }
8238}
8239
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008240// Combine a constant select operand into its use:
8241//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008242// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8243// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8244// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8245// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8246// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008247//
8248// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008249// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008250//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008251// Also recognize sext/zext from i1:
8252//
8253// (add (zext cc), x) -> (select cc (add x, 1), x)
8254// (add (sext cc), x) -> (select cc (add x, -1), x)
8255//
8256// These transformations eventually create predicated instructions.
8257//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008258// @param N The node to transform.
8259// @param Slct The N operand that is a select.
8260// @param OtherOp The other N operand (x above).
8261// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008262// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008263// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008264static
8265SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008266 TargetLowering::DAGCombinerInfo &DCI,
8267 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008268 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008269 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008270 SDValue NonConstantVal;
8271 SDValue CCOp;
8272 bool SwapSelectOps;
8273 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8274 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008275 return SDValue();
8276
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008277 // Slct is now know to be the desired identity constant when CC is true.
8278 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008279 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008280 OtherOp, NonConstantVal);
8281 // Unless SwapSelectOps says CC should be false.
8282 if (SwapSelectOps)
8283 std::swap(TrueVal, FalseVal);
8284
Andrew Trickef9de2a2013-05-25 02:42:55 +00008285 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008286 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008287}
8288
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008289// Attempt combineSelectAndUse on each operand of a commutative operator N.
8290static
8291SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8292 TargetLowering::DAGCombinerInfo &DCI) {
8293 SDValue N0 = N->getOperand(0);
8294 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008295 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008296 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8297 if (Result.getNode())
8298 return Result;
8299 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008300 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008301 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8302 if (Result.getNode())
8303 return Result;
8304 }
8305 return SDValue();
8306}
8307
Eric Christopher1b8b94192011-06-29 21:10:36 +00008308// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008309// (only after legalization).
8310static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8311 TargetLowering::DAGCombinerInfo &DCI,
8312 const ARMSubtarget *Subtarget) {
8313
8314 // Only perform optimization if after legalize, and if NEON is available. We
8315 // also expected both operands to be BUILD_VECTORs.
8316 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8317 || N0.getOpcode() != ISD::BUILD_VECTOR
8318 || N1.getOpcode() != ISD::BUILD_VECTOR)
8319 return SDValue();
8320
8321 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8322 EVT VT = N->getValueType(0);
8323 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8324 return SDValue();
8325
8326 // Check that the vector operands are of the right form.
8327 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8328 // operands, where N is the size of the formed vector.
8329 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8330 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008331
8332 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008333 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008334 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008335 SDValue Vec = N0->getOperand(0)->getOperand(0);
8336 SDNode *V = Vec.getNode();
8337 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008338
Eric Christopher1b8b94192011-06-29 21:10:36 +00008339 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008340 // check to see if each of their operands are an EXTRACT_VECTOR with
8341 // the same vector and appropriate index.
8342 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8343 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8344 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008345
Tanya Lattnere9e67052011-06-14 23:48:48 +00008346 SDValue ExtVec0 = N0->getOperand(i);
8347 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008348
Tanya Lattnere9e67052011-06-14 23:48:48 +00008349 // First operand is the vector, verify its the same.
8350 if (V != ExtVec0->getOperand(0).getNode() ||
8351 V != ExtVec1->getOperand(0).getNode())
8352 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008353
Tanya Lattnere9e67052011-06-14 23:48:48 +00008354 // Second is the constant, verify its correct.
8355 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8356 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008357
Tanya Lattnere9e67052011-06-14 23:48:48 +00008358 // For the constant, we want to see all the even or all the odd.
8359 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8360 || C1->getZExtValue() != nextIndex+1)
8361 return SDValue();
8362
8363 // Increment index.
8364 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008365 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008366 return SDValue();
8367 }
8368
8369 // Create VPADDL node.
8370 SelectionDAG &DAG = DCI.DAG;
8371 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008372
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008373 SDLoc dl(N);
8374
Tanya Lattnere9e67052011-06-14 23:48:48 +00008375 // Build operand list.
8376 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008377 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008378 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008379
8380 // Input is the vector.
8381 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008382
Tanya Lattnere9e67052011-06-14 23:48:48 +00008383 // Get widened type and narrowed type.
8384 MVT widenType;
8385 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008386
Silviu Barangaa3106e62014-04-03 10:44:27 +00008387 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8388 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008389 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8390 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8391 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8392 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008393 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008394 }
8395
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008396 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008397 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008398 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008399}
8400
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008401static SDValue findMUL_LOHI(SDValue V) {
8402 if (V->getOpcode() == ISD::UMUL_LOHI ||
8403 V->getOpcode() == ISD::SMUL_LOHI)
8404 return V;
8405 return SDValue();
8406}
8407
8408static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8409 TargetLowering::DAGCombinerInfo &DCI,
8410 const ARMSubtarget *Subtarget) {
8411
8412 if (Subtarget->isThumb1Only()) return SDValue();
8413
8414 // Only perform the checks after legalize when the pattern is available.
8415 if (DCI.isBeforeLegalize()) return SDValue();
8416
8417 // Look for multiply add opportunities.
8418 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8419 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8420 // a glue link from the first add to the second add.
8421 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8422 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008423 // UMUL_LOHI
8424 // / :lo \ :hi
8425 // / \ [no multiline comment]
8426 // loAdd -> ADDE |
8427 // \ :glue /
8428 // \ /
8429 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008430 //
8431 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8432 SDValue AddcOp0 = AddcNode->getOperand(0);
8433 SDValue AddcOp1 = AddcNode->getOperand(1);
8434
8435 // Check if the two operands are from the same mul_lohi node.
8436 if (AddcOp0.getNode() == AddcOp1.getNode())
8437 return SDValue();
8438
8439 assert(AddcNode->getNumValues() == 2 &&
8440 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008441 "Expect ADDC with two result values. First: i32");
8442
8443 // Check that we have a glued ADDC node.
8444 if (AddcNode->getValueType(1) != MVT::Glue)
8445 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008446
8447 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8448 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8449 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8450 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8451 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8452 return SDValue();
8453
8454 // Look for the glued ADDE.
8455 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008456 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008457 return SDValue();
8458
8459 // Make sure it is really an ADDE.
8460 if (AddeNode->getOpcode() != ISD::ADDE)
8461 return SDValue();
8462
8463 assert(AddeNode->getNumOperands() == 3 &&
8464 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8465 "ADDE node has the wrong inputs");
8466
8467 // Check for the triangle shape.
8468 SDValue AddeOp0 = AddeNode->getOperand(0);
8469 SDValue AddeOp1 = AddeNode->getOperand(1);
8470
8471 // Make sure that the ADDE operands are not coming from the same node.
8472 if (AddeOp0.getNode() == AddeOp1.getNode())
8473 return SDValue();
8474
8475 // Find the MUL_LOHI node walking up ADDE's operands.
8476 bool IsLeftOperandMUL = false;
8477 SDValue MULOp = findMUL_LOHI(AddeOp0);
8478 if (MULOp == SDValue())
8479 MULOp = findMUL_LOHI(AddeOp1);
8480 else
8481 IsLeftOperandMUL = true;
8482 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008483 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008484
8485 // Figure out the right opcode.
8486 unsigned Opc = MULOp->getOpcode();
8487 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8488
8489 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008490 SDValue* HiAdd = nullptr;
8491 SDValue* LoMul = nullptr;
8492 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008493
Jyoti Allurf1d70502015-01-23 09:10:03 +00008494 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8495 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8496 return SDValue();
8497
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008498 if (IsLeftOperandMUL)
8499 HiAdd = &AddeOp1;
8500 else
8501 HiAdd = &AddeOp0;
8502
8503
Jyoti Allurf1d70502015-01-23 09:10:03 +00008504 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8505 // whose low result is fed to the ADDC we are checking.
8506
8507 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008508 LoMul = &AddcOp0;
8509 LowAdd = &AddcOp1;
8510 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008511 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008512 LoMul = &AddcOp1;
8513 LowAdd = &AddcOp0;
8514 }
8515
Craig Topper062a2ba2014-04-25 05:30:21 +00008516 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008517 return SDValue();
8518
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008519 // Create the merged node.
8520 SelectionDAG &DAG = DCI.DAG;
8521
8522 // Build operand list.
8523 SmallVector<SDValue, 8> Ops;
8524 Ops.push_back(LoMul->getOperand(0));
8525 Ops.push_back(LoMul->getOperand(1));
8526 Ops.push_back(*LowAdd);
8527 Ops.push_back(*HiAdd);
8528
Andrew Trickef9de2a2013-05-25 02:42:55 +00008529 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008530 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008531
8532 // Replace the ADDs' nodes uses by the MLA node's values.
8533 SDValue HiMLALResult(MLALNode.getNode(), 1);
8534 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8535
8536 SDValue LoMLALResult(MLALNode.getNode(), 0);
8537 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8538
8539 // Return original node to notify the driver to stop replacing.
8540 SDValue resNode(AddcNode, 0);
8541 return resNode;
8542}
8543
8544/// PerformADDCCombine - Target-specific dag combine transform from
8545/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8546static SDValue PerformADDCCombine(SDNode *N,
8547 TargetLowering::DAGCombinerInfo &DCI,
8548 const ARMSubtarget *Subtarget) {
8549
8550 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8551
8552}
8553
Bob Wilson728eb292010-07-29 20:34:14 +00008554/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8555/// operands N0 and N1. This is a helper for PerformADDCombine that is
8556/// called with the default operands, and if that fails, with commuted
8557/// operands.
8558static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008559 TargetLowering::DAGCombinerInfo &DCI,
8560 const ARMSubtarget *Subtarget){
8561
8562 // Attempt to create vpaddl for this add.
8563 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8564 if (Result.getNode())
8565 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008566
Chris Lattner4147f082009-03-12 06:52:53 +00008567 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008568 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008569 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8570 if (Result.getNode()) return Result;
8571 }
Chris Lattner4147f082009-03-12 06:52:53 +00008572 return SDValue();
8573}
8574
Bob Wilson728eb292010-07-29 20:34:14 +00008575/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8576///
8577static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008578 TargetLowering::DAGCombinerInfo &DCI,
8579 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008580 SDValue N0 = N->getOperand(0);
8581 SDValue N1 = N->getOperand(1);
8582
8583 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008584 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008585 if (Result.getNode())
8586 return Result;
8587
8588 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008589 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008590}
8591
Chris Lattner4147f082009-03-12 06:52:53 +00008592/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008593///
Chris Lattner4147f082009-03-12 06:52:53 +00008594static SDValue PerformSUBCombine(SDNode *N,
8595 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008596 SDValue N0 = N->getOperand(0);
8597 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008598
Chris Lattner4147f082009-03-12 06:52:53 +00008599 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008600 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008601 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8602 if (Result.getNode()) return Result;
8603 }
Bob Wilson7117a912009-03-20 22:42:55 +00008604
Chris Lattner4147f082009-03-12 06:52:53 +00008605 return SDValue();
8606}
8607
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008608/// PerformVMULCombine
8609/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8610/// special multiplier accumulator forwarding.
8611/// vmul d3, d0, d2
8612/// vmla d3, d1, d2
8613/// is faster than
8614/// vadd d3, d0, d1
8615/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008616// However, for (A + B) * (A + B),
8617// vadd d2, d0, d1
8618// vmul d3, d0, d2
8619// vmla d3, d1, d2
8620// is slower than
8621// vadd d2, d0, d1
8622// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008623static SDValue PerformVMULCombine(SDNode *N,
8624 TargetLowering::DAGCombinerInfo &DCI,
8625 const ARMSubtarget *Subtarget) {
8626 if (!Subtarget->hasVMLxForwarding())
8627 return SDValue();
8628
8629 SelectionDAG &DAG = DCI.DAG;
8630 SDValue N0 = N->getOperand(0);
8631 SDValue N1 = N->getOperand(1);
8632 unsigned Opcode = N0.getOpcode();
8633 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8634 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008635 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008636 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8637 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8638 return SDValue();
8639 std::swap(N0, N1);
8640 }
8641
Weiming Zhao2052f482013-09-25 23:12:06 +00008642 if (N0 == N1)
8643 return SDValue();
8644
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008645 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008646 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008647 SDValue N00 = N0->getOperand(0);
8648 SDValue N01 = N0->getOperand(1);
8649 return DAG.getNode(Opcode, DL, VT,
8650 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8651 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8652}
8653
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008654static SDValue PerformMULCombine(SDNode *N,
8655 TargetLowering::DAGCombinerInfo &DCI,
8656 const ARMSubtarget *Subtarget) {
8657 SelectionDAG &DAG = DCI.DAG;
8658
8659 if (Subtarget->isThumb1Only())
8660 return SDValue();
8661
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008662 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8663 return SDValue();
8664
8665 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008666 if (VT.is64BitVector() || VT.is128BitVector())
8667 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008668 if (VT != MVT::i32)
8669 return SDValue();
8670
8671 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8672 if (!C)
8673 return SDValue();
8674
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008675 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008676 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008677
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008678 ShiftAmt = ShiftAmt & (32 - 1);
8679 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008680 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008681
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008682 SDValue Res;
8683 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008684
8685 if (MulAmt >= 0) {
8686 if (isPowerOf2_32(MulAmt - 1)) {
8687 // (mul x, 2^N + 1) => (add (shl x, N), x)
8688 Res = DAG.getNode(ISD::ADD, DL, VT,
8689 V,
8690 DAG.getNode(ISD::SHL, DL, VT,
8691 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008692 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008693 MVT::i32)));
8694 } else if (isPowerOf2_32(MulAmt + 1)) {
8695 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8696 Res = DAG.getNode(ISD::SUB, DL, VT,
8697 DAG.getNode(ISD::SHL, DL, VT,
8698 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008699 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008700 MVT::i32)),
8701 V);
8702 } else
8703 return SDValue();
8704 } else {
8705 uint64_t MulAmtAbs = -MulAmt;
8706 if (isPowerOf2_32(MulAmtAbs + 1)) {
8707 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8708 Res = DAG.getNode(ISD::SUB, DL, VT,
8709 V,
8710 DAG.getNode(ISD::SHL, DL, VT,
8711 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008712 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008713 MVT::i32)));
8714 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8715 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8716 Res = DAG.getNode(ISD::ADD, DL, VT,
8717 V,
8718 DAG.getNode(ISD::SHL, DL, VT,
8719 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008720 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008721 MVT::i32)));
8722 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008723 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008724
8725 } else
8726 return SDValue();
8727 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008728
8729 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008730 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008731 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008732
8733 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008734 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008735 return SDValue();
8736}
8737
Owen Anderson30c48922010-11-05 19:27:46 +00008738static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008739 TargetLowering::DAGCombinerInfo &DCI,
8740 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008741
Owen Anderson30c48922010-11-05 19:27:46 +00008742 // Attempt to use immediate-form VBIC
8743 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008744 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008745 EVT VT = N->getValueType(0);
8746 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008747
Tanya Lattner266792a2011-04-07 15:24:20 +00008748 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8749 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008750
Owen Anderson30c48922010-11-05 19:27:46 +00008751 APInt SplatBits, SplatUndef;
8752 unsigned SplatBitSize;
8753 bool HasAnyUndefs;
8754 if (BVN &&
8755 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8756 if (SplatBitSize <= 64) {
8757 EVT VbicVT;
8758 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8759 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008760 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008761 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008762 if (Val.getNode()) {
8763 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008764 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008765 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008766 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008767 }
8768 }
8769 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008770
Evan Chenge87681c2012-02-23 01:19:06 +00008771 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008772 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8773 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8774 if (Result.getNode())
8775 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008776 }
8777
Owen Anderson30c48922010-11-05 19:27:46 +00008778 return SDValue();
8779}
8780
Jim Grosbach11013ed2010-07-16 23:05:05 +00008781/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8782static SDValue PerformORCombine(SDNode *N,
8783 TargetLowering::DAGCombinerInfo &DCI,
8784 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008785 // Attempt to use immediate-form VORR
8786 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008787 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008788 EVT VT = N->getValueType(0);
8789 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008790
Tanya Lattner266792a2011-04-07 15:24:20 +00008791 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8792 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008793
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008794 APInt SplatBits, SplatUndef;
8795 unsigned SplatBitSize;
8796 bool HasAnyUndefs;
8797 if (BVN && Subtarget->hasNEON() &&
8798 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8799 if (SplatBitSize <= 64) {
8800 EVT VorrVT;
8801 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8802 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008803 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008804 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008805 if (Val.getNode()) {
8806 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008807 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008808 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008809 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008810 }
8811 }
8812 }
8813
Evan Chenge87681c2012-02-23 01:19:06 +00008814 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008815 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8816 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8817 if (Result.getNode())
8818 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008819 }
8820
Nadav Rotem3a94c542012-08-13 18:52:44 +00008821 // The code below optimizes (or (and X, Y), Z).
8822 // The AND operand needs to have a single user to make these optimizations
8823 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008824 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008825 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008826 return SDValue();
8827 SDValue N1 = N->getOperand(1);
8828
8829 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8830 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8831 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8832 APInt SplatUndef;
8833 unsigned SplatBitSize;
8834 bool HasAnyUndefs;
8835
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008836 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008837 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008838 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8839 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008840 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008841 HasAnyUndefs) && !HasAnyUndefs) {
8842 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8843 HasAnyUndefs) && !HasAnyUndefs) {
8844 // Ensure that the bit width of the constants are the same and that
8845 // the splat arguments are logical inverses as per the pattern we
8846 // are trying to simplify.
8847 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8848 SplatBits0 == ~SplatBits1) {
8849 // Canonicalize the vector type to make instruction selection
8850 // simpler.
8851 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8852 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8853 N0->getOperand(1),
8854 N0->getOperand(0),
8855 N1->getOperand(0));
8856 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8857 }
8858 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008859 }
8860 }
8861
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008862 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8863 // reasonable.
8864
Jim Grosbach11013ed2010-07-16 23:05:05 +00008865 // BFI is only available on V6T2+
8866 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8867 return SDValue();
8868
Andrew Trickef9de2a2013-05-25 02:42:55 +00008869 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008870 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008871 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008872 //
8873 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008874 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008875 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008876 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008877 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008878 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008879
Jim Grosbach11013ed2010-07-16 23:05:05 +00008880 if (VT != MVT::i32)
8881 return SDValue();
8882
Evan Cheng2e51bb42010-12-13 20:32:54 +00008883 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008884
Jim Grosbach11013ed2010-07-16 23:05:05 +00008885 // The value and the mask need to be constants so we can verify this is
8886 // actually a bitfield set. If the mask is 0xffff, we can do better
8887 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008888 SDValue MaskOp = N0.getOperand(1);
8889 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8890 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008891 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008892 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008893 if (Mask == 0xffff)
8894 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008895 SDValue Res;
8896 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008897 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8898 if (N1C) {
8899 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008900 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008901 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008902
Evan Cheng34345752010-12-11 04:11:38 +00008903 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008904 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008905
Evan Cheng2e51bb42010-12-13 20:32:54 +00008906 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008907 DAG.getConstant(Val, DL, MVT::i32),
8908 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008909
8910 // Do not add new nodes to DAG combiner worklist.
8911 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008912 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008913 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008914 } else if (N1.getOpcode() == ISD::AND) {
8915 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008916 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8917 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008918 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008919 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008920
Eric Christopherd5530962011-03-26 01:21:03 +00008921 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8922 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008923 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008924 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008925 // The pack halfword instruction works better for masks that fit it,
8926 // so use that when it's available.
8927 if (Subtarget->hasT2ExtractPack() &&
8928 (Mask == 0xffff || Mask == 0xffff0000))
8929 return SDValue();
8930 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008931 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008932 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008933 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008934 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008935 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008936 // Do not add new nodes to DAG combiner worklist.
8937 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008938 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008939 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008940 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008941 // The pack halfword instruction works better for masks that fit it,
8942 // so use that when it's available.
8943 if (Subtarget->hasT2ExtractPack() &&
8944 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8945 return SDValue();
8946 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008947 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008948 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008949 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008950 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008951 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008952 // Do not add new nodes to DAG combiner worklist.
8953 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008954 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008955 }
8956 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008957
Evan Cheng2e51bb42010-12-13 20:32:54 +00008958 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8959 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8960 ARM::isBitFieldInvertedMask(~Mask)) {
8961 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8962 // where lsb(mask) == #shamt and masked bits of B are known zero.
8963 SDValue ShAmt = N00.getOperand(1);
8964 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008965 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008966 if (ShAmtC != LSB)
8967 return SDValue();
8968
8969 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008970 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008971
8972 // Do not add new nodes to DAG combiner worklist.
8973 DCI.CombineTo(N, Res, false);
8974 }
8975
Jim Grosbach11013ed2010-07-16 23:05:05 +00008976 return SDValue();
8977}
8978
Evan Chenge87681c2012-02-23 01:19:06 +00008979static SDValue PerformXORCombine(SDNode *N,
8980 TargetLowering::DAGCombinerInfo &DCI,
8981 const ARMSubtarget *Subtarget) {
8982 EVT VT = N->getValueType(0);
8983 SelectionDAG &DAG = DCI.DAG;
8984
8985 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8986 return SDValue();
8987
8988 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008989 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8990 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8991 if (Result.getNode())
8992 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008993 }
8994
8995 return SDValue();
8996}
8997
Evan Cheng6d02d902011-06-15 01:12:31 +00008998/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8999/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009000static SDValue PerformBFICombine(SDNode *N,
9001 TargetLowering::DAGCombinerInfo &DCI) {
9002 SDValue N1 = N->getOperand(1);
9003 if (N1.getOpcode() == ISD::AND) {
9004 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9005 if (!N11C)
9006 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009007 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009008 unsigned LSB = countTrailingZeros(~InvMask);
9009 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009010 assert(Width <
9011 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009012 "undefined behavior");
9013 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009014 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009015 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009016 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009017 N->getOperand(0), N1.getOperand(0),
9018 N->getOperand(2));
9019 }
9020 return SDValue();
9021}
9022
Bob Wilson22806742010-09-22 22:09:21 +00009023/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9024/// ARMISD::VMOVRRD.
9025static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009026 TargetLowering::DAGCombinerInfo &DCI,
9027 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009028 // vmovrrd(vmovdrr x, y) -> x,y
9029 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009030 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009031 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009032
9033 // vmovrrd(load f64) -> (load i32), (load i32)
9034 SDNode *InNode = InDouble.getNode();
9035 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9036 InNode->getValueType(0) == MVT::f64 &&
9037 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9038 !cast<LoadSDNode>(InNode)->isVolatile()) {
9039 // TODO: Should this be done for non-FrameIndex operands?
9040 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9041
9042 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009043 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009044 SDValue BasePtr = LD->getBasePtr();
9045 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9046 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009047 LD->isNonTemporal(), LD->isInvariant(),
9048 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009049
9050 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009051 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009052 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9053 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009054 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009055 std::min(4U, LD->getAlignment() / 2));
9056
9057 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009058 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009059 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009060 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009061 return Result;
9062 }
9063
Bob Wilson22806742010-09-22 22:09:21 +00009064 return SDValue();
9065}
9066
9067/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9068/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9069static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9070 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9071 SDValue Op0 = N->getOperand(0);
9072 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009073 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009074 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009075 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009076 Op1 = Op1.getOperand(0);
9077 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9078 Op0.getNode() == Op1.getNode() &&
9079 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009080 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009081 N->getValueType(0), Op0.getOperand(0));
9082 return SDValue();
9083}
9084
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009085/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9086/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9087/// i64 vector to have f64 elements, since the value can then be loaded
9088/// directly into a VFP register.
9089static bool hasNormalLoadOperand(SDNode *N) {
9090 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9091 for (unsigned i = 0; i < NumElts; ++i) {
9092 SDNode *Elt = N->getOperand(i).getNode();
9093 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9094 return true;
9095 }
9096 return false;
9097}
9098
Bob Wilsoncb6db982010-09-17 22:59:05 +00009099/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9100/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009101static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009102 TargetLowering::DAGCombinerInfo &DCI,
9103 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009104 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9105 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9106 // into a pair of GPRs, which is fine when the value is used as a scalar,
9107 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009108 SelectionDAG &DAG = DCI.DAG;
9109 if (N->getNumOperands() == 2) {
9110 SDValue RV = PerformVMOVDRRCombine(N, DAG);
9111 if (RV.getNode())
9112 return RV;
9113 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00009114
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009115 // Load i64 elements as f64 values so that type legalization does not split
9116 // them up into i32 values.
9117 EVT VT = N->getValueType(0);
9118 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9119 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009120 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009121 SmallVector<SDValue, 8> Ops;
9122 unsigned NumElts = VT.getVectorNumElements();
9123 for (unsigned i = 0; i < NumElts; ++i) {
9124 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9125 Ops.push_back(V);
9126 // Make the DAGCombiner fold the bitcast.
9127 DCI.AddToWorklist(V.getNode());
9128 }
9129 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009130 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009131 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9132}
9133
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009134/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9135static SDValue
9136PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9137 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9138 // At that time, we may have inserted bitcasts from integer to float.
9139 // If these bitcasts have survived DAGCombine, change the lowering of this
9140 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9141 // force to use floating point types.
9142
9143 // Make sure we can change the type of the vector.
9144 // This is possible iff:
9145 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9146 // 1.1. Vector is used only once.
9147 // 1.2. Use is a bit convert to an integer type.
9148 // 2. The size of its operands are 32-bits (64-bits are not legal).
9149 EVT VT = N->getValueType(0);
9150 EVT EltVT = VT.getVectorElementType();
9151
9152 // Check 1.1. and 2.
9153 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9154 return SDValue();
9155
9156 // By construction, the input type must be float.
9157 assert(EltVT == MVT::f32 && "Unexpected type!");
9158
9159 // Check 1.2.
9160 SDNode *Use = *N->use_begin();
9161 if (Use->getOpcode() != ISD::BITCAST ||
9162 Use->getValueType(0).isFloatingPoint())
9163 return SDValue();
9164
9165 // Check profitability.
9166 // Model is, if more than half of the relevant operands are bitcast from
9167 // i32, turn the build_vector into a sequence of insert_vector_elt.
9168 // Relevant operands are everything that is not statically
9169 // (i.e., at compile time) bitcasted.
9170 unsigned NumOfBitCastedElts = 0;
9171 unsigned NumElts = VT.getVectorNumElements();
9172 unsigned NumOfRelevantElts = NumElts;
9173 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9174 SDValue Elt = N->getOperand(Idx);
9175 if (Elt->getOpcode() == ISD::BITCAST) {
9176 // Assume only bit cast to i32 will go away.
9177 if (Elt->getOperand(0).getValueType() == MVT::i32)
9178 ++NumOfBitCastedElts;
9179 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
9180 // Constants are statically casted, thus do not count them as
9181 // relevant operands.
9182 --NumOfRelevantElts;
9183 }
9184
9185 // Check if more than half of the elements require a non-free bitcast.
9186 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9187 return SDValue();
9188
9189 SelectionDAG &DAG = DCI.DAG;
9190 // Create the new vector type.
9191 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9192 // Check if the type is legal.
9193 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9194 if (!TLI.isTypeLegal(VecVT))
9195 return SDValue();
9196
9197 // Combine:
9198 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9199 // => BITCAST INSERT_VECTOR_ELT
9200 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9201 // (BITCAST EN), N.
9202 SDValue Vec = DAG.getUNDEF(VecVT);
9203 SDLoc dl(N);
9204 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9205 SDValue V = N->getOperand(Idx);
9206 if (V.getOpcode() == ISD::UNDEF)
9207 continue;
9208 if (V.getOpcode() == ISD::BITCAST &&
9209 V->getOperand(0).getValueType() == MVT::i32)
9210 // Fold obvious case.
9211 V = V.getOperand(0);
9212 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009213 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009214 // Make the DAGCombiner fold the bitcasts.
9215 DCI.AddToWorklist(V.getNode());
9216 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009217 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009218 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9219 }
9220 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9221 // Make the DAGCombiner fold the bitcasts.
9222 DCI.AddToWorklist(Vec.getNode());
9223 return Vec;
9224}
9225
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009226/// PerformInsertEltCombine - Target-specific dag combine xforms for
9227/// ISD::INSERT_VECTOR_ELT.
9228static SDValue PerformInsertEltCombine(SDNode *N,
9229 TargetLowering::DAGCombinerInfo &DCI) {
9230 // Bitcast an i64 load inserted into a vector to f64.
9231 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9232 EVT VT = N->getValueType(0);
9233 SDNode *Elt = N->getOperand(1).getNode();
9234 if (VT.getVectorElementType() != MVT::i64 ||
9235 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9236 return SDValue();
9237
9238 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009239 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009240 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9241 VT.getVectorNumElements());
9242 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9243 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9244 // Make the DAGCombiner fold the bitcasts.
9245 DCI.AddToWorklist(Vec.getNode());
9246 DCI.AddToWorklist(V.getNode());
9247 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9248 Vec, V, N->getOperand(2));
9249 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009250}
9251
Bob Wilsonc7334a12010-10-27 20:38:28 +00009252/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9253/// ISD::VECTOR_SHUFFLE.
9254static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9255 // The LLVM shufflevector instruction does not require the shuffle mask
9256 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9257 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9258 // operands do not match the mask length, they are extended by concatenating
9259 // them with undef vectors. That is probably the right thing for other
9260 // targets, but for NEON it is better to concatenate two double-register
9261 // size vector operands into a single quad-register size vector. Do that
9262 // transformation here:
9263 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9264 // shuffle(concat(v1, v2), undef)
9265 SDValue Op0 = N->getOperand(0);
9266 SDValue Op1 = N->getOperand(1);
9267 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9268 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9269 Op0.getNumOperands() != 2 ||
9270 Op1.getNumOperands() != 2)
9271 return SDValue();
9272 SDValue Concat0Op1 = Op0.getOperand(1);
9273 SDValue Concat1Op1 = Op1.getOperand(1);
9274 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9275 Concat1Op1.getOpcode() != ISD::UNDEF)
9276 return SDValue();
9277 // Skip the transformation if any of the types are illegal.
9278 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9279 EVT VT = N->getValueType(0);
9280 if (!TLI.isTypeLegal(VT) ||
9281 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9282 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9283 return SDValue();
9284
Andrew Trickef9de2a2013-05-25 02:42:55 +00009285 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009286 Op0.getOperand(0), Op1.getOperand(0));
9287 // Translate the shuffle mask.
9288 SmallVector<int, 16> NewMask;
9289 unsigned NumElts = VT.getVectorNumElements();
9290 unsigned HalfElts = NumElts/2;
9291 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9292 for (unsigned n = 0; n < NumElts; ++n) {
9293 int MaskElt = SVN->getMaskElt(n);
9294 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009295 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009296 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009297 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009298 NewElt = HalfElts + MaskElt - NumElts;
9299 NewMask.push_back(NewElt);
9300 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009301 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009302 DAG.getUNDEF(VT), NewMask.data());
9303}
9304
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009305/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9306/// NEON load/store intrinsics, and generic vector load/stores, to merge
9307/// base address updates.
9308/// For generic load/stores, the memory type is assumed to be a vector.
9309/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009310static SDValue CombineBaseUpdate(SDNode *N,
9311 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009312 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009313 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9314 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009315 const bool isStore = N->getOpcode() == ISD::STORE;
9316 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009317 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009318 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009319 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009320
9321 // Search for a use of the address operand that is an increment.
9322 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9323 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9324 SDNode *User = *UI;
9325 if (User->getOpcode() != ISD::ADD ||
9326 UI.getUse().getResNo() != Addr.getResNo())
9327 continue;
9328
9329 // Check that the add is independent of the load/store. Otherwise, folding
9330 // it would create a cycle.
9331 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9332 continue;
9333
9334 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009335 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009336 bool isLaneOp = false;
9337 unsigned NewOpc = 0;
9338 unsigned NumVecs = 0;
9339 if (isIntrinsic) {
9340 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9341 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009342 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009343 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9344 NumVecs = 1; break;
9345 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9346 NumVecs = 2; break;
9347 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9348 NumVecs = 3; break;
9349 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9350 NumVecs = 4; break;
9351 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9352 NumVecs = 2; isLaneOp = true; break;
9353 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9354 NumVecs = 3; isLaneOp = true; break;
9355 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9356 NumVecs = 4; isLaneOp = true; break;
9357 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009358 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009359 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009360 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009361 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009362 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009363 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009364 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009365 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009366 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009367 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009368 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009369 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009370 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009371 }
9372 } else {
9373 isLaneOp = true;
9374 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009375 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009376 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9377 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9378 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009379 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9380 NumVecs = 1; isLaneOp = false; break;
9381 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9382 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009383 }
9384 }
9385
9386 // Find the size of memory referenced by the load/store.
9387 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009388 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009389 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009390 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009391 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009392 } else {
9393 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9394 VecTy = N->getOperand(1).getValueType();
9395 }
9396
Bob Wilson06fce872011-02-07 17:43:21 +00009397 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9398 if (isLaneOp)
9399 NumBytes /= VecTy.getVectorNumElements();
9400
9401 // If the increment is a constant, it must match the memory ref size.
9402 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9403 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9404 uint64_t IncVal = CInc->getZExtValue();
9405 if (IncVal != NumBytes)
9406 continue;
9407 } else if (NumBytes >= 3 * 16) {
9408 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9409 // separate instructions that make it harder to use a non-constant update.
9410 continue;
9411 }
9412
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009413 // OK, we found an ADD we can fold into the base update.
9414 // Now, create a _UPD node, taking care of not breaking alignment.
9415
9416 EVT AlignedVecTy = VecTy;
9417 unsigned Alignment = MemN->getAlignment();
9418
9419 // If this is a less-than-standard-aligned load/store, change the type to
9420 // match the standard alignment.
9421 // The alignment is overlooked when selecting _UPD variants; and it's
9422 // easier to introduce bitcasts here than fix that.
9423 // There are 3 ways to get to this base-update combine:
9424 // - intrinsics: they are assumed to be properly aligned (to the standard
9425 // alignment of the memory type), so we don't need to do anything.
9426 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9427 // intrinsics, so, likewise, there's nothing to do.
9428 // - generic load/store instructions: the alignment is specified as an
9429 // explicit operand, rather than implicitly as the standard alignment
9430 // of the memory type (like the intrisics). We need to change the
9431 // memory type to match the explicit alignment. That way, we don't
9432 // generate non-standard-aligned ARMISD::VLDx nodes.
9433 if (isa<LSBaseSDNode>(N)) {
9434 if (Alignment == 0)
9435 Alignment = 1;
9436 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9437 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9438 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9439 assert(!isLaneOp && "Unexpected generic load/store lane.");
9440 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9441 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9442 }
9443 // Don't set an explicit alignment on regular load/stores that we want
9444 // to transform to VLD/VST 1_UPD nodes.
9445 // This matches the behavior of regular load/stores, which only get an
9446 // explicit alignment if the MMO alignment is larger than the standard
9447 // alignment of the memory type.
9448 // Intrinsics, however, always get an explicit alignment, set to the
9449 // alignment of the MMO.
9450 Alignment = 1;
9451 }
9452
Bob Wilson06fce872011-02-07 17:43:21 +00009453 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009454 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009455 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009456 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009457 unsigned n;
9458 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009459 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009460 Tys[n++] = MVT::i32;
9461 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009462 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009463
9464 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009465 SmallVector<SDValue, 8> Ops;
9466 Ops.push_back(N->getOperand(0)); // incoming chain
9467 Ops.push_back(N->getOperand(AddrOpIdx));
9468 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009469
9470 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9471 // Try to match the intrinsic's signature
9472 Ops.push_back(StN->getValue());
9473 } else {
9474 // Loads (and of course intrinsics) match the intrinsics' signature,
9475 // so just add all but the alignment operand.
9476 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9477 Ops.push_back(N->getOperand(i));
9478 }
9479
9480 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009481 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009482
9483 // If this is a non-standard-aligned STORE, the penultimate operand is the
9484 // stored value. Bitcast it to the aligned type.
9485 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9486 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009487 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009488 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009489
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009490 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009491 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009492 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009493
9494 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009495 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009496 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009497 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009498
9499 // If this is an non-standard-aligned LOAD, the first result is the loaded
9500 // value. Bitcast it to the expected result type.
9501 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9502 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009503 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009504 }
9505
Bob Wilson06fce872011-02-07 17:43:21 +00009506 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9507 DCI.CombineTo(N, NewResults);
9508 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9509
9510 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009511 }
Bob Wilson06fce872011-02-07 17:43:21 +00009512 return SDValue();
9513}
9514
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009515static SDValue PerformVLDCombine(SDNode *N,
9516 TargetLowering::DAGCombinerInfo &DCI) {
9517 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9518 return SDValue();
9519
9520 return CombineBaseUpdate(N, DCI);
9521}
9522
Bob Wilson2d790df2010-11-28 06:51:26 +00009523/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9524/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9525/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9526/// return true.
9527static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9528 SelectionDAG &DAG = DCI.DAG;
9529 EVT VT = N->getValueType(0);
9530 // vldN-dup instructions only support 64-bit vectors for N > 1.
9531 if (!VT.is64BitVector())
9532 return false;
9533
9534 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9535 SDNode *VLD = N->getOperand(0).getNode();
9536 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9537 return false;
9538 unsigned NumVecs = 0;
9539 unsigned NewOpc = 0;
9540 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9541 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9542 NumVecs = 2;
9543 NewOpc = ARMISD::VLD2DUP;
9544 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9545 NumVecs = 3;
9546 NewOpc = ARMISD::VLD3DUP;
9547 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9548 NumVecs = 4;
9549 NewOpc = ARMISD::VLD4DUP;
9550 } else {
9551 return false;
9552 }
9553
9554 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9555 // numbers match the load.
9556 unsigned VLDLaneNo =
9557 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9558 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9559 UI != UE; ++UI) {
9560 // Ignore uses of the chain result.
9561 if (UI.getUse().getResNo() == NumVecs)
9562 continue;
9563 SDNode *User = *UI;
9564 if (User->getOpcode() != ARMISD::VDUPLANE ||
9565 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9566 return false;
9567 }
9568
9569 // Create the vldN-dup node.
9570 EVT Tys[5];
9571 unsigned n;
9572 for (n = 0; n < NumVecs; ++n)
9573 Tys[n] = VT;
9574 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009575 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009576 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9577 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009578 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009579 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009580 VLDMemInt->getMemOperand());
9581
9582 // Update the uses.
9583 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9584 UI != UE; ++UI) {
9585 unsigned ResNo = UI.getUse().getResNo();
9586 // Ignore uses of the chain result.
9587 if (ResNo == NumVecs)
9588 continue;
9589 SDNode *User = *UI;
9590 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9591 }
9592
9593 // Now the vldN-lane intrinsic is dead except for its chain result.
9594 // Update uses of the chain.
9595 std::vector<SDValue> VLDDupResults;
9596 for (unsigned n = 0; n < NumVecs; ++n)
9597 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9598 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9599 DCI.CombineTo(VLD, VLDDupResults);
9600
9601 return true;
9602}
9603
Bob Wilson103a0dc2010-07-14 01:22:12 +00009604/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9605/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009606static SDValue PerformVDUPLANECombine(SDNode *N,
9607 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009608 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009609
Bob Wilson2d790df2010-11-28 06:51:26 +00009610 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9611 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9612 if (CombineVLDDUP(N, DCI))
9613 return SDValue(N, 0);
9614
9615 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9616 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009617 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009618 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009619 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009620 return SDValue();
9621
9622 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9623 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9624 // The canonical VMOV for a zero vector uses a 32-bit element size.
9625 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9626 unsigned EltBits;
9627 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9628 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009629 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009630 if (EltSize > VT.getVectorElementType().getSizeInBits())
9631 return SDValue();
9632
Andrew Trickef9de2a2013-05-25 02:42:55 +00009633 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009634}
9635
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009636static SDValue PerformLOADCombine(SDNode *N,
9637 TargetLowering::DAGCombinerInfo &DCI) {
9638 EVT VT = N->getValueType(0);
9639
9640 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9641 if (ISD::isNormalLoad(N) && VT.isVector() &&
9642 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9643 return CombineBaseUpdate(N, DCI);
9644
9645 return SDValue();
9646}
9647
Ahmed Bougacha23167462014-12-09 21:26:53 +00009648/// PerformSTORECombine - Target-specific dag combine xforms for
9649/// ISD::STORE.
9650static SDValue PerformSTORECombine(SDNode *N,
9651 TargetLowering::DAGCombinerInfo &DCI) {
9652 StoreSDNode *St = cast<StoreSDNode>(N);
9653 if (St->isVolatile())
9654 return SDValue();
9655
9656 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9657 // pack all of the elements in one place. Next, store to memory in fewer
9658 // chunks.
9659 SDValue StVal = St->getValue();
9660 EVT VT = StVal.getValueType();
9661 if (St->isTruncatingStore() && VT.isVector()) {
9662 SelectionDAG &DAG = DCI.DAG;
9663 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9664 EVT StVT = St->getMemoryVT();
9665 unsigned NumElems = VT.getVectorNumElements();
9666 assert(StVT != VT && "Cannot truncate to the same type");
9667 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9668 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9669
9670 // From, To sizes and ElemCount must be pow of two
9671 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9672
9673 // We are going to use the original vector elt for storing.
9674 // Accumulated smaller vector elements must be a multiple of the store size.
9675 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9676
9677 unsigned SizeRatio = FromEltSz / ToEltSz;
9678 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9679
9680 // Create a type on which we perform the shuffle.
9681 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9682 NumElems*SizeRatio);
9683 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9684
9685 SDLoc DL(St);
9686 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9687 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9688 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009689 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9690 ? (i + 1) * SizeRatio - 1
9691 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009692
9693 // Can't shuffle using an illegal type.
9694 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9695
9696 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9697 DAG.getUNDEF(WideVec.getValueType()),
9698 ShuffleVec.data());
9699 // At this point all of the data is stored at the bottom of the
9700 // register. We now need to save it to mem.
9701
9702 // Find the largest store unit
9703 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009704 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009705 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9706 StoreType = Tp;
9707 }
9708 // Didn't find a legal store type.
9709 if (!TLI.isTypeLegal(StoreType))
9710 return SDValue();
9711
9712 // Bitcast the original vector into a vector of store-size units
9713 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9714 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9715 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9716 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9717 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009718 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9719 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009720 SDValue BasePtr = St->getBasePtr();
9721
9722 // Perform one or more big stores into memory.
9723 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9724 for (unsigned I = 0; I < E; I++) {
9725 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9726 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009727 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009728 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9729 St->getPointerInfo(), St->isVolatile(),
9730 St->isNonTemporal(), St->getAlignment());
9731 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9732 Increment);
9733 Chains.push_back(Ch);
9734 }
9735 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9736 }
9737
9738 if (!ISD::isNormalStore(St))
9739 return SDValue();
9740
9741 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9742 // ARM stores of arguments in the same cache line.
9743 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9744 StVal.getNode()->hasOneUse()) {
9745 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009746 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009747 SDLoc DL(St);
9748 SDValue BasePtr = St->getBasePtr();
9749 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9750 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9751 BasePtr, St->getPointerInfo(), St->isVolatile(),
9752 St->isNonTemporal(), St->getAlignment());
9753
9754 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009755 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009756 return DAG.getStore(NewST1.getValue(0), DL,
9757 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9758 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9759 St->isNonTemporal(),
9760 std::min(4U, St->getAlignment() / 2));
9761 }
9762
9763 if (StVal.getValueType() == MVT::i64 &&
9764 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9765
9766 // Bitcast an i64 store extracted from a vector to f64.
9767 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9768 SelectionDAG &DAG = DCI.DAG;
9769 SDLoc dl(StVal);
9770 SDValue IntVec = StVal.getOperand(0);
9771 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9772 IntVec.getValueType().getVectorNumElements());
9773 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9774 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9775 Vec, StVal.getOperand(1));
9776 dl = SDLoc(N);
9777 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9778 // Make the DAGCombiner fold the bitcasts.
9779 DCI.AddToWorklist(Vec.getNode());
9780 DCI.AddToWorklist(ExtElt.getNode());
9781 DCI.AddToWorklist(V.getNode());
9782 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9783 St->getPointerInfo(), St->isVolatile(),
9784 St->isNonTemporal(), St->getAlignment(),
9785 St->getAAInfo());
9786 }
9787
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009788 // If this is a legal vector store, try to combine it into a VST1_UPD.
9789 if (ISD::isNormalStore(N) && VT.isVector() &&
9790 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9791 return CombineBaseUpdate(N, DCI);
9792
Ahmed Bougacha23167462014-12-09 21:26:53 +00009793 return SDValue();
9794}
9795
Chad Rosierfa8d8932011-06-24 19:23:04 +00009796/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9797/// can replace combinations of VMUL and VCVT (floating-point to integer)
9798/// when the VMUL has a constant operand that is a power of 2.
9799///
9800/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9801/// vmul.f32 d16, d17, d16
9802/// vcvt.s32.f32 d16, d16
9803/// becomes:
9804/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +00009805static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009806 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +00009807 if (!Subtarget->hasNEON())
9808 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +00009809
Chad Rosiera087fd22015-10-06 20:23:42 +00009810 SDValue Op = N->getOperand(0);
9811 if (!Op.getValueType().isVector() || Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +00009812 return SDValue();
9813
Chad Rosierfa8d8932011-06-24 19:23:04 +00009814 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +00009815 if (!isa<BuildVectorSDNode>(ConstVec))
9816 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +00009817
Tim Northover7cbc2152013-06-28 15:29:25 +00009818 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +00009819 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +00009820 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +00009821 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +00009822 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +00009823 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009824 // These instructions only exist converting from f32 to i32. We can handle
9825 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009826 // be lossy. We also can't handle more then 4 lanes, since these intructions
9827 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009828 return SDValue();
9829 }
9830
Chad Rosier169865f2015-10-07 17:28:58 +00009831 BitVector UndefElements;
9832 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
9833 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
9834 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +00009835 return SDValue();
9836
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009837 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +00009838 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009839 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9840 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +00009841 SDValue FixConv = DAG.getNode(
9842 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9843 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +00009844 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009845
Chad Rosier9df4aff2015-10-06 20:45:45 +00009846 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009847 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009848
9849 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009850}
9851
9852/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9853/// can replace combinations of VCVT (integer to floating-point) and VDIV
9854/// when the VDIV has a constant operand that is a power of 2.
9855///
9856/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9857/// vcvt.f32.s32 d16, d16
9858/// vdiv.f32 d16, d17, d16
9859/// becomes:
9860/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +00009861static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009862 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +00009863 if (!Subtarget->hasNEON())
9864 return SDValue();
9865
Chad Rosierfa8d8932011-06-24 19:23:04 +00009866 SDValue Op = N->getOperand(0);
9867 unsigned OpOpcode = Op.getNode()->getOpcode();
Chad Rosiera087fd22015-10-06 20:23:42 +00009868 if (!N->getValueType(0).isVector() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009869 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9870 return SDValue();
9871
Chad Rosierfa8d8932011-06-24 19:23:04 +00009872 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +00009873 if (!isa<BuildVectorSDNode>(ConstVec))
9874 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +00009875
Tim Northover7cbc2152013-06-28 15:29:25 +00009876 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +00009877 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +00009878 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +00009879 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +00009880 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9881 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009882 // These instructions only exist converting from i32 to f32. We can handle
9883 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +00009884 // be lossy. We also can't handle more then 4 lanes, since these intructions
9885 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009886 return SDValue();
9887 }
9888
Chad Rosier169865f2015-10-07 17:28:58 +00009889 BitVector UndefElements;
9890 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
9891 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
9892 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +00009893 return SDValue();
9894
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009895 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +00009896 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +00009897 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +00009898 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +00009899 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009900 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009901 ConvInput);
9902
Eric Christopher1b8b94192011-06-29 21:10:36 +00009903 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009904 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009905 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009906 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009907 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +00009908 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009909}
9910
9911/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009912/// operand of a vector shift operation, where all the elements of the
9913/// build_vector must have the same constant integer value.
9914static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9915 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009916 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009917 Op = Op.getOperand(0);
9918 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9919 APInt SplatBits, SplatUndef;
9920 unsigned SplatBitSize;
9921 bool HasAnyUndefs;
9922 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9923 HasAnyUndefs, ElementBits) ||
9924 SplatBitSize > ElementBits)
9925 return false;
9926 Cnt = SplatBits.getSExtValue();
9927 return true;
9928}
9929
9930/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9931/// operand of a vector shift left operation. That value must be in the range:
9932/// 0 <= Value < ElementBits for a left shift; or
9933/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009934static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009935 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009936 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009937 if (! getVShiftImm(Op, ElementBits, Cnt))
9938 return false;
9939 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9940}
9941
9942/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9943/// operand of a vector shift right operation. For a shift opcode, the value
9944/// is positive, but for an intrinsic the value count must be negative. The
9945/// absolute value must be in the range:
9946/// 1 <= |Value| <= ElementBits for a right shift; or
9947/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009948static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009949 int64_t &Cnt) {
9950 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009951 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009952 if (! getVShiftImm(Op, ElementBits, Cnt))
9953 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009954 if (!isIntrinsic)
9955 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9956 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009957 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009958 return true;
9959 }
9960 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +00009961}
9962
9963/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9964static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9965 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9966 switch (IntNo) {
9967 default:
9968 // Don't do anything for most intrinsics.
9969 break;
9970
James Molloya6702e22015-07-17 17:10:55 +00009971 case Intrinsic::arm_neon_vabds:
9972 if (!N->getValueType(0).isInteger())
9973 return SDValue();
9974 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0),
9975 N->getOperand(1), N->getOperand(2));
9976 case Intrinsic::arm_neon_vabdu:
9977 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0),
9978 N->getOperand(1), N->getOperand(2));
9979
Bob Wilson2e076c42009-06-22 23:27:02 +00009980 // Vector shifts: check for immediate versions and lower them.
9981 // Note: This is done during DAG combining instead of DAG legalizing because
9982 // the build_vectors for 64-bit vector element shift counts are generally
9983 // not legal, and it is hard to see their values after they get legalized to
9984 // loads from a constant pool.
9985 case Intrinsic::arm_neon_vshifts:
9986 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009987 case Intrinsic::arm_neon_vrshifts:
9988 case Intrinsic::arm_neon_vrshiftu:
9989 case Intrinsic::arm_neon_vrshiftn:
9990 case Intrinsic::arm_neon_vqshifts:
9991 case Intrinsic::arm_neon_vqshiftu:
9992 case Intrinsic::arm_neon_vqshiftsu:
9993 case Intrinsic::arm_neon_vqshiftns:
9994 case Intrinsic::arm_neon_vqshiftnu:
9995 case Intrinsic::arm_neon_vqshiftnsu:
9996 case Intrinsic::arm_neon_vqrshiftns:
9997 case Intrinsic::arm_neon_vqrshiftnu:
9998 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009999 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010000 int64_t Cnt;
10001 unsigned VShiftOpc = 0;
10002
10003 switch (IntNo) {
10004 case Intrinsic::arm_neon_vshifts:
10005 case Intrinsic::arm_neon_vshiftu:
10006 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10007 VShiftOpc = ARMISD::VSHL;
10008 break;
10009 }
10010 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10011 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10012 ARMISD::VSHRs : ARMISD::VSHRu);
10013 break;
10014 }
10015 return SDValue();
10016
Bob Wilson2e076c42009-06-22 23:27:02 +000010017 case Intrinsic::arm_neon_vrshifts:
10018 case Intrinsic::arm_neon_vrshiftu:
10019 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10020 break;
10021 return SDValue();
10022
10023 case Intrinsic::arm_neon_vqshifts:
10024 case Intrinsic::arm_neon_vqshiftu:
10025 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10026 break;
10027 return SDValue();
10028
10029 case Intrinsic::arm_neon_vqshiftsu:
10030 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10031 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010032 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010033
Bob Wilson2e076c42009-06-22 23:27:02 +000010034 case Intrinsic::arm_neon_vrshiftn:
10035 case Intrinsic::arm_neon_vqshiftns:
10036 case Intrinsic::arm_neon_vqshiftnu:
10037 case Intrinsic::arm_neon_vqshiftnsu:
10038 case Intrinsic::arm_neon_vqrshiftns:
10039 case Intrinsic::arm_neon_vqrshiftnu:
10040 case Intrinsic::arm_neon_vqrshiftnsu:
10041 // Narrowing shifts require an immediate right shift.
10042 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10043 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010044 llvm_unreachable("invalid shift count for narrowing vector shift "
10045 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010046
10047 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010048 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010049 }
10050
10051 switch (IntNo) {
10052 case Intrinsic::arm_neon_vshifts:
10053 case Intrinsic::arm_neon_vshiftu:
10054 // Opcode already set above.
10055 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010056 case Intrinsic::arm_neon_vrshifts:
10057 VShiftOpc = ARMISD::VRSHRs; break;
10058 case Intrinsic::arm_neon_vrshiftu:
10059 VShiftOpc = ARMISD::VRSHRu; break;
10060 case Intrinsic::arm_neon_vrshiftn:
10061 VShiftOpc = ARMISD::VRSHRN; break;
10062 case Intrinsic::arm_neon_vqshifts:
10063 VShiftOpc = ARMISD::VQSHLs; break;
10064 case Intrinsic::arm_neon_vqshiftu:
10065 VShiftOpc = ARMISD::VQSHLu; break;
10066 case Intrinsic::arm_neon_vqshiftsu:
10067 VShiftOpc = ARMISD::VQSHLsu; break;
10068 case Intrinsic::arm_neon_vqshiftns:
10069 VShiftOpc = ARMISD::VQSHRNs; break;
10070 case Intrinsic::arm_neon_vqshiftnu:
10071 VShiftOpc = ARMISD::VQSHRNu; break;
10072 case Intrinsic::arm_neon_vqshiftnsu:
10073 VShiftOpc = ARMISD::VQSHRNsu; break;
10074 case Intrinsic::arm_neon_vqrshiftns:
10075 VShiftOpc = ARMISD::VQRSHRNs; break;
10076 case Intrinsic::arm_neon_vqrshiftnu:
10077 VShiftOpc = ARMISD::VQRSHRNu; break;
10078 case Intrinsic::arm_neon_vqrshiftnsu:
10079 VShiftOpc = ARMISD::VQRSHRNsu; break;
10080 }
10081
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010082 SDLoc dl(N);
10083 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10084 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010085 }
10086
10087 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010088 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010089 int64_t Cnt;
10090 unsigned VShiftOpc = 0;
10091
10092 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10093 VShiftOpc = ARMISD::VSLI;
10094 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10095 VShiftOpc = ARMISD::VSRI;
10096 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010097 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010098 }
10099
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010100 SDLoc dl(N);
10101 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010102 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010103 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010104 }
10105
10106 case Intrinsic::arm_neon_vqrshifts:
10107 case Intrinsic::arm_neon_vqrshiftu:
10108 // No immediate versions of these to check for.
10109 break;
10110 }
10111
10112 return SDValue();
10113}
10114
10115/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10116/// lowers them. As with the vector shift intrinsics, this is done during DAG
10117/// combining instead of DAG legalizing because the build_vectors for 64-bit
10118/// vector element shift counts are generally not legal, and it is hard to see
10119/// their values after they get legalized to loads from a constant pool.
10120static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10121 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010122 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010123 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10124 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10125 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10126 SDValue N1 = N->getOperand(1);
10127 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10128 SDValue N0 = N->getOperand(0);
10129 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10130 DAG.MaskedValueIsZero(N0.getOperand(0),
10131 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010132 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010133 }
10134 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010135
10136 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010137 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10138 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010139 return SDValue();
10140
10141 assert(ST->hasNEON() && "unexpected vector shift");
10142 int64_t Cnt;
10143
10144 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010145 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010146
10147 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010148 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10149 SDLoc dl(N);
10150 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10151 DAG.getConstant(Cnt, dl, MVT::i32));
10152 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010153 break;
10154
10155 case ISD::SRA:
10156 case ISD::SRL:
10157 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10158 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10159 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010160 SDLoc dl(N);
10161 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10162 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010163 }
10164 }
10165 return SDValue();
10166}
10167
10168/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10169/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10170static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10171 const ARMSubtarget *ST) {
10172 SDValue N0 = N->getOperand(0);
10173
10174 // Check for sign- and zero-extensions of vector extract operations of 8-
10175 // and 16-bit vector elements. NEON supports these directly. They are
10176 // handled during DAG combining because type legalization will promote them
10177 // to 32-bit types and it is messy to recognize the operations after that.
10178 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10179 SDValue Vec = N0.getOperand(0);
10180 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010181 EVT VT = N->getValueType(0);
10182 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010183 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10184
Owen Anderson9f944592009-08-11 20:47:22 +000010185 if (VT == MVT::i32 &&
10186 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010187 TLI.isTypeLegal(Vec.getValueType()) &&
10188 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010189
10190 unsigned Opc = 0;
10191 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010192 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010193 case ISD::SIGN_EXTEND:
10194 Opc = ARMISD::VGETLANEs;
10195 break;
10196 case ISD::ZERO_EXTEND:
10197 case ISD::ANY_EXTEND:
10198 Opc = ARMISD::VGETLANEu;
10199 break;
10200 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010201 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010202 }
10203 }
10204
10205 return SDValue();
10206}
10207
Evan Chengf863e3f2011-07-13 00:42:17 +000010208/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10209SDValue
10210ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10211 SDValue Cmp = N->getOperand(4);
10212 if (Cmp.getOpcode() != ARMISD::CMPZ)
10213 // Only looking at EQ and NE cases.
10214 return SDValue();
10215
10216 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010217 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010218 SDValue LHS = Cmp.getOperand(0);
10219 SDValue RHS = Cmp.getOperand(1);
10220 SDValue FalseVal = N->getOperand(0);
10221 SDValue TrueVal = N->getOperand(1);
10222 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010223 ARMCC::CondCodes CC =
10224 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010225
10226 // Simplify
10227 // mov r1, r0
10228 // cmp r1, x
10229 // mov r0, y
10230 // moveq r0, x
10231 // to
10232 // cmp r0, x
10233 // movne r0, y
10234 //
10235 // mov r1, r0
10236 // cmp r1, x
10237 // mov r0, x
10238 // movne r0, y
10239 // to
10240 // cmp r0, x
10241 // movne r0, y
10242 /// FIXME: Turn this into a target neutral optimization?
10243 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010244 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010245 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10246 N->getOperand(3), Cmp);
10247 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10248 SDValue ARMcc;
10249 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10250 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10251 N->getOperand(3), NewCmp);
10252 }
10253
10254 if (Res.getNode()) {
10255 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010256 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010257 // Capture demanded bits information that would be otherwise lost.
10258 if (KnownZero == 0xfffffffe)
10259 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10260 DAG.getValueType(MVT::i1));
10261 else if (KnownZero == 0xffffff00)
10262 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10263 DAG.getValueType(MVT::i8));
10264 else if (KnownZero == 0xffff0000)
10265 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10266 DAG.getValueType(MVT::i16));
10267 }
10268
10269 return Res;
10270}
10271
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010272SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010273 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010274 switch (N->getOpcode()) {
10275 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010276 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010277 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010278 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010279 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010280 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010281 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10282 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010283 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010284 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010285 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010286 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010287 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010288 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010289 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010290 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010291 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010292 case ISD::FP_TO_UINT:
10293 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10294 case ISD::FDIV:
10295 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010296 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010297 case ISD::SHL:
10298 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010299 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010300 case ISD::SIGN_EXTEND:
10301 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010302 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010303 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010304 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010305 case ARMISD::VLD2DUP:
10306 case ARMISD::VLD3DUP:
10307 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010308 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010309 case ARMISD::BUILD_VECTOR:
10310 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010311 case ISD::INTRINSIC_VOID:
10312 case ISD::INTRINSIC_W_CHAIN:
10313 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10314 case Intrinsic::arm_neon_vld1:
10315 case Intrinsic::arm_neon_vld2:
10316 case Intrinsic::arm_neon_vld3:
10317 case Intrinsic::arm_neon_vld4:
10318 case Intrinsic::arm_neon_vld2lane:
10319 case Intrinsic::arm_neon_vld3lane:
10320 case Intrinsic::arm_neon_vld4lane:
10321 case Intrinsic::arm_neon_vst1:
10322 case Intrinsic::arm_neon_vst2:
10323 case Intrinsic::arm_neon_vst3:
10324 case Intrinsic::arm_neon_vst4:
10325 case Intrinsic::arm_neon_vst2lane:
10326 case Intrinsic::arm_neon_vst3lane:
10327 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010328 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010329 default: break;
10330 }
10331 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010332 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010333 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010334}
10335
Evan Chengd42641c2011-02-02 01:06:55 +000010336bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10337 EVT VT) const {
10338 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10339}
10340
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010341bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10342 unsigned,
10343 unsigned,
10344 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010345 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010346 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010347
10348 switch (VT.getSimpleVT().SimpleTy) {
10349 default:
10350 return false;
10351 case MVT::i8:
10352 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010353 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010354 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010355 if (AllowsUnaligned) {
10356 if (Fast)
10357 *Fast = Subtarget->hasV7Ops();
10358 return true;
10359 }
10360 return false;
10361 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010362 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010363 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010364 // For any little-endian targets with neon, we can support unaligned ld/st
10365 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010366 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010367 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010368 if (Fast)
10369 *Fast = true;
10370 return true;
10371 }
10372 return false;
10373 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010374 }
10375}
10376
Lang Hames9929c422011-11-02 22:52:45 +000010377static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10378 unsigned AlignCheck) {
10379 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10380 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10381}
10382
10383EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10384 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010385 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010386 bool MemcpyStrSrc,
10387 MachineFunction &MF) const {
10388 const Function *F = MF.getFunction();
10389
10390 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010391 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10392 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010393 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010394 if (Size >= 16 &&
10395 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010396 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010397 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010398 } else if (Size >= 8 &&
10399 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010400 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10401 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010402 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010403 }
10404 }
10405
Lang Hamesb85fcd02011-11-08 18:56:23 +000010406 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010407 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010408 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010409 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010410 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010411
Lang Hames9929c422011-11-02 22:52:45 +000010412 // Let the target-independent logic figure it out.
10413 return MVT::Other;
10414}
10415
Evan Cheng9ec512d2012-12-06 19:13:27 +000010416bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10417 if (Val.getOpcode() != ISD::LOAD)
10418 return false;
10419
10420 EVT VT1 = Val.getValueType();
10421 if (!VT1.isSimple() || !VT1.isInteger() ||
10422 !VT2.isSimple() || !VT2.isInteger())
10423 return false;
10424
10425 switch (VT1.getSimpleVT().SimpleTy) {
10426 default: break;
10427 case MVT::i1:
10428 case MVT::i8:
10429 case MVT::i16:
10430 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10431 return true;
10432 }
10433
10434 return false;
10435}
10436
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010437bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10438 EVT VT = ExtVal.getValueType();
10439
10440 if (!isTypeLegal(VT))
10441 return false;
10442
10443 // Don't create a loadext if we can fold the extension into a wide/long
10444 // instruction.
10445 // If there's more than one user instruction, the loadext is desirable no
10446 // matter what. There can be two uses by the same instruction.
10447 if (ExtVal->use_empty() ||
10448 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10449 return true;
10450
10451 SDNode *U = *ExtVal->use_begin();
10452 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10453 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10454 return false;
10455
10456 return true;
10457}
10458
Tim Northovercc2e9032013-08-06 13:58:03 +000010459bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10460 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10461 return false;
10462
10463 if (!isTypeLegal(EVT::getEVT(Ty1)))
10464 return false;
10465
10466 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10467
10468 // Assuming the caller doesn't have a zeroext or signext return parameter,
10469 // truncation all the way down to i1 is valid.
10470 return true;
10471}
10472
10473
Evan Chengdc49a8d2009-08-14 20:09:37 +000010474static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10475 if (V < 0)
10476 return false;
10477
10478 unsigned Scale = 1;
10479 switch (VT.getSimpleVT().SimpleTy) {
10480 default: return false;
10481 case MVT::i1:
10482 case MVT::i8:
10483 // Scale == 1;
10484 break;
10485 case MVT::i16:
10486 // Scale == 2;
10487 Scale = 2;
10488 break;
10489 case MVT::i32:
10490 // Scale == 4;
10491 Scale = 4;
10492 break;
10493 }
10494
10495 if ((V & (Scale - 1)) != 0)
10496 return false;
10497 V /= Scale;
10498 return V == (V & ((1LL << 5) - 1));
10499}
10500
10501static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10502 const ARMSubtarget *Subtarget) {
10503 bool isNeg = false;
10504 if (V < 0) {
10505 isNeg = true;
10506 V = - V;
10507 }
10508
10509 switch (VT.getSimpleVT().SimpleTy) {
10510 default: return false;
10511 case MVT::i1:
10512 case MVT::i8:
10513 case MVT::i16:
10514 case MVT::i32:
10515 // + imm12 or - imm8
10516 if (isNeg)
10517 return V == (V & ((1LL << 8) - 1));
10518 return V == (V & ((1LL << 12) - 1));
10519 case MVT::f32:
10520 case MVT::f64:
10521 // Same as ARM mode. FIXME: NEON?
10522 if (!Subtarget->hasVFP2())
10523 return false;
10524 if ((V & 3) != 0)
10525 return false;
10526 V >>= 2;
10527 return V == (V & ((1LL << 8) - 1));
10528 }
10529}
10530
Evan Cheng2150b922007-03-12 23:30:29 +000010531/// isLegalAddressImmediate - Return true if the integer value can be used
10532/// as the offset of the target addressing mode for load / store of the
10533/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010534static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010535 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010536 if (V == 0)
10537 return true;
10538
Evan Chengce5dfb62009-03-09 19:15:00 +000010539 if (!VT.isSimple())
10540 return false;
10541
Evan Chengdc49a8d2009-08-14 20:09:37 +000010542 if (Subtarget->isThumb1Only())
10543 return isLegalT1AddressImmediate(V, VT);
10544 else if (Subtarget->isThumb2())
10545 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010546
Evan Chengdc49a8d2009-08-14 20:09:37 +000010547 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010548 if (V < 0)
10549 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010550 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010551 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010552 case MVT::i1:
10553 case MVT::i8:
10554 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010555 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010556 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010557 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010558 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010559 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010560 case MVT::f32:
10561 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010562 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010563 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010564 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010565 return false;
10566 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010567 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010568 }
Evan Cheng10043e22007-01-19 07:51:42 +000010569}
10570
Evan Chengdc49a8d2009-08-14 20:09:37 +000010571bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10572 EVT VT) const {
10573 int Scale = AM.Scale;
10574 if (Scale < 0)
10575 return false;
10576
10577 switch (VT.getSimpleVT().SimpleTy) {
10578 default: return false;
10579 case MVT::i1:
10580 case MVT::i8:
10581 case MVT::i16:
10582 case MVT::i32:
10583 if (Scale == 1)
10584 return true;
10585 // r + r << imm
10586 Scale = Scale & ~1;
10587 return Scale == 2 || Scale == 4 || Scale == 8;
10588 case MVT::i64:
10589 // r + r
10590 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10591 return true;
10592 return false;
10593 case MVT::isVoid:
10594 // Note, we allow "void" uses (basically, uses that aren't loads or
10595 // stores), because arm allows folding a scale into many arithmetic
10596 // operations. This should be made more precise and revisited later.
10597
10598 // Allow r << imm, but the imm has to be a multiple of two.
10599 if (Scale & 1) return false;
10600 return isPowerOf2_32(Scale);
10601 }
10602}
10603
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010604/// isLegalAddressingMode - Return true if the addressing mode represented
10605/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010606bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10607 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010608 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010609 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010610 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010611 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010612
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010613 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010614 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010615 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010616
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010617 switch (AM.Scale) {
10618 case 0: // no scale reg, must be "r+i" or "r", or "i".
10619 break;
10620 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010621 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010622 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010623 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010624 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010625 // ARM doesn't support any R+R*scale+imm addr modes.
10626 if (AM.BaseOffs)
10627 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010628
Bob Wilson866c1742009-04-08 17:55:28 +000010629 if (!VT.isSimple())
10630 return false;
10631
Evan Chengdc49a8d2009-08-14 20:09:37 +000010632 if (Subtarget->isThumb2())
10633 return isLegalT2ScaledAddressingMode(AM, VT);
10634
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010635 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010636 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010637 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010638 case MVT::i1:
10639 case MVT::i8:
10640 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010641 if (Scale < 0) Scale = -Scale;
10642 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010643 return true;
10644 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010645 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010646 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010647 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010648 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010649 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010650 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010651 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010652
Owen Anderson9f944592009-08-11 20:47:22 +000010653 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010654 // Note, we allow "void" uses (basically, uses that aren't loads or
10655 // stores), because arm allows folding a scale into many arithmetic
10656 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010657
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010658 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010659 if (Scale & 1) return false;
10660 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010661 }
Evan Cheng2150b922007-03-12 23:30:29 +000010662 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010663 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010664}
10665
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010666/// isLegalICmpImmediate - Return true if the specified immediate is legal
10667/// icmp immediate, that is the target has icmp instructions which can compare
10668/// a register against the immediate without having to materialize the
10669/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010670bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010671 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010672 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010673 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010674 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010675 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010676 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010677 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010678}
10679
Andrew Tricka22cdb72012-07-18 18:34:27 +000010680/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10681/// *or sub* immediate, that is the target has add or sub instructions which can
10682/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010683/// immediate into a register.
10684bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010685 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010686 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010687 if (!Subtarget->isThumb())
10688 return ARM_AM::getSOImmVal(AbsImm) != -1;
10689 if (Subtarget->isThumb2())
10690 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10691 // Thumb1 only has 8-bit unsigned immediate.
10692 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010693}
10694
Owen Anderson53aa7a92009-08-10 22:56:29 +000010695static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010696 bool isSEXTLoad, SDValue &Base,
10697 SDValue &Offset, bool &isInc,
10698 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010699 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10700 return false;
10701
Owen Anderson9f944592009-08-11 20:47:22 +000010702 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010703 // AddressingMode 3
10704 Base = Ptr->getOperand(0);
10705 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010706 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010707 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010708 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010709 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010710 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010711 return true;
10712 }
10713 }
10714 isInc = (Ptr->getOpcode() == ISD::ADD);
10715 Offset = Ptr->getOperand(1);
10716 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010717 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010718 // AddressingMode 2
10719 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010720 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010721 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010722 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010723 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010724 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010725 Base = Ptr->getOperand(0);
10726 return true;
10727 }
10728 }
10729
10730 if (Ptr->getOpcode() == ISD::ADD) {
10731 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010732 ARM_AM::ShiftOpc ShOpcVal=
10733 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010734 if (ShOpcVal != ARM_AM::no_shift) {
10735 Base = Ptr->getOperand(1);
10736 Offset = Ptr->getOperand(0);
10737 } else {
10738 Base = Ptr->getOperand(0);
10739 Offset = Ptr->getOperand(1);
10740 }
10741 return true;
10742 }
10743
10744 isInc = (Ptr->getOpcode() == ISD::ADD);
10745 Base = Ptr->getOperand(0);
10746 Offset = Ptr->getOperand(1);
10747 return true;
10748 }
10749
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010750 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010751 return false;
10752}
10753
Owen Anderson53aa7a92009-08-10 22:56:29 +000010754static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010755 bool isSEXTLoad, SDValue &Base,
10756 SDValue &Offset, bool &isInc,
10757 SelectionDAG &DAG) {
10758 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10759 return false;
10760
10761 Base = Ptr->getOperand(0);
10762 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10763 int RHSC = (int)RHS->getZExtValue();
10764 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10765 assert(Ptr->getOpcode() == ISD::ADD);
10766 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010767 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010768 return true;
10769 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10770 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010771 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010772 return true;
10773 }
10774 }
10775
10776 return false;
10777}
10778
Evan Cheng10043e22007-01-19 07:51:42 +000010779/// getPreIndexedAddressParts - returns true by value, base pointer and
10780/// offset pointer and addressing mode by reference if the node's address
10781/// can be legally represented as pre-indexed load / store address.
10782bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010783ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10784 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010785 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010786 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010787 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010788 return false;
10789
Owen Anderson53aa7a92009-08-10 22:56:29 +000010790 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010791 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010792 bool isSEXTLoad = false;
10793 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10794 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010795 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010796 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10797 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10798 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010799 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010800 } else
10801 return false;
10802
10803 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010804 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010805 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010806 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10807 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010808 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010809 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010810 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010811 if (!isLegal)
10812 return false;
10813
10814 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10815 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010816}
10817
10818/// getPostIndexedAddressParts - returns true by value, base pointer and
10819/// offset pointer and addressing mode by reference if this node can be
10820/// combined with a load / store to form a post-indexed load / store.
10821bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010822 SDValue &Base,
10823 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010824 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010825 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010826 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010827 return false;
10828
Owen Anderson53aa7a92009-08-10 22:56:29 +000010829 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010830 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010831 bool isSEXTLoad = false;
10832 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010833 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010834 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010835 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10836 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010837 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010838 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010839 } else
10840 return false;
10841
10842 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010843 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010844 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010845 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010846 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010847 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010848 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10849 isInc, DAG);
10850 if (!isLegal)
10851 return false;
10852
Evan Chengf19384d2010-05-18 21:31:17 +000010853 if (Ptr != Base) {
10854 // Swap base ptr and offset to catch more post-index load / store when
10855 // it's legal. In Thumb2 mode, offset must be an immediate.
10856 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10857 !Subtarget->isThumb2())
10858 std::swap(Base, Offset);
10859
10860 // Post-indexed load / store update the base pointer.
10861 if (Ptr != Base)
10862 return false;
10863 }
10864
Evan Cheng84c6cda2009-07-02 07:28:31 +000010865 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10866 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010867}
10868
Jay Foada0653a32014-05-14 21:14:37 +000010869void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10870 APInt &KnownZero,
10871 APInt &KnownOne,
10872 const SelectionDAG &DAG,
10873 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010874 unsigned BitWidth = KnownOne.getBitWidth();
10875 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010876 switch (Op.getOpcode()) {
10877 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010878 case ARMISD::ADDC:
10879 case ARMISD::ADDE:
10880 case ARMISD::SUBC:
10881 case ARMISD::SUBE:
10882 // These nodes' second result is a boolean
10883 if (Op.getResNo() == 0)
10884 break;
10885 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10886 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010887 case ARMISD::CMOV: {
10888 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010889 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010890 if (KnownZero == 0 && KnownOne == 0) return;
10891
Dan Gohmanf990faf2008-02-13 00:35:47 +000010892 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010893 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010894 KnownZero &= KnownZeroRHS;
10895 KnownOne &= KnownOneRHS;
10896 return;
10897 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010898 case ISD::INTRINSIC_W_CHAIN: {
10899 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10900 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10901 switch (IntID) {
10902 default: return;
10903 case Intrinsic::arm_ldaex:
10904 case Intrinsic::arm_ldrex: {
10905 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10906 unsigned MemBits = VT.getScalarType().getSizeInBits();
10907 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10908 return;
10909 }
10910 }
10911 }
Evan Cheng10043e22007-01-19 07:51:42 +000010912 }
10913}
10914
10915//===----------------------------------------------------------------------===//
10916// ARM Inline Assembly Support
10917//===----------------------------------------------------------------------===//
10918
Evan Cheng078b0b02011-01-08 01:24:27 +000010919bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10920 // Looking for "rev" which is V6+.
10921 if (!Subtarget->hasV6Ops())
10922 return false;
10923
10924 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10925 std::string AsmStr = IA->getAsmString();
10926 SmallVector<StringRef, 4> AsmPieces;
10927 SplitString(AsmStr, AsmPieces, ";\n");
10928
10929 switch (AsmPieces.size()) {
10930 default: return false;
10931 case 1:
10932 AsmStr = AsmPieces[0];
10933 AsmPieces.clear();
10934 SplitString(AsmStr, AsmPieces, " \t,");
10935
10936 // rev $0, $1
10937 if (AsmPieces.size() == 3 &&
10938 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10939 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010940 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010941 if (Ty && Ty->getBitWidth() == 32)
10942 return IntrinsicLowering::LowerToByteSwap(CI);
10943 }
10944 break;
10945 }
10946
10947 return false;
10948}
10949
Evan Cheng10043e22007-01-19 07:51:42 +000010950/// getConstraintType - Given a constraint letter, return the type of
10951/// constraint it is for this target.
10952ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010953ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000010954 if (Constraint.size() == 1) {
10955 switch (Constraint[0]) {
10956 default: break;
10957 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010958 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010959 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010960 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010961 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010962 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010963 // An address with a single base register. Due to the way we
10964 // currently handle addresses it is the same as an 'r' memory constraint.
10965 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010966 }
Eric Christophere256cd02011-06-21 22:10:57 +000010967 } else if (Constraint.size() == 2) {
10968 switch (Constraint[0]) {
10969 default: break;
10970 // All 'U+' constraints are addresses.
10971 case 'U': return C_Memory;
10972 }
Evan Cheng10043e22007-01-19 07:51:42 +000010973 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010974 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010975}
10976
John Thompsone8360b72010-10-29 17:29:13 +000010977/// Examine constraint type and operand type and determine a weight value.
10978/// This object must already have been set up with the operand type
10979/// and the current alternative constraint selected.
10980TargetLowering::ConstraintWeight
10981ARMTargetLowering::getSingleConstraintMatchWeight(
10982 AsmOperandInfo &info, const char *constraint) const {
10983 ConstraintWeight weight = CW_Invalid;
10984 Value *CallOperandVal = info.CallOperandVal;
10985 // If we don't have a value, we can't do a match,
10986 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010987 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010988 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010989 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010990 // Look at the constraint type.
10991 switch (*constraint) {
10992 default:
10993 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10994 break;
10995 case 'l':
10996 if (type->isIntegerTy()) {
10997 if (Subtarget->isThumb())
10998 weight = CW_SpecificReg;
10999 else
11000 weight = CW_Register;
11001 }
11002 break;
11003 case 'w':
11004 if (type->isFloatingPointTy())
11005 weight = CW_Register;
11006 break;
11007 }
11008 return weight;
11009}
11010
Eric Christophercf2007c2011-06-30 23:50:52 +000011011typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011012RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11013 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011014 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011015 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011016 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011017 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011018 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011019 return RCPair(0U, &ARM::tGPRRegClass);
11020 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011021 case 'h': // High regs or no regs.
11022 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011023 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011024 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011025 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011026 if (Subtarget->isThumb1Only())
11027 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011028 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011029 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011030 if (VT == MVT::Other)
11031 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011032 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011033 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011034 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011035 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011036 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011037 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011038 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011039 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011040 if (VT == MVT::Other)
11041 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011042 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011043 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011044 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011045 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011046 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011047 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011048 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011049 case 't':
11050 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011051 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011052 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011053 }
11054 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011055 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011056 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011057
Eric Christopher11e4df72015-02-26 22:38:43 +000011058 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011059}
11060
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011061/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11062/// vector. If it is invalid, don't add anything to Ops.
11063void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011064 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011065 std::vector<SDValue>&Ops,
11066 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011067 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011068
Eric Christopherde9399b2011-06-02 23:16:42 +000011069 // Currently only support length 1 constraints.
11070 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011071
Eric Christopherde9399b2011-06-02 23:16:42 +000011072 char ConstraintLetter = Constraint[0];
11073 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011074 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011075 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011076 case 'I': case 'J': case 'K': case 'L':
11077 case 'M': case 'N': case 'O':
11078 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11079 if (!C)
11080 return;
11081
11082 int64_t CVal64 = C->getSExtValue();
11083 int CVal = (int) CVal64;
11084 // None of these constraints allow values larger than 32 bits. Check
11085 // that the value fits in an int.
11086 if (CVal != CVal64)
11087 return;
11088
Eric Christopherde9399b2011-06-02 23:16:42 +000011089 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011090 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011091 // Constant suitable for movw, must be between 0 and
11092 // 65535.
11093 if (Subtarget->hasV6T2Ops())
11094 if (CVal >= 0 && CVal <= 65535)
11095 break;
11096 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011097 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011098 if (Subtarget->isThumb1Only()) {
11099 // This must be a constant between 0 and 255, for ADD
11100 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011101 if (CVal >= 0 && CVal <= 255)
11102 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011103 } else if (Subtarget->isThumb2()) {
11104 // A constant that can be used as an immediate value in a
11105 // data-processing instruction.
11106 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11107 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011108 } else {
11109 // A constant that can be used as an immediate value in a
11110 // data-processing instruction.
11111 if (ARM_AM::getSOImmVal(CVal) != -1)
11112 break;
11113 }
11114 return;
11115
11116 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000011117 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011118 // This must be a constant between -255 and -1, for negated ADD
11119 // immediates. This can be used in GCC with an "n" modifier that
11120 // prints the negated value, for use with SUB instructions. It is
11121 // not useful otherwise but is implemented for compatibility.
11122 if (CVal >= -255 && CVal <= -1)
11123 break;
11124 } else {
11125 // This must be a constant between -4095 and 4095. It is not clear
11126 // what this constraint is intended for. Implemented for
11127 // compatibility with GCC.
11128 if (CVal >= -4095 && CVal <= 4095)
11129 break;
11130 }
11131 return;
11132
11133 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011134 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011135 // A 32-bit value where only one byte has a nonzero value. Exclude
11136 // zero to match GCC. This constraint is used by GCC internally for
11137 // constants that can be loaded with a move/shift combination.
11138 // It is not useful otherwise but is implemented for compatibility.
11139 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11140 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011141 } else if (Subtarget->isThumb2()) {
11142 // A constant whose bitwise inverse can be used as an immediate
11143 // value in a data-processing instruction. This can be used in GCC
11144 // with a "B" modifier that prints the inverted value, for use with
11145 // BIC and MVN instructions. It is not useful otherwise but is
11146 // implemented for compatibility.
11147 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11148 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011149 } else {
11150 // A constant whose bitwise inverse can be used as an immediate
11151 // value in a data-processing instruction. This can be used in GCC
11152 // with a "B" modifier that prints the inverted value, for use with
11153 // BIC and MVN instructions. It is not useful otherwise but is
11154 // implemented for compatibility.
11155 if (ARM_AM::getSOImmVal(~CVal) != -1)
11156 break;
11157 }
11158 return;
11159
11160 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011161 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011162 // This must be a constant between -7 and 7,
11163 // for 3-operand ADD/SUB immediate instructions.
11164 if (CVal >= -7 && CVal < 7)
11165 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011166 } else if (Subtarget->isThumb2()) {
11167 // A constant whose negation can be used as an immediate value in a
11168 // data-processing instruction. This can be used in GCC with an "n"
11169 // modifier that prints the negated value, for use with SUB
11170 // instructions. It is not useful otherwise but is implemented for
11171 // compatibility.
11172 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11173 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011174 } else {
11175 // A constant whose negation can be used as an immediate value in a
11176 // data-processing instruction. This can be used in GCC with an "n"
11177 // modifier that prints the negated value, for use with SUB
11178 // instructions. It is not useful otherwise but is implemented for
11179 // compatibility.
11180 if (ARM_AM::getSOImmVal(-CVal) != -1)
11181 break;
11182 }
11183 return;
11184
11185 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000011186 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011187 // This must be a multiple of 4 between 0 and 1020, for
11188 // ADD sp + immediate.
11189 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11190 break;
11191 } else {
11192 // A power of two or a constant between 0 and 32. This is used in
11193 // GCC for the shift amount on shifted register operands, but it is
11194 // useful in general for any shift amounts.
11195 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11196 break;
11197 }
11198 return;
11199
11200 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011201 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011202 // This must be a constant between 0 and 31, for shift amounts.
11203 if (CVal >= 0 && CVal <= 31)
11204 break;
11205 }
11206 return;
11207
11208 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011209 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011210 // This must be a multiple of 4 between -508 and 508, for
11211 // ADD/SUB sp = sp + immediate.
11212 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11213 break;
11214 }
11215 return;
11216 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011217 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011218 break;
11219 }
11220
11221 if (Result.getNode()) {
11222 Ops.push_back(Result);
11223 return;
11224 }
Dale Johannesence97d552010-06-25 21:55:36 +000011225 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011226}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011227
Scott Douglassd2974a62015-08-24 09:17:11 +000011228static RTLIB::Libcall getDivRemLibcall(
11229 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011230 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11231 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011232 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011233 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11234 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011235 RTLIB::Libcall LC;
11236 switch (SVT) {
11237 default: llvm_unreachable("Unexpected request for libcall!");
11238 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11239 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11240 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11241 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11242 }
11243 return LC;
11244}
11245
11246static TargetLowering::ArgListTy getDivRemArgList(
11247 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011248 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11249 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011250 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011251 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11252 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011253 TargetLowering::ArgListTy Args;
11254 TargetLowering::ArgListEntry Entry;
11255 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11256 EVT ArgVT = N->getOperand(i).getValueType();
11257 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11258 Entry.Node = N->getOperand(i);
11259 Entry.Ty = ArgTy;
11260 Entry.isSExt = isSigned;
11261 Entry.isZExt = !isSigned;
11262 Args.push_back(Entry);
11263 }
11264 return Args;
11265}
11266
Renato Golin87610692013-07-16 09:32:17 +000011267SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011268 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) &&
11269 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011270 unsigned Opcode = Op->getOpcode();
11271 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011272 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011273 bool isSigned = (Opcode == ISD::SDIVREM);
11274 EVT VT = Op->getValueType(0);
11275 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11276
Scott Douglassd2974a62015-08-24 09:17:11 +000011277 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11278 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011279 SDValue InChain = DAG.getEntryNode();
11280
Scott Douglassd2974a62015-08-24 09:17:11 +000011281 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11282 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011283
11284 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011285 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011286
Reid Kleckner343c3952014-11-20 23:51:47 +000011287 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011288
11289 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011290 TargetLowering::CallLoweringInfo CLI(DAG);
11291 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011292 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011293 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011294
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011295 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011296 return CallInfo.first;
11297}
11298
Scott Douglassbdef6042015-08-24 09:17:18 +000011299// Lowers REM using divmod helpers
11300// see RTABI section 4.2/4.3
11301SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11302 // Build return types (div and rem)
11303 std::vector<Type*> RetTyParams;
11304 Type *RetTyElement;
11305
11306 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11307 default: llvm_unreachable("Unexpected request for libcall!");
11308 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11309 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11310 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11311 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11312 }
11313
11314 RetTyParams.push_back(RetTyElement);
11315 RetTyParams.push_back(RetTyElement);
11316 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11317 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11318
11319 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11320 SimpleTy);
11321 SDValue InChain = DAG.getEntryNode();
11322 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11323 bool isSigned = N->getOpcode() == ISD::SREM;
11324 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11325 getPointerTy(DAG.getDataLayout()));
11326
11327 // Lower call
11328 CallLoweringInfo CLI(DAG);
11329 CLI.setChain(InChain)
11330 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11331 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11332 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11333
11334 // Return second (rem) result operand (first contains div)
11335 SDNode *ResNode = CallResult.first.getNode();
11336 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11337 return ResNode->getOperand(1);
11338}
11339
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011340SDValue
11341ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11342 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11343 SDLoc DL(Op);
11344
11345 // Get the inputs.
11346 SDValue Chain = Op.getOperand(0);
11347 SDValue Size = Op.getOperand(1);
11348
11349 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011350 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011351
11352 SDValue Flag;
11353 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11354 Flag = Chain.getValue(1);
11355
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011356 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011357 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11358
11359 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11360 Chain = NewSP.getValue(1);
11361
11362 SDValue Ops[2] = { NewSP, Chain };
11363 return DAG.getMergeValues(Ops, DL);
11364}
11365
Oliver Stannard51b1d462014-08-21 12:50:31 +000011366SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11367 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11368 "Unexpected type for custom-lowering FP_EXTEND");
11369
11370 RTLIB::Libcall LC;
11371 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11372
11373 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011374 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11375 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011376}
11377
11378SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11379 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11380 Subtarget->isFPOnlySP() &&
11381 "Unexpected type for custom-lowering FP_ROUND");
11382
11383 RTLIB::Libcall LC;
11384 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11385
11386 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011387 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11388 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011389}
11390
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011391bool
11392ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11393 // The ARM target isn't yet aware of offsets.
11394 return false;
11395}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011396
Jim Grosbach11013ed2010-07-16 23:05:05 +000011397bool ARM::isBitFieldInvertedMask(unsigned v) {
11398 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011399 return false;
11400
Jim Grosbach11013ed2010-07-16 23:05:05 +000011401 // there can be 1's on either or both "outsides", all the "inside"
11402 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011403 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011404}
11405
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011406/// isFPImmLegal - Returns true if the target can instruction select the
11407/// specified FP immediate natively. If false, the legalizer will
11408/// materialize the FP immediate as a load from a constant pool.
11409bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11410 if (!Subtarget->hasVFP3())
11411 return false;
11412 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011413 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011414 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011415 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011416 return false;
11417}
Bob Wilson5549d492010-09-21 17:56:22 +000011418
Wesley Peck527da1b2010-11-23 03:31:01 +000011419/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011420/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11421/// specified in the intrinsic calls.
11422bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11423 const CallInst &I,
11424 unsigned Intrinsic) const {
11425 switch (Intrinsic) {
11426 case Intrinsic::arm_neon_vld1:
11427 case Intrinsic::arm_neon_vld2:
11428 case Intrinsic::arm_neon_vld3:
11429 case Intrinsic::arm_neon_vld4:
11430 case Intrinsic::arm_neon_vld2lane:
11431 case Intrinsic::arm_neon_vld3lane:
11432 case Intrinsic::arm_neon_vld4lane: {
11433 Info.opc = ISD::INTRINSIC_W_CHAIN;
11434 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011435 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11436 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011437 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11438 Info.ptrVal = I.getArgOperand(0);
11439 Info.offset = 0;
11440 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11441 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11442 Info.vol = false; // volatile loads with NEON intrinsics not supported
11443 Info.readMem = true;
11444 Info.writeMem = false;
11445 return true;
11446 }
11447 case Intrinsic::arm_neon_vst1:
11448 case Intrinsic::arm_neon_vst2:
11449 case Intrinsic::arm_neon_vst3:
11450 case Intrinsic::arm_neon_vst4:
11451 case Intrinsic::arm_neon_vst2lane:
11452 case Intrinsic::arm_neon_vst3lane:
11453 case Intrinsic::arm_neon_vst4lane: {
11454 Info.opc = ISD::INTRINSIC_VOID;
11455 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011456 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011457 unsigned NumElts = 0;
11458 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011459 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011460 if (!ArgTy->isVectorTy())
11461 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011462 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011463 }
11464 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11465 Info.ptrVal = I.getArgOperand(0);
11466 Info.offset = 0;
11467 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11468 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11469 Info.vol = false; // volatile stores with NEON intrinsics not supported
11470 Info.readMem = false;
11471 Info.writeMem = true;
11472 return true;
11473 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011474 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011475 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011476 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011477 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11478 Info.opc = ISD::INTRINSIC_W_CHAIN;
11479 Info.memVT = MVT::getVT(PtrTy->getElementType());
11480 Info.ptrVal = I.getArgOperand(0);
11481 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011482 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011483 Info.vol = true;
11484 Info.readMem = true;
11485 Info.writeMem = false;
11486 return true;
11487 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011488 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011489 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011490 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011491 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11492 Info.opc = ISD::INTRINSIC_W_CHAIN;
11493 Info.memVT = MVT::getVT(PtrTy->getElementType());
11494 Info.ptrVal = I.getArgOperand(1);
11495 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011496 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011497 Info.vol = true;
11498 Info.readMem = false;
11499 Info.writeMem = true;
11500 return true;
11501 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011502 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011503 case Intrinsic::arm_strexd: {
11504 Info.opc = ISD::INTRINSIC_W_CHAIN;
11505 Info.memVT = MVT::i64;
11506 Info.ptrVal = I.getArgOperand(2);
11507 Info.offset = 0;
11508 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011509 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011510 Info.readMem = false;
11511 Info.writeMem = true;
11512 return true;
11513 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011514 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011515 case Intrinsic::arm_ldrexd: {
11516 Info.opc = ISD::INTRINSIC_W_CHAIN;
11517 Info.memVT = MVT::i64;
11518 Info.ptrVal = I.getArgOperand(0);
11519 Info.offset = 0;
11520 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011521 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011522 Info.readMem = true;
11523 Info.writeMem = false;
11524 return true;
11525 }
Bob Wilson5549d492010-09-21 17:56:22 +000011526 default:
11527 break;
11528 }
11529
11530 return false;
11531}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011532
11533/// \brief Returns true if it is beneficial to convert a load of a constant
11534/// to just the constant itself.
11535bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11536 Type *Ty) const {
11537 assert(Ty->isIntegerTy());
11538
11539 unsigned Bits = Ty->getPrimitiveSizeInBits();
11540 if (Bits == 0 || Bits > 32)
11541 return false;
11542 return true;
11543}
Tim Northover037f26f22014-04-17 18:22:47 +000011544
Robin Morisset5349e8e2014-09-18 18:56:04 +000011545Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11546 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011547 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011548
11549 // First, if the target has no DMB, see what fallback we can use.
11550 if (!Subtarget->hasDataBarrier()) {
11551 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11552 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11553 // here.
11554 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11555 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11556 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11557 Builder.getInt32(0), Builder.getInt32(7),
11558 Builder.getInt32(10), Builder.getInt32(5)};
11559 return Builder.CreateCall(MCR, args);
11560 } else {
11561 // Instead of using barriers, atomic accesses on these subtargets use
11562 // libcalls.
11563 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11564 }
11565 } else {
11566 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11567 // Only a full system barrier exists in the M-class architectures.
11568 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11569 Constant *CDomain = Builder.getInt32(Domain);
11570 return Builder.CreateCall(DMB, CDomain);
11571 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011572}
11573
11574// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011575Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011576 AtomicOrdering Ord, bool IsStore,
11577 bool IsLoad) const {
11578 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011579 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011580
11581 switch (Ord) {
11582 case NotAtomic:
11583 case Unordered:
11584 llvm_unreachable("Invalid fence: unordered/non-atomic");
11585 case Monotonic:
11586 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011587 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011588 case SequentiallyConsistent:
11589 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011590 return nullptr; // Nothing to do
11591 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011592 case Release:
11593 case AcquireRelease:
11594 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011595 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011596 // FIXME: add a comment with a link to documentation justifying this.
11597 else
Robin Morissetdedef332014-09-23 20:31:14 +000011598 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011599 }
Robin Morissetdedef332014-09-23 20:31:14 +000011600 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011601}
11602
Robin Morissetdedef332014-09-23 20:31:14 +000011603Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011604 AtomicOrdering Ord, bool IsStore,
11605 bool IsLoad) const {
11606 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011607 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011608
11609 switch (Ord) {
11610 case NotAtomic:
11611 case Unordered:
11612 llvm_unreachable("Invalid fence: unordered/not-atomic");
11613 case Monotonic:
11614 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011615 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011616 case Acquire:
11617 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011618 case SequentiallyConsistent:
11619 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011620 }
Robin Morissetdedef332014-09-23 20:31:14 +000011621 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011622}
11623
Robin Morisseted3d48f2014-09-03 21:29:59 +000011624// Loads and stores less than 64-bits are already atomic; ones above that
11625// are doomed anyway, so defer to the default libcall and blame the OS when
11626// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11627// anything for those.
11628bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11629 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11630 return (Size == 64) && !Subtarget->isMClass();
11631}
Tim Northover037f26f22014-04-17 18:22:47 +000011632
Robin Morisseted3d48f2014-09-03 21:29:59 +000011633// Loads and stores less than 64-bits are already atomic; ones above that
11634// are doomed anyway, so defer to the default libcall and blame the OS when
11635// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11636// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011637// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11638// guarantee, see DDI0406C ARM architecture reference manual,
11639// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000011640TargetLowering::AtomicExpansionKind
11641ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011642 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Ahmed Bougacha52468672015-09-11 17:08:28 +000011643 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLSC
11644 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000011645}
11646
11647// For the real atomic operations, we have ldrex/strex up to 32 bits,
11648// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000011649TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000011650ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011651 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011652 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000011653 ? AtomicExpansionKind::LLSC
11654 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011655}
11656
Ahmed Bougacha52468672015-09-11 17:08:28 +000011657bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
11658 AtomicCmpXchgInst *AI) const {
11659 return true;
11660}
11661
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011662// This has so far only been implemented for MachO.
11663bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011664 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011665}
11666
Quentin Colombetc32615d2014-10-31 17:52:53 +000011667bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11668 unsigned &Cost) const {
11669 // If we do not have NEON, vector types are not natively supported.
11670 if (!Subtarget->hasNEON())
11671 return false;
11672
11673 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000011674 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000011675 // better to leave at float as we have more freedom in the addressing mode for
11676 // those.
11677 if (VectorTy->isFPOrFPVectorTy())
11678 return false;
11679
11680 // If the index is unknown at compile time, this is very expensive to lower
11681 // and it is not possible to combine the store with the extract.
11682 if (!isa<ConstantInt>(Idx))
11683 return false;
11684
11685 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11686 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11687 // We can do a store + vector extract on any vector that fits perfectly in a D
11688 // or Q register.
11689 if (BitWidth == 64 || BitWidth == 128) {
11690 Cost = 0;
11691 return true;
11692 }
11693 return false;
11694}
11695
Tim Northover037f26f22014-04-17 18:22:47 +000011696Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11697 AtomicOrdering Ord) const {
11698 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11699 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011700 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011701
11702 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11703 // intrinsic must return {i32, i32} and we have to recombine them into a
11704 // single i64 here.
11705 if (ValTy->getPrimitiveSizeInBits() == 64) {
11706 Intrinsic::ID Int =
11707 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11708 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11709
11710 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11711 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11712
11713 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11714 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011715 if (!Subtarget->isLittle())
11716 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011717 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11718 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11719 return Builder.CreateOr(
11720 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11721 }
11722
11723 Type *Tys[] = { Addr->getType() };
11724 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11725 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11726
11727 return Builder.CreateTruncOrBitCast(
11728 Builder.CreateCall(Ldrex, Addr),
11729 cast<PointerType>(Addr->getType())->getElementType());
11730}
11731
Ahmed Bougacha81616a72015-09-22 17:22:58 +000011732void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
11733 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000011734 if (!Subtarget->hasV7Ops())
11735 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000011736 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11737 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
11738}
11739
Tim Northover037f26f22014-04-17 18:22:47 +000011740Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11741 Value *Addr,
11742 AtomicOrdering Ord) const {
11743 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011744 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011745
11746 // Since the intrinsics must have legal type, the i64 intrinsics take two
11747 // parameters: "i32, i32". We must marshal Val into the appropriate form
11748 // before the call.
11749 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11750 Intrinsic::ID Int =
11751 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11752 Function *Strex = Intrinsic::getDeclaration(M, Int);
11753 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11754
11755 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11756 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011757 if (!Subtarget->isLittle())
11758 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011759 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011760 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011761 }
11762
11763 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11764 Type *Tys[] = { Addr->getType() };
11765 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11766
David Blaikieff6409d2015-05-18 22:13:54 +000011767 return Builder.CreateCall(
11768 Strex, {Builder.CreateZExtOrBitCast(
11769 Val, Strex->getFunctionType()->getParamType(0)),
11770 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011771}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011772
Hao Liu2cd34bb2015-06-26 02:45:36 +000011773/// \brief Lower an interleaved load into a vldN intrinsic.
11774///
11775/// E.g. Lower an interleaved load (Factor = 2):
11776/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
11777/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
11778/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
11779///
11780/// Into:
11781/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
11782/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
11783/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
11784bool ARMTargetLowering::lowerInterleavedLoad(
11785 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
11786 ArrayRef<unsigned> Indices, unsigned Factor) const {
11787 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11788 "Invalid interleave factor");
11789 assert(!Shuffles.empty() && "Empty shufflevector input");
11790 assert(Shuffles.size() == Indices.size() &&
11791 "Unmatched number of shufflevectors and indices");
11792
11793 VectorType *VecTy = Shuffles[0]->getType();
11794 Type *EltTy = VecTy->getVectorElementType();
11795
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011796 const DataLayout &DL = LI->getModule()->getDataLayout();
11797 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
11798 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011799
Jeroen Ketemaaebca092015-10-07 14:53:29 +000011800 // Skip if we do not have NEON and skip illegal vector types and vector types
11801 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
11802 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000011803 return false;
11804
11805 // A pointer vector can not be the return type of the ldN intrinsics. Need to
11806 // load integer vectors first and then convert to pointer vectors.
11807 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011808 VecTy =
11809 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000011810
11811 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
11812 Intrinsic::arm_neon_vld3,
11813 Intrinsic::arm_neon_vld4};
11814
Hao Liu2cd34bb2015-06-26 02:45:36 +000011815 IRBuilder<> Builder(LI);
11816 SmallVector<Value *, 2> Ops;
11817
11818 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
11819 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
11820 Ops.push_back(Builder.getInt32(LI->getAlignment()));
11821
Jeroen Ketemaab99b592015-09-30 10:56:37 +000011822 Type *Tys[] = { VecTy, Int8Ptr };
11823 Function *VldnFunc =
11824 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011825 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
11826
11827 // Replace uses of each shufflevector with the corresponding vector loaded
11828 // by ldN.
11829 for (unsigned i = 0; i < Shuffles.size(); i++) {
11830 ShuffleVectorInst *SV = Shuffles[i];
11831 unsigned Index = Indices[i];
11832
11833 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
11834
11835 // Convert the integer vector to pointer vector if the element is pointer.
11836 if (EltTy->isPointerTy())
11837 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
11838
11839 SV->replaceAllUsesWith(SubVec);
11840 }
11841
11842 return true;
11843}
11844
11845/// \brief Get a mask consisting of sequential integers starting from \p Start.
11846///
11847/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
11848static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
11849 unsigned NumElts) {
11850 SmallVector<Constant *, 16> Mask;
11851 for (unsigned i = 0; i < NumElts; i++)
11852 Mask.push_back(Builder.getInt32(Start + i));
11853
11854 return ConstantVector::get(Mask);
11855}
11856
11857/// \brief Lower an interleaved store into a vstN intrinsic.
11858///
11859/// E.g. Lower an interleaved store (Factor = 3):
11860/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
11861/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
11862/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
11863///
11864/// Into:
11865/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
11866/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
11867/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
11868/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
11869///
11870/// Note that the new shufflevectors will be removed and we'll only generate one
11871/// vst3 instruction in CodeGen.
11872bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
11873 ShuffleVectorInst *SVI,
11874 unsigned Factor) const {
11875 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11876 "Invalid interleave factor");
11877
11878 VectorType *VecTy = SVI->getType();
11879 assert(VecTy->getVectorNumElements() % Factor == 0 &&
11880 "Invalid interleaved store");
11881
11882 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
11883 Type *EltTy = VecTy->getVectorElementType();
11884 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
11885
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011886 const DataLayout &DL = SI->getModule()->getDataLayout();
11887 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
11888 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011889
Jeroen Ketemaaebca092015-10-07 14:53:29 +000011890 // Skip if we do not have NEON and skip illegal vector types and vector types
11891 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
11892 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
11893 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000011894 return false;
11895
11896 Value *Op0 = SVI->getOperand(0);
11897 Value *Op1 = SVI->getOperand(1);
11898 IRBuilder<> Builder(SI);
11899
11900 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
11901 // vectors to integer vectors.
11902 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011903 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011904
11905 // Convert to the corresponding integer vector.
11906 Type *IntVecTy =
11907 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
11908 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
11909 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
11910
11911 SubVecTy = VectorType::get(IntTy, NumSubElts);
11912 }
11913
Craig Topper26260942015-10-18 05:15:34 +000011914 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
11915 Intrinsic::arm_neon_vst3,
11916 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000011917 SmallVector<Value *, 6> Ops;
11918
11919 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
11920 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
11921
Jeroen Ketemaab99b592015-09-30 10:56:37 +000011922 Type *Tys[] = { Int8Ptr, SubVecTy };
11923 Function *VstNFunc = Intrinsic::getDeclaration(
11924 SI->getModule(), StoreInts[Factor - 2], Tys);
11925
Hao Liu2cd34bb2015-06-26 02:45:36 +000011926 // Split the shufflevector operands into sub vectors for the new vstN call.
11927 for (unsigned i = 0; i < Factor; i++)
11928 Ops.push_back(Builder.CreateShuffleVector(
11929 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
11930
11931 Ops.push_back(Builder.getInt32(SI->getAlignment()));
11932 Builder.CreateCall(VstNFunc, Ops);
11933 return true;
11934}
11935
Oliver Stannardc24f2172014-05-09 14:01:47 +000011936enum HABaseType {
11937 HA_UNKNOWN = 0,
11938 HA_FLOAT,
11939 HA_DOUBLE,
11940 HA_VECT64,
11941 HA_VECT128
11942};
11943
11944static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11945 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000011946 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011947 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11948 uint64_t SubMembers = 0;
11949 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11950 return false;
11951 Members += SubMembers;
11952 }
Craig Toppere3dcce92015-08-01 22:20:21 +000011953 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011954 uint64_t SubMembers = 0;
11955 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11956 return false;
11957 Members += SubMembers * AT->getNumElements();
11958 } else if (Ty->isFloatTy()) {
11959 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11960 return false;
11961 Members = 1;
11962 Base = HA_FLOAT;
11963 } else if (Ty->isDoubleTy()) {
11964 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11965 return false;
11966 Members = 1;
11967 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000011968 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011969 Members = 1;
11970 switch (Base) {
11971 case HA_FLOAT:
11972 case HA_DOUBLE:
11973 return false;
11974 case HA_VECT64:
11975 return VT->getBitWidth() == 64;
11976 case HA_VECT128:
11977 return VT->getBitWidth() == 128;
11978 case HA_UNKNOWN:
11979 switch (VT->getBitWidth()) {
11980 case 64:
11981 Base = HA_VECT64;
11982 return true;
11983 case 128:
11984 Base = HA_VECT128;
11985 return true;
11986 default:
11987 return false;
11988 }
11989 }
11990 }
11991
11992 return (Members > 0 && Members <= 4);
11993}
11994
Tim Northovere95c5b32015-02-24 17:22:34 +000011995/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11996/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11997/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011998bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11999 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012000 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12001 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012002 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012003
12004 HABaseType Base = HA_UNKNOWN;
12005 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012006 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12007 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12008
12009 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12010 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012011}