blob: 6cac892d7c722baaa1a63da9321e05603d54eea5 [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 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000158 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Eric Christopher1889fdc2015-01-29 00:19:39 +0000162ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
163 const ARMSubtarget &STI)
164 : TargetLowering(TM), Subtarget(&STI) {
165 RegInfo = Subtarget->getRegisterInfo();
166 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000167
Duncan Sandsf2641e12011-09-06 19:07:46 +0000168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
169
Tim Northoverd6a729b2014-01-06 14:28:05 +0000170 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000172 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000174 static const struct {
175 const RTLIB::Libcall Op;
176 const char * const Name;
177 const ISD::CondCode Cond;
178 } LibraryCalls[] = {
179 // Single-precision floating-point arithmetic.
180 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
181 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
182 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
183 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000184
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000185 // Double-precision floating-point arithmetic.
186 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
187 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
189 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000190
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000191 // Single-precision comparisons.
192 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
193 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
194 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
195 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
196 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
197 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
198 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
199 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000200
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000201 // Double-precision comparisons.
202 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
203 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
204 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
205 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
206 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
207 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
208 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
209 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000210
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000211 // Floating-point to integer conversions.
212 // i64 conversions are done via library routines even when generating VFP
213 // instructions, so use the same ones.
214 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
215 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
216 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
217 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000219 // Conversions between floating types.
220 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
221 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000222
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000223 // Integer to floating-point conversions.
224 // i64 conversions are done via library routines even when generating VFP
225 // instructions, so use the same ones.
226 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
227 // e.g., __floatunsidf vs. __floatunssidfvfp.
228 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
229 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
230 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
231 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
232 };
Evan Cheng10043e22007-01-19 07:51:42 +0000233
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000234 for (const auto &LC : LibraryCalls) {
235 setLibcallName(LC.Op, LC.Name);
236 if (LC.Cond != ISD::SETCC_INVALID)
237 setCmpLibcallCC(LC.Op, LC.Cond);
238 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000239 }
Evan Cheng10043e22007-01-19 07:51:42 +0000240 }
241
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000242 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000243 setLibcallName(RTLIB::SHL_I128, nullptr);
244 setLibcallName(RTLIB::SRL_I128, nullptr);
245 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000246
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000247 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
248 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000249 static const struct {
250 const RTLIB::Libcall Op;
251 const char * const Name;
252 const CallingConv::ID CC;
253 const ISD::CondCode Cond;
254 } LibraryCalls[] = {
255 // Double-precision floating-point arithmetic helper functions
256 // RTABI chapter 4.1.2, Table 2
257 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
258 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
259 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
260 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000261
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000262 // Double-precision floating-point comparison helper functions
263 // RTABI chapter 4.1.2, Table 3
264 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
265 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
266 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
267 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
268 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
269 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
270 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000272
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000273 // Single-precision floating-point arithmetic helper functions
274 // RTABI chapter 4.1.2, Table 4
275 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
276 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
277 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
278 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000279
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000280 // Single-precision floating-point comparison helper functions
281 // RTABI chapter 4.1.2, Table 5
282 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
283 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
284 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
285 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
286 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
287 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
288 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000290
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000291 // Floating-point to integer conversions.
292 // RTABI chapter 4.1.2, Table 6
293 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
294 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
295 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
296 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
297 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
298 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000301
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000302 // Conversions between floating types.
303 // RTABI chapter 4.1.2, Table 7
304 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000305 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000306 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000307
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000308 // Integer to floating-point conversions.
309 // RTABI chapter 4.1.2, Table 8
310 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
311 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
312 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
313 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
314 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000318
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000319 // Long long helper functions
320 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000321 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000325
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000326 // Integer division functions
327 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000328 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
329 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
330 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000336
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000337 // Memory operations
338 // RTABI chapter 4.3.4
339 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 };
343
344 for (const auto &LC : LibraryCalls) {
345 setLibcallName(LC.Op, LC.Name);
346 setLibcallCallingConv(LC.Op, LC.CC);
347 if (LC.Cond != ISD::SETCC_INVALID)
348 setCmpLibcallCC(LC.Op, LC.Cond);
349 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000350 }
351
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000352 if (Subtarget->isTargetWindows()) {
353 static const struct {
354 const RTLIB::Libcall Op;
355 const char * const Name;
356 const CallingConv::ID CC;
357 } LibraryCalls[] = {
358 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
359 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
360 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
361 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
362 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
363 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool0a2672b2015-08-04 03:57:56 +0000366
367 { RTLIB::SDIV_I32, "__rt_sdiv", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::UDIV_I32, "__rt_udiv", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::SDIV_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::UDIV_I64, "__rt_udiv64", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000371 };
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
David Goodwin22c2fba2009-07-08 23:10:31 +0000399 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000400 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000401 else
Craig Topperc7242e02012-04-20 07:30:17 +0000402 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000403 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000404 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000405 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000406 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000407 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000408
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000409 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000410 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000411 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000412 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
413 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
414 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
415 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000416
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000417 setOperationAction(ISD::MULHS, VT, Expand);
418 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
419 setOperationAction(ISD::MULHU, VT, Expand);
420 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000421
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000422 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000423 }
424
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000425 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000426 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000427
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000428 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
429 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
430
Bob Wilson2e076c42009-06-22 23:27:02 +0000431 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000432 addDRTypeForNEON(MVT::v2f32);
433 addDRTypeForNEON(MVT::v8i8);
434 addDRTypeForNEON(MVT::v4i16);
435 addDRTypeForNEON(MVT::v2i32);
436 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000437
Owen Anderson9f944592009-08-11 20:47:22 +0000438 addQRTypeForNEON(MVT::v4f32);
439 addQRTypeForNEON(MVT::v2f64);
440 addQRTypeForNEON(MVT::v16i8);
441 addQRTypeForNEON(MVT::v8i16);
442 addQRTypeForNEON(MVT::v4i32);
443 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000444
Bob Wilson194a2512009-09-15 23:55:57 +0000445 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
446 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000447 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
448 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000449 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
450 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
451 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000452 // FIXME: Code duplication: FDIV and FREM are expanded always, see
453 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000454 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
455 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000456 // FIXME: Create unittest.
457 // In another words, find a way when "copysign" appears in DAG with vector
458 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000459 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000460 // FIXME: Code duplication: SETCC has custom operation action, see
461 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000462 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000463 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000464 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
466 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
467 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
468 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
469 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
470 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
471 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
472 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
473 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
474 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
475 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000476 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000477 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
478 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
479 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
480 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
481 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000482 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000483
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000484 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
485 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
486 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
487 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
488 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
489 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
490 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
491 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
492 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
493 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000494 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
495 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
496 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
497 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000498 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000499
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000500 // Mark v2f32 intrinsics.
501 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
502 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
503 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
504 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
505 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
506 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
507 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
508 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
509 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
510 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
511 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
512 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
513 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
514 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
515 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
516
Bob Wilson6cc46572009-09-16 00:32:15 +0000517 // Neon does not support some operations on v1i64 and v2i64 types.
518 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000519 // Custom handling for some quad-vector types to detect VMULL.
520 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
521 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
522 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000523 // Custom handling for some vector types to avoid expensive expansions
524 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
525 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
526 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
527 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000528 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
529 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000530 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000531 // a destination type that is wider than the source, and nor does
532 // it have a FP_TO_[SU]INT instruction with a narrower destination than
533 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000534 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
535 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000536 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
537 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000538
Eli Friedmane6385e62012-11-15 22:44:27 +0000539 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000540 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000541
Evan Chengb4eae132012-12-04 22:41:50 +0000542 // NEON does not have single instruction CTPOP for vectors with element
543 // types wider than 8-bits. However, custom lowering can leverage the
544 // v8i8/v16i8 vcnt instruction.
545 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
546 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
547 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
548 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
549
Logan Chien0a43abc2015-07-13 15:37:30 +0000550 // NEON does not have single instruction CTTZ for vectors.
551 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
552 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
553 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
554 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
555
556 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
557 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
558 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
559 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
560
561 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
562 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
563 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
564 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
565
566 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
567 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
568 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
569 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
570
Jim Grosbach5f215872013-02-27 21:31:12 +0000571 // NEON only has FMA instructions as of VFP4.
572 if (!Subtarget->hasVFP4()) {
573 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
574 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
575 }
576
Bob Wilson06fce872011-02-07 17:43:21 +0000577 setTargetDAGCombine(ISD::INTRINSIC_VOID);
578 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000579 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
580 setTargetDAGCombine(ISD::SHL);
581 setTargetDAGCombine(ISD::SRL);
582 setTargetDAGCombine(ISD::SRA);
583 setTargetDAGCombine(ISD::SIGN_EXTEND);
584 setTargetDAGCombine(ISD::ZERO_EXTEND);
585 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000586 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000587 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000588 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000589 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
590 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000591 setTargetDAGCombine(ISD::FP_TO_SINT);
592 setTargetDAGCombine(ISD::FP_TO_UINT);
593 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000594 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000595
James Molloy547d4c02012-02-20 09:24:05 +0000596 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000597 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
598 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000599 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000600 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
601 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
602 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000603 }
James Molloy547d4c02012-02-20 09:24:05 +0000604 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000605 }
606
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000607 // ARM and Thumb2 support UMLAL/SMLAL.
608 if (!Subtarget->isThumb1Only())
609 setTargetDAGCombine(ISD::ADDC);
610
Oliver Stannard51b1d462014-08-21 12:50:31 +0000611 if (Subtarget->isFPOnlySP()) {
612 // When targetting a floating-point unit with only single-precision
613 // operations, f64 is legal for the few double-precision instructions which
614 // are present However, no double-precision operations other than moves,
615 // loads and stores are provided by the hardware.
616 setOperationAction(ISD::FADD, MVT::f64, Expand);
617 setOperationAction(ISD::FSUB, MVT::f64, Expand);
618 setOperationAction(ISD::FMUL, MVT::f64, Expand);
619 setOperationAction(ISD::FMA, MVT::f64, Expand);
620 setOperationAction(ISD::FDIV, MVT::f64, Expand);
621 setOperationAction(ISD::FREM, MVT::f64, Expand);
622 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
623 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
624 setOperationAction(ISD::FNEG, MVT::f64, Expand);
625 setOperationAction(ISD::FABS, MVT::f64, Expand);
626 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
627 setOperationAction(ISD::FSIN, MVT::f64, Expand);
628 setOperationAction(ISD::FCOS, MVT::f64, Expand);
629 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
630 setOperationAction(ISD::FPOW, MVT::f64, Expand);
631 setOperationAction(ISD::FLOG, MVT::f64, Expand);
632 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
633 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
634 setOperationAction(ISD::FEXP, MVT::f64, Expand);
635 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
636 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
637 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
638 setOperationAction(ISD::FRINT, MVT::f64, Expand);
639 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
640 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000641 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
642 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
643 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
644 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
645 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
646 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000647 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
648 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
649 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000650
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000651 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000652
Tim Northover4e80b582014-07-18 13:01:19 +0000653 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000654 for (MVT VT : MVT::fp_valuetypes()) {
655 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
656 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
657 }
Tim Northover4e80b582014-07-18 13:01:19 +0000658
659 // ... or truncating stores
660 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
661 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
662 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000663
Duncan Sands95d46ef2008-01-23 20:39:46 +0000664 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000665 for (MVT VT : MVT::integer_valuetypes())
666 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000667
Evan Cheng10043e22007-01-19 07:51:42 +0000668 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000669 if (!Subtarget->isThumb1Only()) {
670 for (unsigned im = (unsigned)ISD::PRE_INC;
671 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000672 setIndexedLoadAction(im, MVT::i1, Legal);
673 setIndexedLoadAction(im, MVT::i8, Legal);
674 setIndexedLoadAction(im, MVT::i16, Legal);
675 setIndexedLoadAction(im, MVT::i32, Legal);
676 setIndexedStoreAction(im, MVT::i1, Legal);
677 setIndexedStoreAction(im, MVT::i8, Legal);
678 setIndexedStoreAction(im, MVT::i16, Legal);
679 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000680 }
Evan Cheng10043e22007-01-19 07:51:42 +0000681 }
682
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000683 setOperationAction(ISD::SADDO, MVT::i32, Custom);
684 setOperationAction(ISD::UADDO, MVT::i32, Custom);
685 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
686 setOperationAction(ISD::USUBO, MVT::i32, Custom);
687
Evan Cheng10043e22007-01-19 07:51:42 +0000688 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000689 setOperationAction(ISD::MUL, MVT::i64, Expand);
690 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000691 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000692 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
693 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000694 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000695 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
696 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000697 setOperationAction(ISD::MULHS, MVT::i32, Expand);
698
Jim Grosbach5d994042009-10-31 19:38:01 +0000699 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000700 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000701 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000702 setOperationAction(ISD::SRL, MVT::i64, Custom);
703 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000704
Evan Chenge8916542011-08-30 01:34:54 +0000705 if (!Subtarget->isThumb1Only()) {
706 // FIXME: We should do this for Thumb1 as well.
707 setOperationAction(ISD::ADDC, MVT::i32, Custom);
708 setOperationAction(ISD::ADDE, MVT::i32, Custom);
709 setOperationAction(ISD::SUBC, MVT::i32, Custom);
710 setOperationAction(ISD::SUBE, MVT::i32, Custom);
711 }
712
Evan Cheng10043e22007-01-19 07:51:42 +0000713 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000714 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000715 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000716 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000717 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000718 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000719
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000720 // These just redirect to CTTZ and CTLZ on ARM.
721 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
722 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
723
Tim Northoverbc933082013-05-23 19:11:20 +0000724 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
725
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000726 // Only ARMv6 has BSWAP.
727 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000728 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000729
Bob Wilsone8a549c2012-09-29 21:43:49 +0000730 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
731 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
732 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000733 setOperationAction(ISD::SDIV, MVT::i32, Expand);
734 setOperationAction(ISD::UDIV, MVT::i32, Expand);
735 }
Renato Golin87610692013-07-16 09:32:17 +0000736
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000737 // FIXME: Also set divmod for SREM on EABI/androideabi
Chad Rosierad7c9102014-08-23 18:29:43 +0000738 setOperationAction(ISD::SREM, MVT::i32, Expand);
739 setOperationAction(ISD::UREM, MVT::i32, Expand);
740 // Register based DivRem for AEABI (RTABI 4.2)
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000741 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) {
Chad Rosierad7c9102014-08-23 18:29:43 +0000742 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
743 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
744 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
745 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
746 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
747 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
748 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
749 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
750
751 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
752 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
753 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
754 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
755 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
756 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
757 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
758 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
759
760 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
761 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
762 } else {
Renato Golin87610692013-07-16 09:32:17 +0000763 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
764 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
765 }
Bob Wilson7117a912009-03-20 22:42:55 +0000766
Owen Anderson9f944592009-08-11 20:47:22 +0000767 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
768 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
769 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
770 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000771 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000772
Evan Cheng74d92c12011-04-08 21:37:21 +0000773 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000774
Evan Cheng10043e22007-01-19 07:51:42 +0000775 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000776 setOperationAction(ISD::VASTART, MVT::Other, Custom);
777 setOperationAction(ISD::VAARG, MVT::Other, Expand);
778 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
779 setOperationAction(ISD::VAEND, MVT::Other, Expand);
780 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
781 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000782
Tim Northoverd6a729b2014-01-06 14:28:05 +0000783 if (!Subtarget->isTargetMachO()) {
784 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000785 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000786 setExceptionPointerRegister(ARM::R0);
787 setExceptionSelectorRegister(ARM::R1);
788 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000789
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000790 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
791 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
792 else
793 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
794
Evan Cheng6e809de2010-08-11 06:22:01 +0000795 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000796 // the default expansion. If we are targeting a single threaded system,
797 // then set them all for expand so we can lower them later into their
798 // non-atomic form.
799 if (TM.Options.ThreadModel == ThreadModel::Single)
800 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
801 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000802 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
803 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000804 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000805
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000806 // On v8, we have particularly efficient implementations of atomic fences
807 // if they can be combined with nearby atomic loads and stores.
808 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000809 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000810 setInsertFencesForAtomic(true);
811 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000812 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000813 // If there's anything we can use as a barrier, go through custom lowering
814 // for ATOMIC_FENCE.
815 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
816 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
817
Jim Grosbach6860bb72010-06-18 22:35:32 +0000818 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000819 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000820 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000821 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000822 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000823 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000824 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000825 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000826 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000827 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000828 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000829 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000830 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000831 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
832 // Unordered/Monotonic case.
833 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
834 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000835 }
Evan Cheng10043e22007-01-19 07:51:42 +0000836
Evan Cheng21acf9f2010-11-04 05:19:35 +0000837 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000838
Eli Friedman8cfa7712010-06-26 04:36:50 +0000839 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
840 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000841 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
842 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000843 }
Owen Anderson9f944592009-08-11 20:47:22 +0000844 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000845
Eric Christopher824f42f2015-05-12 01:26:05 +0000846 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000847 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000848 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000849 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000850 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000851 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
852 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000853
854 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000855 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000856 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
857 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
858 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
859 if (Subtarget->isTargetDarwin())
John McCall7d84ece2011-05-29 19:50:32 +0000860 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000861
Owen Anderson9f944592009-08-11 20:47:22 +0000862 setOperationAction(ISD::SETCC, MVT::i32, Expand);
863 setOperationAction(ISD::SETCC, MVT::f32, Expand);
864 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000865 setOperationAction(ISD::SELECT, MVT::i32, Custom);
866 setOperationAction(ISD::SELECT, MVT::f32, Custom);
867 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000868 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
869 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
870 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000871
Owen Anderson9f944592009-08-11 20:47:22 +0000872 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
873 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
874 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
875 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
876 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000877
Dan Gohman482732a2007-10-11 23:21:31 +0000878 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000879 setOperationAction(ISD::FSIN, MVT::f64, Expand);
880 setOperationAction(ISD::FSIN, MVT::f32, Expand);
881 setOperationAction(ISD::FCOS, MVT::f32, Expand);
882 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000883 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
884 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000885 setOperationAction(ISD::FREM, MVT::f64, Expand);
886 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000887 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000888 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000889 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
890 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000891 }
Owen Anderson9f944592009-08-11 20:47:22 +0000892 setOperationAction(ISD::FPOW, MVT::f64, Expand);
893 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000894
Evan Chengd0007f32012-04-10 21:40:28 +0000895 if (!Subtarget->hasVFP4()) {
896 setOperationAction(ISD::FMA, MVT::f64, Expand);
897 setOperationAction(ISD::FMA, MVT::f32, Expand);
898 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000899
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000900 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000901 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000902 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
903 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000904 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
905 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
906 }
907
908 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000909 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000910 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
911 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000912 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000913 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000914
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000915 // Combine sin / cos into one node or libcall if possible.
916 if (Subtarget->hasSinCos()) {
917 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
918 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000919 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000920 // For iOS, we don't want to the normal expansion of a libcall to
921 // sincos. We want to issue a libcall to __sincos_stret.
922 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
923 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
924 }
925 }
Evan Cheng10043e22007-01-19 07:51:42 +0000926
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000927 // FP-ARMv8 implements a lot of rounding-like FP operations.
928 if (Subtarget->hasFPARMv8()) {
929 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
930 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
931 setOperationAction(ISD::FROUND, MVT::f32, Legal);
932 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
933 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
934 setOperationAction(ISD::FRINT, MVT::f32, Legal);
935 if (!Subtarget->isFPOnlySP()) {
936 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
937 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
938 setOperationAction(ISD::FROUND, MVT::f64, Legal);
939 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
940 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
941 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000942 }
943 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000944 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000945 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000946 setTargetDAGCombine(ISD::ADD);
947 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000948 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000949 setTargetDAGCombine(ISD::AND);
950 setTargetDAGCombine(ISD::OR);
951 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000952
Evan Chengf258a152012-02-23 02:58:19 +0000953 if (Subtarget->hasV6Ops())
954 setTargetDAGCombine(ISD::SRL);
955
Evan Cheng10043e22007-01-19 07:51:42 +0000956 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000957
Eric Christopher824f42f2015-05-12 01:26:05 +0000958 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000959 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000960 setSchedulingPreference(Sched::RegPressure);
961 else
962 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000963
Evan Cheng3ae2b792011-01-06 06:52:41 +0000964 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000965 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +0000966 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000967 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +0000968 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000969 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +0000970 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000971
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000972 // On ARM arguments smaller than 4 bytes are extended, so all arguments
973 // are at least 4 bytes aligned.
974 setMinStackArgumentAlignment(4);
975
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000976 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000977 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000978
Eli Friedman2518f832011-05-06 20:34:06 +0000979 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000980}
981
Eric Christopher824f42f2015-05-12 01:26:05 +0000982bool ARMTargetLowering::useSoftFloat() const {
983 return Subtarget->useSoftFloat();
984}
985
Andrew Trick43f25632011-01-19 02:35:27 +0000986// FIXME: It might make sense to define the representative register class as the
987// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
988// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
989// SPR's representative would be DPR_VFP2. This should work well if register
990// pressure tracking were modified such that a register use would increment the
991// pressure of the register class's representative and all of it's super
992// classes' representatives transitively. We have not implemented this because
993// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000994// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000995// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000996std::pair<const TargetRegisterClass *, uint8_t>
997ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
998 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000999 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001000 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001001 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001002 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001003 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001004 // Use DPR as representative register class for all floating point
1005 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1006 // the cost is 1 for both f32 and f64.
1007 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001008 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001009 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001010 // When NEON is used for SP, only half of the register file is available
1011 // because operations that define both SP and DP results will be constrained
1012 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1013 // coalescing by double-counting the SP regs. See the FIXME above.
1014 if (Subtarget->useNEONForSinglePrecisionFP())
1015 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001016 break;
1017 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1018 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001019 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001020 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001021 break;
1022 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001023 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001024 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001025 break;
1026 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001027 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001028 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001029 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001030 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001031 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001032}
1033
Evan Cheng10043e22007-01-19 07:51:42 +00001034const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001035 switch ((ARMISD::NodeType)Opcode) {
1036 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001037 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001038 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001039 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001040 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001041 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001042 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001043 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1044 case ARMISD::tCALL: return "ARMISD::tCALL";
1045 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1046 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001047 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001048 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001049 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001050 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1051 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001052 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001053 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001054 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1055 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001056 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001057 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001058
Evan Cheng10043e22007-01-19 07:51:42 +00001059 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001060
Jim Grosbach8546ec92010-01-18 19:58:49 +00001061 case ARMISD::RBIT: return "ARMISD::RBIT";
1062
Evan Cheng10043e22007-01-19 07:51:42 +00001063 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1064 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1065 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001066
Evan Chenge8916542011-08-30 01:34:54 +00001067 case ARMISD::ADDC: return "ARMISD::ADDC";
1068 case ARMISD::ADDE: return "ARMISD::ADDE";
1069 case ARMISD::SUBC: return "ARMISD::SUBC";
1070 case ARMISD::SUBE: return "ARMISD::SUBE";
1071
Bob Wilson22806742010-09-22 22:09:21 +00001072 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1073 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001074
Evan Chengec6d7c92009-10-28 06:55:03 +00001075 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001076 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1077 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001078
Dale Johannesend679ff72010-06-03 21:09:53 +00001079 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001080
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001081 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001082
Evan Chengb972e562009-08-07 00:34:42 +00001083 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1084
Bob Wilson7ed59712010-10-30 00:54:37 +00001085 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001086
Evan Cheng8740ee32010-11-03 06:34:55 +00001087 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1088
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001089 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1090
Bob Wilson2e076c42009-06-22 23:27:02 +00001091 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001092 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001093 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001094 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1095 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001096 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1097 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001098 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1099 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001100 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1101 case ARMISD::VTST: return "ARMISD::VTST";
1102
1103 case ARMISD::VSHL: return "ARMISD::VSHL";
1104 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1105 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001106 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1107 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1108 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1109 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1110 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1111 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1112 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1113 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1114 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1115 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1116 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1117 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001118 case ARMISD::VSLI: return "ARMISD::VSLI";
1119 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001120 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1121 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001122 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001123 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001124 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001125 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001126 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001127 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001128 case ARMISD::VREV64: return "ARMISD::VREV64";
1129 case ARMISD::VREV32: return "ARMISD::VREV32";
1130 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001131 case ARMISD::VZIP: return "ARMISD::VZIP";
1132 case ARMISD::VUZP: return "ARMISD::VUZP";
1133 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001134 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1135 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001136 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1137 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001138 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1139 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001140 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001141 case ARMISD::FMAX: return "ARMISD::FMAX";
1142 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001143 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1144 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001145 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001146 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1147 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001148 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001149 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1150 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1151 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001152 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1153 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1154 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1155 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1156 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1157 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1158 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1159 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1160 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1161 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1162 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1163 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1164 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1165 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1166 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1167 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1168 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001169 }
Matthias Braund04893f2015-05-07 21:33:59 +00001170 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001171}
1172
Mehdi Amini44ede332015-07-09 02:09:04 +00001173EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1174 EVT VT) const {
1175 if (!VT.isVector())
1176 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001177 return VT.changeVectorElementTypeToInteger();
1178}
1179
Evan Cheng4cad68e2010-05-15 02:18:07 +00001180/// getRegClassFor - Return the register class that should be used for the
1181/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001182const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001183 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1184 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1185 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001186 if (Subtarget->hasNEON()) {
1187 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001188 return &ARM::QQPRRegClass;
1189 if (VT == MVT::v8i64)
1190 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001191 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001192 return TargetLowering::getRegClassFor(VT);
1193}
1194
John Brawn0dbcd652015-03-18 12:01:59 +00001195// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1196// source/dest is aligned and the copy size is large enough. We therefore want
1197// to align such objects passed to memory intrinsics.
1198bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1199 unsigned &PrefAlign) const {
1200 if (!isa<MemIntrinsic>(CI))
1201 return false;
1202 MinSize = 8;
1203 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1204 // cycle faster than 4-byte aligned LDM.
1205 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1206 return true;
1207}
1208
Eric Christopher84bdfd82010-07-21 22:26:11 +00001209// Create a fast isel object.
1210FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001211ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1212 const TargetLibraryInfo *libInfo) const {
1213 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001214}
1215
Evan Cheng4401f882010-05-20 23:26:43 +00001216Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001217 unsigned NumVals = N->getNumValues();
1218 if (!NumVals)
1219 return Sched::RegPressure;
1220
1221 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001222 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001223 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001224 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001225 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001226 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001227 }
Evan Chengbf914992010-05-28 23:25:23 +00001228
1229 if (!N->isMachineOpcode())
1230 return Sched::RegPressure;
1231
1232 // Load are scheduled for latency even if there instruction itinerary
1233 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001234 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001235 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001236
Evan Cheng6cc775f2011-06-28 19:10:37 +00001237 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001238 return Sched::RegPressure;
1239 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001240 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001241 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001242
Evan Cheng4401f882010-05-20 23:26:43 +00001243 return Sched::RegPressure;
1244}
1245
Evan Cheng10043e22007-01-19 07:51:42 +00001246//===----------------------------------------------------------------------===//
1247// Lowering Code
1248//===----------------------------------------------------------------------===//
1249
Evan Cheng10043e22007-01-19 07:51:42 +00001250/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1251static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1252 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001253 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001254 case ISD::SETNE: return ARMCC::NE;
1255 case ISD::SETEQ: return ARMCC::EQ;
1256 case ISD::SETGT: return ARMCC::GT;
1257 case ISD::SETGE: return ARMCC::GE;
1258 case ISD::SETLT: return ARMCC::LT;
1259 case ISD::SETLE: return ARMCC::LE;
1260 case ISD::SETUGT: return ARMCC::HI;
1261 case ISD::SETUGE: return ARMCC::HS;
1262 case ISD::SETULT: return ARMCC::LO;
1263 case ISD::SETULE: return ARMCC::LS;
1264 }
1265}
1266
Bob Wilsona2e83332009-09-09 23:14:54 +00001267/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1268static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001269 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001270 CondCode2 = ARMCC::AL;
1271 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001272 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001273 case ISD::SETEQ:
1274 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1275 case ISD::SETGT:
1276 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1277 case ISD::SETGE:
1278 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1279 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001280 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001281 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1282 case ISD::SETO: CondCode = ARMCC::VC; break;
1283 case ISD::SETUO: CondCode = ARMCC::VS; break;
1284 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1285 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1286 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1287 case ISD::SETLT:
1288 case ISD::SETULT: CondCode = ARMCC::LT; break;
1289 case ISD::SETLE:
1290 case ISD::SETULE: CondCode = ARMCC::LE; break;
1291 case ISD::SETNE:
1292 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1293 }
Evan Cheng10043e22007-01-19 07:51:42 +00001294}
1295
Bob Wilsona4c22902009-04-17 19:07:39 +00001296//===----------------------------------------------------------------------===//
1297// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001298//===----------------------------------------------------------------------===//
1299
1300#include "ARMGenCallingConv.inc"
1301
Oliver Stannardc24f2172014-05-09 14:01:47 +00001302/// getEffectiveCallingConv - Get the effective calling convention, taking into
1303/// account presence of floating point hardware and calling convention
1304/// limitations, such as support for variadic functions.
1305CallingConv::ID
1306ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1307 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001308 switch (CC) {
1309 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001310 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001311 case CallingConv::ARM_AAPCS:
1312 case CallingConv::ARM_APCS:
1313 case CallingConv::GHC:
1314 return CC;
1315 case CallingConv::ARM_AAPCS_VFP:
1316 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1317 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001318 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001319 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001320 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001321 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1322 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001323 return CallingConv::ARM_AAPCS_VFP;
1324 else
1325 return CallingConv::ARM_AAPCS;
1326 case CallingConv::Fast:
1327 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001328 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001329 return CallingConv::Fast;
1330 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001331 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001332 return CallingConv::ARM_AAPCS_VFP;
1333 else
1334 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001335 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001336}
1337
1338/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1339/// CallingConvention.
1340CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1341 bool Return,
1342 bool isVarArg) const {
1343 switch (getEffectiveCallingConv(CC, isVarArg)) {
1344 default:
1345 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001346 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001347 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001348 case CallingConv::ARM_AAPCS:
1349 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1350 case CallingConv::ARM_AAPCS_VFP:
1351 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1352 case CallingConv::Fast:
1353 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001354 case CallingConv::GHC:
1355 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001356 }
1357}
1358
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001359/// LowerCallResult - Lower the result values of a call into the
1360/// appropriate copies out of appropriate physical registers.
1361SDValue
1362ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001363 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001364 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001365 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001366 SmallVectorImpl<SDValue> &InVals,
1367 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001368
Bob Wilsona4c22902009-04-17 19:07:39 +00001369 // Assign locations to each value returned by this call.
1370 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001371 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1372 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001373 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001374 CCAssignFnForNode(CallConv, /* Return*/ true,
1375 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001376
1377 // Copy all of the result registers out of their specified physreg.
1378 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1379 CCValAssign VA = RVLocs[i];
1380
Stephen Linb8bd2322013-04-20 05:14:40 +00001381 // Pass 'this' value directly from the argument to return value, to avoid
1382 // reg unit interference
1383 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001384 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1385 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001386 InVals.push_back(ThisVal);
1387 continue;
1388 }
1389
Bob Wilson0041bd32009-04-25 00:33:20 +00001390 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001391 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001392 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001393 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001394 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001395 Chain = Lo.getValue(1);
1396 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001397 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001398 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001399 InFlag);
1400 Chain = Hi.getValue(1);
1401 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001402 if (!Subtarget->isLittle())
1403 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001404 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001405
Owen Anderson9f944592009-08-11 20:47:22 +00001406 if (VA.getLocVT() == MVT::v2f64) {
1407 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1408 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001409 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001410
1411 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001412 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001413 Chain = Lo.getValue(1);
1414 InFlag = Lo.getValue(2);
1415 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001416 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001417 Chain = Hi.getValue(1);
1418 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001419 if (!Subtarget->isLittle())
1420 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001421 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001422 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001423 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001424 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001425 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001426 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1427 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001428 Chain = Val.getValue(1);
1429 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001430 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001431
1432 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001433 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001434 case CCValAssign::Full: break;
1435 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001436 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001437 break;
1438 }
1439
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001440 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001441 }
1442
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001443 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001444}
1445
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001446/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001447SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001448ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1449 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001450 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001451 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001452 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001453 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001454 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001455 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1456 StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001457 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001458 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001459 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001460}
1461
Andrew Trickef9de2a2013-05-25 02:42:55 +00001462void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001463 SDValue Chain, SDValue &Arg,
1464 RegsToPassVector &RegsToPass,
1465 CCValAssign &VA, CCValAssign &NextVA,
1466 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001467 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001468 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001469
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001470 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001471 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001472 unsigned id = Subtarget->isLittle() ? 0 : 1;
1473 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001474
1475 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001476 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001477 else {
1478 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001479 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001480 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1481 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001482
Christian Pirkerb5728192014-05-08 14:06:24 +00001483 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001484 dl, DAG, NextVA,
1485 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001486 }
1487}
1488
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001489/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001490/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1491/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001492SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001493ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001494 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001495 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001496 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001497 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1498 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1499 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001500 SDValue Chain = CLI.Chain;
1501 SDValue Callee = CLI.Callee;
1502 bool &isTailCall = CLI.IsTailCall;
1503 CallingConv::ID CallConv = CLI.CallConv;
1504 bool doesNotRet = CLI.DoesNotReturn;
1505 bool isVarArg = CLI.IsVarArg;
1506
Dale Johannesend679ff72010-06-03 21:09:53 +00001507 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001508 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1509 bool isThisReturn = false;
1510 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001511 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001512
Bob Wilson8decdc42011-10-07 17:17:49 +00001513 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001514 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001515 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001516
Dale Johannesend679ff72010-06-03 21:09:53 +00001517 if (isTailCall) {
1518 // Check if it's really possible to do a tail call.
1519 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001520 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001521 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001522 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1523 report_fatal_error("failed to perform tail call elimination on a call "
1524 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001525 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1526 // detected sibcalls.
1527 if (isTailCall) {
1528 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001529 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001530 }
1531 }
Evan Cheng10043e22007-01-19 07:51:42 +00001532
Bob Wilsona4c22902009-04-17 19:07:39 +00001533 // Analyze operands of the call, assigning locations to each operand.
1534 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001535 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1536 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001537 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001538 CCAssignFnForNode(CallConv, /* Return*/ false,
1539 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001540
Bob Wilsona4c22902009-04-17 19:07:39 +00001541 // Get a count of how many bytes are to be pushed on the stack.
1542 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001543
Dale Johannesend679ff72010-06-03 21:09:53 +00001544 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001545 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001546 NumBytes = 0;
1547
Evan Cheng10043e22007-01-19 07:51:42 +00001548 // Adjust the stack pointer for the new arguments...
1549 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001550 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001551 Chain = DAG.getCALLSEQ_START(Chain,
1552 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001553
Mehdi Amini44ede332015-07-09 02:09:04 +00001554 SDValue StackPtr =
1555 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001556
Bob Wilson2e076c42009-06-22 23:27:02 +00001557 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001558 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001559
Bob Wilsona4c22902009-04-17 19:07:39 +00001560 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001561 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001562 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1563 i != e;
1564 ++i, ++realArgIdx) {
1565 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001566 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001567 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001568 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001569
Bob Wilsona4c22902009-04-17 19:07:39 +00001570 // Promote the value if needed.
1571 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001572 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001573 case CCValAssign::Full: break;
1574 case CCValAssign::SExt:
1575 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1576 break;
1577 case CCValAssign::ZExt:
1578 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1579 break;
1580 case CCValAssign::AExt:
1581 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1582 break;
1583 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001584 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001585 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001586 }
1587
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001588 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001589 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001590 if (VA.getLocVT() == MVT::v2f64) {
1591 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001592 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001593 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001594 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001595
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001596 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001597 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1598
1599 VA = ArgLocs[++i]; // skip ahead to next loc
1600 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001601 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001602 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1603 } else {
1604 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001605
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001606 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1607 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001608 }
1609 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001610 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001611 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001612 }
1613 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001614 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1615 assert(VA.getLocVT() == MVT::i32 &&
1616 "unexpected calling convention register assignment");
1617 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001618 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001619 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001620 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001621 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001622 } else if (isByVal) {
1623 assert(VA.isMemLoc());
1624 unsigned offset = 0;
1625
1626 // True if this byval aggregate will be split between registers
1627 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001628 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001629 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001630
1631 if (CurByValIdx < ByValArgsCount) {
1632
1633 unsigned RegBegin, RegEnd;
1634 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1635
Mehdi Amini44ede332015-07-09 02:09:04 +00001636 EVT PtrVT =
1637 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001638 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001639 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001640 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001641 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1642 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1643 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001644 false, false, false,
1645 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001646 MemOpChains.push_back(Load.getValue(1));
1647 RegsToPass.push_back(std::make_pair(j, Load));
1648 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001649
1650 // If parameter size outsides register area, "offset" value
1651 // helps us to calculate stack slot for remained part properly.
1652 offset = RegEnd - RegBegin;
1653
1654 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001655 }
1656
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001657 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001658 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001659 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001660 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001661 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001662 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001663 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001664 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001665 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001666 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1667 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001668
Manman Ren9f911162012-06-01 02:44:42 +00001669 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001670 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001671 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001672 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001673 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001674 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001675 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001676
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001677 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1678 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001679 }
Evan Cheng10043e22007-01-19 07:51:42 +00001680 }
1681
1682 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001683 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001684
1685 // Build a sequence of copy-to-reg nodes chained together with token chain
1686 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001687 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001688 // Tail call byval lowering might overwrite argument registers so in case of
1689 // tail call optimization the copies to registers are lowered later.
1690 if (!isTailCall)
1691 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1692 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1693 RegsToPass[i].second, InFlag);
1694 InFlag = Chain.getValue(1);
1695 }
Evan Cheng10043e22007-01-19 07:51:42 +00001696
Dale Johannesend679ff72010-06-03 21:09:53 +00001697 // For tail calls lower the arguments to the 'real' stack slot.
1698 if (isTailCall) {
1699 // Force all the incoming stack arguments to be loaded from the stack
1700 // before any new outgoing arguments are stored to the stack, because the
1701 // outgoing stack slots may alias the incoming argument stack slots, and
1702 // the alias isn't otherwise explicit. This is slightly more conservative
1703 // than necessary, because it means that each store effectively depends
1704 // on every argument instead of just those arguments it would clobber.
1705
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001706 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001707 InFlag = SDValue();
1708 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1709 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1710 RegsToPass[i].second, InFlag);
1711 InFlag = Chain.getValue(1);
1712 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001713 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001714 }
1715
Bill Wendling24c79f22008-09-16 21:48:12 +00001716 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1717 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1718 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001719 bool isDirect = false;
1720 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001721 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001722 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001723 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001724
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001725 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001726 assert((Subtarget->isTargetWindows() ||
1727 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1728 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001729 // Handle a global address or an external symbol. If it's not one of
1730 // those, the target's already in a register, so we don't need to do
1731 // anything extra.
1732 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001733 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001734 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001735 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001736 ARMConstantPoolValue *CPV =
1737 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1738
Jim Grosbach32bb3622010-04-14 22:28:31 +00001739 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001740 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001741 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001742 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1743 MachinePointerInfo::getConstantPool(), false, false,
1744 false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001745 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1746 const char *Sym = S->getSymbol();
1747
1748 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001749 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001750 ARMConstantPoolValue *CPV =
1751 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1752 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001753 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001754 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001755 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001756 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1757 MachinePointerInfo::getConstantPool(), false, false,
1758 false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001759 }
1760 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001761 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001762 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001763 bool isDef = GV->isStrongDefinitionForLinker();
1764 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001765 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001766 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001767 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001768 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001769 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001770 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001771 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001772 Callee = DAG.getNode(
1773 ARMISD::WrapperPIC, dl, PtrVt,
1774 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1775 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Tim Northoverd4d294d2014-08-06 11:13:06 +00001776 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001777 } else if (Subtarget->isTargetCOFF()) {
1778 assert(Subtarget->isTargetWindows() &&
1779 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001780 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1781 ? ARMII::MO_DLLIMPORT
1782 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001783 Callee =
1784 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001785 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001786 Callee =
1787 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1788 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
1789 MachinePointerInfo::getGOT(), false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001790 } else {
1791 // On ELF targets for PIC code, direct calls should go through the PLT
1792 unsigned OpFlags = 0;
1793 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001794 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001795 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001796 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001797 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001798 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001799 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001800 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001801 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001802 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001803 // tBX takes a register source operand.
1804 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001805 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001806 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001807 ARMConstantPoolValue *CPV =
1808 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1809 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001810 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001811 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001812 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1813 MachinePointerInfo::getConstantPool(), false, false,
1814 false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001815 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001816 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001817 } else {
1818 unsigned OpFlags = 0;
1819 // On ELF targets for PIC code, direct calls should go through the PLT
1820 if (Subtarget->isTargetELF() &&
1821 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1822 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001823 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001824 }
Evan Cheng10043e22007-01-19 07:51:42 +00001825 }
1826
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001827 // FIXME: handle tail calls differently.
1828 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001829 if (Subtarget->isThumb()) {
1830 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001831 CallOpc = ARMISD::CALL_NOLINK;
1832 else
1833 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1834 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001835 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001836 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001837 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001838 // Emit regular call when code size is the priority
1839 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001840 // "mov lr, pc; b _foo" to avoid confusing the RSP
1841 CallOpc = ARMISD::CALL_NOLINK;
1842 else
1843 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001844 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001845
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001846 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001847 Ops.push_back(Chain);
1848 Ops.push_back(Callee);
1849
1850 // Add argument registers to the end of the list so that they are known live
1851 // into the call.
1852 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1853 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1854 RegsToPass[i].second.getValueType()));
1855
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001856 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001857 if (!isTailCall) {
1858 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001859 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001860 if (isThisReturn) {
1861 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001862 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001863 if (!Mask) {
1864 // Set isThisReturn to false if the calling convention is not one that
1865 // allows 'returned' to be modeled in this way, so LowerCallResult does
1866 // not try to pass 'this' straight through
1867 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001868 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001869 }
1870 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001871 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001872
Matthias Braunc22630e2013-10-04 16:52:54 +00001873 assert(Mask && "Missing call preserved mask for calling convention");
1874 Ops.push_back(DAG.getRegisterMask(Mask));
1875 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001876
Gabor Greiff304a7a2008-08-28 21:40:38 +00001877 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001878 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001879
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001880 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001881 if (isTailCall) {
1882 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001883 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001884 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001885
Duncan Sands739a0542008-07-02 17:40:58 +00001886 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001887 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001888 InFlag = Chain.getValue(1);
1889
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001890 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1891 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001892 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001893 InFlag = Chain.getValue(1);
1894
Bob Wilsona4c22902009-04-17 19:07:39 +00001895 // Handle result values, copying them out of physregs into vregs that we
1896 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001897 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001898 InVals, isThisReturn,
1899 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001900}
1901
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001902/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001903/// on the stack. Remember the next parameter register to allocate,
1904/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001905/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001906void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1907 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001908 assert((State->getCallOrPrologue() == Prologue ||
1909 State->getCallOrPrologue() == Call) &&
1910 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001911
Tim Northover8cda34f2015-03-11 18:54:22 +00001912 // Byval (as with any stack) slots are always at least 4 byte aligned.
1913 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001914
Tim Northover8cda34f2015-03-11 18:54:22 +00001915 unsigned Reg = State->AllocateReg(GPRArgRegs);
1916 if (!Reg)
1917 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001918
Tim Northover8cda34f2015-03-11 18:54:22 +00001919 unsigned AlignInRegs = Align / 4;
1920 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1921 for (unsigned i = 0; i < Waste; ++i)
1922 Reg = State->AllocateReg(GPRArgRegs);
1923
1924 if (!Reg)
1925 return;
1926
1927 unsigned Excess = 4 * (ARM::R4 - Reg);
1928
1929 // Special case when NSAA != SP and parameter size greater than size of
1930 // all remained GPR regs. In that case we can't split parameter, we must
1931 // send it to stack. We also must set NCRN to R4, so waste all
1932 // remained registers.
1933 const unsigned NSAAOffset = State->getNextStackOffset();
1934 if (NSAAOffset != 0 && Size > Excess) {
1935 while (State->AllocateReg(GPRArgRegs))
1936 ;
1937 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001938 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001939
1940 // First register for byval parameter is the first register that wasn't
1941 // allocated before this method call, so it would be "reg".
1942 // If parameter is small enough to be saved in range [reg, r4), then
1943 // the end (first after last) register would be reg + param-size-in-regs,
1944 // else parameter would be splitted between registers and stack,
1945 // end register would be r4 in this case.
1946 unsigned ByValRegBegin = Reg;
1947 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1948 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1949 // Note, first register is allocated in the beginning of function already,
1950 // allocate remained amount of registers we need.
1951 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1952 State->AllocateReg(GPRArgRegs);
1953 // A byval parameter that is split between registers and memory needs its
1954 // size truncated here.
1955 // In the case where the entire structure fits in registers, we set the
1956 // size in memory to zero.
1957 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001958}
1959
Dale Johannesend679ff72010-06-03 21:09:53 +00001960/// MatchingStackOffset - Return true if the given stack call argument is
1961/// already available in the same position (relatively) of the caller's
1962/// incoming argument stack.
1963static
1964bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1965 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001966 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001967 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1968 int FI = INT_MAX;
1969 if (Arg.getOpcode() == ISD::CopyFromReg) {
1970 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001971 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001972 return false;
1973 MachineInstr *Def = MRI->getVRegDef(VR);
1974 if (!Def)
1975 return false;
1976 if (!Flags.isByVal()) {
1977 if (!TII->isLoadFromStackSlot(Def, FI))
1978 return false;
1979 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001980 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001981 }
1982 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1983 if (Flags.isByVal())
1984 // ByVal argument is passed in as a pointer but it's now being
1985 // dereferenced. e.g.
1986 // define @foo(%struct.X* %A) {
1987 // tail call @bar(%struct.X* byval %A)
1988 // }
1989 return false;
1990 SDValue Ptr = Ld->getBasePtr();
1991 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1992 if (!FINode)
1993 return false;
1994 FI = FINode->getIndex();
1995 } else
1996 return false;
1997
1998 assert(FI != INT_MAX);
1999 if (!MFI->isFixedObjectIndex(FI))
2000 return false;
2001 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2002}
2003
2004/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2005/// for tail call optimization. Targets which want to do tail call
2006/// optimization should implement this function.
2007bool
2008ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2009 CallingConv::ID CalleeCC,
2010 bool isVarArg,
2011 bool isCalleeStructRet,
2012 bool isCallerStructRet,
2013 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002014 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002015 const SmallVectorImpl<ISD::InputArg> &Ins,
2016 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002017 const Function *CallerF = DAG.getMachineFunction().getFunction();
2018 CallingConv::ID CallerCC = CallerF->getCallingConv();
2019 bool CCMatch = CallerCC == CalleeCC;
2020
2021 // Look for obvious safe cases to perform tail call optimization that do not
2022 // require ABI changes. This is what gcc calls sibcall.
2023
Jim Grosbache3864cc2010-06-16 23:45:49 +00002024 // Do not sibcall optimize vararg calls unless the call site is not passing
2025 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002026 if (isVarArg && !Outs.empty())
2027 return false;
2028
Tim Northoverd8407452013-10-01 14:33:28 +00002029 // Exception-handling functions need a special set of instructions to indicate
2030 // a return to the hardware. Tail-calling another function would probably
2031 // break this.
2032 if (CallerF->hasFnAttribute("interrupt"))
2033 return false;
2034
Dale Johannesend679ff72010-06-03 21:09:53 +00002035 // Also avoid sibcall optimization if either caller or callee uses struct
2036 // return semantics.
2037 if (isCalleeStructRet || isCallerStructRet)
2038 return false;
2039
Eric Christopherae326492015-03-12 22:48:50 +00002040 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002041 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2042 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2043 // support in the assembler and linker to be used. This would need to be
2044 // fixed to fully support tail calls in Thumb1.
2045 //
Dale Johannesene2289282010-07-08 01:18:23 +00002046 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2047 // LR. This means if we need to reload LR, it takes an extra instructions,
2048 // which outweighs the value of the tail call; but here we don't know yet
2049 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002050 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002051 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002052
2053 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2054 // but we need to make sure there are enough registers; the only valid
2055 // registers are the 4 used for parameters. We don't currently do this
2056 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002057 if (Subtarget->isThumb1Only())
2058 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002059
Oliver Stannard12993dd2014-08-18 12:42:15 +00002060 // Externally-defined functions with weak linkage should not be
2061 // tail-called on ARM when the OS does not support dynamic
2062 // pre-emption of symbols, as the AAELF spec requires normal calls
2063 // to undefined weak functions to be replaced with a NOP or jump to the
2064 // next instruction. The behaviour of branch instructions in this
2065 // situation (as used for tail calls) is implementation-defined, so we
2066 // cannot rely on the linker replacing the tail call with a return.
2067 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2068 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002069 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002070 if (GV->hasExternalWeakLinkage() &&
2071 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002072 return false;
2073 }
2074
Dale Johannesend679ff72010-06-03 21:09:53 +00002075 // If the calling conventions do not match, then we'd better make sure the
2076 // results are returned in the same way as what the caller expects.
2077 if (!CCMatch) {
2078 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002079 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2080 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002081 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2082
2083 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002084 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2085 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002086 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2087
2088 if (RVLocs1.size() != RVLocs2.size())
2089 return false;
2090 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2091 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2092 return false;
2093 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2094 return false;
2095 if (RVLocs1[i].isRegLoc()) {
2096 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2097 return false;
2098 } else {
2099 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2100 return false;
2101 }
2102 }
2103 }
2104
Manman Ren7e48b252012-10-12 23:39:43 +00002105 // If Caller's vararg or byval argument has been split between registers and
2106 // stack, do not perform tail call, since part of the argument is in caller's
2107 // local frame.
2108 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2109 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002110 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002111 return false;
2112
Dale Johannesend679ff72010-06-03 21:09:53 +00002113 // If the callee takes no arguments then go on to check the results of the
2114 // call.
2115 if (!Outs.empty()) {
2116 // Check if stack adjustment is needed. For now, do not do this if any
2117 // argument is passed on the stack.
2118 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002119 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2120 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002121 CCInfo.AnalyzeCallOperands(Outs,
2122 CCAssignFnForNode(CalleeCC, false, isVarArg));
2123 if (CCInfo.getNextStackOffset()) {
2124 MachineFunction &MF = DAG.getMachineFunction();
2125
2126 // Check if the arguments are already laid out in the right way as
2127 // the caller's fixed stack objects.
2128 MachineFrameInfo *MFI = MF.getFrameInfo();
2129 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002130 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002131 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2132 i != e;
2133 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002134 CCValAssign &VA = ArgLocs[i];
2135 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002136 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002137 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002138 if (VA.getLocInfo() == CCValAssign::Indirect)
2139 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002140 if (VA.needsCustom()) {
2141 // f64 and vector types are split into multiple registers or
2142 // register/stack-slot combinations. The types will not match
2143 // the registers; give up on memory f64 refs until we figure
2144 // out what to do about this.
2145 if (!VA.isRegLoc())
2146 return false;
2147 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002148 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002149 if (RegVT == MVT::v2f64) {
2150 if (!ArgLocs[++i].isRegLoc())
2151 return false;
2152 if (!ArgLocs[++i].isRegLoc())
2153 return false;
2154 }
2155 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002156 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2157 MFI, MRI, TII))
2158 return false;
2159 }
2160 }
2161 }
2162 }
2163
2164 return true;
2165}
2166
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002167bool
2168ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2169 MachineFunction &MF, bool isVarArg,
2170 const SmallVectorImpl<ISD::OutputArg> &Outs,
2171 LLVMContext &Context) const {
2172 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002173 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002174 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2175 isVarArg));
2176}
2177
Tim Northoverd8407452013-10-01 14:33:28 +00002178static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2179 SDLoc DL, SelectionDAG &DAG) {
2180 const MachineFunction &MF = DAG.getMachineFunction();
2181 const Function *F = MF.getFunction();
2182
2183 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2184
2185 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2186 // version of the "preferred return address". These offsets affect the return
2187 // instruction if this is a return from PL1 without hypervisor extensions.
2188 // IRQ/FIQ: +4 "subs pc, lr, #4"
2189 // SWI: 0 "subs pc, lr, #0"
2190 // ABORT: +4 "subs pc, lr, #4"
2191 // UNDEF: +4/+2 "subs pc, lr, #0"
2192 // UNDEF varies depending on where the exception came from ARM or Thumb
2193 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2194
2195 int64_t LROffset;
2196 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2197 IntKind == "ABORT")
2198 LROffset = 4;
2199 else if (IntKind == "SWI" || IntKind == "UNDEF")
2200 LROffset = 0;
2201 else
2202 report_fatal_error("Unsupported interrupt attribute. If present, value "
2203 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2204
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002205 RetOps.insert(RetOps.begin() + 1,
2206 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002207
Craig Topper48d114b2014-04-26 18:35:24 +00002208 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002209}
2210
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002211SDValue
2212ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002213 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002214 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002215 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002216 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002217
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002218 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002219 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002220
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002221 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002222 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2223 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002224
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002225 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002226 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2227 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002228
Bob Wilsona4c22902009-04-17 19:07:39 +00002229 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002230 SmallVector<SDValue, 4> RetOps;
2231 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002232 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002233
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002234 MachineFunction &MF = DAG.getMachineFunction();
2235 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2236 AFI->setReturnRegsCount(RVLocs.size());
2237
Bob Wilsona4c22902009-04-17 19:07:39 +00002238 // Copy the result values into the output registers.
2239 for (unsigned i = 0, realRVLocIdx = 0;
2240 i != RVLocs.size();
2241 ++i, ++realRVLocIdx) {
2242 CCValAssign &VA = RVLocs[i];
2243 assert(VA.isRegLoc() && "Can only return in registers!");
2244
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002245 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002246
2247 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002248 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002249 case CCValAssign::Full: break;
2250 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002251 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002252 break;
2253 }
2254
Bob Wilsona4c22902009-04-17 19:07:39 +00002255 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002256 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002257 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002258 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002259 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002260 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002261 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002262
Christian Pirkerb5728192014-05-08 14:06:24 +00002263 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2264 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2265 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002266 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002267 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002268 VA = RVLocs[++i]; // skip ahead to next loc
2269 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002270 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2271 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002272 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002273 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002274 VA = RVLocs[++i]; // skip ahead to next loc
2275
2276 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002277 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002278 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002279 }
2280 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2281 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002282 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002283 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002284 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2285 fmrrd.getValue(isLittleEndian ? 0 : 1),
2286 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002287 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002288 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002289 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002290 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2291 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002292 Flag);
2293 } else
2294 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2295
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002296 // Guarantee that all emitted copies are
2297 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002298 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002299 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002300 }
2301
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002302 // Update chain and glue.
2303 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002304 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002305 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002306
Tim Northoverd8407452013-10-01 14:33:28 +00002307 // CPUs which aren't M-class use a special sequence to return from
2308 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2309 // though we use "subs pc, lr, #N").
2310 //
2311 // M-class CPUs actually use a normal return sequence with a special
2312 // (hardware-provided) value in LR, so the normal code path works.
2313 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2314 !Subtarget->isMClass()) {
2315 if (Subtarget->isThumb1Only())
2316 report_fatal_error("interrupt attribute is not supported in Thumb1");
2317 return LowerInterruptReturn(RetOps, dl, DAG);
2318 }
2319
Craig Topper48d114b2014-04-26 18:35:24 +00002320 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002321}
2322
Evan Chengf8bad082012-04-10 01:51:00 +00002323bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002324 if (N->getNumValues() != 1)
2325 return false;
2326 if (!N->hasNUsesOfValue(1, 0))
2327 return false;
2328
Evan Chengf8bad082012-04-10 01:51:00 +00002329 SDValue TCChain = Chain;
2330 SDNode *Copy = *N->use_begin();
2331 if (Copy->getOpcode() == ISD::CopyToReg) {
2332 // If the copy has a glue operand, we conservatively assume it isn't safe to
2333 // perform a tail call.
2334 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2335 return false;
2336 TCChain = Copy->getOperand(0);
2337 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2338 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002339 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002340 SmallPtrSet<SDNode*, 2> Copies;
2341 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002342 UI != UE; ++UI) {
2343 if (UI->getOpcode() != ISD::CopyToReg)
2344 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002345 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002346 }
Evan Chengf8bad082012-04-10 01:51:00 +00002347 if (Copies.size() > 2)
2348 return false;
2349
2350 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2351 UI != UE; ++UI) {
2352 SDValue UseChain = UI->getOperand(0);
2353 if (Copies.count(UseChain.getNode()))
2354 // Second CopyToReg
2355 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002356 else {
2357 // We are at the top of this chain.
2358 // If the copy has a glue operand, we conservatively assume it
2359 // isn't safe to perform a tail call.
2360 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2361 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002362 // First CopyToReg
2363 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002364 }
Evan Chengf8bad082012-04-10 01:51:00 +00002365 }
2366 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002367 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002368 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002369 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002370 Copy = *Copy->use_begin();
2371 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002372 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002373 // If the copy has a glue operand, we conservatively assume it isn't safe to
2374 // perform a tail call.
2375 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2376 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002377 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002378 } else {
2379 return false;
2380 }
2381
Evan Cheng419ea282010-12-01 22:59:46 +00002382 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002383 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2384 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002385 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2386 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002387 return false;
2388 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002389 }
2390
Evan Chengf8bad082012-04-10 01:51:00 +00002391 if (!HasRet)
2392 return false;
2393
2394 Chain = TCChain;
2395 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002396}
2397
Evan Cheng0663f232011-03-21 01:19:09 +00002398bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002399 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002400 return false;
2401
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002402 auto Attr =
2403 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2404 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002405 return false;
2406
2407 return !Subtarget->isThumb1Only();
2408}
2409
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002410// Trying to write a 64 bit value so need to split into two 32 bit values first,
2411// and pass the lower and high parts through.
2412static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2413 SDLoc DL(Op);
2414 SDValue WriteValue = Op->getOperand(2);
2415
2416 // This function is only supposed to be called for i64 type argument.
2417 assert(WriteValue.getValueType() == MVT::i64
2418 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2419
2420 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2421 DAG.getConstant(0, DL, MVT::i32));
2422 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2423 DAG.getConstant(1, DL, MVT::i32));
2424 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2425 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2426}
2427
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002428// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2429// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2430// one of the above mentioned nodes. It has to be wrapped because otherwise
2431// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2432// be used to form addressing mode. These wrapped nodes will be selected
2433// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002434static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002435 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002436 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002437 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002438 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002439 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002440 if (CP->isMachineConstantPoolEntry())
2441 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2442 CP->getAlignment());
2443 else
2444 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2445 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002446 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002447}
2448
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002449unsigned ARMTargetLowering::getJumpTableEncoding() const {
2450 return MachineJumpTableInfo::EK_Inline;
2451}
2452
Dan Gohman21cea8a2010-04-17 15:26:15 +00002453SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2454 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002455 MachineFunction &MF = DAG.getMachineFunction();
2456 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2457 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002458 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002459 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002460 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002461 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2462 SDValue CPAddr;
2463 if (RelocM == Reloc::Static) {
2464 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2465 } else {
2466 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002467 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002468 ARMConstantPoolValue *CPV =
2469 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2470 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002471 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2472 }
2473 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2474 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002475 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002476 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002477 if (RelocM == Reloc::Static)
2478 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002479 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002480 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002481}
2482
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002483// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002484SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002485ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002486 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002487 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002488 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002489 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002490 MachineFunction &MF = DAG.getMachineFunction();
2491 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002492 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002493 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002494 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2495 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002496 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002497 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002498 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002499 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002500 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002501 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002502
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002503 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002504 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002505
2506 // call __tls_get_addr.
2507 ArgListTy Args;
2508 ArgListEntry Entry;
2509 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002510 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002511 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002512
Dale Johannesen555a3752009-01-30 23:10:59 +00002513 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002514 TargetLowering::CallLoweringInfo CLI(DAG);
2515 CLI.setDebugLoc(dl).setChain(Chain)
2516 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002517 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2518 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002519
Justin Holewinskiaa583972012-05-25 16:35:28 +00002520 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002521 return CallResult.first;
2522}
2523
2524// Lower ISD::GlobalTLSAddress using the "initial exec" or
2525// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002526SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002527ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002528 SelectionDAG &DAG,
2529 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002530 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002531 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002532 SDValue Offset;
2533 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002534 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002535 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002536 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002537
Hans Wennborgaea41202012-05-04 09:40:39 +00002538 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002539 MachineFunction &MF = DAG.getMachineFunction();
2540 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002541 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002542 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002543 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2544 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002545 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2546 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2547 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002548 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002549 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002550 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002551 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002552 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002553 Chain = Offset.getValue(1);
2554
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002555 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002556 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002557
Evan Chengcdbb70c2009-10-31 03:39:36 +00002558 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002559 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002560 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002561 } else {
2562 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002563 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002564 ARMConstantPoolValue *CPV =
2565 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002566 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002567 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002568 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002569 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002570 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002571 }
2572
2573 // The address of the thread local variable is the add of the thread
2574 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002575 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002576}
2577
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002578SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002579ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002580 // TODO: implement the "local dynamic" model
2581 assert(Subtarget->isTargetELF() &&
2582 "TLS not implemented for non-ELF targets");
2583 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002584 if (DAG.getTarget().Options.EmulatedTLS)
2585 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002586
2587 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2588
2589 switch (model) {
2590 case TLSModel::GeneralDynamic:
2591 case TLSModel::LocalDynamic:
2592 return LowerToTLSGeneralDynamicModel(GA, DAG);
2593 case TLSModel::InitialExec:
2594 case TLSModel::LocalExec:
2595 return LowerToTLSExecModels(GA, DAG, model);
2596 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002597 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002598}
2599
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002600SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002601 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002602 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002603 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002604 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002605 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002606 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002607 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002608 ARMConstantPoolConstant::Create(GV,
2609 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002610 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002611 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002612 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002613 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002614 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002615 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002616 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002617 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002618 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002619 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002620 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002621 MachinePointerInfo::getGOT(),
2622 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002623 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002624 }
2625
2626 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002627 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002628 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002629 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002630 // FIXME: Once remat is capable of dealing with instructions with register
2631 // operands, expand this into two nodes.
2632 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2633 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002634 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002635 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2636 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2637 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2638 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002639 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002640 }
2641}
2642
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002643SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002644 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002645 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002646 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002647 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002648 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002649
Eric Christopherc1058df2014-07-04 01:55:26 +00002650 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002651 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002652
Tim Northover72360d22013-12-02 10:35:41 +00002653 // FIXME: Once remat is capable of dealing with instructions with register
2654 // operands, expand this into multiple nodes
2655 unsigned Wrapper =
2656 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002657
Tim Northover72360d22013-12-02 10:35:41 +00002658 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2659 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002660
Evan Cheng1b389522009-09-03 07:04:02 +00002661 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002662 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2663 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002664 return Result;
2665}
2666
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002667SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2668 SelectionDAG &DAG) const {
2669 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002670 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2671 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002672
2673 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002674 const ARMII::TOF TargetFlags =
2675 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002676 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002677 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002678 SDLoc DL(Op);
2679
2680 ++NumMovwMovt;
2681
2682 // FIXME: Once remat is capable of dealing with instructions with register
2683 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002684 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2685 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002686 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002687 if (GV->hasDLLImportStorageClass())
2688 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2689 MachinePointerInfo::getGOT(), false, false, false, 0);
2690 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002691}
2692
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002693SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002694 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002695 assert(Subtarget->isTargetELF() &&
2696 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002697 MachineFunction &MF = DAG.getMachineFunction();
2698 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002699 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002700 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002701 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002702 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002703 ARMConstantPoolValue *CPV =
2704 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2705 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002706 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002707 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002708 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002709 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002710 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002711 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002712 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002713}
2714
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002715SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002716ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002717 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002718 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002719 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2720 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002721 Op.getOperand(1), Val);
2722}
2723
2724SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002725ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002726 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002727 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002728 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002729}
2730
Matthias Braun3cd00c12015-07-16 22:34:16 +00002731SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2732 SelectionDAG &DAG) const {
2733 SDLoc dl(Op);
2734 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2735 Op.getOperand(0));
2736}
2737
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002738SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002739ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002740 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002741 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002742 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002743 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002744 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002745 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002746 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002747 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002748 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002749 }
Bob Wilson17f88782009-08-04 00:25:01 +00002750 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002751 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002752 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2753 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002754 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002755 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002756 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002757 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002758 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002759 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2760 SDValue CPAddr;
2761 unsigned PCAdj = (RelocM != Reloc::PIC_)
2762 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002763 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002764 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2765 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002766 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002767 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002768 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002769 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002770 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002771 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002772
2773 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002774 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002775 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2776 }
2777 return Result;
2778 }
Evan Cheng18381b42011-03-29 23:06:19 +00002779 case Intrinsic::arm_neon_vmulls:
2780 case Intrinsic::arm_neon_vmullu: {
2781 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2782 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002783 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002784 Op.getOperand(1), Op.getOperand(2));
2785 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002786 }
2787}
2788
Eli Friedman30a49e92011-08-03 21:06:02 +00002789static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2790 const ARMSubtarget *Subtarget) {
2791 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002792 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002793 if (!Subtarget->hasDataBarrier()) {
2794 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2795 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2796 // here.
2797 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002798 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002799 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002800 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002801 }
2802
Tim Northover36b24172013-07-03 09:20:36 +00002803 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2804 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002805 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002806 if (Subtarget->isMClass()) {
2807 // Only a full system barrier exists in the M-class architectures.
2808 Domain = ARM_MB::SY;
2809 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002810 // Swift happens to implement ISHST barriers in a way that's compatible with
2811 // Release semantics but weaker than ISH so we'd be fools not to use
2812 // it. Beware: other processors probably don't!
2813 Domain = ARM_MB::ISHST;
2814 }
2815
Joey Gouly926d3f52013-09-05 15:35:24 +00002816 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2818 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002819}
2820
Evan Cheng8740ee32010-11-03 06:34:55 +00002821static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2822 const ARMSubtarget *Subtarget) {
2823 // ARM pre v5TE and Thumb1 does not have preload instructions.
2824 if (!(Subtarget->isThumb2() ||
2825 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2826 // Just preserve the chain.
2827 return Op.getOperand(0);
2828
Andrew Trickef9de2a2013-05-25 02:42:55 +00002829 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002830 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2831 if (!isRead &&
2832 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2833 // ARMv7 with MP extension has PLDW.
2834 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002835
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002836 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2837 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002838 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002839 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002840 isData = ~isData & 1;
2841 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002842
2843 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002844 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2845 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002846}
2847
Dan Gohman31ae5862010-04-17 14:41:14 +00002848static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2849 MachineFunction &MF = DAG.getMachineFunction();
2850 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2851
Evan Cheng10043e22007-01-19 07:51:42 +00002852 // vastart just stores the address of the VarArgsFrameIndex slot into the
2853 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002854 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002855 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002856 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002857 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002858 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2859 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002860}
2861
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002862SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002863ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2864 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002865 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002866 MachineFunction &MF = DAG.getMachineFunction();
2867 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2868
Craig Topper760b1342012-02-22 05:59:10 +00002869 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002870 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002871 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002872 else
Craig Topperc7242e02012-04-20 07:30:17 +00002873 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002874
2875 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002876 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002877 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002878
2879 SDValue ArgValue2;
2880 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002881 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002882 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002883
2884 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002885 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Evan Chengcdbb70c2009-10-31 03:39:36 +00002886 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002887 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002888 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002889 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002890 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002891 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002892 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002893 if (!Subtarget->isLittle())
2894 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002895 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002896}
2897
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002898// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002899// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002900// byval). Either way, we allocate stack slots adjacent to the data
2901// provided by our caller, and store the unallocated registers there.
2902// If this is a variadic function, the va_list pointer will begin with
2903// these values; otherwise, this reassembles a (byval) structure that
2904// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002905// Return: The frame index registers were stored into.
2906int
2907ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002908 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002909 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002910 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002911 int ArgOffset,
2912 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002913 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002914 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002915 // Setup first unallocated register as first byval register;
2916 // eat all remained registers
2917 // (these two actions are performed by HandleByVal method).
2918 // Then, here, we initialize stack frame with
2919 // "store-reg" instructions.
2920 // Case #2. Var-args function, that doesn't contain byval parameters.
2921 // The same: eat all remained unallocated registers,
2922 // initialize stack frame.
2923
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002924 MachineFunction &MF = DAG.getMachineFunction();
2925 MachineFrameInfo *MFI = MF.getFrameInfo();
2926 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002927 unsigned RBegin, REnd;
2928 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2929 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002930 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002931 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00002932 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00002933 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002934 }
2935
Tim Northover8cda34f2015-03-11 18:54:22 +00002936 if (REnd != RBegin)
2937 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002938
Mehdi Amini44ede332015-07-09 02:09:04 +00002939 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00002940 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002941 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002942
Tim Northover8cda34f2015-03-11 18:54:22 +00002943 SmallVector<SDValue, 4> MemOps;
2944 const TargetRegisterClass *RC =
2945 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002946
Tim Northover8cda34f2015-03-11 18:54:22 +00002947 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
2948 unsigned VReg = MF.addLiveIn(Reg, RC);
2949 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2950 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002951 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00002952 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
2953 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00002954 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00002955 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002956
2957 if (!MemOps.empty())
2958 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
2959 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002960}
2961
2962// Setup stack frame, the va_list pointer will start from.
2963void
2964ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002965 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002966 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002967 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002968 bool ForceMutable) const {
2969 MachineFunction &MF = DAG.getMachineFunction();
2970 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2971
2972 // Try to store any remaining integer argument regs
2973 // to their spots on the stack so that they may be loaded by deferencing
2974 // the result of va_next.
2975 // If there is no regs to be stored, just point address after last
2976 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00002977 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2978 CCInfo.getInRegsParamsCount(),
2979 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002980 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002981}
2982
Bob Wilson2e076c42009-06-22 23:27:02 +00002983SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002984ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002985 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002986 const SmallVectorImpl<ISD::InputArg>
2987 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002988 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002989 SmallVectorImpl<SDValue> &InVals)
2990 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002991 MachineFunction &MF = DAG.getMachineFunction();
2992 MachineFrameInfo *MFI = MF.getFrameInfo();
2993
Bob Wilsona4c22902009-04-17 19:07:39 +00002994 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2995
2996 // Assign locations to all of the incoming arguments.
2997 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002998 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2999 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003000 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003001 CCAssignFnForNode(CallConv, /* Return*/ false,
3002 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003003
Bob Wilsona4c22902009-04-17 19:07:39 +00003004 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003005 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003006 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3007 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003008
3009 // Initially ArgRegsSaveSize is zero.
3010 // Then we increase this value each time we meet byval parameter.
3011 // We also increase this value in case of varargs function.
3012 AFI->setArgRegsSaveSize(0);
3013
Oliver Stannardd55e1152014-03-05 15:25:27 +00003014 // Calculate the amount of stack space that we need to allocate to store
3015 // byval and variadic arguments that are passed in registers.
3016 // We need to know this before we allocate the first byval or variadic
3017 // argument, as they will be allocated a stack slot below the CFA (Canonical
3018 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003019 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003020 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003021 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3022 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003023
Tim Northover8cda34f2015-03-11 18:54:22 +00003024 CCValAssign &VA = ArgLocs[i];
3025 unsigned Index = VA.getValNo();
3026 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3027 if (!Flags.isByVal())
3028 continue;
3029
3030 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3031 unsigned RBegin, REnd;
3032 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3033 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3034
3035 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003036 }
3037 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003038
3039 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003040 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003041 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3042 if (RegIdx != array_lengthof(GPRArgRegs))
3043 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003044 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003045
3046 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3047 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003048 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003049
Bob Wilsona4c22902009-04-17 19:07:39 +00003050 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3051 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003052 if (Ins[VA.getValNo()].isOrigArg()) {
3053 std::advance(CurOrigArg,
3054 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3055 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3056 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003057 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003058 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003059 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003060
Bob Wilsona4c22902009-04-17 19:07:39 +00003061 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003062 // f64 and vector types are split up into multiple registers or
3063 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003064 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003065 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003066 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003067 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003068 SDValue ArgValue2;
3069 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003070 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003071 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bob Wilson699bdf72010-04-13 22:03:22 +00003072 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003073 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003074 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003075 } else {
3076 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3077 Chain, DAG, dl);
3078 }
Owen Anderson9f944592009-08-11 20:47:22 +00003079 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3080 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003081 ArgValue, ArgValue1,
3082 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003083 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003084 ArgValue, ArgValue2,
3085 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003086 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003087 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003088
Bob Wilson2e076c42009-06-22 23:27:02 +00003089 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003090 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003091
Owen Anderson9f944592009-08-11 20:47:22 +00003092 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003093 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003094 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003095 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003096 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003097 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003098 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003099 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3100 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003101 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003102 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003103
3104 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003105 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003106 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003107 }
3108
3109 // If this is an 8 or 16-bit value, it is really passed promoted
3110 // to 32 bits. Insert an assert[sz]ext to capture this, then
3111 // truncate to the right size.
3112 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003113 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003114 case CCValAssign::Full: break;
3115 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003116 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003117 break;
3118 case CCValAssign::SExt:
3119 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3120 DAG.getValueType(VA.getValVT()));
3121 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3122 break;
3123 case CCValAssign::ZExt:
3124 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3125 DAG.getValueType(VA.getValVT()));
3126 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3127 break;
3128 }
3129
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003130 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003131
3132 } else { // VA.isRegLoc()
3133
3134 // sanity check
3135 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003136 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003137
Andrew Trick05938a52015-02-16 18:10:47 +00003138 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003139
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003140 // Some Ins[] entries become multiple ArgLoc[] entries.
3141 // Process them only once.
3142 if (index != lastInsIndex)
3143 {
3144 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003145 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003146 // This can be changed with more analysis.
3147 // In case of tail call optimization mark all arguments mutable.
3148 // Since they could be overwritten by lowering of arguments in case of
3149 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003150 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003151 assert(Ins[index].isOrigArg() &&
3152 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003153 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003154
Tim Northover8cda34f2015-03-11 18:54:22 +00003155 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3156 CurByValIndex, VA.getLocMemOffset(),
3157 Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003158 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003159 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003160 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003161 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003162 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003163 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003164
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003165 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003166 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003167 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3168 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003169 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003170 }
3171 lastInsIndex = index;
3172 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003173 }
3174 }
3175
3176 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003177 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003178 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003179 CCInfo.getNextStackOffset(),
3180 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003181
Oliver Stannardb14c6252014-04-02 16:10:33 +00003182 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3183
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003184 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003185}
3186
3187/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003188static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003189 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003190 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003191 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003192 // Maybe this has already been legalized into the constant pool?
3193 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003194 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003195 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003196 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003197 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003198 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003199 } else if (Op->getOpcode() == ISD::BITCAST &&
3200 Op->getValueType(0) == MVT::f64) {
3201 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3202 // created by LowerConstantFP().
3203 SDValue BitcastOp = Op->getOperand(0);
3204 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3205 SDValue MoveOp = BitcastOp->getOperand(0);
3206 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3207 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3208 return true;
3209 }
3210 }
Evan Cheng10043e22007-01-19 07:51:42 +00003211 }
3212 return false;
3213}
3214
Evan Cheng10043e22007-01-19 07:51:42 +00003215/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3216/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003217SDValue
3218ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003219 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003220 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003221 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003222 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003223 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003224 // Constant does not fit, try adjusting it by one?
3225 switch (CC) {
3226 default: break;
3227 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003228 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003229 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003230 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003231 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003232 }
3233 break;
3234 case ISD::SETULT:
3235 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003236 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003237 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003238 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003239 }
3240 break;
3241 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003242 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003243 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003244 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003245 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003246 }
3247 break;
3248 case ISD::SETULE:
3249 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003250 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003251 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003252 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003253 }
3254 break;
3255 }
3256 }
3257 }
3258
3259 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003260 ARMISD::NodeType CompareType;
3261 switch (CondCode) {
3262 default:
3263 CompareType = ARMISD::CMP;
3264 break;
3265 case ARMCC::EQ:
3266 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003267 // Uses only Z Flag
3268 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003269 break;
3270 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003271 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003272 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003273}
3274
3275/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003276SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003277ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003278 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003279 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003280 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003281 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003282 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003283 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003284 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3285 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003286}
3287
Bob Wilson45acbd02011-03-08 01:17:20 +00003288/// duplicateCmp - Glue values can have only one use, so this function
3289/// duplicates a comparison node.
3290SDValue
3291ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3292 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003293 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003294 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3295 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3296
3297 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3298 Cmp = Cmp.getOperand(0);
3299 Opc = Cmp.getOpcode();
3300 if (Opc == ARMISD::CMPFP)
3301 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3302 else {
3303 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3304 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3305 }
3306 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3307}
3308
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003309std::pair<SDValue, SDValue>
3310ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3311 SDValue &ARMcc) const {
3312 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3313
3314 SDValue Value, OverflowCmp;
3315 SDValue LHS = Op.getOperand(0);
3316 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003317 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003318
3319 // FIXME: We are currently always generating CMPs because we don't support
3320 // generating CMN through the backend. This is not as good as the natural
3321 // CMP case because it causes a register dependency and cannot be folded
3322 // later.
3323
3324 switch (Op.getOpcode()) {
3325 default:
3326 llvm_unreachable("Unknown overflow instruction!");
3327 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003328 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3329 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3330 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003331 break;
3332 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003333 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3334 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3335 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003336 break;
3337 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003338 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3339 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3340 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003341 break;
3342 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003343 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3344 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3345 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003346 break;
3347 } // switch (...)
3348
3349 return std::make_pair(Value, OverflowCmp);
3350}
3351
3352
3353SDValue
3354ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3355 // Let legalize expand this if it isn't a legal type yet.
3356 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3357 return SDValue();
3358
3359 SDValue Value, OverflowCmp;
3360 SDValue ARMcc;
3361 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3362 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003363 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003364 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003365 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3366 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003367 EVT VT = Op.getValueType();
3368
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003369 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003370 ARMcc, CCR, OverflowCmp);
3371
3372 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003373 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003374}
3375
3376
Bill Wendling6a981312010-08-11 08:43:16 +00003377SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3378 SDValue Cond = Op.getOperand(0);
3379 SDValue SelectTrue = Op.getOperand(1);
3380 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003381 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003382 unsigned Opc = Cond.getOpcode();
3383
3384 if (Cond.getResNo() == 1 &&
3385 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3386 Opc == ISD::USUBO)) {
3387 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3388 return SDValue();
3389
3390 SDValue Value, OverflowCmp;
3391 SDValue ARMcc;
3392 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3393 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3394 EVT VT = Op.getValueType();
3395
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003396 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003397 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003398 }
Bill Wendling6a981312010-08-11 08:43:16 +00003399
3400 // Convert:
3401 //
3402 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3403 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3404 //
3405 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3406 const ConstantSDNode *CMOVTrue =
3407 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3408 const ConstantSDNode *CMOVFalse =
3409 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3410
3411 if (CMOVTrue && CMOVFalse) {
3412 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3413 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3414
3415 SDValue True;
3416 SDValue False;
3417 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3418 True = SelectTrue;
3419 False = SelectFalse;
3420 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3421 True = SelectFalse;
3422 False = SelectTrue;
3423 }
3424
3425 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003426 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003427 SDValue ARMcc = Cond.getOperand(2);
3428 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003429 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003430 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003431 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003432 }
3433 }
3434 }
3435
Dan Gohmand4a77c42012-02-24 00:09:36 +00003436 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3437 // undefined bits before doing a full-word comparison with zero.
3438 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003439 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003440
Bill Wendling6a981312010-08-11 08:43:16 +00003441 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003442 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003443 SelectTrue, SelectFalse, ISD::SETNE);
3444}
3445
Joey Gouly881eab52013-08-22 15:29:11 +00003446static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3447 bool &swpCmpOps, bool &swpVselOps) {
3448 // Start by selecting the GE condition code for opcodes that return true for
3449 // 'equality'
3450 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3451 CC == ISD::SETULE)
3452 CondCode = ARMCC::GE;
3453
3454 // and GT for opcodes that return false for 'equality'.
3455 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3456 CC == ISD::SETULT)
3457 CondCode = ARMCC::GT;
3458
3459 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3460 // to swap the compare operands.
3461 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3462 CC == ISD::SETULT)
3463 swpCmpOps = true;
3464
3465 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3466 // If we have an unordered opcode, we need to swap the operands to the VSEL
3467 // instruction (effectively negating the condition).
3468 //
3469 // This also has the effect of swapping which one of 'less' or 'greater'
3470 // returns true, so we also swap the compare operands. It also switches
3471 // whether we return true for 'equality', so we compensate by picking the
3472 // opposite condition code to our original choice.
3473 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3474 CC == ISD::SETUGT) {
3475 swpCmpOps = !swpCmpOps;
3476 swpVselOps = !swpVselOps;
3477 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3478 }
3479
3480 // 'ordered' is 'anything but unordered', so use the VS condition code and
3481 // swap the VSEL operands.
3482 if (CC == ISD::SETO) {
3483 CondCode = ARMCC::VS;
3484 swpVselOps = true;
3485 }
3486
3487 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3488 // code and swap the VSEL operands.
3489 if (CC == ISD::SETUNE) {
3490 CondCode = ARMCC::EQ;
3491 swpVselOps = true;
3492 }
3493}
3494
Oliver Stannard51b1d462014-08-21 12:50:31 +00003495SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3496 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3497 SDValue Cmp, SelectionDAG &DAG) const {
3498 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3499 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3500 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3501 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3502 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3503
3504 SDValue TrueLow = TrueVal.getValue(0);
3505 SDValue TrueHigh = TrueVal.getValue(1);
3506 SDValue FalseLow = FalseVal.getValue(0);
3507 SDValue FalseHigh = FalseVal.getValue(1);
3508
3509 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3510 ARMcc, CCR, Cmp);
3511 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3512 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3513
3514 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3515 } else {
3516 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3517 Cmp);
3518 }
3519}
3520
Dan Gohman21cea8a2010-04-17 15:26:15 +00003521SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003522 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003523 SDValue LHS = Op.getOperand(0);
3524 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003525 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003526 SDValue TrueVal = Op.getOperand(2);
3527 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003528 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003529
Oliver Stannard51b1d462014-08-21 12:50:31 +00003530 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3531 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3532 dl);
3533
3534 // If softenSetCCOperands only returned one value, we should compare it to
3535 // zero.
3536 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003537 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003538 CC = ISD::SETNE;
3539 }
3540 }
3541
Owen Anderson9f944592009-08-11 20:47:22 +00003542 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003543 // Try to generate VSEL on ARMv8.
3544 // The VSEL instruction can't use all the usual ARM condition
3545 // codes: it only has two bits to select the condition code, so it's
3546 // constrained to use only GE, GT, VS and EQ.
3547 //
3548 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3549 // swap the operands of the previous compare instruction (effectively
3550 // inverting the compare condition, swapping 'less' and 'greater') and
3551 // sometimes need to swap the operands to the VSEL (which inverts the
3552 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003553 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3554 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003555 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3556 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3557 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003558 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003559 std::swap(TrueVal, FalseVal);
3560 }
3561 }
3562
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003563 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003564 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003565 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003566 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003567 }
3568
3569 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003570 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003571
Scott Douglass7ad77922015-04-08 17:18:28 +00003572 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003573 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3574 TrueVal.getValueType() == MVT::f64)) {
Scott Douglass7ad77922015-04-08 17:18:28 +00003575 // We can use VMAXNM/VMINNM for a compare followed by a select with the
Joey Goulye3dd6842013-08-23 12:01:13 +00003576 // same operands, as follows:
Scott Douglass7ad77922015-04-08 17:18:28 +00003577 // c = fcmp [?gt, ?ge, ?lt, ?le] a, b
Joey Goulye3dd6842013-08-23 12:01:13 +00003578 // select c, a, b
Scott Douglass7ad77922015-04-08 17:18:28 +00003579 // In NoNaNsFPMath the CC will have been changed from, e.g., 'ogt' to 'gt'.
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003580 bool swapSides = false;
3581 if (!getTargetMachine().Options.NoNaNsFPMath) {
3582 // transformability may depend on which way around we compare
3583 switch (CC) {
3584 default:
3585 break;
3586 case ISD::SETOGT:
3587 case ISD::SETOGE:
3588 case ISD::SETOLT:
3589 case ISD::SETOLE:
3590 // the non-NaN should be RHS
3591 swapSides = DAG.isKnownNeverNaN(LHS) && !DAG.isKnownNeverNaN(RHS);
3592 break;
3593 case ISD::SETUGT:
3594 case ISD::SETUGE:
3595 case ISD::SETULT:
3596 case ISD::SETULE:
3597 // the non-NaN should be LHS
3598 swapSides = DAG.isKnownNeverNaN(RHS) && !DAG.isKnownNeverNaN(LHS);
3599 break;
3600 }
3601 }
3602 swapSides = swapSides || (LHS == FalseVal && RHS == TrueVal);
3603 if (swapSides) {
3604 CC = ISD::getSetCCSwappedOperands(CC);
3605 std::swap(LHS, RHS);
3606 }
3607 if (LHS == TrueVal && RHS == FalseVal) {
3608 bool canTransform = true;
3609 // FIXME: FastMathFlags::noSignedZeros() doesn't appear reachable from here
3610 if (!getTargetMachine().Options.UnsafeFPMath &&
3611 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
3612 const ConstantFPSDNode *Zero;
3613 switch (CC) {
3614 default:
3615 break;
3616 case ISD::SETOGT:
3617 case ISD::SETUGT:
3618 case ISD::SETGT:
3619 // RHS must not be -0
3620 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3621 !Zero->isNegative();
3622 break;
3623 case ISD::SETOGE:
3624 case ISD::SETUGE:
3625 case ISD::SETGE:
3626 // LHS must not be -0
3627 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3628 !Zero->isNegative();
3629 break;
3630 case ISD::SETOLT:
3631 case ISD::SETULT:
3632 case ISD::SETLT:
3633 // RHS must not be +0
3634 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3635 Zero->isNegative();
3636 break;
3637 case ISD::SETOLE:
3638 case ISD::SETULE:
3639 case ISD::SETLE:
3640 // LHS must not be +0
3641 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3642 Zero->isNegative();
3643 break;
3644 }
3645 }
3646 if (canTransform) {
3647 // Note: If one of the elements in a pair is a number and the other
3648 // element is NaN, the corresponding result element is the number.
3649 // This is consistent with the IEEE 754-2008 standard.
3650 // Therefore, a > b ? a : b <=> vmax(a,b), if b is constant and a is NaN
3651 switch (CC) {
3652 default:
3653 break;
3654 case ISD::SETOGT:
3655 case ISD::SETOGE:
3656 if (!DAG.isKnownNeverNaN(RHS))
3657 break;
3658 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3659 case ISD::SETUGT:
3660 case ISD::SETUGE:
3661 if (!DAG.isKnownNeverNaN(LHS))
3662 break;
3663 case ISD::SETGT:
3664 case ISD::SETGE:
3665 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3666 case ISD::SETOLT:
3667 case ISD::SETOLE:
3668 if (!DAG.isKnownNeverNaN(RHS))
3669 break;
3670 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3671 case ISD::SETULT:
3672 case ISD::SETULE:
3673 if (!DAG.isKnownNeverNaN(LHS))
3674 break;
3675 case ISD::SETLT:
3676 case ISD::SETLE:
3677 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3678 }
Oliver Stannard79efe412014-10-27 09:23:02 +00003679 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003680 }
3681
Joey Gouly881eab52013-08-22 15:29:11 +00003682 bool swpCmpOps = false;
3683 bool swpVselOps = false;
3684 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3685
3686 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3687 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3688 if (swpCmpOps)
3689 std::swap(LHS, RHS);
3690 if (swpVselOps)
3691 std::swap(TrueVal, FalseVal);
3692 }
3693 }
3694
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003695 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003696 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003697 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003698 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003699 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003700 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003701 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003702 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003703 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003704 }
3705 return Result;
3706}
3707
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003708/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3709/// to morph to an integer compare sequence.
3710static bool canChangeToInt(SDValue Op, bool &SeenZero,
3711 const ARMSubtarget *Subtarget) {
3712 SDNode *N = Op.getNode();
3713 if (!N->hasOneUse())
3714 // Otherwise it requires moving the value from fp to integer registers.
3715 return false;
3716 if (!N->getNumValues())
3717 return false;
3718 EVT VT = Op.getValueType();
3719 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3720 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3721 // vmrs are very slow, e.g. cortex-a8.
3722 return false;
3723
3724 if (isFloatingPointZero(Op)) {
3725 SeenZero = true;
3726 return true;
3727 }
3728 return ISD::isNormalLoad(N);
3729}
3730
3731static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3732 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003733 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003734
3735 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003736 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003737 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003738 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003739 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003740
3741 llvm_unreachable("Unknown VFP cmp argument!");
3742}
3743
3744static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3745 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003746 SDLoc dl(Op);
3747
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003748 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003749 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3750 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003751 return;
3752 }
3753
3754 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3755 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003756 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003757 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003758 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003759 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003760 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003761
3762 EVT PtrType = Ptr.getValueType();
3763 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003764 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3765 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3766 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003767 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003768 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003769 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003770 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003771 return;
3772 }
3773
3774 llvm_unreachable("Unknown VFP cmp argument!");
3775}
3776
3777/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3778/// f32 and even f64 comparisons to integer ones.
3779SDValue
3780ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3781 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003782 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003783 SDValue LHS = Op.getOperand(2);
3784 SDValue RHS = Op.getOperand(3);
3785 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003786 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003787
Evan Chengd12af5d2012-03-01 23:27:13 +00003788 bool LHSSeenZero = false;
3789 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3790 bool RHSSeenZero = false;
3791 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3792 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003793 // If unsafe fp math optimization is enabled and there are no other uses of
3794 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003795 // to an integer comparison.
3796 if (CC == ISD::SETOEQ)
3797 CC = ISD::SETEQ;
3798 else if (CC == ISD::SETUNE)
3799 CC = ISD::SETNE;
3800
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003801 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003802 SDValue ARMcc;
3803 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003804 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3805 bitcastf32Toi32(LHS, DAG), Mask);
3806 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3807 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003808 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3809 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3810 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3811 Chain, Dest, ARMcc, CCR, Cmp);
3812 }
3813
3814 SDValue LHS1, LHS2;
3815 SDValue RHS1, RHS2;
3816 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3817 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003818 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3819 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003820 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003821 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003822 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003823 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003824 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003825 }
3826
3827 return SDValue();
3828}
3829
3830SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3831 SDValue Chain = Op.getOperand(0);
3832 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3833 SDValue LHS = Op.getOperand(2);
3834 SDValue RHS = Op.getOperand(3);
3835 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003836 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003837
Oliver Stannard51b1d462014-08-21 12:50:31 +00003838 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3839 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3840 dl);
3841
3842 // If softenSetCCOperands only returned one value, we should compare it to
3843 // zero.
3844 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003845 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003846 CC = ISD::SETNE;
3847 }
3848 }
3849
Owen Anderson9f944592009-08-11 20:47:22 +00003850 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003851 SDValue ARMcc;
3852 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003853 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003854 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003855 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003856 }
3857
Owen Anderson9f944592009-08-11 20:47:22 +00003858 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003859
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003860 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003861 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3862 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3863 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3864 if (Result.getNode())
3865 return Result;
3866 }
3867
Evan Cheng10043e22007-01-19 07:51:42 +00003868 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003869 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003870
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003871 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003872 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003873 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003874 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003875 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003876 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003877 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003878 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003879 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003880 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003881 }
3882 return Res;
3883}
3884
Dan Gohman21cea8a2010-04-17 15:26:15 +00003885SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003886 SDValue Chain = Op.getOperand(0);
3887 SDValue Table = Op.getOperand(1);
3888 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003889 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003890
Mehdi Amini44ede332015-07-09 02:09:04 +00003891 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003892 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003893 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003894 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003895 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003896 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003897 if (Subtarget->isThumb2()) {
3898 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3899 // which does another jump to the destination. This also makes it easier
3900 // to translate it to TBB / TBH later.
3901 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003902 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003903 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003904 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003905 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003906 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003907 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003908 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003909 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003910 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003911 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003912 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003913 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003914 MachinePointerInfo::getJumpTable(),
3915 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003916 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003917 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003918 }
Evan Cheng10043e22007-01-19 07:51:42 +00003919}
3920
Eli Friedman2d4055b2011-11-09 23:36:02 +00003921static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003922 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003923 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003924
James Molloy547d4c02012-02-20 09:24:05 +00003925 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3926 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3927 return Op;
3928 return DAG.UnrollVectorOp(Op.getNode());
3929 }
3930
3931 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3932 "Invalid type for custom lowering!");
3933 if (VT != MVT::v4i16)
3934 return DAG.UnrollVectorOp(Op.getNode());
3935
3936 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3937 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003938}
3939
Oliver Stannard51b1d462014-08-21 12:50:31 +00003940SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003941 EVT VT = Op.getValueType();
3942 if (VT.isVector())
3943 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003944 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3945 RTLIB::Libcall LC;
3946 if (Op.getOpcode() == ISD::FP_TO_SINT)
3947 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3948 Op.getValueType());
3949 else
3950 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3951 Op.getValueType());
3952 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3953 /*isSigned*/ false, SDLoc(Op)).first;
3954 }
3955
James Molloyfa041152015-03-23 16:15:16 +00003956 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003957}
3958
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003959static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3960 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003961 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003962
Eli Friedman2d4055b2011-11-09 23:36:02 +00003963 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3964 if (VT.getVectorElementType() == MVT::f32)
3965 return Op;
3966 return DAG.UnrollVectorOp(Op.getNode());
3967 }
3968
Duncan Sandsa41634e2011-08-12 14:54:45 +00003969 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3970 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003971 if (VT != MVT::v4f32)
3972 return DAG.UnrollVectorOp(Op.getNode());
3973
3974 unsigned CastOpc;
3975 unsigned Opc;
3976 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003977 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003978 case ISD::SINT_TO_FP:
3979 CastOpc = ISD::SIGN_EXTEND;
3980 Opc = ISD::SINT_TO_FP;
3981 break;
3982 case ISD::UINT_TO_FP:
3983 CastOpc = ISD::ZERO_EXTEND;
3984 Opc = ISD::UINT_TO_FP;
3985 break;
3986 }
3987
3988 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3989 return DAG.getNode(Opc, dl, VT, Op);
3990}
3991
Oliver Stannard51b1d462014-08-21 12:50:31 +00003992SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003993 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003994 if (VT.isVector())
3995 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003996 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3997 RTLIB::Libcall LC;
3998 if (Op.getOpcode() == ISD::SINT_TO_FP)
3999 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4000 Op.getValueType());
4001 else
4002 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4003 Op.getValueType());
4004 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
4005 /*isSigned*/ false, SDLoc(Op)).first;
4006 }
4007
James Molloyfa041152015-03-23 16:15:16 +00004008 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004009}
4010
Evan Cheng25f93642010-07-08 02:08:50 +00004011SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004012 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004013 SDValue Tmp0 = Op.getOperand(0);
4014 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004015 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004016 EVT VT = Op.getValueType();
4017 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004018 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4019 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4020 bool UseNEON = !InGPR && Subtarget->hasNEON();
4021
4022 if (UseNEON) {
4023 // Use VBSL to copy the sign bit.
4024 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4025 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004026 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004027 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4028 if (VT == MVT::f64)
4029 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4030 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004031 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004032 else /*if (VT == MVT::f32)*/
4033 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4034 if (SrcVT == MVT::f32) {
4035 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4036 if (VT == MVT::f64)
4037 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4038 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004039 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004040 } else if (VT == MVT::f32)
4041 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4042 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004043 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004044 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4045 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4046
4047 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004048 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004049 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4050 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4051 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004052
Evan Chengd6b641e2011-02-23 02:24:55 +00004053 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4054 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4055 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004056 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004057 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4058 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004059 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004060 } else {
4061 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4062 }
4063
4064 return Res;
4065 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004066
4067 // Bitcast operand 1 to i32.
4068 if (SrcVT == MVT::f64)
4069 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004070 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004071 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4072
Evan Chengd6b641e2011-02-23 02:24:55 +00004073 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004074 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4075 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004076 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4077 if (VT == MVT::f32) {
4078 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4079 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4080 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4081 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004082 }
4083
Evan Chengd6b641e2011-02-23 02:24:55 +00004084 // f64: Or the high part with signbit and then combine two parts.
4085 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004086 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004087 SDValue Lo = Tmp0.getValue(0);
4088 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4089 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4090 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004091}
4092
Evan Cheng168ced92010-05-22 01:47:14 +00004093SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4094 MachineFunction &MF = DAG.getMachineFunction();
4095 MachineFrameInfo *MFI = MF.getFrameInfo();
4096 MFI->setReturnAddressIsTaken(true);
4097
Bill Wendling908bf812014-01-06 00:43:20 +00004098 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004099 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004100
Evan Cheng168ced92010-05-22 01:47:14 +00004101 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004102 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004103 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4104 if (Depth) {
4105 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004106 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004107 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4108 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004109 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004110 }
4111
4112 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004113 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004114 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4115}
4116
Dan Gohman21cea8a2010-04-17 15:26:15 +00004117SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004118 const ARMBaseRegisterInfo &ARI =
4119 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4120 MachineFunction &MF = DAG.getMachineFunction();
4121 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004122 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004123
Owen Anderson53aa7a92009-08-10 22:56:29 +00004124 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004125 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004126 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004127 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004128 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4129 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004130 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4131 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004132 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004133 return FrameAddr;
4134}
4135
Renato Golinc7aea402014-05-06 16:51:25 +00004136// FIXME? Maybe this could be a TableGen attribute on some registers and
4137// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004138unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4139 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004140 unsigned Reg = StringSwitch<unsigned>(RegName)
4141 .Case("sp", ARM::SP)
4142 .Default(0);
4143 if (Reg)
4144 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004145 report_fatal_error(Twine("Invalid register name \""
4146 + StringRef(RegName) + "\"."));
4147}
4148
4149// Result is 64 bit value so split into two 32 bit values and return as a
4150// pair of values.
4151static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4152 SelectionDAG &DAG) {
4153 SDLoc DL(N);
4154
4155 // This function is only supposed to be called for i64 type destination.
4156 assert(N->getValueType(0) == MVT::i64
4157 && "ExpandREAD_REGISTER called for non-i64 type result.");
4158
4159 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4160 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4161 N->getOperand(0),
4162 N->getOperand(1));
4163
4164 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4165 Read.getValue(1)));
4166 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004167}
4168
Wesley Peck527da1b2010-11-23 03:31:01 +00004169/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004170/// expand a bit convert where either the source or destination type is i64 to
4171/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4172/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4173/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004174static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004176 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004177 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004178
Bob Wilson59b70ea2010-04-17 05:30:19 +00004179 // This function is only supposed to be called for i64 types, either as the
4180 // source or destination of the bit convert.
4181 EVT SrcVT = Op.getValueType();
4182 EVT DstVT = N->getValueType(0);
4183 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004184 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004185
Bob Wilson59b70ea2010-04-17 05:30:19 +00004186 // Turn i64->f64 into VMOVDRR.
4187 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004188 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004189 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004190 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004191 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004192 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004193 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004194 }
Bob Wilson7117a912009-03-20 22:42:55 +00004195
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004196 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004197 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004198 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004199 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004200 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004201 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4202 DAG.getVTList(MVT::i32, MVT::i32),
4203 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4204 else
4205 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4206 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004207 // Merge the pieces into a single i64 value.
4208 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4209 }
Bob Wilson7117a912009-03-20 22:42:55 +00004210
Bob Wilson59b70ea2010-04-17 05:30:19 +00004211 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004212}
4213
Bob Wilson2e076c42009-06-22 23:27:02 +00004214/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004215/// Zero vectors are used to represent vector negation and in those cases
4216/// will be implemented with the NEON VNEG instruction. However, VNEG does
4217/// not support i64 elements, so sometimes the zero vectors will need to be
4218/// explicitly constructed. Regardless, use a canonical VMOV to create the
4219/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004220static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004221 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004222 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004223 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004224 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4225 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004226 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004227}
4228
Jim Grosbach624fcb22009-10-31 21:00:56 +00004229/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4230/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004231SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4232 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004233 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4234 EVT VT = Op.getValueType();
4235 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004237 SDValue ShOpLo = Op.getOperand(0);
4238 SDValue ShOpHi = Op.getOperand(1);
4239 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004240 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004241 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004242
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004243 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4244
Jim Grosbach624fcb22009-10-31 21:00:56 +00004245 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004246 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004247 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4248 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004249 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004250 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4251 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004252 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004253
4254 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004255 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4256 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004257 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004258 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004259 CCR, Cmp);
4260
4261 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004262 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004263}
4264
Jim Grosbach5d994042009-10-31 19:38:01 +00004265/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4266/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004267SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4268 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004269 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4270 EVT VT = Op.getValueType();
4271 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004272 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004273 SDValue ShOpLo = Op.getOperand(0);
4274 SDValue ShOpHi = Op.getOperand(1);
4275 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004276 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004277
4278 assert(Op.getOpcode() == ISD::SHL_PARTS);
4279 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004280 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004281 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4282 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004283 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004284 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4285 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4286
4287 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4288 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004289 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4290 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004291 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004292 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004293 CCR, Cmp);
4294
4295 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004296 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004297}
4298
Jim Grosbach535d3b42010-09-08 03:54:02 +00004299SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004300 SelectionDAG &DAG) const {
4301 // The rounding mode is in bits 23:22 of the FPSCR.
4302 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4303 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4304 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004305 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004306 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004307 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004308 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004309 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004310 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004311 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004312 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004313 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004314 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004315}
4316
Jim Grosbach8546ec92010-01-18 19:58:49 +00004317static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4318 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004319 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004320 EVT VT = N->getValueType(0);
4321 if (VT.isVector()) {
4322 assert(ST->hasNEON());
4323
4324 // Compute the least significant set bit: LSB = X & -X
4325 SDValue X = N->getOperand(0);
4326 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4327 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4328
4329 EVT ElemTy = VT.getVectorElementType();
4330
4331 if (ElemTy == MVT::i8) {
4332 // Compute with: cttz(x) = ctpop(lsb - 1)
4333 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4334 DAG.getTargetConstant(1, dl, ElemTy));
4335 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4336 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4337 }
4338
4339 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4340 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4341 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4342 unsigned NumBits = ElemTy.getSizeInBits();
4343 SDValue WidthMinus1 =
4344 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4345 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4346 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4347 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4348 }
4349
4350 // Compute with: cttz(x) = ctpop(lsb - 1)
4351
4352 // Since we can only compute the number of bits in a byte with vcnt.8, we
4353 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4354 // and i64.
4355
4356 // Compute LSB - 1.
4357 SDValue Bits;
4358 if (ElemTy == MVT::i64) {
4359 // Load constant 0xffff'ffff'ffff'ffff to register.
4360 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4361 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4362 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4363 } else {
4364 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4365 DAG.getTargetConstant(1, dl, ElemTy));
4366 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4367 }
4368
4369 // Count #bits with vcnt.8.
4370 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4371 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4372 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4373
4374 // Gather the #bits with vpaddl (pairwise add.)
4375 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4376 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4377 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4378 Cnt8);
4379 if (ElemTy == MVT::i16)
4380 return Cnt16;
4381
4382 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4383 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4384 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4385 Cnt16);
4386 if (ElemTy == MVT::i32)
4387 return Cnt32;
4388
4389 assert(ElemTy == MVT::i64);
4390 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4391 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4392 Cnt32);
4393 return Cnt64;
4394 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004395
4396 if (!ST->hasV6T2Ops())
4397 return SDValue();
4398
4399 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4400 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4401}
4402
Evan Chengb4eae132012-12-04 22:41:50 +00004403/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4404/// for each 16-bit element from operand, repeated. The basic idea is to
4405/// leverage vcnt to get the 8-bit counts, gather and add the results.
4406///
4407/// Trace for v4i16:
4408/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4409/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4410/// 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 +00004411/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004412/// [b0 b1 b2 b3 b4 b5 b6 b7]
4413/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4414/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4415/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4416static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4417 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004418 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004419
4420 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4421 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4422 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4423 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4424 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4425 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4426}
4427
4428/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4429/// bit-count for each 16-bit element from the operand. We need slightly
4430/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4431/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004432///
Evan Chengb4eae132012-12-04 22:41:50 +00004433/// Trace for v4i16:
4434/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4435/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4436/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4437/// v4i16:Extracted = [k0 k1 k2 k3 ]
4438static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4439 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004440 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004441
4442 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4443 if (VT.is64BitVector()) {
4444 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4445 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004446 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004447 } else {
4448 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004449 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004450 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4451 }
4452}
4453
4454/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4455/// bit-count for each 32-bit element from the operand. The idea here is
4456/// to split the vector into 16-bit elements, leverage the 16-bit count
4457/// routine, and then combine the results.
4458///
4459/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4460/// input = [v0 v1 ] (vi: 32-bit elements)
4461/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4462/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004463/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004464/// [k0 k1 k2 k3 ]
4465/// N1 =+[k1 k0 k3 k2 ]
4466/// [k0 k2 k1 k3 ]
4467/// N2 =+[k1 k3 k0 k2 ]
4468/// [k0 k2 k1 k3 ]
4469/// Extended =+[k1 k3 k0 k2 ]
4470/// [k0 k2 ]
4471/// Extracted=+[k1 k3 ]
4472///
4473static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4474 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004475 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004476
4477 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4478
4479 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4480 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4481 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4482 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4483 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4484
4485 if (VT.is64BitVector()) {
4486 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4487 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004488 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004489 } else {
4490 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004491 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004492 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4493 }
4494}
4495
4496static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4497 const ARMSubtarget *ST) {
4498 EVT VT = N->getValueType(0);
4499
4500 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004501 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4502 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004503 "Unexpected type for custom ctpop lowering");
4504
4505 if (VT.getVectorElementType() == MVT::i32)
4506 return lowerCTPOP32BitElements(N, DAG);
4507 else
4508 return lowerCTPOP16BitElements(N, DAG);
4509}
4510
Bob Wilson2e076c42009-06-22 23:27:02 +00004511static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4512 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004513 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004514 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004515
Bob Wilson7d471332010-11-18 21:16:28 +00004516 if (!VT.isVector())
4517 return SDValue();
4518
Bob Wilson2e076c42009-06-22 23:27:02 +00004519 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004520 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004521
Bob Wilson7d471332010-11-18 21:16:28 +00004522 // Left shifts translate directly to the vshiftu intrinsic.
4523 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004524 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004525 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4526 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004527 N->getOperand(0), N->getOperand(1));
4528
4529 assert((N->getOpcode() == ISD::SRA ||
4530 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4531
4532 // NEON uses the same intrinsics for both left and right shifts. For
4533 // right shifts, the shift amounts are negative, so negate the vector of
4534 // shift amounts.
4535 EVT ShiftVT = N->getOperand(1).getValueType();
4536 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4537 getZeroVector(ShiftVT, DAG, dl),
4538 N->getOperand(1));
4539 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4540 Intrinsic::arm_neon_vshifts :
4541 Intrinsic::arm_neon_vshiftu);
4542 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004543 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004544 N->getOperand(0), NegatedCount);
4545}
4546
4547static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4548 const ARMSubtarget *ST) {
4549 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004550 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004551
Eli Friedman682d8c12009-08-22 03:13:10 +00004552 // We can get here for a node like i32 = ISD::SHL i32, i64
4553 if (VT != MVT::i64)
4554 return SDValue();
4555
4556 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004557 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004558
Chris Lattnerf81d5882007-11-24 07:07:01 +00004559 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4560 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004561 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004562 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004563
Chris Lattnerf81d5882007-11-24 07:07:01 +00004564 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004565 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004566
Chris Lattnerf81d5882007-11-24 07:07:01 +00004567 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004568 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004569 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004570 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004571 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004572
Chris Lattnerf81d5882007-11-24 07:07:01 +00004573 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4574 // captures the result into a carry flag.
4575 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004576 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004577
Chris Lattnerf81d5882007-11-24 07:07:01 +00004578 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004579 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004580
Chris Lattnerf81d5882007-11-24 07:07:01 +00004581 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004582 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004583}
4584
Bob Wilson2e076c42009-06-22 23:27:02 +00004585static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4586 SDValue TmpOp0, TmpOp1;
4587 bool Invert = false;
4588 bool Swap = false;
4589 unsigned Opc = 0;
4590
4591 SDValue Op0 = Op.getOperand(0);
4592 SDValue Op1 = Op.getOperand(1);
4593 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004594 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004595 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004596 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004597 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004598
Oliver Stannard51b1d462014-08-21 12:50:31 +00004599 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004600 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004601 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004602 case ISD::SETUNE:
4603 case ISD::SETNE: Invert = true; // Fallthrough
4604 case ISD::SETOEQ:
4605 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4606 case ISD::SETOLT:
4607 case ISD::SETLT: Swap = true; // Fallthrough
4608 case ISD::SETOGT:
4609 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4610 case ISD::SETOLE:
4611 case ISD::SETLE: Swap = true; // Fallthrough
4612 case ISD::SETOGE:
4613 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4614 case ISD::SETUGE: Swap = true; // Fallthrough
4615 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4616 case ISD::SETUGT: Swap = true; // Fallthrough
4617 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4618 case ISD::SETUEQ: Invert = true; // Fallthrough
4619 case ISD::SETONE:
4620 // Expand this to (OLT | OGT).
4621 TmpOp0 = Op0;
4622 TmpOp1 = Op1;
4623 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004624 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4625 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004626 break;
4627 case ISD::SETUO: Invert = true; // Fallthrough
4628 case ISD::SETO:
4629 // Expand this to (OLT | OGE).
4630 TmpOp0 = Op0;
4631 TmpOp1 = Op1;
4632 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004633 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4634 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004635 break;
4636 }
4637 } else {
4638 // Integer comparisons.
4639 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004640 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004641 case ISD::SETNE: Invert = true;
4642 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4643 case ISD::SETLT: Swap = true;
4644 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4645 case ISD::SETLE: Swap = true;
4646 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4647 case ISD::SETULT: Swap = true;
4648 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4649 case ISD::SETULE: Swap = true;
4650 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4651 }
4652
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004653 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004654 if (Opc == ARMISD::VCEQ) {
4655
4656 SDValue AndOp;
4657 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4658 AndOp = Op0;
4659 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4660 AndOp = Op1;
4661
4662 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004663 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004664 AndOp = AndOp.getOperand(0);
4665
4666 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4667 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004668 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4669 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004670 Invert = !Invert;
4671 }
4672 }
4673 }
4674
4675 if (Swap)
4676 std::swap(Op0, Op1);
4677
Owen Andersonc7baee32010-11-08 23:21:22 +00004678 // If one of the operands is a constant vector zero, attempt to fold the
4679 // comparison to a specialized compare-against-zero form.
4680 SDValue SingleOp;
4681 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4682 SingleOp = Op0;
4683 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4684 if (Opc == ARMISD::VCGE)
4685 Opc = ARMISD::VCLEZ;
4686 else if (Opc == ARMISD::VCGT)
4687 Opc = ARMISD::VCLTZ;
4688 SingleOp = Op1;
4689 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004690
Owen Andersonc7baee32010-11-08 23:21:22 +00004691 SDValue Result;
4692 if (SingleOp.getNode()) {
4693 switch (Opc) {
4694 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004695 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004696 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004697 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004698 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004699 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004700 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004701 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004702 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004703 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004704 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004705 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004706 }
4707 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004708 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004709 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004710
Tim Northover45aa89c2015-02-08 00:50:47 +00004711 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4712
Bob Wilson2e076c42009-06-22 23:27:02 +00004713 if (Invert)
4714 Result = DAG.getNOT(dl, Result, VT);
4715
4716 return Result;
4717}
4718
Bob Wilson5b2b5042010-06-14 22:19:57 +00004719/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4720/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004721/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004722static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4723 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004724 SDLoc dl, EVT &VT, bool is128Bits,
4725 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004726 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004727
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004728 // SplatBitSize is set to the smallest size that splats the vector, so a
4729 // zero vector will always have SplatBitSize == 8. However, NEON modified
4730 // immediate instructions others than VMOV do not support the 8-bit encoding
4731 // of a zero vector, and the default encoding of zero is supposed to be the
4732 // 32-bit version.
4733 if (SplatBits == 0)
4734 SplatBitSize = 32;
4735
Bob Wilson2e076c42009-06-22 23:27:02 +00004736 switch (SplatBitSize) {
4737 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004738 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004739 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004740 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004741 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004742 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004743 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004744 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004745 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004746
4747 case 16:
4748 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004749 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004750 if ((SplatBits & ~0xff) == 0) {
4751 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004752 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004753 Imm = SplatBits;
4754 break;
4755 }
4756 if ((SplatBits & ~0xff00) == 0) {
4757 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004758 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004759 Imm = SplatBits >> 8;
4760 break;
4761 }
4762 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004763
4764 case 32:
4765 // NEON's 32-bit VMOV supports splat values where:
4766 // * only one byte is nonzero, or
4767 // * the least significant byte is 0xff and the second byte is nonzero, or
4768 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004769 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004770 if ((SplatBits & ~0xff) == 0) {
4771 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004772 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004773 Imm = SplatBits;
4774 break;
4775 }
4776 if ((SplatBits & ~0xff00) == 0) {
4777 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004778 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004779 Imm = SplatBits >> 8;
4780 break;
4781 }
4782 if ((SplatBits & ~0xff0000) == 0) {
4783 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004784 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004785 Imm = SplatBits >> 16;
4786 break;
4787 }
4788 if ((SplatBits & ~0xff000000) == 0) {
4789 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004790 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004791 Imm = SplatBits >> 24;
4792 break;
4793 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004794
Owen Andersona4076922010-11-05 21:57:54 +00004795 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4796 if (type == OtherModImm) return SDValue();
4797
Bob Wilson2e076c42009-06-22 23:27:02 +00004798 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004799 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4800 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004801 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004802 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004803 break;
4804 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004805
4806 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004807 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4808 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004809 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004810 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004811 break;
4812 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004813
4814 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4815 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4816 // VMOV.I32. A (very) minor optimization would be to replicate the value
4817 // and fall through here to test for a valid 64-bit splat. But, then the
4818 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004819 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004820
4821 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004822 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004823 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004824 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004825 uint64_t BitMask = 0xff;
4826 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004827 unsigned ImmMask = 1;
4828 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004829 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004830 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004831 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004832 Imm |= ImmMask;
4833 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004834 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004835 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004836 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004837 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004838 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004839
Mehdi Aminiffc14022015-07-08 01:00:38 +00004840 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004841 // swap higher and lower 32 bit word
4842 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4843
Bob Wilson6eae5202010-06-11 21:34:50 +00004844 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004845 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004846 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004847 break;
4848 }
4849
Bob Wilson6eae5202010-06-11 21:34:50 +00004850 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004851 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004852 }
4853
Bob Wilsona3f19012010-07-13 21:16:48 +00004854 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004855 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004856}
4857
Lang Hames591cdaf2012-03-29 21:56:11 +00004858SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4859 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004860 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004861 return SDValue();
4862
Tim Northoverf79c3a52013-08-20 08:57:11 +00004863 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004864 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004865
Oliver Stannard51b1d462014-08-21 12:50:31 +00004866 // Use the default (constant pool) lowering for double constants when we have
4867 // an SP-only FPU
4868 if (IsDouble && Subtarget->isFPOnlySP())
4869 return SDValue();
4870
Lang Hames591cdaf2012-03-29 21:56:11 +00004871 // Try splatting with a VMOV.f32...
4872 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004873 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4874
Lang Hames591cdaf2012-03-29 21:56:11 +00004875 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004876 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4877 // We have code in place to select a valid ConstantFP already, no need to
4878 // do any mangling.
4879 return Op;
4880 }
4881
4882 // It's a float and we are trying to use NEON operations where
4883 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004884 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004885 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004886 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4887 NewVal);
4888 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004889 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004890 }
4891
Tim Northoverf79c3a52013-08-20 08:57:11 +00004892 // The rest of our options are NEON only, make sure that's allowed before
4893 // proceeding..
4894 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4895 return SDValue();
4896
Lang Hames591cdaf2012-03-29 21:56:11 +00004897 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004898 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4899
4900 // It wouldn't really be worth bothering for doubles except for one very
4901 // important value, which does happen to match: 0.0. So make sure we don't do
4902 // anything stupid.
4903 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4904 return SDValue();
4905
4906 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004907 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4908 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004909 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004910 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004911 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4912 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004913 if (IsDouble)
4914 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4915
4916 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004917 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4918 VecConstant);
4919 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004920 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004921 }
4922
4923 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004924 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004925 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004926 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004927 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004928 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004929
4930 if (IsDouble)
4931 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4932
4933 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004934 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4935 VecConstant);
4936 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004937 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004938 }
4939
4940 return SDValue();
4941}
4942
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004943// check if an VEXT instruction can handle the shuffle mask when the
4944// vector sources of the shuffle are the same.
4945static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4946 unsigned NumElts = VT.getVectorNumElements();
4947
4948 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4949 if (M[0] < 0)
4950 return false;
4951
4952 Imm = M[0];
4953
4954 // If this is a VEXT shuffle, the immediate value is the index of the first
4955 // element. The other shuffle indices must be the successive elements after
4956 // the first one.
4957 unsigned ExpectedElt = Imm;
4958 for (unsigned i = 1; i < NumElts; ++i) {
4959 // Increment the expected index. If it wraps around, just follow it
4960 // back to index zero and keep going.
4961 ++ExpectedElt;
4962 if (ExpectedElt == NumElts)
4963 ExpectedElt = 0;
4964
4965 if (M[i] < 0) continue; // ignore UNDEF indices
4966 if (ExpectedElt != static_cast<unsigned>(M[i]))
4967 return false;
4968 }
4969
4970 return true;
4971}
4972
Lang Hames591cdaf2012-03-29 21:56:11 +00004973
Benjamin Kramer339ced42012-01-15 13:16:05 +00004974static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004975 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004976 unsigned NumElts = VT.getVectorNumElements();
4977 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004978
4979 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4980 if (M[0] < 0)
4981 return false;
4982
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004983 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004984
4985 // If this is a VEXT shuffle, the immediate value is the index of the first
4986 // element. The other shuffle indices must be the successive elements after
4987 // the first one.
4988 unsigned ExpectedElt = Imm;
4989 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004990 // Increment the expected index. If it wraps around, it may still be
4991 // a VEXT but the source vectors must be swapped.
4992 ExpectedElt += 1;
4993 if (ExpectedElt == NumElts * 2) {
4994 ExpectedElt = 0;
4995 ReverseVEXT = true;
4996 }
4997
Bob Wilson411dfad2010-08-17 05:54:34 +00004998 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004999 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005000 return false;
5001 }
5002
5003 // Adjust the index value if the source operands will be swapped.
5004 if (ReverseVEXT)
5005 Imm -= NumElts;
5006
Bob Wilson32cd8552009-08-19 17:03:43 +00005007 return true;
5008}
5009
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005010/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5011/// instruction with the specified blocksize. (The order of the elements
5012/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005013static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005014 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5015 "Only possible block sizes for VREV are: 16, 32, 64");
5016
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005017 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005018 if (EltSz == 64)
5019 return false;
5020
5021 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005022 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005023 // If the first shuffle index is UNDEF, be optimistic.
5024 if (M[0] < 0)
5025 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005026
5027 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5028 return false;
5029
5030 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005031 if (M[i] < 0) continue; // ignore UNDEF indices
5032 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005033 return false;
5034 }
5035
5036 return true;
5037}
5038
Benjamin Kramer339ced42012-01-15 13:16:05 +00005039static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005040 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5041 // range, then 0 is placed into the resulting vector. So pretty much any mask
5042 // of 8 elements can work here.
5043 return VT == MVT::v8i8 && M.size() == 8;
5044}
5045
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005046// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5047// checking that pairs of elements in the shuffle mask represent the same index
5048// in each vector, incrementing the expected index by 2 at each step.
5049// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5050// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5051// v2={e,f,g,h}
5052// WhichResult gives the offset for each element in the mask based on which
5053// of the two results it belongs to.
5054//
5055// The transpose can be represented either as:
5056// result1 = shufflevector v1, v2, result1_shuffle_mask
5057// result2 = shufflevector v1, v2, result2_shuffle_mask
5058// where v1/v2 and the shuffle masks have the same number of elements
5059// (here WhichResult (see below) indicates which result is being checked)
5060//
5061// or as:
5062// results = shufflevector v1, v2, shuffle_mask
5063// where both results are returned in one vector and the shuffle mask has twice
5064// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5065// want to check the low half and high half of the shuffle mask as if it were
5066// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005067static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005068 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5069 if (EltSz == 64)
5070 return false;
5071
Bob Wilsona7062312009-08-21 20:54:19 +00005072 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005073 if (M.size() != NumElts && M.size() != NumElts*2)
5074 return false;
5075
5076 // If the mask is twice as long as the result then we need to check the upper
5077 // and lower parts of the mask
5078 for (unsigned i = 0; i < M.size(); i += NumElts) {
5079 WhichResult = M[i] == 0 ? 0 : 1;
5080 for (unsigned j = 0; j < NumElts; j += 2) {
5081 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5082 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5083 return false;
5084 }
Bob Wilsona7062312009-08-21 20:54:19 +00005085 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005086
5087 if (M.size() == NumElts*2)
5088 WhichResult = 0;
5089
Bob Wilsona7062312009-08-21 20:54:19 +00005090 return true;
5091}
5092
Bob Wilson0bbd3072009-12-03 06:40:55 +00005093/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5094/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5095/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005096static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005097 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5098 if (EltSz == 64)
5099 return false;
5100
5101 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005102 if (M.size() != NumElts && M.size() != NumElts*2)
5103 return false;
5104
5105 for (unsigned i = 0; i < M.size(); i += NumElts) {
5106 WhichResult = M[i] == 0 ? 0 : 1;
5107 for (unsigned j = 0; j < NumElts; j += 2) {
5108 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5109 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5110 return false;
5111 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005112 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005113
5114 if (M.size() == NumElts*2)
5115 WhichResult = 0;
5116
Bob Wilson0bbd3072009-12-03 06:40:55 +00005117 return true;
5118}
5119
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005120// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5121// that the mask elements are either all even and in steps of size 2 or all odd
5122// and in steps of size 2.
5123// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5124// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5125// v2={e,f,g,h}
5126// Requires similar checks to that of isVTRNMask with
5127// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005128static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005129 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5130 if (EltSz == 64)
5131 return false;
5132
Bob Wilsona7062312009-08-21 20:54:19 +00005133 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005134 if (M.size() != NumElts && M.size() != NumElts*2)
5135 return false;
5136
5137 for (unsigned i = 0; i < M.size(); i += NumElts) {
5138 WhichResult = M[i] == 0 ? 0 : 1;
5139 for (unsigned j = 0; j < NumElts; ++j) {
5140 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5141 return false;
5142 }
Bob Wilsona7062312009-08-21 20:54:19 +00005143 }
5144
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005145 if (M.size() == NumElts*2)
5146 WhichResult = 0;
5147
Bob Wilsona7062312009-08-21 20:54:19 +00005148 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005149 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005150 return false;
5151
5152 return true;
5153}
5154
Bob Wilson0bbd3072009-12-03 06:40:55 +00005155/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5156/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5157/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005158static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005159 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5160 if (EltSz == 64)
5161 return false;
5162
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005163 unsigned NumElts = VT.getVectorNumElements();
5164 if (M.size() != NumElts && M.size() != NumElts*2)
5165 return false;
5166
5167 unsigned Half = NumElts / 2;
5168 for (unsigned i = 0; i < M.size(); i += NumElts) {
5169 WhichResult = M[i] == 0 ? 0 : 1;
5170 for (unsigned j = 0; j < NumElts; j += Half) {
5171 unsigned Idx = WhichResult;
5172 for (unsigned k = 0; k < Half; ++k) {
5173 int MIdx = M[i + j + k];
5174 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5175 return false;
5176 Idx += 2;
5177 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005178 }
5179 }
5180
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005181 if (M.size() == NumElts*2)
5182 WhichResult = 0;
5183
Bob Wilson0bbd3072009-12-03 06:40:55 +00005184 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5185 if (VT.is64BitVector() && EltSz == 32)
5186 return false;
5187
5188 return true;
5189}
5190
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005191// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5192// that pairs of elements of the shufflemask represent the same index in each
5193// vector incrementing sequentially through the vectors.
5194// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5195// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5196// v2={e,f,g,h}
5197// Requires similar checks to that of isVTRNMask with respect the how results
5198// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005199static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005200 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5201 if (EltSz == 64)
5202 return false;
5203
Bob Wilsona7062312009-08-21 20:54:19 +00005204 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005205 if (M.size() != NumElts && M.size() != NumElts*2)
5206 return false;
5207
5208 for (unsigned i = 0; i < M.size(); i += NumElts) {
5209 WhichResult = M[i] == 0 ? 0 : 1;
5210 unsigned Idx = WhichResult * NumElts / 2;
5211 for (unsigned j = 0; j < NumElts; j += 2) {
5212 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5213 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5214 return false;
5215 Idx += 1;
5216 }
Bob Wilsona7062312009-08-21 20:54:19 +00005217 }
5218
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005219 if (M.size() == NumElts*2)
5220 WhichResult = 0;
5221
Bob Wilsona7062312009-08-21 20:54:19 +00005222 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005223 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005224 return false;
5225
5226 return true;
5227}
5228
Bob Wilson0bbd3072009-12-03 06:40:55 +00005229/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5230/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5231/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005232static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005233 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5234 if (EltSz == 64)
5235 return false;
5236
5237 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005238 if (M.size() != NumElts && M.size() != NumElts*2)
5239 return false;
5240
5241 for (unsigned i = 0; i < M.size(); i += NumElts) {
5242 WhichResult = M[i] == 0 ? 0 : 1;
5243 unsigned Idx = WhichResult * NumElts / 2;
5244 for (unsigned j = 0; j < NumElts; j += 2) {
5245 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5246 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5247 return false;
5248 Idx += 1;
5249 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005250 }
5251
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005252 if (M.size() == NumElts*2)
5253 WhichResult = 0;
5254
Bob Wilson0bbd3072009-12-03 06:40:55 +00005255 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5256 if (VT.is64BitVector() && EltSz == 32)
5257 return false;
5258
5259 return true;
5260}
5261
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005262/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5263/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5264static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5265 unsigned &WhichResult,
5266 bool &isV_UNDEF) {
5267 isV_UNDEF = false;
5268 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5269 return ARMISD::VTRN;
5270 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5271 return ARMISD::VUZP;
5272 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5273 return ARMISD::VZIP;
5274
5275 isV_UNDEF = true;
5276 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5277 return ARMISD::VTRN;
5278 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5279 return ARMISD::VUZP;
5280 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5281 return ARMISD::VZIP;
5282
5283 return 0;
5284}
5285
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005286/// \return true if this is a reverse operation on an vector.
5287static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5288 unsigned NumElts = VT.getVectorNumElements();
5289 // Make sure the mask has the right size.
5290 if (NumElts != M.size())
5291 return false;
5292
5293 // Look for <15, ..., 3, -1, 1, 0>.
5294 for (unsigned i = 0; i != NumElts; ++i)
5295 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5296 return false;
5297
5298 return true;
5299}
5300
Dale Johannesen2bff5052010-07-29 20:10:08 +00005301// If N is an integer constant that can be moved into a register in one
5302// instruction, return an SDValue of such a constant (will become a MOV
5303// instruction). Otherwise return null.
5304static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005305 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005306 uint64_t Val;
5307 if (!isa<ConstantSDNode>(N))
5308 return SDValue();
5309 Val = cast<ConstantSDNode>(N)->getZExtValue();
5310
5311 if (ST->isThumb1Only()) {
5312 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005313 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005314 } else {
5315 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005316 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005317 }
5318 return SDValue();
5319}
5320
Bob Wilson2e076c42009-06-22 23:27:02 +00005321// If this is a case we can't handle, return null and let the default
5322// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005323SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5324 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005325 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005326 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005327 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005328
5329 APInt SplatBits, SplatUndef;
5330 unsigned SplatBitSize;
5331 bool HasAnyUndefs;
5332 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005333 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005334 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005335 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005336 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005337 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005338 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005339 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005340 if (Val.getNode()) {
5341 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005342 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005343 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005344
5345 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005346 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005347 Val = isNEONModifiedImm(NegatedImm,
5348 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005349 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005350 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005351 if (Val.getNode()) {
5352 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005353 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005354 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005355
5356 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005357 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005358 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005359 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005360 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005361 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5362 }
5363 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005364 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005365 }
5366
Bob Wilson91fdf682010-05-22 00:23:12 +00005367 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005368 //
5369 // As an optimisation, even if more than one value is used it may be more
5370 // profitable to splat with one value then change some lanes.
5371 //
5372 // Heuristically we decide to do this if the vector has a "dominant" value,
5373 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005374 unsigned NumElts = VT.getVectorNumElements();
5375 bool isOnlyLowElement = true;
5376 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005377 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005378 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005379
5380 // Map of the number of times a particular SDValue appears in the
5381 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005382 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005383 SDValue Value;
5384 for (unsigned i = 0; i < NumElts; ++i) {
5385 SDValue V = Op.getOperand(i);
5386 if (V.getOpcode() == ISD::UNDEF)
5387 continue;
5388 if (i > 0)
5389 isOnlyLowElement = false;
5390 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5391 isConstant = false;
5392
James Molloy49bdbce2012-09-06 09:55:02 +00005393 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005394 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005395
James Molloy49bdbce2012-09-06 09:55:02 +00005396 // Is this value dominant? (takes up more than half of the lanes)
5397 if (++Count > (NumElts / 2)) {
5398 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005399 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005400 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005401 }
James Molloy49bdbce2012-09-06 09:55:02 +00005402 if (ValueCounts.size() != 1)
5403 usesOnlyOneValue = false;
5404 if (!Value.getNode() && ValueCounts.size() > 0)
5405 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005406
James Molloy49bdbce2012-09-06 09:55:02 +00005407 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005408 return DAG.getUNDEF(VT);
5409
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005410 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5411 // Keep going if we are hitting this case.
5412 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005413 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5414
Dale Johannesen2bff5052010-07-29 20:10:08 +00005415 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5416
Dale Johannesen710a2d92010-10-19 20:00:17 +00005417 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5418 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005419 if (hasDominantValue && EltSize <= 32) {
5420 if (!isConstant) {
5421 SDValue N;
5422
5423 // If we are VDUPing a value that comes directly from a vector, that will
5424 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005425 // just use VDUPLANE. We can only do this if the lane being extracted
5426 // is at a constant index, as the VDUP from lane instructions only have
5427 // constant-index forms.
5428 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5429 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005430 // We need to create a new undef vector to use for the VDUPLANE if the
5431 // size of the vector from which we get the value is different than the
5432 // size of the vector that we need to create. We will insert the element
5433 // such that the register coalescer will remove unnecessary copies.
5434 if (VT != Value->getOperand(0).getValueType()) {
5435 ConstantSDNode *constIndex;
5436 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5437 assert(constIndex && "The index is not a constant!");
5438 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5439 VT.getVectorNumElements();
5440 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5441 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005442 Value, DAG.getConstant(index, dl, MVT::i32)),
5443 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005444 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005445 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005446 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005447 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005448 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5449
5450 if (!usesOnlyOneValue) {
5451 // The dominant value was splatted as 'N', but we now have to insert
5452 // all differing elements.
5453 for (unsigned I = 0; I < NumElts; ++I) {
5454 if (Op.getOperand(I) == Value)
5455 continue;
5456 SmallVector<SDValue, 3> Ops;
5457 Ops.push_back(N);
5458 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005459 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005460 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005461 }
5462 }
5463 return N;
5464 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005465 if (VT.getVectorElementType().isFloatingPoint()) {
5466 SmallVector<SDValue, 8> Ops;
5467 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005468 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005469 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005470 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005471 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005472 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5473 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005474 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005475 }
James Molloy49bdbce2012-09-06 09:55:02 +00005476 if (usesOnlyOneValue) {
5477 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5478 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005479 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005480 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005481 }
5482
5483 // If all elements are constants and the case above didn't get hit, fall back
5484 // to the default expansion, which will generate a load from the constant
5485 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005486 if (isConstant)
5487 return SDValue();
5488
Bob Wilson6f2b8962011-01-07 21:37:30 +00005489 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5490 if (NumElts >= 4) {
5491 SDValue shuffle = ReconstructShuffle(Op, DAG);
5492 if (shuffle != SDValue())
5493 return shuffle;
5494 }
5495
Bob Wilson91fdf682010-05-22 00:23:12 +00005496 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005497 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5498 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005499 if (EltSize >= 32) {
5500 // Do the expansion with floating-point types, since that is what the VFP
5501 // registers are defined to use, and since i64 is not legal.
5502 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5503 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005504 SmallVector<SDValue, 8> Ops;
5505 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005506 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005507 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005508 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005509 }
5510
Jim Grosbach24e102a2013-07-08 18:18:52 +00005511 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5512 // know the default expansion would otherwise fall back on something even
5513 // worse. For a vector with one or two non-undef values, that's
5514 // scalar_to_vector for the elements followed by a shuffle (provided the
5515 // shuffle is valid for the target) and materialization element by element
5516 // on the stack followed by a load for everything else.
5517 if (!isConstant && !usesOnlyOneValue) {
5518 SDValue Vec = DAG.getUNDEF(VT);
5519 for (unsigned i = 0 ; i < NumElts; ++i) {
5520 SDValue V = Op.getOperand(i);
5521 if (V.getOpcode() == ISD::UNDEF)
5522 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005523 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005524 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5525 }
5526 return Vec;
5527 }
5528
Bob Wilson2e076c42009-06-22 23:27:02 +00005529 return SDValue();
5530}
5531
Bob Wilson6f2b8962011-01-07 21:37:30 +00005532// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005533// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005534SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5535 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005536 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005537 EVT VT = Op.getValueType();
5538 unsigned NumElts = VT.getVectorNumElements();
5539
5540 SmallVector<SDValue, 2> SourceVecs;
5541 SmallVector<unsigned, 2> MinElts;
5542 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005543
Bob Wilson6f2b8962011-01-07 21:37:30 +00005544 for (unsigned i = 0; i < NumElts; ++i) {
5545 SDValue V = Op.getOperand(i);
5546 if (V.getOpcode() == ISD::UNDEF)
5547 continue;
5548 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5549 // A shuffle can only come from building a vector from various
5550 // elements of other vectors.
5551 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005552 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5553 VT.getVectorElementType()) {
5554 // This code doesn't know how to handle shuffles where the vector
5555 // element types do not match (this happens because type legalization
5556 // promotes the return type of EXTRACT_VECTOR_ELT).
5557 // FIXME: It might be appropriate to extend this code to handle
5558 // mismatched types.
5559 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005560 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005561
Bob Wilson6f2b8962011-01-07 21:37:30 +00005562 // Record this extraction against the appropriate vector if possible...
5563 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005564 // If the element number isn't a constant, we can't effectively
5565 // analyze what's going on.
5566 if (!isa<ConstantSDNode>(V.getOperand(1)))
5567 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005568 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5569 bool FoundSource = false;
5570 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5571 if (SourceVecs[j] == SourceVec) {
5572 if (MinElts[j] > EltNo)
5573 MinElts[j] = EltNo;
5574 if (MaxElts[j] < EltNo)
5575 MaxElts[j] = EltNo;
5576 FoundSource = true;
5577 break;
5578 }
5579 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005580
Bob Wilson6f2b8962011-01-07 21:37:30 +00005581 // Or record a new source if not...
5582 if (!FoundSource) {
5583 SourceVecs.push_back(SourceVec);
5584 MinElts.push_back(EltNo);
5585 MaxElts.push_back(EltNo);
5586 }
5587 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005588
Bob Wilson6f2b8962011-01-07 21:37:30 +00005589 // Currently only do something sane when at most two source vectors
5590 // involved.
5591 if (SourceVecs.size() > 2)
5592 return SDValue();
5593
5594 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5595 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005596
Bob Wilson6f2b8962011-01-07 21:37:30 +00005597 // This loop extracts the usage patterns of the source vectors
5598 // and prepares appropriate SDValues for a shuffle if possible.
5599 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5600 if (SourceVecs[i].getValueType() == VT) {
5601 // No VEXT necessary
5602 ShuffleSrcs[i] = SourceVecs[i];
5603 VEXTOffsets[i] = 0;
5604 continue;
5605 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5606 // It probably isn't worth padding out a smaller vector just to
5607 // break it down again in a shuffle.
5608 return SDValue();
5609 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005610
Bob Wilson6f2b8962011-01-07 21:37:30 +00005611 // Since only 64-bit and 128-bit vectors are legal on ARM and
5612 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005613 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5614 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005615
Bob Wilson6f2b8962011-01-07 21:37:30 +00005616 if (MaxElts[i] - MinElts[i] >= NumElts) {
5617 // Span too large for a VEXT to cope
5618 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005619 }
5620
Bob Wilson6f2b8962011-01-07 21:37:30 +00005621 if (MinElts[i] >= NumElts) {
5622 // The extraction can just take the second half
5623 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005624 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5625 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005626 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005627 } else if (MaxElts[i] < NumElts) {
5628 // The extraction can just take the first half
5629 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005630 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5631 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005632 DAG.getIntPtrConstant(0, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005633 } else {
5634 // An actual VEXT is needed
5635 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005636 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5637 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005638 DAG.getIntPtrConstant(0, dl));
Eric Christopher2af95512011-01-14 23:50:53 +00005639 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5640 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005641 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005642 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005643 DAG.getConstant(VEXTOffsets[i], dl,
5644 MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005645 }
5646 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005647
Bob Wilson6f2b8962011-01-07 21:37:30 +00005648 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005649
Bob Wilson6f2b8962011-01-07 21:37:30 +00005650 for (unsigned i = 0; i < NumElts; ++i) {
5651 SDValue Entry = Op.getOperand(i);
5652 if (Entry.getOpcode() == ISD::UNDEF) {
5653 Mask.push_back(-1);
5654 continue;
5655 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005656
Bob Wilson6f2b8962011-01-07 21:37:30 +00005657 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005658 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5659 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005660 if (ExtractVec == SourceVecs[0]) {
5661 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5662 } else {
5663 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5664 }
5665 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005666
Bob Wilson6f2b8962011-01-07 21:37:30 +00005667 // Final check before we try to produce nonsense...
5668 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005669 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5670 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005671
Bob Wilson6f2b8962011-01-07 21:37:30 +00005672 return SDValue();
5673}
5674
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005675/// isShuffleMaskLegal - Targets can use this to indicate that they only
5676/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5677/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5678/// are assumed to be legal.
5679bool
5680ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5681 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005682 if (VT.getVectorNumElements() == 4 &&
5683 (VT.is128BitVector() || VT.is64BitVector())) {
5684 unsigned PFIndexes[4];
5685 for (unsigned i = 0; i != 4; ++i) {
5686 if (M[i] < 0)
5687 PFIndexes[i] = 8;
5688 else
5689 PFIndexes[i] = M[i];
5690 }
5691
5692 // Compute the index in the perfect shuffle table.
5693 unsigned PFTableIndex =
5694 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5695 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5696 unsigned Cost = (PFEntry >> 30);
5697
5698 if (Cost <= 4)
5699 return true;
5700 }
5701
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005702 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005703 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005704
Bob Wilson846bd792010-06-07 23:53:38 +00005705 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5706 return (EltSize >= 32 ||
5707 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005708 isVREVMask(M, VT, 64) ||
5709 isVREVMask(M, VT, 32) ||
5710 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005711 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005712 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005713 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005714 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005715}
5716
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005717/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5718/// the specified operations to build the shuffle.
5719static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5720 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005721 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005722 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5723 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5724 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5725
5726 enum {
5727 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5728 OP_VREV,
5729 OP_VDUP0,
5730 OP_VDUP1,
5731 OP_VDUP2,
5732 OP_VDUP3,
5733 OP_VEXT1,
5734 OP_VEXT2,
5735 OP_VEXT3,
5736 OP_VUZPL, // VUZP, left result
5737 OP_VUZPR, // VUZP, right result
5738 OP_VZIPL, // VZIP, left result
5739 OP_VZIPR, // VZIP, right result
5740 OP_VTRNL, // VTRN, left result
5741 OP_VTRNR // VTRN, right result
5742 };
5743
5744 if (OpNum == OP_COPY) {
5745 if (LHSID == (1*9+2)*9+3) return LHS;
5746 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5747 return RHS;
5748 }
5749
5750 SDValue OpLHS, OpRHS;
5751 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5752 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5753 EVT VT = OpLHS.getValueType();
5754
5755 switch (OpNum) {
5756 default: llvm_unreachable("Unknown shuffle opcode!");
5757 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005758 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005759 if (VT.getVectorElementType() == MVT::i32 ||
5760 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005761 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5762 // vrev <4 x i16> -> VREV32
5763 if (VT.getVectorElementType() == MVT::i16)
5764 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5765 // vrev <4 x i8> -> VREV16
5766 assert(VT.getVectorElementType() == MVT::i8);
5767 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005768 case OP_VDUP0:
5769 case OP_VDUP1:
5770 case OP_VDUP2:
5771 case OP_VDUP3:
5772 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005773 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005774 case OP_VEXT1:
5775 case OP_VEXT2:
5776 case OP_VEXT3:
5777 return DAG.getNode(ARMISD::VEXT, dl, VT,
5778 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005779 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005780 case OP_VUZPL:
5781 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005782 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005783 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5784 case OP_VZIPL:
5785 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005786 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005787 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5788 case OP_VTRNL:
5789 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005790 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5791 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005792 }
5793}
5794
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005795static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005796 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005797 SelectionDAG &DAG) {
5798 // Check to see if we can use the VTBL instruction.
5799 SDValue V1 = Op.getOperand(0);
5800 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005801 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005802
5803 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005804 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005805 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005806 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005807
5808 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5809 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005810 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005811
Owen Anderson77aa2662011-04-05 21:48:57 +00005812 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005813 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005814}
5815
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005816static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5817 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005818 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005819 SDValue OpLHS = Op.getOperand(0);
5820 EVT VT = OpLHS.getValueType();
5821
5822 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5823 "Expect an v8i16/v16i8 type");
5824 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5825 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5826 // extract the first 8 bytes into the top double word and the last 8 bytes
5827 // into the bottom double word. The v8i16 case is similar.
5828 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5829 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005830 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005831}
5832
Bob Wilson2e076c42009-06-22 23:27:02 +00005833static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005834 SDValue V1 = Op.getOperand(0);
5835 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005836 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005837 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005838 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005839
Bob Wilsonc6800b52009-08-13 02:13:04 +00005840 // Convert shuffles that are directly supported on NEON to target-specific
5841 // DAG nodes, instead of keeping them as shuffles and matching them again
5842 // during code selection. This is more efficient and avoids the possibility
5843 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005844 // FIXME: floating-point vectors should be canonicalized to integer vectors
5845 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005846 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005847
Bob Wilson846bd792010-06-07 23:53:38 +00005848 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5849 if (EltSize <= 32) {
5850 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5851 int Lane = SVN->getSplatIndex();
5852 // If this is undef splat, generate it via "just" vdup, if possible.
5853 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005854
Dan Gohman198b7ff2011-11-03 21:49:52 +00005855 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005856 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5857 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5858 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005859 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5860 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5861 // reaches it).
5862 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5863 !isa<ConstantSDNode>(V1.getOperand(0))) {
5864 bool IsScalarToVector = true;
5865 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5866 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5867 IsScalarToVector = false;
5868 break;
5869 }
5870 if (IsScalarToVector)
5871 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5872 }
Bob Wilson846bd792010-06-07 23:53:38 +00005873 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005874 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005875 }
Bob Wilson846bd792010-06-07 23:53:38 +00005876
5877 bool ReverseVEXT;
5878 unsigned Imm;
5879 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5880 if (ReverseVEXT)
5881 std::swap(V1, V2);
5882 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005883 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005884 }
5885
5886 if (isVREVMask(ShuffleMask, VT, 64))
5887 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5888 if (isVREVMask(ShuffleMask, VT, 32))
5889 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5890 if (isVREVMask(ShuffleMask, VT, 16))
5891 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5892
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005893 if (V2->getOpcode() == ISD::UNDEF &&
5894 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5895 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005896 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005897 }
5898
Bob Wilson846bd792010-06-07 23:53:38 +00005899 // Check for Neon shuffles that modify both input vectors in place.
5900 // If both results are used, i.e., if there are two shuffles with the same
5901 // source operands and with masks corresponding to both results of one of
5902 // these operations, DAG memoization will ensure that a single node is
5903 // used for both shuffles.
5904 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005905 bool isV_UNDEF;
5906 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5907 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5908 if (isV_UNDEF)
5909 V2 = V1;
5910 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5911 .getValue(WhichResult);
5912 }
Bob Wilson846bd792010-06-07 23:53:38 +00005913
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005914 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5915 // shuffles that produce a result larger than their operands with:
5916 // shuffle(concat(v1, undef), concat(v2, undef))
5917 // ->
5918 // shuffle(concat(v1, v2), undef)
5919 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5920 //
5921 // This is useful in the general case, but there are special cases where
5922 // native shuffles produce larger results: the two-result ops.
5923 //
5924 // Look through the concat when lowering them:
5925 // shuffle(concat(v1, v2), undef)
5926 // ->
5927 // concat(VZIP(v1, v2):0, :1)
5928 //
5929 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5930 V2->getOpcode() == ISD::UNDEF) {
5931 SDValue SubV1 = V1->getOperand(0);
5932 SDValue SubV2 = V1->getOperand(1);
5933 EVT SubVT = SubV1.getValueType();
5934
5935 // We expect these to have been canonicalized to -1.
5936 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5937 return i < (int)VT.getVectorNumElements();
5938 }) && "Unexpected shuffle index into UNDEF operand!");
5939
5940 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5941 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5942 if (isV_UNDEF)
5943 SubV2 = SubV1;
5944 assert((WhichResult == 0) &&
5945 "In-place shuffle of concat can only have one result!");
5946 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
5947 SubV1, SubV2);
5948 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
5949 Res.getValue(1));
5950 }
5951 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00005952 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005953
Bob Wilsona7062312009-08-21 20:54:19 +00005954 // If the shuffle is not directly supported and it has 4 elements, use
5955 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005956 unsigned NumElts = VT.getVectorNumElements();
5957 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005958 unsigned PFIndexes[4];
5959 for (unsigned i = 0; i != 4; ++i) {
5960 if (ShuffleMask[i] < 0)
5961 PFIndexes[i] = 8;
5962 else
5963 PFIndexes[i] = ShuffleMask[i];
5964 }
5965
5966 // Compute the index in the perfect shuffle table.
5967 unsigned PFTableIndex =
5968 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005969 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5970 unsigned Cost = (PFEntry >> 30);
5971
5972 if (Cost <= 4)
5973 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5974 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005975
Bob Wilsond8a9a042010-06-04 00:04:02 +00005976 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005977 if (EltSize >= 32) {
5978 // Do the expansion with floating-point types, since that is what the VFP
5979 // registers are defined to use, and since i64 is not legal.
5980 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5981 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005982 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5983 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005984 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005985 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005986 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005987 Ops.push_back(DAG.getUNDEF(EltVT));
5988 else
5989 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5990 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5991 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005992 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005993 }
Craig Topper48d114b2014-04-26 18:35:24 +00005994 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005995 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005996 }
5997
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005998 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5999 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6000
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006001 if (VT == MVT::v8i8) {
6002 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
6003 if (NewOp.getNode())
6004 return NewOp;
6005 }
6006
Bob Wilson6f34e272009-08-14 05:16:33 +00006007 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006008}
6009
Eli Friedmana5e244c2011-10-24 23:08:52 +00006010static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6011 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6012 SDValue Lane = Op.getOperand(2);
6013 if (!isa<ConstantSDNode>(Lane))
6014 return SDValue();
6015
6016 return Op;
6017}
6018
Bob Wilson2e076c42009-06-22 23:27:02 +00006019static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006020 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006021 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006022 if (!isa<ConstantSDNode>(Lane))
6023 return SDValue();
6024
6025 SDValue Vec = Op.getOperand(0);
6026 if (Op.getValueType() == MVT::i32 &&
6027 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006028 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006029 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6030 }
6031
6032 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006033}
6034
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006035static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6036 // The only time a CONCAT_VECTORS operation can have legal types is when
6037 // two 64-bit vectors are concatenated to a 128-bit vector.
6038 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6039 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006040 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006041 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006042 SDValue Op0 = Op.getOperand(0);
6043 SDValue Op1 = Op.getOperand(1);
6044 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006045 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006046 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006047 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006048 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006049 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006050 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006051 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006052 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006053}
6054
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006055/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6056/// element has been zero/sign-extended, depending on the isSigned parameter,
6057/// from an integer type half its size.
6058static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6059 bool isSigned) {
6060 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6061 EVT VT = N->getValueType(0);
6062 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6063 SDNode *BVN = N->getOperand(0).getNode();
6064 if (BVN->getValueType(0) != MVT::v4i32 ||
6065 BVN->getOpcode() != ISD::BUILD_VECTOR)
6066 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006067 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006068 unsigned HiElt = 1 - LoElt;
6069 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6070 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6071 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6072 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6073 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6074 return false;
6075 if (isSigned) {
6076 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6077 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6078 return true;
6079 } else {
6080 if (Hi0->isNullValue() && Hi1->isNullValue())
6081 return true;
6082 }
6083 return false;
6084 }
6085
6086 if (N->getOpcode() != ISD::BUILD_VECTOR)
6087 return false;
6088
6089 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6090 SDNode *Elt = N->getOperand(i).getNode();
6091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6092 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6093 unsigned HalfSize = EltSize / 2;
6094 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006095 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006096 return false;
6097 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006098 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006099 return false;
6100 }
6101 continue;
6102 }
6103 return false;
6104 }
6105
6106 return true;
6107}
6108
6109/// isSignExtended - Check if a node is a vector value that is sign-extended
6110/// or a constant BUILD_VECTOR with sign-extended elements.
6111static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6112 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6113 return true;
6114 if (isExtendedBUILD_VECTOR(N, DAG, true))
6115 return true;
6116 return false;
6117}
6118
6119/// isZeroExtended - Check if a node is a vector value that is zero-extended
6120/// or a constant BUILD_VECTOR with zero-extended elements.
6121static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6122 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6123 return true;
6124 if (isExtendedBUILD_VECTOR(N, DAG, false))
6125 return true;
6126 return false;
6127}
6128
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006129static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6130 if (OrigVT.getSizeInBits() >= 64)
6131 return OrigVT;
6132
6133 assert(OrigVT.isSimple() && "Expecting a simple value type");
6134
6135 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6136 switch (OrigSimpleTy) {
6137 default: llvm_unreachable("Unexpected Vector Type");
6138 case MVT::v2i8:
6139 case MVT::v2i16:
6140 return MVT::v2i32;
6141 case MVT::v4i8:
6142 return MVT::v4i16;
6143 }
6144}
6145
Sebastian Popa204f722012-11-30 19:08:04 +00006146/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6147/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6148/// We insert the required extension here to get the vector to fill a D register.
6149static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6150 const EVT &OrigTy,
6151 const EVT &ExtTy,
6152 unsigned ExtOpcode) {
6153 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6154 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6155 // 64-bits we need to insert a new extension so that it will be 64-bits.
6156 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6157 if (OrigTy.getSizeInBits() >= 64)
6158 return N;
6159
6160 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006161 EVT NewVT = getExtensionTo64Bits(OrigTy);
6162
Andrew Trickef9de2a2013-05-25 02:42:55 +00006163 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006164}
6165
6166/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6167/// does not do any sign/zero extension. If the original vector is less
6168/// than 64 bits, an appropriate extension will be added after the load to
6169/// reach a total size of 64 bits. We have to add the extension separately
6170/// because ARM does not have a sign/zero extending load for vectors.
6171static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006172 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6173
6174 // The load already has the right type.
6175 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006176 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006177 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6178 LD->isNonTemporal(), LD->isInvariant(),
6179 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006180
6181 // We need to create a zextload/sextload. We cannot just create a load
6182 // followed by a zext/zext node because LowerMUL is also run during normal
6183 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006184 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006185 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006186 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006187 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006188}
6189
6190/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6191/// extending load, or BUILD_VECTOR with extended elements, return the
6192/// unextended value. The unextended vector should be 64 bits so that it can
6193/// be used as an operand to a VMULL instruction. If the original vector size
6194/// before extension is less than 64 bits we add a an extension to resize
6195/// the vector to 64 bits.
6196static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006197 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006198 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6199 N->getOperand(0)->getValueType(0),
6200 N->getValueType(0),
6201 N->getOpcode());
6202
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006203 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006204 return SkipLoadExtensionForVMULL(LD, DAG);
6205
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006206 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6207 // have been legalized as a BITCAST from v4i32.
6208 if (N->getOpcode() == ISD::BITCAST) {
6209 SDNode *BVN = N->getOperand(0).getNode();
6210 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6211 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006212 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006213 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006214 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6215 }
6216 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6217 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6218 EVT VT = N->getValueType(0);
6219 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6220 unsigned NumElts = VT.getVectorNumElements();
6221 MVT TruncVT = MVT::getIntegerVT(EltSize);
6222 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006223 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006224 for (unsigned i = 0; i != NumElts; ++i) {
6225 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6226 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006227 // Element types smaller than 32 bits are not legal, so use i32 elements.
6228 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006229 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006230 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006231 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006232 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006233}
6234
Evan Chenge2086e72011-03-29 01:56:09 +00006235static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6236 unsigned Opcode = N->getOpcode();
6237 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6238 SDNode *N0 = N->getOperand(0).getNode();
6239 SDNode *N1 = N->getOperand(1).getNode();
6240 return N0->hasOneUse() && N1->hasOneUse() &&
6241 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6242 }
6243 return false;
6244}
6245
6246static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6247 unsigned Opcode = N->getOpcode();
6248 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6249 SDNode *N0 = N->getOperand(0).getNode();
6250 SDNode *N1 = N->getOperand(1).getNode();
6251 return N0->hasOneUse() && N1->hasOneUse() &&
6252 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6253 }
6254 return false;
6255}
6256
Bob Wilson38ab35a2010-09-01 23:50:19 +00006257static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6258 // Multiplications are only custom-lowered for 128-bit vectors so that
6259 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6260 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006261 assert(VT.is128BitVector() && VT.isInteger() &&
6262 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006263 SDNode *N0 = Op.getOperand(0).getNode();
6264 SDNode *N1 = Op.getOperand(1).getNode();
6265 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006266 bool isMLA = false;
6267 bool isN0SExt = isSignExtended(N0, DAG);
6268 bool isN1SExt = isSignExtended(N1, DAG);
6269 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006270 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006271 else {
6272 bool isN0ZExt = isZeroExtended(N0, DAG);
6273 bool isN1ZExt = isZeroExtended(N1, DAG);
6274 if (isN0ZExt && isN1ZExt)
6275 NewOpc = ARMISD::VMULLu;
6276 else if (isN1SExt || isN1ZExt) {
6277 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6278 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6279 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6280 NewOpc = ARMISD::VMULLs;
6281 isMLA = true;
6282 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6283 NewOpc = ARMISD::VMULLu;
6284 isMLA = true;
6285 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6286 std::swap(N0, N1);
6287 NewOpc = ARMISD::VMULLu;
6288 isMLA = true;
6289 }
6290 }
6291
6292 if (!NewOpc) {
6293 if (VT == MVT::v2i64)
6294 // Fall through to expand this. It is not legal.
6295 return SDValue();
6296 else
6297 // Other vector multiplications are legal.
6298 return Op;
6299 }
6300 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006301
6302 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006303 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006304 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006305 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006306 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006307 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006308 assert(Op0.getValueType().is64BitVector() &&
6309 Op1.getValueType().is64BitVector() &&
6310 "unexpected types for extended operands to VMULL");
6311 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6312 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006313
Evan Chenge2086e72011-03-29 01:56:09 +00006314 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6315 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6316 // vmull q0, d4, d6
6317 // vmlal q0, d5, d6
6318 // is faster than
6319 // vaddl q0, d4, d5
6320 // vmovl q1, d6
6321 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006322 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6323 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006324 EVT Op1VT = Op1.getValueType();
6325 return DAG.getNode(N0->getOpcode(), DL, VT,
6326 DAG.getNode(NewOpc, DL, VT,
6327 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6328 DAG.getNode(NewOpc, DL, VT,
6329 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006330}
6331
Owen Anderson77aa2662011-04-05 21:48:57 +00006332static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006333LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006334 // Convert to float
6335 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6336 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6337 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6338 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6339 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6340 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6341 // Get reciprocal estimate.
6342 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006343 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006344 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6345 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006346 // Because char has a smaller range than uchar, we can actually get away
6347 // without any newton steps. This requires that we use a weird bias
6348 // of 0xb000, however (again, this has been exhaustively tested).
6349 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6350 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6351 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006352 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006353 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6354 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6355 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6356 // Convert back to short.
6357 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6358 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6359 return X;
6360}
6361
Owen Anderson77aa2662011-04-05 21:48:57 +00006362static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006363LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006364 SDValue N2;
6365 // Convert to float.
6366 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6367 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6368 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6369 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6370 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6371 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006372
Nate Begemanfa62d502011-02-11 20:53:29 +00006373 // Use reciprocal estimate and one refinement step.
6374 // float4 recip = vrecpeq_f32(yf);
6375 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006376 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006377 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6378 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006379 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006380 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006381 N1, N2);
6382 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6383 // Because short has a smaller range than ushort, we can actually get away
6384 // with only a single newton step. This requires that we use a weird bias
6385 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006386 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006387 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6388 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006389 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006390 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6391 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6392 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6393 // Convert back to integer and return.
6394 // return vmovn_s32(vcvt_s32_f32(result));
6395 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6396 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6397 return N0;
6398}
6399
6400static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6401 EVT VT = Op.getValueType();
6402 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6403 "unexpected type for custom-lowering ISD::SDIV");
6404
Andrew Trickef9de2a2013-05-25 02:42:55 +00006405 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006406 SDValue N0 = Op.getOperand(0);
6407 SDValue N1 = Op.getOperand(1);
6408 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006409
Nate Begemanfa62d502011-02-11 20:53:29 +00006410 if (VT == MVT::v8i8) {
6411 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6412 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006413
Nate Begemanfa62d502011-02-11 20:53:29 +00006414 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006415 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006416 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006417 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006418 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006419 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006420 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006421 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006422
6423 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6424 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6425
6426 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6427 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006428
Nate Begemanfa62d502011-02-11 20:53:29 +00006429 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6430 return N0;
6431 }
6432 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6433}
6434
6435static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6436 EVT VT = Op.getValueType();
6437 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6438 "unexpected type for custom-lowering ISD::UDIV");
6439
Andrew Trickef9de2a2013-05-25 02:42:55 +00006440 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006441 SDValue N0 = Op.getOperand(0);
6442 SDValue N1 = Op.getOperand(1);
6443 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006444
Nate Begemanfa62d502011-02-11 20:53:29 +00006445 if (VT == MVT::v8i8) {
6446 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6447 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006448
Nate Begemanfa62d502011-02-11 20:53:29 +00006449 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006450 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006451 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006452 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006453 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006454 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006455 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006456 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006457
Nate Begemanfa62d502011-02-11 20:53:29 +00006458 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6459 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006460
Nate Begemanfa62d502011-02-11 20:53:29 +00006461 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6462 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006463
6464 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006465 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6466 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006467 N0);
6468 return N0;
6469 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006470
Nate Begemanfa62d502011-02-11 20:53:29 +00006471 // v4i16 sdiv ... Convert to float.
6472 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6473 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6474 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6475 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6476 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006477 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006478
6479 // Use reciprocal estimate and two refinement steps.
6480 // float4 recip = vrecpeq_f32(yf);
6481 // recip *= vrecpsq_f32(yf, recip);
6482 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006483 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006484 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6485 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006486 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006487 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006488 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006489 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006490 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006491 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006492 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006493 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6494 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6495 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6496 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006497 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006498 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6499 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006500 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006501 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6502 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6503 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6504 // Convert back to integer and return.
6505 // return vmovn_u32(vcvt_s32_f32(result));
6506 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6507 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6508 return N0;
6509}
6510
Evan Chenge8916542011-08-30 01:34:54 +00006511static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6512 EVT VT = Op.getNode()->getValueType(0);
6513 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6514
6515 unsigned Opc;
6516 bool ExtraOp = false;
6517 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006518 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006519 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6520 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6521 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6522 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6523 }
6524
6525 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006526 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006527 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006528 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006529 Op.getOperand(1), Op.getOperand(2));
6530}
6531
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006532SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6533 assert(Subtarget->isTargetDarwin());
6534
6535 // For iOS, we want to call an alternative entry point: __sincos_stret,
6536 // return values are passed via sret.
6537 SDLoc dl(Op);
6538 SDValue Arg = Op.getOperand(0);
6539 EVT ArgVT = Arg.getValueType();
6540 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006541 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006542
6543 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006544
6545 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006546 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006547
6548 // Create stack object for sret.
Mehdi Amini44ede332015-07-09 02:09:04 +00006549 auto &DL = DAG.getDataLayout();
6550 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6551 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006552 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00006553 SDValue SRet = DAG.getFrameIndex(FrameIdx, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006554
6555 ArgListTy Args;
6556 ArgListEntry Entry;
6557
6558 Entry.Node = SRet;
6559 Entry.Ty = RetTy->getPointerTo();
6560 Entry.isSExt = false;
6561 Entry.isZExt = false;
6562 Entry.isSRet = true;
6563 Args.push_back(Entry);
6564
6565 Entry.Node = Arg;
6566 Entry.Ty = ArgTy;
6567 Entry.isSExt = false;
6568 Entry.isZExt = false;
6569 Args.push_back(Entry);
6570
6571 const char *LibcallName = (ArgVT == MVT::f64)
6572 ? "__sincos_stret" : "__sincosf_stret";
Mehdi Amini44ede332015-07-09 02:09:04 +00006573 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006574
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006575 TargetLowering::CallLoweringInfo CLI(DAG);
6576 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6577 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006578 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006579 .setDiscardResult();
6580
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006581 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6582
6583 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6584 MachinePointerInfo(), false, false, false, 0);
6585
6586 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006587 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006588 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006589 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6590 MachinePointerInfo(), false, false, false, 0);
6591
6592 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6593 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6594 LoadSin.getValue(0), LoadCos.getValue(0));
6595}
6596
Eli Friedman10f9ce22011-09-15 22:26:18 +00006597static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006598 // Monotonic load/store is legal for all targets
6599 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6600 return Op;
6601
Alp Tokercb402912014-01-24 17:20:08 +00006602 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006603 // dmb or equivalent available.
6604 return SDValue();
6605}
6606
Tim Northoverbc933082013-05-23 19:11:20 +00006607static void ReplaceREADCYCLECOUNTER(SDNode *N,
6608 SmallVectorImpl<SDValue> &Results,
6609 SelectionDAG &DAG,
6610 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006611 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006612 SDValue Cycles32, OutChain;
6613
6614 if (Subtarget->hasPerfMon()) {
6615 // Under Power Management extensions, the cycle-count is:
6616 // mrc p15, #0, <Rt>, c9, c13, #0
6617 SDValue Ops[] = { N->getOperand(0), // Chain
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006618 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6619 DAG.getConstant(15, DL, MVT::i32),
6620 DAG.getConstant(0, DL, MVT::i32),
6621 DAG.getConstant(9, DL, MVT::i32),
6622 DAG.getConstant(13, DL, MVT::i32),
6623 DAG.getConstant(0, DL, MVT::i32)
Tim Northoverbc933082013-05-23 19:11:20 +00006624 };
6625
6626 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006627 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006628 OutChain = Cycles32.getValue(1);
6629 } else {
6630 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6631 // there are older ARM CPUs that have implementation-specific ways of
6632 // obtaining this information (FIXME!).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006633 Cycles32 = DAG.getConstant(0, DL, MVT::i32);
Tim Northoverbc933082013-05-23 19:11:20 +00006634 OutChain = DAG.getEntryNode();
6635 }
6636
6637
6638 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006639 Cycles32, DAG.getConstant(0, DL, MVT::i32));
Tim Northoverbc933082013-05-23 19:11:20 +00006640 Results.push_back(Cycles64);
6641 Results.push_back(OutChain);
6642}
6643
Dan Gohman21cea8a2010-04-17 15:26:15 +00006644SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006645 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006646 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006647 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006648 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006649 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006650 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006651 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6652 default: llvm_unreachable("unknown object format");
6653 case Triple::COFF:
6654 return LowerGlobalAddressWindows(Op, DAG);
6655 case Triple::ELF:
6656 return LowerGlobalAddressELF(Op, DAG);
6657 case Triple::MachO:
6658 return LowerGlobalAddressDarwin(Op, DAG);
6659 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006660 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006661 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006662 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6663 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006664 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006665 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006666 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006667 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006668 case ISD::SINT_TO_FP:
6669 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6670 case ISD::FP_TO_SINT:
6671 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006672 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006673 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006674 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006675 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006676 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006677 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006678 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006679 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6680 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006681 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006682 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006683 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006684 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006685 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006686 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006687 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006688 case ISD::CTTZ:
6689 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006690 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006691 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006692 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006693 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006694 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006695 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006696 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006697 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006698 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006699 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006700 case ISD::SDIV: return LowerSDIV(Op, DAG);
6701 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006702 case ISD::ADDC:
6703 case ISD::ADDE:
6704 case ISD::SUBC:
6705 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006706 case ISD::SADDO:
6707 case ISD::UADDO:
6708 case ISD::SSUBO:
6709 case ISD::USUBO:
6710 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006711 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006712 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006713 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006714 case ISD::SDIVREM:
6715 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006716 case ISD::DYNAMIC_STACKALLOC:
6717 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6718 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6719 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006720 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6721 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006722 }
Evan Cheng10043e22007-01-19 07:51:42 +00006723}
6724
Duncan Sands6ed40142008-12-01 11:39:25 +00006725/// ReplaceNodeResults - Replace the results of node with an illegal result
6726/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006727void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6728 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006729 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006730 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006731 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006732 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006733 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006734 case ISD::READ_REGISTER:
6735 ExpandREAD_REGISTER(N, Results, DAG);
6736 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006737 case ISD::BITCAST:
6738 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006739 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006740 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006741 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006742 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006743 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006744 case ISD::READCYCLECOUNTER:
6745 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6746 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006747 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006748 if (Res.getNode())
6749 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006750}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006751
Evan Cheng10043e22007-01-19 07:51:42 +00006752//===----------------------------------------------------------------------===//
6753// ARM Scheduler Hooks
6754//===----------------------------------------------------------------------===//
6755
Bill Wendling030b58e2011-10-06 22:18:16 +00006756/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6757/// registers the function context.
6758void ARMTargetLowering::
6759SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6760 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006761 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006762 DebugLoc dl = MI->getDebugLoc();
6763 MachineFunction *MF = MBB->getParent();
6764 MachineRegisterInfo *MRI = &MF->getRegInfo();
6765 MachineConstantPool *MCP = MF->getConstantPool();
6766 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6767 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006768
Bill Wendling374ee192011-10-03 21:25:38 +00006769 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006770 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006771
Bill Wendling374ee192011-10-03 21:25:38 +00006772 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006773 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006774 ARMConstantPoolValue *CPV =
6775 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6776 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6777
Craig Topper61e88f42014-11-21 05:58:21 +00006778 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6779 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006780
Bill Wendling030b58e2011-10-06 22:18:16 +00006781 // Grab constant pool and fixed stack memory operands.
6782 MachineMemOperand *CPMMO =
6783 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6784 MachineMemOperand::MOLoad, 4, 4);
6785
6786 MachineMemOperand *FIMMOSt =
6787 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6788 MachineMemOperand::MOStore, 4, 4);
6789
6790 // Load the address of the dispatch MBB into the jump buffer.
6791 if (isThumb2) {
6792 // Incoming value: jbuf
6793 // ldr.n r5, LCPI1_1
6794 // orr r5, r5, #1
6795 // add r5, pc
6796 // str r5, [$jbuf, #+4] ; &jbuf[1]
6797 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6798 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6799 .addConstantPoolIndex(CPI)
6800 .addMemOperand(CPMMO));
6801 // Set the low bit because of thumb mode.
6802 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6803 AddDefaultCC(
6804 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6805 .addReg(NewVReg1, RegState::Kill)
6806 .addImm(0x01)));
6807 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6808 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6809 .addReg(NewVReg2, RegState::Kill)
6810 .addImm(PCLabelId);
6811 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6812 .addReg(NewVReg3, RegState::Kill)
6813 .addFrameIndex(FI)
6814 .addImm(36) // &jbuf[1] :: pc
6815 .addMemOperand(FIMMOSt));
6816 } else if (isThumb) {
6817 // Incoming value: jbuf
6818 // ldr.n r1, LCPI1_4
6819 // add r1, pc
6820 // mov r2, #1
6821 // orrs r1, r2
6822 // add r2, $jbuf, #+4 ; &jbuf[1]
6823 // str r1, [r2]
6824 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6825 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6826 .addConstantPoolIndex(CPI)
6827 .addMemOperand(CPMMO));
6828 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6829 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6830 .addReg(NewVReg1, RegState::Kill)
6831 .addImm(PCLabelId);
6832 // Set the low bit because of thumb mode.
6833 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6834 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6835 .addReg(ARM::CPSR, RegState::Define)
6836 .addImm(1));
6837 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6838 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6839 .addReg(ARM::CPSR, RegState::Define)
6840 .addReg(NewVReg2, RegState::Kill)
6841 .addReg(NewVReg3, RegState::Kill));
6842 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006843 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6844 .addFrameIndex(FI)
6845 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006846 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6847 .addReg(NewVReg4, RegState::Kill)
6848 .addReg(NewVReg5, RegState::Kill)
6849 .addImm(0)
6850 .addMemOperand(FIMMOSt));
6851 } else {
6852 // Incoming value: jbuf
6853 // ldr r1, LCPI1_1
6854 // add r1, pc, r1
6855 // str r1, [$jbuf, #+4] ; &jbuf[1]
6856 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6857 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6858 .addConstantPoolIndex(CPI)
6859 .addImm(0)
6860 .addMemOperand(CPMMO));
6861 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6862 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6863 .addReg(NewVReg1, RegState::Kill)
6864 .addImm(PCLabelId));
6865 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6866 .addReg(NewVReg2, RegState::Kill)
6867 .addFrameIndex(FI)
6868 .addImm(36) // &jbuf[1] :: pc
6869 .addMemOperand(FIMMOSt));
6870 }
6871}
6872
Matthias Brauneec4efc2015-04-28 00:37:05 +00006873void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6874 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006875 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006876 DebugLoc dl = MI->getDebugLoc();
6877 MachineFunction *MF = MBB->getParent();
6878 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006879 MachineFrameInfo *MFI = MF->getFrameInfo();
6880 int FI = MFI->getFunctionContextIndex();
6881
Craig Topper61e88f42014-11-21 05:58:21 +00006882 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6883 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006884
Bill Wendling362c1b02011-10-06 21:29:56 +00006885 // Get a mapping of the call site numbers to all of the landing pads they're
6886 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006887 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6888 unsigned MaxCSNum = 0;
6889 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006890 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6891 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006892 if (!BB->isLandingPad()) continue;
6893
6894 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6895 // pad.
6896 for (MachineBasicBlock::iterator
6897 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6898 if (!II->isEHLabel()) continue;
6899
6900 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006901 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006902
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006903 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6904 for (SmallVectorImpl<unsigned>::iterator
6905 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6906 CSI != CSE; ++CSI) {
6907 CallSiteNumToLPad[*CSI].push_back(BB);
6908 MaxCSNum = std::max(MaxCSNum, *CSI);
6909 }
Bill Wendling202803e2011-10-05 00:02:33 +00006910 break;
6911 }
6912 }
6913
6914 // Get an ordered list of the machine basic blocks for the jump table.
6915 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006916 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006917 LPadList.reserve(CallSiteNumToLPad.size());
6918 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6919 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6920 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006921 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006922 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006923 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6924 }
Bill Wendling202803e2011-10-05 00:02:33 +00006925 }
6926
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006927 assert(!LPadList.empty() &&
6928 "No landing pad destinations for the dispatch jump table!");
6929
Bill Wendling362c1b02011-10-06 21:29:56 +00006930 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006931 MachineJumpTableInfo *JTI =
6932 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6933 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00006934 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006935
Bill Wendling362c1b02011-10-06 21:29:56 +00006936 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006937
6938 // Shove the dispatch's address into the return slot in the function context.
6939 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6940 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006941
Bill Wendling324be982011-10-05 00:39:32 +00006942 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006943 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006944 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006945 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006946 else
6947 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6948
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006949 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006950 DispatchBB->addSuccessor(TrapBB);
6951
6952 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6953 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006954
Bill Wendling510fbcd2011-10-17 21:32:56 +00006955 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006956 MF->insert(MF->end(), DispatchBB);
6957 MF->insert(MF->end(), DispContBB);
6958 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006959
Bill Wendling030b58e2011-10-06 22:18:16 +00006960 // Insert code into the entry block that creates and registers the function
6961 // context.
6962 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6963
Bill Wendling030b58e2011-10-06 22:18:16 +00006964 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006965 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006966 MachineMemOperand::MOLoad |
6967 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006968
Chad Rosier1ec8e402012-11-06 23:05:24 +00006969 MachineInstrBuilder MIB;
6970 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6971
6972 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6973 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6974
6975 // Add a register mask with no preserved registers. This results in all
6976 // registers being marked as clobbered.
6977 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006978
Bill Wendling85833f72011-10-18 22:49:07 +00006979 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006980 if (Subtarget->isThumb2()) {
6981 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6982 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6983 .addFrameIndex(FI)
6984 .addImm(4)
6985 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006986
Bill Wendling85833f72011-10-18 22:49:07 +00006987 if (NumLPads < 256) {
6988 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6989 .addReg(NewVReg1)
6990 .addImm(LPadList.size()));
6991 } else {
6992 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6993 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006994 .addImm(NumLPads & 0xFFFF));
6995
6996 unsigned VReg2 = VReg1;
6997 if ((NumLPads & 0xFFFF0000) != 0) {
6998 VReg2 = MRI->createVirtualRegister(TRC);
6999 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7000 .addReg(VReg1)
7001 .addImm(NumLPads >> 16));
7002 }
7003
Bill Wendling85833f72011-10-18 22:49:07 +00007004 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7005 .addReg(NewVReg1)
7006 .addReg(VReg2));
7007 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007008
Bill Wendling5626c662011-10-06 22:53:00 +00007009 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7010 .addMBB(TrapBB)
7011 .addImm(ARMCC::HI)
7012 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007013
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007014 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7015 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007016 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007017
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007018 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007019 AddDefaultCC(
7020 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007021 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7022 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007023 .addReg(NewVReg1)
7024 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7025
7026 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007027 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007028 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007029 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007030 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007031 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7032 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7033 .addFrameIndex(FI)
7034 .addImm(1)
7035 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007036
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007037 if (NumLPads < 256) {
7038 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7039 .addReg(NewVReg1)
7040 .addImm(NumLPads));
7041 } else {
7042 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007043 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7044 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7045
7046 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007047 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007048 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007049 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007050 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007051
7052 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7053 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7054 .addReg(VReg1, RegState::Define)
7055 .addConstantPoolIndex(Idx));
7056 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7057 .addReg(NewVReg1)
7058 .addReg(VReg1));
7059 }
7060
Bill Wendlingb3d46782011-10-06 23:37:36 +00007061 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7062 .addMBB(TrapBB)
7063 .addImm(ARMCC::HI)
7064 .addReg(ARM::CPSR);
7065
7066 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7067 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7068 .addReg(ARM::CPSR, RegState::Define)
7069 .addReg(NewVReg1)
7070 .addImm(2));
7071
7072 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007073 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007074 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007075
7076 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7077 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7078 .addReg(ARM::CPSR, RegState::Define)
7079 .addReg(NewVReg2, RegState::Kill)
7080 .addReg(NewVReg3));
7081
7082 MachineMemOperand *JTMMOLd =
7083 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
7084 MachineMemOperand::MOLoad, 4, 4);
7085
7086 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7087 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7088 .addReg(NewVReg4, RegState::Kill)
7089 .addImm(0)
7090 .addMemOperand(JTMMOLd));
7091
Chad Rosier96603432013-03-01 18:30:38 +00007092 unsigned NewVReg6 = NewVReg5;
7093 if (RelocM == Reloc::PIC_) {
7094 NewVReg6 = MRI->createVirtualRegister(TRC);
7095 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7096 .addReg(ARM::CPSR, RegState::Define)
7097 .addReg(NewVReg5, RegState::Kill)
7098 .addReg(NewVReg3));
7099 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007100
7101 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7102 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007103 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007104 } else {
7105 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7106 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7107 .addFrameIndex(FI)
7108 .addImm(4)
7109 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007110
Bill Wendling4969dcd2011-10-18 22:52:20 +00007111 if (NumLPads < 256) {
7112 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7113 .addReg(NewVReg1)
7114 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007115 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007116 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7117 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007118 .addImm(NumLPads & 0xFFFF));
7119
7120 unsigned VReg2 = VReg1;
7121 if ((NumLPads & 0xFFFF0000) != 0) {
7122 VReg2 = MRI->createVirtualRegister(TRC);
7123 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7124 .addReg(VReg1)
7125 .addImm(NumLPads >> 16));
7126 }
7127
Bill Wendling4969dcd2011-10-18 22:52:20 +00007128 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7129 .addReg(NewVReg1)
7130 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007131 } else {
7132 MachineConstantPool *ConstantPool = MF->getConstantPool();
7133 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7134 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7135
7136 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007137 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007138 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007139 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007140 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7141
7142 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7143 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7144 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007145 .addConstantPoolIndex(Idx)
7146 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007147 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7148 .addReg(NewVReg1)
7149 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007150 }
7151
Bill Wendling5626c662011-10-06 22:53:00 +00007152 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7153 .addMBB(TrapBB)
7154 .addImm(ARMCC::HI)
7155 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007156
Bill Wendling973c8172011-10-18 22:11:18 +00007157 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007158 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007159 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007160 .addReg(NewVReg1)
7161 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007162 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7163 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007164 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007165
7166 MachineMemOperand *JTMMOLd =
7167 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
7168 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007169 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007170 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007171 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7172 .addReg(NewVReg3, RegState::Kill)
7173 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007174 .addImm(0)
7175 .addMemOperand(JTMMOLd));
7176
Chad Rosier96603432013-03-01 18:30:38 +00007177 if (RelocM == Reloc::PIC_) {
7178 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7179 .addReg(NewVReg5, RegState::Kill)
7180 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007181 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007182 } else {
7183 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7184 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007185 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007186 }
Bill Wendling5626c662011-10-06 22:53:00 +00007187 }
Bill Wendling202803e2011-10-05 00:02:33 +00007188
Bill Wendling324be982011-10-05 00:39:32 +00007189 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007190 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007191 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007192 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7193 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007194 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007195 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007196 }
7197
Bill Wendling26d27802011-10-17 05:25:09 +00007198 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007199 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007200 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007201 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007202
7203 // Remove the landing pad successor from the invoke block and replace it
7204 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007205 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7206 BB->succ_end());
7207 while (!Successors.empty()) {
7208 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00007209 if (SMBB->isLandingPad()) {
7210 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007211 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007212 }
7213 }
7214
7215 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007216
7217 // Find the invoke call and mark all of the callee-saved registers as
7218 // 'implicit defined' so that they're spilled. This prevents code from
7219 // moving instructions to before the EH block, where they will never be
7220 // executed.
7221 for (MachineBasicBlock::reverse_iterator
7222 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007223 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007224
7225 DenseMap<unsigned, bool> DefRegs;
7226 for (MachineInstr::mop_iterator
7227 OI = II->operands_begin(), OE = II->operands_end();
7228 OI != OE; ++OI) {
7229 if (!OI->isReg()) continue;
7230 DefRegs[OI->getReg()] = true;
7231 }
7232
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007233 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007234
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007235 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007236 unsigned Reg = SavedRegs[i];
7237 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007238 !ARM::tGPRRegClass.contains(Reg) &&
7239 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007240 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007241 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007242 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007243 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007244 continue;
7245 if (!DefRegs[Reg])
7246 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007247 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007248
7249 break;
7250 }
Bill Wendling883ec972011-10-07 23:18:02 +00007251 }
Bill Wendling324be982011-10-05 00:39:32 +00007252
Bill Wendling617075f2011-10-18 18:30:49 +00007253 // Mark all former landing pads as non-landing pads. The dispatch is the only
7254 // landing pad now.
7255 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7256 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7257 (*I)->setIsLandingPad(false);
7258
Bill Wendling324be982011-10-05 00:39:32 +00007259 // The instruction is gone now.
7260 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007261}
7262
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007263static
7264MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7265 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7266 E = MBB->succ_end(); I != E; ++I)
7267 if (*I != Succ)
7268 return *I;
7269 llvm_unreachable("Expecting a BB with two successors!");
7270}
7271
Manman Renb504f492013-10-29 22:27:32 +00007272/// Return the load opcode for a given load size. If load size >= 8,
7273/// neon opcode will be returned.
7274static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7275 if (LdSize >= 8)
7276 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7277 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7278 if (IsThumb1)
7279 return LdSize == 4 ? ARM::tLDRi
7280 : LdSize == 2 ? ARM::tLDRHi
7281 : LdSize == 1 ? ARM::tLDRBi : 0;
7282 if (IsThumb2)
7283 return LdSize == 4 ? ARM::t2LDR_POST
7284 : LdSize == 2 ? ARM::t2LDRH_POST
7285 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7286 return LdSize == 4 ? ARM::LDR_POST_IMM
7287 : LdSize == 2 ? ARM::LDRH_POST
7288 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7289}
7290
7291/// Return the store opcode for a given store size. If store size >= 8,
7292/// neon opcode will be returned.
7293static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7294 if (StSize >= 8)
7295 return StSize == 16 ? ARM::VST1q32wb_fixed
7296 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7297 if (IsThumb1)
7298 return StSize == 4 ? ARM::tSTRi
7299 : StSize == 2 ? ARM::tSTRHi
7300 : StSize == 1 ? ARM::tSTRBi : 0;
7301 if (IsThumb2)
7302 return StSize == 4 ? ARM::t2STR_POST
7303 : StSize == 2 ? ARM::t2STRH_POST
7304 : StSize == 1 ? ARM::t2STRB_POST : 0;
7305 return StSize == 4 ? ARM::STR_POST_IMM
7306 : StSize == 2 ? ARM::STRH_POST
7307 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7308}
7309
7310/// Emit a post-increment load operation with given size. The instructions
7311/// will be added to BB at Pos.
7312static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7313 const TargetInstrInfo *TII, DebugLoc dl,
7314 unsigned LdSize, unsigned Data, unsigned AddrIn,
7315 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7316 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7317 assert(LdOpc != 0 && "Should have a load opcode");
7318 if (LdSize >= 8) {
7319 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7320 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7321 .addImm(0));
7322 } else if (IsThumb1) {
7323 // load + update AddrIn
7324 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7325 .addReg(AddrIn).addImm(0));
7326 MachineInstrBuilder MIB =
7327 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7328 MIB = AddDefaultT1CC(MIB);
7329 MIB.addReg(AddrIn).addImm(LdSize);
7330 AddDefaultPred(MIB);
7331 } else if (IsThumb2) {
7332 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7333 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7334 .addImm(LdSize));
7335 } else { // arm
7336 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7337 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7338 .addReg(0).addImm(LdSize));
7339 }
7340}
7341
7342/// Emit a post-increment store operation with given size. The instructions
7343/// will be added to BB at Pos.
7344static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7345 const TargetInstrInfo *TII, DebugLoc dl,
7346 unsigned StSize, unsigned Data, unsigned AddrIn,
7347 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7348 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7349 assert(StOpc != 0 && "Should have a store opcode");
7350 if (StSize >= 8) {
7351 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7352 .addReg(AddrIn).addImm(0).addReg(Data));
7353 } else if (IsThumb1) {
7354 // store + update AddrIn
7355 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7356 .addReg(AddrIn).addImm(0));
7357 MachineInstrBuilder MIB =
7358 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7359 MIB = AddDefaultT1CC(MIB);
7360 MIB.addReg(AddrIn).addImm(StSize);
7361 AddDefaultPred(MIB);
7362 } else if (IsThumb2) {
7363 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7364 .addReg(Data).addReg(AddrIn).addImm(StSize));
7365 } else { // arm
7366 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7367 .addReg(Data).addReg(AddrIn).addReg(0)
7368 .addImm(StSize));
7369 }
7370}
7371
David Peixottoc32e24a2013-10-17 19:49:22 +00007372MachineBasicBlock *
7373ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7374 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007375 // This pseudo instruction has 3 operands: dst, src, size
7376 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7377 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007378 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007379 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7380 MachineFunction::iterator It = BB;
7381 ++It;
7382
7383 unsigned dest = MI->getOperand(0).getReg();
7384 unsigned src = MI->getOperand(1).getReg();
7385 unsigned SizeVal = MI->getOperand(2).getImm();
7386 unsigned Align = MI->getOperand(3).getImm();
7387 DebugLoc dl = MI->getDebugLoc();
7388
Manman Rene8735522012-06-01 19:33:18 +00007389 MachineFunction *MF = BB->getParent();
7390 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007391 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007392 const TargetRegisterClass *TRC = nullptr;
7393 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007394
7395 bool IsThumb1 = Subtarget->isThumb1Only();
7396 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007397
7398 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007399 UnitSize = 1;
7400 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007401 UnitSize = 2;
7402 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007403 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007404 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007405 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007406 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007407 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007408 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007409 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007410 }
7411 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007412 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007413 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007414 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007415
David Peixottob0653e532013-10-24 16:39:36 +00007416 // Select the correct opcode and register class for unit size load/store
7417 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007418 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007419 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007420 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7421 : UnitSize == 8 ? &ARM::DPRRegClass
7422 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007423
Manman Rene8735522012-06-01 19:33:18 +00007424 unsigned BytesLeft = SizeVal % UnitSize;
7425 unsigned LoopSize = SizeVal - BytesLeft;
7426
7427 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7428 // Use LDR and STR to copy.
7429 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7430 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7431 unsigned srcIn = src;
7432 unsigned destIn = dest;
7433 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007434 unsigned srcOut = MRI.createVirtualRegister(TRC);
7435 unsigned destOut = MRI.createVirtualRegister(TRC);
7436 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007437 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7438 IsThumb1, IsThumb2);
7439 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7440 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007441 srcIn = srcOut;
7442 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007443 }
7444
7445 // Handle the leftover bytes with LDRB and STRB.
7446 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7447 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007448 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007449 unsigned srcOut = MRI.createVirtualRegister(TRC);
7450 unsigned destOut = MRI.createVirtualRegister(TRC);
7451 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007452 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7453 IsThumb1, IsThumb2);
7454 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7455 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007456 srcIn = srcOut;
7457 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007458 }
7459 MI->eraseFromParent(); // The instruction is gone now.
7460 return BB;
7461 }
7462
7463 // Expand the pseudo op to a loop.
7464 // thisMBB:
7465 // ...
7466 // movw varEnd, # --> with thumb2
7467 // movt varEnd, #
7468 // ldrcp varEnd, idx --> without thumb2
7469 // fallthrough --> loopMBB
7470 // loopMBB:
7471 // PHI varPhi, varEnd, varLoop
7472 // PHI srcPhi, src, srcLoop
7473 // PHI destPhi, dst, destLoop
7474 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7475 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7476 // subs varLoop, varPhi, #UnitSize
7477 // bne loopMBB
7478 // fallthrough --> exitMBB
7479 // exitMBB:
7480 // epilogue to handle left-over bytes
7481 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7482 // [destOut] = STRB_POST(scratch, destLoop, 1)
7483 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7484 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7485 MF->insert(It, loopMBB);
7486 MF->insert(It, exitMBB);
7487
7488 // Transfer the remainder of BB and its successor edges to exitMBB.
7489 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007490 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007491 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7492
7493 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007494 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007495 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007496 unsigned Vtmp = varEnd;
7497 if ((LoopSize & 0xFFFF0000) != 0)
7498 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007499 AddDefaultPred(BuildMI(BB, dl,
7500 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7501 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007502
7503 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007504 AddDefaultPred(BuildMI(BB, dl,
7505 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7506 varEnd)
7507 .addReg(Vtmp)
7508 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007509 } else {
7510 MachineConstantPool *ConstantPool = MF->getConstantPool();
7511 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7512 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7513
7514 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007515 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007516 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007517 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007518 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7519
7520 if (IsThumb1)
7521 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7522 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7523 else
7524 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7525 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7526 }
Manman Rene8735522012-06-01 19:33:18 +00007527 BB->addSuccessor(loopMBB);
7528
7529 // Generate the loop body:
7530 // varPhi = PHI(varLoop, varEnd)
7531 // srcPhi = PHI(srcLoop, src)
7532 // destPhi = PHI(destLoop, dst)
7533 MachineBasicBlock *entryBB = BB;
7534 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007535 unsigned varLoop = MRI.createVirtualRegister(TRC);
7536 unsigned varPhi = MRI.createVirtualRegister(TRC);
7537 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7538 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7539 unsigned destLoop = MRI.createVirtualRegister(TRC);
7540 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007541
7542 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7543 .addReg(varLoop).addMBB(loopMBB)
7544 .addReg(varEnd).addMBB(entryBB);
7545 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7546 .addReg(srcLoop).addMBB(loopMBB)
7547 .addReg(src).addMBB(entryBB);
7548 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7549 .addReg(destLoop).addMBB(loopMBB)
7550 .addReg(dest).addMBB(entryBB);
7551
7552 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7553 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007554 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007555 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7556 IsThumb1, IsThumb2);
7557 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7558 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007559
7560 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007561 if (IsThumb1) {
7562 MachineInstrBuilder MIB =
7563 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7564 MIB = AddDefaultT1CC(MIB);
7565 MIB.addReg(varPhi).addImm(UnitSize);
7566 AddDefaultPred(MIB);
7567 } else {
7568 MachineInstrBuilder MIB =
7569 BuildMI(*BB, BB->end(), dl,
7570 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7571 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7572 MIB->getOperand(5).setReg(ARM::CPSR);
7573 MIB->getOperand(5).setIsDef(true);
7574 }
7575 BuildMI(*BB, BB->end(), dl,
7576 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7577 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007578
7579 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7580 BB->addSuccessor(loopMBB);
7581 BB->addSuccessor(exitMBB);
7582
7583 // Add epilogue to handle BytesLeft.
7584 BB = exitMBB;
7585 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007586
7587 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7588 // [destOut] = STRB_POST(scratch, destLoop, 1)
7589 unsigned srcIn = srcLoop;
7590 unsigned destIn = destLoop;
7591 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007592 unsigned srcOut = MRI.createVirtualRegister(TRC);
7593 unsigned destOut = MRI.createVirtualRegister(TRC);
7594 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007595 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7596 IsThumb1, IsThumb2);
7597 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7598 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007599 srcIn = srcOut;
7600 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007601 }
7602
7603 MI->eraseFromParent(); // The instruction is gone now.
7604 return BB;
7605}
7606
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007607MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007608ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7609 MachineBasicBlock *MBB) const {
7610 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007611 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007612 DebugLoc DL = MI->getDebugLoc();
7613
7614 assert(Subtarget->isTargetWindows() &&
7615 "__chkstk is only supported on Windows");
7616 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7617
7618 // __chkstk takes the number of words to allocate on the stack in R4, and
7619 // returns the stack adjustment in number of bytes in R4. This will not
7620 // clober any other registers (other than the obvious lr).
7621 //
7622 // Although, technically, IP should be considered a register which may be
7623 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7624 // thumb-2 environment, so there is no interworking required. As a result, we
7625 // do not expect a veneer to be emitted by the linker, clobbering IP.
7626 //
Alp Toker1d099d92014-06-19 19:41:26 +00007627 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007628 // required, again, ensuring that IP is not clobbered.
7629 //
7630 // Finally, although some linkers may theoretically provide a trampoline for
7631 // out of range calls (which is quite common due to a 32M range limitation of
7632 // branches for Thumb), we can generate the long-call version via
7633 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7634 // IP.
7635
7636 switch (TM.getCodeModel()) {
7637 case CodeModel::Small:
7638 case CodeModel::Medium:
7639 case CodeModel::Default:
7640 case CodeModel::Kernel:
7641 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7642 .addImm((unsigned)ARMCC::AL).addReg(0)
7643 .addExternalSymbol("__chkstk")
7644 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7645 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7646 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7647 break;
7648 case CodeModel::Large:
7649 case CodeModel::JITDefault: {
7650 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7651 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7652
7653 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7654 .addExternalSymbol("__chkstk");
7655 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7656 .addImm((unsigned)ARMCC::AL).addReg(0)
7657 .addReg(Reg, RegState::Kill)
7658 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7659 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7660 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7661 break;
7662 }
7663 }
7664
7665 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7666 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007667 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007668
7669 MI->eraseFromParent();
7670 return MBB;
7671}
7672
7673MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007674ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007675 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007676 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007677 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007678 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007679 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007680 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007681 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007682 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007683 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007684 // The Thumb2 pre-indexed stores have the same MI operands, they just
7685 // define them differently in the .td files from the isel patterns, so
7686 // they need pseudos.
7687 case ARM::t2STR_preidx:
7688 MI->setDesc(TII->get(ARM::t2STR_PRE));
7689 return BB;
7690 case ARM::t2STRB_preidx:
7691 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7692 return BB;
7693 case ARM::t2STRH_preidx:
7694 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7695 return BB;
7696
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007697 case ARM::STRi_preidx:
7698 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007699 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007700 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7701 // Decode the offset.
7702 unsigned Offset = MI->getOperand(4).getImm();
7703 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7704 Offset = ARM_AM::getAM2Offset(Offset);
7705 if (isSub)
7706 Offset = -Offset;
7707
Jim Grosbachf402f692011-08-12 21:02:34 +00007708 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007709 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007710 .addOperand(MI->getOperand(0)) // Rn_wb
7711 .addOperand(MI->getOperand(1)) // Rt
7712 .addOperand(MI->getOperand(2)) // Rn
7713 .addImm(Offset) // offset (skip GPR==zero_reg)
7714 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007715 .addOperand(MI->getOperand(6))
7716 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007717 MI->eraseFromParent();
7718 return BB;
7719 }
7720 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007721 case ARM::STRBr_preidx:
7722 case ARM::STRH_preidx: {
7723 unsigned NewOpc;
7724 switch (MI->getOpcode()) {
7725 default: llvm_unreachable("unexpected opcode!");
7726 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7727 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7728 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7729 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007730 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7731 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7732 MIB.addOperand(MI->getOperand(i));
7733 MI->eraseFromParent();
7734 return BB;
7735 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007736
Evan Chengbb2af352009-08-12 05:17:19 +00007737 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007738 // To "insert" a SELECT_CC instruction, we actually have to insert the
7739 // diamond control-flow pattern. The incoming instruction knows the
7740 // destination vreg to set, the condition code register to branch on, the
7741 // true/false values to select between, and a branch opcode to use.
7742 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007743 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007744 ++It;
7745
7746 // thisMBB:
7747 // ...
7748 // TrueVal = ...
7749 // cmpTY ccX, r1, r2
7750 // bCC copy1MBB
7751 // fallthrough --> copy0MBB
7752 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007753 MachineFunction *F = BB->getParent();
7754 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7755 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007756 F->insert(It, copy0MBB);
7757 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007758
7759 // Transfer the remainder of BB and its successor edges to sinkMBB.
7760 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007761 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007762 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7763
Dan Gohmanf4f04102010-07-06 15:49:48 +00007764 BB->addSuccessor(copy0MBB);
7765 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007766
Dan Gohman34396292010-07-06 20:24:04 +00007767 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7768 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7769
Evan Cheng10043e22007-01-19 07:51:42 +00007770 // copy0MBB:
7771 // %FalseValue = ...
7772 // # fallthrough to sinkMBB
7773 BB = copy0MBB;
7774
7775 // Update machine-CFG edges
7776 BB->addSuccessor(sinkMBB);
7777
7778 // sinkMBB:
7779 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7780 // ...
7781 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007782 BuildMI(*BB, BB->begin(), dl,
7783 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007784 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7785 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7786
Dan Gohman34396292010-07-06 20:24:04 +00007787 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007788 return BB;
7789 }
Evan Chengb972e562009-08-07 00:34:42 +00007790
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007791 case ARM::BCCi64:
7792 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007793 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007794 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007795
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007796 // Compare both parts that make up the double comparison separately for
7797 // equality.
7798 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7799
7800 unsigned LHS1 = MI->getOperand(1).getReg();
7801 unsigned LHS2 = MI->getOperand(2).getReg();
7802 if (RHSisZero) {
7803 AddDefaultPred(BuildMI(BB, dl,
7804 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7805 .addReg(LHS1).addImm(0));
7806 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7807 .addReg(LHS2).addImm(0)
7808 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7809 } else {
7810 unsigned RHS1 = MI->getOperand(3).getReg();
7811 unsigned RHS2 = MI->getOperand(4).getReg();
7812 AddDefaultPred(BuildMI(BB, dl,
7813 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7814 .addReg(LHS1).addReg(RHS1));
7815 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7816 .addReg(LHS2).addReg(RHS2)
7817 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7818 }
7819
7820 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7821 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7822 if (MI->getOperand(0).getImm() == ARMCC::NE)
7823 std::swap(destMBB, exitMBB);
7824
7825 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7826 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007827 if (isThumb2)
7828 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7829 else
7830 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007831
7832 MI->eraseFromParent(); // The pseudo instruction is gone now.
7833 return BB;
7834 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007835
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007836 case ARM::Int_eh_sjlj_setjmp:
7837 case ARM::Int_eh_sjlj_setjmp_nofp:
7838 case ARM::tInt_eh_sjlj_setjmp:
7839 case ARM::t2Int_eh_sjlj_setjmp:
7840 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00007841 return BB;
7842
7843 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007844 EmitSjLjDispatchBlock(MI, BB);
7845 return BB;
7846
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007847 case ARM::ABS:
7848 case ARM::t2ABS: {
7849 // To insert an ABS instruction, we have to insert the
7850 // diamond control-flow pattern. The incoming instruction knows the
7851 // source vreg to test against 0, the destination vreg to set,
7852 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007853 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007854 // It transforms
7855 // V1 = ABS V0
7856 // into
7857 // V2 = MOVS V0
7858 // BCC (branch to SinkBB if V0 >= 0)
7859 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007860 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007861 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7862 MachineFunction::iterator BBI = BB;
7863 ++BBI;
7864 MachineFunction *Fn = BB->getParent();
7865 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7866 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7867 Fn->insert(BBI, RSBBB);
7868 Fn->insert(BBI, SinkBB);
7869
7870 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7871 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00007872 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007873 bool isThumb2 = Subtarget->isThumb2();
7874 MachineRegisterInfo &MRI = Fn->getRegInfo();
7875 // In Thumb mode S must not be specified if source register is the SP or
7876 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007877 unsigned NewRsbDstReg =
7878 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007879
7880 // Transfer the remainder of BB and its successor edges to sinkMBB.
7881 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007882 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007883 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7884
7885 BB->addSuccessor(RSBBB);
7886 BB->addSuccessor(SinkBB);
7887
7888 // fall through to SinkMBB
7889 RSBBB->addSuccessor(SinkBB);
7890
Manman Rene0763c72012-06-15 21:32:12 +00007891 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007892 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007893 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7894 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007895
7896 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007897 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007898 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7899 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7900
7901 // insert rsbri in RSBBB
7902 // Note: BCC and rsbri will be converted into predicated rsbmi
7903 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007904 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007905 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00007906 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007907 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7908
Andrew Trick3f07c422011-10-18 18:40:53 +00007909 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007910 // reuse ABSDstReg to not change uses of ABS instruction
7911 BuildMI(*SinkBB, SinkBB->begin(), dl,
7912 TII->get(ARM::PHI), ABSDstReg)
7913 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007914 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007915
7916 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007917 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007918
7919 // return last added BB
7920 return SinkBB;
7921 }
Manman Rene8735522012-06-01 19:33:18 +00007922 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007923 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007924 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007925 case ARM::WIN__CHKSTK:
7926 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007927 }
7928}
7929
Evan Chenge6fba772011-08-30 19:09:48 +00007930void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7931 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007932 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007933 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7934 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7935 // operand is still set to noreg. If needed, set the optional operand's
7936 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007937 //
Andrew Trick88b24502011-10-18 19:18:52 +00007938 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007939
Andrew Trick924123a2011-09-21 02:20:46 +00007940 // Rename pseudo opcodes.
7941 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7942 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007943 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007944 MCID = &TII->get(NewOpc);
7945
7946 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7947 "converted opcode should be the same except for cc_out");
7948
7949 MI->setDesc(*MCID);
7950
7951 // Add the optional cc_out operand
7952 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007953 }
Andrew Trick88b24502011-10-18 19:18:52 +00007954 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007955
7956 // Any ARM instruction that sets the 's' bit should specify an optional
7957 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007958 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007959 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007960 return;
7961 }
Andrew Trick924123a2011-09-21 02:20:46 +00007962 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7963 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007964 bool definesCPSR = false;
7965 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007966 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007967 i != e; ++i) {
7968 const MachineOperand &MO = MI->getOperand(i);
7969 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7970 definesCPSR = true;
7971 if (MO.isDead())
7972 deadCPSR = true;
7973 MI->RemoveOperand(i);
7974 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007975 }
7976 }
Andrew Trick8586e622011-09-20 03:17:40 +00007977 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007978 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007979 return;
7980 }
7981 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007982 if (deadCPSR) {
7983 assert(!MI->getOperand(ccOutIdx).getReg() &&
7984 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007985 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007986 }
Andrew Trick8586e622011-09-20 03:17:40 +00007987
Andrew Trick924123a2011-09-21 02:20:46 +00007988 // If this instruction was defined with an optional CPSR def and its dag node
7989 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007990 MachineOperand &MO = MI->getOperand(ccOutIdx);
7991 MO.setReg(ARM::CPSR);
7992 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007993}
7994
Evan Cheng10043e22007-01-19 07:51:42 +00007995//===----------------------------------------------------------------------===//
7996// ARM Optimization Hooks
7997//===----------------------------------------------------------------------===//
7998
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007999// Helper function that checks if N is a null or all ones constant.
8000static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
8001 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
8002 if (!C)
8003 return false;
8004 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
8005}
8006
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008007// Return true if N is conditionally 0 or all ones.
8008// Detects these expressions where cc is an i1 value:
8009//
8010// (select cc 0, y) [AllOnes=0]
8011// (select cc y, 0) [AllOnes=0]
8012// (zext cc) [AllOnes=0]
8013// (sext cc) [AllOnes=0/1]
8014// (select cc -1, y) [AllOnes=1]
8015// (select cc y, -1) [AllOnes=1]
8016//
8017// Invert is set when N is the null/all ones constant when CC is false.
8018// OtherOp is set to the alternative value of N.
8019static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8020 SDValue &CC, bool &Invert,
8021 SDValue &OtherOp,
8022 SelectionDAG &DAG) {
8023 switch (N->getOpcode()) {
8024 default: return false;
8025 case ISD::SELECT: {
8026 CC = N->getOperand(0);
8027 SDValue N1 = N->getOperand(1);
8028 SDValue N2 = N->getOperand(2);
8029 if (isZeroOrAllOnes(N1, AllOnes)) {
8030 Invert = false;
8031 OtherOp = N2;
8032 return true;
8033 }
8034 if (isZeroOrAllOnes(N2, AllOnes)) {
8035 Invert = true;
8036 OtherOp = N1;
8037 return true;
8038 }
8039 return false;
8040 }
8041 case ISD::ZERO_EXTEND:
8042 // (zext cc) can never be the all ones value.
8043 if (AllOnes)
8044 return false;
8045 // Fall through.
8046 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008047 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008048 EVT VT = N->getValueType(0);
8049 CC = N->getOperand(0);
8050 if (CC.getValueType() != MVT::i1)
8051 return false;
8052 Invert = !AllOnes;
8053 if (AllOnes)
8054 // When looking for an AllOnes constant, N is an sext, and the 'other'
8055 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008056 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008057 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8058 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008059 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008060 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008061 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8062 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008063 return true;
8064 }
8065 }
8066}
8067
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008068// Combine a constant select operand into its use:
8069//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008070// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8071// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8072// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8073// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8074// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008075//
8076// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008077// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008078//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008079// Also recognize sext/zext from i1:
8080//
8081// (add (zext cc), x) -> (select cc (add x, 1), x)
8082// (add (sext cc), x) -> (select cc (add x, -1), x)
8083//
8084// These transformations eventually create predicated instructions.
8085//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008086// @param N The node to transform.
8087// @param Slct The N operand that is a select.
8088// @param OtherOp The other N operand (x above).
8089// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008090// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008091// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008092static
8093SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008094 TargetLowering::DAGCombinerInfo &DCI,
8095 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008096 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008097 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008098 SDValue NonConstantVal;
8099 SDValue CCOp;
8100 bool SwapSelectOps;
8101 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8102 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008103 return SDValue();
8104
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008105 // Slct is now know to be the desired identity constant when CC is true.
8106 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008107 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008108 OtherOp, NonConstantVal);
8109 // Unless SwapSelectOps says CC should be false.
8110 if (SwapSelectOps)
8111 std::swap(TrueVal, FalseVal);
8112
Andrew Trickef9de2a2013-05-25 02:42:55 +00008113 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008114 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008115}
8116
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008117// Attempt combineSelectAndUse on each operand of a commutative operator N.
8118static
8119SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8120 TargetLowering::DAGCombinerInfo &DCI) {
8121 SDValue N0 = N->getOperand(0);
8122 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008123 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008124 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8125 if (Result.getNode())
8126 return Result;
8127 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008128 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008129 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8130 if (Result.getNode())
8131 return Result;
8132 }
8133 return SDValue();
8134}
8135
Eric Christopher1b8b94192011-06-29 21:10:36 +00008136// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008137// (only after legalization).
8138static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8139 TargetLowering::DAGCombinerInfo &DCI,
8140 const ARMSubtarget *Subtarget) {
8141
8142 // Only perform optimization if after legalize, and if NEON is available. We
8143 // also expected both operands to be BUILD_VECTORs.
8144 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8145 || N0.getOpcode() != ISD::BUILD_VECTOR
8146 || N1.getOpcode() != ISD::BUILD_VECTOR)
8147 return SDValue();
8148
8149 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8150 EVT VT = N->getValueType(0);
8151 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8152 return SDValue();
8153
8154 // Check that the vector operands are of the right form.
8155 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8156 // operands, where N is the size of the formed vector.
8157 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8158 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008159
8160 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008161 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008162 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008163 SDValue Vec = N0->getOperand(0)->getOperand(0);
8164 SDNode *V = Vec.getNode();
8165 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008166
Eric Christopher1b8b94192011-06-29 21:10:36 +00008167 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008168 // check to see if each of their operands are an EXTRACT_VECTOR with
8169 // the same vector and appropriate index.
8170 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8171 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8172 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008173
Tanya Lattnere9e67052011-06-14 23:48:48 +00008174 SDValue ExtVec0 = N0->getOperand(i);
8175 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008176
Tanya Lattnere9e67052011-06-14 23:48:48 +00008177 // First operand is the vector, verify its the same.
8178 if (V != ExtVec0->getOperand(0).getNode() ||
8179 V != ExtVec1->getOperand(0).getNode())
8180 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008181
Tanya Lattnere9e67052011-06-14 23:48:48 +00008182 // Second is the constant, verify its correct.
8183 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8184 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008185
Tanya Lattnere9e67052011-06-14 23:48:48 +00008186 // For the constant, we want to see all the even or all the odd.
8187 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8188 || C1->getZExtValue() != nextIndex+1)
8189 return SDValue();
8190
8191 // Increment index.
8192 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008193 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008194 return SDValue();
8195 }
8196
8197 // Create VPADDL node.
8198 SelectionDAG &DAG = DCI.DAG;
8199 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008200
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008201 SDLoc dl(N);
8202
Tanya Lattnere9e67052011-06-14 23:48:48 +00008203 // Build operand list.
8204 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008205 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008206 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008207
8208 // Input is the vector.
8209 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008210
Tanya Lattnere9e67052011-06-14 23:48:48 +00008211 // Get widened type and narrowed type.
8212 MVT widenType;
8213 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008214
Silviu Barangaa3106e62014-04-03 10:44:27 +00008215 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8216 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008217 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8218 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8219 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8220 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008221 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008222 }
8223
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008224 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008225 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008226 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008227}
8228
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008229static SDValue findMUL_LOHI(SDValue V) {
8230 if (V->getOpcode() == ISD::UMUL_LOHI ||
8231 V->getOpcode() == ISD::SMUL_LOHI)
8232 return V;
8233 return SDValue();
8234}
8235
8236static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8237 TargetLowering::DAGCombinerInfo &DCI,
8238 const ARMSubtarget *Subtarget) {
8239
8240 if (Subtarget->isThumb1Only()) return SDValue();
8241
8242 // Only perform the checks after legalize when the pattern is available.
8243 if (DCI.isBeforeLegalize()) return SDValue();
8244
8245 // Look for multiply add opportunities.
8246 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8247 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8248 // a glue link from the first add to the second add.
8249 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8250 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008251 // UMUL_LOHI
8252 // / :lo \ :hi
8253 // / \ [no multiline comment]
8254 // loAdd -> ADDE |
8255 // \ :glue /
8256 // \ /
8257 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008258 //
8259 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8260 SDValue AddcOp0 = AddcNode->getOperand(0);
8261 SDValue AddcOp1 = AddcNode->getOperand(1);
8262
8263 // Check if the two operands are from the same mul_lohi node.
8264 if (AddcOp0.getNode() == AddcOp1.getNode())
8265 return SDValue();
8266
8267 assert(AddcNode->getNumValues() == 2 &&
8268 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008269 "Expect ADDC with two result values. First: i32");
8270
8271 // Check that we have a glued ADDC node.
8272 if (AddcNode->getValueType(1) != MVT::Glue)
8273 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008274
8275 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8276 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8277 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8278 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8279 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8280 return SDValue();
8281
8282 // Look for the glued ADDE.
8283 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008284 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008285 return SDValue();
8286
8287 // Make sure it is really an ADDE.
8288 if (AddeNode->getOpcode() != ISD::ADDE)
8289 return SDValue();
8290
8291 assert(AddeNode->getNumOperands() == 3 &&
8292 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8293 "ADDE node has the wrong inputs");
8294
8295 // Check for the triangle shape.
8296 SDValue AddeOp0 = AddeNode->getOperand(0);
8297 SDValue AddeOp1 = AddeNode->getOperand(1);
8298
8299 // Make sure that the ADDE operands are not coming from the same node.
8300 if (AddeOp0.getNode() == AddeOp1.getNode())
8301 return SDValue();
8302
8303 // Find the MUL_LOHI node walking up ADDE's operands.
8304 bool IsLeftOperandMUL = false;
8305 SDValue MULOp = findMUL_LOHI(AddeOp0);
8306 if (MULOp == SDValue())
8307 MULOp = findMUL_LOHI(AddeOp1);
8308 else
8309 IsLeftOperandMUL = true;
8310 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008311 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008312
8313 // Figure out the right opcode.
8314 unsigned Opc = MULOp->getOpcode();
8315 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8316
8317 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008318 SDValue* HiAdd = nullptr;
8319 SDValue* LoMul = nullptr;
8320 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008321
Jyoti Allurf1d70502015-01-23 09:10:03 +00008322 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8323 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8324 return SDValue();
8325
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008326 if (IsLeftOperandMUL)
8327 HiAdd = &AddeOp1;
8328 else
8329 HiAdd = &AddeOp0;
8330
8331
Jyoti Allurf1d70502015-01-23 09:10:03 +00008332 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8333 // whose low result is fed to the ADDC we are checking.
8334
8335 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008336 LoMul = &AddcOp0;
8337 LowAdd = &AddcOp1;
8338 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008339 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008340 LoMul = &AddcOp1;
8341 LowAdd = &AddcOp0;
8342 }
8343
Craig Topper062a2ba2014-04-25 05:30:21 +00008344 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008345 return SDValue();
8346
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008347 // Create the merged node.
8348 SelectionDAG &DAG = DCI.DAG;
8349
8350 // Build operand list.
8351 SmallVector<SDValue, 8> Ops;
8352 Ops.push_back(LoMul->getOperand(0));
8353 Ops.push_back(LoMul->getOperand(1));
8354 Ops.push_back(*LowAdd);
8355 Ops.push_back(*HiAdd);
8356
Andrew Trickef9de2a2013-05-25 02:42:55 +00008357 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008358 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008359
8360 // Replace the ADDs' nodes uses by the MLA node's values.
8361 SDValue HiMLALResult(MLALNode.getNode(), 1);
8362 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8363
8364 SDValue LoMLALResult(MLALNode.getNode(), 0);
8365 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8366
8367 // Return original node to notify the driver to stop replacing.
8368 SDValue resNode(AddcNode, 0);
8369 return resNode;
8370}
8371
8372/// PerformADDCCombine - Target-specific dag combine transform from
8373/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8374static SDValue PerformADDCCombine(SDNode *N,
8375 TargetLowering::DAGCombinerInfo &DCI,
8376 const ARMSubtarget *Subtarget) {
8377
8378 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8379
8380}
8381
Bob Wilson728eb292010-07-29 20:34:14 +00008382/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8383/// operands N0 and N1. This is a helper for PerformADDCombine that is
8384/// called with the default operands, and if that fails, with commuted
8385/// operands.
8386static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008387 TargetLowering::DAGCombinerInfo &DCI,
8388 const ARMSubtarget *Subtarget){
8389
8390 // Attempt to create vpaddl for this add.
8391 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8392 if (Result.getNode())
8393 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008394
Chris Lattner4147f082009-03-12 06:52:53 +00008395 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008396 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008397 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8398 if (Result.getNode()) return Result;
8399 }
Chris Lattner4147f082009-03-12 06:52:53 +00008400 return SDValue();
8401}
8402
Bob Wilson728eb292010-07-29 20:34:14 +00008403/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8404///
8405static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008406 TargetLowering::DAGCombinerInfo &DCI,
8407 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008408 SDValue N0 = N->getOperand(0);
8409 SDValue N1 = N->getOperand(1);
8410
8411 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008412 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008413 if (Result.getNode())
8414 return Result;
8415
8416 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008417 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008418}
8419
Chris Lattner4147f082009-03-12 06:52:53 +00008420/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008421///
Chris Lattner4147f082009-03-12 06:52:53 +00008422static SDValue PerformSUBCombine(SDNode *N,
8423 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008424 SDValue N0 = N->getOperand(0);
8425 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008426
Chris Lattner4147f082009-03-12 06:52:53 +00008427 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008428 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008429 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8430 if (Result.getNode()) return Result;
8431 }
Bob Wilson7117a912009-03-20 22:42:55 +00008432
Chris Lattner4147f082009-03-12 06:52:53 +00008433 return SDValue();
8434}
8435
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008436/// PerformVMULCombine
8437/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8438/// special multiplier accumulator forwarding.
8439/// vmul d3, d0, d2
8440/// vmla d3, d1, d2
8441/// is faster than
8442/// vadd d3, d0, d1
8443/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008444// However, for (A + B) * (A + B),
8445// vadd d2, d0, d1
8446// vmul d3, d0, d2
8447// vmla d3, d1, d2
8448// is slower than
8449// vadd d2, d0, d1
8450// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008451static SDValue PerformVMULCombine(SDNode *N,
8452 TargetLowering::DAGCombinerInfo &DCI,
8453 const ARMSubtarget *Subtarget) {
8454 if (!Subtarget->hasVMLxForwarding())
8455 return SDValue();
8456
8457 SelectionDAG &DAG = DCI.DAG;
8458 SDValue N0 = N->getOperand(0);
8459 SDValue N1 = N->getOperand(1);
8460 unsigned Opcode = N0.getOpcode();
8461 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8462 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008463 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008464 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8465 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8466 return SDValue();
8467 std::swap(N0, N1);
8468 }
8469
Weiming Zhao2052f482013-09-25 23:12:06 +00008470 if (N0 == N1)
8471 return SDValue();
8472
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008473 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008474 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008475 SDValue N00 = N0->getOperand(0);
8476 SDValue N01 = N0->getOperand(1);
8477 return DAG.getNode(Opcode, DL, VT,
8478 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8479 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8480}
8481
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008482static SDValue PerformMULCombine(SDNode *N,
8483 TargetLowering::DAGCombinerInfo &DCI,
8484 const ARMSubtarget *Subtarget) {
8485 SelectionDAG &DAG = DCI.DAG;
8486
8487 if (Subtarget->isThumb1Only())
8488 return SDValue();
8489
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008490 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8491 return SDValue();
8492
8493 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008494 if (VT.is64BitVector() || VT.is128BitVector())
8495 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008496 if (VT != MVT::i32)
8497 return SDValue();
8498
8499 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8500 if (!C)
8501 return SDValue();
8502
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008503 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008504 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008505
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008506 ShiftAmt = ShiftAmt & (32 - 1);
8507 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008508 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008509
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008510 SDValue Res;
8511 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008512
8513 if (MulAmt >= 0) {
8514 if (isPowerOf2_32(MulAmt - 1)) {
8515 // (mul x, 2^N + 1) => (add (shl x, N), x)
8516 Res = DAG.getNode(ISD::ADD, DL, VT,
8517 V,
8518 DAG.getNode(ISD::SHL, DL, VT,
8519 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008520 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008521 MVT::i32)));
8522 } else if (isPowerOf2_32(MulAmt + 1)) {
8523 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8524 Res = DAG.getNode(ISD::SUB, DL, VT,
8525 DAG.getNode(ISD::SHL, DL, VT,
8526 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008527 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008528 MVT::i32)),
8529 V);
8530 } else
8531 return SDValue();
8532 } else {
8533 uint64_t MulAmtAbs = -MulAmt;
8534 if (isPowerOf2_32(MulAmtAbs + 1)) {
8535 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8536 Res = DAG.getNode(ISD::SUB, DL, VT,
8537 V,
8538 DAG.getNode(ISD::SHL, DL, VT,
8539 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008540 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008541 MVT::i32)));
8542 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8543 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8544 Res = DAG.getNode(ISD::ADD, DL, VT,
8545 V,
8546 DAG.getNode(ISD::SHL, DL, VT,
8547 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008548 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008549 MVT::i32)));
8550 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008551 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008552
8553 } else
8554 return SDValue();
8555 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008556
8557 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008558 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008559 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008560
8561 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008562 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008563 return SDValue();
8564}
8565
Owen Anderson30c48922010-11-05 19:27:46 +00008566static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008567 TargetLowering::DAGCombinerInfo &DCI,
8568 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008569
Owen Anderson30c48922010-11-05 19:27:46 +00008570 // Attempt to use immediate-form VBIC
8571 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008572 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008573 EVT VT = N->getValueType(0);
8574 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008575
Tanya Lattner266792a2011-04-07 15:24:20 +00008576 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8577 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008578
Owen Anderson30c48922010-11-05 19:27:46 +00008579 APInt SplatBits, SplatUndef;
8580 unsigned SplatBitSize;
8581 bool HasAnyUndefs;
8582 if (BVN &&
8583 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8584 if (SplatBitSize <= 64) {
8585 EVT VbicVT;
8586 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8587 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008588 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008589 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008590 if (Val.getNode()) {
8591 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008592 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008593 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008594 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008595 }
8596 }
8597 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008598
Evan Chenge87681c2012-02-23 01:19:06 +00008599 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008600 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8601 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8602 if (Result.getNode())
8603 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008604 }
8605
Owen Anderson30c48922010-11-05 19:27:46 +00008606 return SDValue();
8607}
8608
Jim Grosbach11013ed2010-07-16 23:05:05 +00008609/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8610static SDValue PerformORCombine(SDNode *N,
8611 TargetLowering::DAGCombinerInfo &DCI,
8612 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008613 // Attempt to use immediate-form VORR
8614 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008615 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008616 EVT VT = N->getValueType(0);
8617 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008618
Tanya Lattner266792a2011-04-07 15:24:20 +00008619 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8620 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008621
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008622 APInt SplatBits, SplatUndef;
8623 unsigned SplatBitSize;
8624 bool HasAnyUndefs;
8625 if (BVN && Subtarget->hasNEON() &&
8626 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8627 if (SplatBitSize <= 64) {
8628 EVT VorrVT;
8629 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8630 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008631 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008632 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008633 if (Val.getNode()) {
8634 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008635 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008636 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008637 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008638 }
8639 }
8640 }
8641
Evan Chenge87681c2012-02-23 01:19:06 +00008642 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008643 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8644 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8645 if (Result.getNode())
8646 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008647 }
8648
Nadav Rotem3a94c542012-08-13 18:52:44 +00008649 // The code below optimizes (or (and X, Y), Z).
8650 // The AND operand needs to have a single user to make these optimizations
8651 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008652 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008653 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008654 return SDValue();
8655 SDValue N1 = N->getOperand(1);
8656
8657 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8658 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8659 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8660 APInt SplatUndef;
8661 unsigned SplatBitSize;
8662 bool HasAnyUndefs;
8663
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008664 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008665 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008666 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8667 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008668 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008669 HasAnyUndefs) && !HasAnyUndefs) {
8670 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8671 HasAnyUndefs) && !HasAnyUndefs) {
8672 // Ensure that the bit width of the constants are the same and that
8673 // the splat arguments are logical inverses as per the pattern we
8674 // are trying to simplify.
8675 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8676 SplatBits0 == ~SplatBits1) {
8677 // Canonicalize the vector type to make instruction selection
8678 // simpler.
8679 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8680 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8681 N0->getOperand(1),
8682 N0->getOperand(0),
8683 N1->getOperand(0));
8684 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8685 }
8686 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008687 }
8688 }
8689
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008690 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8691 // reasonable.
8692
Jim Grosbach11013ed2010-07-16 23:05:05 +00008693 // BFI is only available on V6T2+
8694 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8695 return SDValue();
8696
Andrew Trickef9de2a2013-05-25 02:42:55 +00008697 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008698 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008699 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008700 //
8701 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008702 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008703 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008704 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008705 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008706 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008707
Jim Grosbach11013ed2010-07-16 23:05:05 +00008708 if (VT != MVT::i32)
8709 return SDValue();
8710
Evan Cheng2e51bb42010-12-13 20:32:54 +00008711 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008712
Jim Grosbach11013ed2010-07-16 23:05:05 +00008713 // The value and the mask need to be constants so we can verify this is
8714 // actually a bitfield set. If the mask is 0xffff, we can do better
8715 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008716 SDValue MaskOp = N0.getOperand(1);
8717 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8718 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008719 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008720 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008721 if (Mask == 0xffff)
8722 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008723 SDValue Res;
8724 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008725 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8726 if (N1C) {
8727 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008728 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008729 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008730
Evan Cheng34345752010-12-11 04:11:38 +00008731 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008732 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008733
Evan Cheng2e51bb42010-12-13 20:32:54 +00008734 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008735 DAG.getConstant(Val, DL, MVT::i32),
8736 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008737
8738 // Do not add new nodes to DAG combiner worklist.
8739 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008740 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008741 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008742 } else if (N1.getOpcode() == ISD::AND) {
8743 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008744 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8745 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008746 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008747 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008748
Eric Christopherd5530962011-03-26 01:21:03 +00008749 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8750 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008751 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008752 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008753 // The pack halfword instruction works better for masks that fit it,
8754 // so use that when it's available.
8755 if (Subtarget->hasT2ExtractPack() &&
8756 (Mask == 0xffff || Mask == 0xffff0000))
8757 return SDValue();
8758 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008759 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008760 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008761 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008762 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008763 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008764 // Do not add new nodes to DAG combiner worklist.
8765 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008766 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008767 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008768 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008769 // The pack halfword instruction works better for masks that fit it,
8770 // so use that when it's available.
8771 if (Subtarget->hasT2ExtractPack() &&
8772 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8773 return SDValue();
8774 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008775 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008776 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008777 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008778 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008779 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008780 // Do not add new nodes to DAG combiner worklist.
8781 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008782 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008783 }
8784 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008785
Evan Cheng2e51bb42010-12-13 20:32:54 +00008786 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8787 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8788 ARM::isBitFieldInvertedMask(~Mask)) {
8789 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8790 // where lsb(mask) == #shamt and masked bits of B are known zero.
8791 SDValue ShAmt = N00.getOperand(1);
8792 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008793 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008794 if (ShAmtC != LSB)
8795 return SDValue();
8796
8797 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008798 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008799
8800 // Do not add new nodes to DAG combiner worklist.
8801 DCI.CombineTo(N, Res, false);
8802 }
8803
Jim Grosbach11013ed2010-07-16 23:05:05 +00008804 return SDValue();
8805}
8806
Evan Chenge87681c2012-02-23 01:19:06 +00008807static SDValue PerformXORCombine(SDNode *N,
8808 TargetLowering::DAGCombinerInfo &DCI,
8809 const ARMSubtarget *Subtarget) {
8810 EVT VT = N->getValueType(0);
8811 SelectionDAG &DAG = DCI.DAG;
8812
8813 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8814 return SDValue();
8815
8816 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008817 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8818 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8819 if (Result.getNode())
8820 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008821 }
8822
8823 return SDValue();
8824}
8825
Evan Cheng6d02d902011-06-15 01:12:31 +00008826/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8827/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008828static SDValue PerformBFICombine(SDNode *N,
8829 TargetLowering::DAGCombinerInfo &DCI) {
8830 SDValue N1 = N->getOperand(1);
8831 if (N1.getOpcode() == ISD::AND) {
8832 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8833 if (!N11C)
8834 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008835 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008836 unsigned LSB = countTrailingZeros(~InvMask);
8837 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008838 assert(Width <
8839 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008840 "undefined behavior");
8841 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008842 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008843 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008844 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008845 N->getOperand(0), N1.getOperand(0),
8846 N->getOperand(2));
8847 }
8848 return SDValue();
8849}
8850
Bob Wilson22806742010-09-22 22:09:21 +00008851/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8852/// ARMISD::VMOVRRD.
8853static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008854 TargetLowering::DAGCombinerInfo &DCI,
8855 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008856 // vmovrrd(vmovdrr x, y) -> x,y
8857 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008858 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008859 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008860
8861 // vmovrrd(load f64) -> (load i32), (load i32)
8862 SDNode *InNode = InDouble.getNode();
8863 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8864 InNode->getValueType(0) == MVT::f64 &&
8865 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8866 !cast<LoadSDNode>(InNode)->isVolatile()) {
8867 // TODO: Should this be done for non-FrameIndex operands?
8868 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8869
8870 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008871 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008872 SDValue BasePtr = LD->getBasePtr();
8873 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8874 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008875 LD->isNonTemporal(), LD->isInvariant(),
8876 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008877
8878 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008879 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008880 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8881 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008882 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008883 std::min(4U, LD->getAlignment() / 2));
8884
8885 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00008886 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00008887 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008888 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008889 return Result;
8890 }
8891
Bob Wilson22806742010-09-22 22:09:21 +00008892 return SDValue();
8893}
8894
8895/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8896/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8897static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8898 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8899 SDValue Op0 = N->getOperand(0);
8900 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008901 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008902 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008903 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008904 Op1 = Op1.getOperand(0);
8905 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8906 Op0.getNode() == Op1.getNode() &&
8907 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008908 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008909 N->getValueType(0), Op0.getOperand(0));
8910 return SDValue();
8911}
8912
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008913/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8914/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8915/// i64 vector to have f64 elements, since the value can then be loaded
8916/// directly into a VFP register.
8917static bool hasNormalLoadOperand(SDNode *N) {
8918 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8919 for (unsigned i = 0; i < NumElts; ++i) {
8920 SDNode *Elt = N->getOperand(i).getNode();
8921 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8922 return true;
8923 }
8924 return false;
8925}
8926
Bob Wilsoncb6db982010-09-17 22:59:05 +00008927/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8928/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008929static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008930 TargetLowering::DAGCombinerInfo &DCI,
8931 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008932 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8933 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8934 // into a pair of GPRs, which is fine when the value is used as a scalar,
8935 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008936 SelectionDAG &DAG = DCI.DAG;
8937 if (N->getNumOperands() == 2) {
8938 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8939 if (RV.getNode())
8940 return RV;
8941 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008942
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008943 // Load i64 elements as f64 values so that type legalization does not split
8944 // them up into i32 values.
8945 EVT VT = N->getValueType(0);
8946 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8947 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008948 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008949 SmallVector<SDValue, 8> Ops;
8950 unsigned NumElts = VT.getVectorNumElements();
8951 for (unsigned i = 0; i < NumElts; ++i) {
8952 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8953 Ops.push_back(V);
8954 // Make the DAGCombiner fold the bitcast.
8955 DCI.AddToWorklist(V.getNode());
8956 }
8957 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008958 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008959 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8960}
8961
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008962/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8963static SDValue
8964PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8965 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8966 // At that time, we may have inserted bitcasts from integer to float.
8967 // If these bitcasts have survived DAGCombine, change the lowering of this
8968 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8969 // force to use floating point types.
8970
8971 // Make sure we can change the type of the vector.
8972 // This is possible iff:
8973 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8974 // 1.1. Vector is used only once.
8975 // 1.2. Use is a bit convert to an integer type.
8976 // 2. The size of its operands are 32-bits (64-bits are not legal).
8977 EVT VT = N->getValueType(0);
8978 EVT EltVT = VT.getVectorElementType();
8979
8980 // Check 1.1. and 2.
8981 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8982 return SDValue();
8983
8984 // By construction, the input type must be float.
8985 assert(EltVT == MVT::f32 && "Unexpected type!");
8986
8987 // Check 1.2.
8988 SDNode *Use = *N->use_begin();
8989 if (Use->getOpcode() != ISD::BITCAST ||
8990 Use->getValueType(0).isFloatingPoint())
8991 return SDValue();
8992
8993 // Check profitability.
8994 // Model is, if more than half of the relevant operands are bitcast from
8995 // i32, turn the build_vector into a sequence of insert_vector_elt.
8996 // Relevant operands are everything that is not statically
8997 // (i.e., at compile time) bitcasted.
8998 unsigned NumOfBitCastedElts = 0;
8999 unsigned NumElts = VT.getVectorNumElements();
9000 unsigned NumOfRelevantElts = NumElts;
9001 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9002 SDValue Elt = N->getOperand(Idx);
9003 if (Elt->getOpcode() == ISD::BITCAST) {
9004 // Assume only bit cast to i32 will go away.
9005 if (Elt->getOperand(0).getValueType() == MVT::i32)
9006 ++NumOfBitCastedElts;
9007 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
9008 // Constants are statically casted, thus do not count them as
9009 // relevant operands.
9010 --NumOfRelevantElts;
9011 }
9012
9013 // Check if more than half of the elements require a non-free bitcast.
9014 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9015 return SDValue();
9016
9017 SelectionDAG &DAG = DCI.DAG;
9018 // Create the new vector type.
9019 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9020 // Check if the type is legal.
9021 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9022 if (!TLI.isTypeLegal(VecVT))
9023 return SDValue();
9024
9025 // Combine:
9026 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9027 // => BITCAST INSERT_VECTOR_ELT
9028 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9029 // (BITCAST EN), N.
9030 SDValue Vec = DAG.getUNDEF(VecVT);
9031 SDLoc dl(N);
9032 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9033 SDValue V = N->getOperand(Idx);
9034 if (V.getOpcode() == ISD::UNDEF)
9035 continue;
9036 if (V.getOpcode() == ISD::BITCAST &&
9037 V->getOperand(0).getValueType() == MVT::i32)
9038 // Fold obvious case.
9039 V = V.getOperand(0);
9040 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009041 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009042 // Make the DAGCombiner fold the bitcasts.
9043 DCI.AddToWorklist(V.getNode());
9044 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009045 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009046 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9047 }
9048 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9049 // Make the DAGCombiner fold the bitcasts.
9050 DCI.AddToWorklist(Vec.getNode());
9051 return Vec;
9052}
9053
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009054/// PerformInsertEltCombine - Target-specific dag combine xforms for
9055/// ISD::INSERT_VECTOR_ELT.
9056static SDValue PerformInsertEltCombine(SDNode *N,
9057 TargetLowering::DAGCombinerInfo &DCI) {
9058 // Bitcast an i64 load inserted into a vector to f64.
9059 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9060 EVT VT = N->getValueType(0);
9061 SDNode *Elt = N->getOperand(1).getNode();
9062 if (VT.getVectorElementType() != MVT::i64 ||
9063 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9064 return SDValue();
9065
9066 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009067 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009068 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9069 VT.getVectorNumElements());
9070 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9071 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9072 // Make the DAGCombiner fold the bitcasts.
9073 DCI.AddToWorklist(Vec.getNode());
9074 DCI.AddToWorklist(V.getNode());
9075 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9076 Vec, V, N->getOperand(2));
9077 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009078}
9079
Bob Wilsonc7334a12010-10-27 20:38:28 +00009080/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9081/// ISD::VECTOR_SHUFFLE.
9082static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9083 // The LLVM shufflevector instruction does not require the shuffle mask
9084 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9085 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9086 // operands do not match the mask length, they are extended by concatenating
9087 // them with undef vectors. That is probably the right thing for other
9088 // targets, but for NEON it is better to concatenate two double-register
9089 // size vector operands into a single quad-register size vector. Do that
9090 // transformation here:
9091 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9092 // shuffle(concat(v1, v2), undef)
9093 SDValue Op0 = N->getOperand(0);
9094 SDValue Op1 = N->getOperand(1);
9095 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9096 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9097 Op0.getNumOperands() != 2 ||
9098 Op1.getNumOperands() != 2)
9099 return SDValue();
9100 SDValue Concat0Op1 = Op0.getOperand(1);
9101 SDValue Concat1Op1 = Op1.getOperand(1);
9102 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9103 Concat1Op1.getOpcode() != ISD::UNDEF)
9104 return SDValue();
9105 // Skip the transformation if any of the types are illegal.
9106 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9107 EVT VT = N->getValueType(0);
9108 if (!TLI.isTypeLegal(VT) ||
9109 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9110 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9111 return SDValue();
9112
Andrew Trickef9de2a2013-05-25 02:42:55 +00009113 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009114 Op0.getOperand(0), Op1.getOperand(0));
9115 // Translate the shuffle mask.
9116 SmallVector<int, 16> NewMask;
9117 unsigned NumElts = VT.getVectorNumElements();
9118 unsigned HalfElts = NumElts/2;
9119 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9120 for (unsigned n = 0; n < NumElts; ++n) {
9121 int MaskElt = SVN->getMaskElt(n);
9122 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009123 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009124 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009125 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009126 NewElt = HalfElts + MaskElt - NumElts;
9127 NewMask.push_back(NewElt);
9128 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009129 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009130 DAG.getUNDEF(VT), NewMask.data());
9131}
9132
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009133/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9134/// NEON load/store intrinsics, and generic vector load/stores, to merge
9135/// base address updates.
9136/// For generic load/stores, the memory type is assumed to be a vector.
9137/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009138static SDValue CombineBaseUpdate(SDNode *N,
9139 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009140 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009141 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9142 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009143 const bool isStore = N->getOpcode() == ISD::STORE;
9144 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009145 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009146 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009147 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009148
9149 // Search for a use of the address operand that is an increment.
9150 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9151 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9152 SDNode *User = *UI;
9153 if (User->getOpcode() != ISD::ADD ||
9154 UI.getUse().getResNo() != Addr.getResNo())
9155 continue;
9156
9157 // Check that the add is independent of the load/store. Otherwise, folding
9158 // it would create a cycle.
9159 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9160 continue;
9161
9162 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009163 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009164 bool isLaneOp = false;
9165 unsigned NewOpc = 0;
9166 unsigned NumVecs = 0;
9167 if (isIntrinsic) {
9168 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9169 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009170 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009171 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9172 NumVecs = 1; break;
9173 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9174 NumVecs = 2; break;
9175 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9176 NumVecs = 3; break;
9177 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9178 NumVecs = 4; break;
9179 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9180 NumVecs = 2; isLaneOp = true; break;
9181 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9182 NumVecs = 3; isLaneOp = true; break;
9183 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9184 NumVecs = 4; isLaneOp = true; break;
9185 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009186 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009187 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009188 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009189 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009190 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009191 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009192 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009193 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009194 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009195 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009196 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009197 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009198 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009199 }
9200 } else {
9201 isLaneOp = true;
9202 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009203 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009204 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9205 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9206 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009207 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9208 NumVecs = 1; isLaneOp = false; break;
9209 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9210 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009211 }
9212 }
9213
9214 // Find the size of memory referenced by the load/store.
9215 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009216 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009217 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009218 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009219 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009220 } else {
9221 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9222 VecTy = N->getOperand(1).getValueType();
9223 }
9224
Bob Wilson06fce872011-02-07 17:43:21 +00009225 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9226 if (isLaneOp)
9227 NumBytes /= VecTy.getVectorNumElements();
9228
9229 // If the increment is a constant, it must match the memory ref size.
9230 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9231 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9232 uint64_t IncVal = CInc->getZExtValue();
9233 if (IncVal != NumBytes)
9234 continue;
9235 } else if (NumBytes >= 3 * 16) {
9236 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9237 // separate instructions that make it harder to use a non-constant update.
9238 continue;
9239 }
9240
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009241 // OK, we found an ADD we can fold into the base update.
9242 // Now, create a _UPD node, taking care of not breaking alignment.
9243
9244 EVT AlignedVecTy = VecTy;
9245 unsigned Alignment = MemN->getAlignment();
9246
9247 // If this is a less-than-standard-aligned load/store, change the type to
9248 // match the standard alignment.
9249 // The alignment is overlooked when selecting _UPD variants; and it's
9250 // easier to introduce bitcasts here than fix that.
9251 // There are 3 ways to get to this base-update combine:
9252 // - intrinsics: they are assumed to be properly aligned (to the standard
9253 // alignment of the memory type), so we don't need to do anything.
9254 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9255 // intrinsics, so, likewise, there's nothing to do.
9256 // - generic load/store instructions: the alignment is specified as an
9257 // explicit operand, rather than implicitly as the standard alignment
9258 // of the memory type (like the intrisics). We need to change the
9259 // memory type to match the explicit alignment. That way, we don't
9260 // generate non-standard-aligned ARMISD::VLDx nodes.
9261 if (isa<LSBaseSDNode>(N)) {
9262 if (Alignment == 0)
9263 Alignment = 1;
9264 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9265 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9266 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9267 assert(!isLaneOp && "Unexpected generic load/store lane.");
9268 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9269 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9270 }
9271 // Don't set an explicit alignment on regular load/stores that we want
9272 // to transform to VLD/VST 1_UPD nodes.
9273 // This matches the behavior of regular load/stores, which only get an
9274 // explicit alignment if the MMO alignment is larger than the standard
9275 // alignment of the memory type.
9276 // Intrinsics, however, always get an explicit alignment, set to the
9277 // alignment of the MMO.
9278 Alignment = 1;
9279 }
9280
Bob Wilson06fce872011-02-07 17:43:21 +00009281 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009282 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009283 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009284 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009285 unsigned n;
9286 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009287 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009288 Tys[n++] = MVT::i32;
9289 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009290 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009291
9292 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009293 SmallVector<SDValue, 8> Ops;
9294 Ops.push_back(N->getOperand(0)); // incoming chain
9295 Ops.push_back(N->getOperand(AddrOpIdx));
9296 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009297
9298 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9299 // Try to match the intrinsic's signature
9300 Ops.push_back(StN->getValue());
9301 } else {
9302 // Loads (and of course intrinsics) match the intrinsics' signature,
9303 // so just add all but the alignment operand.
9304 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9305 Ops.push_back(N->getOperand(i));
9306 }
9307
9308 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009309 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009310
9311 // If this is a non-standard-aligned STORE, the penultimate operand is the
9312 // stored value. Bitcast it to the aligned type.
9313 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9314 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009315 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009316 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009317
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009318 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009319 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009320 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009321
9322 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009323 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009324 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009325 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009326
9327 // If this is an non-standard-aligned LOAD, the first result is the loaded
9328 // value. Bitcast it to the expected result type.
9329 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9330 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009331 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009332 }
9333
Bob Wilson06fce872011-02-07 17:43:21 +00009334 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9335 DCI.CombineTo(N, NewResults);
9336 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9337
9338 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009339 }
Bob Wilson06fce872011-02-07 17:43:21 +00009340 return SDValue();
9341}
9342
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009343static SDValue PerformVLDCombine(SDNode *N,
9344 TargetLowering::DAGCombinerInfo &DCI) {
9345 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9346 return SDValue();
9347
9348 return CombineBaseUpdate(N, DCI);
9349}
9350
Bob Wilson2d790df2010-11-28 06:51:26 +00009351/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9352/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9353/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9354/// return true.
9355static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9356 SelectionDAG &DAG = DCI.DAG;
9357 EVT VT = N->getValueType(0);
9358 // vldN-dup instructions only support 64-bit vectors for N > 1.
9359 if (!VT.is64BitVector())
9360 return false;
9361
9362 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9363 SDNode *VLD = N->getOperand(0).getNode();
9364 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9365 return false;
9366 unsigned NumVecs = 0;
9367 unsigned NewOpc = 0;
9368 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9369 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9370 NumVecs = 2;
9371 NewOpc = ARMISD::VLD2DUP;
9372 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9373 NumVecs = 3;
9374 NewOpc = ARMISD::VLD3DUP;
9375 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9376 NumVecs = 4;
9377 NewOpc = ARMISD::VLD4DUP;
9378 } else {
9379 return false;
9380 }
9381
9382 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9383 // numbers match the load.
9384 unsigned VLDLaneNo =
9385 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9386 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9387 UI != UE; ++UI) {
9388 // Ignore uses of the chain result.
9389 if (UI.getUse().getResNo() == NumVecs)
9390 continue;
9391 SDNode *User = *UI;
9392 if (User->getOpcode() != ARMISD::VDUPLANE ||
9393 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9394 return false;
9395 }
9396
9397 // Create the vldN-dup node.
9398 EVT Tys[5];
9399 unsigned n;
9400 for (n = 0; n < NumVecs; ++n)
9401 Tys[n] = VT;
9402 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009403 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009404 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9405 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009406 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009407 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009408 VLDMemInt->getMemOperand());
9409
9410 // Update the uses.
9411 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9412 UI != UE; ++UI) {
9413 unsigned ResNo = UI.getUse().getResNo();
9414 // Ignore uses of the chain result.
9415 if (ResNo == NumVecs)
9416 continue;
9417 SDNode *User = *UI;
9418 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9419 }
9420
9421 // Now the vldN-lane intrinsic is dead except for its chain result.
9422 // Update uses of the chain.
9423 std::vector<SDValue> VLDDupResults;
9424 for (unsigned n = 0; n < NumVecs; ++n)
9425 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9426 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9427 DCI.CombineTo(VLD, VLDDupResults);
9428
9429 return true;
9430}
9431
Bob Wilson103a0dc2010-07-14 01:22:12 +00009432/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9433/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009434static SDValue PerformVDUPLANECombine(SDNode *N,
9435 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009436 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009437
Bob Wilson2d790df2010-11-28 06:51:26 +00009438 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9439 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9440 if (CombineVLDDUP(N, DCI))
9441 return SDValue(N, 0);
9442
9443 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9444 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009445 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009446 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009447 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009448 return SDValue();
9449
9450 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9451 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9452 // The canonical VMOV for a zero vector uses a 32-bit element size.
9453 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9454 unsigned EltBits;
9455 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9456 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009457 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009458 if (EltSize > VT.getVectorElementType().getSizeInBits())
9459 return SDValue();
9460
Andrew Trickef9de2a2013-05-25 02:42:55 +00009461 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009462}
9463
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009464static SDValue PerformLOADCombine(SDNode *N,
9465 TargetLowering::DAGCombinerInfo &DCI) {
9466 EVT VT = N->getValueType(0);
9467
9468 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9469 if (ISD::isNormalLoad(N) && VT.isVector() &&
9470 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9471 return CombineBaseUpdate(N, DCI);
9472
9473 return SDValue();
9474}
9475
Ahmed Bougacha23167462014-12-09 21:26:53 +00009476/// PerformSTORECombine - Target-specific dag combine xforms for
9477/// ISD::STORE.
9478static SDValue PerformSTORECombine(SDNode *N,
9479 TargetLowering::DAGCombinerInfo &DCI) {
9480 StoreSDNode *St = cast<StoreSDNode>(N);
9481 if (St->isVolatile())
9482 return SDValue();
9483
9484 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9485 // pack all of the elements in one place. Next, store to memory in fewer
9486 // chunks.
9487 SDValue StVal = St->getValue();
9488 EVT VT = StVal.getValueType();
9489 if (St->isTruncatingStore() && VT.isVector()) {
9490 SelectionDAG &DAG = DCI.DAG;
9491 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9492 EVT StVT = St->getMemoryVT();
9493 unsigned NumElems = VT.getVectorNumElements();
9494 assert(StVT != VT && "Cannot truncate to the same type");
9495 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9496 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9497
9498 // From, To sizes and ElemCount must be pow of two
9499 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9500
9501 // We are going to use the original vector elt for storing.
9502 // Accumulated smaller vector elements must be a multiple of the store size.
9503 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9504
9505 unsigned SizeRatio = FromEltSz / ToEltSz;
9506 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9507
9508 // Create a type on which we perform the shuffle.
9509 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9510 NumElems*SizeRatio);
9511 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9512
9513 SDLoc DL(St);
9514 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9515 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9516 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009517 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9518 ? (i + 1) * SizeRatio - 1
9519 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009520
9521 // Can't shuffle using an illegal type.
9522 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9523
9524 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9525 DAG.getUNDEF(WideVec.getValueType()),
9526 ShuffleVec.data());
9527 // At this point all of the data is stored at the bottom of the
9528 // register. We now need to save it to mem.
9529
9530 // Find the largest store unit
9531 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009532 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009533 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9534 StoreType = Tp;
9535 }
9536 // Didn't find a legal store type.
9537 if (!TLI.isTypeLegal(StoreType))
9538 return SDValue();
9539
9540 // Bitcast the original vector into a vector of store-size units
9541 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9542 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9543 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9544 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9545 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009546 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9547 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009548 SDValue BasePtr = St->getBasePtr();
9549
9550 // Perform one or more big stores into memory.
9551 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9552 for (unsigned I = 0; I < E; I++) {
9553 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9554 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009555 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009556 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9557 St->getPointerInfo(), St->isVolatile(),
9558 St->isNonTemporal(), St->getAlignment());
9559 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9560 Increment);
9561 Chains.push_back(Ch);
9562 }
9563 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9564 }
9565
9566 if (!ISD::isNormalStore(St))
9567 return SDValue();
9568
9569 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9570 // ARM stores of arguments in the same cache line.
9571 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9572 StVal.getNode()->hasOneUse()) {
9573 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009574 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009575 SDLoc DL(St);
9576 SDValue BasePtr = St->getBasePtr();
9577 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9578 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9579 BasePtr, St->getPointerInfo(), St->isVolatile(),
9580 St->isNonTemporal(), St->getAlignment());
9581
9582 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009583 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009584 return DAG.getStore(NewST1.getValue(0), DL,
9585 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9586 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9587 St->isNonTemporal(),
9588 std::min(4U, St->getAlignment() / 2));
9589 }
9590
9591 if (StVal.getValueType() == MVT::i64 &&
9592 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9593
9594 // Bitcast an i64 store extracted from a vector to f64.
9595 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9596 SelectionDAG &DAG = DCI.DAG;
9597 SDLoc dl(StVal);
9598 SDValue IntVec = StVal.getOperand(0);
9599 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9600 IntVec.getValueType().getVectorNumElements());
9601 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9602 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9603 Vec, StVal.getOperand(1));
9604 dl = SDLoc(N);
9605 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9606 // Make the DAGCombiner fold the bitcasts.
9607 DCI.AddToWorklist(Vec.getNode());
9608 DCI.AddToWorklist(ExtElt.getNode());
9609 DCI.AddToWorklist(V.getNode());
9610 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9611 St->getPointerInfo(), St->isVolatile(),
9612 St->isNonTemporal(), St->getAlignment(),
9613 St->getAAInfo());
9614 }
9615
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009616 // If this is a legal vector store, try to combine it into a VST1_UPD.
9617 if (ISD::isNormalStore(N) && VT.isVector() &&
9618 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9619 return CombineBaseUpdate(N, DCI);
9620
Ahmed Bougacha23167462014-12-09 21:26:53 +00009621 return SDValue();
9622}
9623
Eric Christopher1b8b94192011-06-29 21:10:36 +00009624// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009625// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9626static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9627{
Chad Rosier6b610b32011-06-28 17:26:57 +00009628 integerPart cN;
9629 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009630 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9631 I != E; I++) {
9632 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9633 if (!C)
9634 return false;
9635
Eric Christopher1b8b94192011-06-29 21:10:36 +00009636 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009637 APFloat APF = C->getValueAPF();
9638 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9639 != APFloat::opOK || !isExact)
9640 return false;
9641
9642 c0 = (I == 0) ? cN : c0;
9643 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9644 return false;
9645 }
9646 C = c0;
9647 return true;
9648}
9649
9650/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9651/// can replace combinations of VMUL and VCVT (floating-point to integer)
9652/// when the VMUL has a constant operand that is a power of 2.
9653///
9654/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9655/// vmul.f32 d16, d17, d16
9656/// vcvt.s32.f32 d16, d16
9657/// becomes:
9658/// vcvt.s32.f32 d16, d16, #3
9659static SDValue PerformVCVTCombine(SDNode *N,
9660 TargetLowering::DAGCombinerInfo &DCI,
9661 const ARMSubtarget *Subtarget) {
9662 SelectionDAG &DAG = DCI.DAG;
9663 SDValue Op = N->getOperand(0);
9664
9665 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9666 Op.getOpcode() != ISD::FMUL)
9667 return SDValue();
9668
9669 uint64_t C;
9670 SDValue N0 = Op->getOperand(0);
9671 SDValue ConstVec = Op->getOperand(1);
9672 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9673
Eric Christopher1b8b94192011-06-29 21:10:36 +00009674 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009675 !isConstVecPow2(ConstVec, isSigned, C))
9676 return SDValue();
9677
Tim Northover7cbc2152013-06-28 15:29:25 +00009678 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9679 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009680 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9681 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9682 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009683 // These instructions only exist converting from f32 to i32. We can handle
9684 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009685 // be lossy. We also can't handle more then 4 lanes, since these intructions
9686 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009687 return SDValue();
9688 }
9689
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009690 SDLoc dl(N);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009691 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9692 Intrinsic::arm_neon_vcvtfp2fxu;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009693 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Tim Northover7cbc2152013-06-28 15:29:25 +00009694 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009695 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9696 N0,
9697 DAG.getConstant(Log2_64(C), dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009698
9699 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009700 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009701
9702 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009703}
9704
9705/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9706/// can replace combinations of VCVT (integer to floating-point) and VDIV
9707/// when the VDIV has a constant operand that is a power of 2.
9708///
9709/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9710/// vcvt.f32.s32 d16, d16
9711/// vdiv.f32 d16, d17, d16
9712/// becomes:
9713/// vcvt.f32.s32 d16, d16, #3
9714static SDValue PerformVDIVCombine(SDNode *N,
9715 TargetLowering::DAGCombinerInfo &DCI,
9716 const ARMSubtarget *Subtarget) {
9717 SelectionDAG &DAG = DCI.DAG;
9718 SDValue Op = N->getOperand(0);
9719 unsigned OpOpcode = Op.getNode()->getOpcode();
9720
9721 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9722 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9723 return SDValue();
9724
9725 uint64_t C;
9726 SDValue ConstVec = N->getOperand(1);
9727 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9728
9729 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9730 !isConstVecPow2(ConstVec, isSigned, C))
9731 return SDValue();
9732
Tim Northover7cbc2152013-06-28 15:29:25 +00009733 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9734 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9735 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9736 // These instructions only exist converting from i32 to f32. We can handle
9737 // smaller integers by generating an extra extend, but larger ones would
9738 // be lossy.
9739 return SDValue();
9740 }
9741
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009742 SDLoc dl(N);
Tim Northover7cbc2152013-06-28 15:29:25 +00009743 SDValue ConvInput = Op.getOperand(0);
9744 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9745 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9746 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009747 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009748 ConvInput);
9749
Eric Christopher1b8b94192011-06-29 21:10:36 +00009750 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009751 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009752 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009753 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009754 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9755 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009756}
9757
9758/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009759/// operand of a vector shift operation, where all the elements of the
9760/// build_vector must have the same constant integer value.
9761static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9762 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009763 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009764 Op = Op.getOperand(0);
9765 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9766 APInt SplatBits, SplatUndef;
9767 unsigned SplatBitSize;
9768 bool HasAnyUndefs;
9769 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9770 HasAnyUndefs, ElementBits) ||
9771 SplatBitSize > ElementBits)
9772 return false;
9773 Cnt = SplatBits.getSExtValue();
9774 return true;
9775}
9776
9777/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9778/// operand of a vector shift left operation. That value must be in the range:
9779/// 0 <= Value < ElementBits for a left shift; or
9780/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009781static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009782 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009783 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009784 if (! getVShiftImm(Op, ElementBits, Cnt))
9785 return false;
9786 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9787}
9788
9789/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9790/// operand of a vector shift right operation. For a shift opcode, the value
9791/// is positive, but for an intrinsic the value count must be negative. The
9792/// absolute value must be in the range:
9793/// 1 <= |Value| <= ElementBits for a right shift; or
9794/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009795static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009796 int64_t &Cnt) {
9797 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009798 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009799 if (! getVShiftImm(Op, ElementBits, Cnt))
9800 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009801 if (!isIntrinsic)
9802 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9803 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009804 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009805 return true;
9806 }
9807 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +00009808}
9809
9810/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9811static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9812 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9813 switch (IntNo) {
9814 default:
9815 // Don't do anything for most intrinsics.
9816 break;
9817
James Molloya6702e22015-07-17 17:10:55 +00009818 case Intrinsic::arm_neon_vabds:
9819 if (!N->getValueType(0).isInteger())
9820 return SDValue();
9821 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0),
9822 N->getOperand(1), N->getOperand(2));
9823 case Intrinsic::arm_neon_vabdu:
9824 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0),
9825 N->getOperand(1), N->getOperand(2));
9826
Bob Wilson2e076c42009-06-22 23:27:02 +00009827 // Vector shifts: check for immediate versions and lower them.
9828 // Note: This is done during DAG combining instead of DAG legalizing because
9829 // the build_vectors for 64-bit vector element shift counts are generally
9830 // not legal, and it is hard to see their values after they get legalized to
9831 // loads from a constant pool.
9832 case Intrinsic::arm_neon_vshifts:
9833 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009834 case Intrinsic::arm_neon_vrshifts:
9835 case Intrinsic::arm_neon_vrshiftu:
9836 case Intrinsic::arm_neon_vrshiftn:
9837 case Intrinsic::arm_neon_vqshifts:
9838 case Intrinsic::arm_neon_vqshiftu:
9839 case Intrinsic::arm_neon_vqshiftsu:
9840 case Intrinsic::arm_neon_vqshiftns:
9841 case Intrinsic::arm_neon_vqshiftnu:
9842 case Intrinsic::arm_neon_vqshiftnsu:
9843 case Intrinsic::arm_neon_vqrshiftns:
9844 case Intrinsic::arm_neon_vqrshiftnu:
9845 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009846 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009847 int64_t Cnt;
9848 unsigned VShiftOpc = 0;
9849
9850 switch (IntNo) {
9851 case Intrinsic::arm_neon_vshifts:
9852 case Intrinsic::arm_neon_vshiftu:
9853 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9854 VShiftOpc = ARMISD::VSHL;
9855 break;
9856 }
9857 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9858 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9859 ARMISD::VSHRs : ARMISD::VSHRu);
9860 break;
9861 }
9862 return SDValue();
9863
Bob Wilson2e076c42009-06-22 23:27:02 +00009864 case Intrinsic::arm_neon_vrshifts:
9865 case Intrinsic::arm_neon_vrshiftu:
9866 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9867 break;
9868 return SDValue();
9869
9870 case Intrinsic::arm_neon_vqshifts:
9871 case Intrinsic::arm_neon_vqshiftu:
9872 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9873 break;
9874 return SDValue();
9875
9876 case Intrinsic::arm_neon_vqshiftsu:
9877 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9878 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009879 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009880
Bob Wilson2e076c42009-06-22 23:27:02 +00009881 case Intrinsic::arm_neon_vrshiftn:
9882 case Intrinsic::arm_neon_vqshiftns:
9883 case Intrinsic::arm_neon_vqshiftnu:
9884 case Intrinsic::arm_neon_vqshiftnsu:
9885 case Intrinsic::arm_neon_vqrshiftns:
9886 case Intrinsic::arm_neon_vqrshiftnu:
9887 case Intrinsic::arm_neon_vqrshiftnsu:
9888 // Narrowing shifts require an immediate right shift.
9889 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9890 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009891 llvm_unreachable("invalid shift count for narrowing vector shift "
9892 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009893
9894 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009895 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009896 }
9897
9898 switch (IntNo) {
9899 case Intrinsic::arm_neon_vshifts:
9900 case Intrinsic::arm_neon_vshiftu:
9901 // Opcode already set above.
9902 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009903 case Intrinsic::arm_neon_vrshifts:
9904 VShiftOpc = ARMISD::VRSHRs; break;
9905 case Intrinsic::arm_neon_vrshiftu:
9906 VShiftOpc = ARMISD::VRSHRu; break;
9907 case Intrinsic::arm_neon_vrshiftn:
9908 VShiftOpc = ARMISD::VRSHRN; break;
9909 case Intrinsic::arm_neon_vqshifts:
9910 VShiftOpc = ARMISD::VQSHLs; break;
9911 case Intrinsic::arm_neon_vqshiftu:
9912 VShiftOpc = ARMISD::VQSHLu; break;
9913 case Intrinsic::arm_neon_vqshiftsu:
9914 VShiftOpc = ARMISD::VQSHLsu; break;
9915 case Intrinsic::arm_neon_vqshiftns:
9916 VShiftOpc = ARMISD::VQSHRNs; break;
9917 case Intrinsic::arm_neon_vqshiftnu:
9918 VShiftOpc = ARMISD::VQSHRNu; break;
9919 case Intrinsic::arm_neon_vqshiftnsu:
9920 VShiftOpc = ARMISD::VQSHRNsu; break;
9921 case Intrinsic::arm_neon_vqrshiftns:
9922 VShiftOpc = ARMISD::VQRSHRNs; break;
9923 case Intrinsic::arm_neon_vqrshiftnu:
9924 VShiftOpc = ARMISD::VQRSHRNu; break;
9925 case Intrinsic::arm_neon_vqrshiftnsu:
9926 VShiftOpc = ARMISD::VQRSHRNsu; break;
9927 }
9928
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009929 SDLoc dl(N);
9930 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
9931 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009932 }
9933
9934 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009935 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009936 int64_t Cnt;
9937 unsigned VShiftOpc = 0;
9938
9939 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9940 VShiftOpc = ARMISD::VSLI;
9941 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9942 VShiftOpc = ARMISD::VSRI;
9943 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009944 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009945 }
9946
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009947 SDLoc dl(N);
9948 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009949 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009950 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009951 }
9952
9953 case Intrinsic::arm_neon_vqrshifts:
9954 case Intrinsic::arm_neon_vqrshiftu:
9955 // No immediate versions of these to check for.
9956 break;
9957 }
9958
9959 return SDValue();
9960}
9961
9962/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9963/// lowers them. As with the vector shift intrinsics, this is done during DAG
9964/// combining instead of DAG legalizing because the build_vectors for 64-bit
9965/// vector element shift counts are generally not legal, and it is hard to see
9966/// their values after they get legalized to loads from a constant pool.
9967static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9968 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009969 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009970 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9971 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9972 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9973 SDValue N1 = N->getOperand(1);
9974 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9975 SDValue N0 = N->getOperand(0);
9976 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9977 DAG.MaskedValueIsZero(N0.getOperand(0),
9978 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009979 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009980 }
9981 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009982
9983 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009984 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9985 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009986 return SDValue();
9987
9988 assert(ST->hasNEON() && "unexpected vector shift");
9989 int64_t Cnt;
9990
9991 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009992 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009993
9994 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009995 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
9996 SDLoc dl(N);
9997 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
9998 DAG.getConstant(Cnt, dl, MVT::i32));
9999 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010000 break;
10001
10002 case ISD::SRA:
10003 case ISD::SRL:
10004 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10005 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10006 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010007 SDLoc dl(N);
10008 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10009 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010010 }
10011 }
10012 return SDValue();
10013}
10014
10015/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10016/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10017static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10018 const ARMSubtarget *ST) {
10019 SDValue N0 = N->getOperand(0);
10020
10021 // Check for sign- and zero-extensions of vector extract operations of 8-
10022 // and 16-bit vector elements. NEON supports these directly. They are
10023 // handled during DAG combining because type legalization will promote them
10024 // to 32-bit types and it is messy to recognize the operations after that.
10025 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10026 SDValue Vec = N0.getOperand(0);
10027 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010028 EVT VT = N->getValueType(0);
10029 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010030 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10031
Owen Anderson9f944592009-08-11 20:47:22 +000010032 if (VT == MVT::i32 &&
10033 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010034 TLI.isTypeLegal(Vec.getValueType()) &&
10035 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010036
10037 unsigned Opc = 0;
10038 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010039 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010040 case ISD::SIGN_EXTEND:
10041 Opc = ARMISD::VGETLANEs;
10042 break;
10043 case ISD::ZERO_EXTEND:
10044 case ISD::ANY_EXTEND:
10045 Opc = ARMISD::VGETLANEu;
10046 break;
10047 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010048 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010049 }
10050 }
10051
10052 return SDValue();
10053}
10054
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010055/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
10056/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
10057static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
10058 const ARMSubtarget *ST) {
10059 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +000010060 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010061 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
10062 // a NaN; only do the transformation when it matches that behavior.
10063
10064 // For now only do this when using NEON for FP operations; if using VFP, it
10065 // is not obvious that the benefit outweighs the cost of switching to the
10066 // NEON pipeline.
10067 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
10068 N->getValueType(0) != MVT::f32)
10069 return SDValue();
10070
10071 SDValue CondLHS = N->getOperand(0);
10072 SDValue CondRHS = N->getOperand(1);
10073 SDValue LHS = N->getOperand(2);
10074 SDValue RHS = N->getOperand(3);
10075 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
10076
10077 unsigned Opcode = 0;
10078 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +000010079 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010080 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +000010081 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010082 IsReversed = true ; // x CC y ? y : x
10083 } else {
10084 return SDValue();
10085 }
10086
Bob Wilsonba8ac742010-02-24 22:15:53 +000010087 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010088 switch (CC) {
10089 default: break;
10090 case ISD::SETOLT:
10091 case ISD::SETOLE:
10092 case ISD::SETLT:
10093 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010094 case ISD::SETULT:
10095 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +000010096 // If LHS is NaN, an ordered comparison will be false and the result will
10097 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
10098 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
10099 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
10100 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
10101 break;
10102 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
10103 // will return -0, so vmin can only be used for unsafe math or if one of
10104 // the operands is known to be nonzero.
10105 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +000010106 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +000010107 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10108 break;
10109 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010110 break;
10111
10112 case ISD::SETOGT:
10113 case ISD::SETOGE:
10114 case ISD::SETGT:
10115 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010116 case ISD::SETUGT:
10117 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +000010118 // If LHS is NaN, an ordered comparison will be false and the result will
10119 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
10120 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
10121 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
10122 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
10123 break;
10124 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
10125 // will return +0, so vmax can only be used for unsafe math or if one of
10126 // the operands is known to be nonzero.
10127 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +000010128 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +000010129 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10130 break;
10131 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010132 break;
10133 }
10134
10135 if (!Opcode)
10136 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010137 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010138}
10139
Evan Chengf863e3f2011-07-13 00:42:17 +000010140/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10141SDValue
10142ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10143 SDValue Cmp = N->getOperand(4);
10144 if (Cmp.getOpcode() != ARMISD::CMPZ)
10145 // Only looking at EQ and NE cases.
10146 return SDValue();
10147
10148 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010149 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010150 SDValue LHS = Cmp.getOperand(0);
10151 SDValue RHS = Cmp.getOperand(1);
10152 SDValue FalseVal = N->getOperand(0);
10153 SDValue TrueVal = N->getOperand(1);
10154 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010155 ARMCC::CondCodes CC =
10156 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010157
10158 // Simplify
10159 // mov r1, r0
10160 // cmp r1, x
10161 // mov r0, y
10162 // moveq r0, x
10163 // to
10164 // cmp r0, x
10165 // movne r0, y
10166 //
10167 // mov r1, r0
10168 // cmp r1, x
10169 // mov r0, x
10170 // movne r0, y
10171 // to
10172 // cmp r0, x
10173 // movne r0, y
10174 /// FIXME: Turn this into a target neutral optimization?
10175 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010176 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010177 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10178 N->getOperand(3), Cmp);
10179 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10180 SDValue ARMcc;
10181 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10182 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10183 N->getOperand(3), NewCmp);
10184 }
10185
10186 if (Res.getNode()) {
10187 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010188 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010189 // Capture demanded bits information that would be otherwise lost.
10190 if (KnownZero == 0xfffffffe)
10191 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10192 DAG.getValueType(MVT::i1));
10193 else if (KnownZero == 0xffffff00)
10194 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10195 DAG.getValueType(MVT::i8));
10196 else if (KnownZero == 0xffff0000)
10197 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10198 DAG.getValueType(MVT::i16));
10199 }
10200
10201 return Res;
10202}
10203
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010204SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010205 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010206 switch (N->getOpcode()) {
10207 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010208 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010209 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010210 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010211 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010212 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010213 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10214 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010215 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010216 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010217 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010218 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010219 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010220 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010221 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010222 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010223 case ISD::FP_TO_SINT:
10224 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
10225 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010226 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010227 case ISD::SHL:
10228 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010229 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010230 case ISD::SIGN_EXTEND:
10231 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010232 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
10233 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010234 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010235 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010236 case ARMISD::VLD2DUP:
10237 case ARMISD::VLD3DUP:
10238 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010239 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010240 case ARMISD::BUILD_VECTOR:
10241 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010242 case ISD::INTRINSIC_VOID:
10243 case ISD::INTRINSIC_W_CHAIN:
10244 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10245 case Intrinsic::arm_neon_vld1:
10246 case Intrinsic::arm_neon_vld2:
10247 case Intrinsic::arm_neon_vld3:
10248 case Intrinsic::arm_neon_vld4:
10249 case Intrinsic::arm_neon_vld2lane:
10250 case Intrinsic::arm_neon_vld3lane:
10251 case Intrinsic::arm_neon_vld4lane:
10252 case Intrinsic::arm_neon_vst1:
10253 case Intrinsic::arm_neon_vst2:
10254 case Intrinsic::arm_neon_vst3:
10255 case Intrinsic::arm_neon_vst4:
10256 case Intrinsic::arm_neon_vst2lane:
10257 case Intrinsic::arm_neon_vst3lane:
10258 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010259 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010260 default: break;
10261 }
10262 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010263 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010264 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010265}
10266
Evan Chengd42641c2011-02-02 01:06:55 +000010267bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10268 EVT VT) const {
10269 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10270}
10271
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010272bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10273 unsigned,
10274 unsigned,
10275 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010276 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010277 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010278
10279 switch (VT.getSimpleVT().SimpleTy) {
10280 default:
10281 return false;
10282 case MVT::i8:
10283 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010284 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010285 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010286 if (AllowsUnaligned) {
10287 if (Fast)
10288 *Fast = Subtarget->hasV7Ops();
10289 return true;
10290 }
10291 return false;
10292 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010293 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010294 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010295 // For any little-endian targets with neon, we can support unaligned ld/st
10296 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010297 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010298 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010299 if (Fast)
10300 *Fast = true;
10301 return true;
10302 }
10303 return false;
10304 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010305 }
10306}
10307
Lang Hames9929c422011-11-02 22:52:45 +000010308static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10309 unsigned AlignCheck) {
10310 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10311 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10312}
10313
10314EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10315 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010316 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010317 bool MemcpyStrSrc,
10318 MachineFunction &MF) const {
10319 const Function *F = MF.getFunction();
10320
10321 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010322 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10323 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010324 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010325 if (Size >= 16 &&
10326 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010327 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010328 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010329 } else if (Size >= 8 &&
10330 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010331 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10332 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010333 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010334 }
10335 }
10336
Lang Hamesb85fcd02011-11-08 18:56:23 +000010337 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010338 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010339 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010340 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010341 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010342
Lang Hames9929c422011-11-02 22:52:45 +000010343 // Let the target-independent logic figure it out.
10344 return MVT::Other;
10345}
10346
Evan Cheng9ec512d2012-12-06 19:13:27 +000010347bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10348 if (Val.getOpcode() != ISD::LOAD)
10349 return false;
10350
10351 EVT VT1 = Val.getValueType();
10352 if (!VT1.isSimple() || !VT1.isInteger() ||
10353 !VT2.isSimple() || !VT2.isInteger())
10354 return false;
10355
10356 switch (VT1.getSimpleVT().SimpleTy) {
10357 default: break;
10358 case MVT::i1:
10359 case MVT::i8:
10360 case MVT::i16:
10361 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10362 return true;
10363 }
10364
10365 return false;
10366}
10367
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010368bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10369 EVT VT = ExtVal.getValueType();
10370
10371 if (!isTypeLegal(VT))
10372 return false;
10373
10374 // Don't create a loadext if we can fold the extension into a wide/long
10375 // instruction.
10376 // If there's more than one user instruction, the loadext is desirable no
10377 // matter what. There can be two uses by the same instruction.
10378 if (ExtVal->use_empty() ||
10379 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10380 return true;
10381
10382 SDNode *U = *ExtVal->use_begin();
10383 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10384 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10385 return false;
10386
10387 return true;
10388}
10389
Tim Northovercc2e9032013-08-06 13:58:03 +000010390bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10391 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10392 return false;
10393
10394 if (!isTypeLegal(EVT::getEVT(Ty1)))
10395 return false;
10396
10397 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10398
10399 // Assuming the caller doesn't have a zeroext or signext return parameter,
10400 // truncation all the way down to i1 is valid.
10401 return true;
10402}
10403
10404
Evan Chengdc49a8d2009-08-14 20:09:37 +000010405static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10406 if (V < 0)
10407 return false;
10408
10409 unsigned Scale = 1;
10410 switch (VT.getSimpleVT().SimpleTy) {
10411 default: return false;
10412 case MVT::i1:
10413 case MVT::i8:
10414 // Scale == 1;
10415 break;
10416 case MVT::i16:
10417 // Scale == 2;
10418 Scale = 2;
10419 break;
10420 case MVT::i32:
10421 // Scale == 4;
10422 Scale = 4;
10423 break;
10424 }
10425
10426 if ((V & (Scale - 1)) != 0)
10427 return false;
10428 V /= Scale;
10429 return V == (V & ((1LL << 5) - 1));
10430}
10431
10432static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10433 const ARMSubtarget *Subtarget) {
10434 bool isNeg = false;
10435 if (V < 0) {
10436 isNeg = true;
10437 V = - V;
10438 }
10439
10440 switch (VT.getSimpleVT().SimpleTy) {
10441 default: return false;
10442 case MVT::i1:
10443 case MVT::i8:
10444 case MVT::i16:
10445 case MVT::i32:
10446 // + imm12 or - imm8
10447 if (isNeg)
10448 return V == (V & ((1LL << 8) - 1));
10449 return V == (V & ((1LL << 12) - 1));
10450 case MVT::f32:
10451 case MVT::f64:
10452 // Same as ARM mode. FIXME: NEON?
10453 if (!Subtarget->hasVFP2())
10454 return false;
10455 if ((V & 3) != 0)
10456 return false;
10457 V >>= 2;
10458 return V == (V & ((1LL << 8) - 1));
10459 }
10460}
10461
Evan Cheng2150b922007-03-12 23:30:29 +000010462/// isLegalAddressImmediate - Return true if the integer value can be used
10463/// as the offset of the target addressing mode for load / store of the
10464/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010465static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010466 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010467 if (V == 0)
10468 return true;
10469
Evan Chengce5dfb62009-03-09 19:15:00 +000010470 if (!VT.isSimple())
10471 return false;
10472
Evan Chengdc49a8d2009-08-14 20:09:37 +000010473 if (Subtarget->isThumb1Only())
10474 return isLegalT1AddressImmediate(V, VT);
10475 else if (Subtarget->isThumb2())
10476 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010477
Evan Chengdc49a8d2009-08-14 20:09:37 +000010478 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010479 if (V < 0)
10480 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010481 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010482 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010483 case MVT::i1:
10484 case MVT::i8:
10485 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010486 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010487 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010488 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010489 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010490 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010491 case MVT::f32:
10492 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010493 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010494 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010495 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010496 return false;
10497 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010498 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010499 }
Evan Cheng10043e22007-01-19 07:51:42 +000010500}
10501
Evan Chengdc49a8d2009-08-14 20:09:37 +000010502bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10503 EVT VT) const {
10504 int Scale = AM.Scale;
10505 if (Scale < 0)
10506 return false;
10507
10508 switch (VT.getSimpleVT().SimpleTy) {
10509 default: return false;
10510 case MVT::i1:
10511 case MVT::i8:
10512 case MVT::i16:
10513 case MVT::i32:
10514 if (Scale == 1)
10515 return true;
10516 // r + r << imm
10517 Scale = Scale & ~1;
10518 return Scale == 2 || Scale == 4 || Scale == 8;
10519 case MVT::i64:
10520 // r + r
10521 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10522 return true;
10523 return false;
10524 case MVT::isVoid:
10525 // Note, we allow "void" uses (basically, uses that aren't loads or
10526 // stores), because arm allows folding a scale into many arithmetic
10527 // operations. This should be made more precise and revisited later.
10528
10529 // Allow r << imm, but the imm has to be a multiple of two.
10530 if (Scale & 1) return false;
10531 return isPowerOf2_32(Scale);
10532 }
10533}
10534
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010535/// isLegalAddressingMode - Return true if the addressing mode represented
10536/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010537bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10538 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010539 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010540 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010541 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010542 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010543
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010544 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010545 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010546 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010547
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010548 switch (AM.Scale) {
10549 case 0: // no scale reg, must be "r+i" or "r", or "i".
10550 break;
10551 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010552 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010553 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010554 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010555 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010556 // ARM doesn't support any R+R*scale+imm addr modes.
10557 if (AM.BaseOffs)
10558 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010559
Bob Wilson866c1742009-04-08 17:55:28 +000010560 if (!VT.isSimple())
10561 return false;
10562
Evan Chengdc49a8d2009-08-14 20:09:37 +000010563 if (Subtarget->isThumb2())
10564 return isLegalT2ScaledAddressingMode(AM, VT);
10565
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010566 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010567 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010568 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010569 case MVT::i1:
10570 case MVT::i8:
10571 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010572 if (Scale < 0) Scale = -Scale;
10573 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010574 return true;
10575 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010576 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010577 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010578 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010579 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010580 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010581 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010582 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010583
Owen Anderson9f944592009-08-11 20:47:22 +000010584 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010585 // Note, we allow "void" uses (basically, uses that aren't loads or
10586 // stores), because arm allows folding a scale into many arithmetic
10587 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010588
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010589 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010590 if (Scale & 1) return false;
10591 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010592 }
Evan Cheng2150b922007-03-12 23:30:29 +000010593 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010594 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010595}
10596
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010597/// isLegalICmpImmediate - Return true if the specified immediate is legal
10598/// icmp immediate, that is the target has icmp instructions which can compare
10599/// a register against the immediate without having to materialize the
10600/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010601bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010602 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010603 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010604 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010605 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010606 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010607 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010608 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010609}
10610
Andrew Tricka22cdb72012-07-18 18:34:27 +000010611/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10612/// *or sub* immediate, that is the target has add or sub instructions which can
10613/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010614/// immediate into a register.
10615bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010616 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010617 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010618 if (!Subtarget->isThumb())
10619 return ARM_AM::getSOImmVal(AbsImm) != -1;
10620 if (Subtarget->isThumb2())
10621 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10622 // Thumb1 only has 8-bit unsigned immediate.
10623 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010624}
10625
Owen Anderson53aa7a92009-08-10 22:56:29 +000010626static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010627 bool isSEXTLoad, SDValue &Base,
10628 SDValue &Offset, bool &isInc,
10629 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010630 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10631 return false;
10632
Owen Anderson9f944592009-08-11 20:47:22 +000010633 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010634 // AddressingMode 3
10635 Base = Ptr->getOperand(0);
10636 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010637 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010638 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010639 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010640 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010641 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010642 return true;
10643 }
10644 }
10645 isInc = (Ptr->getOpcode() == ISD::ADD);
10646 Offset = Ptr->getOperand(1);
10647 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010648 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010649 // AddressingMode 2
10650 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010651 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010652 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010653 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010654 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010655 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010656 Base = Ptr->getOperand(0);
10657 return true;
10658 }
10659 }
10660
10661 if (Ptr->getOpcode() == ISD::ADD) {
10662 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010663 ARM_AM::ShiftOpc ShOpcVal=
10664 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010665 if (ShOpcVal != ARM_AM::no_shift) {
10666 Base = Ptr->getOperand(1);
10667 Offset = Ptr->getOperand(0);
10668 } else {
10669 Base = Ptr->getOperand(0);
10670 Offset = Ptr->getOperand(1);
10671 }
10672 return true;
10673 }
10674
10675 isInc = (Ptr->getOpcode() == ISD::ADD);
10676 Base = Ptr->getOperand(0);
10677 Offset = Ptr->getOperand(1);
10678 return true;
10679 }
10680
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010681 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010682 return false;
10683}
10684
Owen Anderson53aa7a92009-08-10 22:56:29 +000010685static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010686 bool isSEXTLoad, SDValue &Base,
10687 SDValue &Offset, bool &isInc,
10688 SelectionDAG &DAG) {
10689 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10690 return false;
10691
10692 Base = Ptr->getOperand(0);
10693 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10694 int RHSC = (int)RHS->getZExtValue();
10695 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10696 assert(Ptr->getOpcode() == ISD::ADD);
10697 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010698 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010699 return true;
10700 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10701 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010702 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010703 return true;
10704 }
10705 }
10706
10707 return false;
10708}
10709
Evan Cheng10043e22007-01-19 07:51:42 +000010710/// getPreIndexedAddressParts - returns true by value, base pointer and
10711/// offset pointer and addressing mode by reference if the node's address
10712/// can be legally represented as pre-indexed load / store address.
10713bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010714ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10715 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010716 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010717 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010718 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010719 return false;
10720
Owen Anderson53aa7a92009-08-10 22:56:29 +000010721 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010722 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010723 bool isSEXTLoad = false;
10724 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10725 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010726 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010727 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10728 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10729 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010730 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010731 } else
10732 return false;
10733
10734 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010735 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010736 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010737 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10738 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010739 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010740 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010741 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010742 if (!isLegal)
10743 return false;
10744
10745 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10746 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010747}
10748
10749/// getPostIndexedAddressParts - returns true by value, base pointer and
10750/// offset pointer and addressing mode by reference if this node can be
10751/// combined with a load / store to form a post-indexed load / store.
10752bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010753 SDValue &Base,
10754 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010755 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010756 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010757 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010758 return false;
10759
Owen Anderson53aa7a92009-08-10 22:56:29 +000010760 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010761 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010762 bool isSEXTLoad = false;
10763 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010764 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010765 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010766 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10767 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010768 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010769 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010770 } else
10771 return false;
10772
10773 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010774 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010775 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010776 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010777 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010778 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010779 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10780 isInc, DAG);
10781 if (!isLegal)
10782 return false;
10783
Evan Chengf19384d2010-05-18 21:31:17 +000010784 if (Ptr != Base) {
10785 // Swap base ptr and offset to catch more post-index load / store when
10786 // it's legal. In Thumb2 mode, offset must be an immediate.
10787 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10788 !Subtarget->isThumb2())
10789 std::swap(Base, Offset);
10790
10791 // Post-indexed load / store update the base pointer.
10792 if (Ptr != Base)
10793 return false;
10794 }
10795
Evan Cheng84c6cda2009-07-02 07:28:31 +000010796 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10797 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010798}
10799
Jay Foada0653a32014-05-14 21:14:37 +000010800void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10801 APInt &KnownZero,
10802 APInt &KnownOne,
10803 const SelectionDAG &DAG,
10804 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010805 unsigned BitWidth = KnownOne.getBitWidth();
10806 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010807 switch (Op.getOpcode()) {
10808 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010809 case ARMISD::ADDC:
10810 case ARMISD::ADDE:
10811 case ARMISD::SUBC:
10812 case ARMISD::SUBE:
10813 // These nodes' second result is a boolean
10814 if (Op.getResNo() == 0)
10815 break;
10816 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10817 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010818 case ARMISD::CMOV: {
10819 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010820 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010821 if (KnownZero == 0 && KnownOne == 0) return;
10822
Dan Gohmanf990faf2008-02-13 00:35:47 +000010823 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010824 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010825 KnownZero &= KnownZeroRHS;
10826 KnownOne &= KnownOneRHS;
10827 return;
10828 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010829 case ISD::INTRINSIC_W_CHAIN: {
10830 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10831 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10832 switch (IntID) {
10833 default: return;
10834 case Intrinsic::arm_ldaex:
10835 case Intrinsic::arm_ldrex: {
10836 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10837 unsigned MemBits = VT.getScalarType().getSizeInBits();
10838 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10839 return;
10840 }
10841 }
10842 }
Evan Cheng10043e22007-01-19 07:51:42 +000010843 }
10844}
10845
10846//===----------------------------------------------------------------------===//
10847// ARM Inline Assembly Support
10848//===----------------------------------------------------------------------===//
10849
Evan Cheng078b0b02011-01-08 01:24:27 +000010850bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10851 // Looking for "rev" which is V6+.
10852 if (!Subtarget->hasV6Ops())
10853 return false;
10854
10855 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10856 std::string AsmStr = IA->getAsmString();
10857 SmallVector<StringRef, 4> AsmPieces;
10858 SplitString(AsmStr, AsmPieces, ";\n");
10859
10860 switch (AsmPieces.size()) {
10861 default: return false;
10862 case 1:
10863 AsmStr = AsmPieces[0];
10864 AsmPieces.clear();
10865 SplitString(AsmStr, AsmPieces, " \t,");
10866
10867 // rev $0, $1
10868 if (AsmPieces.size() == 3 &&
10869 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10870 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010871 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010872 if (Ty && Ty->getBitWidth() == 32)
10873 return IntrinsicLowering::LowerToByteSwap(CI);
10874 }
10875 break;
10876 }
10877
10878 return false;
10879}
10880
Evan Cheng10043e22007-01-19 07:51:42 +000010881/// getConstraintType - Given a constraint letter, return the type of
10882/// constraint it is for this target.
10883ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010884ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000010885 if (Constraint.size() == 1) {
10886 switch (Constraint[0]) {
10887 default: break;
10888 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010889 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010890 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010891 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010892 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010893 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010894 // An address with a single base register. Due to the way we
10895 // currently handle addresses it is the same as an 'r' memory constraint.
10896 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010897 }
Eric Christophere256cd02011-06-21 22:10:57 +000010898 } else if (Constraint.size() == 2) {
10899 switch (Constraint[0]) {
10900 default: break;
10901 // All 'U+' constraints are addresses.
10902 case 'U': return C_Memory;
10903 }
Evan Cheng10043e22007-01-19 07:51:42 +000010904 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010905 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010906}
10907
John Thompsone8360b72010-10-29 17:29:13 +000010908/// Examine constraint type and operand type and determine a weight value.
10909/// This object must already have been set up with the operand type
10910/// and the current alternative constraint selected.
10911TargetLowering::ConstraintWeight
10912ARMTargetLowering::getSingleConstraintMatchWeight(
10913 AsmOperandInfo &info, const char *constraint) const {
10914 ConstraintWeight weight = CW_Invalid;
10915 Value *CallOperandVal = info.CallOperandVal;
10916 // If we don't have a value, we can't do a match,
10917 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010918 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010919 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010920 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010921 // Look at the constraint type.
10922 switch (*constraint) {
10923 default:
10924 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10925 break;
10926 case 'l':
10927 if (type->isIntegerTy()) {
10928 if (Subtarget->isThumb())
10929 weight = CW_SpecificReg;
10930 else
10931 weight = CW_Register;
10932 }
10933 break;
10934 case 'w':
10935 if (type->isFloatingPointTy())
10936 weight = CW_Register;
10937 break;
10938 }
10939 return weight;
10940}
10941
Eric Christophercf2007c2011-06-30 23:50:52 +000010942typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010943RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
10944 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010945 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010946 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010947 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010948 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010949 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010950 return RCPair(0U, &ARM::tGPRRegClass);
10951 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010952 case 'h': // High regs or no regs.
10953 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010954 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010955 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010956 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010957 if (Subtarget->isThumb1Only())
10958 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010959 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010960 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010961 if (VT == MVT::Other)
10962 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010963 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010964 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010965 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010966 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010967 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010968 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010969 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010970 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010971 if (VT == MVT::Other)
10972 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010973 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010974 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010975 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010976 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010977 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010978 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010979 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010980 case 't':
10981 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010982 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010983 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010984 }
10985 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010986 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010987 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010988
Eric Christopher11e4df72015-02-26 22:38:43 +000010989 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010990}
10991
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010992/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10993/// vector. If it is invalid, don't add anything to Ops.
10994void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010995 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010996 std::vector<SDValue>&Ops,
10997 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010998 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010999
Eric Christopherde9399b2011-06-02 23:16:42 +000011000 // Currently only support length 1 constraints.
11001 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011002
Eric Christopherde9399b2011-06-02 23:16:42 +000011003 char ConstraintLetter = Constraint[0];
11004 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011005 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011006 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011007 case 'I': case 'J': case 'K': case 'L':
11008 case 'M': case 'N': case 'O':
11009 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11010 if (!C)
11011 return;
11012
11013 int64_t CVal64 = C->getSExtValue();
11014 int CVal = (int) CVal64;
11015 // None of these constraints allow values larger than 32 bits. Check
11016 // that the value fits in an int.
11017 if (CVal != CVal64)
11018 return;
11019
Eric Christopherde9399b2011-06-02 23:16:42 +000011020 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011021 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011022 // Constant suitable for movw, must be between 0 and
11023 // 65535.
11024 if (Subtarget->hasV6T2Ops())
11025 if (CVal >= 0 && CVal <= 65535)
11026 break;
11027 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011028 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011029 if (Subtarget->isThumb1Only()) {
11030 // This must be a constant between 0 and 255, for ADD
11031 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011032 if (CVal >= 0 && CVal <= 255)
11033 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011034 } else if (Subtarget->isThumb2()) {
11035 // A constant that can be used as an immediate value in a
11036 // data-processing instruction.
11037 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11038 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011039 } else {
11040 // A constant that can be used as an immediate value in a
11041 // data-processing instruction.
11042 if (ARM_AM::getSOImmVal(CVal) != -1)
11043 break;
11044 }
11045 return;
11046
11047 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000011048 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011049 // This must be a constant between -255 and -1, for negated ADD
11050 // immediates. This can be used in GCC with an "n" modifier that
11051 // prints the negated value, for use with SUB instructions. It is
11052 // not useful otherwise but is implemented for compatibility.
11053 if (CVal >= -255 && CVal <= -1)
11054 break;
11055 } else {
11056 // This must be a constant between -4095 and 4095. It is not clear
11057 // what this constraint is intended for. Implemented for
11058 // compatibility with GCC.
11059 if (CVal >= -4095 && CVal <= 4095)
11060 break;
11061 }
11062 return;
11063
11064 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011065 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011066 // A 32-bit value where only one byte has a nonzero value. Exclude
11067 // zero to match GCC. This constraint is used by GCC internally for
11068 // constants that can be loaded with a move/shift combination.
11069 // It is not useful otherwise but is implemented for compatibility.
11070 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11071 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011072 } else if (Subtarget->isThumb2()) {
11073 // A constant whose bitwise inverse can be used as an immediate
11074 // value in a data-processing instruction. This can be used in GCC
11075 // with a "B" modifier that prints the inverted value, for use with
11076 // BIC and MVN instructions. It is not useful otherwise but is
11077 // implemented for compatibility.
11078 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11079 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011080 } else {
11081 // A constant whose bitwise inverse can be used as an immediate
11082 // value in a data-processing instruction. This can be used in GCC
11083 // with a "B" modifier that prints the inverted value, for use with
11084 // BIC and MVN instructions. It is not useful otherwise but is
11085 // implemented for compatibility.
11086 if (ARM_AM::getSOImmVal(~CVal) != -1)
11087 break;
11088 }
11089 return;
11090
11091 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011092 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011093 // This must be a constant between -7 and 7,
11094 // for 3-operand ADD/SUB immediate instructions.
11095 if (CVal >= -7 && CVal < 7)
11096 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011097 } else if (Subtarget->isThumb2()) {
11098 // A constant whose negation can be used as an immediate value in a
11099 // data-processing instruction. This can be used in GCC with an "n"
11100 // modifier that prints the negated value, for use with SUB
11101 // instructions. It is not useful otherwise but is implemented for
11102 // compatibility.
11103 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11104 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011105 } else {
11106 // A constant whose negation can be used as an immediate value in a
11107 // data-processing instruction. This can be used in GCC with an "n"
11108 // modifier that prints the negated value, for use with SUB
11109 // instructions. It is not useful otherwise but is implemented for
11110 // compatibility.
11111 if (ARM_AM::getSOImmVal(-CVal) != -1)
11112 break;
11113 }
11114 return;
11115
11116 case 'M':
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 multiple of 4 between 0 and 1020, for
11119 // ADD sp + immediate.
11120 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11121 break;
11122 } else {
11123 // A power of two or a constant between 0 and 32. This is used in
11124 // GCC for the shift amount on shifted register operands, but it is
11125 // useful in general for any shift amounts.
11126 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11127 break;
11128 }
11129 return;
11130
11131 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011132 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011133 // This must be a constant between 0 and 31, for shift amounts.
11134 if (CVal >= 0 && CVal <= 31)
11135 break;
11136 }
11137 return;
11138
11139 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011140 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011141 // This must be a multiple of 4 between -508 and 508, for
11142 // ADD/SUB sp = sp + immediate.
11143 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11144 break;
11145 }
11146 return;
11147 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011148 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011149 break;
11150 }
11151
11152 if (Result.getNode()) {
11153 Ops.push_back(Result);
11154 return;
11155 }
Dale Johannesence97d552010-06-25 21:55:36 +000011156 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011157}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011158
Renato Golin87610692013-07-16 09:32:17 +000011159SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011160 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) &&
11161 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011162 unsigned Opcode = Op->getOpcode();
11163 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011164 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011165 bool isSigned = (Opcode == ISD::SDIVREM);
11166 EVT VT = Op->getValueType(0);
11167 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11168
11169 RTLIB::Libcall LC;
11170 switch (VT.getSimpleVT().SimpleTy) {
11171 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011172 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11173 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11174 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11175 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000011176 }
11177
11178 SDValue InChain = DAG.getEntryNode();
11179
11180 TargetLowering::ArgListTy Args;
11181 TargetLowering::ArgListEntry Entry;
11182 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
11183 EVT ArgVT = Op->getOperand(i).getValueType();
11184 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
11185 Entry.Node = Op->getOperand(i);
11186 Entry.Ty = ArgTy;
11187 Entry.isSExt = isSigned;
11188 Entry.isZExt = !isSigned;
11189 Args.push_back(Entry);
11190 }
11191
11192 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011193 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011194
Reid Kleckner343c3952014-11-20 23:51:47 +000011195 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011196
11197 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011198 TargetLowering::CallLoweringInfo CLI(DAG);
11199 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011200 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011201 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011202
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011203 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011204 return CallInfo.first;
11205}
11206
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011207SDValue
11208ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11209 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11210 SDLoc DL(Op);
11211
11212 // Get the inputs.
11213 SDValue Chain = Op.getOperand(0);
11214 SDValue Size = Op.getOperand(1);
11215
11216 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011217 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011218
11219 SDValue Flag;
11220 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11221 Flag = Chain.getValue(1);
11222
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011223 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011224 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11225
11226 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11227 Chain = NewSP.getValue(1);
11228
11229 SDValue Ops[2] = { NewSP, Chain };
11230 return DAG.getMergeValues(Ops, DL);
11231}
11232
Oliver Stannard51b1d462014-08-21 12:50:31 +000011233SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11234 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11235 "Unexpected type for custom-lowering FP_EXTEND");
11236
11237 RTLIB::Libcall LC;
11238 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11239
11240 SDValue SrcVal = Op.getOperand(0);
11241 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11242 /*isSigned*/ false, SDLoc(Op)).first;
11243}
11244
11245SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11246 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11247 Subtarget->isFPOnlySP() &&
11248 "Unexpected type for custom-lowering FP_ROUND");
11249
11250 RTLIB::Libcall LC;
11251 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11252
11253 SDValue SrcVal = Op.getOperand(0);
11254 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11255 /*isSigned*/ false, SDLoc(Op)).first;
11256}
11257
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011258bool
11259ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11260 // The ARM target isn't yet aware of offsets.
11261 return false;
11262}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011263
Jim Grosbach11013ed2010-07-16 23:05:05 +000011264bool ARM::isBitFieldInvertedMask(unsigned v) {
11265 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011266 return false;
11267
Jim Grosbach11013ed2010-07-16 23:05:05 +000011268 // there can be 1's on either or both "outsides", all the "inside"
11269 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011270 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011271}
11272
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011273/// isFPImmLegal - Returns true if the target can instruction select the
11274/// specified FP immediate natively. If false, the legalizer will
11275/// materialize the FP immediate as a load from a constant pool.
11276bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11277 if (!Subtarget->hasVFP3())
11278 return false;
11279 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011280 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011281 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011282 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011283 return false;
11284}
Bob Wilson5549d492010-09-21 17:56:22 +000011285
Wesley Peck527da1b2010-11-23 03:31:01 +000011286/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011287/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11288/// specified in the intrinsic calls.
11289bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11290 const CallInst &I,
11291 unsigned Intrinsic) const {
11292 switch (Intrinsic) {
11293 case Intrinsic::arm_neon_vld1:
11294 case Intrinsic::arm_neon_vld2:
11295 case Intrinsic::arm_neon_vld3:
11296 case Intrinsic::arm_neon_vld4:
11297 case Intrinsic::arm_neon_vld2lane:
11298 case Intrinsic::arm_neon_vld3lane:
11299 case Intrinsic::arm_neon_vld4lane: {
11300 Info.opc = ISD::INTRINSIC_W_CHAIN;
11301 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011302 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11303 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011304 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11305 Info.ptrVal = I.getArgOperand(0);
11306 Info.offset = 0;
11307 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11308 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11309 Info.vol = false; // volatile loads with NEON intrinsics not supported
11310 Info.readMem = true;
11311 Info.writeMem = false;
11312 return true;
11313 }
11314 case Intrinsic::arm_neon_vst1:
11315 case Intrinsic::arm_neon_vst2:
11316 case Intrinsic::arm_neon_vst3:
11317 case Intrinsic::arm_neon_vst4:
11318 case Intrinsic::arm_neon_vst2lane:
11319 case Intrinsic::arm_neon_vst3lane:
11320 case Intrinsic::arm_neon_vst4lane: {
11321 Info.opc = ISD::INTRINSIC_VOID;
11322 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011323 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011324 unsigned NumElts = 0;
11325 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011326 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011327 if (!ArgTy->isVectorTy())
11328 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011329 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011330 }
11331 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11332 Info.ptrVal = I.getArgOperand(0);
11333 Info.offset = 0;
11334 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11335 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11336 Info.vol = false; // volatile stores with NEON intrinsics not supported
11337 Info.readMem = false;
11338 Info.writeMem = true;
11339 return true;
11340 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011341 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011342 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011343 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011344 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11345 Info.opc = ISD::INTRINSIC_W_CHAIN;
11346 Info.memVT = MVT::getVT(PtrTy->getElementType());
11347 Info.ptrVal = I.getArgOperand(0);
11348 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011349 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011350 Info.vol = true;
11351 Info.readMem = true;
11352 Info.writeMem = false;
11353 return true;
11354 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011355 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011356 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011357 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011358 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11359 Info.opc = ISD::INTRINSIC_W_CHAIN;
11360 Info.memVT = MVT::getVT(PtrTy->getElementType());
11361 Info.ptrVal = I.getArgOperand(1);
11362 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011363 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011364 Info.vol = true;
11365 Info.readMem = false;
11366 Info.writeMem = true;
11367 return true;
11368 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011369 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011370 case Intrinsic::arm_strexd: {
11371 Info.opc = ISD::INTRINSIC_W_CHAIN;
11372 Info.memVT = MVT::i64;
11373 Info.ptrVal = I.getArgOperand(2);
11374 Info.offset = 0;
11375 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011376 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011377 Info.readMem = false;
11378 Info.writeMem = true;
11379 return true;
11380 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011381 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011382 case Intrinsic::arm_ldrexd: {
11383 Info.opc = ISD::INTRINSIC_W_CHAIN;
11384 Info.memVT = MVT::i64;
11385 Info.ptrVal = I.getArgOperand(0);
11386 Info.offset = 0;
11387 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011388 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011389 Info.readMem = true;
11390 Info.writeMem = false;
11391 return true;
11392 }
Bob Wilson5549d492010-09-21 17:56:22 +000011393 default:
11394 break;
11395 }
11396
11397 return false;
11398}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011399
11400/// \brief Returns true if it is beneficial to convert a load of a constant
11401/// to just the constant itself.
11402bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11403 Type *Ty) const {
11404 assert(Ty->isIntegerTy());
11405
11406 unsigned Bits = Ty->getPrimitiveSizeInBits();
11407 if (Bits == 0 || Bits > 32)
11408 return false;
11409 return true;
11410}
Tim Northover037f26f22014-04-17 18:22:47 +000011411
Robin Morisset25c8e312014-09-17 00:06:58 +000011412bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11413
Robin Morisset5349e8e2014-09-18 18:56:04 +000011414Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11415 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011416 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011417
11418 // First, if the target has no DMB, see what fallback we can use.
11419 if (!Subtarget->hasDataBarrier()) {
11420 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11421 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11422 // here.
11423 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11424 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11425 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11426 Builder.getInt32(0), Builder.getInt32(7),
11427 Builder.getInt32(10), Builder.getInt32(5)};
11428 return Builder.CreateCall(MCR, args);
11429 } else {
11430 // Instead of using barriers, atomic accesses on these subtargets use
11431 // libcalls.
11432 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11433 }
11434 } else {
11435 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11436 // Only a full system barrier exists in the M-class architectures.
11437 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11438 Constant *CDomain = Builder.getInt32(Domain);
11439 return Builder.CreateCall(DMB, CDomain);
11440 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011441}
11442
11443// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011444Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011445 AtomicOrdering Ord, bool IsStore,
11446 bool IsLoad) const {
11447 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011448 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011449
11450 switch (Ord) {
11451 case NotAtomic:
11452 case Unordered:
11453 llvm_unreachable("Invalid fence: unordered/non-atomic");
11454 case Monotonic:
11455 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011456 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011457 case SequentiallyConsistent:
11458 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011459 return nullptr; // Nothing to do
11460 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011461 case Release:
11462 case AcquireRelease:
11463 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011464 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011465 // FIXME: add a comment with a link to documentation justifying this.
11466 else
Robin Morissetdedef332014-09-23 20:31:14 +000011467 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011468 }
Robin Morissetdedef332014-09-23 20:31:14 +000011469 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011470}
11471
Robin Morissetdedef332014-09-23 20:31:14 +000011472Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011473 AtomicOrdering Ord, bool IsStore,
11474 bool IsLoad) const {
11475 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011476 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011477
11478 switch (Ord) {
11479 case NotAtomic:
11480 case Unordered:
11481 llvm_unreachable("Invalid fence: unordered/not-atomic");
11482 case Monotonic:
11483 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011484 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011485 case Acquire:
11486 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011487 case SequentiallyConsistent:
11488 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011489 }
Robin Morissetdedef332014-09-23 20:31:14 +000011490 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011491}
11492
Robin Morisseted3d48f2014-09-03 21:29:59 +000011493// Loads and stores less than 64-bits are already atomic; ones above that
11494// are doomed anyway, so defer to the default libcall and blame the OS when
11495// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11496// anything for those.
11497bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11498 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11499 return (Size == 64) && !Subtarget->isMClass();
11500}
Tim Northover037f26f22014-04-17 18:22:47 +000011501
Robin Morisseted3d48f2014-09-03 21:29:59 +000011502// Loads and stores less than 64-bits are already atomic; ones above that
11503// are doomed anyway, so defer to the default libcall and blame the OS when
11504// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11505// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011506// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11507// guarantee, see DDI0406C ARM architecture reference manual,
11508// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011509bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11510 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11511 return (Size == 64) && !Subtarget->isMClass();
11512}
11513
11514// For the real atomic operations, we have ldrex/strex up to 32 bits,
11515// and up to 64 bits on the non-M profiles
JF Bastienf14889e2015-03-04 15:47:57 +000011516TargetLoweringBase::AtomicRMWExpansionKind
11517ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011518 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011519 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
11520 ? AtomicRMWExpansionKind::LLSC
11521 : AtomicRMWExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011522}
11523
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011524// This has so far only been implemented for MachO.
11525bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011526 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011527}
11528
Quentin Colombetc32615d2014-10-31 17:52:53 +000011529bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11530 unsigned &Cost) const {
11531 // If we do not have NEON, vector types are not natively supported.
11532 if (!Subtarget->hasNEON())
11533 return false;
11534
11535 // Floating point values and vector values map to the same register file.
11536 // Therefore, althought we could do a store extract of a vector type, this is
11537 // better to leave at float as we have more freedom in the addressing mode for
11538 // those.
11539 if (VectorTy->isFPOrFPVectorTy())
11540 return false;
11541
11542 // If the index is unknown at compile time, this is very expensive to lower
11543 // and it is not possible to combine the store with the extract.
11544 if (!isa<ConstantInt>(Idx))
11545 return false;
11546
11547 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11548 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11549 // We can do a store + vector extract on any vector that fits perfectly in a D
11550 // or Q register.
11551 if (BitWidth == 64 || BitWidth == 128) {
11552 Cost = 0;
11553 return true;
11554 }
11555 return false;
11556}
11557
Tim Northover037f26f22014-04-17 18:22:47 +000011558Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11559 AtomicOrdering Ord) const {
11560 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11561 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011562 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011563
11564 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11565 // intrinsic must return {i32, i32} and we have to recombine them into a
11566 // single i64 here.
11567 if (ValTy->getPrimitiveSizeInBits() == 64) {
11568 Intrinsic::ID Int =
11569 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11570 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11571
11572 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11573 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11574
11575 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11576 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011577 if (!Subtarget->isLittle())
11578 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011579 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11580 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11581 return Builder.CreateOr(
11582 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11583 }
11584
11585 Type *Tys[] = { Addr->getType() };
11586 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11587 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11588
11589 return Builder.CreateTruncOrBitCast(
11590 Builder.CreateCall(Ldrex, Addr),
11591 cast<PointerType>(Addr->getType())->getElementType());
11592}
11593
11594Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11595 Value *Addr,
11596 AtomicOrdering Ord) const {
11597 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011598 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011599
11600 // Since the intrinsics must have legal type, the i64 intrinsics take two
11601 // parameters: "i32, i32". We must marshal Val into the appropriate form
11602 // before the call.
11603 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11604 Intrinsic::ID Int =
11605 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11606 Function *Strex = Intrinsic::getDeclaration(M, Int);
11607 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11608
11609 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11610 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011611 if (!Subtarget->isLittle())
11612 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011613 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011614 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011615 }
11616
11617 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11618 Type *Tys[] = { Addr->getType() };
11619 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11620
David Blaikieff6409d2015-05-18 22:13:54 +000011621 return Builder.CreateCall(
11622 Strex, {Builder.CreateZExtOrBitCast(
11623 Val, Strex->getFunctionType()->getParamType(0)),
11624 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011625}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011626
Hao Liu2cd34bb2015-06-26 02:45:36 +000011627/// \brief Lower an interleaved load into a vldN intrinsic.
11628///
11629/// E.g. Lower an interleaved load (Factor = 2):
11630/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
11631/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
11632/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
11633///
11634/// Into:
11635/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
11636/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
11637/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
11638bool ARMTargetLowering::lowerInterleavedLoad(
11639 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
11640 ArrayRef<unsigned> Indices, unsigned Factor) const {
11641 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11642 "Invalid interleave factor");
11643 assert(!Shuffles.empty() && "Empty shufflevector input");
11644 assert(Shuffles.size() == Indices.size() &&
11645 "Unmatched number of shufflevectors and indices");
11646
11647 VectorType *VecTy = Shuffles[0]->getType();
11648 Type *EltTy = VecTy->getVectorElementType();
11649
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011650 const DataLayout &DL = LI->getModule()->getDataLayout();
11651 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
11652 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011653
11654 // Skip illegal vector types and vector types of i64/f64 element (vldN doesn't
11655 // support i64/f64 element).
11656 if ((VecSize != 64 && VecSize != 128) || EltIs64Bits)
11657 return false;
11658
11659 // A pointer vector can not be the return type of the ldN intrinsics. Need to
11660 // load integer vectors first and then convert to pointer vectors.
11661 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011662 VecTy =
11663 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000011664
11665 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
11666 Intrinsic::arm_neon_vld3,
11667 Intrinsic::arm_neon_vld4};
11668
11669 Function *VldnFunc =
11670 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], VecTy);
11671
11672 IRBuilder<> Builder(LI);
11673 SmallVector<Value *, 2> Ops;
11674
11675 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
11676 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
11677 Ops.push_back(Builder.getInt32(LI->getAlignment()));
11678
11679 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
11680
11681 // Replace uses of each shufflevector with the corresponding vector loaded
11682 // by ldN.
11683 for (unsigned i = 0; i < Shuffles.size(); i++) {
11684 ShuffleVectorInst *SV = Shuffles[i];
11685 unsigned Index = Indices[i];
11686
11687 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
11688
11689 // Convert the integer vector to pointer vector if the element is pointer.
11690 if (EltTy->isPointerTy())
11691 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
11692
11693 SV->replaceAllUsesWith(SubVec);
11694 }
11695
11696 return true;
11697}
11698
11699/// \brief Get a mask consisting of sequential integers starting from \p Start.
11700///
11701/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
11702static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
11703 unsigned NumElts) {
11704 SmallVector<Constant *, 16> Mask;
11705 for (unsigned i = 0; i < NumElts; i++)
11706 Mask.push_back(Builder.getInt32(Start + i));
11707
11708 return ConstantVector::get(Mask);
11709}
11710
11711/// \brief Lower an interleaved store into a vstN intrinsic.
11712///
11713/// E.g. Lower an interleaved store (Factor = 3):
11714/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
11715/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
11716/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
11717///
11718/// Into:
11719/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
11720/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
11721/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
11722/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
11723///
11724/// Note that the new shufflevectors will be removed and we'll only generate one
11725/// vst3 instruction in CodeGen.
11726bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
11727 ShuffleVectorInst *SVI,
11728 unsigned Factor) const {
11729 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11730 "Invalid interleave factor");
11731
11732 VectorType *VecTy = SVI->getType();
11733 assert(VecTy->getVectorNumElements() % Factor == 0 &&
11734 "Invalid interleaved store");
11735
11736 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
11737 Type *EltTy = VecTy->getVectorElementType();
11738 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
11739
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011740 const DataLayout &DL = SI->getModule()->getDataLayout();
11741 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
11742 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011743
11744 // Skip illegal sub vector types and vector types of i64/f64 element (vstN
11745 // doesn't support i64/f64 element).
11746 if ((SubVecSize != 64 && SubVecSize != 128) || EltIs64Bits)
11747 return false;
11748
11749 Value *Op0 = SVI->getOperand(0);
11750 Value *Op1 = SVI->getOperand(1);
11751 IRBuilder<> Builder(SI);
11752
11753 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
11754 // vectors to integer vectors.
11755 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011756 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011757
11758 // Convert to the corresponding integer vector.
11759 Type *IntVecTy =
11760 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
11761 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
11762 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
11763
11764 SubVecTy = VectorType::get(IntTy, NumSubElts);
11765 }
11766
11767 static Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
11768 Intrinsic::arm_neon_vst3,
11769 Intrinsic::arm_neon_vst4};
11770 Function *VstNFunc = Intrinsic::getDeclaration(
11771 SI->getModule(), StoreInts[Factor - 2], SubVecTy);
11772
11773 SmallVector<Value *, 6> Ops;
11774
11775 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
11776 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
11777
11778 // Split the shufflevector operands into sub vectors for the new vstN call.
11779 for (unsigned i = 0; i < Factor; i++)
11780 Ops.push_back(Builder.CreateShuffleVector(
11781 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
11782
11783 Ops.push_back(Builder.getInt32(SI->getAlignment()));
11784 Builder.CreateCall(VstNFunc, Ops);
11785 return true;
11786}
11787
Oliver Stannardc24f2172014-05-09 14:01:47 +000011788enum HABaseType {
11789 HA_UNKNOWN = 0,
11790 HA_FLOAT,
11791 HA_DOUBLE,
11792 HA_VECT64,
11793 HA_VECT128
11794};
11795
11796static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11797 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000011798 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011799 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11800 uint64_t SubMembers = 0;
11801 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11802 return false;
11803 Members += SubMembers;
11804 }
Craig Toppere3dcce92015-08-01 22:20:21 +000011805 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011806 uint64_t SubMembers = 0;
11807 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11808 return false;
11809 Members += SubMembers * AT->getNumElements();
11810 } else if (Ty->isFloatTy()) {
11811 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11812 return false;
11813 Members = 1;
11814 Base = HA_FLOAT;
11815 } else if (Ty->isDoubleTy()) {
11816 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11817 return false;
11818 Members = 1;
11819 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000011820 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011821 Members = 1;
11822 switch (Base) {
11823 case HA_FLOAT:
11824 case HA_DOUBLE:
11825 return false;
11826 case HA_VECT64:
11827 return VT->getBitWidth() == 64;
11828 case HA_VECT128:
11829 return VT->getBitWidth() == 128;
11830 case HA_UNKNOWN:
11831 switch (VT->getBitWidth()) {
11832 case 64:
11833 Base = HA_VECT64;
11834 return true;
11835 case 128:
11836 Base = HA_VECT128;
11837 return true;
11838 default:
11839 return false;
11840 }
11841 }
11842 }
11843
11844 return (Members > 0 && Members <= 4);
11845}
11846
Tim Northovere95c5b32015-02-24 17:22:34 +000011847/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11848/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11849/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011850bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11851 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011852 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11853 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011854 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011855
11856 HABaseType Base = HA_UNKNOWN;
11857 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011858 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11859 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11860
11861 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11862 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011863}