blob: 1d414a4f7c953a255d681101476fed2e5ee9b52b [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000026#include "llvm/ADT/StringSwitch.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000027#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000028#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000041#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
John Brawn0dbcd652015-03-18 12:01:59 +000044#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000045#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000047#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000049#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000053#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000054#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000055using namespace llvm;
56
Chandler Carruth84e68b22014-04-22 02:41:26 +000057#define DEBUG_TYPE "arm-isel"
58
Dale Johannesend679ff72010-06-03 21:09:53 +000059STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000060STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000061STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000062
Evan Chengf128bdc2010-06-16 07:35:02 +000063static cl::opt<bool>
64ARMInterworking("arm-interworking", cl::Hidden,
65 cl::desc("Enable / disable ARM interworking (for debugging only)"),
66 cl::init(true));
67
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000068namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000069 class ARMCCState : public CCState {
70 public:
71 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000072 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
73 ParmContext PC)
74 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000075 assert(((PC == Call) || (PC == Prologue)) &&
76 "ARMCCState users must specify whether their context is call"
77 "or prologue generation.");
78 CallOrPrologue = PC;
79 }
80 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081}
Cameron Zwarich89019782011-06-10 20:59:24 +000082
Stuart Hastings45fe3c32011-04-20 16:47:52 +000083// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000084static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085 ARM::R0, ARM::R1, ARM::R2, ARM::R3
86};
87
Craig Topper4fa625f2012-08-12 03:16:37 +000088void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
89 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000090 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000091 setOperationAction(ISD::LOAD, VT, Promote);
92 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000093
Craig Topper4fa625f2012-08-12 03:16:37 +000094 setOperationAction(ISD::STORE, VT, Promote);
95 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000096 }
97
Craig Topper4fa625f2012-08-12 03:16:37 +000098 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +000099 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 setOperationAction(ISD::SETCC, VT, Custom);
101 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
102 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000103 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000104 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
105 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
107 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000108 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
110 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
112 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000113 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000114 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
115 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
116 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
117 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
118 setOperationAction(ISD::SELECT, VT, Expand);
119 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000120 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000122 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SHL, VT, Custom);
124 setOperationAction(ISD::SRA, VT, Custom);
125 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000126 }
127
128 // Promote all bit-wise operations.
129 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000130 setOperationAction(ISD::AND, VT, Promote);
131 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
132 setOperationAction(ISD::OR, VT, Promote);
133 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::XOR, VT, Promote);
135 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000136 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000137
138 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000139 setOperationAction(ISD::SDIV, VT, Expand);
140 setOperationAction(ISD::UDIV, VT, Expand);
141 setOperationAction(ISD::FDIV, VT, Expand);
142 setOperationAction(ISD::SREM, VT, Expand);
143 setOperationAction(ISD::UREM, VT, Expand);
144 setOperationAction(ISD::FREM, VT, Expand);
James Molloya6702e22015-07-17 17:10:55 +0000145
146 if (VT.isInteger()) {
147 setOperationAction(ISD::SABSDIFF, VT, Legal);
148 setOperationAction(ISD::UABSDIFF, VT, Legal);
149 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000150 if (!VT.isFloatingPoint() &&
151 VT != MVT::v2i64 && VT != MVT::v1i64)
152 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
153 setOperationAction(Opcode, VT, Legal);
154
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000158 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Craig Topper4fa625f2012-08-12 03:16:37 +0000162void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000163 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000164 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000165}
166
Eric Christopher1889fdc2015-01-29 00:19:39 +0000167ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
168 const ARMSubtarget &STI)
169 : TargetLowering(TM), Subtarget(&STI) {
170 RegInfo = Subtarget->getRegisterInfo();
171 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000172
Duncan Sandsf2641e12011-09-06 19:07:46 +0000173 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
174
Tim Northoverd6a729b2014-01-06 14:28:05 +0000175 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000176 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000177 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000178 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000179 static const struct {
180 const RTLIB::Libcall Op;
181 const char * const Name;
182 const ISD::CondCode Cond;
183 } LibraryCalls[] = {
184 // Single-precision floating-point arithmetic.
185 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
186 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
187 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000189
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000190 // Double-precision floating-point arithmetic.
191 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
192 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
193 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
194 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000195
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000196 // Single-precision comparisons.
197 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
198 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
199 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
200 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
201 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
202 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
203 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
204 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000205
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000206 // Double-precision comparisons.
207 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
208 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
209 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
210 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
211 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
212 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
213 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
214 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000215
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000216 // Floating-point to integer conversions.
217 // i64 conversions are done via library routines even when generating VFP
218 // instructions, so use the same ones.
219 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
220 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
221 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
222 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000223
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000224 // Conversions between floating types.
225 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
226 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000227
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000228 // Integer to floating-point conversions.
229 // i64 conversions are done via library routines even when generating VFP
230 // instructions, so use the same ones.
231 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
232 // e.g., __floatunsidf vs. __floatunssidfvfp.
233 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
234 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
235 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
236 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
237 };
Evan Cheng10043e22007-01-19 07:51:42 +0000238
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000239 for (const auto &LC : LibraryCalls) {
240 setLibcallName(LC.Op, LC.Name);
241 if (LC.Cond != ISD::SETCC_INVALID)
242 setCmpLibcallCC(LC.Op, LC.Cond);
243 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000244 }
Evan Cheng10043e22007-01-19 07:51:42 +0000245 }
246
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000247 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000248 setLibcallName(RTLIB::SHL_I128, nullptr);
249 setLibcallName(RTLIB::SRL_I128, nullptr);
250 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000251
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000252 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
253 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000254 static const struct {
255 const RTLIB::Libcall Op;
256 const char * const Name;
257 const CallingConv::ID CC;
258 const ISD::CondCode Cond;
259 } LibraryCalls[] = {
260 // Double-precision floating-point arithmetic helper functions
261 // RTABI chapter 4.1.2, Table 2
262 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
263 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
264 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
265 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000266
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000267 // Double-precision floating-point comparison helper functions
268 // RTABI chapter 4.1.2, Table 3
269 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
270 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
271 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
272 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000277
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000278 // Single-precision floating-point arithmetic helper functions
279 // RTABI chapter 4.1.2, Table 4
280 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
281 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
282 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
283 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000284
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000285 // Single-precision floating-point comparison helper functions
286 // RTABI chapter 4.1.2, Table 5
287 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
288 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
289 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
290 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000295
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000296 // Floating-point to integer conversions.
297 // RTABI chapter 4.1.2, Table 6
298 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000306
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000307 // Conversions between floating types.
308 // RTABI chapter 4.1.2, Table 7
309 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000310 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000311 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000312
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000313 // Integer to floating-point conversions.
314 // RTABI chapter 4.1.2, Table 8
315 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000323
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000324 // Long long helper functions
325 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000326 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
329 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000330
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000331 // Integer division functions
332 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000333 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000341
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000342 // Memory operations
343 // RTABI chapter 4.3.4
344 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
347 };
348
349 for (const auto &LC : LibraryCalls) {
350 setLibcallName(LC.Op, LC.Name);
351 setLibcallCallingConv(LC.Op, LC.CC);
352 if (LC.Cond != ISD::SETCC_INVALID)
353 setCmpLibcallCC(LC.Op, LC.Cond);
354 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000355 }
356
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000357 if (Subtarget->isTargetWindows()) {
358 static const struct {
359 const RTLIB::Libcall Op;
360 const char * const Name;
361 const CallingConv::ID CC;
362 } LibraryCalls[] = {
363 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
371 };
372
373 for (const auto &LC : LibraryCalls) {
374 setLibcallName(LC.Op, LC.Name);
375 setLibcallCallingConv(LC.Op, LC.CC);
376 }
377 }
378
Bob Wilsonbc158992011-10-07 16:59:21 +0000379 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000380 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000381 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
382 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
383 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
384 }
385
Oliver Stannard11790b22014-08-11 09:12:32 +0000386 // The half <-> float conversion functions are always soft-float, but are
387 // needed for some targets which use a hard-float calling convention by
388 // default.
389 if (Subtarget->isAAPCS_ABI()) {
390 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
393 } else {
394 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
395 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
396 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
397 }
398
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 Wilsoncb6db982010-09-17 22:59:05 +0000586 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000587 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000588 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
589 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000590 setTargetDAGCombine(ISD::FP_TO_SINT);
591 setTargetDAGCombine(ISD::FP_TO_UINT);
592 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000593 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000594
James Molloy547d4c02012-02-20 09:24:05 +0000595 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000596 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
597 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000598 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000599 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
600 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
601 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000602 }
James Molloy547d4c02012-02-20 09:24:05 +0000603 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000604 }
605
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000606 // ARM and Thumb2 support UMLAL/SMLAL.
607 if (!Subtarget->isThumb1Only())
608 setTargetDAGCombine(ISD::ADDC);
609
Oliver Stannard51b1d462014-08-21 12:50:31 +0000610 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000611 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000612 // operations, f64 is legal for the few double-precision instructions which
613 // are present However, no double-precision operations other than moves,
614 // loads and stores are provided by the hardware.
615 setOperationAction(ISD::FADD, MVT::f64, Expand);
616 setOperationAction(ISD::FSUB, MVT::f64, Expand);
617 setOperationAction(ISD::FMUL, MVT::f64, Expand);
618 setOperationAction(ISD::FMA, MVT::f64, Expand);
619 setOperationAction(ISD::FDIV, MVT::f64, Expand);
620 setOperationAction(ISD::FREM, MVT::f64, Expand);
621 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
622 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
623 setOperationAction(ISD::FNEG, MVT::f64, Expand);
624 setOperationAction(ISD::FABS, MVT::f64, Expand);
625 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
626 setOperationAction(ISD::FSIN, MVT::f64, Expand);
627 setOperationAction(ISD::FCOS, MVT::f64, Expand);
628 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
629 setOperationAction(ISD::FPOW, MVT::f64, Expand);
630 setOperationAction(ISD::FLOG, MVT::f64, Expand);
631 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
632 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
633 setOperationAction(ISD::FEXP, MVT::f64, Expand);
634 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
635 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
636 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
637 setOperationAction(ISD::FRINT, MVT::f64, Expand);
638 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
639 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000640 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
641 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
642 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
643 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
644 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
645 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000646 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
647 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
648 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000649
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000650 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000651
Tim Northover4e80b582014-07-18 13:01:19 +0000652 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000653 for (MVT VT : MVT::fp_valuetypes()) {
654 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
655 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
656 }
Tim Northover4e80b582014-07-18 13:01:19 +0000657
658 // ... or truncating stores
659 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
660 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
661 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000662
Duncan Sands95d46ef2008-01-23 20:39:46 +0000663 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000664 for (MVT VT : MVT::integer_valuetypes())
665 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000666
Evan Cheng10043e22007-01-19 07:51:42 +0000667 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000668 if (!Subtarget->isThumb1Only()) {
669 for (unsigned im = (unsigned)ISD::PRE_INC;
670 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000671 setIndexedLoadAction(im, MVT::i1, Legal);
672 setIndexedLoadAction(im, MVT::i8, Legal);
673 setIndexedLoadAction(im, MVT::i16, Legal);
674 setIndexedLoadAction(im, MVT::i32, Legal);
675 setIndexedStoreAction(im, MVT::i1, Legal);
676 setIndexedStoreAction(im, MVT::i8, Legal);
677 setIndexedStoreAction(im, MVT::i16, Legal);
678 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000679 }
Evan Cheng10043e22007-01-19 07:51:42 +0000680 }
681
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000682 setOperationAction(ISD::SADDO, MVT::i32, Custom);
683 setOperationAction(ISD::UADDO, MVT::i32, Custom);
684 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
685 setOperationAction(ISD::USUBO, MVT::i32, Custom);
686
Evan Cheng10043e22007-01-19 07:51:42 +0000687 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000688 setOperationAction(ISD::MUL, MVT::i64, Expand);
689 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000690 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000691 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
692 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000693 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000694 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000695 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000696 setOperationAction(ISD::MULHS, MVT::i32, Expand);
697
Jim Grosbach5d994042009-10-31 19:38:01 +0000698 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000699 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000700 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000701 setOperationAction(ISD::SRL, MVT::i64, Custom);
702 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000703
Evan Chenge8916542011-08-30 01:34:54 +0000704 if (!Subtarget->isThumb1Only()) {
705 // FIXME: We should do this for Thumb1 as well.
706 setOperationAction(ISD::ADDC, MVT::i32, Custom);
707 setOperationAction(ISD::ADDE, MVT::i32, Custom);
708 setOperationAction(ISD::SUBC, MVT::i32, Custom);
709 setOperationAction(ISD::SUBE, MVT::i32, Custom);
710 }
711
Evan Cheng10043e22007-01-19 07:51:42 +0000712 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000713 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000714 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000715 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000716 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000717 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000718
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000719 // These just redirect to CTTZ and CTLZ on ARM.
720 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
721 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
722
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000723 // @llvm.readcyclecounter requires the Performance Monitors extension.
724 // Default to the 0 expansion on unsupported platforms.
725 // FIXME: Technically there are older ARM CPUs that have
726 // implementation-specific ways of obtaining this information.
727 if (Subtarget->hasPerfMon())
728 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000729
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000730 // Only ARMv6 has BSWAP.
731 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000732 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000733
Bob Wilsone8a549c2012-09-29 21:43:49 +0000734 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
735 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
736 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000737 setOperationAction(ISD::SDIV, MVT::i32, Expand);
738 setOperationAction(ISD::UDIV, MVT::i32, Expand);
739 }
Renato Golin87610692013-07-16 09:32:17 +0000740
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +0000741 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
742 setOperationAction(ISD::SDIV, MVT::i32, Custom);
743 setOperationAction(ISD::UDIV, MVT::i32, Custom);
744
745 setOperationAction(ISD::SDIV, MVT::i64, Custom);
746 setOperationAction(ISD::UDIV, MVT::i64, Custom);
747 }
748
Chad Rosierad7c9102014-08-23 18:29:43 +0000749 setOperationAction(ISD::SREM, MVT::i32, Expand);
750 setOperationAction(ISD::UREM, MVT::i32, Expand);
751 // Register based DivRem for AEABI (RTABI 4.2)
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +0000752 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000753 setOperationAction(ISD::SREM, MVT::i64, Custom);
754 setOperationAction(ISD::UREM, MVT::i64, Custom);
755
Chad Rosierad7c9102014-08-23 18:29:43 +0000756 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
757 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
758 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
759 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
760 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
761 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
762 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
763 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
764
765 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
766 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
767 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
768 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
769 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
770 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
771 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
772 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
773
774 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
775 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
776 } else {
Renato Golin87610692013-07-16 09:32:17 +0000777 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
778 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
779 }
Bob Wilson7117a912009-03-20 22:42:55 +0000780
Owen Anderson9f944592009-08-11 20:47:22 +0000781 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
782 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
783 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
784 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000785 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000786
Evan Cheng74d92c12011-04-08 21:37:21 +0000787 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000788
Evan Cheng10043e22007-01-19 07:51:42 +0000789 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000790 setOperationAction(ISD::VASTART, MVT::Other, Custom);
791 setOperationAction(ISD::VAARG, MVT::Other, Expand);
792 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
793 setOperationAction(ISD::VAEND, MVT::Other, Expand);
794 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
795 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000796
Tim Northoverd6a729b2014-01-06 14:28:05 +0000797 if (!Subtarget->isTargetMachO()) {
798 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000799 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000800 setExceptionPointerRegister(ARM::R0);
801 setExceptionSelectorRegister(ARM::R1);
802 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000803
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000804 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
805 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
806 else
807 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
808
Evan Cheng6e809de2010-08-11 06:22:01 +0000809 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000810 // the default expansion. If we are targeting a single threaded system,
811 // then set them all for expand so we can lower them later into their
812 // non-atomic form.
813 if (TM.Options.ThreadModel == ThreadModel::Single)
814 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
815 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000816 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
817 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000818 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000819
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000820 // On v8, we have particularly efficient implementations of atomic fences
821 // if they can be combined with nearby atomic loads and stores.
822 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000823 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000824 setInsertFencesForAtomic(true);
825 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000826 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000827 // If there's anything we can use as a barrier, go through custom lowering
828 // for ATOMIC_FENCE.
829 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
830 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
831
Jim Grosbach6860bb72010-06-18 22:35:32 +0000832 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000833 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000834 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000835 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000836 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000837 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000838 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000839 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000840 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000841 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000842 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000843 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000844 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000845 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
846 // Unordered/Monotonic case.
847 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
848 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000849 }
Evan Cheng10043e22007-01-19 07:51:42 +0000850
Evan Cheng21acf9f2010-11-04 05:19:35 +0000851 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000852
Eli Friedman8cfa7712010-06-26 04:36:50 +0000853 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
854 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000855 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
856 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000857 }
Owen Anderson9f944592009-08-11 20:47:22 +0000858 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000859
Eric Christopher824f42f2015-05-12 01:26:05 +0000860 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000861 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000862 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000863 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000864 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000865 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
866 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000867
868 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000869 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000870 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
871 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
872 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
873 if (Subtarget->isTargetDarwin())
John McCall7d84ece2011-05-29 19:50:32 +0000874 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000875
Owen Anderson9f944592009-08-11 20:47:22 +0000876 setOperationAction(ISD::SETCC, MVT::i32, Expand);
877 setOperationAction(ISD::SETCC, MVT::f32, Expand);
878 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000879 setOperationAction(ISD::SELECT, MVT::i32, Custom);
880 setOperationAction(ISD::SELECT, MVT::f32, Custom);
881 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000882 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
883 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
884 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000885
Owen Anderson9f944592009-08-11 20:47:22 +0000886 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
887 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
888 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
889 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
890 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000891
Dan Gohman482732a2007-10-11 23:21:31 +0000892 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000893 setOperationAction(ISD::FSIN, MVT::f64, Expand);
894 setOperationAction(ISD::FSIN, MVT::f32, Expand);
895 setOperationAction(ISD::FCOS, MVT::f32, Expand);
896 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000897 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
898 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000899 setOperationAction(ISD::FREM, MVT::f64, Expand);
900 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000901 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000902 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000903 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
904 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000905 }
Owen Anderson9f944592009-08-11 20:47:22 +0000906 setOperationAction(ISD::FPOW, MVT::f64, Expand);
907 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000908
Evan Chengd0007f32012-04-10 21:40:28 +0000909 if (!Subtarget->hasVFP4()) {
910 setOperationAction(ISD::FMA, MVT::f64, Expand);
911 setOperationAction(ISD::FMA, MVT::f32, Expand);
912 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000913
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000914 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000915 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000916 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
917 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000918 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
919 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
920 }
921
922 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000923 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000924 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
925 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000926 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000927 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000928
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000929 // Combine sin / cos into one node or libcall if possible.
930 if (Subtarget->hasSinCos()) {
931 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
932 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000933 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000934 // For iOS, we don't want to the normal expansion of a libcall to
935 // sincos. We want to issue a libcall to __sincos_stret.
936 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
937 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
938 }
939 }
Evan Cheng10043e22007-01-19 07:51:42 +0000940
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000941 // FP-ARMv8 implements a lot of rounding-like FP operations.
942 if (Subtarget->hasFPARMv8()) {
943 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
944 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
945 setOperationAction(ISD::FROUND, MVT::f32, Legal);
946 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
947 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
948 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000949 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
950 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000951 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
952 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
953 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
954 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
955
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000956 if (!Subtarget->isFPOnlySP()) {
957 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
958 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
959 setOperationAction(ISD::FROUND, MVT::f64, Legal);
960 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
961 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
962 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000963 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
964 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000965 }
966 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000967
James Molloy974838f2015-08-17 19:37:12 +0000968 if (Subtarget->hasNEON()) {
969 // vmin and vmax aren't available in a scalar form, so we use
970 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +0000971 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
972 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +0000973 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
974 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
975 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
976 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
977 }
James Molloydb8ee4b2015-08-11 12:06:15 +0000978
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000979 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000980 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000981 setTargetDAGCombine(ISD::ADD);
982 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000983 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000984 setTargetDAGCombine(ISD::AND);
985 setTargetDAGCombine(ISD::OR);
986 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000987
Evan Chengf258a152012-02-23 02:58:19 +0000988 if (Subtarget->hasV6Ops())
989 setTargetDAGCombine(ISD::SRL);
990
Evan Cheng10043e22007-01-19 07:51:42 +0000991 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000992
Eric Christopher824f42f2015-05-12 01:26:05 +0000993 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000994 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000995 setSchedulingPreference(Sched::RegPressure);
996 else
997 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000998
Evan Cheng3ae2b792011-01-06 06:52:41 +0000999 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001000 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001001 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001002 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001003 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001004 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001005 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001006
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001007 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1008 // are at least 4 bytes aligned.
1009 setMinStackArgumentAlignment(4);
1010
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001011 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001012 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001013
Eli Friedman2518f832011-05-06 20:34:06 +00001014 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001015}
1016
Eric Christopher824f42f2015-05-12 01:26:05 +00001017bool ARMTargetLowering::useSoftFloat() const {
1018 return Subtarget->useSoftFloat();
1019}
1020
Andrew Trick43f25632011-01-19 02:35:27 +00001021// FIXME: It might make sense to define the representative register class as the
1022// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1023// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1024// SPR's representative would be DPR_VFP2. This should work well if register
1025// pressure tracking were modified such that a register use would increment the
1026// pressure of the register class's representative and all of it's super
1027// classes' representatives transitively. We have not implemented this because
1028// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001029// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001030// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001031std::pair<const TargetRegisterClass *, uint8_t>
1032ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1033 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001034 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001035 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001036 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001037 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001038 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001039 // Use DPR as representative register class for all floating point
1040 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1041 // the cost is 1 for both f32 and f64.
1042 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001043 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001044 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001045 // When NEON is used for SP, only half of the register file is available
1046 // because operations that define both SP and DP results will be constrained
1047 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1048 // coalescing by double-counting the SP regs. See the FIXME above.
1049 if (Subtarget->useNEONForSinglePrecisionFP())
1050 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001051 break;
1052 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1053 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001054 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001055 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001056 break;
1057 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001058 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001059 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001060 break;
1061 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001062 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001063 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001064 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001065 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001066 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001067}
1068
Evan Cheng10043e22007-01-19 07:51:42 +00001069const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001070 switch ((ARMISD::NodeType)Opcode) {
1071 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001072 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001073 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001074 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001075 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001076 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001077 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001078 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1079 case ARMISD::tCALL: return "ARMISD::tCALL";
1080 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1081 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001082 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001083 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001084 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001085 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1086 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001087 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001088 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001089 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1090 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001091 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001092 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001093
Evan Cheng10043e22007-01-19 07:51:42 +00001094 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001095
Jim Grosbach8546ec92010-01-18 19:58:49 +00001096 case ARMISD::RBIT: return "ARMISD::RBIT";
1097
Evan Cheng10043e22007-01-19 07:51:42 +00001098 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1099 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1100 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001101
Evan Chenge8916542011-08-30 01:34:54 +00001102 case ARMISD::ADDC: return "ARMISD::ADDC";
1103 case ARMISD::ADDE: return "ARMISD::ADDE";
1104 case ARMISD::SUBC: return "ARMISD::SUBC";
1105 case ARMISD::SUBE: return "ARMISD::SUBE";
1106
Bob Wilson22806742010-09-22 22:09:21 +00001107 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1108 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001109
Evan Chengec6d7c92009-10-28 06:55:03 +00001110 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001111 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1112 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001113
Dale Johannesend679ff72010-06-03 21:09:53 +00001114 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001115
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001116 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001117
Evan Chengb972e562009-08-07 00:34:42 +00001118 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1119
Bob Wilson7ed59712010-10-30 00:54:37 +00001120 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001121
Evan Cheng8740ee32010-11-03 06:34:55 +00001122 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1123
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001124 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001125 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001126
Bob Wilson2e076c42009-06-22 23:27:02 +00001127 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001128 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001129 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001130 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1131 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001132 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1133 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001134 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1135 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001136 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1137 case ARMISD::VTST: return "ARMISD::VTST";
1138
1139 case ARMISD::VSHL: return "ARMISD::VSHL";
1140 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1141 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001142 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1143 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1144 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1145 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1146 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1147 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1148 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1149 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1150 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1151 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1152 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1153 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001154 case ARMISD::VSLI: return "ARMISD::VSLI";
1155 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001156 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1157 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001158 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001159 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001160 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001161 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001162 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001163 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001164 case ARMISD::VREV64: return "ARMISD::VREV64";
1165 case ARMISD::VREV32: return "ARMISD::VREV32";
1166 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001167 case ARMISD::VZIP: return "ARMISD::VZIP";
1168 case ARMISD::VUZP: return "ARMISD::VUZP";
1169 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001170 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1171 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001172 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1173 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001174 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1175 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001176 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001177 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001178 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1179 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001180 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001181 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001182 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1183 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1184 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001185 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1186 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1187 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1188 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1189 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1190 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1191 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1192 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1193 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1194 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1195 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1196 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1197 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1198 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1199 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1200 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1201 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001202 }
Matthias Braund04893f2015-05-07 21:33:59 +00001203 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001204}
1205
Mehdi Amini44ede332015-07-09 02:09:04 +00001206EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1207 EVT VT) const {
1208 if (!VT.isVector())
1209 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001210 return VT.changeVectorElementTypeToInteger();
1211}
1212
Evan Cheng4cad68e2010-05-15 02:18:07 +00001213/// getRegClassFor - Return the register class that should be used for the
1214/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001215const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001216 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1217 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1218 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001219 if (Subtarget->hasNEON()) {
1220 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001221 return &ARM::QQPRRegClass;
1222 if (VT == MVT::v8i64)
1223 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001224 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001225 return TargetLowering::getRegClassFor(VT);
1226}
1227
John Brawn0dbcd652015-03-18 12:01:59 +00001228// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1229// source/dest is aligned and the copy size is large enough. We therefore want
1230// to align such objects passed to memory intrinsics.
1231bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1232 unsigned &PrefAlign) const {
1233 if (!isa<MemIntrinsic>(CI))
1234 return false;
1235 MinSize = 8;
1236 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1237 // cycle faster than 4-byte aligned LDM.
1238 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1239 return true;
1240}
1241
Eric Christopher84bdfd82010-07-21 22:26:11 +00001242// Create a fast isel object.
1243FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001244ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1245 const TargetLibraryInfo *libInfo) const {
1246 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001247}
1248
Evan Cheng4401f882010-05-20 23:26:43 +00001249Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001250 unsigned NumVals = N->getNumValues();
1251 if (!NumVals)
1252 return Sched::RegPressure;
1253
1254 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001255 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001256 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001257 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001258 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001259 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001260 }
Evan Chengbf914992010-05-28 23:25:23 +00001261
1262 if (!N->isMachineOpcode())
1263 return Sched::RegPressure;
1264
1265 // Load are scheduled for latency even if there instruction itinerary
1266 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001267 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001268 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001269
Evan Cheng6cc775f2011-06-28 19:10:37 +00001270 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001271 return Sched::RegPressure;
1272 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001273 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001274 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001275
Evan Cheng4401f882010-05-20 23:26:43 +00001276 return Sched::RegPressure;
1277}
1278
Evan Cheng10043e22007-01-19 07:51:42 +00001279//===----------------------------------------------------------------------===//
1280// Lowering Code
1281//===----------------------------------------------------------------------===//
1282
Evan Cheng10043e22007-01-19 07:51:42 +00001283/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1284static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1285 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001286 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001287 case ISD::SETNE: return ARMCC::NE;
1288 case ISD::SETEQ: return ARMCC::EQ;
1289 case ISD::SETGT: return ARMCC::GT;
1290 case ISD::SETGE: return ARMCC::GE;
1291 case ISD::SETLT: return ARMCC::LT;
1292 case ISD::SETLE: return ARMCC::LE;
1293 case ISD::SETUGT: return ARMCC::HI;
1294 case ISD::SETUGE: return ARMCC::HS;
1295 case ISD::SETULT: return ARMCC::LO;
1296 case ISD::SETULE: return ARMCC::LS;
1297 }
1298}
1299
Bob Wilsona2e83332009-09-09 23:14:54 +00001300/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1301static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001302 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001303 CondCode2 = ARMCC::AL;
1304 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001305 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001306 case ISD::SETEQ:
1307 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1308 case ISD::SETGT:
1309 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1310 case ISD::SETGE:
1311 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1312 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001313 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001314 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1315 case ISD::SETO: CondCode = ARMCC::VC; break;
1316 case ISD::SETUO: CondCode = ARMCC::VS; break;
1317 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1318 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1319 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1320 case ISD::SETLT:
1321 case ISD::SETULT: CondCode = ARMCC::LT; break;
1322 case ISD::SETLE:
1323 case ISD::SETULE: CondCode = ARMCC::LE; break;
1324 case ISD::SETNE:
1325 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1326 }
Evan Cheng10043e22007-01-19 07:51:42 +00001327}
1328
Bob Wilsona4c22902009-04-17 19:07:39 +00001329//===----------------------------------------------------------------------===//
1330// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001331//===----------------------------------------------------------------------===//
1332
1333#include "ARMGenCallingConv.inc"
1334
Oliver Stannardc24f2172014-05-09 14:01:47 +00001335/// getEffectiveCallingConv - Get the effective calling convention, taking into
1336/// account presence of floating point hardware and calling convention
1337/// limitations, such as support for variadic functions.
1338CallingConv::ID
1339ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1340 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001341 switch (CC) {
1342 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001343 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001344 case CallingConv::ARM_AAPCS:
1345 case CallingConv::ARM_APCS:
1346 case CallingConv::GHC:
1347 return CC;
1348 case CallingConv::ARM_AAPCS_VFP:
1349 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1350 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001351 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001352 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001353 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001354 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1355 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001356 return CallingConv::ARM_AAPCS_VFP;
1357 else
1358 return CallingConv::ARM_AAPCS;
1359 case CallingConv::Fast:
1360 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001361 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001362 return CallingConv::Fast;
1363 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001364 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001365 return CallingConv::ARM_AAPCS_VFP;
1366 else
1367 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001368 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001369}
1370
1371/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1372/// CallingConvention.
1373CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1374 bool Return,
1375 bool isVarArg) const {
1376 switch (getEffectiveCallingConv(CC, isVarArg)) {
1377 default:
1378 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001379 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001380 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001381 case CallingConv::ARM_AAPCS:
1382 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1383 case CallingConv::ARM_AAPCS_VFP:
1384 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1385 case CallingConv::Fast:
1386 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001387 case CallingConv::GHC:
1388 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001389 }
1390}
1391
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001392/// LowerCallResult - Lower the result values of a call into the
1393/// appropriate copies out of appropriate physical registers.
1394SDValue
1395ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001396 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001397 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001398 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001399 SmallVectorImpl<SDValue> &InVals,
1400 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001401
Bob Wilsona4c22902009-04-17 19:07:39 +00001402 // Assign locations to each value returned by this call.
1403 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001404 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1405 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001406 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001407 CCAssignFnForNode(CallConv, /* Return*/ true,
1408 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001409
1410 // Copy all of the result registers out of their specified physreg.
1411 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1412 CCValAssign VA = RVLocs[i];
1413
Stephen Linb8bd2322013-04-20 05:14:40 +00001414 // Pass 'this' value directly from the argument to return value, to avoid
1415 // reg unit interference
1416 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001417 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1418 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001419 InVals.push_back(ThisVal);
1420 continue;
1421 }
1422
Bob Wilson0041bd32009-04-25 00:33:20 +00001423 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001424 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001425 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001426 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001427 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001428 Chain = Lo.getValue(1);
1429 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001430 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001431 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001432 InFlag);
1433 Chain = Hi.getValue(1);
1434 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001435 if (!Subtarget->isLittle())
1436 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001437 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001438
Owen Anderson9f944592009-08-11 20:47:22 +00001439 if (VA.getLocVT() == MVT::v2f64) {
1440 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1441 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001442 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001443
1444 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001445 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001446 Chain = Lo.getValue(1);
1447 InFlag = Lo.getValue(2);
1448 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001449 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001450 Chain = Hi.getValue(1);
1451 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001452 if (!Subtarget->isLittle())
1453 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001454 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001455 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001456 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001457 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001458 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001459 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1460 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001461 Chain = Val.getValue(1);
1462 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001463 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001464
1465 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001466 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001467 case CCValAssign::Full: break;
1468 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001469 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001470 break;
1471 }
1472
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001473 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001474 }
1475
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001476 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001477}
1478
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001479/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001480SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001481ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1482 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001483 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001484 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001485 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001486 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001487 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001488 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1489 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001490 return DAG.getStore(
1491 Chain, dl, Arg, PtrOff,
1492 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1493 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001494}
1495
Andrew Trickef9de2a2013-05-25 02:42:55 +00001496void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001497 SDValue Chain, SDValue &Arg,
1498 RegsToPassVector &RegsToPass,
1499 CCValAssign &VA, CCValAssign &NextVA,
1500 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001501 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001502 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001503
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001504 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001505 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001506 unsigned id = Subtarget->isLittle() ? 0 : 1;
1507 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001508
1509 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001510 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001511 else {
1512 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001513 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001514 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1515 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001516
Christian Pirkerb5728192014-05-08 14:06:24 +00001517 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001518 dl, DAG, NextVA,
1519 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001520 }
1521}
1522
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001523/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001524/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1525/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001526SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001527ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001528 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001529 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001530 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001531 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1532 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1533 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001534 SDValue Chain = CLI.Chain;
1535 SDValue Callee = CLI.Callee;
1536 bool &isTailCall = CLI.IsTailCall;
1537 CallingConv::ID CallConv = CLI.CallConv;
1538 bool doesNotRet = CLI.DoesNotReturn;
1539 bool isVarArg = CLI.IsVarArg;
1540
Dale Johannesend679ff72010-06-03 21:09:53 +00001541 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001542 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1543 bool isThisReturn = false;
1544 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001545 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001546
Bob Wilson8decdc42011-10-07 17:17:49 +00001547 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001548 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001549 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001550
Dale Johannesend679ff72010-06-03 21:09:53 +00001551 if (isTailCall) {
1552 // Check if it's really possible to do a tail call.
1553 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001554 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001555 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001556 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1557 report_fatal_error("failed to perform tail call elimination on a call "
1558 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001559 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1560 // detected sibcalls.
1561 if (isTailCall) {
1562 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001563 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001564 }
1565 }
Evan Cheng10043e22007-01-19 07:51:42 +00001566
Bob Wilsona4c22902009-04-17 19:07:39 +00001567 // Analyze operands of the call, assigning locations to each operand.
1568 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001569 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1570 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001571 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001572 CCAssignFnForNode(CallConv, /* Return*/ false,
1573 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001574
Bob Wilsona4c22902009-04-17 19:07:39 +00001575 // Get a count of how many bytes are to be pushed on the stack.
1576 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001577
Dale Johannesend679ff72010-06-03 21:09:53 +00001578 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001579 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001580 NumBytes = 0;
1581
Evan Cheng10043e22007-01-19 07:51:42 +00001582 // Adjust the stack pointer for the new arguments...
1583 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001584 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001585 Chain = DAG.getCALLSEQ_START(Chain,
1586 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001587
Mehdi Amini44ede332015-07-09 02:09:04 +00001588 SDValue StackPtr =
1589 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001590
Bob Wilson2e076c42009-06-22 23:27:02 +00001591 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001592 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001593
Bob Wilsona4c22902009-04-17 19:07:39 +00001594 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001595 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001596 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1597 i != e;
1598 ++i, ++realArgIdx) {
1599 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001600 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001601 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001602 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001603
Bob Wilsona4c22902009-04-17 19:07:39 +00001604 // Promote the value if needed.
1605 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001606 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001607 case CCValAssign::Full: break;
1608 case CCValAssign::SExt:
1609 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1610 break;
1611 case CCValAssign::ZExt:
1612 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1613 break;
1614 case CCValAssign::AExt:
1615 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1616 break;
1617 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001618 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001619 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001620 }
1621
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001622 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001623 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001624 if (VA.getLocVT() == MVT::v2f64) {
1625 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001627 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001628 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001629
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001630 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001631 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1632
1633 VA = ArgLocs[++i]; // skip ahead to next loc
1634 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001635 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001636 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1637 } else {
1638 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001639
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001640 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1641 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001642 }
1643 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001644 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001645 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001646 }
1647 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001648 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1649 assert(VA.getLocVT() == MVT::i32 &&
1650 "unexpected calling convention register assignment");
1651 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001652 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001653 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001654 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001655 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001656 } else if (isByVal) {
1657 assert(VA.isMemLoc());
1658 unsigned offset = 0;
1659
1660 // True if this byval aggregate will be split between registers
1661 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001662 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001663 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001664
1665 if (CurByValIdx < ByValArgsCount) {
1666
1667 unsigned RegBegin, RegEnd;
1668 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1669
Mehdi Amini44ede332015-07-09 02:09:04 +00001670 EVT PtrVT =
1671 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001672 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001673 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001674 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001675 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1676 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1677 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001678 false, false, false,
1679 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001680 MemOpChains.push_back(Load.getValue(1));
1681 RegsToPass.push_back(std::make_pair(j, Load));
1682 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001683
1684 // If parameter size outsides register area, "offset" value
1685 // helps us to calculate stack slot for remained part properly.
1686 offset = RegEnd - RegBegin;
1687
1688 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001689 }
1690
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001691 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001692 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001693 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001694 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001695 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001696 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001697 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001698 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001699 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001700 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1701 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001702
Manman Ren9f911162012-06-01 02:44:42 +00001703 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001704 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001705 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001706 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001707 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001708 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001709 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001710
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001711 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1712 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001713 }
Evan Cheng10043e22007-01-19 07:51:42 +00001714 }
1715
1716 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001717 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001718
1719 // Build a sequence of copy-to-reg nodes chained together with token chain
1720 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001721 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001722 // Tail call byval lowering might overwrite argument registers so in case of
1723 // tail call optimization the copies to registers are lowered later.
1724 if (!isTailCall)
1725 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1726 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1727 RegsToPass[i].second, InFlag);
1728 InFlag = Chain.getValue(1);
1729 }
Evan Cheng10043e22007-01-19 07:51:42 +00001730
Dale Johannesend679ff72010-06-03 21:09:53 +00001731 // For tail calls lower the arguments to the 'real' stack slot.
1732 if (isTailCall) {
1733 // Force all the incoming stack arguments to be loaded from the stack
1734 // before any new outgoing arguments are stored to the stack, because the
1735 // outgoing stack slots may alias the incoming argument stack slots, and
1736 // the alias isn't otherwise explicit. This is slightly more conservative
1737 // than necessary, because it means that each store effectively depends
1738 // on every argument instead of just those arguments it would clobber.
1739
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001740 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001741 InFlag = SDValue();
1742 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1743 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1744 RegsToPass[i].second, InFlag);
1745 InFlag = Chain.getValue(1);
1746 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001747 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001748 }
1749
Bill Wendling24c79f22008-09-16 21:48:12 +00001750 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1751 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1752 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001753 bool isDirect = false;
1754 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001755 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001756 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001757 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001758
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001759 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001760 assert((Subtarget->isTargetWindows() ||
1761 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1762 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001763 // Handle a global address or an external symbol. If it's not one of
1764 // those, the target's already in a register, so we don't need to do
1765 // anything extra.
1766 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001767 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001768 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001769 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001770 ARMConstantPoolValue *CPV =
1771 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1772
Jim Grosbach32bb3622010-04-14 22:28:31 +00001773 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001774 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001775 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001776 Callee = DAG.getLoad(
1777 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1778 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1779 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001780 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1781 const char *Sym = S->getSymbol();
1782
1783 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001784 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001785 ARMConstantPoolValue *CPV =
1786 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1787 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001788 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001789 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001790 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001791 Callee = DAG.getLoad(
1792 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1793 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1794 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001795 }
1796 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001797 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001798 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001799 bool isDef = GV->isStrongDefinitionForLinker();
1800 bool isStub = (!isDef && 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 Chengc3c949b42007-06-19 21:05:09 +00001803 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001804 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001805 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001806 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001807 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001808 Callee = DAG.getNode(
1809 ARMISD::WrapperPIC, dl, PtrVt,
1810 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1811 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001812 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1813 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001814 } else if (Subtarget->isTargetCOFF()) {
1815 assert(Subtarget->isTargetWindows() &&
1816 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001817 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1818 ? ARMII::MO_DLLIMPORT
1819 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001820 Callee =
1821 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001822 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001823 Callee =
1824 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1825 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001826 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1827 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001828 } else {
1829 // On ELF targets for PIC code, direct calls should go through the PLT
1830 unsigned OpFlags = 0;
1831 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001832 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001833 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001834 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001835 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001836 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001837 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001838 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001839 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001840 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001841 // tBX takes a register source operand.
1842 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001843 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001844 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001845 ARMConstantPoolValue *CPV =
1846 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1847 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001848 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001849 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001850 Callee = DAG.getLoad(
1851 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1852 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1853 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001854 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001855 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001856 } else {
1857 unsigned OpFlags = 0;
1858 // On ELF targets for PIC code, direct calls should go through the PLT
1859 if (Subtarget->isTargetELF() &&
1860 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1861 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001862 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001863 }
Evan Cheng10043e22007-01-19 07:51:42 +00001864 }
1865
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001866 // FIXME: handle tail calls differently.
1867 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001868 if (Subtarget->isThumb()) {
1869 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001870 CallOpc = ARMISD::CALL_NOLINK;
1871 else
1872 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1873 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001874 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001875 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001876 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001877 // Emit regular call when code size is the priority
1878 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001879 // "mov lr, pc; b _foo" to avoid confusing the RSP
1880 CallOpc = ARMISD::CALL_NOLINK;
1881 else
1882 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001883 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001884
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001885 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001886 Ops.push_back(Chain);
1887 Ops.push_back(Callee);
1888
1889 // Add argument registers to the end of the list so that they are known live
1890 // into the call.
1891 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1892 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1893 RegsToPass[i].second.getValueType()));
1894
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001895 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001896 if (!isTailCall) {
1897 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001898 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001899 if (isThisReturn) {
1900 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001901 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001902 if (!Mask) {
1903 // Set isThisReturn to false if the calling convention is not one that
1904 // allows 'returned' to be modeled in this way, so LowerCallResult does
1905 // not try to pass 'this' straight through
1906 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001907 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001908 }
1909 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001910 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001911
Matthias Braunc22630e2013-10-04 16:52:54 +00001912 assert(Mask && "Missing call preserved mask for calling convention");
1913 Ops.push_back(DAG.getRegisterMask(Mask));
1914 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001915
Gabor Greiff304a7a2008-08-28 21:40:38 +00001916 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001917 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001918
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001919 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001920 if (isTailCall) {
1921 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001922 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001923 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001924
Duncan Sands739a0542008-07-02 17:40:58 +00001925 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001926 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001927 InFlag = Chain.getValue(1);
1928
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001929 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1930 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001931 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001932 InFlag = Chain.getValue(1);
1933
Bob Wilsona4c22902009-04-17 19:07:39 +00001934 // Handle result values, copying them out of physregs into vregs that we
1935 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001936 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001937 InVals, isThisReturn,
1938 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001939}
1940
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001941/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001942/// on the stack. Remember the next parameter register to allocate,
1943/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001944/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001945void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1946 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001947 assert((State->getCallOrPrologue() == Prologue ||
1948 State->getCallOrPrologue() == Call) &&
1949 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001950
Tim Northover8cda34f2015-03-11 18:54:22 +00001951 // Byval (as with any stack) slots are always at least 4 byte aligned.
1952 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001953
Tim Northover8cda34f2015-03-11 18:54:22 +00001954 unsigned Reg = State->AllocateReg(GPRArgRegs);
1955 if (!Reg)
1956 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001957
Tim Northover8cda34f2015-03-11 18:54:22 +00001958 unsigned AlignInRegs = Align / 4;
1959 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1960 for (unsigned i = 0; i < Waste; ++i)
1961 Reg = State->AllocateReg(GPRArgRegs);
1962
1963 if (!Reg)
1964 return;
1965
1966 unsigned Excess = 4 * (ARM::R4 - Reg);
1967
1968 // Special case when NSAA != SP and parameter size greater than size of
1969 // all remained GPR regs. In that case we can't split parameter, we must
1970 // send it to stack. We also must set NCRN to R4, so waste all
1971 // remained registers.
1972 const unsigned NSAAOffset = State->getNextStackOffset();
1973 if (NSAAOffset != 0 && Size > Excess) {
1974 while (State->AllocateReg(GPRArgRegs))
1975 ;
1976 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001977 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001978
1979 // First register for byval parameter is the first register that wasn't
1980 // allocated before this method call, so it would be "reg".
1981 // If parameter is small enough to be saved in range [reg, r4), then
1982 // the end (first after last) register would be reg + param-size-in-regs,
1983 // else parameter would be splitted between registers and stack,
1984 // end register would be r4 in this case.
1985 unsigned ByValRegBegin = Reg;
1986 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1987 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1988 // Note, first register is allocated in the beginning of function already,
1989 // allocate remained amount of registers we need.
1990 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1991 State->AllocateReg(GPRArgRegs);
1992 // A byval parameter that is split between registers and memory needs its
1993 // size truncated here.
1994 // In the case where the entire structure fits in registers, we set the
1995 // size in memory to zero.
1996 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001997}
1998
Dale Johannesend679ff72010-06-03 21:09:53 +00001999/// MatchingStackOffset - Return true if the given stack call argument is
2000/// already available in the same position (relatively) of the caller's
2001/// incoming argument stack.
2002static
2003bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2004 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002005 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002006 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2007 int FI = INT_MAX;
2008 if (Arg.getOpcode() == ISD::CopyFromReg) {
2009 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002010 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002011 return false;
2012 MachineInstr *Def = MRI->getVRegDef(VR);
2013 if (!Def)
2014 return false;
2015 if (!Flags.isByVal()) {
2016 if (!TII->isLoadFromStackSlot(Def, FI))
2017 return false;
2018 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002019 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002020 }
2021 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2022 if (Flags.isByVal())
2023 // ByVal argument is passed in as a pointer but it's now being
2024 // dereferenced. e.g.
2025 // define @foo(%struct.X* %A) {
2026 // tail call @bar(%struct.X* byval %A)
2027 // }
2028 return false;
2029 SDValue Ptr = Ld->getBasePtr();
2030 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2031 if (!FINode)
2032 return false;
2033 FI = FINode->getIndex();
2034 } else
2035 return false;
2036
2037 assert(FI != INT_MAX);
2038 if (!MFI->isFixedObjectIndex(FI))
2039 return false;
2040 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2041}
2042
2043/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2044/// for tail call optimization. Targets which want to do tail call
2045/// optimization should implement this function.
2046bool
2047ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2048 CallingConv::ID CalleeCC,
2049 bool isVarArg,
2050 bool isCalleeStructRet,
2051 bool isCallerStructRet,
2052 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002053 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002054 const SmallVectorImpl<ISD::InputArg> &Ins,
2055 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002056 const Function *CallerF = DAG.getMachineFunction().getFunction();
2057 CallingConv::ID CallerCC = CallerF->getCallingConv();
2058 bool CCMatch = CallerCC == CalleeCC;
2059
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002060 assert(Subtarget->supportsTailCall());
2061
Dale Johannesend679ff72010-06-03 21:09:53 +00002062 // Look for obvious safe cases to perform tail call optimization that do not
2063 // require ABI changes. This is what gcc calls sibcall.
2064
Jim Grosbache3864cc2010-06-16 23:45:49 +00002065 // Do not sibcall optimize vararg calls unless the call site is not passing
2066 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002067 if (isVarArg && !Outs.empty())
2068 return false;
2069
Tim Northoverd8407452013-10-01 14:33:28 +00002070 // Exception-handling functions need a special set of instructions to indicate
2071 // a return to the hardware. Tail-calling another function would probably
2072 // break this.
2073 if (CallerF->hasFnAttribute("interrupt"))
2074 return false;
2075
Dale Johannesend679ff72010-06-03 21:09:53 +00002076 // Also avoid sibcall optimization if either caller or callee uses struct
2077 // return semantics.
2078 if (isCalleeStructRet || isCallerStructRet)
2079 return false;
2080
Oliver Stannard12993dd2014-08-18 12:42:15 +00002081 // Externally-defined functions with weak linkage should not be
2082 // tail-called on ARM when the OS does not support dynamic
2083 // pre-emption of symbols, as the AAELF spec requires normal calls
2084 // to undefined weak functions to be replaced with a NOP or jump to the
2085 // next instruction. The behaviour of branch instructions in this
2086 // situation (as used for tail calls) is implementation-defined, so we
2087 // cannot rely on the linker replacing the tail call with a return.
2088 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2089 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002090 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002091 if (GV->hasExternalWeakLinkage() &&
2092 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002093 return false;
2094 }
2095
Dale Johannesend679ff72010-06-03 21:09:53 +00002096 // If the calling conventions do not match, then we'd better make sure the
2097 // results are returned in the same way as what the caller expects.
2098 if (!CCMatch) {
2099 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002100 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2101 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002102 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2103
2104 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002105 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2106 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002107 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2108
2109 if (RVLocs1.size() != RVLocs2.size())
2110 return false;
2111 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2112 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2113 return false;
2114 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2115 return false;
2116 if (RVLocs1[i].isRegLoc()) {
2117 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2118 return false;
2119 } else {
2120 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2121 return false;
2122 }
2123 }
2124 }
2125
Manman Ren7e48b252012-10-12 23:39:43 +00002126 // If Caller's vararg or byval argument has been split between registers and
2127 // stack, do not perform tail call, since part of the argument is in caller's
2128 // local frame.
2129 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2130 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002131 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002132 return false;
2133
Dale Johannesend679ff72010-06-03 21:09:53 +00002134 // If the callee takes no arguments then go on to check the results of the
2135 // call.
2136 if (!Outs.empty()) {
2137 // Check if stack adjustment is needed. For now, do not do this if any
2138 // argument is passed on the stack.
2139 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002140 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2141 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002142 CCInfo.AnalyzeCallOperands(Outs,
2143 CCAssignFnForNode(CalleeCC, false, isVarArg));
2144 if (CCInfo.getNextStackOffset()) {
2145 MachineFunction &MF = DAG.getMachineFunction();
2146
2147 // Check if the arguments are already laid out in the right way as
2148 // the caller's fixed stack objects.
2149 MachineFrameInfo *MFI = MF.getFrameInfo();
2150 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002151 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002152 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2153 i != e;
2154 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002155 CCValAssign &VA = ArgLocs[i];
2156 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002157 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002158 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002159 if (VA.getLocInfo() == CCValAssign::Indirect)
2160 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002161 if (VA.needsCustom()) {
2162 // f64 and vector types are split into multiple registers or
2163 // register/stack-slot combinations. The types will not match
2164 // the registers; give up on memory f64 refs until we figure
2165 // out what to do about this.
2166 if (!VA.isRegLoc())
2167 return false;
2168 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002169 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002170 if (RegVT == MVT::v2f64) {
2171 if (!ArgLocs[++i].isRegLoc())
2172 return false;
2173 if (!ArgLocs[++i].isRegLoc())
2174 return false;
2175 }
2176 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002177 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2178 MFI, MRI, TII))
2179 return false;
2180 }
2181 }
2182 }
2183 }
2184
2185 return true;
2186}
2187
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002188bool
2189ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2190 MachineFunction &MF, bool isVarArg,
2191 const SmallVectorImpl<ISD::OutputArg> &Outs,
2192 LLVMContext &Context) const {
2193 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002194 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002195 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2196 isVarArg));
2197}
2198
Tim Northoverd8407452013-10-01 14:33:28 +00002199static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2200 SDLoc DL, SelectionDAG &DAG) {
2201 const MachineFunction &MF = DAG.getMachineFunction();
2202 const Function *F = MF.getFunction();
2203
2204 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2205
2206 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2207 // version of the "preferred return address". These offsets affect the return
2208 // instruction if this is a return from PL1 without hypervisor extensions.
2209 // IRQ/FIQ: +4 "subs pc, lr, #4"
2210 // SWI: 0 "subs pc, lr, #0"
2211 // ABORT: +4 "subs pc, lr, #4"
2212 // UNDEF: +4/+2 "subs pc, lr, #0"
2213 // UNDEF varies depending on where the exception came from ARM or Thumb
2214 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2215
2216 int64_t LROffset;
2217 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2218 IntKind == "ABORT")
2219 LROffset = 4;
2220 else if (IntKind == "SWI" || IntKind == "UNDEF")
2221 LROffset = 0;
2222 else
2223 report_fatal_error("Unsupported interrupt attribute. If present, value "
2224 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2225
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002226 RetOps.insert(RetOps.begin() + 1,
2227 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002228
Craig Topper48d114b2014-04-26 18:35:24 +00002229 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002230}
2231
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002232SDValue
2233ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002234 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002235 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002236 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002237 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002238
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002239 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002240 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002241
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002242 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002243 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2244 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002245
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002246 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002247 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2248 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002249
Bob Wilsona4c22902009-04-17 19:07:39 +00002250 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002251 SmallVector<SDValue, 4> RetOps;
2252 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002253 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002254
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002255 MachineFunction &MF = DAG.getMachineFunction();
2256 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2257 AFI->setReturnRegsCount(RVLocs.size());
2258
Bob Wilsona4c22902009-04-17 19:07:39 +00002259 // Copy the result values into the output registers.
2260 for (unsigned i = 0, realRVLocIdx = 0;
2261 i != RVLocs.size();
2262 ++i, ++realRVLocIdx) {
2263 CCValAssign &VA = RVLocs[i];
2264 assert(VA.isRegLoc() && "Can only return in registers!");
2265
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002266 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002267
2268 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002269 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002270 case CCValAssign::Full: break;
2271 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002272 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002273 break;
2274 }
2275
Bob Wilsona4c22902009-04-17 19:07:39 +00002276 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002277 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002278 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002279 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002280 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002281 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002282 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002283
Christian Pirkerb5728192014-05-08 14:06:24 +00002284 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2285 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2286 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002287 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002288 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002289 VA = RVLocs[++i]; // skip ahead to next loc
2290 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002291 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2292 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002293 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002294 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002295 VA = RVLocs[++i]; // skip ahead to next loc
2296
2297 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002298 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002299 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002300 }
2301 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2302 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002303 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002304 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002305 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2306 fmrrd.getValue(isLittleEndian ? 0 : 1),
2307 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002308 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002309 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002310 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002311 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2312 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002313 Flag);
2314 } else
2315 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2316
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002317 // Guarantee that all emitted copies are
2318 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002319 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002320 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002321 }
2322
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002323 // Update chain and glue.
2324 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002325 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002326 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002327
Tim Northoverd8407452013-10-01 14:33:28 +00002328 // CPUs which aren't M-class use a special sequence to return from
2329 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2330 // though we use "subs pc, lr, #N").
2331 //
2332 // M-class CPUs actually use a normal return sequence with a special
2333 // (hardware-provided) value in LR, so the normal code path works.
2334 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2335 !Subtarget->isMClass()) {
2336 if (Subtarget->isThumb1Only())
2337 report_fatal_error("interrupt attribute is not supported in Thumb1");
2338 return LowerInterruptReturn(RetOps, dl, DAG);
2339 }
2340
Craig Topper48d114b2014-04-26 18:35:24 +00002341 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002342}
2343
Evan Chengf8bad082012-04-10 01:51:00 +00002344bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002345 if (N->getNumValues() != 1)
2346 return false;
2347 if (!N->hasNUsesOfValue(1, 0))
2348 return false;
2349
Evan Chengf8bad082012-04-10 01:51:00 +00002350 SDValue TCChain = Chain;
2351 SDNode *Copy = *N->use_begin();
2352 if (Copy->getOpcode() == ISD::CopyToReg) {
2353 // If the copy has a glue operand, we conservatively assume it isn't safe to
2354 // perform a tail call.
2355 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2356 return false;
2357 TCChain = Copy->getOperand(0);
2358 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2359 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002360 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002361 SmallPtrSet<SDNode*, 2> Copies;
2362 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002363 UI != UE; ++UI) {
2364 if (UI->getOpcode() != ISD::CopyToReg)
2365 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002366 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002367 }
Evan Chengf8bad082012-04-10 01:51:00 +00002368 if (Copies.size() > 2)
2369 return false;
2370
2371 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2372 UI != UE; ++UI) {
2373 SDValue UseChain = UI->getOperand(0);
2374 if (Copies.count(UseChain.getNode()))
2375 // Second CopyToReg
2376 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002377 else {
2378 // We are at the top of this chain.
2379 // If the copy has a glue operand, we conservatively assume it
2380 // isn't safe to perform a tail call.
2381 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2382 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002383 // First CopyToReg
2384 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002385 }
Evan Chengf8bad082012-04-10 01:51:00 +00002386 }
2387 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002388 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002389 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002390 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002391 Copy = *Copy->use_begin();
2392 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002393 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002394 // If the copy has a glue operand, we conservatively assume it isn't safe to
2395 // perform a tail call.
2396 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2397 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002398 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002399 } else {
2400 return false;
2401 }
2402
Evan Cheng419ea282010-12-01 22:59:46 +00002403 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002404 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2405 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002406 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2407 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002408 return false;
2409 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002410 }
2411
Evan Chengf8bad082012-04-10 01:51:00 +00002412 if (!HasRet)
2413 return false;
2414
2415 Chain = TCChain;
2416 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002417}
2418
Evan Cheng0663f232011-03-21 01:19:09 +00002419bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002420 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002421 return false;
2422
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002423 auto Attr =
2424 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2425 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002426 return false;
2427
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002428 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002429}
2430
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002431// Trying to write a 64 bit value so need to split into two 32 bit values first,
2432// and pass the lower and high parts through.
2433static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2434 SDLoc DL(Op);
2435 SDValue WriteValue = Op->getOperand(2);
2436
2437 // This function is only supposed to be called for i64 type argument.
2438 assert(WriteValue.getValueType() == MVT::i64
2439 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2440
2441 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2442 DAG.getConstant(0, DL, MVT::i32));
2443 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2444 DAG.getConstant(1, DL, MVT::i32));
2445 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2446 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2447}
2448
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002449// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2450// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2451// one of the above mentioned nodes. It has to be wrapped because otherwise
2452// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2453// be used to form addressing mode. These wrapped nodes will be selected
2454// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002455static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002456 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002457 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002458 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002459 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002460 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002461 if (CP->isMachineConstantPoolEntry())
2462 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2463 CP->getAlignment());
2464 else
2465 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2466 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002467 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002468}
2469
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002470unsigned ARMTargetLowering::getJumpTableEncoding() const {
2471 return MachineJumpTableInfo::EK_Inline;
2472}
2473
Dan Gohman21cea8a2010-04-17 15:26:15 +00002474SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2475 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002476 MachineFunction &MF = DAG.getMachineFunction();
2477 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2478 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002479 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002480 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002481 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002482 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2483 SDValue CPAddr;
2484 if (RelocM == Reloc::Static) {
2485 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2486 } else {
2487 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002488 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002489 ARMConstantPoolValue *CPV =
2490 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2491 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002492 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2493 }
2494 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002495 SDValue Result =
2496 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2497 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2498 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002499 if (RelocM == Reloc::Static)
2500 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002501 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002502 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002503}
2504
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002505// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002506SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002507ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002508 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002509 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002510 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002511 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002512 MachineFunction &MF = DAG.getMachineFunction();
2513 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002514 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002515 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002516 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2517 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002518 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002519 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002520 Argument =
2521 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2522 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2523 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002524 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002525
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002526 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002527 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002528
2529 // call __tls_get_addr.
2530 ArgListTy Args;
2531 ArgListEntry Entry;
2532 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002533 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002534 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002535
Dale Johannesen555a3752009-01-30 23:10:59 +00002536 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002537 TargetLowering::CallLoweringInfo CLI(DAG);
2538 CLI.setDebugLoc(dl).setChain(Chain)
2539 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002540 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2541 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002542
Justin Holewinskiaa583972012-05-25 16:35:28 +00002543 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002544 return CallResult.first;
2545}
2546
2547// Lower ISD::GlobalTLSAddress using the "initial exec" or
2548// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002549SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002550ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002551 SelectionDAG &DAG,
2552 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002553 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002554 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002555 SDValue Offset;
2556 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002557 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002558 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002559 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002560
Hans Wennborgaea41202012-05-04 09:40:39 +00002561 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002562 MachineFunction &MF = DAG.getMachineFunction();
2563 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002564 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002565 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002566 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2567 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002568 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2569 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2570 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002571 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002572 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002573 Offset = DAG.getLoad(
2574 PtrVT, dl, Chain, Offset,
2575 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2576 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002577 Chain = Offset.getValue(1);
2578
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002579 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002580 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002581
Alex Lorenze40c8a22015-08-11 23:09:45 +00002582 Offset = DAG.getLoad(
2583 PtrVT, dl, Chain, Offset,
2584 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2585 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002586 } else {
2587 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002588 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002589 ARMConstantPoolValue *CPV =
2590 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002591 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002592 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002593 Offset = DAG.getLoad(
2594 PtrVT, dl, Chain, Offset,
2595 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2596 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002597 }
2598
2599 // The address of the thread local variable is the add of the thread
2600 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002601 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002602}
2603
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002604SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002605ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002606 // TODO: implement the "local dynamic" model
2607 assert(Subtarget->isTargetELF() &&
2608 "TLS not implemented for non-ELF targets");
2609 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002610 if (DAG.getTarget().Options.EmulatedTLS)
2611 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002612
2613 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2614
2615 switch (model) {
2616 case TLSModel::GeneralDynamic:
2617 case TLSModel::LocalDynamic:
2618 return LowerToTLSGeneralDynamicModel(GA, DAG);
2619 case TLSModel::InitialExec:
2620 case TLSModel::LocalExec:
2621 return LowerToTLSExecModels(GA, DAG, model);
2622 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002623 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002624}
2625
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002626SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002627 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002628 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002629 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002630 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002631 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002632 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002633 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002634 ARMConstantPoolConstant::Create(GV,
2635 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002636 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002637 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002638 SDValue Result = DAG.getLoad(
2639 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2640 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2641 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002642 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002643 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002644 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002645 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002646 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002647 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002648 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002649 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002650 }
2651
2652 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002653 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002654 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002655 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002656 // FIXME: Once remat is capable of dealing with instructions with register
2657 // operands, expand this into two nodes.
2658 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2659 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002660 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002661 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2662 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002663 return DAG.getLoad(
2664 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2665 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2666 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002667 }
2668}
2669
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002670SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002671 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002672 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002673 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002674 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002675 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002676
Eric Christopherc1058df2014-07-04 01:55:26 +00002677 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002678 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002679
Tim Northover72360d22013-12-02 10:35:41 +00002680 // FIXME: Once remat is capable of dealing with instructions with register
2681 // operands, expand this into multiple nodes
2682 unsigned Wrapper =
2683 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002684
Tim Northover72360d22013-12-02 10:35:41 +00002685 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2686 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002687
Evan Cheng1b389522009-09-03 07:04:02 +00002688 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002689 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002690 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2691 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002692 return Result;
2693}
2694
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002695SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2696 SelectionDAG &DAG) const {
2697 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002698 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2699 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002700
2701 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002702 const ARMII::TOF TargetFlags =
2703 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002704 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002705 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002706 SDLoc DL(Op);
2707
2708 ++NumMovwMovt;
2709
2710 // FIXME: Once remat is capable of dealing with instructions with register
2711 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002712 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2713 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002714 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002715 if (GV->hasDLLImportStorageClass())
2716 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002717 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2718 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002719 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002720}
2721
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002722SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002723 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002724 assert(Subtarget->isTargetELF() &&
2725 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002726 MachineFunction &MF = DAG.getMachineFunction();
2727 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002728 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002729 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002730 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002731 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002732 ARMConstantPoolValue *CPV =
2733 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2734 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002735 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002736 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002737 SDValue Result =
2738 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2739 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2740 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002741 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002742 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002743}
2744
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002745SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002746ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002747 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002748 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002749 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2750 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002751 Op.getOperand(1), Val);
2752}
2753
2754SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002755ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002756 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002757 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002758 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002759}
2760
Matthias Braun3cd00c12015-07-16 22:34:16 +00002761SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2762 SelectionDAG &DAG) const {
2763 SDLoc dl(Op);
2764 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2765 Op.getOperand(0));
2766}
2767
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002768SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002769ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002770 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002771 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002772 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002773 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002774 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002775 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002776 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002777 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002778 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002779 }
Bob Wilson17f88782009-08-04 00:25:01 +00002780 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002781 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002782 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2783 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002784 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002785 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002786 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002787 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002788 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002789 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2790 SDValue CPAddr;
2791 unsigned PCAdj = (RelocM != Reloc::PIC_)
2792 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002793 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002794 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2795 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002796 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002797 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002798 SDValue Result = DAG.getLoad(
2799 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2800 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2801 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002802
2803 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002804 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002805 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2806 }
2807 return Result;
2808 }
Evan Cheng18381b42011-03-29 23:06:19 +00002809 case Intrinsic::arm_neon_vmulls:
2810 case Intrinsic::arm_neon_vmullu: {
2811 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2812 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002813 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002814 Op.getOperand(1), Op.getOperand(2));
2815 }
James Molloyee868b22015-08-11 12:06:25 +00002816 case Intrinsic::arm_neon_vminnm:
2817 case Intrinsic::arm_neon_vmaxnm: {
2818 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2819 ? ISD::FMINNUM : ISD::FMAXNUM;
2820 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2821 Op.getOperand(1), Op.getOperand(2));
2822 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002823 case Intrinsic::arm_neon_vminu:
2824 case Intrinsic::arm_neon_vmaxu: {
2825 if (Op.getValueType().isFloatingPoint())
2826 return SDValue();
2827 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2828 ? ISD::UMIN : ISD::UMAX;
2829 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2830 Op.getOperand(1), Op.getOperand(2));
2831 }
James Molloyd616c642015-08-11 12:06:28 +00002832 case Intrinsic::arm_neon_vmins:
2833 case Intrinsic::arm_neon_vmaxs: {
2834 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002835 if (!Op.getValueType().isFloatingPoint()) {
2836 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2837 ? ISD::SMIN : ISD::SMAX;
2838 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2839 Op.getOperand(1), Op.getOperand(2));
2840 }
James Molloyd616c642015-08-11 12:06:28 +00002841 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2842 ? ISD::FMINNAN : ISD::FMAXNAN;
2843 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2844 Op.getOperand(1), Op.getOperand(2));
2845 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002846 }
2847}
2848
Eli Friedman30a49e92011-08-03 21:06:02 +00002849static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2850 const ARMSubtarget *Subtarget) {
2851 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002852 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002853 if (!Subtarget->hasDataBarrier()) {
2854 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2855 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2856 // here.
2857 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002858 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002859 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002860 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002861 }
2862
Tim Northover36b24172013-07-03 09:20:36 +00002863 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2864 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002865 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002866 if (Subtarget->isMClass()) {
2867 // Only a full system barrier exists in the M-class architectures.
2868 Domain = ARM_MB::SY;
2869 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002870 // Swift happens to implement ISHST barriers in a way that's compatible with
2871 // Release semantics but weaker than ISH so we'd be fools not to use
2872 // it. Beware: other processors probably don't!
2873 Domain = ARM_MB::ISHST;
2874 }
2875
Joey Gouly926d3f52013-09-05 15:35:24 +00002876 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002877 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2878 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002879}
2880
Evan Cheng8740ee32010-11-03 06:34:55 +00002881static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2882 const ARMSubtarget *Subtarget) {
2883 // ARM pre v5TE and Thumb1 does not have preload instructions.
2884 if (!(Subtarget->isThumb2() ||
2885 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2886 // Just preserve the chain.
2887 return Op.getOperand(0);
2888
Andrew Trickef9de2a2013-05-25 02:42:55 +00002889 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002890 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2891 if (!isRead &&
2892 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2893 // ARMv7 with MP extension has PLDW.
2894 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002895
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002896 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2897 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002898 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002899 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002900 isData = ~isData & 1;
2901 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002902
2903 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002904 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2905 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002906}
2907
Dan Gohman31ae5862010-04-17 14:41:14 +00002908static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2909 MachineFunction &MF = DAG.getMachineFunction();
2910 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2911
Evan Cheng10043e22007-01-19 07:51:42 +00002912 // vastart just stores the address of the VarArgsFrameIndex slot into the
2913 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002914 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002915 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002916 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002917 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002918 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2919 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002920}
2921
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002922SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002923ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2924 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002925 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002926 MachineFunction &MF = DAG.getMachineFunction();
2927 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2928
Craig Topper760b1342012-02-22 05:59:10 +00002929 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002930 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002931 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002932 else
Craig Topperc7242e02012-04-20 07:30:17 +00002933 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002934
2935 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002936 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002937 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002938
2939 SDValue ArgValue2;
2940 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002941 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002942 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002943
2944 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002945 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00002946 ArgValue2 = DAG.getLoad(
2947 MVT::i32, dl, Root, FIN,
2948 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
2949 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002950 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002951 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002952 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002953 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002954 if (!Subtarget->isLittle())
2955 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002956 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002957}
2958
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002959// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002960// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002961// byval). Either way, we allocate stack slots adjacent to the data
2962// provided by our caller, and store the unallocated registers there.
2963// If this is a variadic function, the va_list pointer will begin with
2964// these values; otherwise, this reassembles a (byval) structure that
2965// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002966// Return: The frame index registers were stored into.
2967int
2968ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002969 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002970 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002971 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002972 int ArgOffset,
2973 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002974 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002975 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002976 // Setup first unallocated register as first byval register;
2977 // eat all remained registers
2978 // (these two actions are performed by HandleByVal method).
2979 // Then, here, we initialize stack frame with
2980 // "store-reg" instructions.
2981 // Case #2. Var-args function, that doesn't contain byval parameters.
2982 // The same: eat all remained unallocated registers,
2983 // initialize stack frame.
2984
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002985 MachineFunction &MF = DAG.getMachineFunction();
2986 MachineFrameInfo *MFI = MF.getFrameInfo();
2987 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002988 unsigned RBegin, REnd;
2989 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2990 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002991 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002992 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00002993 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00002994 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002995 }
2996
Tim Northover8cda34f2015-03-11 18:54:22 +00002997 if (REnd != RBegin)
2998 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002999
Mehdi Amini44ede332015-07-09 02:09:04 +00003000 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003001 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003002 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003003
Tim Northover8cda34f2015-03-11 18:54:22 +00003004 SmallVector<SDValue, 4> MemOps;
3005 const TargetRegisterClass *RC =
3006 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003007
Tim Northover8cda34f2015-03-11 18:54:22 +00003008 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3009 unsigned VReg = MF.addLiveIn(Reg, RC);
3010 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3011 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003012 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003013 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3014 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003015 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003016 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003017
3018 if (!MemOps.empty())
3019 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3020 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003021}
3022
3023// Setup stack frame, the va_list pointer will start from.
3024void
3025ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003026 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003027 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003028 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003029 bool ForceMutable) const {
3030 MachineFunction &MF = DAG.getMachineFunction();
3031 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3032
3033 // Try to store any remaining integer argument regs
3034 // to their spots on the stack so that they may be loaded by deferencing
3035 // the result of va_next.
3036 // If there is no regs to be stored, just point address after last
3037 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003038 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3039 CCInfo.getInRegsParamsCount(),
3040 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003041 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003042}
3043
Bob Wilson2e076c42009-06-22 23:27:02 +00003044SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003045ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003046 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003047 const SmallVectorImpl<ISD::InputArg>
3048 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003049 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003050 SmallVectorImpl<SDValue> &InVals)
3051 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003052 MachineFunction &MF = DAG.getMachineFunction();
3053 MachineFrameInfo *MFI = MF.getFrameInfo();
3054
Bob Wilsona4c22902009-04-17 19:07:39 +00003055 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3056
3057 // Assign locations to all of the incoming arguments.
3058 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003059 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3060 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003061 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003062 CCAssignFnForNode(CallConv, /* Return*/ false,
3063 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003064
Bob Wilsona4c22902009-04-17 19:07:39 +00003065 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003066 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003067 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3068 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003069
3070 // Initially ArgRegsSaveSize is zero.
3071 // Then we increase this value each time we meet byval parameter.
3072 // We also increase this value in case of varargs function.
3073 AFI->setArgRegsSaveSize(0);
3074
Oliver Stannardd55e1152014-03-05 15:25:27 +00003075 // Calculate the amount of stack space that we need to allocate to store
3076 // byval and variadic arguments that are passed in registers.
3077 // We need to know this before we allocate the first byval or variadic
3078 // argument, as they will be allocated a stack slot below the CFA (Canonical
3079 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003080 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003081 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003082 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3083 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003084
Tim Northover8cda34f2015-03-11 18:54:22 +00003085 CCValAssign &VA = ArgLocs[i];
3086 unsigned Index = VA.getValNo();
3087 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3088 if (!Flags.isByVal())
3089 continue;
3090
3091 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3092 unsigned RBegin, REnd;
3093 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3094 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3095
3096 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003097 }
3098 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003099
3100 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003101 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003102 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3103 if (RegIdx != array_lengthof(GPRArgRegs))
3104 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003105 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003106
3107 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3108 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003109 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003110
Bob Wilsona4c22902009-04-17 19:07:39 +00003111 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3112 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003113 if (Ins[VA.getValNo()].isOrigArg()) {
3114 std::advance(CurOrigArg,
3115 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3116 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3117 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003118 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003119 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003120 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003121
Bob Wilsona4c22902009-04-17 19:07:39 +00003122 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003123 // f64 and vector types are split up into multiple registers or
3124 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003125 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003126 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003127 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003128 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003129 SDValue ArgValue2;
3130 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003131 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003132 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003133 ArgValue2 = DAG.getLoad(
3134 MVT::f64, dl, Chain, FIN,
3135 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3136 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003137 } else {
3138 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3139 Chain, DAG, dl);
3140 }
Owen Anderson9f944592009-08-11 20:47:22 +00003141 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3142 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003143 ArgValue, ArgValue1,
3144 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003145 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003146 ArgValue, ArgValue2,
3147 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003148 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003149 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003150
Bob Wilson2e076c42009-06-22 23:27:02 +00003151 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003152 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003153
Owen Anderson9f944592009-08-11 20:47:22 +00003154 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003155 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003156 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003157 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003158 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003159 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003160 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003161 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3162 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003163 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003164 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003165
3166 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003167 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003168 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003169 }
3170
3171 // If this is an 8 or 16-bit value, it is really passed promoted
3172 // to 32 bits. Insert an assert[sz]ext to capture this, then
3173 // truncate to the right size.
3174 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003175 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003176 case CCValAssign::Full: break;
3177 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003178 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003179 break;
3180 case CCValAssign::SExt:
3181 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3182 DAG.getValueType(VA.getValVT()));
3183 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3184 break;
3185 case CCValAssign::ZExt:
3186 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3187 DAG.getValueType(VA.getValVT()));
3188 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3189 break;
3190 }
3191
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003192 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003193
3194 } else { // VA.isRegLoc()
3195
3196 // sanity check
3197 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003198 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003199
Andrew Trick05938a52015-02-16 18:10:47 +00003200 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003201
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003202 // Some Ins[] entries become multiple ArgLoc[] entries.
3203 // Process them only once.
3204 if (index != lastInsIndex)
3205 {
3206 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003207 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003208 // This can be changed with more analysis.
3209 // In case of tail call optimization mark all arguments mutable.
3210 // Since they could be overwritten by lowering of arguments in case of
3211 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003212 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003213 assert(Ins[index].isOrigArg() &&
3214 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003215 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003216
Tim Northover8cda34f2015-03-11 18:54:22 +00003217 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3218 CurByValIndex, VA.getLocMemOffset(),
3219 Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003220 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003221 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003222 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003223 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003224 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003225 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003226
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003227 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003228 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003229 InVals.push_back(DAG.getLoad(
3230 VA.getValVT(), dl, Chain, FIN,
3231 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3232 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003233 }
3234 lastInsIndex = index;
3235 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003236 }
3237 }
3238
3239 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003240 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003241 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003242 CCInfo.getNextStackOffset(),
3243 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003244
Oliver Stannardb14c6252014-04-02 16:10:33 +00003245 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3246
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003247 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003248}
3249
3250/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003251static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003252 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003253 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003254 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003255 // Maybe this has already been legalized into the constant pool?
3256 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003257 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003258 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003259 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003260 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003261 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003262 } else if (Op->getOpcode() == ISD::BITCAST &&
3263 Op->getValueType(0) == MVT::f64) {
3264 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3265 // created by LowerConstantFP().
3266 SDValue BitcastOp = Op->getOperand(0);
3267 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3268 SDValue MoveOp = BitcastOp->getOperand(0);
3269 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3270 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3271 return true;
3272 }
3273 }
Evan Cheng10043e22007-01-19 07:51:42 +00003274 }
3275 return false;
3276}
3277
Evan Cheng10043e22007-01-19 07:51:42 +00003278/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3279/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003280SDValue
3281ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003282 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003283 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003284 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003285 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003286 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003287 // Constant does not fit, try adjusting it by one?
3288 switch (CC) {
3289 default: break;
3290 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003291 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003292 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003293 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003294 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003295 }
3296 break;
3297 case ISD::SETULT:
3298 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003299 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003300 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003301 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003302 }
3303 break;
3304 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003305 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003306 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003307 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003308 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003309 }
3310 break;
3311 case ISD::SETULE:
3312 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003313 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003314 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003315 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003316 }
3317 break;
3318 }
3319 }
3320 }
3321
3322 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003323 ARMISD::NodeType CompareType;
3324 switch (CondCode) {
3325 default:
3326 CompareType = ARMISD::CMP;
3327 break;
3328 case ARMCC::EQ:
3329 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003330 // Uses only Z Flag
3331 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003332 break;
3333 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003334 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003335 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003336}
3337
3338/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003339SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003340ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003341 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003342 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003343 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003344 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003345 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003346 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003347 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3348 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003349}
3350
Bob Wilson45acbd02011-03-08 01:17:20 +00003351/// duplicateCmp - Glue values can have only one use, so this function
3352/// duplicates a comparison node.
3353SDValue
3354ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3355 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003356 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003357 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3358 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3359
3360 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3361 Cmp = Cmp.getOperand(0);
3362 Opc = Cmp.getOpcode();
3363 if (Opc == ARMISD::CMPFP)
3364 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3365 else {
3366 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3367 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3368 }
3369 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3370}
3371
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003372std::pair<SDValue, SDValue>
3373ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3374 SDValue &ARMcc) const {
3375 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3376
3377 SDValue Value, OverflowCmp;
3378 SDValue LHS = Op.getOperand(0);
3379 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003380 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003381
3382 // FIXME: We are currently always generating CMPs because we don't support
3383 // generating CMN through the backend. This is not as good as the natural
3384 // CMP case because it causes a register dependency and cannot be folded
3385 // later.
3386
3387 switch (Op.getOpcode()) {
3388 default:
3389 llvm_unreachable("Unknown overflow instruction!");
3390 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003391 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3392 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3393 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003394 break;
3395 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003396 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3397 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3398 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003399 break;
3400 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003401 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3402 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3403 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003404 break;
3405 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003406 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3407 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3408 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003409 break;
3410 } // switch (...)
3411
3412 return std::make_pair(Value, OverflowCmp);
3413}
3414
3415
3416SDValue
3417ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3418 // Let legalize expand this if it isn't a legal type yet.
3419 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3420 return SDValue();
3421
3422 SDValue Value, OverflowCmp;
3423 SDValue ARMcc;
3424 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3425 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003426 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003427 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003428 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3429 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003430 EVT VT = Op.getValueType();
3431
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003432 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003433 ARMcc, CCR, OverflowCmp);
3434
3435 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003436 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003437}
3438
3439
Bill Wendling6a981312010-08-11 08:43:16 +00003440SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3441 SDValue Cond = Op.getOperand(0);
3442 SDValue SelectTrue = Op.getOperand(1);
3443 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003444 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003445 unsigned Opc = Cond.getOpcode();
3446
3447 if (Cond.getResNo() == 1 &&
3448 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3449 Opc == ISD::USUBO)) {
3450 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3451 return SDValue();
3452
3453 SDValue Value, OverflowCmp;
3454 SDValue ARMcc;
3455 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3456 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3457 EVT VT = Op.getValueType();
3458
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003459 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003460 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003461 }
Bill Wendling6a981312010-08-11 08:43:16 +00003462
3463 // Convert:
3464 //
3465 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3466 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3467 //
3468 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3469 const ConstantSDNode *CMOVTrue =
3470 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3471 const ConstantSDNode *CMOVFalse =
3472 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3473
3474 if (CMOVTrue && CMOVFalse) {
3475 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3476 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3477
3478 SDValue True;
3479 SDValue False;
3480 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3481 True = SelectTrue;
3482 False = SelectFalse;
3483 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3484 True = SelectFalse;
3485 False = SelectTrue;
3486 }
3487
3488 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003489 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003490 SDValue ARMcc = Cond.getOperand(2);
3491 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003492 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003493 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003494 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003495 }
3496 }
3497 }
3498
Dan Gohmand4a77c42012-02-24 00:09:36 +00003499 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3500 // undefined bits before doing a full-word comparison with zero.
3501 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003502 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003503
Bill Wendling6a981312010-08-11 08:43:16 +00003504 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003505 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003506 SelectTrue, SelectFalse, ISD::SETNE);
3507}
3508
Joey Gouly881eab52013-08-22 15:29:11 +00003509static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3510 bool &swpCmpOps, bool &swpVselOps) {
3511 // Start by selecting the GE condition code for opcodes that return true for
3512 // 'equality'
3513 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3514 CC == ISD::SETULE)
3515 CondCode = ARMCC::GE;
3516
3517 // and GT for opcodes that return false for 'equality'.
3518 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3519 CC == ISD::SETULT)
3520 CondCode = ARMCC::GT;
3521
3522 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3523 // to swap the compare operands.
3524 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3525 CC == ISD::SETULT)
3526 swpCmpOps = true;
3527
3528 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3529 // If we have an unordered opcode, we need to swap the operands to the VSEL
3530 // instruction (effectively negating the condition).
3531 //
3532 // This also has the effect of swapping which one of 'less' or 'greater'
3533 // returns true, so we also swap the compare operands. It also switches
3534 // whether we return true for 'equality', so we compensate by picking the
3535 // opposite condition code to our original choice.
3536 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3537 CC == ISD::SETUGT) {
3538 swpCmpOps = !swpCmpOps;
3539 swpVselOps = !swpVselOps;
3540 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3541 }
3542
3543 // 'ordered' is 'anything but unordered', so use the VS condition code and
3544 // swap the VSEL operands.
3545 if (CC == ISD::SETO) {
3546 CondCode = ARMCC::VS;
3547 swpVselOps = true;
3548 }
3549
3550 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3551 // code and swap the VSEL operands.
3552 if (CC == ISD::SETUNE) {
3553 CondCode = ARMCC::EQ;
3554 swpVselOps = true;
3555 }
3556}
3557
Oliver Stannard51b1d462014-08-21 12:50:31 +00003558SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3559 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3560 SDValue Cmp, SelectionDAG &DAG) const {
3561 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3562 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3563 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3564 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3565 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3566
3567 SDValue TrueLow = TrueVal.getValue(0);
3568 SDValue TrueHigh = TrueVal.getValue(1);
3569 SDValue FalseLow = FalseVal.getValue(0);
3570 SDValue FalseHigh = FalseVal.getValue(1);
3571
3572 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3573 ARMcc, CCR, Cmp);
3574 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3575 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3576
3577 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3578 } else {
3579 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3580 Cmp);
3581 }
3582}
3583
Dan Gohman21cea8a2010-04-17 15:26:15 +00003584SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003585 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003586 SDValue LHS = Op.getOperand(0);
3587 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003588 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003589 SDValue TrueVal = Op.getOperand(2);
3590 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003591 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003592
Oliver Stannard51b1d462014-08-21 12:50:31 +00003593 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3594 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3595 dl);
3596
3597 // If softenSetCCOperands only returned one value, we should compare it to
3598 // zero.
3599 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003600 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003601 CC = ISD::SETNE;
3602 }
3603 }
3604
Owen Anderson9f944592009-08-11 20:47:22 +00003605 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003606 // Try to generate VSEL on ARMv8.
3607 // The VSEL instruction can't use all the usual ARM condition
3608 // codes: it only has two bits to select the condition code, so it's
3609 // constrained to use only GE, GT, VS and EQ.
3610 //
3611 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3612 // swap the operands of the previous compare instruction (effectively
3613 // inverting the compare condition, swapping 'less' and 'greater') and
3614 // sometimes need to swap the operands to the VSEL (which inverts the
3615 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003616 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3617 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003618 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3619 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3620 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003621 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003622 std::swap(TrueVal, FalseVal);
3623 }
3624 }
3625
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003626 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003627 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003628 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003629 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003630 }
3631
3632 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003633 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003634
Scott Douglass7ad77922015-04-08 17:18:28 +00003635 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003636 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3637 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003638 bool swpCmpOps = false;
3639 bool swpVselOps = false;
3640 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3641
3642 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3643 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3644 if (swpCmpOps)
3645 std::swap(LHS, RHS);
3646 if (swpVselOps)
3647 std::swap(TrueVal, FalseVal);
3648 }
3649 }
3650
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003651 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003652 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003653 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003654 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003655 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003656 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003657 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003658 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003659 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003660 }
3661 return Result;
3662}
3663
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003664/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3665/// to morph to an integer compare sequence.
3666static bool canChangeToInt(SDValue Op, bool &SeenZero,
3667 const ARMSubtarget *Subtarget) {
3668 SDNode *N = Op.getNode();
3669 if (!N->hasOneUse())
3670 // Otherwise it requires moving the value from fp to integer registers.
3671 return false;
3672 if (!N->getNumValues())
3673 return false;
3674 EVT VT = Op.getValueType();
3675 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3676 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3677 // vmrs are very slow, e.g. cortex-a8.
3678 return false;
3679
3680 if (isFloatingPointZero(Op)) {
3681 SeenZero = true;
3682 return true;
3683 }
3684 return ISD::isNormalLoad(N);
3685}
3686
3687static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3688 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003689 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003690
3691 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003692 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003693 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003694 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003695 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003696
3697 llvm_unreachable("Unknown VFP cmp argument!");
3698}
3699
3700static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3701 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003702 SDLoc dl(Op);
3703
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003704 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003705 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3706 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003707 return;
3708 }
3709
3710 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3711 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003712 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003713 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003714 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003715 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003716 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003717
3718 EVT PtrType = Ptr.getValueType();
3719 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003720 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3721 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3722 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003723 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003724 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003725 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003726 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003727 return;
3728 }
3729
3730 llvm_unreachable("Unknown VFP cmp argument!");
3731}
3732
3733/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3734/// f32 and even f64 comparisons to integer ones.
3735SDValue
3736ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3737 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003738 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003739 SDValue LHS = Op.getOperand(2);
3740 SDValue RHS = Op.getOperand(3);
3741 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003742 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003743
Evan Chengd12af5d2012-03-01 23:27:13 +00003744 bool LHSSeenZero = false;
3745 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3746 bool RHSSeenZero = false;
3747 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3748 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003749 // If unsafe fp math optimization is enabled and there are no other uses of
3750 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003751 // to an integer comparison.
3752 if (CC == ISD::SETOEQ)
3753 CC = ISD::SETEQ;
3754 else if (CC == ISD::SETUNE)
3755 CC = ISD::SETNE;
3756
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003757 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003758 SDValue ARMcc;
3759 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003760 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3761 bitcastf32Toi32(LHS, DAG), Mask);
3762 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3763 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003764 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3765 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3766 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3767 Chain, Dest, ARMcc, CCR, Cmp);
3768 }
3769
3770 SDValue LHS1, LHS2;
3771 SDValue RHS1, RHS2;
3772 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3773 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003774 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3775 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003776 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003777 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003778 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003779 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003780 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003781 }
3782
3783 return SDValue();
3784}
3785
3786SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3787 SDValue Chain = Op.getOperand(0);
3788 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3789 SDValue LHS = Op.getOperand(2);
3790 SDValue RHS = Op.getOperand(3);
3791 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003792 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003793
Oliver Stannard51b1d462014-08-21 12:50:31 +00003794 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3795 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3796 dl);
3797
3798 // If softenSetCCOperands only returned one value, we should compare it to
3799 // zero.
3800 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003801 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003802 CC = ISD::SETNE;
3803 }
3804 }
3805
Owen Anderson9f944592009-08-11 20:47:22 +00003806 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003807 SDValue ARMcc;
3808 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003809 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003810 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003811 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003812 }
3813
Owen Anderson9f944592009-08-11 20:47:22 +00003814 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003815
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003816 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003817 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3818 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3819 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3820 if (Result.getNode())
3821 return Result;
3822 }
3823
Evan Cheng10043e22007-01-19 07:51:42 +00003824 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003825 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003826
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003827 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003828 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003829 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003830 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003831 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003832 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003833 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003834 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003835 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003836 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003837 }
3838 return Res;
3839}
3840
Dan Gohman21cea8a2010-04-17 15:26:15 +00003841SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003842 SDValue Chain = Op.getOperand(0);
3843 SDValue Table = Op.getOperand(1);
3844 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003845 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003846
Mehdi Amini44ede332015-07-09 02:09:04 +00003847 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003848 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003849 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003850 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003851 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003852 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003853 if (Subtarget->isThumb2()) {
3854 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3855 // which does another jump to the destination. This also makes it easier
3856 // to translate it to TBB / TBH later.
3857 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003858 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003859 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003860 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003861 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003862 Addr =
3863 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
3864 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3865 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003866 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003867 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003868 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003869 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00003870 Addr =
3871 DAG.getLoad(PTy, dl, Chain, Addr,
3872 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
3873 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003874 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003875 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003876 }
Evan Cheng10043e22007-01-19 07:51:42 +00003877}
3878
Eli Friedman2d4055b2011-11-09 23:36:02 +00003879static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003880 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003881 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003882
James Molloy547d4c02012-02-20 09:24:05 +00003883 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3884 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3885 return Op;
3886 return DAG.UnrollVectorOp(Op.getNode());
3887 }
3888
3889 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3890 "Invalid type for custom lowering!");
3891 if (VT != MVT::v4i16)
3892 return DAG.UnrollVectorOp(Op.getNode());
3893
3894 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3895 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003896}
3897
Oliver Stannard51b1d462014-08-21 12:50:31 +00003898SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003899 EVT VT = Op.getValueType();
3900 if (VT.isVector())
3901 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003902 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3903 RTLIB::Libcall LC;
3904 if (Op.getOpcode() == ISD::FP_TO_SINT)
3905 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3906 Op.getValueType());
3907 else
3908 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3909 Op.getValueType());
3910 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3911 /*isSigned*/ false, SDLoc(Op)).first;
3912 }
3913
James Molloyfa041152015-03-23 16:15:16 +00003914 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003915}
3916
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003917static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3918 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003919 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003920
Eli Friedman2d4055b2011-11-09 23:36:02 +00003921 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3922 if (VT.getVectorElementType() == MVT::f32)
3923 return Op;
3924 return DAG.UnrollVectorOp(Op.getNode());
3925 }
3926
Duncan Sandsa41634e2011-08-12 14:54:45 +00003927 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3928 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003929 if (VT != MVT::v4f32)
3930 return DAG.UnrollVectorOp(Op.getNode());
3931
3932 unsigned CastOpc;
3933 unsigned Opc;
3934 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003935 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003936 case ISD::SINT_TO_FP:
3937 CastOpc = ISD::SIGN_EXTEND;
3938 Opc = ISD::SINT_TO_FP;
3939 break;
3940 case ISD::UINT_TO_FP:
3941 CastOpc = ISD::ZERO_EXTEND;
3942 Opc = ISD::UINT_TO_FP;
3943 break;
3944 }
3945
3946 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3947 return DAG.getNode(Opc, dl, VT, Op);
3948}
3949
Oliver Stannard51b1d462014-08-21 12:50:31 +00003950SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003951 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003952 if (VT.isVector())
3953 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003954 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3955 RTLIB::Libcall LC;
3956 if (Op.getOpcode() == ISD::SINT_TO_FP)
3957 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3958 Op.getValueType());
3959 else
3960 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3961 Op.getValueType());
3962 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3963 /*isSigned*/ false, SDLoc(Op)).first;
3964 }
3965
James Molloyfa041152015-03-23 16:15:16 +00003966 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003967}
3968
Evan Cheng25f93642010-07-08 02:08:50 +00003969SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003970 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003971 SDValue Tmp0 = Op.getOperand(0);
3972 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003973 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003974 EVT VT = Op.getValueType();
3975 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003976 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3977 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3978 bool UseNEON = !InGPR && Subtarget->hasNEON();
3979
3980 if (UseNEON) {
3981 // Use VBSL to copy the sign bit.
3982 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3983 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003984 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003985 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3986 if (VT == MVT::f64)
3987 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3988 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003989 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003990 else /*if (VT == MVT::f32)*/
3991 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3992 if (SrcVT == MVT::f32) {
3993 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3994 if (VT == MVT::f64)
3995 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3996 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003997 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003998 } else if (VT == MVT::f32)
3999 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4000 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004001 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004002 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4003 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4004
4005 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004006 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004007 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4008 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4009 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004010
Evan Chengd6b641e2011-02-23 02:24:55 +00004011 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4012 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4013 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004014 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004015 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4016 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004017 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004018 } else {
4019 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4020 }
4021
4022 return Res;
4023 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004024
4025 // Bitcast operand 1 to i32.
4026 if (SrcVT == MVT::f64)
4027 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004028 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004029 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4030
Evan Chengd6b641e2011-02-23 02:24:55 +00004031 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004032 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4033 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004034 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4035 if (VT == MVT::f32) {
4036 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4037 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4038 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4039 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004040 }
4041
Evan Chengd6b641e2011-02-23 02:24:55 +00004042 // f64: Or the high part with signbit and then combine two parts.
4043 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004044 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004045 SDValue Lo = Tmp0.getValue(0);
4046 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4047 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4048 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004049}
4050
Evan Cheng168ced92010-05-22 01:47:14 +00004051SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4052 MachineFunction &MF = DAG.getMachineFunction();
4053 MachineFrameInfo *MFI = MF.getFrameInfo();
4054 MFI->setReturnAddressIsTaken(true);
4055
Bill Wendling908bf812014-01-06 00:43:20 +00004056 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004057 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004058
Evan Cheng168ced92010-05-22 01:47:14 +00004059 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004060 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004061 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4062 if (Depth) {
4063 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004064 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004065 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4066 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004067 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004068 }
4069
4070 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004071 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004072 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4073}
4074
Dan Gohman21cea8a2010-04-17 15:26:15 +00004075SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004076 const ARMBaseRegisterInfo &ARI =
4077 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4078 MachineFunction &MF = DAG.getMachineFunction();
4079 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004080 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004081
Owen Anderson53aa7a92009-08-10 22:56:29 +00004082 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004083 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004084 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004085 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004086 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4087 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004088 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4089 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004090 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004091 return FrameAddr;
4092}
4093
Renato Golinc7aea402014-05-06 16:51:25 +00004094// FIXME? Maybe this could be a TableGen attribute on some registers and
4095// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004096unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4097 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004098 unsigned Reg = StringSwitch<unsigned>(RegName)
4099 .Case("sp", ARM::SP)
4100 .Default(0);
4101 if (Reg)
4102 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004103 report_fatal_error(Twine("Invalid register name \""
4104 + StringRef(RegName) + "\"."));
4105}
4106
4107// Result is 64 bit value so split into two 32 bit values and return as a
4108// pair of values.
4109static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4110 SelectionDAG &DAG) {
4111 SDLoc DL(N);
4112
4113 // This function is only supposed to be called for i64 type destination.
4114 assert(N->getValueType(0) == MVT::i64
4115 && "ExpandREAD_REGISTER called for non-i64 type result.");
4116
4117 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4118 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4119 N->getOperand(0),
4120 N->getOperand(1));
4121
4122 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4123 Read.getValue(1)));
4124 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004125}
4126
Wesley Peck527da1b2010-11-23 03:31:01 +00004127/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004128/// expand a bit convert where either the source or destination type is i64 to
4129/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4130/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4131/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004132static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004133 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004134 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004135 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004136
Bob Wilson59b70ea2010-04-17 05:30:19 +00004137 // This function is only supposed to be called for i64 types, either as the
4138 // source or destination of the bit convert.
4139 EVT SrcVT = Op.getValueType();
4140 EVT DstVT = N->getValueType(0);
4141 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004142 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004143
Bob Wilson59b70ea2010-04-17 05:30:19 +00004144 // Turn i64->f64 into VMOVDRR.
4145 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004146 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004147 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004148 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004149 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004150 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004151 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004152 }
Bob Wilson7117a912009-03-20 22:42:55 +00004153
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004154 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004155 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004156 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004157 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004158 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004159 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4160 DAG.getVTList(MVT::i32, MVT::i32),
4161 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4162 else
4163 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4164 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004165 // Merge the pieces into a single i64 value.
4166 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4167 }
Bob Wilson7117a912009-03-20 22:42:55 +00004168
Bob Wilson59b70ea2010-04-17 05:30:19 +00004169 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004170}
4171
Bob Wilson2e076c42009-06-22 23:27:02 +00004172/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004173/// Zero vectors are used to represent vector negation and in those cases
4174/// will be implemented with the NEON VNEG instruction. However, VNEG does
4175/// not support i64 elements, so sometimes the zero vectors will need to be
4176/// explicitly constructed. Regardless, use a canonical VMOV to create the
4177/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004178static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004179 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004180 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004181 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004182 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4183 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004184 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004185}
4186
Jim Grosbach624fcb22009-10-31 21:00:56 +00004187/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4188/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004189SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4190 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004191 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4192 EVT VT = Op.getValueType();
4193 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004194 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004195 SDValue ShOpLo = Op.getOperand(0);
4196 SDValue ShOpHi = Op.getOperand(1);
4197 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004198 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004199 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004200
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004201 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4202
Jim Grosbach624fcb22009-10-31 21:00:56 +00004203 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004204 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004205 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4206 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004207 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004208 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4209 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004210 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004211
4212 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004213 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4214 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004215 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004216 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004217 CCR, Cmp);
4218
4219 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004220 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004221}
4222
Jim Grosbach5d994042009-10-31 19:38:01 +00004223/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4224/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004225SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4226 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004227 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4228 EVT VT = Op.getValueType();
4229 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004230 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004231 SDValue ShOpLo = Op.getOperand(0);
4232 SDValue ShOpHi = Op.getOperand(1);
4233 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004234 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004235
4236 assert(Op.getOpcode() == ISD::SHL_PARTS);
4237 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004238 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004239 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4240 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004241 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004242 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4243 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4244
4245 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4246 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004247 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4248 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004249 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004250 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004251 CCR, Cmp);
4252
4253 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004254 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004255}
4256
Jim Grosbach535d3b42010-09-08 03:54:02 +00004257SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004258 SelectionDAG &DAG) const {
4259 // The rounding mode is in bits 23:22 of the FPSCR.
4260 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4261 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4262 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004263 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004264 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004265 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004266 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004267 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004268 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004269 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004270 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004271 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004272 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004273}
4274
Jim Grosbach8546ec92010-01-18 19:58:49 +00004275static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4276 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004277 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004278 EVT VT = N->getValueType(0);
4279 if (VT.isVector()) {
4280 assert(ST->hasNEON());
4281
4282 // Compute the least significant set bit: LSB = X & -X
4283 SDValue X = N->getOperand(0);
4284 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4285 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4286
4287 EVT ElemTy = VT.getVectorElementType();
4288
4289 if (ElemTy == MVT::i8) {
4290 // Compute with: cttz(x) = ctpop(lsb - 1)
4291 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4292 DAG.getTargetConstant(1, dl, ElemTy));
4293 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4294 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4295 }
4296
4297 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4298 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4299 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4300 unsigned NumBits = ElemTy.getSizeInBits();
4301 SDValue WidthMinus1 =
4302 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4303 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4304 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4305 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4306 }
4307
4308 // Compute with: cttz(x) = ctpop(lsb - 1)
4309
4310 // Since we can only compute the number of bits in a byte with vcnt.8, we
4311 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4312 // and i64.
4313
4314 // Compute LSB - 1.
4315 SDValue Bits;
4316 if (ElemTy == MVT::i64) {
4317 // Load constant 0xffff'ffff'ffff'ffff to register.
4318 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4319 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4320 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4321 } else {
4322 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4323 DAG.getTargetConstant(1, dl, ElemTy));
4324 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4325 }
4326
4327 // Count #bits with vcnt.8.
4328 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4329 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4330 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4331
4332 // Gather the #bits with vpaddl (pairwise add.)
4333 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4334 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4335 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4336 Cnt8);
4337 if (ElemTy == MVT::i16)
4338 return Cnt16;
4339
4340 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4341 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4342 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4343 Cnt16);
4344 if (ElemTy == MVT::i32)
4345 return Cnt32;
4346
4347 assert(ElemTy == MVT::i64);
4348 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4349 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4350 Cnt32);
4351 return Cnt64;
4352 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004353
4354 if (!ST->hasV6T2Ops())
4355 return SDValue();
4356
4357 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4358 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4359}
4360
Evan Chengb4eae132012-12-04 22:41:50 +00004361/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4362/// for each 16-bit element from operand, repeated. The basic idea is to
4363/// leverage vcnt to get the 8-bit counts, gather and add the results.
4364///
4365/// Trace for v4i16:
4366/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4367/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4368/// 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 +00004369/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004370/// [b0 b1 b2 b3 b4 b5 b6 b7]
4371/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4372/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4373/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4374static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4375 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004376 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004377
4378 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4379 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4380 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4381 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4382 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4383 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4384}
4385
4386/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4387/// bit-count for each 16-bit element from the operand. We need slightly
4388/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4389/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004390///
Evan Chengb4eae132012-12-04 22:41:50 +00004391/// Trace for v4i16:
4392/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4393/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4394/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4395/// v4i16:Extracted = [k0 k1 k2 k3 ]
4396static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4397 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004398 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004399
4400 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4401 if (VT.is64BitVector()) {
4402 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4403 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004404 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004405 } else {
4406 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004407 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004408 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4409 }
4410}
4411
4412/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4413/// bit-count for each 32-bit element from the operand. The idea here is
4414/// to split the vector into 16-bit elements, leverage the 16-bit count
4415/// routine, and then combine the results.
4416///
4417/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4418/// input = [v0 v1 ] (vi: 32-bit elements)
4419/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4420/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004421/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004422/// [k0 k1 k2 k3 ]
4423/// N1 =+[k1 k0 k3 k2 ]
4424/// [k0 k2 k1 k3 ]
4425/// N2 =+[k1 k3 k0 k2 ]
4426/// [k0 k2 k1 k3 ]
4427/// Extended =+[k1 k3 k0 k2 ]
4428/// [k0 k2 ]
4429/// Extracted=+[k1 k3 ]
4430///
4431static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4432 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004433 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004434
4435 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4436
4437 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4438 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4439 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4440 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4441 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4442
4443 if (VT.is64BitVector()) {
4444 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4445 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, 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::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004449 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004450 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4451 }
4452}
4453
4454static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4455 const ARMSubtarget *ST) {
4456 EVT VT = N->getValueType(0);
4457
4458 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004459 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4460 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004461 "Unexpected type for custom ctpop lowering");
4462
4463 if (VT.getVectorElementType() == MVT::i32)
4464 return lowerCTPOP32BitElements(N, DAG);
4465 else
4466 return lowerCTPOP16BitElements(N, DAG);
4467}
4468
Bob Wilson2e076c42009-06-22 23:27:02 +00004469static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4470 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004471 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004472 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004473
Bob Wilson7d471332010-11-18 21:16:28 +00004474 if (!VT.isVector())
4475 return SDValue();
4476
Bob Wilson2e076c42009-06-22 23:27:02 +00004477 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004478 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004479
Bob Wilson7d471332010-11-18 21:16:28 +00004480 // Left shifts translate directly to the vshiftu intrinsic.
4481 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004482 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004483 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4484 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004485 N->getOperand(0), N->getOperand(1));
4486
4487 assert((N->getOpcode() == ISD::SRA ||
4488 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4489
4490 // NEON uses the same intrinsics for both left and right shifts. For
4491 // right shifts, the shift amounts are negative, so negate the vector of
4492 // shift amounts.
4493 EVT ShiftVT = N->getOperand(1).getValueType();
4494 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4495 getZeroVector(ShiftVT, DAG, dl),
4496 N->getOperand(1));
4497 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4498 Intrinsic::arm_neon_vshifts :
4499 Intrinsic::arm_neon_vshiftu);
4500 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004501 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004502 N->getOperand(0), NegatedCount);
4503}
4504
4505static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4506 const ARMSubtarget *ST) {
4507 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004508 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004509
Eli Friedman682d8c12009-08-22 03:13:10 +00004510 // We can get here for a node like i32 = ISD::SHL i32, i64
4511 if (VT != MVT::i64)
4512 return SDValue();
4513
4514 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004515 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004516
Chris Lattnerf81d5882007-11-24 07:07:01 +00004517 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4518 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004519 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004520 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004521
Chris Lattnerf81d5882007-11-24 07:07:01 +00004522 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004523 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004524
Chris Lattnerf81d5882007-11-24 07:07:01 +00004525 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004526 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004527 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004528 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004529 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004530
Chris Lattnerf81d5882007-11-24 07:07:01 +00004531 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4532 // captures the result into a carry flag.
4533 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004534 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004535
Chris Lattnerf81d5882007-11-24 07:07:01 +00004536 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004537 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004538
Chris Lattnerf81d5882007-11-24 07:07:01 +00004539 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004540 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004541}
4542
Bob Wilson2e076c42009-06-22 23:27:02 +00004543static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4544 SDValue TmpOp0, TmpOp1;
4545 bool Invert = false;
4546 bool Swap = false;
4547 unsigned Opc = 0;
4548
4549 SDValue Op0 = Op.getOperand(0);
4550 SDValue Op1 = Op.getOperand(1);
4551 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004552 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004553 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004554 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004555 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004556
James Molloybf170092015-08-20 16:33:44 +00004557 if (CmpVT.getVectorElementType() == MVT::i64)
4558 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4559 // but it's possible that our operands are 64-bit but our result is 32-bit.
4560 // Bail in this case.
4561 return SDValue();
4562
Oliver Stannard51b1d462014-08-21 12:50:31 +00004563 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004564 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004565 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004566 case ISD::SETUNE:
4567 case ISD::SETNE: Invert = true; // Fallthrough
4568 case ISD::SETOEQ:
4569 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4570 case ISD::SETOLT:
4571 case ISD::SETLT: Swap = true; // Fallthrough
4572 case ISD::SETOGT:
4573 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4574 case ISD::SETOLE:
4575 case ISD::SETLE: Swap = true; // Fallthrough
4576 case ISD::SETOGE:
4577 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4578 case ISD::SETUGE: Swap = true; // Fallthrough
4579 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4580 case ISD::SETUGT: Swap = true; // Fallthrough
4581 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4582 case ISD::SETUEQ: Invert = true; // Fallthrough
4583 case ISD::SETONE:
4584 // Expand this to (OLT | OGT).
4585 TmpOp0 = Op0;
4586 TmpOp1 = Op1;
4587 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004588 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4589 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004590 break;
4591 case ISD::SETUO: Invert = true; // Fallthrough
4592 case ISD::SETO:
4593 // Expand this to (OLT | OGE).
4594 TmpOp0 = Op0;
4595 TmpOp1 = Op1;
4596 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004597 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4598 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004599 break;
4600 }
4601 } else {
4602 // Integer comparisons.
4603 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004604 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004605 case ISD::SETNE: Invert = true;
4606 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4607 case ISD::SETLT: Swap = true;
4608 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4609 case ISD::SETLE: Swap = true;
4610 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4611 case ISD::SETULT: Swap = true;
4612 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4613 case ISD::SETULE: Swap = true;
4614 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4615 }
4616
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004617 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004618 if (Opc == ARMISD::VCEQ) {
4619
4620 SDValue AndOp;
4621 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4622 AndOp = Op0;
4623 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4624 AndOp = Op1;
4625
4626 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004627 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004628 AndOp = AndOp.getOperand(0);
4629
4630 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4631 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004632 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4633 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004634 Invert = !Invert;
4635 }
4636 }
4637 }
4638
4639 if (Swap)
4640 std::swap(Op0, Op1);
4641
Owen Andersonc7baee32010-11-08 23:21:22 +00004642 // If one of the operands is a constant vector zero, attempt to fold the
4643 // comparison to a specialized compare-against-zero form.
4644 SDValue SingleOp;
4645 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4646 SingleOp = Op0;
4647 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4648 if (Opc == ARMISD::VCGE)
4649 Opc = ARMISD::VCLEZ;
4650 else if (Opc == ARMISD::VCGT)
4651 Opc = ARMISD::VCLTZ;
4652 SingleOp = Op1;
4653 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004654
Owen Andersonc7baee32010-11-08 23:21:22 +00004655 SDValue Result;
4656 if (SingleOp.getNode()) {
4657 switch (Opc) {
4658 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004659 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004660 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004661 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004662 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004663 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004664 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004665 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004666 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004667 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004668 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004669 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004670 }
4671 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004672 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004673 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004674
Tim Northover45aa89c2015-02-08 00:50:47 +00004675 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4676
Bob Wilson2e076c42009-06-22 23:27:02 +00004677 if (Invert)
4678 Result = DAG.getNOT(dl, Result, VT);
4679
4680 return Result;
4681}
4682
Bob Wilson5b2b5042010-06-14 22:19:57 +00004683/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4684/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004685/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004686static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4687 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004688 SDLoc dl, EVT &VT, bool is128Bits,
4689 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004690 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004691
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004692 // SplatBitSize is set to the smallest size that splats the vector, so a
4693 // zero vector will always have SplatBitSize == 8. However, NEON modified
4694 // immediate instructions others than VMOV do not support the 8-bit encoding
4695 // of a zero vector, and the default encoding of zero is supposed to be the
4696 // 32-bit version.
4697 if (SplatBits == 0)
4698 SplatBitSize = 32;
4699
Bob Wilson2e076c42009-06-22 23:27:02 +00004700 switch (SplatBitSize) {
4701 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004702 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004703 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004704 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004705 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004706 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004707 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004708 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004709 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004710
4711 case 16:
4712 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004713 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004714 if ((SplatBits & ~0xff) == 0) {
4715 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004716 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004717 Imm = SplatBits;
4718 break;
4719 }
4720 if ((SplatBits & ~0xff00) == 0) {
4721 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004722 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004723 Imm = SplatBits >> 8;
4724 break;
4725 }
4726 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004727
4728 case 32:
4729 // NEON's 32-bit VMOV supports splat values where:
4730 // * only one byte is nonzero, or
4731 // * the least significant byte is 0xff and the second byte is nonzero, or
4732 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004733 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004734 if ((SplatBits & ~0xff) == 0) {
4735 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004736 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004737 Imm = SplatBits;
4738 break;
4739 }
4740 if ((SplatBits & ~0xff00) == 0) {
4741 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004742 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004743 Imm = SplatBits >> 8;
4744 break;
4745 }
4746 if ((SplatBits & ~0xff0000) == 0) {
4747 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004748 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004749 Imm = SplatBits >> 16;
4750 break;
4751 }
4752 if ((SplatBits & ~0xff000000) == 0) {
4753 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004754 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004755 Imm = SplatBits >> 24;
4756 break;
4757 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004758
Owen Andersona4076922010-11-05 21:57:54 +00004759 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4760 if (type == OtherModImm) return SDValue();
4761
Bob Wilson2e076c42009-06-22 23:27:02 +00004762 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004763 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4764 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004765 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004766 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004767 break;
4768 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004769
4770 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004771 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4772 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004773 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004774 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004775 break;
4776 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004777
4778 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4779 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4780 // VMOV.I32. A (very) minor optimization would be to replicate the value
4781 // and fall through here to test for a valid 64-bit splat. But, then the
4782 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004783 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004784
4785 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004786 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004787 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004788 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004789 uint64_t BitMask = 0xff;
4790 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004791 unsigned ImmMask = 1;
4792 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004793 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004794 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004795 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004796 Imm |= ImmMask;
4797 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004798 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004799 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004800 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004801 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004802 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004803
Mehdi Aminiffc14022015-07-08 01:00:38 +00004804 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004805 // swap higher and lower 32 bit word
4806 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4807
Bob Wilson6eae5202010-06-11 21:34:50 +00004808 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004809 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004810 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004811 break;
4812 }
4813
Bob Wilson6eae5202010-06-11 21:34:50 +00004814 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004815 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004816 }
4817
Bob Wilsona3f19012010-07-13 21:16:48 +00004818 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004819 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004820}
4821
Lang Hames591cdaf2012-03-29 21:56:11 +00004822SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4823 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004824 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004825 return SDValue();
4826
Tim Northoverf79c3a52013-08-20 08:57:11 +00004827 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004828 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004829
Oliver Stannard51b1d462014-08-21 12:50:31 +00004830 // Use the default (constant pool) lowering for double constants when we have
4831 // an SP-only FPU
4832 if (IsDouble && Subtarget->isFPOnlySP())
4833 return SDValue();
4834
Lang Hames591cdaf2012-03-29 21:56:11 +00004835 // Try splatting with a VMOV.f32...
4836 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004837 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4838
Lang Hames591cdaf2012-03-29 21:56:11 +00004839 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004840 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4841 // We have code in place to select a valid ConstantFP already, no need to
4842 // do any mangling.
4843 return Op;
4844 }
4845
4846 // It's a float and we are trying to use NEON operations where
4847 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004848 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004849 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004850 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4851 NewVal);
4852 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004853 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004854 }
4855
Tim Northoverf79c3a52013-08-20 08:57:11 +00004856 // The rest of our options are NEON only, make sure that's allowed before
4857 // proceeding..
4858 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4859 return SDValue();
4860
Lang Hames591cdaf2012-03-29 21:56:11 +00004861 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004862 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4863
4864 // It wouldn't really be worth bothering for doubles except for one very
4865 // important value, which does happen to match: 0.0. So make sure we don't do
4866 // anything stupid.
4867 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4868 return SDValue();
4869
4870 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004871 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4872 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004873 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004874 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004875 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4876 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004877 if (IsDouble)
4878 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4879
4880 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004881 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4882 VecConstant);
4883 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004884 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004885 }
4886
4887 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004888 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004889 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004890 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004891 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004892 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004893
4894 if (IsDouble)
4895 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4896
4897 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004898 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4899 VecConstant);
4900 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004901 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004902 }
4903
4904 return SDValue();
4905}
4906
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004907// check if an VEXT instruction can handle the shuffle mask when the
4908// vector sources of the shuffle are the same.
4909static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4910 unsigned NumElts = VT.getVectorNumElements();
4911
4912 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4913 if (M[0] < 0)
4914 return false;
4915
4916 Imm = M[0];
4917
4918 // If this is a VEXT shuffle, the immediate value is the index of the first
4919 // element. The other shuffle indices must be the successive elements after
4920 // the first one.
4921 unsigned ExpectedElt = Imm;
4922 for (unsigned i = 1; i < NumElts; ++i) {
4923 // Increment the expected index. If it wraps around, just follow it
4924 // back to index zero and keep going.
4925 ++ExpectedElt;
4926 if (ExpectedElt == NumElts)
4927 ExpectedElt = 0;
4928
4929 if (M[i] < 0) continue; // ignore UNDEF indices
4930 if (ExpectedElt != static_cast<unsigned>(M[i]))
4931 return false;
4932 }
4933
4934 return true;
4935}
4936
Lang Hames591cdaf2012-03-29 21:56:11 +00004937
Benjamin Kramer339ced42012-01-15 13:16:05 +00004938static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004939 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004940 unsigned NumElts = VT.getVectorNumElements();
4941 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004942
4943 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4944 if (M[0] < 0)
4945 return false;
4946
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004947 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004948
4949 // If this is a VEXT shuffle, the immediate value is the index of the first
4950 // element. The other shuffle indices must be the successive elements after
4951 // the first one.
4952 unsigned ExpectedElt = Imm;
4953 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004954 // Increment the expected index. If it wraps around, it may still be
4955 // a VEXT but the source vectors must be swapped.
4956 ExpectedElt += 1;
4957 if (ExpectedElt == NumElts * 2) {
4958 ExpectedElt = 0;
4959 ReverseVEXT = true;
4960 }
4961
Bob Wilson411dfad2010-08-17 05:54:34 +00004962 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004963 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004964 return false;
4965 }
4966
4967 // Adjust the index value if the source operands will be swapped.
4968 if (ReverseVEXT)
4969 Imm -= NumElts;
4970
Bob Wilson32cd8552009-08-19 17:03:43 +00004971 return true;
4972}
4973
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004974/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4975/// instruction with the specified blocksize. (The order of the elements
4976/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004977static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004978 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4979 "Only possible block sizes for VREV are: 16, 32, 64");
4980
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004981 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004982 if (EltSz == 64)
4983 return false;
4984
4985 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004986 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004987 // If the first shuffle index is UNDEF, be optimistic.
4988 if (M[0] < 0)
4989 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004990
4991 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4992 return false;
4993
4994 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004995 if (M[i] < 0) continue; // ignore UNDEF indices
4996 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004997 return false;
4998 }
4999
5000 return true;
5001}
5002
Benjamin Kramer339ced42012-01-15 13:16:05 +00005003static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005004 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5005 // range, then 0 is placed into the resulting vector. So pretty much any mask
5006 // of 8 elements can work here.
5007 return VT == MVT::v8i8 && M.size() == 8;
5008}
5009
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005010// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5011// checking that pairs of elements in the shuffle mask represent the same index
5012// in each vector, incrementing the expected index by 2 at each step.
5013// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5014// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5015// v2={e,f,g,h}
5016// WhichResult gives the offset for each element in the mask based on which
5017// of the two results it belongs to.
5018//
5019// The transpose can be represented either as:
5020// result1 = shufflevector v1, v2, result1_shuffle_mask
5021// result2 = shufflevector v1, v2, result2_shuffle_mask
5022// where v1/v2 and the shuffle masks have the same number of elements
5023// (here WhichResult (see below) indicates which result is being checked)
5024//
5025// or as:
5026// results = shufflevector v1, v2, shuffle_mask
5027// where both results are returned in one vector and the shuffle mask has twice
5028// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5029// want to check the low half and high half of the shuffle mask as if it were
5030// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005031static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005032 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5033 if (EltSz == 64)
5034 return false;
5035
Bob Wilsona7062312009-08-21 20:54:19 +00005036 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005037 if (M.size() != NumElts && M.size() != NumElts*2)
5038 return false;
5039
James Molloy8c995a92015-09-10 08:42:28 +00005040 // If the mask is twice as long as the input vector then we need to check the
5041 // upper and lower parts of the mask with a matching value for WhichResult
5042 // FIXME: A mask with only even values will be rejected in case the first
5043 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5044 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005045 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005046 if (M.size() == NumElts * 2)
5047 WhichResult = i / NumElts;
5048 else
5049 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005050 for (unsigned j = 0; j < NumElts; j += 2) {
5051 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5052 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5053 return false;
5054 }
Bob Wilsona7062312009-08-21 20:54:19 +00005055 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005056
5057 if (M.size() == NumElts*2)
5058 WhichResult = 0;
5059
Bob Wilsona7062312009-08-21 20:54:19 +00005060 return true;
5061}
5062
Bob Wilson0bbd3072009-12-03 06:40:55 +00005063/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5064/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5065/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005066static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005067 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5068 if (EltSz == 64)
5069 return false;
5070
5071 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005072 if (M.size() != NumElts && M.size() != NumElts*2)
5073 return false;
5074
5075 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005076 if (M.size() == NumElts * 2)
5077 WhichResult = i / NumElts;
5078 else
5079 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005080 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 + WhichResult))
5083 return false;
5084 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005085 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005086
5087 if (M.size() == NumElts*2)
5088 WhichResult = 0;
5089
Bob Wilson0bbd3072009-12-03 06:40:55 +00005090 return true;
5091}
5092
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005093// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5094// that the mask elements are either all even and in steps of size 2 or all odd
5095// and in steps of size 2.
5096// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5097// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5098// v2={e,f,g,h}
5099// Requires similar checks to that of isVTRNMask with
5100// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005101static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005102 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5103 if (EltSz == 64)
5104 return false;
5105
Bob Wilsona7062312009-08-21 20:54:19 +00005106 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005107 if (M.size() != NumElts && M.size() != NumElts*2)
5108 return false;
5109
5110 for (unsigned i = 0; i < M.size(); i += NumElts) {
5111 WhichResult = M[i] == 0 ? 0 : 1;
5112 for (unsigned j = 0; j < NumElts; ++j) {
5113 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5114 return false;
5115 }
Bob Wilsona7062312009-08-21 20:54:19 +00005116 }
5117
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005118 if (M.size() == NumElts*2)
5119 WhichResult = 0;
5120
Bob Wilsona7062312009-08-21 20:54:19 +00005121 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005122 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005123 return false;
5124
5125 return true;
5126}
5127
Bob Wilson0bbd3072009-12-03 06:40:55 +00005128/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5129/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5130/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005131static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005132 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5133 if (EltSz == 64)
5134 return false;
5135
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005136 unsigned NumElts = VT.getVectorNumElements();
5137 if (M.size() != NumElts && M.size() != NumElts*2)
5138 return false;
5139
5140 unsigned Half = NumElts / 2;
5141 for (unsigned i = 0; i < M.size(); i += NumElts) {
5142 WhichResult = M[i] == 0 ? 0 : 1;
5143 for (unsigned j = 0; j < NumElts; j += Half) {
5144 unsigned Idx = WhichResult;
5145 for (unsigned k = 0; k < Half; ++k) {
5146 int MIdx = M[i + j + k];
5147 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5148 return false;
5149 Idx += 2;
5150 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005151 }
5152 }
5153
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005154 if (M.size() == NumElts*2)
5155 WhichResult = 0;
5156
Bob Wilson0bbd3072009-12-03 06:40:55 +00005157 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5158 if (VT.is64BitVector() && EltSz == 32)
5159 return false;
5160
5161 return true;
5162}
5163
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005164// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5165// that pairs of elements of the shufflemask represent the same index in each
5166// vector incrementing sequentially through the vectors.
5167// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5168// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5169// v2={e,f,g,h}
5170// Requires similar checks to that of isVTRNMask with respect the how results
5171// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005172static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005173 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5174 if (EltSz == 64)
5175 return false;
5176
Bob Wilsona7062312009-08-21 20:54:19 +00005177 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005178 if (M.size() != NumElts && M.size() != NumElts*2)
5179 return false;
5180
5181 for (unsigned i = 0; i < M.size(); i += NumElts) {
5182 WhichResult = M[i] == 0 ? 0 : 1;
5183 unsigned Idx = WhichResult * NumElts / 2;
5184 for (unsigned j = 0; j < NumElts; j += 2) {
5185 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5186 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5187 return false;
5188 Idx += 1;
5189 }
Bob Wilsona7062312009-08-21 20:54:19 +00005190 }
5191
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005192 if (M.size() == NumElts*2)
5193 WhichResult = 0;
5194
Bob Wilsona7062312009-08-21 20:54:19 +00005195 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005196 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005197 return false;
5198
5199 return true;
5200}
5201
Bob Wilson0bbd3072009-12-03 06:40:55 +00005202/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5203/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5204/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005205static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005206 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5207 if (EltSz == 64)
5208 return false;
5209
5210 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005211 if (M.size() != NumElts && M.size() != NumElts*2)
5212 return false;
5213
5214 for (unsigned i = 0; i < M.size(); i += NumElts) {
5215 WhichResult = M[i] == 0 ? 0 : 1;
5216 unsigned Idx = WhichResult * NumElts / 2;
5217 for (unsigned j = 0; j < NumElts; j += 2) {
5218 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5219 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5220 return false;
5221 Idx += 1;
5222 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005223 }
5224
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005225 if (M.size() == NumElts*2)
5226 WhichResult = 0;
5227
Bob Wilson0bbd3072009-12-03 06:40:55 +00005228 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5229 if (VT.is64BitVector() && EltSz == 32)
5230 return false;
5231
5232 return true;
5233}
5234
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005235/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5236/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5237static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5238 unsigned &WhichResult,
5239 bool &isV_UNDEF) {
5240 isV_UNDEF = false;
5241 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5242 return ARMISD::VTRN;
5243 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5244 return ARMISD::VUZP;
5245 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5246 return ARMISD::VZIP;
5247
5248 isV_UNDEF = true;
5249 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5250 return ARMISD::VTRN;
5251 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5252 return ARMISD::VUZP;
5253 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5254 return ARMISD::VZIP;
5255
5256 return 0;
5257}
5258
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005259/// \return true if this is a reverse operation on an vector.
5260static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5261 unsigned NumElts = VT.getVectorNumElements();
5262 // Make sure the mask has the right size.
5263 if (NumElts != M.size())
5264 return false;
5265
5266 // Look for <15, ..., 3, -1, 1, 0>.
5267 for (unsigned i = 0; i != NumElts; ++i)
5268 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5269 return false;
5270
5271 return true;
5272}
5273
Dale Johannesen2bff5052010-07-29 20:10:08 +00005274// If N is an integer constant that can be moved into a register in one
5275// instruction, return an SDValue of such a constant (will become a MOV
5276// instruction). Otherwise return null.
5277static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005278 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005279 uint64_t Val;
5280 if (!isa<ConstantSDNode>(N))
5281 return SDValue();
5282 Val = cast<ConstantSDNode>(N)->getZExtValue();
5283
5284 if (ST->isThumb1Only()) {
5285 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005286 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005287 } else {
5288 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005289 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005290 }
5291 return SDValue();
5292}
5293
Bob Wilson2e076c42009-06-22 23:27:02 +00005294// If this is a case we can't handle, return null and let the default
5295// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005296SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5297 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005298 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005299 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005300 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005301
5302 APInt SplatBits, SplatUndef;
5303 unsigned SplatBitSize;
5304 bool HasAnyUndefs;
5305 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005306 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005307 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005308 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005309 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005310 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005311 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005312 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005313 if (Val.getNode()) {
5314 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005315 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005316 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005317
5318 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005319 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005320 Val = isNEONModifiedImm(NegatedImm,
5321 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005322 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005323 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005324 if (Val.getNode()) {
5325 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005326 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005327 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005328
5329 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005330 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005331 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005332 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005333 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005334 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5335 }
5336 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005337 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005338 }
5339
Bob Wilson91fdf682010-05-22 00:23:12 +00005340 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005341 //
5342 // As an optimisation, even if more than one value is used it may be more
5343 // profitable to splat with one value then change some lanes.
5344 //
5345 // Heuristically we decide to do this if the vector has a "dominant" value,
5346 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005347 unsigned NumElts = VT.getVectorNumElements();
5348 bool isOnlyLowElement = true;
5349 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005350 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005351 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005352
5353 // Map of the number of times a particular SDValue appears in the
5354 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005355 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005356 SDValue Value;
5357 for (unsigned i = 0; i < NumElts; ++i) {
5358 SDValue V = Op.getOperand(i);
5359 if (V.getOpcode() == ISD::UNDEF)
5360 continue;
5361 if (i > 0)
5362 isOnlyLowElement = false;
5363 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5364 isConstant = false;
5365
James Molloy49bdbce2012-09-06 09:55:02 +00005366 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005367 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005368
James Molloy49bdbce2012-09-06 09:55:02 +00005369 // Is this value dominant? (takes up more than half of the lanes)
5370 if (++Count > (NumElts / 2)) {
5371 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005372 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005373 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005374 }
James Molloy49bdbce2012-09-06 09:55:02 +00005375 if (ValueCounts.size() != 1)
5376 usesOnlyOneValue = false;
5377 if (!Value.getNode() && ValueCounts.size() > 0)
5378 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005379
James Molloy49bdbce2012-09-06 09:55:02 +00005380 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005381 return DAG.getUNDEF(VT);
5382
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005383 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5384 // Keep going if we are hitting this case.
5385 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005386 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5387
Dale Johannesen2bff5052010-07-29 20:10:08 +00005388 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5389
Dale Johannesen710a2d92010-10-19 20:00:17 +00005390 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5391 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005392 if (hasDominantValue && EltSize <= 32) {
5393 if (!isConstant) {
5394 SDValue N;
5395
5396 // If we are VDUPing a value that comes directly from a vector, that will
5397 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005398 // just use VDUPLANE. We can only do this if the lane being extracted
5399 // is at a constant index, as the VDUP from lane instructions only have
5400 // constant-index forms.
5401 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5402 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005403 // We need to create a new undef vector to use for the VDUPLANE if the
5404 // size of the vector from which we get the value is different than the
5405 // size of the vector that we need to create. We will insert the element
5406 // such that the register coalescer will remove unnecessary copies.
5407 if (VT != Value->getOperand(0).getValueType()) {
5408 ConstantSDNode *constIndex;
5409 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5410 assert(constIndex && "The index is not a constant!");
5411 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5412 VT.getVectorNumElements();
5413 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5414 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005415 Value, DAG.getConstant(index, dl, MVT::i32)),
5416 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005417 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005418 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005419 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005420 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005421 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5422
5423 if (!usesOnlyOneValue) {
5424 // The dominant value was splatted as 'N', but we now have to insert
5425 // all differing elements.
5426 for (unsigned I = 0; I < NumElts; ++I) {
5427 if (Op.getOperand(I) == Value)
5428 continue;
5429 SmallVector<SDValue, 3> Ops;
5430 Ops.push_back(N);
5431 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005432 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005433 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005434 }
5435 }
5436 return N;
5437 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005438 if (VT.getVectorElementType().isFloatingPoint()) {
5439 SmallVector<SDValue, 8> Ops;
5440 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005441 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005442 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005443 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005444 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005445 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5446 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005447 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005448 }
James Molloy49bdbce2012-09-06 09:55:02 +00005449 if (usesOnlyOneValue) {
5450 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5451 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005452 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005453 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005454 }
5455
5456 // If all elements are constants and the case above didn't get hit, fall back
5457 // to the default expansion, which will generate a load from the constant
5458 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005459 if (isConstant)
5460 return SDValue();
5461
Bob Wilson6f2b8962011-01-07 21:37:30 +00005462 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5463 if (NumElts >= 4) {
5464 SDValue shuffle = ReconstructShuffle(Op, DAG);
5465 if (shuffle != SDValue())
5466 return shuffle;
5467 }
5468
Bob Wilson91fdf682010-05-22 00:23:12 +00005469 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005470 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5471 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005472 if (EltSize >= 32) {
5473 // Do the expansion with floating-point types, since that is what the VFP
5474 // registers are defined to use, and since i64 is not legal.
5475 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5476 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005477 SmallVector<SDValue, 8> Ops;
5478 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005479 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005480 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005481 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005482 }
5483
Jim Grosbach24e102a2013-07-08 18:18:52 +00005484 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5485 // know the default expansion would otherwise fall back on something even
5486 // worse. For a vector with one or two non-undef values, that's
5487 // scalar_to_vector for the elements followed by a shuffle (provided the
5488 // shuffle is valid for the target) and materialization element by element
5489 // on the stack followed by a load for everything else.
5490 if (!isConstant && !usesOnlyOneValue) {
5491 SDValue Vec = DAG.getUNDEF(VT);
5492 for (unsigned i = 0 ; i < NumElts; ++i) {
5493 SDValue V = Op.getOperand(i);
5494 if (V.getOpcode() == ISD::UNDEF)
5495 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005496 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005497 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5498 }
5499 return Vec;
5500 }
5501
Bob Wilson2e076c42009-06-22 23:27:02 +00005502 return SDValue();
5503}
5504
Bob Wilson6f2b8962011-01-07 21:37:30 +00005505// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005506// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005507SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5508 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005509 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005510 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005511 EVT VT = Op.getValueType();
5512 unsigned NumElts = VT.getVectorNumElements();
5513
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005514 struct ShuffleSourceInfo {
5515 SDValue Vec;
5516 unsigned MinElt;
5517 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005518
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005519 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5520 // be compatible with the shuffle we intend to construct. As a result
5521 // ShuffleVec will be some sliding window into the original Vec.
5522 SDValue ShuffleVec;
5523
5524 // Code should guarantee that element i in Vec starts at element "WindowBase
5525 // + i * WindowScale in ShuffleVec".
5526 int WindowBase;
5527 int WindowScale;
5528
5529 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5530 ShuffleSourceInfo(SDValue Vec)
5531 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5532 WindowScale(1) {}
5533 };
5534
5535 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5536 // node.
5537 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005538 for (unsigned i = 0; i < NumElts; ++i) {
5539 SDValue V = Op.getOperand(i);
5540 if (V.getOpcode() == ISD::UNDEF)
5541 continue;
5542 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5543 // A shuffle can only come from building a vector from various
5544 // elements of other vectors.
5545 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005546 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5547 // Furthermore, shuffles require a constant mask, whereas extractelts
5548 // accept variable indices.
5549 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005550 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005551
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005552 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005553 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005554 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5555 if (Source == Sources.end())
5556 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005557
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005558 // Update the minimum and maximum lane number seen.
5559 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5560 Source->MinElt = std::min(Source->MinElt, EltNo);
5561 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005562 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005563
Bob Wilson6f2b8962011-01-07 21:37:30 +00005564 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005565 // are involved.
5566 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005567 return SDValue();
5568
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005569 // Find out the smallest element size among result and two sources, and use
5570 // it as element size to build the shuffle_vector.
5571 EVT SmallestEltTy = VT.getVectorElementType();
5572 for (auto &Source : Sources) {
5573 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5574 if (SrcEltTy.bitsLT(SmallestEltTy))
5575 SmallestEltTy = SrcEltTy;
5576 }
5577 unsigned ResMultiplier =
5578 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5579 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5580 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005581
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005582 // If the source vector is too wide or too narrow, we may nevertheless be able
5583 // to construct a compatible shuffle either by concatenating it with UNDEF or
5584 // extracting a suitable range of elements.
5585 for (auto &Src : Sources) {
5586 EVT SrcVT = Src.ShuffleVec.getValueType();
5587
5588 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005589 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005590
5591 // This stage of the search produces a source with the same element type as
5592 // the original, but with a total width matching the BUILD_VECTOR output.
5593 EVT EltVT = SrcVT.getVectorElementType();
5594 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5595 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5596
5597 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5598 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5599 return SDValue();
5600 // We can pad out the smaller vector for free, so if it's part of a
5601 // shuffle...
5602 Src.ShuffleVec =
5603 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5604 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5605 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005606 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005607
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005608 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5609 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005610
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005611 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005612 // Span too large for a VEXT to cope
5613 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005614 }
5615
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005616 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005617 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005618 Src.ShuffleVec =
5619 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5620 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5621 Src.WindowBase = -NumSrcElts;
5622 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005623 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005624 Src.ShuffleVec =
5625 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5626 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005627 } else {
5628 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005629 SDValue VEXTSrc1 =
5630 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5631 DAG.getConstant(0, dl, MVT::i32));
5632 SDValue VEXTSrc2 =
5633 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5634 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005635
5636 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5637 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005638 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005639 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005640 }
5641 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005642
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005643 // Another possible incompatibility occurs from the vector element types. We
5644 // can fix this by bitcasting the source vectors to the same type we intend
5645 // for the shuffle.
5646 for (auto &Src : Sources) {
5647 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5648 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005649 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005650 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5651 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5652 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5653 Src.WindowBase *= Src.WindowScale;
5654 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005655
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005656 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005657 DEBUG(
5658 for (auto Src : Sources)
5659 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5660 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005661
5662 // The stars all align, our next step is to produce the mask for the shuffle.
5663 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5664 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5665 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5666 SDValue Entry = Op.getOperand(i);
5667 if (Entry.getOpcode() == ISD::UNDEF)
5668 continue;
5669
5670 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5671 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5672
5673 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5674 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5675 // segment.
5676 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5677 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5678 VT.getVectorElementType().getSizeInBits());
5679 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5680
5681 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5682 // starting at the appropriate offset.
5683 int *LaneMask = &Mask[i * ResMultiplier];
5684
5685 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5686 ExtractBase += NumElts * (Src - Sources.begin());
5687 for (int j = 0; j < LanesDefined; ++j)
5688 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005689 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005690
Bob Wilson6f2b8962011-01-07 21:37:30 +00005691 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005692 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5693 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005694
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005695 // We can't handle more than two sources. This should have already
5696 // been checked before this point.
5697 assert(Sources.size() <= 2 && "Too many sources!");
5698
5699 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5700 for (unsigned i = 0; i < Sources.size(); ++i)
5701 ShuffleOps[i] = Sources[i].ShuffleVec;
5702
5703 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5704 ShuffleOps[1], &Mask[0]);
5705 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005706}
5707
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005708/// isShuffleMaskLegal - Targets can use this to indicate that they only
5709/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5710/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5711/// are assumed to be legal.
5712bool
5713ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5714 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005715 if (VT.getVectorNumElements() == 4 &&
5716 (VT.is128BitVector() || VT.is64BitVector())) {
5717 unsigned PFIndexes[4];
5718 for (unsigned i = 0; i != 4; ++i) {
5719 if (M[i] < 0)
5720 PFIndexes[i] = 8;
5721 else
5722 PFIndexes[i] = M[i];
5723 }
5724
5725 // Compute the index in the perfect shuffle table.
5726 unsigned PFTableIndex =
5727 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5728 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5729 unsigned Cost = (PFEntry >> 30);
5730
5731 if (Cost <= 4)
5732 return true;
5733 }
5734
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005735 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005736 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005737
Bob Wilson846bd792010-06-07 23:53:38 +00005738 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5739 return (EltSize >= 32 ||
5740 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005741 isVREVMask(M, VT, 64) ||
5742 isVREVMask(M, VT, 32) ||
5743 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005744 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005745 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005746 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005747 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005748}
5749
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005750/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5751/// the specified operations to build the shuffle.
5752static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5753 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005754 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005755 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5756 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5757 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5758
5759 enum {
5760 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5761 OP_VREV,
5762 OP_VDUP0,
5763 OP_VDUP1,
5764 OP_VDUP2,
5765 OP_VDUP3,
5766 OP_VEXT1,
5767 OP_VEXT2,
5768 OP_VEXT3,
5769 OP_VUZPL, // VUZP, left result
5770 OP_VUZPR, // VUZP, right result
5771 OP_VZIPL, // VZIP, left result
5772 OP_VZIPR, // VZIP, right result
5773 OP_VTRNL, // VTRN, left result
5774 OP_VTRNR // VTRN, right result
5775 };
5776
5777 if (OpNum == OP_COPY) {
5778 if (LHSID == (1*9+2)*9+3) return LHS;
5779 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5780 return RHS;
5781 }
5782
5783 SDValue OpLHS, OpRHS;
5784 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5785 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5786 EVT VT = OpLHS.getValueType();
5787
5788 switch (OpNum) {
5789 default: llvm_unreachable("Unknown shuffle opcode!");
5790 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005791 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005792 if (VT.getVectorElementType() == MVT::i32 ||
5793 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005794 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5795 // vrev <4 x i16> -> VREV32
5796 if (VT.getVectorElementType() == MVT::i16)
5797 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5798 // vrev <4 x i8> -> VREV16
5799 assert(VT.getVectorElementType() == MVT::i8);
5800 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005801 case OP_VDUP0:
5802 case OP_VDUP1:
5803 case OP_VDUP2:
5804 case OP_VDUP3:
5805 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005806 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005807 case OP_VEXT1:
5808 case OP_VEXT2:
5809 case OP_VEXT3:
5810 return DAG.getNode(ARMISD::VEXT, dl, VT,
5811 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005812 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005813 case OP_VUZPL:
5814 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005815 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005816 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5817 case OP_VZIPL:
5818 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005819 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005820 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5821 case OP_VTRNL:
5822 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005823 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5824 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005825 }
5826}
5827
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005828static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005829 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005830 SelectionDAG &DAG) {
5831 // Check to see if we can use the VTBL instruction.
5832 SDValue V1 = Op.getOperand(0);
5833 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005834 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005835
5836 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005837 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005838 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005839 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005840
5841 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5842 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005843 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005844
Owen Anderson77aa2662011-04-05 21:48:57 +00005845 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005846 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005847}
5848
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005849static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5850 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005851 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005852 SDValue OpLHS = Op.getOperand(0);
5853 EVT VT = OpLHS.getValueType();
5854
5855 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5856 "Expect an v8i16/v16i8 type");
5857 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5858 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5859 // extract the first 8 bytes into the top double word and the last 8 bytes
5860 // into the bottom double word. The v8i16 case is similar.
5861 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5862 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005863 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005864}
5865
Bob Wilson2e076c42009-06-22 23:27:02 +00005866static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005867 SDValue V1 = Op.getOperand(0);
5868 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005869 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005870 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005871 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005872
Bob Wilsonc6800b52009-08-13 02:13:04 +00005873 // Convert shuffles that are directly supported on NEON to target-specific
5874 // DAG nodes, instead of keeping them as shuffles and matching them again
5875 // during code selection. This is more efficient and avoids the possibility
5876 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005877 // FIXME: floating-point vectors should be canonicalized to integer vectors
5878 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005879 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005880
Bob Wilson846bd792010-06-07 23:53:38 +00005881 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5882 if (EltSize <= 32) {
5883 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5884 int Lane = SVN->getSplatIndex();
5885 // If this is undef splat, generate it via "just" vdup, if possible.
5886 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005887
Dan Gohman198b7ff2011-11-03 21:49:52 +00005888 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005889 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5890 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5891 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005892 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5893 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5894 // reaches it).
5895 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5896 !isa<ConstantSDNode>(V1.getOperand(0))) {
5897 bool IsScalarToVector = true;
5898 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5899 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5900 IsScalarToVector = false;
5901 break;
5902 }
5903 if (IsScalarToVector)
5904 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5905 }
Bob Wilson846bd792010-06-07 23:53:38 +00005906 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005907 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005908 }
Bob Wilson846bd792010-06-07 23:53:38 +00005909
5910 bool ReverseVEXT;
5911 unsigned Imm;
5912 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5913 if (ReverseVEXT)
5914 std::swap(V1, V2);
5915 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005916 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005917 }
5918
5919 if (isVREVMask(ShuffleMask, VT, 64))
5920 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5921 if (isVREVMask(ShuffleMask, VT, 32))
5922 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5923 if (isVREVMask(ShuffleMask, VT, 16))
5924 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5925
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005926 if (V2->getOpcode() == ISD::UNDEF &&
5927 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5928 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005929 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005930 }
5931
Bob Wilson846bd792010-06-07 23:53:38 +00005932 // Check for Neon shuffles that modify both input vectors in place.
5933 // If both results are used, i.e., if there are two shuffles with the same
5934 // source operands and with masks corresponding to both results of one of
5935 // these operations, DAG memoization will ensure that a single node is
5936 // used for both shuffles.
5937 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005938 bool isV_UNDEF;
5939 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5940 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5941 if (isV_UNDEF)
5942 V2 = V1;
5943 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5944 .getValue(WhichResult);
5945 }
Bob Wilson846bd792010-06-07 23:53:38 +00005946
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005947 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5948 // shuffles that produce a result larger than their operands with:
5949 // shuffle(concat(v1, undef), concat(v2, undef))
5950 // ->
5951 // shuffle(concat(v1, v2), undef)
5952 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5953 //
5954 // This is useful in the general case, but there are special cases where
5955 // native shuffles produce larger results: the two-result ops.
5956 //
5957 // Look through the concat when lowering them:
5958 // shuffle(concat(v1, v2), undef)
5959 // ->
5960 // concat(VZIP(v1, v2):0, :1)
5961 //
5962 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5963 V2->getOpcode() == ISD::UNDEF) {
5964 SDValue SubV1 = V1->getOperand(0);
5965 SDValue SubV2 = V1->getOperand(1);
5966 EVT SubVT = SubV1.getValueType();
5967
5968 // We expect these to have been canonicalized to -1.
5969 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5970 return i < (int)VT.getVectorNumElements();
5971 }) && "Unexpected shuffle index into UNDEF operand!");
5972
5973 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5974 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5975 if (isV_UNDEF)
5976 SubV2 = SubV1;
5977 assert((WhichResult == 0) &&
5978 "In-place shuffle of concat can only have one result!");
5979 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
5980 SubV1, SubV2);
5981 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
5982 Res.getValue(1));
5983 }
5984 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00005985 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005986
Bob Wilsona7062312009-08-21 20:54:19 +00005987 // If the shuffle is not directly supported and it has 4 elements, use
5988 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005989 unsigned NumElts = VT.getVectorNumElements();
5990 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005991 unsigned PFIndexes[4];
5992 for (unsigned i = 0; i != 4; ++i) {
5993 if (ShuffleMask[i] < 0)
5994 PFIndexes[i] = 8;
5995 else
5996 PFIndexes[i] = ShuffleMask[i];
5997 }
5998
5999 // Compute the index in the perfect shuffle table.
6000 unsigned PFTableIndex =
6001 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006002 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6003 unsigned Cost = (PFEntry >> 30);
6004
6005 if (Cost <= 4)
6006 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6007 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006008
Bob Wilsond8a9a042010-06-04 00:04:02 +00006009 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006010 if (EltSize >= 32) {
6011 // Do the expansion with floating-point types, since that is what the VFP
6012 // registers are defined to use, and since i64 is not legal.
6013 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6014 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006015 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6016 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006017 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006018 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006019 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006020 Ops.push_back(DAG.getUNDEF(EltVT));
6021 else
6022 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6023 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6024 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006025 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006026 }
Craig Topper48d114b2014-04-26 18:35:24 +00006027 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006028 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006029 }
6030
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006031 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6032 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6033
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006034 if (VT == MVT::v8i8) {
6035 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
6036 if (NewOp.getNode())
6037 return NewOp;
6038 }
6039
Bob Wilson6f34e272009-08-14 05:16:33 +00006040 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006041}
6042
Eli Friedmana5e244c2011-10-24 23:08:52 +00006043static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6044 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6045 SDValue Lane = Op.getOperand(2);
6046 if (!isa<ConstantSDNode>(Lane))
6047 return SDValue();
6048
6049 return Op;
6050}
6051
Bob Wilson2e076c42009-06-22 23:27:02 +00006052static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006053 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006054 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006055 if (!isa<ConstantSDNode>(Lane))
6056 return SDValue();
6057
6058 SDValue Vec = Op.getOperand(0);
6059 if (Op.getValueType() == MVT::i32 &&
6060 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006061 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006062 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6063 }
6064
6065 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006066}
6067
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006068static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6069 // The only time a CONCAT_VECTORS operation can have legal types is when
6070 // two 64-bit vectors are concatenated to a 128-bit vector.
6071 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6072 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006073 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006074 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006075 SDValue Op0 = Op.getOperand(0);
6076 SDValue Op1 = Op.getOperand(1);
6077 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006078 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006079 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006080 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006081 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00006082 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006083 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006084 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006085 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006086}
6087
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006088/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6089/// element has been zero/sign-extended, depending on the isSigned parameter,
6090/// from an integer type half its size.
6091static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6092 bool isSigned) {
6093 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6094 EVT VT = N->getValueType(0);
6095 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6096 SDNode *BVN = N->getOperand(0).getNode();
6097 if (BVN->getValueType(0) != MVT::v4i32 ||
6098 BVN->getOpcode() != ISD::BUILD_VECTOR)
6099 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006100 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006101 unsigned HiElt = 1 - LoElt;
6102 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6103 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6104 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6105 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6106 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6107 return false;
6108 if (isSigned) {
6109 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6110 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6111 return true;
6112 } else {
6113 if (Hi0->isNullValue() && Hi1->isNullValue())
6114 return true;
6115 }
6116 return false;
6117 }
6118
6119 if (N->getOpcode() != ISD::BUILD_VECTOR)
6120 return false;
6121
6122 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6123 SDNode *Elt = N->getOperand(i).getNode();
6124 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6125 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6126 unsigned HalfSize = EltSize / 2;
6127 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006128 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006129 return false;
6130 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006131 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006132 return false;
6133 }
6134 continue;
6135 }
6136 return false;
6137 }
6138
6139 return true;
6140}
6141
6142/// isSignExtended - Check if a node is a vector value that is sign-extended
6143/// or a constant BUILD_VECTOR with sign-extended elements.
6144static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6145 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6146 return true;
6147 if (isExtendedBUILD_VECTOR(N, DAG, true))
6148 return true;
6149 return false;
6150}
6151
6152/// isZeroExtended - Check if a node is a vector value that is zero-extended
6153/// or a constant BUILD_VECTOR with zero-extended elements.
6154static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6155 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6156 return true;
6157 if (isExtendedBUILD_VECTOR(N, DAG, false))
6158 return true;
6159 return false;
6160}
6161
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006162static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6163 if (OrigVT.getSizeInBits() >= 64)
6164 return OrigVT;
6165
6166 assert(OrigVT.isSimple() && "Expecting a simple value type");
6167
6168 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6169 switch (OrigSimpleTy) {
6170 default: llvm_unreachable("Unexpected Vector Type");
6171 case MVT::v2i8:
6172 case MVT::v2i16:
6173 return MVT::v2i32;
6174 case MVT::v4i8:
6175 return MVT::v4i16;
6176 }
6177}
6178
Sebastian Popa204f722012-11-30 19:08:04 +00006179/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6180/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6181/// We insert the required extension here to get the vector to fill a D register.
6182static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6183 const EVT &OrigTy,
6184 const EVT &ExtTy,
6185 unsigned ExtOpcode) {
6186 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6187 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6188 // 64-bits we need to insert a new extension so that it will be 64-bits.
6189 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6190 if (OrigTy.getSizeInBits() >= 64)
6191 return N;
6192
6193 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006194 EVT NewVT = getExtensionTo64Bits(OrigTy);
6195
Andrew Trickef9de2a2013-05-25 02:42:55 +00006196 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006197}
6198
6199/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6200/// does not do any sign/zero extension. If the original vector is less
6201/// than 64 bits, an appropriate extension will be added after the load to
6202/// reach a total size of 64 bits. We have to add the extension separately
6203/// because ARM does not have a sign/zero extending load for vectors.
6204static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006205 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6206
6207 // The load already has the right type.
6208 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006209 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006210 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6211 LD->isNonTemporal(), LD->isInvariant(),
6212 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006213
6214 // We need to create a zextload/sextload. We cannot just create a load
6215 // followed by a zext/zext node because LowerMUL is also run during normal
6216 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006217 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006218 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006219 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006220 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006221}
6222
6223/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6224/// extending load, or BUILD_VECTOR with extended elements, return the
6225/// unextended value. The unextended vector should be 64 bits so that it can
6226/// be used as an operand to a VMULL instruction. If the original vector size
6227/// before extension is less than 64 bits we add a an extension to resize
6228/// the vector to 64 bits.
6229static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006230 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006231 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6232 N->getOperand(0)->getValueType(0),
6233 N->getValueType(0),
6234 N->getOpcode());
6235
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006236 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006237 return SkipLoadExtensionForVMULL(LD, DAG);
6238
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006239 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6240 // have been legalized as a BITCAST from v4i32.
6241 if (N->getOpcode() == ISD::BITCAST) {
6242 SDNode *BVN = N->getOperand(0).getNode();
6243 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6244 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006245 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006246 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006247 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6248 }
6249 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6250 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6251 EVT VT = N->getValueType(0);
6252 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6253 unsigned NumElts = VT.getVectorNumElements();
6254 MVT TruncVT = MVT::getIntegerVT(EltSize);
6255 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006256 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006257 for (unsigned i = 0; i != NumElts; ++i) {
6258 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6259 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006260 // Element types smaller than 32 bits are not legal, so use i32 elements.
6261 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006262 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006263 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006264 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006265 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006266}
6267
Evan Chenge2086e72011-03-29 01:56:09 +00006268static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6269 unsigned Opcode = N->getOpcode();
6270 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6271 SDNode *N0 = N->getOperand(0).getNode();
6272 SDNode *N1 = N->getOperand(1).getNode();
6273 return N0->hasOneUse() && N1->hasOneUse() &&
6274 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6275 }
6276 return false;
6277}
6278
6279static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6280 unsigned Opcode = N->getOpcode();
6281 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6282 SDNode *N0 = N->getOperand(0).getNode();
6283 SDNode *N1 = N->getOperand(1).getNode();
6284 return N0->hasOneUse() && N1->hasOneUse() &&
6285 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6286 }
6287 return false;
6288}
6289
Bob Wilson38ab35a2010-09-01 23:50:19 +00006290static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6291 // Multiplications are only custom-lowered for 128-bit vectors so that
6292 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6293 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006294 assert(VT.is128BitVector() && VT.isInteger() &&
6295 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006296 SDNode *N0 = Op.getOperand(0).getNode();
6297 SDNode *N1 = Op.getOperand(1).getNode();
6298 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006299 bool isMLA = false;
6300 bool isN0SExt = isSignExtended(N0, DAG);
6301 bool isN1SExt = isSignExtended(N1, DAG);
6302 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006303 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006304 else {
6305 bool isN0ZExt = isZeroExtended(N0, DAG);
6306 bool isN1ZExt = isZeroExtended(N1, DAG);
6307 if (isN0ZExt && isN1ZExt)
6308 NewOpc = ARMISD::VMULLu;
6309 else if (isN1SExt || isN1ZExt) {
6310 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6311 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6312 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6313 NewOpc = ARMISD::VMULLs;
6314 isMLA = true;
6315 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6316 NewOpc = ARMISD::VMULLu;
6317 isMLA = true;
6318 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6319 std::swap(N0, N1);
6320 NewOpc = ARMISD::VMULLu;
6321 isMLA = true;
6322 }
6323 }
6324
6325 if (!NewOpc) {
6326 if (VT == MVT::v2i64)
6327 // Fall through to expand this. It is not legal.
6328 return SDValue();
6329 else
6330 // Other vector multiplications are legal.
6331 return Op;
6332 }
6333 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006334
6335 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006336 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006337 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006338 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006339 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006340 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006341 assert(Op0.getValueType().is64BitVector() &&
6342 Op1.getValueType().is64BitVector() &&
6343 "unexpected types for extended operands to VMULL");
6344 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6345 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006346
Evan Chenge2086e72011-03-29 01:56:09 +00006347 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6348 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6349 // vmull q0, d4, d6
6350 // vmlal q0, d5, d6
6351 // is faster than
6352 // vaddl q0, d4, d5
6353 // vmovl q1, d6
6354 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006355 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6356 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006357 EVT Op1VT = Op1.getValueType();
6358 return DAG.getNode(N0->getOpcode(), DL, VT,
6359 DAG.getNode(NewOpc, DL, VT,
6360 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6361 DAG.getNode(NewOpc, DL, VT,
6362 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006363}
6364
Owen Anderson77aa2662011-04-05 21:48:57 +00006365static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006366LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006367 // TODO: Should this propagate fast-math-flags?
6368
Nate Begemanfa62d502011-02-11 20:53:29 +00006369 // Convert to float
6370 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6371 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6372 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6373 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6374 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6375 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6376 // Get reciprocal estimate.
6377 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006378 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006379 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6380 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006381 // Because char has a smaller range than uchar, we can actually get away
6382 // without any newton steps. This requires that we use a weird bias
6383 // of 0xb000, however (again, this has been exhaustively tested).
6384 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6385 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6386 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006387 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006388 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6389 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6390 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6391 // Convert back to short.
6392 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6393 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6394 return X;
6395}
6396
Owen Anderson77aa2662011-04-05 21:48:57 +00006397static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006398LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006399 // TODO: Should this propagate fast-math-flags?
6400
Nate Begemanfa62d502011-02-11 20:53:29 +00006401 SDValue N2;
6402 // Convert to float.
6403 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6404 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6405 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6406 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6407 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6408 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006409
Nate Begemanfa62d502011-02-11 20:53:29 +00006410 // Use reciprocal estimate and one refinement step.
6411 // float4 recip = vrecpeq_f32(yf);
6412 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006413 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006414 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6415 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006416 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006417 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006418 N1, N2);
6419 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6420 // Because short has a smaller range than ushort, we can actually get away
6421 // with only a single newton step. This requires that we use a weird bias
6422 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006423 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006424 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6425 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006426 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006427 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6428 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6429 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6430 // Convert back to integer and return.
6431 // return vmovn_s32(vcvt_s32_f32(result));
6432 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6433 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6434 return N0;
6435}
6436
6437static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6438 EVT VT = Op.getValueType();
6439 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6440 "unexpected type for custom-lowering ISD::SDIV");
6441
Andrew Trickef9de2a2013-05-25 02:42:55 +00006442 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006443 SDValue N0 = Op.getOperand(0);
6444 SDValue N1 = Op.getOperand(1);
6445 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006446
Nate Begemanfa62d502011-02-11 20:53:29 +00006447 if (VT == MVT::v8i8) {
6448 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6449 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006450
Nate Begemanfa62d502011-02-11 20:53:29 +00006451 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006452 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006453 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006454 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006455 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006456 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006457 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006458 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006459
6460 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6461 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6462
6463 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6464 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006465
Nate Begemanfa62d502011-02-11 20:53:29 +00006466 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6467 return N0;
6468 }
6469 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6470}
6471
6472static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006473 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006474 EVT VT = Op.getValueType();
6475 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6476 "unexpected type for custom-lowering ISD::UDIV");
6477
Andrew Trickef9de2a2013-05-25 02:42:55 +00006478 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006479 SDValue N0 = Op.getOperand(0);
6480 SDValue N1 = Op.getOperand(1);
6481 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006482
Nate Begemanfa62d502011-02-11 20:53:29 +00006483 if (VT == MVT::v8i8) {
6484 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6485 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006486
Nate Begemanfa62d502011-02-11 20:53:29 +00006487 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006488 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006489 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006490 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006491 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006492 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006493 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006494 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006495
Nate Begemanfa62d502011-02-11 20:53:29 +00006496 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6497 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006498
Nate Begemanfa62d502011-02-11 20:53:29 +00006499 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6500 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006501
6502 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006503 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6504 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006505 N0);
6506 return N0;
6507 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006508
Nate Begemanfa62d502011-02-11 20:53:29 +00006509 // v4i16 sdiv ... Convert to float.
6510 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6511 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6512 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6513 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6514 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006515 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006516
6517 // Use reciprocal estimate and two refinement steps.
6518 // float4 recip = vrecpeq_f32(yf);
6519 // recip *= vrecpsq_f32(yf, recip);
6520 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006521 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006522 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6523 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006524 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006525 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006526 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006527 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006528 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006529 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006530 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006531 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6532 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6533 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6534 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006535 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006536 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6537 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006538 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006539 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6540 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6541 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6542 // Convert back to integer and return.
6543 // return vmovn_u32(vcvt_s32_f32(result));
6544 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6545 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6546 return N0;
6547}
6548
Evan Chenge8916542011-08-30 01:34:54 +00006549static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6550 EVT VT = Op.getNode()->getValueType(0);
6551 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6552
6553 unsigned Opc;
6554 bool ExtraOp = false;
6555 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006556 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006557 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6558 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6559 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6560 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6561 }
6562
6563 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006564 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006565 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006566 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006567 Op.getOperand(1), Op.getOperand(2));
6568}
6569
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006570SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6571 assert(Subtarget->isTargetDarwin());
6572
6573 // For iOS, we want to call an alternative entry point: __sincos_stret,
6574 // return values are passed via sret.
6575 SDLoc dl(Op);
6576 SDValue Arg = Op.getOperand(0);
6577 EVT ArgVT = Arg.getValueType();
6578 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006579 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006580
6581 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006582
6583 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006584 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006585
6586 // Create stack object for sret.
Mehdi Amini44ede332015-07-09 02:09:04 +00006587 auto &DL = DAG.getDataLayout();
6588 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6589 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006590 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00006591 SDValue SRet = DAG.getFrameIndex(FrameIdx, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006592
6593 ArgListTy Args;
6594 ArgListEntry Entry;
6595
6596 Entry.Node = SRet;
6597 Entry.Ty = RetTy->getPointerTo();
6598 Entry.isSExt = false;
6599 Entry.isZExt = false;
6600 Entry.isSRet = true;
6601 Args.push_back(Entry);
6602
6603 Entry.Node = Arg;
6604 Entry.Ty = ArgTy;
6605 Entry.isSExt = false;
6606 Entry.isZExt = false;
6607 Args.push_back(Entry);
6608
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006609 const char *LibcallName =
6610 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Mehdi Amini44ede332015-07-09 02:09:04 +00006611 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006612
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006613 TargetLowering::CallLoweringInfo CLI(DAG);
6614 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6615 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006616 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006617 .setDiscardResult();
6618
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006619 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6620
6621 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6622 MachinePointerInfo(), false, false, false, 0);
6623
6624 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006625 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006626 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006627 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6628 MachinePointerInfo(), false, false, false, 0);
6629
6630 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6631 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6632 LoadSin.getValue(0), LoadCos.getValue(0));
6633}
6634
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006635SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
6636 bool Signed,
6637 SDValue &Chain) const {
6638 EVT VT = Op.getValueType();
6639 assert((VT == MVT::i32 || VT == MVT::i64) &&
6640 "unexpected type for custom lowering DIV");
6641 SDLoc dl(Op);
6642
6643 const auto &DL = DAG.getDataLayout();
6644 const auto &TLI = DAG.getTargetLoweringInfo();
6645
6646 const char *Name = nullptr;
6647 if (Signed)
6648 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6649 else
6650 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
6651
6652 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6653
6654 ARMTargetLowering::ArgListTy Args;
6655
6656 for (auto AI : {1, 0}) {
6657 ArgListEntry Arg;
6658 Arg.Node = Op.getOperand(AI);
6659 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6660 Args.push_back(Arg);
6661 }
6662
6663 CallLoweringInfo CLI(DAG);
6664 CLI.setDebugLoc(dl)
6665 .setChain(Chain)
6666 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6667 ES, std::move(Args), 0);
6668
6669 return LowerCallTo(CLI).first;
6670}
6671
6672SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6673 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006674 assert(Op.getValueType() == MVT::i32 &&
6675 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006676 SDLoc dl(Op);
6677
6678 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6679 DAG.getEntryNode(), Op.getOperand(1));
6680
6681 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
6682}
6683
6684void ARMTargetLowering::ExpandDIV_Windows(
6685 SDValue Op, SelectionDAG &DAG, bool Signed,
6686 SmallVectorImpl<SDValue> &Results) const {
6687 const auto &DL = DAG.getDataLayout();
6688 const auto &TLI = DAG.getTargetLoweringInfo();
6689
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006690 assert(Op.getValueType() == MVT::i64 &&
6691 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006692 SDLoc dl(Op);
6693
6694 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6695 DAG.getConstant(0, dl, MVT::i32));
6696 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6697 DAG.getConstant(1, dl, MVT::i32));
6698 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6699
6700 SDValue DBZCHK =
6701 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6702
6703 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
6704
6705 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6706 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6707 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6708 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6709
6710 Results.push_back(Lower);
6711 Results.push_back(Upper);
6712}
6713
Eli Friedman10f9ce22011-09-15 22:26:18 +00006714static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006715 // Monotonic load/store is legal for all targets
6716 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6717 return Op;
6718
Alp Tokercb402912014-01-24 17:20:08 +00006719 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006720 // dmb or equivalent available.
6721 return SDValue();
6722}
6723
Tim Northoverbc933082013-05-23 19:11:20 +00006724static void ReplaceREADCYCLECOUNTER(SDNode *N,
6725 SmallVectorImpl<SDValue> &Results,
6726 SelectionDAG &DAG,
6727 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006728 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006729 // Under Power Management extensions, the cycle-count is:
6730 // mrc p15, #0, <Rt>, c9, c13, #0
6731 SDValue Ops[] = { N->getOperand(0), // Chain
6732 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6733 DAG.getConstant(15, DL, MVT::i32),
6734 DAG.getConstant(0, DL, MVT::i32),
6735 DAG.getConstant(9, DL, MVT::i32),
6736 DAG.getConstant(13, DL, MVT::i32),
6737 DAG.getConstant(0, DL, MVT::i32)
6738 };
Tim Northoverbc933082013-05-23 19:11:20 +00006739
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006740 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6741 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6742 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6743 DAG.getConstant(0, DL, MVT::i32)));
6744 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006745}
6746
Dan Gohman21cea8a2010-04-17 15:26:15 +00006747SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006748 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006749 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006750 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006751 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006752 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006753 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006754 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6755 default: llvm_unreachable("unknown object format");
6756 case Triple::COFF:
6757 return LowerGlobalAddressWindows(Op, DAG);
6758 case Triple::ELF:
6759 return LowerGlobalAddressELF(Op, DAG);
6760 case Triple::MachO:
6761 return LowerGlobalAddressDarwin(Op, DAG);
6762 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006763 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006764 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006765 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6766 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006767 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006768 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006769 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006770 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006771 case ISD::SINT_TO_FP:
6772 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6773 case ISD::FP_TO_SINT:
6774 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006775 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006776 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006777 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006778 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006779 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006780 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006781 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006782 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6783 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006784 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006785 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006786 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006787 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00006788 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
6789 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006790 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006791 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006792 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006793 case ISD::CTTZ:
6794 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006795 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006796 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006797 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006798 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006799 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006800 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006801 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006802 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006803 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006804 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006805 case ISD::SDIV:
6806 if (Subtarget->isTargetWindows())
6807 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
6808 return LowerSDIV(Op, DAG);
6809 case ISD::UDIV:
6810 if (Subtarget->isTargetWindows())
6811 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
6812 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006813 case ISD::ADDC:
6814 case ISD::ADDE:
6815 case ISD::SUBC:
6816 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006817 case ISD::SADDO:
6818 case ISD::UADDO:
6819 case ISD::SSUBO:
6820 case ISD::USUBO:
6821 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006822 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006823 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006824 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006825 case ISD::SDIVREM:
6826 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006827 case ISD::DYNAMIC_STACKALLOC:
6828 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6829 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6830 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006831 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6832 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006833 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00006834 }
Evan Cheng10043e22007-01-19 07:51:42 +00006835}
6836
Duncan Sands6ed40142008-12-01 11:39:25 +00006837/// ReplaceNodeResults - Replace the results of node with an illegal result
6838/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006839void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006840 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006841 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006842 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006843 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006844 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006845 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006846 case ISD::READ_REGISTER:
6847 ExpandREAD_REGISTER(N, Results, DAG);
6848 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006849 case ISD::BITCAST:
6850 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006851 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006852 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006853 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006854 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006855 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00006856 case ISD::SREM:
6857 case ISD::UREM:
6858 Res = LowerREM(N, DAG);
6859 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006860 case ISD::READCYCLECOUNTER:
6861 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6862 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006863 case ISD::UDIV:
6864 case ISD::SDIV:
6865 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
6866 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
6867 Results);
Duncan Sands6ed40142008-12-01 11:39:25 +00006868 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006869 if (Res.getNode())
6870 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006871}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006872
Evan Cheng10043e22007-01-19 07:51:42 +00006873//===----------------------------------------------------------------------===//
6874// ARM Scheduler Hooks
6875//===----------------------------------------------------------------------===//
6876
Bill Wendling030b58e2011-10-06 22:18:16 +00006877/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6878/// registers the function context.
6879void ARMTargetLowering::
6880SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6881 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006882 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006883 DebugLoc dl = MI->getDebugLoc();
6884 MachineFunction *MF = MBB->getParent();
6885 MachineRegisterInfo *MRI = &MF->getRegInfo();
6886 MachineConstantPool *MCP = MF->getConstantPool();
6887 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6888 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006889
Bill Wendling374ee192011-10-03 21:25:38 +00006890 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006891 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006892
Bill Wendling374ee192011-10-03 21:25:38 +00006893 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006894 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006895 ARMConstantPoolValue *CPV =
6896 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6897 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6898
Craig Topper61e88f42014-11-21 05:58:21 +00006899 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6900 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006901
Bill Wendling030b58e2011-10-06 22:18:16 +00006902 // Grab constant pool and fixed stack memory operands.
6903 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006904 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
6905 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006906
6907 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00006908 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
6909 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00006910
6911 // Load the address of the dispatch MBB into the jump buffer.
6912 if (isThumb2) {
6913 // Incoming value: jbuf
6914 // ldr.n r5, LCPI1_1
6915 // orr r5, r5, #1
6916 // add r5, pc
6917 // str r5, [$jbuf, #+4] ; &jbuf[1]
6918 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6919 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6920 .addConstantPoolIndex(CPI)
6921 .addMemOperand(CPMMO));
6922 // Set the low bit because of thumb mode.
6923 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6924 AddDefaultCC(
6925 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6926 .addReg(NewVReg1, RegState::Kill)
6927 .addImm(0x01)));
6928 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6929 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6930 .addReg(NewVReg2, RegState::Kill)
6931 .addImm(PCLabelId);
6932 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6933 .addReg(NewVReg3, RegState::Kill)
6934 .addFrameIndex(FI)
6935 .addImm(36) // &jbuf[1] :: pc
6936 .addMemOperand(FIMMOSt));
6937 } else if (isThumb) {
6938 // Incoming value: jbuf
6939 // ldr.n r1, LCPI1_4
6940 // add r1, pc
6941 // mov r2, #1
6942 // orrs r1, r2
6943 // add r2, $jbuf, #+4 ; &jbuf[1]
6944 // str r1, [r2]
6945 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6946 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6947 .addConstantPoolIndex(CPI)
6948 .addMemOperand(CPMMO));
6949 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6950 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6951 .addReg(NewVReg1, RegState::Kill)
6952 .addImm(PCLabelId);
6953 // Set the low bit because of thumb mode.
6954 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6955 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6956 .addReg(ARM::CPSR, RegState::Define)
6957 .addImm(1));
6958 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6959 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6960 .addReg(ARM::CPSR, RegState::Define)
6961 .addReg(NewVReg2, RegState::Kill)
6962 .addReg(NewVReg3, RegState::Kill));
6963 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006964 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6965 .addFrameIndex(FI)
6966 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006967 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6968 .addReg(NewVReg4, RegState::Kill)
6969 .addReg(NewVReg5, RegState::Kill)
6970 .addImm(0)
6971 .addMemOperand(FIMMOSt));
6972 } else {
6973 // Incoming value: jbuf
6974 // ldr r1, LCPI1_1
6975 // add r1, pc, r1
6976 // str r1, [$jbuf, #+4] ; &jbuf[1]
6977 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6978 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6979 .addConstantPoolIndex(CPI)
6980 .addImm(0)
6981 .addMemOperand(CPMMO));
6982 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6983 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6984 .addReg(NewVReg1, RegState::Kill)
6985 .addImm(PCLabelId));
6986 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6987 .addReg(NewVReg2, RegState::Kill)
6988 .addFrameIndex(FI)
6989 .addImm(36) // &jbuf[1] :: pc
6990 .addMemOperand(FIMMOSt));
6991 }
6992}
6993
Matthias Brauneec4efc2015-04-28 00:37:05 +00006994void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6995 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006996 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006997 DebugLoc dl = MI->getDebugLoc();
6998 MachineFunction *MF = MBB->getParent();
6999 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007000 MachineFrameInfo *MFI = MF->getFrameInfo();
7001 int FI = MFI->getFunctionContextIndex();
7002
Craig Topper61e88f42014-11-21 05:58:21 +00007003 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7004 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007005
Bill Wendling362c1b02011-10-06 21:29:56 +00007006 // Get a mapping of the call site numbers to all of the landing pads they're
7007 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007008 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7009 unsigned MaxCSNum = 0;
7010 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007011 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7012 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007013 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007014
7015 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7016 // pad.
7017 for (MachineBasicBlock::iterator
7018 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7019 if (!II->isEHLabel()) continue;
7020
7021 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007022 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007023
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007024 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7025 for (SmallVectorImpl<unsigned>::iterator
7026 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7027 CSI != CSE; ++CSI) {
7028 CallSiteNumToLPad[*CSI].push_back(BB);
7029 MaxCSNum = std::max(MaxCSNum, *CSI);
7030 }
Bill Wendling202803e2011-10-05 00:02:33 +00007031 break;
7032 }
7033 }
7034
7035 // Get an ordered list of the machine basic blocks for the jump table.
7036 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00007037 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007038 LPadList.reserve(CallSiteNumToLPad.size());
7039 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7040 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7041 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007042 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007043 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007044 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7045 }
Bill Wendling202803e2011-10-05 00:02:33 +00007046 }
7047
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007048 assert(!LPadList.empty() &&
7049 "No landing pad destinations for the dispatch jump table!");
7050
Bill Wendling362c1b02011-10-06 21:29:56 +00007051 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007052 MachineJumpTableInfo *JTI =
7053 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7054 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007055 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007056
Bill Wendling362c1b02011-10-06 21:29:56 +00007057 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007058
7059 // Shove the dispatch's address into the return slot in the function context.
7060 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007061 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007062
Bill Wendling324be982011-10-05 00:39:32 +00007063 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007064 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007065 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007066 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007067 else
7068 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7069
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007070 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007071 DispatchBB->addSuccessor(TrapBB);
7072
7073 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7074 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007075
Bill Wendling510fbcd2011-10-17 21:32:56 +00007076 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007077 MF->insert(MF->end(), DispatchBB);
7078 MF->insert(MF->end(), DispContBB);
7079 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007080
Bill Wendling030b58e2011-10-06 22:18:16 +00007081 // Insert code into the entry block that creates and registers the function
7082 // context.
7083 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7084
Alex Lorenze40c8a22015-08-11 23:09:45 +00007085 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7086 MachinePointerInfo::getFixedStack(*MF, FI),
7087 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007088
Chad Rosier1ec8e402012-11-06 23:05:24 +00007089 MachineInstrBuilder MIB;
7090 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7091
7092 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7093 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7094
7095 // Add a register mask with no preserved registers. This results in all
7096 // registers being marked as clobbered.
7097 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007098
Bill Wendling85833f72011-10-18 22:49:07 +00007099 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007100 if (Subtarget->isThumb2()) {
7101 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7102 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7103 .addFrameIndex(FI)
7104 .addImm(4)
7105 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007106
Bill Wendling85833f72011-10-18 22:49:07 +00007107 if (NumLPads < 256) {
7108 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7109 .addReg(NewVReg1)
7110 .addImm(LPadList.size()));
7111 } else {
7112 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7113 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007114 .addImm(NumLPads & 0xFFFF));
7115
7116 unsigned VReg2 = VReg1;
7117 if ((NumLPads & 0xFFFF0000) != 0) {
7118 VReg2 = MRI->createVirtualRegister(TRC);
7119 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7120 .addReg(VReg1)
7121 .addImm(NumLPads >> 16));
7122 }
7123
Bill Wendling85833f72011-10-18 22:49:07 +00007124 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7125 .addReg(NewVReg1)
7126 .addReg(VReg2));
7127 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007128
Bill Wendling5626c662011-10-06 22:53:00 +00007129 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7130 .addMBB(TrapBB)
7131 .addImm(ARMCC::HI)
7132 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007133
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007134 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7135 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007136 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007137
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007138 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007139 AddDefaultCC(
7140 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007141 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7142 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007143 .addReg(NewVReg1)
7144 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7145
7146 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007147 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007148 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007149 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007150 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007151 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7152 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7153 .addFrameIndex(FI)
7154 .addImm(1)
7155 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007156
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007157 if (NumLPads < 256) {
7158 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7159 .addReg(NewVReg1)
7160 .addImm(NumLPads));
7161 } else {
7162 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007163 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7164 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7165
7166 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007167 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007168 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007169 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007170 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007171
7172 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7173 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7174 .addReg(VReg1, RegState::Define)
7175 .addConstantPoolIndex(Idx));
7176 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7177 .addReg(NewVReg1)
7178 .addReg(VReg1));
7179 }
7180
Bill Wendlingb3d46782011-10-06 23:37:36 +00007181 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7182 .addMBB(TrapBB)
7183 .addImm(ARMCC::HI)
7184 .addReg(ARM::CPSR);
7185
7186 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7187 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7188 .addReg(ARM::CPSR, RegState::Define)
7189 .addReg(NewVReg1)
7190 .addImm(2));
7191
7192 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007193 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007194 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007195
7196 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7197 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7198 .addReg(ARM::CPSR, RegState::Define)
7199 .addReg(NewVReg2, RegState::Kill)
7200 .addReg(NewVReg3));
7201
Alex Lorenze40c8a22015-08-11 23:09:45 +00007202 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7203 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007204
7205 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7206 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7207 .addReg(NewVReg4, RegState::Kill)
7208 .addImm(0)
7209 .addMemOperand(JTMMOLd));
7210
Chad Rosier96603432013-03-01 18:30:38 +00007211 unsigned NewVReg6 = NewVReg5;
7212 if (RelocM == Reloc::PIC_) {
7213 NewVReg6 = MRI->createVirtualRegister(TRC);
7214 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7215 .addReg(ARM::CPSR, RegState::Define)
7216 .addReg(NewVReg5, RegState::Kill)
7217 .addReg(NewVReg3));
7218 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007219
7220 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7221 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007222 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007223 } else {
7224 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7225 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7226 .addFrameIndex(FI)
7227 .addImm(4)
7228 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007229
Bill Wendling4969dcd2011-10-18 22:52:20 +00007230 if (NumLPads < 256) {
7231 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7232 .addReg(NewVReg1)
7233 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007234 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007235 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7236 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007237 .addImm(NumLPads & 0xFFFF));
7238
7239 unsigned VReg2 = VReg1;
7240 if ((NumLPads & 0xFFFF0000) != 0) {
7241 VReg2 = MRI->createVirtualRegister(TRC);
7242 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7243 .addReg(VReg1)
7244 .addImm(NumLPads >> 16));
7245 }
7246
Bill Wendling4969dcd2011-10-18 22:52:20 +00007247 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7248 .addReg(NewVReg1)
7249 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007250 } else {
7251 MachineConstantPool *ConstantPool = MF->getConstantPool();
7252 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7253 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7254
7255 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007256 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007257 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007258 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007259 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7260
7261 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7262 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7263 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007264 .addConstantPoolIndex(Idx)
7265 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007266 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7267 .addReg(NewVReg1)
7268 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007269 }
7270
Bill Wendling5626c662011-10-06 22:53:00 +00007271 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7272 .addMBB(TrapBB)
7273 .addImm(ARMCC::HI)
7274 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007275
Bill Wendling973c8172011-10-18 22:11:18 +00007276 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007277 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007278 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007279 .addReg(NewVReg1)
7280 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007281 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7282 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007283 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007284
Alex Lorenze40c8a22015-08-11 23:09:45 +00007285 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7286 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007287 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007288 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007289 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7290 .addReg(NewVReg3, RegState::Kill)
7291 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007292 .addImm(0)
7293 .addMemOperand(JTMMOLd));
7294
Chad Rosier96603432013-03-01 18:30:38 +00007295 if (RelocM == Reloc::PIC_) {
7296 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7297 .addReg(NewVReg5, RegState::Kill)
7298 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007299 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007300 } else {
7301 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7302 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007303 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007304 }
Bill Wendling5626c662011-10-06 22:53:00 +00007305 }
Bill Wendling202803e2011-10-05 00:02:33 +00007306
Bill Wendling324be982011-10-05 00:39:32 +00007307 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007308 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007309 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007310 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7311 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007312 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007313 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007314 }
7315
Bill Wendling26d27802011-10-17 05:25:09 +00007316 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007317 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007318 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007319 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007320
7321 // Remove the landing pad successor from the invoke block and replace it
7322 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007323 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7324 BB->succ_end());
7325 while (!Successors.empty()) {
7326 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007327 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007328 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007329 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007330 }
7331 }
7332
7333 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007334
7335 // Find the invoke call and mark all of the callee-saved registers as
7336 // 'implicit defined' so that they're spilled. This prevents code from
7337 // moving instructions to before the EH block, where they will never be
7338 // executed.
7339 for (MachineBasicBlock::reverse_iterator
7340 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007341 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007342
7343 DenseMap<unsigned, bool> DefRegs;
7344 for (MachineInstr::mop_iterator
7345 OI = II->operands_begin(), OE = II->operands_end();
7346 OI != OE; ++OI) {
7347 if (!OI->isReg()) continue;
7348 DefRegs[OI->getReg()] = true;
7349 }
7350
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007351 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007352
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007353 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007354 unsigned Reg = SavedRegs[i];
7355 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007356 !ARM::tGPRRegClass.contains(Reg) &&
7357 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007358 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007359 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007360 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007361 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007362 continue;
7363 if (!DefRegs[Reg])
7364 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007365 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007366
7367 break;
7368 }
Bill Wendling883ec972011-10-07 23:18:02 +00007369 }
Bill Wendling324be982011-10-05 00:39:32 +00007370
Bill Wendling617075f2011-10-18 18:30:49 +00007371 // Mark all former landing pads as non-landing pads. The dispatch is the only
7372 // landing pad now.
7373 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7374 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007375 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007376
Bill Wendling324be982011-10-05 00:39:32 +00007377 // The instruction is gone now.
7378 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007379}
7380
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007381static
7382MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7383 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7384 E = MBB->succ_end(); I != E; ++I)
7385 if (*I != Succ)
7386 return *I;
7387 llvm_unreachable("Expecting a BB with two successors!");
7388}
7389
Manman Renb504f492013-10-29 22:27:32 +00007390/// Return the load opcode for a given load size. If load size >= 8,
7391/// neon opcode will be returned.
7392static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7393 if (LdSize >= 8)
7394 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7395 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7396 if (IsThumb1)
7397 return LdSize == 4 ? ARM::tLDRi
7398 : LdSize == 2 ? ARM::tLDRHi
7399 : LdSize == 1 ? ARM::tLDRBi : 0;
7400 if (IsThumb2)
7401 return LdSize == 4 ? ARM::t2LDR_POST
7402 : LdSize == 2 ? ARM::t2LDRH_POST
7403 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7404 return LdSize == 4 ? ARM::LDR_POST_IMM
7405 : LdSize == 2 ? ARM::LDRH_POST
7406 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7407}
7408
7409/// Return the store opcode for a given store size. If store size >= 8,
7410/// neon opcode will be returned.
7411static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7412 if (StSize >= 8)
7413 return StSize == 16 ? ARM::VST1q32wb_fixed
7414 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7415 if (IsThumb1)
7416 return StSize == 4 ? ARM::tSTRi
7417 : StSize == 2 ? ARM::tSTRHi
7418 : StSize == 1 ? ARM::tSTRBi : 0;
7419 if (IsThumb2)
7420 return StSize == 4 ? ARM::t2STR_POST
7421 : StSize == 2 ? ARM::t2STRH_POST
7422 : StSize == 1 ? ARM::t2STRB_POST : 0;
7423 return StSize == 4 ? ARM::STR_POST_IMM
7424 : StSize == 2 ? ARM::STRH_POST
7425 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7426}
7427
7428/// Emit a post-increment load operation with given size. The instructions
7429/// will be added to BB at Pos.
7430static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7431 const TargetInstrInfo *TII, DebugLoc dl,
7432 unsigned LdSize, unsigned Data, unsigned AddrIn,
7433 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7434 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7435 assert(LdOpc != 0 && "Should have a load opcode");
7436 if (LdSize >= 8) {
7437 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7438 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7439 .addImm(0));
7440 } else if (IsThumb1) {
7441 // load + update AddrIn
7442 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7443 .addReg(AddrIn).addImm(0));
7444 MachineInstrBuilder MIB =
7445 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7446 MIB = AddDefaultT1CC(MIB);
7447 MIB.addReg(AddrIn).addImm(LdSize);
7448 AddDefaultPred(MIB);
7449 } else if (IsThumb2) {
7450 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7451 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7452 .addImm(LdSize));
7453 } else { // arm
7454 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7455 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7456 .addReg(0).addImm(LdSize));
7457 }
7458}
7459
7460/// Emit a post-increment store operation with given size. The instructions
7461/// will be added to BB at Pos.
7462static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7463 const TargetInstrInfo *TII, DebugLoc dl,
7464 unsigned StSize, unsigned Data, unsigned AddrIn,
7465 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7466 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7467 assert(StOpc != 0 && "Should have a store opcode");
7468 if (StSize >= 8) {
7469 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7470 .addReg(AddrIn).addImm(0).addReg(Data));
7471 } else if (IsThumb1) {
7472 // store + update AddrIn
7473 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7474 .addReg(AddrIn).addImm(0));
7475 MachineInstrBuilder MIB =
7476 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7477 MIB = AddDefaultT1CC(MIB);
7478 MIB.addReg(AddrIn).addImm(StSize);
7479 AddDefaultPred(MIB);
7480 } else if (IsThumb2) {
7481 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7482 .addReg(Data).addReg(AddrIn).addImm(StSize));
7483 } else { // arm
7484 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7485 .addReg(Data).addReg(AddrIn).addReg(0)
7486 .addImm(StSize));
7487 }
7488}
7489
David Peixottoc32e24a2013-10-17 19:49:22 +00007490MachineBasicBlock *
7491ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7492 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007493 // This pseudo instruction has 3 operands: dst, src, size
7494 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7495 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007496 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007497 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7498 MachineFunction::iterator It = BB;
7499 ++It;
7500
7501 unsigned dest = MI->getOperand(0).getReg();
7502 unsigned src = MI->getOperand(1).getReg();
7503 unsigned SizeVal = MI->getOperand(2).getImm();
7504 unsigned Align = MI->getOperand(3).getImm();
7505 DebugLoc dl = MI->getDebugLoc();
7506
Manman Rene8735522012-06-01 19:33:18 +00007507 MachineFunction *MF = BB->getParent();
7508 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007509 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007510 const TargetRegisterClass *TRC = nullptr;
7511 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007512
7513 bool IsThumb1 = Subtarget->isThumb1Only();
7514 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007515
7516 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007517 UnitSize = 1;
7518 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007519 UnitSize = 2;
7520 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007521 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007522 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007523 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007524 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007525 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007526 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007527 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007528 }
7529 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007530 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007531 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007532 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007533
David Peixottob0653e532013-10-24 16:39:36 +00007534 // Select the correct opcode and register class for unit size load/store
7535 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007536 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007537 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007538 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7539 : UnitSize == 8 ? &ARM::DPRRegClass
7540 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007541
Manman Rene8735522012-06-01 19:33:18 +00007542 unsigned BytesLeft = SizeVal % UnitSize;
7543 unsigned LoopSize = SizeVal - BytesLeft;
7544
7545 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7546 // Use LDR and STR to copy.
7547 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7548 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7549 unsigned srcIn = src;
7550 unsigned destIn = dest;
7551 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007552 unsigned srcOut = MRI.createVirtualRegister(TRC);
7553 unsigned destOut = MRI.createVirtualRegister(TRC);
7554 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007555 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7556 IsThumb1, IsThumb2);
7557 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7558 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007559 srcIn = srcOut;
7560 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007561 }
7562
7563 // Handle the leftover bytes with LDRB and STRB.
7564 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7565 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007566 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007567 unsigned srcOut = MRI.createVirtualRegister(TRC);
7568 unsigned destOut = MRI.createVirtualRegister(TRC);
7569 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007570 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7571 IsThumb1, IsThumb2);
7572 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7573 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007574 srcIn = srcOut;
7575 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007576 }
7577 MI->eraseFromParent(); // The instruction is gone now.
7578 return BB;
7579 }
7580
7581 // Expand the pseudo op to a loop.
7582 // thisMBB:
7583 // ...
7584 // movw varEnd, # --> with thumb2
7585 // movt varEnd, #
7586 // ldrcp varEnd, idx --> without thumb2
7587 // fallthrough --> loopMBB
7588 // loopMBB:
7589 // PHI varPhi, varEnd, varLoop
7590 // PHI srcPhi, src, srcLoop
7591 // PHI destPhi, dst, destLoop
7592 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7593 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7594 // subs varLoop, varPhi, #UnitSize
7595 // bne loopMBB
7596 // fallthrough --> exitMBB
7597 // exitMBB:
7598 // epilogue to handle left-over bytes
7599 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7600 // [destOut] = STRB_POST(scratch, destLoop, 1)
7601 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7602 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7603 MF->insert(It, loopMBB);
7604 MF->insert(It, exitMBB);
7605
7606 // Transfer the remainder of BB and its successor edges to exitMBB.
7607 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007608 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007609 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7610
7611 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007612 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007613 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007614 unsigned Vtmp = varEnd;
7615 if ((LoopSize & 0xFFFF0000) != 0)
7616 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007617 AddDefaultPred(BuildMI(BB, dl,
7618 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7619 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007620
7621 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007622 AddDefaultPred(BuildMI(BB, dl,
7623 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7624 varEnd)
7625 .addReg(Vtmp)
7626 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007627 } else {
7628 MachineConstantPool *ConstantPool = MF->getConstantPool();
7629 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7630 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7631
7632 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007633 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007634 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007635 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007636 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7637
7638 if (IsThumb1)
7639 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7640 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7641 else
7642 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7643 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7644 }
Manman Rene8735522012-06-01 19:33:18 +00007645 BB->addSuccessor(loopMBB);
7646
7647 // Generate the loop body:
7648 // varPhi = PHI(varLoop, varEnd)
7649 // srcPhi = PHI(srcLoop, src)
7650 // destPhi = PHI(destLoop, dst)
7651 MachineBasicBlock *entryBB = BB;
7652 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007653 unsigned varLoop = MRI.createVirtualRegister(TRC);
7654 unsigned varPhi = MRI.createVirtualRegister(TRC);
7655 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7656 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7657 unsigned destLoop = MRI.createVirtualRegister(TRC);
7658 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007659
7660 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7661 .addReg(varLoop).addMBB(loopMBB)
7662 .addReg(varEnd).addMBB(entryBB);
7663 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7664 .addReg(srcLoop).addMBB(loopMBB)
7665 .addReg(src).addMBB(entryBB);
7666 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7667 .addReg(destLoop).addMBB(loopMBB)
7668 .addReg(dest).addMBB(entryBB);
7669
7670 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7671 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007672 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007673 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7674 IsThumb1, IsThumb2);
7675 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7676 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007677
7678 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007679 if (IsThumb1) {
7680 MachineInstrBuilder MIB =
7681 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7682 MIB = AddDefaultT1CC(MIB);
7683 MIB.addReg(varPhi).addImm(UnitSize);
7684 AddDefaultPred(MIB);
7685 } else {
7686 MachineInstrBuilder MIB =
7687 BuildMI(*BB, BB->end(), dl,
7688 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7689 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7690 MIB->getOperand(5).setReg(ARM::CPSR);
7691 MIB->getOperand(5).setIsDef(true);
7692 }
7693 BuildMI(*BB, BB->end(), dl,
7694 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7695 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007696
7697 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7698 BB->addSuccessor(loopMBB);
7699 BB->addSuccessor(exitMBB);
7700
7701 // Add epilogue to handle BytesLeft.
7702 BB = exitMBB;
7703 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007704
7705 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7706 // [destOut] = STRB_POST(scratch, destLoop, 1)
7707 unsigned srcIn = srcLoop;
7708 unsigned destIn = destLoop;
7709 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007710 unsigned srcOut = MRI.createVirtualRegister(TRC);
7711 unsigned destOut = MRI.createVirtualRegister(TRC);
7712 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007713 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7714 IsThumb1, IsThumb2);
7715 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7716 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007717 srcIn = srcOut;
7718 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007719 }
7720
7721 MI->eraseFromParent(); // The instruction is gone now.
7722 return BB;
7723}
7724
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007725MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007726ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7727 MachineBasicBlock *MBB) const {
7728 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007729 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007730 DebugLoc DL = MI->getDebugLoc();
7731
7732 assert(Subtarget->isTargetWindows() &&
7733 "__chkstk is only supported on Windows");
7734 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7735
7736 // __chkstk takes the number of words to allocate on the stack in R4, and
7737 // returns the stack adjustment in number of bytes in R4. This will not
7738 // clober any other registers (other than the obvious lr).
7739 //
7740 // Although, technically, IP should be considered a register which may be
7741 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7742 // thumb-2 environment, so there is no interworking required. As a result, we
7743 // do not expect a veneer to be emitted by the linker, clobbering IP.
7744 //
Alp Toker1d099d92014-06-19 19:41:26 +00007745 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007746 // required, again, ensuring that IP is not clobbered.
7747 //
7748 // Finally, although some linkers may theoretically provide a trampoline for
7749 // out of range calls (which is quite common due to a 32M range limitation of
7750 // branches for Thumb), we can generate the long-call version via
7751 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7752 // IP.
7753
7754 switch (TM.getCodeModel()) {
7755 case CodeModel::Small:
7756 case CodeModel::Medium:
7757 case CodeModel::Default:
7758 case CodeModel::Kernel:
7759 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7760 .addImm((unsigned)ARMCC::AL).addReg(0)
7761 .addExternalSymbol("__chkstk")
7762 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7763 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7764 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7765 break;
7766 case CodeModel::Large:
7767 case CodeModel::JITDefault: {
7768 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7769 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7770
7771 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7772 .addExternalSymbol("__chkstk");
7773 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7774 .addImm((unsigned)ARMCC::AL).addReg(0)
7775 .addReg(Reg, RegState::Kill)
7776 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7777 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7778 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7779 break;
7780 }
7781 }
7782
7783 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7784 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007785 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007786
7787 MI->eraseFromParent();
7788 return MBB;
7789}
7790
7791MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007792ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
7793 MachineBasicBlock *MBB) const {
7794 DebugLoc DL = MI->getDebugLoc();
7795 MachineFunction *MF = MBB->getParent();
7796 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
7797
7798 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
7799 MF->push_back(ContBB);
7800 ContBB->splice(ContBB->begin(), MBB,
7801 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
7802 MBB->addSuccessor(ContBB);
7803
7804 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
7805 MF->push_back(TrapBB);
7806 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
7807 MBB->addSuccessor(TrapBB);
7808
7809 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
7810 .addReg(MI->getOperand(0).getReg())
7811 .addMBB(TrapBB);
7812
7813 MI->eraseFromParent();
7814 return ContBB;
7815}
7816
7817MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007818ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007819 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007820 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007821 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007822 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007823 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007824 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007825 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007826 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007827 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007828 // The Thumb2 pre-indexed stores have the same MI operands, they just
7829 // define them differently in the .td files from the isel patterns, so
7830 // they need pseudos.
7831 case ARM::t2STR_preidx:
7832 MI->setDesc(TII->get(ARM::t2STR_PRE));
7833 return BB;
7834 case ARM::t2STRB_preidx:
7835 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7836 return BB;
7837 case ARM::t2STRH_preidx:
7838 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7839 return BB;
7840
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007841 case ARM::STRi_preidx:
7842 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007843 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007844 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7845 // Decode the offset.
7846 unsigned Offset = MI->getOperand(4).getImm();
7847 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7848 Offset = ARM_AM::getAM2Offset(Offset);
7849 if (isSub)
7850 Offset = -Offset;
7851
Jim Grosbachf402f692011-08-12 21:02:34 +00007852 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007853 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007854 .addOperand(MI->getOperand(0)) // Rn_wb
7855 .addOperand(MI->getOperand(1)) // Rt
7856 .addOperand(MI->getOperand(2)) // Rn
7857 .addImm(Offset) // offset (skip GPR==zero_reg)
7858 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007859 .addOperand(MI->getOperand(6))
7860 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007861 MI->eraseFromParent();
7862 return BB;
7863 }
7864 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007865 case ARM::STRBr_preidx:
7866 case ARM::STRH_preidx: {
7867 unsigned NewOpc;
7868 switch (MI->getOpcode()) {
7869 default: llvm_unreachable("unexpected opcode!");
7870 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7871 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7872 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7873 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007874 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7875 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7876 MIB.addOperand(MI->getOperand(i));
7877 MI->eraseFromParent();
7878 return BB;
7879 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007880
Evan Chengbb2af352009-08-12 05:17:19 +00007881 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007882 // To "insert" a SELECT_CC instruction, we actually have to insert the
7883 // diamond control-flow pattern. The incoming instruction knows the
7884 // destination vreg to set, the condition code register to branch on, the
7885 // true/false values to select between, and a branch opcode to use.
7886 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007887 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007888 ++It;
7889
7890 // thisMBB:
7891 // ...
7892 // TrueVal = ...
7893 // cmpTY ccX, r1, r2
7894 // bCC copy1MBB
7895 // fallthrough --> copy0MBB
7896 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007897 MachineFunction *F = BB->getParent();
7898 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7899 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007900 F->insert(It, copy0MBB);
7901 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007902
7903 // Transfer the remainder of BB and its successor edges to sinkMBB.
7904 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007905 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007906 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7907
Dan Gohmanf4f04102010-07-06 15:49:48 +00007908 BB->addSuccessor(copy0MBB);
7909 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007910
Dan Gohman34396292010-07-06 20:24:04 +00007911 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7912 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7913
Evan Cheng10043e22007-01-19 07:51:42 +00007914 // copy0MBB:
7915 // %FalseValue = ...
7916 // # fallthrough to sinkMBB
7917 BB = copy0MBB;
7918
7919 // Update machine-CFG edges
7920 BB->addSuccessor(sinkMBB);
7921
7922 // sinkMBB:
7923 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7924 // ...
7925 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007926 BuildMI(*BB, BB->begin(), dl,
7927 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007928 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7929 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7930
Dan Gohman34396292010-07-06 20:24:04 +00007931 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007932 return BB;
7933 }
Evan Chengb972e562009-08-07 00:34:42 +00007934
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007935 case ARM::BCCi64:
7936 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007937 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007938 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007939
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007940 // Compare both parts that make up the double comparison separately for
7941 // equality.
7942 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7943
7944 unsigned LHS1 = MI->getOperand(1).getReg();
7945 unsigned LHS2 = MI->getOperand(2).getReg();
7946 if (RHSisZero) {
7947 AddDefaultPred(BuildMI(BB, dl,
7948 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7949 .addReg(LHS1).addImm(0));
7950 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7951 .addReg(LHS2).addImm(0)
7952 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7953 } else {
7954 unsigned RHS1 = MI->getOperand(3).getReg();
7955 unsigned RHS2 = MI->getOperand(4).getReg();
7956 AddDefaultPred(BuildMI(BB, dl,
7957 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7958 .addReg(LHS1).addReg(RHS1));
7959 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7960 .addReg(LHS2).addReg(RHS2)
7961 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7962 }
7963
7964 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7965 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7966 if (MI->getOperand(0).getImm() == ARMCC::NE)
7967 std::swap(destMBB, exitMBB);
7968
7969 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7970 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007971 if (isThumb2)
7972 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7973 else
7974 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007975
7976 MI->eraseFromParent(); // The pseudo instruction is gone now.
7977 return BB;
7978 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007979
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007980 case ARM::Int_eh_sjlj_setjmp:
7981 case ARM::Int_eh_sjlj_setjmp_nofp:
7982 case ARM::tInt_eh_sjlj_setjmp:
7983 case ARM::t2Int_eh_sjlj_setjmp:
7984 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00007985 return BB;
7986
7987 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007988 EmitSjLjDispatchBlock(MI, BB);
7989 return BB;
7990
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007991 case ARM::ABS:
7992 case ARM::t2ABS: {
7993 // To insert an ABS instruction, we have to insert the
7994 // diamond control-flow pattern. The incoming instruction knows the
7995 // source vreg to test against 0, the destination vreg to set,
7996 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007997 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007998 // It transforms
7999 // V1 = ABS V0
8000 // into
8001 // V2 = MOVS V0
8002 // BCC (branch to SinkBB if V0 >= 0)
8003 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008004 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008005 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8006 MachineFunction::iterator BBI = BB;
8007 ++BBI;
8008 MachineFunction *Fn = BB->getParent();
8009 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8010 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8011 Fn->insert(BBI, RSBBB);
8012 Fn->insert(BBI, SinkBB);
8013
8014 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8015 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008016 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008017 bool isThumb2 = Subtarget->isThumb2();
8018 MachineRegisterInfo &MRI = Fn->getRegInfo();
8019 // In Thumb mode S must not be specified if source register is the SP or
8020 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008021 unsigned NewRsbDstReg =
8022 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008023
8024 // Transfer the remainder of BB and its successor edges to sinkMBB.
8025 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008026 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008027 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8028
8029 BB->addSuccessor(RSBBB);
8030 BB->addSuccessor(SinkBB);
8031
8032 // fall through to SinkMBB
8033 RSBBB->addSuccessor(SinkBB);
8034
Manman Rene0763c72012-06-15 21:32:12 +00008035 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008036 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008037 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8038 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008039
8040 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008041 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008042 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8043 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8044
8045 // insert rsbri in RSBBB
8046 // Note: BCC and rsbri will be converted into predicated rsbmi
8047 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008048 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008049 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008050 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008051 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8052
Andrew Trick3f07c422011-10-18 18:40:53 +00008053 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008054 // reuse ABSDstReg to not change uses of ABS instruction
8055 BuildMI(*SinkBB, SinkBB->begin(), dl,
8056 TII->get(ARM::PHI), ABSDstReg)
8057 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008058 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008059
8060 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008061 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008062
8063 // return last added BB
8064 return SinkBB;
8065 }
Manman Rene8735522012-06-01 19:33:18 +00008066 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008067 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008068 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008069 case ARM::WIN__CHKSTK:
8070 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008071 case ARM::WIN__DBZCHK:
8072 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008073 }
8074}
8075
Scott Douglass953f9082015-10-05 14:49:54 +00008076/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8077/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8078/// instead of as a custom inserter because we need the use list from the SDNode.
8079static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8080 MachineInstr *MI, const SDNode *Node) {
8081 bool isThumb1 = Subtarget->isThumb1Only();
8082
8083 DebugLoc DL = MI->getDebugLoc();
8084 MachineFunction *MF = MI->getParent()->getParent();
8085 MachineRegisterInfo &MRI = MF->getRegInfo();
8086 MachineInstrBuilder MIB(*MF, MI);
8087
8088 // If the new dst/src is unused mark it as dead.
8089 if (!Node->hasAnyUseOfValue(0)) {
8090 MI->getOperand(0).setIsDead(true);
8091 }
8092 if (!Node->hasAnyUseOfValue(1)) {
8093 MI->getOperand(1).setIsDead(true);
8094 }
8095
8096 // The MEMCPY both defines and kills the scratch registers.
8097 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8098 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8099 : &ARM::GPRRegClass);
8100 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8101 }
8102}
8103
Evan Chenge6fba772011-08-30 19:09:48 +00008104void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8105 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008106 if (MI->getOpcode() == ARM::MEMCPY) {
8107 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8108 return;
8109 }
8110
Evan Cheng7f8e5632011-12-07 07:15:52 +00008111 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008112 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8113 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8114 // operand is still set to noreg. If needed, set the optional operand's
8115 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008116 //
Andrew Trick88b24502011-10-18 19:18:52 +00008117 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008118
Andrew Trick924123a2011-09-21 02:20:46 +00008119 // Rename pseudo opcodes.
8120 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8121 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008122 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008123 MCID = &TII->get(NewOpc);
8124
8125 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8126 "converted opcode should be the same except for cc_out");
8127
8128 MI->setDesc(*MCID);
8129
8130 // Add the optional cc_out operand
8131 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008132 }
Andrew Trick88b24502011-10-18 19:18:52 +00008133 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008134
8135 // Any ARM instruction that sets the 's' bit should specify an optional
8136 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008137 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008138 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008139 return;
8140 }
Andrew Trick924123a2011-09-21 02:20:46 +00008141 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8142 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008143 bool definesCPSR = false;
8144 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008145 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008146 i != e; ++i) {
8147 const MachineOperand &MO = MI->getOperand(i);
8148 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8149 definesCPSR = true;
8150 if (MO.isDead())
8151 deadCPSR = true;
8152 MI->RemoveOperand(i);
8153 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008154 }
8155 }
Andrew Trick8586e622011-09-20 03:17:40 +00008156 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008157 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008158 return;
8159 }
8160 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008161 if (deadCPSR) {
8162 assert(!MI->getOperand(ccOutIdx).getReg() &&
8163 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008164 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008165 }
Andrew Trick8586e622011-09-20 03:17:40 +00008166
Andrew Trick924123a2011-09-21 02:20:46 +00008167 // If this instruction was defined with an optional CPSR def and its dag node
8168 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008169 MachineOperand &MO = MI->getOperand(ccOutIdx);
8170 MO.setReg(ARM::CPSR);
8171 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008172}
8173
Evan Cheng10043e22007-01-19 07:51:42 +00008174//===----------------------------------------------------------------------===//
8175// ARM Optimization Hooks
8176//===----------------------------------------------------------------------===//
8177
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008178// Helper function that checks if N is a null or all ones constant.
8179static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
8180 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
8181 if (!C)
8182 return false;
8183 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
8184}
8185
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008186// Return true if N is conditionally 0 or all ones.
8187// Detects these expressions where cc is an i1 value:
8188//
8189// (select cc 0, y) [AllOnes=0]
8190// (select cc y, 0) [AllOnes=0]
8191// (zext cc) [AllOnes=0]
8192// (sext cc) [AllOnes=0/1]
8193// (select cc -1, y) [AllOnes=1]
8194// (select cc y, -1) [AllOnes=1]
8195//
8196// Invert is set when N is the null/all ones constant when CC is false.
8197// OtherOp is set to the alternative value of N.
8198static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8199 SDValue &CC, bool &Invert,
8200 SDValue &OtherOp,
8201 SelectionDAG &DAG) {
8202 switch (N->getOpcode()) {
8203 default: return false;
8204 case ISD::SELECT: {
8205 CC = N->getOperand(0);
8206 SDValue N1 = N->getOperand(1);
8207 SDValue N2 = N->getOperand(2);
8208 if (isZeroOrAllOnes(N1, AllOnes)) {
8209 Invert = false;
8210 OtherOp = N2;
8211 return true;
8212 }
8213 if (isZeroOrAllOnes(N2, AllOnes)) {
8214 Invert = true;
8215 OtherOp = N1;
8216 return true;
8217 }
8218 return false;
8219 }
8220 case ISD::ZERO_EXTEND:
8221 // (zext cc) can never be the all ones value.
8222 if (AllOnes)
8223 return false;
8224 // Fall through.
8225 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008226 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008227 EVT VT = N->getValueType(0);
8228 CC = N->getOperand(0);
8229 if (CC.getValueType() != MVT::i1)
8230 return false;
8231 Invert = !AllOnes;
8232 if (AllOnes)
8233 // When looking for an AllOnes constant, N is an sext, and the 'other'
8234 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008235 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008236 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8237 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008238 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008239 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008240 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8241 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008242 return true;
8243 }
8244 }
8245}
8246
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008247// Combine a constant select operand into its use:
8248//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008249// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8250// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8251// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8252// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8253// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008254//
8255// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008256// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008257//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008258// Also recognize sext/zext from i1:
8259//
8260// (add (zext cc), x) -> (select cc (add x, 1), x)
8261// (add (sext cc), x) -> (select cc (add x, -1), x)
8262//
8263// These transformations eventually create predicated instructions.
8264//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008265// @param N The node to transform.
8266// @param Slct The N operand that is a select.
8267// @param OtherOp The other N operand (x above).
8268// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008269// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008270// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008271static
8272SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008273 TargetLowering::DAGCombinerInfo &DCI,
8274 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008275 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008276 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008277 SDValue NonConstantVal;
8278 SDValue CCOp;
8279 bool SwapSelectOps;
8280 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8281 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008282 return SDValue();
8283
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008284 // Slct is now know to be the desired identity constant when CC is true.
8285 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008286 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008287 OtherOp, NonConstantVal);
8288 // Unless SwapSelectOps says CC should be false.
8289 if (SwapSelectOps)
8290 std::swap(TrueVal, FalseVal);
8291
Andrew Trickef9de2a2013-05-25 02:42:55 +00008292 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008293 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008294}
8295
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008296// Attempt combineSelectAndUse on each operand of a commutative operator N.
8297static
8298SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8299 TargetLowering::DAGCombinerInfo &DCI) {
8300 SDValue N0 = N->getOperand(0);
8301 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008302 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008303 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8304 if (Result.getNode())
8305 return Result;
8306 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008307 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008308 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8309 if (Result.getNode())
8310 return Result;
8311 }
8312 return SDValue();
8313}
8314
Eric Christopher1b8b94192011-06-29 21:10:36 +00008315// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008316// (only after legalization).
8317static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8318 TargetLowering::DAGCombinerInfo &DCI,
8319 const ARMSubtarget *Subtarget) {
8320
8321 // Only perform optimization if after legalize, and if NEON is available. We
8322 // also expected both operands to be BUILD_VECTORs.
8323 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8324 || N0.getOpcode() != ISD::BUILD_VECTOR
8325 || N1.getOpcode() != ISD::BUILD_VECTOR)
8326 return SDValue();
8327
8328 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8329 EVT VT = N->getValueType(0);
8330 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8331 return SDValue();
8332
8333 // Check that the vector operands are of the right form.
8334 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8335 // operands, where N is the size of the formed vector.
8336 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8337 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008338
8339 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008340 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008341 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008342 SDValue Vec = N0->getOperand(0)->getOperand(0);
8343 SDNode *V = Vec.getNode();
8344 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008345
Eric Christopher1b8b94192011-06-29 21:10:36 +00008346 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008347 // check to see if each of their operands are an EXTRACT_VECTOR with
8348 // the same vector and appropriate index.
8349 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8350 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8351 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008352
Tanya Lattnere9e67052011-06-14 23:48:48 +00008353 SDValue ExtVec0 = N0->getOperand(i);
8354 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008355
Tanya Lattnere9e67052011-06-14 23:48:48 +00008356 // First operand is the vector, verify its the same.
8357 if (V != ExtVec0->getOperand(0).getNode() ||
8358 V != ExtVec1->getOperand(0).getNode())
8359 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008360
Tanya Lattnere9e67052011-06-14 23:48:48 +00008361 // Second is the constant, verify its correct.
8362 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8363 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008364
Tanya Lattnere9e67052011-06-14 23:48:48 +00008365 // For the constant, we want to see all the even or all the odd.
8366 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8367 || C1->getZExtValue() != nextIndex+1)
8368 return SDValue();
8369
8370 // Increment index.
8371 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008372 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008373 return SDValue();
8374 }
8375
8376 // Create VPADDL node.
8377 SelectionDAG &DAG = DCI.DAG;
8378 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008379
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008380 SDLoc dl(N);
8381
Tanya Lattnere9e67052011-06-14 23:48:48 +00008382 // Build operand list.
8383 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008384 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008385 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008386
8387 // Input is the vector.
8388 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008389
Tanya Lattnere9e67052011-06-14 23:48:48 +00008390 // Get widened type and narrowed type.
8391 MVT widenType;
8392 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008393
Silviu Barangaa3106e62014-04-03 10:44:27 +00008394 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8395 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008396 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8397 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8398 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8399 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008400 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008401 }
8402
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008403 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008404 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008405 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008406}
8407
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008408static SDValue findMUL_LOHI(SDValue V) {
8409 if (V->getOpcode() == ISD::UMUL_LOHI ||
8410 V->getOpcode() == ISD::SMUL_LOHI)
8411 return V;
8412 return SDValue();
8413}
8414
8415static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8416 TargetLowering::DAGCombinerInfo &DCI,
8417 const ARMSubtarget *Subtarget) {
8418
8419 if (Subtarget->isThumb1Only()) return SDValue();
8420
8421 // Only perform the checks after legalize when the pattern is available.
8422 if (DCI.isBeforeLegalize()) return SDValue();
8423
8424 // Look for multiply add opportunities.
8425 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8426 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8427 // a glue link from the first add to the second add.
8428 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8429 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008430 // UMUL_LOHI
8431 // / :lo \ :hi
8432 // / \ [no multiline comment]
8433 // loAdd -> ADDE |
8434 // \ :glue /
8435 // \ /
8436 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008437 //
8438 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8439 SDValue AddcOp0 = AddcNode->getOperand(0);
8440 SDValue AddcOp1 = AddcNode->getOperand(1);
8441
8442 // Check if the two operands are from the same mul_lohi node.
8443 if (AddcOp0.getNode() == AddcOp1.getNode())
8444 return SDValue();
8445
8446 assert(AddcNode->getNumValues() == 2 &&
8447 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008448 "Expect ADDC with two result values. First: i32");
8449
8450 // Check that we have a glued ADDC node.
8451 if (AddcNode->getValueType(1) != MVT::Glue)
8452 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008453
8454 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8455 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8456 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8457 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8458 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8459 return SDValue();
8460
8461 // Look for the glued ADDE.
8462 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008463 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008464 return SDValue();
8465
8466 // Make sure it is really an ADDE.
8467 if (AddeNode->getOpcode() != ISD::ADDE)
8468 return SDValue();
8469
8470 assert(AddeNode->getNumOperands() == 3 &&
8471 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8472 "ADDE node has the wrong inputs");
8473
8474 // Check for the triangle shape.
8475 SDValue AddeOp0 = AddeNode->getOperand(0);
8476 SDValue AddeOp1 = AddeNode->getOperand(1);
8477
8478 // Make sure that the ADDE operands are not coming from the same node.
8479 if (AddeOp0.getNode() == AddeOp1.getNode())
8480 return SDValue();
8481
8482 // Find the MUL_LOHI node walking up ADDE's operands.
8483 bool IsLeftOperandMUL = false;
8484 SDValue MULOp = findMUL_LOHI(AddeOp0);
8485 if (MULOp == SDValue())
8486 MULOp = findMUL_LOHI(AddeOp1);
8487 else
8488 IsLeftOperandMUL = true;
8489 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008490 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008491
8492 // Figure out the right opcode.
8493 unsigned Opc = MULOp->getOpcode();
8494 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8495
8496 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008497 SDValue* HiAdd = nullptr;
8498 SDValue* LoMul = nullptr;
8499 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008500
Jyoti Allurf1d70502015-01-23 09:10:03 +00008501 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8502 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8503 return SDValue();
8504
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008505 if (IsLeftOperandMUL)
8506 HiAdd = &AddeOp1;
8507 else
8508 HiAdd = &AddeOp0;
8509
8510
Jyoti Allurf1d70502015-01-23 09:10:03 +00008511 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8512 // whose low result is fed to the ADDC we are checking.
8513
8514 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008515 LoMul = &AddcOp0;
8516 LowAdd = &AddcOp1;
8517 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008518 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008519 LoMul = &AddcOp1;
8520 LowAdd = &AddcOp0;
8521 }
8522
Craig Topper062a2ba2014-04-25 05:30:21 +00008523 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008524 return SDValue();
8525
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008526 // Create the merged node.
8527 SelectionDAG &DAG = DCI.DAG;
8528
8529 // Build operand list.
8530 SmallVector<SDValue, 8> Ops;
8531 Ops.push_back(LoMul->getOperand(0));
8532 Ops.push_back(LoMul->getOperand(1));
8533 Ops.push_back(*LowAdd);
8534 Ops.push_back(*HiAdd);
8535
Andrew Trickef9de2a2013-05-25 02:42:55 +00008536 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008537 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008538
8539 // Replace the ADDs' nodes uses by the MLA node's values.
8540 SDValue HiMLALResult(MLALNode.getNode(), 1);
8541 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8542
8543 SDValue LoMLALResult(MLALNode.getNode(), 0);
8544 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8545
8546 // Return original node to notify the driver to stop replacing.
8547 SDValue resNode(AddcNode, 0);
8548 return resNode;
8549}
8550
8551/// PerformADDCCombine - Target-specific dag combine transform from
8552/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8553static SDValue PerformADDCCombine(SDNode *N,
8554 TargetLowering::DAGCombinerInfo &DCI,
8555 const ARMSubtarget *Subtarget) {
8556
8557 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8558
8559}
8560
Bob Wilson728eb292010-07-29 20:34:14 +00008561/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8562/// operands N0 and N1. This is a helper for PerformADDCombine that is
8563/// called with the default operands, and if that fails, with commuted
8564/// operands.
8565static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008566 TargetLowering::DAGCombinerInfo &DCI,
8567 const ARMSubtarget *Subtarget){
8568
8569 // Attempt to create vpaddl for this add.
8570 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8571 if (Result.getNode())
8572 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008573
Chris Lattner4147f082009-03-12 06:52:53 +00008574 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008575 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008576 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8577 if (Result.getNode()) return Result;
8578 }
Chris Lattner4147f082009-03-12 06:52:53 +00008579 return SDValue();
8580}
8581
Bob Wilson728eb292010-07-29 20:34:14 +00008582/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8583///
8584static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008585 TargetLowering::DAGCombinerInfo &DCI,
8586 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008587 SDValue N0 = N->getOperand(0);
8588 SDValue N1 = N->getOperand(1);
8589
8590 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008591 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008592 if (Result.getNode())
8593 return Result;
8594
8595 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008596 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008597}
8598
Chris Lattner4147f082009-03-12 06:52:53 +00008599/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008600///
Chris Lattner4147f082009-03-12 06:52:53 +00008601static SDValue PerformSUBCombine(SDNode *N,
8602 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008603 SDValue N0 = N->getOperand(0);
8604 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008605
Chris Lattner4147f082009-03-12 06:52:53 +00008606 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008607 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008608 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8609 if (Result.getNode()) return Result;
8610 }
Bob Wilson7117a912009-03-20 22:42:55 +00008611
Chris Lattner4147f082009-03-12 06:52:53 +00008612 return SDValue();
8613}
8614
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008615/// PerformVMULCombine
8616/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8617/// special multiplier accumulator forwarding.
8618/// vmul d3, d0, d2
8619/// vmla d3, d1, d2
8620/// is faster than
8621/// vadd d3, d0, d1
8622/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008623// However, for (A + B) * (A + B),
8624// vadd d2, d0, d1
8625// vmul d3, d0, d2
8626// vmla d3, d1, d2
8627// is slower than
8628// vadd d2, d0, d1
8629// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008630static SDValue PerformVMULCombine(SDNode *N,
8631 TargetLowering::DAGCombinerInfo &DCI,
8632 const ARMSubtarget *Subtarget) {
8633 if (!Subtarget->hasVMLxForwarding())
8634 return SDValue();
8635
8636 SelectionDAG &DAG = DCI.DAG;
8637 SDValue N0 = N->getOperand(0);
8638 SDValue N1 = N->getOperand(1);
8639 unsigned Opcode = N0.getOpcode();
8640 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8641 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008642 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008643 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8644 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8645 return SDValue();
8646 std::swap(N0, N1);
8647 }
8648
Weiming Zhao2052f482013-09-25 23:12:06 +00008649 if (N0 == N1)
8650 return SDValue();
8651
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008652 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008653 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008654 SDValue N00 = N0->getOperand(0);
8655 SDValue N01 = N0->getOperand(1);
8656 return DAG.getNode(Opcode, DL, VT,
8657 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8658 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8659}
8660
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008661static SDValue PerformMULCombine(SDNode *N,
8662 TargetLowering::DAGCombinerInfo &DCI,
8663 const ARMSubtarget *Subtarget) {
8664 SelectionDAG &DAG = DCI.DAG;
8665
8666 if (Subtarget->isThumb1Only())
8667 return SDValue();
8668
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008669 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8670 return SDValue();
8671
8672 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008673 if (VT.is64BitVector() || VT.is128BitVector())
8674 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008675 if (VT != MVT::i32)
8676 return SDValue();
8677
8678 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8679 if (!C)
8680 return SDValue();
8681
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008682 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008683 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008684
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008685 ShiftAmt = ShiftAmt & (32 - 1);
8686 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008687 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008688
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008689 SDValue Res;
8690 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008691
8692 if (MulAmt >= 0) {
8693 if (isPowerOf2_32(MulAmt - 1)) {
8694 // (mul x, 2^N + 1) => (add (shl x, N), x)
8695 Res = DAG.getNode(ISD::ADD, DL, VT,
8696 V,
8697 DAG.getNode(ISD::SHL, DL, VT,
8698 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008699 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008700 MVT::i32)));
8701 } else if (isPowerOf2_32(MulAmt + 1)) {
8702 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8703 Res = DAG.getNode(ISD::SUB, DL, VT,
8704 DAG.getNode(ISD::SHL, DL, VT,
8705 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008706 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008707 MVT::i32)),
8708 V);
8709 } else
8710 return SDValue();
8711 } else {
8712 uint64_t MulAmtAbs = -MulAmt;
8713 if (isPowerOf2_32(MulAmtAbs + 1)) {
8714 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8715 Res = DAG.getNode(ISD::SUB, DL, VT,
8716 V,
8717 DAG.getNode(ISD::SHL, DL, VT,
8718 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008719 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008720 MVT::i32)));
8721 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8722 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8723 Res = DAG.getNode(ISD::ADD, DL, VT,
8724 V,
8725 DAG.getNode(ISD::SHL, DL, VT,
8726 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008727 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008728 MVT::i32)));
8729 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008730 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008731
8732 } else
8733 return SDValue();
8734 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008735
8736 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008737 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008738 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008739
8740 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008741 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008742 return SDValue();
8743}
8744
Owen Anderson30c48922010-11-05 19:27:46 +00008745static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008746 TargetLowering::DAGCombinerInfo &DCI,
8747 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008748
Owen Anderson30c48922010-11-05 19:27:46 +00008749 // Attempt to use immediate-form VBIC
8750 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008751 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008752 EVT VT = N->getValueType(0);
8753 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008754
Tanya Lattner266792a2011-04-07 15:24:20 +00008755 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8756 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008757
Owen Anderson30c48922010-11-05 19:27:46 +00008758 APInt SplatBits, SplatUndef;
8759 unsigned SplatBitSize;
8760 bool HasAnyUndefs;
8761 if (BVN &&
8762 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8763 if (SplatBitSize <= 64) {
8764 EVT VbicVT;
8765 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8766 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008767 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008768 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008769 if (Val.getNode()) {
8770 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008771 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008772 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008773 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008774 }
8775 }
8776 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008777
Evan Chenge87681c2012-02-23 01:19:06 +00008778 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008779 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8780 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8781 if (Result.getNode())
8782 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008783 }
8784
Owen Anderson30c48922010-11-05 19:27:46 +00008785 return SDValue();
8786}
8787
Jim Grosbach11013ed2010-07-16 23:05:05 +00008788/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8789static SDValue PerformORCombine(SDNode *N,
8790 TargetLowering::DAGCombinerInfo &DCI,
8791 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008792 // Attempt to use immediate-form VORR
8793 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008794 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008795 EVT VT = N->getValueType(0);
8796 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008797
Tanya Lattner266792a2011-04-07 15:24:20 +00008798 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8799 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008800
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008801 APInt SplatBits, SplatUndef;
8802 unsigned SplatBitSize;
8803 bool HasAnyUndefs;
8804 if (BVN && Subtarget->hasNEON() &&
8805 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8806 if (SplatBitSize <= 64) {
8807 EVT VorrVT;
8808 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8809 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008810 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008811 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008812 if (Val.getNode()) {
8813 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008814 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008815 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008816 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008817 }
8818 }
8819 }
8820
Evan Chenge87681c2012-02-23 01:19:06 +00008821 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008822 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8823 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8824 if (Result.getNode())
8825 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008826 }
8827
Nadav Rotem3a94c542012-08-13 18:52:44 +00008828 // The code below optimizes (or (and X, Y), Z).
8829 // The AND operand needs to have a single user to make these optimizations
8830 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008831 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008832 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008833 return SDValue();
8834 SDValue N1 = N->getOperand(1);
8835
8836 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8837 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8838 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8839 APInt SplatUndef;
8840 unsigned SplatBitSize;
8841 bool HasAnyUndefs;
8842
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008843 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008844 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008845 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8846 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008847 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008848 HasAnyUndefs) && !HasAnyUndefs) {
8849 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8850 HasAnyUndefs) && !HasAnyUndefs) {
8851 // Ensure that the bit width of the constants are the same and that
8852 // the splat arguments are logical inverses as per the pattern we
8853 // are trying to simplify.
8854 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8855 SplatBits0 == ~SplatBits1) {
8856 // Canonicalize the vector type to make instruction selection
8857 // simpler.
8858 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8859 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8860 N0->getOperand(1),
8861 N0->getOperand(0),
8862 N1->getOperand(0));
8863 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8864 }
8865 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008866 }
8867 }
8868
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008869 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8870 // reasonable.
8871
Jim Grosbach11013ed2010-07-16 23:05:05 +00008872 // BFI is only available on V6T2+
8873 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8874 return SDValue();
8875
Andrew Trickef9de2a2013-05-25 02:42:55 +00008876 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008877 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008878 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008879 //
8880 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008881 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008882 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008883 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008884 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008885 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008886
Jim Grosbach11013ed2010-07-16 23:05:05 +00008887 if (VT != MVT::i32)
8888 return SDValue();
8889
Evan Cheng2e51bb42010-12-13 20:32:54 +00008890 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008891
Jim Grosbach11013ed2010-07-16 23:05:05 +00008892 // The value and the mask need to be constants so we can verify this is
8893 // actually a bitfield set. If the mask is 0xffff, we can do better
8894 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008895 SDValue MaskOp = N0.getOperand(1);
8896 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8897 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008898 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008899 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008900 if (Mask == 0xffff)
8901 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008902 SDValue Res;
8903 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008904 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8905 if (N1C) {
8906 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008907 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008908 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008909
Evan Cheng34345752010-12-11 04:11:38 +00008910 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008911 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008912
Evan Cheng2e51bb42010-12-13 20:32:54 +00008913 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008914 DAG.getConstant(Val, DL, MVT::i32),
8915 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008916
8917 // Do not add new nodes to DAG combiner worklist.
8918 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008919 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008920 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008921 } else if (N1.getOpcode() == ISD::AND) {
8922 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008923 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8924 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008925 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008926 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008927
Eric Christopherd5530962011-03-26 01:21:03 +00008928 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8929 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008930 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008931 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008932 // The pack halfword instruction works better for masks that fit it,
8933 // so use that when it's available.
8934 if (Subtarget->hasT2ExtractPack() &&
8935 (Mask == 0xffff || Mask == 0xffff0000))
8936 return SDValue();
8937 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008938 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008939 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008940 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008941 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008942 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008943 // Do not add new nodes to DAG combiner worklist.
8944 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008945 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008946 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008947 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008948 // The pack halfword instruction works better for masks that fit it,
8949 // so use that when it's available.
8950 if (Subtarget->hasT2ExtractPack() &&
8951 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8952 return SDValue();
8953 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008954 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008955 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008956 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008957 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008958 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008959 // Do not add new nodes to DAG combiner worklist.
8960 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008961 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008962 }
8963 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008964
Evan Cheng2e51bb42010-12-13 20:32:54 +00008965 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8966 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8967 ARM::isBitFieldInvertedMask(~Mask)) {
8968 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8969 // where lsb(mask) == #shamt and masked bits of B are known zero.
8970 SDValue ShAmt = N00.getOperand(1);
8971 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008972 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008973 if (ShAmtC != LSB)
8974 return SDValue();
8975
8976 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008977 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008978
8979 // Do not add new nodes to DAG combiner worklist.
8980 DCI.CombineTo(N, Res, false);
8981 }
8982
Jim Grosbach11013ed2010-07-16 23:05:05 +00008983 return SDValue();
8984}
8985
Evan Chenge87681c2012-02-23 01:19:06 +00008986static SDValue PerformXORCombine(SDNode *N,
8987 TargetLowering::DAGCombinerInfo &DCI,
8988 const ARMSubtarget *Subtarget) {
8989 EVT VT = N->getValueType(0);
8990 SelectionDAG &DAG = DCI.DAG;
8991
8992 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8993 return SDValue();
8994
8995 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008996 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8997 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8998 if (Result.getNode())
8999 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009000 }
9001
9002 return SDValue();
9003}
9004
Evan Cheng6d02d902011-06-15 01:12:31 +00009005/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9006/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009007static SDValue PerformBFICombine(SDNode *N,
9008 TargetLowering::DAGCombinerInfo &DCI) {
9009 SDValue N1 = N->getOperand(1);
9010 if (N1.getOpcode() == ISD::AND) {
9011 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9012 if (!N11C)
9013 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009014 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009015 unsigned LSB = countTrailingZeros(~InvMask);
9016 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009017 assert(Width <
9018 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009019 "undefined behavior");
9020 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009021 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009022 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009023 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009024 N->getOperand(0), N1.getOperand(0),
9025 N->getOperand(2));
9026 }
9027 return SDValue();
9028}
9029
Bob Wilson22806742010-09-22 22:09:21 +00009030/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9031/// ARMISD::VMOVRRD.
9032static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009033 TargetLowering::DAGCombinerInfo &DCI,
9034 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009035 // vmovrrd(vmovdrr x, y) -> x,y
9036 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009037 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009038 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009039
9040 // vmovrrd(load f64) -> (load i32), (load i32)
9041 SDNode *InNode = InDouble.getNode();
9042 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9043 InNode->getValueType(0) == MVT::f64 &&
9044 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9045 !cast<LoadSDNode>(InNode)->isVolatile()) {
9046 // TODO: Should this be done for non-FrameIndex operands?
9047 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9048
9049 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009050 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009051 SDValue BasePtr = LD->getBasePtr();
9052 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9053 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009054 LD->isNonTemporal(), LD->isInvariant(),
9055 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009056
9057 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009058 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009059 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9060 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009061 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009062 std::min(4U, LD->getAlignment() / 2));
9063
9064 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009065 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009066 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009067 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009068 return Result;
9069 }
9070
Bob Wilson22806742010-09-22 22:09:21 +00009071 return SDValue();
9072}
9073
9074/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9075/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9076static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9077 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9078 SDValue Op0 = N->getOperand(0);
9079 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009080 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009081 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009082 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009083 Op1 = Op1.getOperand(0);
9084 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9085 Op0.getNode() == Op1.getNode() &&
9086 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009087 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009088 N->getValueType(0), Op0.getOperand(0));
9089 return SDValue();
9090}
9091
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009092/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9093/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9094/// i64 vector to have f64 elements, since the value can then be loaded
9095/// directly into a VFP register.
9096static bool hasNormalLoadOperand(SDNode *N) {
9097 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9098 for (unsigned i = 0; i < NumElts; ++i) {
9099 SDNode *Elt = N->getOperand(i).getNode();
9100 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9101 return true;
9102 }
9103 return false;
9104}
9105
Bob Wilsoncb6db982010-09-17 22:59:05 +00009106/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9107/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009108static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009109 TargetLowering::DAGCombinerInfo &DCI,
9110 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009111 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9112 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9113 // into a pair of GPRs, which is fine when the value is used as a scalar,
9114 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009115 SelectionDAG &DAG = DCI.DAG;
9116 if (N->getNumOperands() == 2) {
9117 SDValue RV = PerformVMOVDRRCombine(N, DAG);
9118 if (RV.getNode())
9119 return RV;
9120 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00009121
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009122 // Load i64 elements as f64 values so that type legalization does not split
9123 // them up into i32 values.
9124 EVT VT = N->getValueType(0);
9125 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9126 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009127 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009128 SmallVector<SDValue, 8> Ops;
9129 unsigned NumElts = VT.getVectorNumElements();
9130 for (unsigned i = 0; i < NumElts; ++i) {
9131 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9132 Ops.push_back(V);
9133 // Make the DAGCombiner fold the bitcast.
9134 DCI.AddToWorklist(V.getNode());
9135 }
9136 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009137 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009138 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9139}
9140
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009141/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9142static SDValue
9143PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9144 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9145 // At that time, we may have inserted bitcasts from integer to float.
9146 // If these bitcasts have survived DAGCombine, change the lowering of this
9147 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9148 // force to use floating point types.
9149
9150 // Make sure we can change the type of the vector.
9151 // This is possible iff:
9152 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9153 // 1.1. Vector is used only once.
9154 // 1.2. Use is a bit convert to an integer type.
9155 // 2. The size of its operands are 32-bits (64-bits are not legal).
9156 EVT VT = N->getValueType(0);
9157 EVT EltVT = VT.getVectorElementType();
9158
9159 // Check 1.1. and 2.
9160 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9161 return SDValue();
9162
9163 // By construction, the input type must be float.
9164 assert(EltVT == MVT::f32 && "Unexpected type!");
9165
9166 // Check 1.2.
9167 SDNode *Use = *N->use_begin();
9168 if (Use->getOpcode() != ISD::BITCAST ||
9169 Use->getValueType(0).isFloatingPoint())
9170 return SDValue();
9171
9172 // Check profitability.
9173 // Model is, if more than half of the relevant operands are bitcast from
9174 // i32, turn the build_vector into a sequence of insert_vector_elt.
9175 // Relevant operands are everything that is not statically
9176 // (i.e., at compile time) bitcasted.
9177 unsigned NumOfBitCastedElts = 0;
9178 unsigned NumElts = VT.getVectorNumElements();
9179 unsigned NumOfRelevantElts = NumElts;
9180 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9181 SDValue Elt = N->getOperand(Idx);
9182 if (Elt->getOpcode() == ISD::BITCAST) {
9183 // Assume only bit cast to i32 will go away.
9184 if (Elt->getOperand(0).getValueType() == MVT::i32)
9185 ++NumOfBitCastedElts;
9186 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
9187 // Constants are statically casted, thus do not count them as
9188 // relevant operands.
9189 --NumOfRelevantElts;
9190 }
9191
9192 // Check if more than half of the elements require a non-free bitcast.
9193 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9194 return SDValue();
9195
9196 SelectionDAG &DAG = DCI.DAG;
9197 // Create the new vector type.
9198 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9199 // Check if the type is legal.
9200 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9201 if (!TLI.isTypeLegal(VecVT))
9202 return SDValue();
9203
9204 // Combine:
9205 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9206 // => BITCAST INSERT_VECTOR_ELT
9207 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9208 // (BITCAST EN), N.
9209 SDValue Vec = DAG.getUNDEF(VecVT);
9210 SDLoc dl(N);
9211 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9212 SDValue V = N->getOperand(Idx);
9213 if (V.getOpcode() == ISD::UNDEF)
9214 continue;
9215 if (V.getOpcode() == ISD::BITCAST &&
9216 V->getOperand(0).getValueType() == MVT::i32)
9217 // Fold obvious case.
9218 V = V.getOperand(0);
9219 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009220 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009221 // Make the DAGCombiner fold the bitcasts.
9222 DCI.AddToWorklist(V.getNode());
9223 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009224 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009225 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9226 }
9227 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9228 // Make the DAGCombiner fold the bitcasts.
9229 DCI.AddToWorklist(Vec.getNode());
9230 return Vec;
9231}
9232
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009233/// PerformInsertEltCombine - Target-specific dag combine xforms for
9234/// ISD::INSERT_VECTOR_ELT.
9235static SDValue PerformInsertEltCombine(SDNode *N,
9236 TargetLowering::DAGCombinerInfo &DCI) {
9237 // Bitcast an i64 load inserted into a vector to f64.
9238 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9239 EVT VT = N->getValueType(0);
9240 SDNode *Elt = N->getOperand(1).getNode();
9241 if (VT.getVectorElementType() != MVT::i64 ||
9242 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9243 return SDValue();
9244
9245 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009246 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009247 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9248 VT.getVectorNumElements());
9249 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9250 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9251 // Make the DAGCombiner fold the bitcasts.
9252 DCI.AddToWorklist(Vec.getNode());
9253 DCI.AddToWorklist(V.getNode());
9254 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9255 Vec, V, N->getOperand(2));
9256 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009257}
9258
Bob Wilsonc7334a12010-10-27 20:38:28 +00009259/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9260/// ISD::VECTOR_SHUFFLE.
9261static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9262 // The LLVM shufflevector instruction does not require the shuffle mask
9263 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9264 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9265 // operands do not match the mask length, they are extended by concatenating
9266 // them with undef vectors. That is probably the right thing for other
9267 // targets, but for NEON it is better to concatenate two double-register
9268 // size vector operands into a single quad-register size vector. Do that
9269 // transformation here:
9270 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9271 // shuffle(concat(v1, v2), undef)
9272 SDValue Op0 = N->getOperand(0);
9273 SDValue Op1 = N->getOperand(1);
9274 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9275 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9276 Op0.getNumOperands() != 2 ||
9277 Op1.getNumOperands() != 2)
9278 return SDValue();
9279 SDValue Concat0Op1 = Op0.getOperand(1);
9280 SDValue Concat1Op1 = Op1.getOperand(1);
9281 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9282 Concat1Op1.getOpcode() != ISD::UNDEF)
9283 return SDValue();
9284 // Skip the transformation if any of the types are illegal.
9285 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9286 EVT VT = N->getValueType(0);
9287 if (!TLI.isTypeLegal(VT) ||
9288 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9289 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9290 return SDValue();
9291
Andrew Trickef9de2a2013-05-25 02:42:55 +00009292 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009293 Op0.getOperand(0), Op1.getOperand(0));
9294 // Translate the shuffle mask.
9295 SmallVector<int, 16> NewMask;
9296 unsigned NumElts = VT.getVectorNumElements();
9297 unsigned HalfElts = NumElts/2;
9298 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9299 for (unsigned n = 0; n < NumElts; ++n) {
9300 int MaskElt = SVN->getMaskElt(n);
9301 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009302 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009303 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009304 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009305 NewElt = HalfElts + MaskElt - NumElts;
9306 NewMask.push_back(NewElt);
9307 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009308 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009309 DAG.getUNDEF(VT), NewMask.data());
9310}
9311
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009312/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9313/// NEON load/store intrinsics, and generic vector load/stores, to merge
9314/// base address updates.
9315/// For generic load/stores, the memory type is assumed to be a vector.
9316/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009317static SDValue CombineBaseUpdate(SDNode *N,
9318 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009319 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009320 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9321 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009322 const bool isStore = N->getOpcode() == ISD::STORE;
9323 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009324 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009325 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009326 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009327
9328 // Search for a use of the address operand that is an increment.
9329 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9330 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9331 SDNode *User = *UI;
9332 if (User->getOpcode() != ISD::ADD ||
9333 UI.getUse().getResNo() != Addr.getResNo())
9334 continue;
9335
9336 // Check that the add is independent of the load/store. Otherwise, folding
9337 // it would create a cycle.
9338 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9339 continue;
9340
9341 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009342 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009343 bool isLaneOp = false;
9344 unsigned NewOpc = 0;
9345 unsigned NumVecs = 0;
9346 if (isIntrinsic) {
9347 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9348 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009349 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009350 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9351 NumVecs = 1; break;
9352 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9353 NumVecs = 2; break;
9354 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9355 NumVecs = 3; break;
9356 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9357 NumVecs = 4; break;
9358 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9359 NumVecs = 2; isLaneOp = true; break;
9360 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9361 NumVecs = 3; isLaneOp = true; break;
9362 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9363 NumVecs = 4; isLaneOp = true; break;
9364 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009365 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009366 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009367 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009368 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009369 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009370 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009371 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009372 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009373 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009374 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009375 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009376 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009377 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009378 }
9379 } else {
9380 isLaneOp = true;
9381 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009382 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009383 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9384 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9385 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009386 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9387 NumVecs = 1; isLaneOp = false; break;
9388 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9389 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009390 }
9391 }
9392
9393 // Find the size of memory referenced by the load/store.
9394 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009395 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009396 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009397 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009398 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009399 } else {
9400 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9401 VecTy = N->getOperand(1).getValueType();
9402 }
9403
Bob Wilson06fce872011-02-07 17:43:21 +00009404 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9405 if (isLaneOp)
9406 NumBytes /= VecTy.getVectorNumElements();
9407
9408 // If the increment is a constant, it must match the memory ref size.
9409 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9410 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9411 uint64_t IncVal = CInc->getZExtValue();
9412 if (IncVal != NumBytes)
9413 continue;
9414 } else if (NumBytes >= 3 * 16) {
9415 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9416 // separate instructions that make it harder to use a non-constant update.
9417 continue;
9418 }
9419
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009420 // OK, we found an ADD we can fold into the base update.
9421 // Now, create a _UPD node, taking care of not breaking alignment.
9422
9423 EVT AlignedVecTy = VecTy;
9424 unsigned Alignment = MemN->getAlignment();
9425
9426 // If this is a less-than-standard-aligned load/store, change the type to
9427 // match the standard alignment.
9428 // The alignment is overlooked when selecting _UPD variants; and it's
9429 // easier to introduce bitcasts here than fix that.
9430 // There are 3 ways to get to this base-update combine:
9431 // - intrinsics: they are assumed to be properly aligned (to the standard
9432 // alignment of the memory type), so we don't need to do anything.
9433 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9434 // intrinsics, so, likewise, there's nothing to do.
9435 // - generic load/store instructions: the alignment is specified as an
9436 // explicit operand, rather than implicitly as the standard alignment
9437 // of the memory type (like the intrisics). We need to change the
9438 // memory type to match the explicit alignment. That way, we don't
9439 // generate non-standard-aligned ARMISD::VLDx nodes.
9440 if (isa<LSBaseSDNode>(N)) {
9441 if (Alignment == 0)
9442 Alignment = 1;
9443 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9444 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9445 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9446 assert(!isLaneOp && "Unexpected generic load/store lane.");
9447 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9448 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9449 }
9450 // Don't set an explicit alignment on regular load/stores that we want
9451 // to transform to VLD/VST 1_UPD nodes.
9452 // This matches the behavior of regular load/stores, which only get an
9453 // explicit alignment if the MMO alignment is larger than the standard
9454 // alignment of the memory type.
9455 // Intrinsics, however, always get an explicit alignment, set to the
9456 // alignment of the MMO.
9457 Alignment = 1;
9458 }
9459
Bob Wilson06fce872011-02-07 17:43:21 +00009460 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009461 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009462 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009463 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009464 unsigned n;
9465 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009466 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009467 Tys[n++] = MVT::i32;
9468 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009469 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009470
9471 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009472 SmallVector<SDValue, 8> Ops;
9473 Ops.push_back(N->getOperand(0)); // incoming chain
9474 Ops.push_back(N->getOperand(AddrOpIdx));
9475 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009476
9477 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9478 // Try to match the intrinsic's signature
9479 Ops.push_back(StN->getValue());
9480 } else {
9481 // Loads (and of course intrinsics) match the intrinsics' signature,
9482 // so just add all but the alignment operand.
9483 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9484 Ops.push_back(N->getOperand(i));
9485 }
9486
9487 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009488 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009489
9490 // If this is a non-standard-aligned STORE, the penultimate operand is the
9491 // stored value. Bitcast it to the aligned type.
9492 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9493 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009494 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009495 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009496
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009497 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009498 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009499 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009500
9501 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009502 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009503 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009504 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009505
9506 // If this is an non-standard-aligned LOAD, the first result is the loaded
9507 // value. Bitcast it to the expected result type.
9508 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9509 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009510 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009511 }
9512
Bob Wilson06fce872011-02-07 17:43:21 +00009513 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9514 DCI.CombineTo(N, NewResults);
9515 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9516
9517 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009518 }
Bob Wilson06fce872011-02-07 17:43:21 +00009519 return SDValue();
9520}
9521
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009522static SDValue PerformVLDCombine(SDNode *N,
9523 TargetLowering::DAGCombinerInfo &DCI) {
9524 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9525 return SDValue();
9526
9527 return CombineBaseUpdate(N, DCI);
9528}
9529
Bob Wilson2d790df2010-11-28 06:51:26 +00009530/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9531/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9532/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9533/// return true.
9534static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9535 SelectionDAG &DAG = DCI.DAG;
9536 EVT VT = N->getValueType(0);
9537 // vldN-dup instructions only support 64-bit vectors for N > 1.
9538 if (!VT.is64BitVector())
9539 return false;
9540
9541 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9542 SDNode *VLD = N->getOperand(0).getNode();
9543 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9544 return false;
9545 unsigned NumVecs = 0;
9546 unsigned NewOpc = 0;
9547 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9548 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9549 NumVecs = 2;
9550 NewOpc = ARMISD::VLD2DUP;
9551 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9552 NumVecs = 3;
9553 NewOpc = ARMISD::VLD3DUP;
9554 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9555 NumVecs = 4;
9556 NewOpc = ARMISD::VLD4DUP;
9557 } else {
9558 return false;
9559 }
9560
9561 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9562 // numbers match the load.
9563 unsigned VLDLaneNo =
9564 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9565 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9566 UI != UE; ++UI) {
9567 // Ignore uses of the chain result.
9568 if (UI.getUse().getResNo() == NumVecs)
9569 continue;
9570 SDNode *User = *UI;
9571 if (User->getOpcode() != ARMISD::VDUPLANE ||
9572 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9573 return false;
9574 }
9575
9576 // Create the vldN-dup node.
9577 EVT Tys[5];
9578 unsigned n;
9579 for (n = 0; n < NumVecs; ++n)
9580 Tys[n] = VT;
9581 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009582 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009583 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9584 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009585 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009586 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009587 VLDMemInt->getMemOperand());
9588
9589 // Update the uses.
9590 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9591 UI != UE; ++UI) {
9592 unsigned ResNo = UI.getUse().getResNo();
9593 // Ignore uses of the chain result.
9594 if (ResNo == NumVecs)
9595 continue;
9596 SDNode *User = *UI;
9597 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9598 }
9599
9600 // Now the vldN-lane intrinsic is dead except for its chain result.
9601 // Update uses of the chain.
9602 std::vector<SDValue> VLDDupResults;
9603 for (unsigned n = 0; n < NumVecs; ++n)
9604 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9605 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9606 DCI.CombineTo(VLD, VLDDupResults);
9607
9608 return true;
9609}
9610
Bob Wilson103a0dc2010-07-14 01:22:12 +00009611/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9612/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009613static SDValue PerformVDUPLANECombine(SDNode *N,
9614 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009615 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009616
Bob Wilson2d790df2010-11-28 06:51:26 +00009617 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9618 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9619 if (CombineVLDDUP(N, DCI))
9620 return SDValue(N, 0);
9621
9622 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9623 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009624 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009625 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009626 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009627 return SDValue();
9628
9629 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9630 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9631 // The canonical VMOV for a zero vector uses a 32-bit element size.
9632 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9633 unsigned EltBits;
9634 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9635 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009636 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009637 if (EltSize > VT.getVectorElementType().getSizeInBits())
9638 return SDValue();
9639
Andrew Trickef9de2a2013-05-25 02:42:55 +00009640 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009641}
9642
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009643static SDValue PerformLOADCombine(SDNode *N,
9644 TargetLowering::DAGCombinerInfo &DCI) {
9645 EVT VT = N->getValueType(0);
9646
9647 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9648 if (ISD::isNormalLoad(N) && VT.isVector() &&
9649 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9650 return CombineBaseUpdate(N, DCI);
9651
9652 return SDValue();
9653}
9654
Ahmed Bougacha23167462014-12-09 21:26:53 +00009655/// PerformSTORECombine - Target-specific dag combine xforms for
9656/// ISD::STORE.
9657static SDValue PerformSTORECombine(SDNode *N,
9658 TargetLowering::DAGCombinerInfo &DCI) {
9659 StoreSDNode *St = cast<StoreSDNode>(N);
9660 if (St->isVolatile())
9661 return SDValue();
9662
9663 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9664 // pack all of the elements in one place. Next, store to memory in fewer
9665 // chunks.
9666 SDValue StVal = St->getValue();
9667 EVT VT = StVal.getValueType();
9668 if (St->isTruncatingStore() && VT.isVector()) {
9669 SelectionDAG &DAG = DCI.DAG;
9670 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9671 EVT StVT = St->getMemoryVT();
9672 unsigned NumElems = VT.getVectorNumElements();
9673 assert(StVT != VT && "Cannot truncate to the same type");
9674 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9675 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9676
9677 // From, To sizes and ElemCount must be pow of two
9678 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9679
9680 // We are going to use the original vector elt for storing.
9681 // Accumulated smaller vector elements must be a multiple of the store size.
9682 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9683
9684 unsigned SizeRatio = FromEltSz / ToEltSz;
9685 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9686
9687 // Create a type on which we perform the shuffle.
9688 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9689 NumElems*SizeRatio);
9690 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9691
9692 SDLoc DL(St);
9693 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9694 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9695 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009696 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9697 ? (i + 1) * SizeRatio - 1
9698 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009699
9700 // Can't shuffle using an illegal type.
9701 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9702
9703 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9704 DAG.getUNDEF(WideVec.getValueType()),
9705 ShuffleVec.data());
9706 // At this point all of the data is stored at the bottom of the
9707 // register. We now need to save it to mem.
9708
9709 // Find the largest store unit
9710 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009711 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009712 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9713 StoreType = Tp;
9714 }
9715 // Didn't find a legal store type.
9716 if (!TLI.isTypeLegal(StoreType))
9717 return SDValue();
9718
9719 // Bitcast the original vector into a vector of store-size units
9720 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9721 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9722 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9723 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9724 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009725 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9726 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009727 SDValue BasePtr = St->getBasePtr();
9728
9729 // Perform one or more big stores into memory.
9730 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9731 for (unsigned I = 0; I < E; I++) {
9732 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9733 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009734 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009735 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9736 St->getPointerInfo(), St->isVolatile(),
9737 St->isNonTemporal(), St->getAlignment());
9738 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9739 Increment);
9740 Chains.push_back(Ch);
9741 }
9742 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9743 }
9744
9745 if (!ISD::isNormalStore(St))
9746 return SDValue();
9747
9748 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9749 // ARM stores of arguments in the same cache line.
9750 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9751 StVal.getNode()->hasOneUse()) {
9752 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009753 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009754 SDLoc DL(St);
9755 SDValue BasePtr = St->getBasePtr();
9756 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9757 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9758 BasePtr, St->getPointerInfo(), St->isVolatile(),
9759 St->isNonTemporal(), St->getAlignment());
9760
9761 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009762 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009763 return DAG.getStore(NewST1.getValue(0), DL,
9764 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9765 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9766 St->isNonTemporal(),
9767 std::min(4U, St->getAlignment() / 2));
9768 }
9769
9770 if (StVal.getValueType() == MVT::i64 &&
9771 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9772
9773 // Bitcast an i64 store extracted from a vector to f64.
9774 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9775 SelectionDAG &DAG = DCI.DAG;
9776 SDLoc dl(StVal);
9777 SDValue IntVec = StVal.getOperand(0);
9778 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9779 IntVec.getValueType().getVectorNumElements());
9780 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9781 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9782 Vec, StVal.getOperand(1));
9783 dl = SDLoc(N);
9784 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9785 // Make the DAGCombiner fold the bitcasts.
9786 DCI.AddToWorklist(Vec.getNode());
9787 DCI.AddToWorklist(ExtElt.getNode());
9788 DCI.AddToWorklist(V.getNode());
9789 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9790 St->getPointerInfo(), St->isVolatile(),
9791 St->isNonTemporal(), St->getAlignment(),
9792 St->getAAInfo());
9793 }
9794
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009795 // If this is a legal vector store, try to combine it into a VST1_UPD.
9796 if (ISD::isNormalStore(N) && VT.isVector() &&
9797 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9798 return CombineBaseUpdate(N, DCI);
9799
Ahmed Bougacha23167462014-12-09 21:26:53 +00009800 return SDValue();
9801}
9802
Eric Christopher1b8b94192011-06-29 21:10:36 +00009803// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009804// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9805static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9806{
Chad Rosier6b610b32011-06-28 17:26:57 +00009807 integerPart cN;
9808 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009809 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9810 I != E; I++) {
9811 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9812 if (!C)
9813 return false;
9814
Eric Christopher1b8b94192011-06-29 21:10:36 +00009815 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009816 APFloat APF = C->getValueAPF();
9817 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9818 != APFloat::opOK || !isExact)
9819 return false;
9820
9821 c0 = (I == 0) ? cN : c0;
9822 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9823 return false;
9824 }
9825 C = c0;
9826 return true;
9827}
9828
9829/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9830/// can replace combinations of VMUL and VCVT (floating-point to integer)
9831/// when the VMUL has a constant operand that is a power of 2.
9832///
9833/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9834/// vmul.f32 d16, d17, d16
9835/// vcvt.s32.f32 d16, d16
9836/// becomes:
9837/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +00009838static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009839 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +00009840 if (!Subtarget->hasNEON())
9841 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +00009842
Chad Rosiera087fd22015-10-06 20:23:42 +00009843 SDValue Op = N->getOperand(0);
9844 if (!Op.getValueType().isVector() || Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +00009845 return SDValue();
9846
9847 uint64_t C;
9848 SDValue N0 = Op->getOperand(0);
9849 SDValue ConstVec = Op->getOperand(1);
9850 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9851
Eric Christopher1b8b94192011-06-29 21:10:36 +00009852 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009853 !isConstVecPow2(ConstVec, isSigned, C))
9854 return SDValue();
9855
Tim Northover7cbc2152013-06-28 15:29:25 +00009856 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9857 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009858 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9859 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9860 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009861 // These instructions only exist converting from f32 to i32. We can handle
9862 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009863 // be lossy. We also can't handle more then 4 lanes, since these intructions
9864 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009865 return SDValue();
9866 }
9867
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009868 SDLoc dl(N);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009869 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9870 Intrinsic::arm_neon_vcvtfp2fxu;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009871 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Tim Northover7cbc2152013-06-28 15:29:25 +00009872 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009873 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9874 N0,
9875 DAG.getConstant(Log2_64(C), dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009876
9877 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009878 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009879
9880 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009881}
9882
9883/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9884/// can replace combinations of VCVT (integer to floating-point) and VDIV
9885/// when the VDIV has a constant operand that is a power of 2.
9886///
9887/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9888/// vcvt.f32.s32 d16, d16
9889/// vdiv.f32 d16, d17, d16
9890/// becomes:
9891/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +00009892static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009893 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +00009894 if (!Subtarget->hasNEON())
9895 return SDValue();
9896
Chad Rosierfa8d8932011-06-24 19:23:04 +00009897 SDValue Op = N->getOperand(0);
9898 unsigned OpOpcode = Op.getNode()->getOpcode();
Chad Rosiera087fd22015-10-06 20:23:42 +00009899 if (!N->getValueType(0).isVector() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009900 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9901 return SDValue();
9902
9903 uint64_t C;
9904 SDValue ConstVec = N->getOperand(1);
9905 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9906
9907 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9908 !isConstVecPow2(ConstVec, isSigned, C))
9909 return SDValue();
9910
Tim Northover7cbc2152013-06-28 15:29:25 +00009911 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9912 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9913 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9914 // These instructions only exist converting from i32 to f32. We can handle
9915 // smaller integers by generating an extra extend, but larger ones would
9916 // be lossy.
9917 return SDValue();
9918 }
9919
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009920 SDLoc dl(N);
Tim Northover7cbc2152013-06-28 15:29:25 +00009921 SDValue ConvInput = Op.getOperand(0);
9922 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9923 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9924 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009925 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009926 ConvInput);
9927
Eric Christopher1b8b94192011-06-29 21:10:36 +00009928 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009929 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009930 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009931 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009932 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9933 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009934}
9935
9936/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009937/// operand of a vector shift operation, where all the elements of the
9938/// build_vector must have the same constant integer value.
9939static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9940 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009941 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009942 Op = Op.getOperand(0);
9943 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9944 APInt SplatBits, SplatUndef;
9945 unsigned SplatBitSize;
9946 bool HasAnyUndefs;
9947 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9948 HasAnyUndefs, ElementBits) ||
9949 SplatBitSize > ElementBits)
9950 return false;
9951 Cnt = SplatBits.getSExtValue();
9952 return true;
9953}
9954
9955/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9956/// operand of a vector shift left operation. That value must be in the range:
9957/// 0 <= Value < ElementBits for a left shift; or
9958/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009959static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009960 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009961 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009962 if (! getVShiftImm(Op, ElementBits, Cnt))
9963 return false;
9964 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9965}
9966
9967/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9968/// operand of a vector shift right operation. For a shift opcode, the value
9969/// is positive, but for an intrinsic the value count must be negative. The
9970/// absolute value must be in the range:
9971/// 1 <= |Value| <= ElementBits for a right shift; or
9972/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009973static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009974 int64_t &Cnt) {
9975 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009976 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009977 if (! getVShiftImm(Op, ElementBits, Cnt))
9978 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009979 if (!isIntrinsic)
9980 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9981 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009982 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009983 return true;
9984 }
9985 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +00009986}
9987
9988/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9989static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9990 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9991 switch (IntNo) {
9992 default:
9993 // Don't do anything for most intrinsics.
9994 break;
9995
James Molloya6702e22015-07-17 17:10:55 +00009996 case Intrinsic::arm_neon_vabds:
9997 if (!N->getValueType(0).isInteger())
9998 return SDValue();
9999 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0),
10000 N->getOperand(1), N->getOperand(2));
10001 case Intrinsic::arm_neon_vabdu:
10002 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0),
10003 N->getOperand(1), N->getOperand(2));
10004
Bob Wilson2e076c42009-06-22 23:27:02 +000010005 // Vector shifts: check for immediate versions and lower them.
10006 // Note: This is done during DAG combining instead of DAG legalizing because
10007 // the build_vectors for 64-bit vector element shift counts are generally
10008 // not legal, and it is hard to see their values after they get legalized to
10009 // loads from a constant pool.
10010 case Intrinsic::arm_neon_vshifts:
10011 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010012 case Intrinsic::arm_neon_vrshifts:
10013 case Intrinsic::arm_neon_vrshiftu:
10014 case Intrinsic::arm_neon_vrshiftn:
10015 case Intrinsic::arm_neon_vqshifts:
10016 case Intrinsic::arm_neon_vqshiftu:
10017 case Intrinsic::arm_neon_vqshiftsu:
10018 case Intrinsic::arm_neon_vqshiftns:
10019 case Intrinsic::arm_neon_vqshiftnu:
10020 case Intrinsic::arm_neon_vqshiftnsu:
10021 case Intrinsic::arm_neon_vqrshiftns:
10022 case Intrinsic::arm_neon_vqrshiftnu:
10023 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010024 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010025 int64_t Cnt;
10026 unsigned VShiftOpc = 0;
10027
10028 switch (IntNo) {
10029 case Intrinsic::arm_neon_vshifts:
10030 case Intrinsic::arm_neon_vshiftu:
10031 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10032 VShiftOpc = ARMISD::VSHL;
10033 break;
10034 }
10035 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10036 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10037 ARMISD::VSHRs : ARMISD::VSHRu);
10038 break;
10039 }
10040 return SDValue();
10041
Bob Wilson2e076c42009-06-22 23:27:02 +000010042 case Intrinsic::arm_neon_vrshifts:
10043 case Intrinsic::arm_neon_vrshiftu:
10044 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10045 break;
10046 return SDValue();
10047
10048 case Intrinsic::arm_neon_vqshifts:
10049 case Intrinsic::arm_neon_vqshiftu:
10050 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10051 break;
10052 return SDValue();
10053
10054 case Intrinsic::arm_neon_vqshiftsu:
10055 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10056 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010057 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010058
Bob Wilson2e076c42009-06-22 23:27:02 +000010059 case Intrinsic::arm_neon_vrshiftn:
10060 case Intrinsic::arm_neon_vqshiftns:
10061 case Intrinsic::arm_neon_vqshiftnu:
10062 case Intrinsic::arm_neon_vqshiftnsu:
10063 case Intrinsic::arm_neon_vqrshiftns:
10064 case Intrinsic::arm_neon_vqrshiftnu:
10065 case Intrinsic::arm_neon_vqrshiftnsu:
10066 // Narrowing shifts require an immediate right shift.
10067 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10068 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010069 llvm_unreachable("invalid shift count for narrowing vector shift "
10070 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010071
10072 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010073 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010074 }
10075
10076 switch (IntNo) {
10077 case Intrinsic::arm_neon_vshifts:
10078 case Intrinsic::arm_neon_vshiftu:
10079 // Opcode already set above.
10080 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010081 case Intrinsic::arm_neon_vrshifts:
10082 VShiftOpc = ARMISD::VRSHRs; break;
10083 case Intrinsic::arm_neon_vrshiftu:
10084 VShiftOpc = ARMISD::VRSHRu; break;
10085 case Intrinsic::arm_neon_vrshiftn:
10086 VShiftOpc = ARMISD::VRSHRN; break;
10087 case Intrinsic::arm_neon_vqshifts:
10088 VShiftOpc = ARMISD::VQSHLs; break;
10089 case Intrinsic::arm_neon_vqshiftu:
10090 VShiftOpc = ARMISD::VQSHLu; break;
10091 case Intrinsic::arm_neon_vqshiftsu:
10092 VShiftOpc = ARMISD::VQSHLsu; break;
10093 case Intrinsic::arm_neon_vqshiftns:
10094 VShiftOpc = ARMISD::VQSHRNs; break;
10095 case Intrinsic::arm_neon_vqshiftnu:
10096 VShiftOpc = ARMISD::VQSHRNu; break;
10097 case Intrinsic::arm_neon_vqshiftnsu:
10098 VShiftOpc = ARMISD::VQSHRNsu; break;
10099 case Intrinsic::arm_neon_vqrshiftns:
10100 VShiftOpc = ARMISD::VQRSHRNs; break;
10101 case Intrinsic::arm_neon_vqrshiftnu:
10102 VShiftOpc = ARMISD::VQRSHRNu; break;
10103 case Intrinsic::arm_neon_vqrshiftnsu:
10104 VShiftOpc = ARMISD::VQRSHRNsu; break;
10105 }
10106
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010107 SDLoc dl(N);
10108 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10109 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010110 }
10111
10112 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010113 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010114 int64_t Cnt;
10115 unsigned VShiftOpc = 0;
10116
10117 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10118 VShiftOpc = ARMISD::VSLI;
10119 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10120 VShiftOpc = ARMISD::VSRI;
10121 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010122 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010123 }
10124
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010125 SDLoc dl(N);
10126 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010127 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010128 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010129 }
10130
10131 case Intrinsic::arm_neon_vqrshifts:
10132 case Intrinsic::arm_neon_vqrshiftu:
10133 // No immediate versions of these to check for.
10134 break;
10135 }
10136
10137 return SDValue();
10138}
10139
10140/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10141/// lowers them. As with the vector shift intrinsics, this is done during DAG
10142/// combining instead of DAG legalizing because the build_vectors for 64-bit
10143/// vector element shift counts are generally not legal, and it is hard to see
10144/// their values after they get legalized to loads from a constant pool.
10145static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10146 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010147 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010148 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10149 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10150 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10151 SDValue N1 = N->getOperand(1);
10152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10153 SDValue N0 = N->getOperand(0);
10154 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10155 DAG.MaskedValueIsZero(N0.getOperand(0),
10156 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010157 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010158 }
10159 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010160
10161 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010162 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10163 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010164 return SDValue();
10165
10166 assert(ST->hasNEON() && "unexpected vector shift");
10167 int64_t Cnt;
10168
10169 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010170 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010171
10172 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010173 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10174 SDLoc dl(N);
10175 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10176 DAG.getConstant(Cnt, dl, MVT::i32));
10177 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010178 break;
10179
10180 case ISD::SRA:
10181 case ISD::SRL:
10182 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10183 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10184 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010185 SDLoc dl(N);
10186 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10187 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010188 }
10189 }
10190 return SDValue();
10191}
10192
10193/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10194/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10195static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10196 const ARMSubtarget *ST) {
10197 SDValue N0 = N->getOperand(0);
10198
10199 // Check for sign- and zero-extensions of vector extract operations of 8-
10200 // and 16-bit vector elements. NEON supports these directly. They are
10201 // handled during DAG combining because type legalization will promote them
10202 // to 32-bit types and it is messy to recognize the operations after that.
10203 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10204 SDValue Vec = N0.getOperand(0);
10205 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010206 EVT VT = N->getValueType(0);
10207 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010208 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10209
Owen Anderson9f944592009-08-11 20:47:22 +000010210 if (VT == MVT::i32 &&
10211 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010212 TLI.isTypeLegal(Vec.getValueType()) &&
10213 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010214
10215 unsigned Opc = 0;
10216 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010217 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010218 case ISD::SIGN_EXTEND:
10219 Opc = ARMISD::VGETLANEs;
10220 break;
10221 case ISD::ZERO_EXTEND:
10222 case ISD::ANY_EXTEND:
10223 Opc = ARMISD::VGETLANEu;
10224 break;
10225 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010226 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010227 }
10228 }
10229
10230 return SDValue();
10231}
10232
Evan Chengf863e3f2011-07-13 00:42:17 +000010233/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10234SDValue
10235ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10236 SDValue Cmp = N->getOperand(4);
10237 if (Cmp.getOpcode() != ARMISD::CMPZ)
10238 // Only looking at EQ and NE cases.
10239 return SDValue();
10240
10241 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010242 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010243 SDValue LHS = Cmp.getOperand(0);
10244 SDValue RHS = Cmp.getOperand(1);
10245 SDValue FalseVal = N->getOperand(0);
10246 SDValue TrueVal = N->getOperand(1);
10247 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010248 ARMCC::CondCodes CC =
10249 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010250
10251 // Simplify
10252 // mov r1, r0
10253 // cmp r1, x
10254 // mov r0, y
10255 // moveq r0, x
10256 // to
10257 // cmp r0, x
10258 // movne r0, y
10259 //
10260 // mov r1, r0
10261 // cmp r1, x
10262 // mov r0, x
10263 // movne r0, y
10264 // to
10265 // cmp r0, x
10266 // movne r0, y
10267 /// FIXME: Turn this into a target neutral optimization?
10268 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010269 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010270 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10271 N->getOperand(3), Cmp);
10272 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10273 SDValue ARMcc;
10274 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10275 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10276 N->getOperand(3), NewCmp);
10277 }
10278
10279 if (Res.getNode()) {
10280 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010281 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010282 // Capture demanded bits information that would be otherwise lost.
10283 if (KnownZero == 0xfffffffe)
10284 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10285 DAG.getValueType(MVT::i1));
10286 else if (KnownZero == 0xffffff00)
10287 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10288 DAG.getValueType(MVT::i8));
10289 else if (KnownZero == 0xffff0000)
10290 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10291 DAG.getValueType(MVT::i16));
10292 }
10293
10294 return Res;
10295}
10296
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010297SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010298 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010299 switch (N->getOpcode()) {
10300 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010301 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010302 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010303 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010304 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010305 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010306 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10307 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010308 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010309 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010310 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010311 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010312 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010313 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010314 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010315 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010316 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010317 case ISD::FP_TO_UINT:
10318 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10319 case ISD::FDIV:
10320 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010321 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010322 case ISD::SHL:
10323 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010324 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010325 case ISD::SIGN_EXTEND:
10326 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010327 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010328 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010329 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010330 case ARMISD::VLD2DUP:
10331 case ARMISD::VLD3DUP:
10332 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010333 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010334 case ARMISD::BUILD_VECTOR:
10335 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010336 case ISD::INTRINSIC_VOID:
10337 case ISD::INTRINSIC_W_CHAIN:
10338 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10339 case Intrinsic::arm_neon_vld1:
10340 case Intrinsic::arm_neon_vld2:
10341 case Intrinsic::arm_neon_vld3:
10342 case Intrinsic::arm_neon_vld4:
10343 case Intrinsic::arm_neon_vld2lane:
10344 case Intrinsic::arm_neon_vld3lane:
10345 case Intrinsic::arm_neon_vld4lane:
10346 case Intrinsic::arm_neon_vst1:
10347 case Intrinsic::arm_neon_vst2:
10348 case Intrinsic::arm_neon_vst3:
10349 case Intrinsic::arm_neon_vst4:
10350 case Intrinsic::arm_neon_vst2lane:
10351 case Intrinsic::arm_neon_vst3lane:
10352 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010353 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010354 default: break;
10355 }
10356 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010357 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010358 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010359}
10360
Evan Chengd42641c2011-02-02 01:06:55 +000010361bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10362 EVT VT) const {
10363 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10364}
10365
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010366bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10367 unsigned,
10368 unsigned,
10369 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010370 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010371 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010372
10373 switch (VT.getSimpleVT().SimpleTy) {
10374 default:
10375 return false;
10376 case MVT::i8:
10377 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010378 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010379 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010380 if (AllowsUnaligned) {
10381 if (Fast)
10382 *Fast = Subtarget->hasV7Ops();
10383 return true;
10384 }
10385 return false;
10386 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010387 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010388 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010389 // For any little-endian targets with neon, we can support unaligned ld/st
10390 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010391 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010392 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010393 if (Fast)
10394 *Fast = true;
10395 return true;
10396 }
10397 return false;
10398 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010399 }
10400}
10401
Lang Hames9929c422011-11-02 22:52:45 +000010402static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10403 unsigned AlignCheck) {
10404 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10405 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10406}
10407
10408EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10409 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010410 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010411 bool MemcpyStrSrc,
10412 MachineFunction &MF) const {
10413 const Function *F = MF.getFunction();
10414
10415 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010416 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10417 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010418 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010419 if (Size >= 16 &&
10420 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010421 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010422 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010423 } else if (Size >= 8 &&
10424 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010425 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10426 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010427 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010428 }
10429 }
10430
Lang Hamesb85fcd02011-11-08 18:56:23 +000010431 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010432 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010433 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010434 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010435 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010436
Lang Hames9929c422011-11-02 22:52:45 +000010437 // Let the target-independent logic figure it out.
10438 return MVT::Other;
10439}
10440
Evan Cheng9ec512d2012-12-06 19:13:27 +000010441bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10442 if (Val.getOpcode() != ISD::LOAD)
10443 return false;
10444
10445 EVT VT1 = Val.getValueType();
10446 if (!VT1.isSimple() || !VT1.isInteger() ||
10447 !VT2.isSimple() || !VT2.isInteger())
10448 return false;
10449
10450 switch (VT1.getSimpleVT().SimpleTy) {
10451 default: break;
10452 case MVT::i1:
10453 case MVT::i8:
10454 case MVT::i16:
10455 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10456 return true;
10457 }
10458
10459 return false;
10460}
10461
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010462bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10463 EVT VT = ExtVal.getValueType();
10464
10465 if (!isTypeLegal(VT))
10466 return false;
10467
10468 // Don't create a loadext if we can fold the extension into a wide/long
10469 // instruction.
10470 // If there's more than one user instruction, the loadext is desirable no
10471 // matter what. There can be two uses by the same instruction.
10472 if (ExtVal->use_empty() ||
10473 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10474 return true;
10475
10476 SDNode *U = *ExtVal->use_begin();
10477 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10478 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10479 return false;
10480
10481 return true;
10482}
10483
Tim Northovercc2e9032013-08-06 13:58:03 +000010484bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10485 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10486 return false;
10487
10488 if (!isTypeLegal(EVT::getEVT(Ty1)))
10489 return false;
10490
10491 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10492
10493 // Assuming the caller doesn't have a zeroext or signext return parameter,
10494 // truncation all the way down to i1 is valid.
10495 return true;
10496}
10497
10498
Evan Chengdc49a8d2009-08-14 20:09:37 +000010499static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10500 if (V < 0)
10501 return false;
10502
10503 unsigned Scale = 1;
10504 switch (VT.getSimpleVT().SimpleTy) {
10505 default: return false;
10506 case MVT::i1:
10507 case MVT::i8:
10508 // Scale == 1;
10509 break;
10510 case MVT::i16:
10511 // Scale == 2;
10512 Scale = 2;
10513 break;
10514 case MVT::i32:
10515 // Scale == 4;
10516 Scale = 4;
10517 break;
10518 }
10519
10520 if ((V & (Scale - 1)) != 0)
10521 return false;
10522 V /= Scale;
10523 return V == (V & ((1LL << 5) - 1));
10524}
10525
10526static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10527 const ARMSubtarget *Subtarget) {
10528 bool isNeg = false;
10529 if (V < 0) {
10530 isNeg = true;
10531 V = - V;
10532 }
10533
10534 switch (VT.getSimpleVT().SimpleTy) {
10535 default: return false;
10536 case MVT::i1:
10537 case MVT::i8:
10538 case MVT::i16:
10539 case MVT::i32:
10540 // + imm12 or - imm8
10541 if (isNeg)
10542 return V == (V & ((1LL << 8) - 1));
10543 return V == (V & ((1LL << 12) - 1));
10544 case MVT::f32:
10545 case MVT::f64:
10546 // Same as ARM mode. FIXME: NEON?
10547 if (!Subtarget->hasVFP2())
10548 return false;
10549 if ((V & 3) != 0)
10550 return false;
10551 V >>= 2;
10552 return V == (V & ((1LL << 8) - 1));
10553 }
10554}
10555
Evan Cheng2150b922007-03-12 23:30:29 +000010556/// isLegalAddressImmediate - Return true if the integer value can be used
10557/// as the offset of the target addressing mode for load / store of the
10558/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010559static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010560 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010561 if (V == 0)
10562 return true;
10563
Evan Chengce5dfb62009-03-09 19:15:00 +000010564 if (!VT.isSimple())
10565 return false;
10566
Evan Chengdc49a8d2009-08-14 20:09:37 +000010567 if (Subtarget->isThumb1Only())
10568 return isLegalT1AddressImmediate(V, VT);
10569 else if (Subtarget->isThumb2())
10570 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010571
Evan Chengdc49a8d2009-08-14 20:09:37 +000010572 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010573 if (V < 0)
10574 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010575 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010576 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010577 case MVT::i1:
10578 case MVT::i8:
10579 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010580 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010581 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010582 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010583 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010584 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010585 case MVT::f32:
10586 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010587 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010588 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010589 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010590 return false;
10591 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010592 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010593 }
Evan Cheng10043e22007-01-19 07:51:42 +000010594}
10595
Evan Chengdc49a8d2009-08-14 20:09:37 +000010596bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10597 EVT VT) const {
10598 int Scale = AM.Scale;
10599 if (Scale < 0)
10600 return false;
10601
10602 switch (VT.getSimpleVT().SimpleTy) {
10603 default: return false;
10604 case MVT::i1:
10605 case MVT::i8:
10606 case MVT::i16:
10607 case MVT::i32:
10608 if (Scale == 1)
10609 return true;
10610 // r + r << imm
10611 Scale = Scale & ~1;
10612 return Scale == 2 || Scale == 4 || Scale == 8;
10613 case MVT::i64:
10614 // r + r
10615 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10616 return true;
10617 return false;
10618 case MVT::isVoid:
10619 // Note, we allow "void" uses (basically, uses that aren't loads or
10620 // stores), because arm allows folding a scale into many arithmetic
10621 // operations. This should be made more precise and revisited later.
10622
10623 // Allow r << imm, but the imm has to be a multiple of two.
10624 if (Scale & 1) return false;
10625 return isPowerOf2_32(Scale);
10626 }
10627}
10628
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010629/// isLegalAddressingMode - Return true if the addressing mode represented
10630/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010631bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10632 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010633 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010634 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010635 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010636 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010637
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010638 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010639 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010640 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010641
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010642 switch (AM.Scale) {
10643 case 0: // no scale reg, must be "r+i" or "r", or "i".
10644 break;
10645 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010646 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010647 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010648 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010649 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010650 // ARM doesn't support any R+R*scale+imm addr modes.
10651 if (AM.BaseOffs)
10652 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010653
Bob Wilson866c1742009-04-08 17:55:28 +000010654 if (!VT.isSimple())
10655 return false;
10656
Evan Chengdc49a8d2009-08-14 20:09:37 +000010657 if (Subtarget->isThumb2())
10658 return isLegalT2ScaledAddressingMode(AM, VT);
10659
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010660 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010661 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010662 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010663 case MVT::i1:
10664 case MVT::i8:
10665 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010666 if (Scale < 0) Scale = -Scale;
10667 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010668 return true;
10669 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010670 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010671 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010672 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010673 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010674 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010675 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010676 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010677
Owen Anderson9f944592009-08-11 20:47:22 +000010678 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010679 // Note, we allow "void" uses (basically, uses that aren't loads or
10680 // stores), because arm allows folding a scale into many arithmetic
10681 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010682
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010683 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010684 if (Scale & 1) return false;
10685 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010686 }
Evan Cheng2150b922007-03-12 23:30:29 +000010687 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010688 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010689}
10690
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010691/// isLegalICmpImmediate - Return true if the specified immediate is legal
10692/// icmp immediate, that is the target has icmp instructions which can compare
10693/// a register against the immediate without having to materialize the
10694/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010695bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010696 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010697 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010698 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010699 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010700 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010701 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010702 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010703}
10704
Andrew Tricka22cdb72012-07-18 18:34:27 +000010705/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10706/// *or sub* immediate, that is the target has add or sub instructions which can
10707/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010708/// immediate into a register.
10709bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010710 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010711 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010712 if (!Subtarget->isThumb())
10713 return ARM_AM::getSOImmVal(AbsImm) != -1;
10714 if (Subtarget->isThumb2())
10715 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10716 // Thumb1 only has 8-bit unsigned immediate.
10717 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010718}
10719
Owen Anderson53aa7a92009-08-10 22:56:29 +000010720static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010721 bool isSEXTLoad, SDValue &Base,
10722 SDValue &Offset, bool &isInc,
10723 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010724 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10725 return false;
10726
Owen Anderson9f944592009-08-11 20:47:22 +000010727 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010728 // AddressingMode 3
10729 Base = Ptr->getOperand(0);
10730 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010731 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010732 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010733 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010734 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010735 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010736 return true;
10737 }
10738 }
10739 isInc = (Ptr->getOpcode() == ISD::ADD);
10740 Offset = Ptr->getOperand(1);
10741 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010742 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010743 // AddressingMode 2
10744 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010745 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010746 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010747 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010748 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010749 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010750 Base = Ptr->getOperand(0);
10751 return true;
10752 }
10753 }
10754
10755 if (Ptr->getOpcode() == ISD::ADD) {
10756 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010757 ARM_AM::ShiftOpc ShOpcVal=
10758 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010759 if (ShOpcVal != ARM_AM::no_shift) {
10760 Base = Ptr->getOperand(1);
10761 Offset = Ptr->getOperand(0);
10762 } else {
10763 Base = Ptr->getOperand(0);
10764 Offset = Ptr->getOperand(1);
10765 }
10766 return true;
10767 }
10768
10769 isInc = (Ptr->getOpcode() == ISD::ADD);
10770 Base = Ptr->getOperand(0);
10771 Offset = Ptr->getOperand(1);
10772 return true;
10773 }
10774
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010775 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010776 return false;
10777}
10778
Owen Anderson53aa7a92009-08-10 22:56:29 +000010779static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010780 bool isSEXTLoad, SDValue &Base,
10781 SDValue &Offset, bool &isInc,
10782 SelectionDAG &DAG) {
10783 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10784 return false;
10785
10786 Base = Ptr->getOperand(0);
10787 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10788 int RHSC = (int)RHS->getZExtValue();
10789 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10790 assert(Ptr->getOpcode() == ISD::ADD);
10791 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010792 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010793 return true;
10794 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10795 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010796 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010797 return true;
10798 }
10799 }
10800
10801 return false;
10802}
10803
Evan Cheng10043e22007-01-19 07:51:42 +000010804/// getPreIndexedAddressParts - returns true by value, base pointer and
10805/// offset pointer and addressing mode by reference if the node's address
10806/// can be legally represented as pre-indexed load / store address.
10807bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010808ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10809 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010810 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010811 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010812 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010813 return false;
10814
Owen Anderson53aa7a92009-08-10 22:56:29 +000010815 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010816 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010817 bool isSEXTLoad = false;
10818 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10819 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010820 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010821 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10822 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10823 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010824 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010825 } else
10826 return false;
10827
10828 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010829 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010830 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010831 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10832 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010833 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010834 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010835 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010836 if (!isLegal)
10837 return false;
10838
10839 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10840 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010841}
10842
10843/// getPostIndexedAddressParts - returns true by value, base pointer and
10844/// offset pointer and addressing mode by reference if this node can be
10845/// combined with a load / store to form a post-indexed load / store.
10846bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010847 SDValue &Base,
10848 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010849 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010850 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010851 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010852 return false;
10853
Owen Anderson53aa7a92009-08-10 22:56:29 +000010854 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010855 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010856 bool isSEXTLoad = false;
10857 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010858 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010859 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010860 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10861 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010862 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010863 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010864 } else
10865 return false;
10866
10867 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010868 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010869 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010870 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010871 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010872 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010873 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10874 isInc, DAG);
10875 if (!isLegal)
10876 return false;
10877
Evan Chengf19384d2010-05-18 21:31:17 +000010878 if (Ptr != Base) {
10879 // Swap base ptr and offset to catch more post-index load / store when
10880 // it's legal. In Thumb2 mode, offset must be an immediate.
10881 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10882 !Subtarget->isThumb2())
10883 std::swap(Base, Offset);
10884
10885 // Post-indexed load / store update the base pointer.
10886 if (Ptr != Base)
10887 return false;
10888 }
10889
Evan Cheng84c6cda2009-07-02 07:28:31 +000010890 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10891 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010892}
10893
Jay Foada0653a32014-05-14 21:14:37 +000010894void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10895 APInt &KnownZero,
10896 APInt &KnownOne,
10897 const SelectionDAG &DAG,
10898 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010899 unsigned BitWidth = KnownOne.getBitWidth();
10900 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010901 switch (Op.getOpcode()) {
10902 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010903 case ARMISD::ADDC:
10904 case ARMISD::ADDE:
10905 case ARMISD::SUBC:
10906 case ARMISD::SUBE:
10907 // These nodes' second result is a boolean
10908 if (Op.getResNo() == 0)
10909 break;
10910 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10911 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010912 case ARMISD::CMOV: {
10913 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010914 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010915 if (KnownZero == 0 && KnownOne == 0) return;
10916
Dan Gohmanf990faf2008-02-13 00:35:47 +000010917 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010918 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010919 KnownZero &= KnownZeroRHS;
10920 KnownOne &= KnownOneRHS;
10921 return;
10922 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010923 case ISD::INTRINSIC_W_CHAIN: {
10924 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10925 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10926 switch (IntID) {
10927 default: return;
10928 case Intrinsic::arm_ldaex:
10929 case Intrinsic::arm_ldrex: {
10930 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10931 unsigned MemBits = VT.getScalarType().getSizeInBits();
10932 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10933 return;
10934 }
10935 }
10936 }
Evan Cheng10043e22007-01-19 07:51:42 +000010937 }
10938}
10939
10940//===----------------------------------------------------------------------===//
10941// ARM Inline Assembly Support
10942//===----------------------------------------------------------------------===//
10943
Evan Cheng078b0b02011-01-08 01:24:27 +000010944bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10945 // Looking for "rev" which is V6+.
10946 if (!Subtarget->hasV6Ops())
10947 return false;
10948
10949 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10950 std::string AsmStr = IA->getAsmString();
10951 SmallVector<StringRef, 4> AsmPieces;
10952 SplitString(AsmStr, AsmPieces, ";\n");
10953
10954 switch (AsmPieces.size()) {
10955 default: return false;
10956 case 1:
10957 AsmStr = AsmPieces[0];
10958 AsmPieces.clear();
10959 SplitString(AsmStr, AsmPieces, " \t,");
10960
10961 // rev $0, $1
10962 if (AsmPieces.size() == 3 &&
10963 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10964 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010965 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010966 if (Ty && Ty->getBitWidth() == 32)
10967 return IntrinsicLowering::LowerToByteSwap(CI);
10968 }
10969 break;
10970 }
10971
10972 return false;
10973}
10974
Evan Cheng10043e22007-01-19 07:51:42 +000010975/// getConstraintType - Given a constraint letter, return the type of
10976/// constraint it is for this target.
10977ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010978ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000010979 if (Constraint.size() == 1) {
10980 switch (Constraint[0]) {
10981 default: break;
10982 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010983 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010984 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010985 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010986 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010987 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010988 // An address with a single base register. Due to the way we
10989 // currently handle addresses it is the same as an 'r' memory constraint.
10990 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010991 }
Eric Christophere256cd02011-06-21 22:10:57 +000010992 } else if (Constraint.size() == 2) {
10993 switch (Constraint[0]) {
10994 default: break;
10995 // All 'U+' constraints are addresses.
10996 case 'U': return C_Memory;
10997 }
Evan Cheng10043e22007-01-19 07:51:42 +000010998 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010999 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011000}
11001
John Thompsone8360b72010-10-29 17:29:13 +000011002/// Examine constraint type and operand type and determine a weight value.
11003/// This object must already have been set up with the operand type
11004/// and the current alternative constraint selected.
11005TargetLowering::ConstraintWeight
11006ARMTargetLowering::getSingleConstraintMatchWeight(
11007 AsmOperandInfo &info, const char *constraint) const {
11008 ConstraintWeight weight = CW_Invalid;
11009 Value *CallOperandVal = info.CallOperandVal;
11010 // If we don't have a value, we can't do a match,
11011 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011012 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011013 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011014 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011015 // Look at the constraint type.
11016 switch (*constraint) {
11017 default:
11018 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11019 break;
11020 case 'l':
11021 if (type->isIntegerTy()) {
11022 if (Subtarget->isThumb())
11023 weight = CW_SpecificReg;
11024 else
11025 weight = CW_Register;
11026 }
11027 break;
11028 case 'w':
11029 if (type->isFloatingPointTy())
11030 weight = CW_Register;
11031 break;
11032 }
11033 return weight;
11034}
11035
Eric Christophercf2007c2011-06-30 23:50:52 +000011036typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011037RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11038 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011039 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011040 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011041 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011042 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011043 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011044 return RCPair(0U, &ARM::tGPRRegClass);
11045 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011046 case 'h': // High regs or no regs.
11047 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011048 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011049 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011050 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011051 if (Subtarget->isThumb1Only())
11052 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011053 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011054 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011055 if (VT == MVT::Other)
11056 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011057 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011058 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011059 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011060 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011061 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011062 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011063 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011064 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011065 if (VT == MVT::Other)
11066 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011067 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011068 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011069 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011070 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011071 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011072 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011073 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011074 case 't':
11075 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011076 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011077 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011078 }
11079 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011080 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011081 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011082
Eric Christopher11e4df72015-02-26 22:38:43 +000011083 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011084}
11085
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011086/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11087/// vector. If it is invalid, don't add anything to Ops.
11088void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011089 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011090 std::vector<SDValue>&Ops,
11091 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011092 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011093
Eric Christopherde9399b2011-06-02 23:16:42 +000011094 // Currently only support length 1 constraints.
11095 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011096
Eric Christopherde9399b2011-06-02 23:16:42 +000011097 char ConstraintLetter = Constraint[0];
11098 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011099 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011100 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011101 case 'I': case 'J': case 'K': case 'L':
11102 case 'M': case 'N': case 'O':
11103 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11104 if (!C)
11105 return;
11106
11107 int64_t CVal64 = C->getSExtValue();
11108 int CVal = (int) CVal64;
11109 // None of these constraints allow values larger than 32 bits. Check
11110 // that the value fits in an int.
11111 if (CVal != CVal64)
11112 return;
11113
Eric Christopherde9399b2011-06-02 23:16:42 +000011114 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011115 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011116 // Constant suitable for movw, must be between 0 and
11117 // 65535.
11118 if (Subtarget->hasV6T2Ops())
11119 if (CVal >= 0 && CVal <= 65535)
11120 break;
11121 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011122 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011123 if (Subtarget->isThumb1Only()) {
11124 // This must be a constant between 0 and 255, for ADD
11125 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011126 if (CVal >= 0 && CVal <= 255)
11127 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011128 } else if (Subtarget->isThumb2()) {
11129 // A constant that can be used as an immediate value in a
11130 // data-processing instruction.
11131 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11132 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011133 } else {
11134 // A constant that can be used as an immediate value in a
11135 // data-processing instruction.
11136 if (ARM_AM::getSOImmVal(CVal) != -1)
11137 break;
11138 }
11139 return;
11140
11141 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000011142 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011143 // This must be a constant between -255 and -1, for negated ADD
11144 // immediates. This can be used in GCC with an "n" modifier that
11145 // prints the negated value, for use with SUB instructions. It is
11146 // not useful otherwise but is implemented for compatibility.
11147 if (CVal >= -255 && CVal <= -1)
11148 break;
11149 } else {
11150 // This must be a constant between -4095 and 4095. It is not clear
11151 // what this constraint is intended for. Implemented for
11152 // compatibility with GCC.
11153 if (CVal >= -4095 && CVal <= 4095)
11154 break;
11155 }
11156 return;
11157
11158 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011159 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011160 // A 32-bit value where only one byte has a nonzero value. Exclude
11161 // zero to match GCC. This constraint is used by GCC internally for
11162 // constants that can be loaded with a move/shift combination.
11163 // It is not useful otherwise but is implemented for compatibility.
11164 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11165 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011166 } else if (Subtarget->isThumb2()) {
11167 // A constant whose bitwise inverse can be used as an immediate
11168 // value in a data-processing instruction. This can be used in GCC
11169 // with a "B" modifier that prints the inverted value, for use with
11170 // BIC and MVN instructions. It is not useful otherwise but is
11171 // implemented for compatibility.
11172 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11173 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011174 } else {
11175 // A constant whose bitwise inverse can be used as an immediate
11176 // value in a data-processing instruction. This can be used in GCC
11177 // with a "B" modifier that prints the inverted value, for use with
11178 // BIC and MVN instructions. It is not useful otherwise but is
11179 // implemented for compatibility.
11180 if (ARM_AM::getSOImmVal(~CVal) != -1)
11181 break;
11182 }
11183 return;
11184
11185 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011186 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011187 // This must be a constant between -7 and 7,
11188 // for 3-operand ADD/SUB immediate instructions.
11189 if (CVal >= -7 && CVal < 7)
11190 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011191 } else if (Subtarget->isThumb2()) {
11192 // A constant whose negation can be used as an immediate value in a
11193 // data-processing instruction. This can be used in GCC with an "n"
11194 // modifier that prints the negated value, for use with SUB
11195 // instructions. It is not useful otherwise but is implemented for
11196 // compatibility.
11197 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11198 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011199 } else {
11200 // A constant whose negation can be used as an immediate value in a
11201 // data-processing instruction. This can be used in GCC with an "n"
11202 // modifier that prints the negated value, for use with SUB
11203 // instructions. It is not useful otherwise but is implemented for
11204 // compatibility.
11205 if (ARM_AM::getSOImmVal(-CVal) != -1)
11206 break;
11207 }
11208 return;
11209
11210 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000011211 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011212 // This must be a multiple of 4 between 0 and 1020, for
11213 // ADD sp + immediate.
11214 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11215 break;
11216 } else {
11217 // A power of two or a constant between 0 and 32. This is used in
11218 // GCC for the shift amount on shifted register operands, but it is
11219 // useful in general for any shift amounts.
11220 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11221 break;
11222 }
11223 return;
11224
11225 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011226 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011227 // This must be a constant between 0 and 31, for shift amounts.
11228 if (CVal >= 0 && CVal <= 31)
11229 break;
11230 }
11231 return;
11232
11233 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011234 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011235 // This must be a multiple of 4 between -508 and 508, for
11236 // ADD/SUB sp = sp + immediate.
11237 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11238 break;
11239 }
11240 return;
11241 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011242 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011243 break;
11244 }
11245
11246 if (Result.getNode()) {
11247 Ops.push_back(Result);
11248 return;
11249 }
Dale Johannesence97d552010-06-25 21:55:36 +000011250 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011251}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011252
Scott Douglassd2974a62015-08-24 09:17:11 +000011253static RTLIB::Libcall getDivRemLibcall(
11254 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011255 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11256 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011257 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011258 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11259 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011260 RTLIB::Libcall LC;
11261 switch (SVT) {
11262 default: llvm_unreachable("Unexpected request for libcall!");
11263 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11264 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11265 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11266 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11267 }
11268 return LC;
11269}
11270
11271static TargetLowering::ArgListTy getDivRemArgList(
11272 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011273 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11274 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011275 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011276 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11277 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011278 TargetLowering::ArgListTy Args;
11279 TargetLowering::ArgListEntry Entry;
11280 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11281 EVT ArgVT = N->getOperand(i).getValueType();
11282 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11283 Entry.Node = N->getOperand(i);
11284 Entry.Ty = ArgTy;
11285 Entry.isSExt = isSigned;
11286 Entry.isZExt = !isSigned;
11287 Args.push_back(Entry);
11288 }
11289 return Args;
11290}
11291
Renato Golin87610692013-07-16 09:32:17 +000011292SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011293 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid()) &&
11294 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011295 unsigned Opcode = Op->getOpcode();
11296 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011297 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011298 bool isSigned = (Opcode == ISD::SDIVREM);
11299 EVT VT = Op->getValueType(0);
11300 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11301
Scott Douglassd2974a62015-08-24 09:17:11 +000011302 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11303 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011304 SDValue InChain = DAG.getEntryNode();
11305
Scott Douglassd2974a62015-08-24 09:17:11 +000011306 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11307 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011308
11309 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011310 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011311
Reid Kleckner343c3952014-11-20 23:51:47 +000011312 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011313
11314 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011315 TargetLowering::CallLoweringInfo CLI(DAG);
11316 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011317 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011318 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011319
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011320 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011321 return CallInfo.first;
11322}
11323
Scott Douglassbdef6042015-08-24 09:17:18 +000011324// Lowers REM using divmod helpers
11325// see RTABI section 4.2/4.3
11326SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11327 // Build return types (div and rem)
11328 std::vector<Type*> RetTyParams;
11329 Type *RetTyElement;
11330
11331 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11332 default: llvm_unreachable("Unexpected request for libcall!");
11333 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11334 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11335 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11336 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11337 }
11338
11339 RetTyParams.push_back(RetTyElement);
11340 RetTyParams.push_back(RetTyElement);
11341 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11342 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11343
11344 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11345 SimpleTy);
11346 SDValue InChain = DAG.getEntryNode();
11347 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11348 bool isSigned = N->getOpcode() == ISD::SREM;
11349 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11350 getPointerTy(DAG.getDataLayout()));
11351
11352 // Lower call
11353 CallLoweringInfo CLI(DAG);
11354 CLI.setChain(InChain)
11355 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11356 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11357 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11358
11359 // Return second (rem) result operand (first contains div)
11360 SDNode *ResNode = CallResult.first.getNode();
11361 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11362 return ResNode->getOperand(1);
11363}
11364
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011365SDValue
11366ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11367 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11368 SDLoc DL(Op);
11369
11370 // Get the inputs.
11371 SDValue Chain = Op.getOperand(0);
11372 SDValue Size = Op.getOperand(1);
11373
11374 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011375 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011376
11377 SDValue Flag;
11378 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11379 Flag = Chain.getValue(1);
11380
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011381 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011382 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11383
11384 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11385 Chain = NewSP.getValue(1);
11386
11387 SDValue Ops[2] = { NewSP, Chain };
11388 return DAG.getMergeValues(Ops, DL);
11389}
11390
Oliver Stannard51b1d462014-08-21 12:50:31 +000011391SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11392 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11393 "Unexpected type for custom-lowering FP_EXTEND");
11394
11395 RTLIB::Libcall LC;
11396 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11397
11398 SDValue SrcVal = Op.getOperand(0);
11399 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11400 /*isSigned*/ false, SDLoc(Op)).first;
11401}
11402
11403SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11404 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11405 Subtarget->isFPOnlySP() &&
11406 "Unexpected type for custom-lowering FP_ROUND");
11407
11408 RTLIB::Libcall LC;
11409 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11410
11411 SDValue SrcVal = Op.getOperand(0);
11412 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11413 /*isSigned*/ false, SDLoc(Op)).first;
11414}
11415
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011416bool
11417ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11418 // The ARM target isn't yet aware of offsets.
11419 return false;
11420}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011421
Jim Grosbach11013ed2010-07-16 23:05:05 +000011422bool ARM::isBitFieldInvertedMask(unsigned v) {
11423 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011424 return false;
11425
Jim Grosbach11013ed2010-07-16 23:05:05 +000011426 // there can be 1's on either or both "outsides", all the "inside"
11427 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011428 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011429}
11430
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011431/// isFPImmLegal - Returns true if the target can instruction select the
11432/// specified FP immediate natively. If false, the legalizer will
11433/// materialize the FP immediate as a load from a constant pool.
11434bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11435 if (!Subtarget->hasVFP3())
11436 return false;
11437 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011438 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011439 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011440 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011441 return false;
11442}
Bob Wilson5549d492010-09-21 17:56:22 +000011443
Wesley Peck527da1b2010-11-23 03:31:01 +000011444/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011445/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11446/// specified in the intrinsic calls.
11447bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11448 const CallInst &I,
11449 unsigned Intrinsic) const {
11450 switch (Intrinsic) {
11451 case Intrinsic::arm_neon_vld1:
11452 case Intrinsic::arm_neon_vld2:
11453 case Intrinsic::arm_neon_vld3:
11454 case Intrinsic::arm_neon_vld4:
11455 case Intrinsic::arm_neon_vld2lane:
11456 case Intrinsic::arm_neon_vld3lane:
11457 case Intrinsic::arm_neon_vld4lane: {
11458 Info.opc = ISD::INTRINSIC_W_CHAIN;
11459 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011460 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11461 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011462 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11463 Info.ptrVal = I.getArgOperand(0);
11464 Info.offset = 0;
11465 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11466 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11467 Info.vol = false; // volatile loads with NEON intrinsics not supported
11468 Info.readMem = true;
11469 Info.writeMem = false;
11470 return true;
11471 }
11472 case Intrinsic::arm_neon_vst1:
11473 case Intrinsic::arm_neon_vst2:
11474 case Intrinsic::arm_neon_vst3:
11475 case Intrinsic::arm_neon_vst4:
11476 case Intrinsic::arm_neon_vst2lane:
11477 case Intrinsic::arm_neon_vst3lane:
11478 case Intrinsic::arm_neon_vst4lane: {
11479 Info.opc = ISD::INTRINSIC_VOID;
11480 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011481 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011482 unsigned NumElts = 0;
11483 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011484 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011485 if (!ArgTy->isVectorTy())
11486 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011487 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011488 }
11489 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11490 Info.ptrVal = I.getArgOperand(0);
11491 Info.offset = 0;
11492 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11493 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11494 Info.vol = false; // volatile stores with NEON intrinsics not supported
11495 Info.readMem = false;
11496 Info.writeMem = true;
11497 return true;
11498 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011499 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011500 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011501 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011502 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11503 Info.opc = ISD::INTRINSIC_W_CHAIN;
11504 Info.memVT = MVT::getVT(PtrTy->getElementType());
11505 Info.ptrVal = I.getArgOperand(0);
11506 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011507 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011508 Info.vol = true;
11509 Info.readMem = true;
11510 Info.writeMem = false;
11511 return true;
11512 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011513 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011514 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011515 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011516 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11517 Info.opc = ISD::INTRINSIC_W_CHAIN;
11518 Info.memVT = MVT::getVT(PtrTy->getElementType());
11519 Info.ptrVal = I.getArgOperand(1);
11520 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011521 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011522 Info.vol = true;
11523 Info.readMem = false;
11524 Info.writeMem = true;
11525 return true;
11526 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011527 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011528 case Intrinsic::arm_strexd: {
11529 Info.opc = ISD::INTRINSIC_W_CHAIN;
11530 Info.memVT = MVT::i64;
11531 Info.ptrVal = I.getArgOperand(2);
11532 Info.offset = 0;
11533 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011534 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011535 Info.readMem = false;
11536 Info.writeMem = true;
11537 return true;
11538 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011539 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011540 case Intrinsic::arm_ldrexd: {
11541 Info.opc = ISD::INTRINSIC_W_CHAIN;
11542 Info.memVT = MVT::i64;
11543 Info.ptrVal = I.getArgOperand(0);
11544 Info.offset = 0;
11545 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011546 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011547 Info.readMem = true;
11548 Info.writeMem = false;
11549 return true;
11550 }
Bob Wilson5549d492010-09-21 17:56:22 +000011551 default:
11552 break;
11553 }
11554
11555 return false;
11556}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011557
11558/// \brief Returns true if it is beneficial to convert a load of a constant
11559/// to just the constant itself.
11560bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11561 Type *Ty) const {
11562 assert(Ty->isIntegerTy());
11563
11564 unsigned Bits = Ty->getPrimitiveSizeInBits();
11565 if (Bits == 0 || Bits > 32)
11566 return false;
11567 return true;
11568}
Tim Northover037f26f22014-04-17 18:22:47 +000011569
Robin Morisset5349e8e2014-09-18 18:56:04 +000011570Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11571 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011572 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011573
11574 // First, if the target has no DMB, see what fallback we can use.
11575 if (!Subtarget->hasDataBarrier()) {
11576 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11577 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11578 // here.
11579 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11580 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11581 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11582 Builder.getInt32(0), Builder.getInt32(7),
11583 Builder.getInt32(10), Builder.getInt32(5)};
11584 return Builder.CreateCall(MCR, args);
11585 } else {
11586 // Instead of using barriers, atomic accesses on these subtargets use
11587 // libcalls.
11588 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11589 }
11590 } else {
11591 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11592 // Only a full system barrier exists in the M-class architectures.
11593 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11594 Constant *CDomain = Builder.getInt32(Domain);
11595 return Builder.CreateCall(DMB, CDomain);
11596 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011597}
11598
11599// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011600Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011601 AtomicOrdering Ord, bool IsStore,
11602 bool IsLoad) const {
11603 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011604 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011605
11606 switch (Ord) {
11607 case NotAtomic:
11608 case Unordered:
11609 llvm_unreachable("Invalid fence: unordered/non-atomic");
11610 case Monotonic:
11611 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011612 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011613 case SequentiallyConsistent:
11614 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011615 return nullptr; // Nothing to do
11616 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011617 case Release:
11618 case AcquireRelease:
11619 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011620 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011621 // FIXME: add a comment with a link to documentation justifying this.
11622 else
Robin Morissetdedef332014-09-23 20:31:14 +000011623 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011624 }
Robin Morissetdedef332014-09-23 20:31:14 +000011625 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011626}
11627
Robin Morissetdedef332014-09-23 20:31:14 +000011628Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011629 AtomicOrdering Ord, bool IsStore,
11630 bool IsLoad) const {
11631 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011632 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011633
11634 switch (Ord) {
11635 case NotAtomic:
11636 case Unordered:
11637 llvm_unreachable("Invalid fence: unordered/not-atomic");
11638 case Monotonic:
11639 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011640 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011641 case Acquire:
11642 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011643 case SequentiallyConsistent:
11644 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011645 }
Robin Morissetdedef332014-09-23 20:31:14 +000011646 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011647}
11648
Robin Morisseted3d48f2014-09-03 21:29:59 +000011649// Loads and stores less than 64-bits are already atomic; ones above that
11650// are doomed anyway, so defer to the default libcall and blame the OS when
11651// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11652// anything for those.
11653bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11654 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11655 return (Size == 64) && !Subtarget->isMClass();
11656}
Tim Northover037f26f22014-04-17 18:22:47 +000011657
Robin Morisseted3d48f2014-09-03 21:29:59 +000011658// Loads and stores less than 64-bits are already atomic; ones above that
11659// are doomed anyway, so defer to the default libcall and blame the OS when
11660// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11661// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011662// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11663// guarantee, see DDI0406C ARM architecture reference manual,
11664// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000011665TargetLowering::AtomicExpansionKind
11666ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011667 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Ahmed Bougacha52468672015-09-11 17:08:28 +000011668 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLSC
11669 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000011670}
11671
11672// For the real atomic operations, we have ldrex/strex up to 32 bits,
11673// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000011674TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000011675ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011676 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011677 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000011678 ? AtomicExpansionKind::LLSC
11679 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011680}
11681
Ahmed Bougacha52468672015-09-11 17:08:28 +000011682bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
11683 AtomicCmpXchgInst *AI) const {
11684 return true;
11685}
11686
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011687// This has so far only been implemented for MachO.
11688bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011689 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011690}
11691
Quentin Colombetc32615d2014-10-31 17:52:53 +000011692bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11693 unsigned &Cost) const {
11694 // If we do not have NEON, vector types are not natively supported.
11695 if (!Subtarget->hasNEON())
11696 return false;
11697
11698 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000011699 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000011700 // better to leave at float as we have more freedom in the addressing mode for
11701 // those.
11702 if (VectorTy->isFPOrFPVectorTy())
11703 return false;
11704
11705 // If the index is unknown at compile time, this is very expensive to lower
11706 // and it is not possible to combine the store with the extract.
11707 if (!isa<ConstantInt>(Idx))
11708 return false;
11709
11710 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11711 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11712 // We can do a store + vector extract on any vector that fits perfectly in a D
11713 // or Q register.
11714 if (BitWidth == 64 || BitWidth == 128) {
11715 Cost = 0;
11716 return true;
11717 }
11718 return false;
11719}
11720
Tim Northover037f26f22014-04-17 18:22:47 +000011721Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11722 AtomicOrdering Ord) const {
11723 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11724 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011725 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011726
11727 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11728 // intrinsic must return {i32, i32} and we have to recombine them into a
11729 // single i64 here.
11730 if (ValTy->getPrimitiveSizeInBits() == 64) {
11731 Intrinsic::ID Int =
11732 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11733 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11734
11735 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11736 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11737
11738 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11739 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011740 if (!Subtarget->isLittle())
11741 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011742 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11743 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11744 return Builder.CreateOr(
11745 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11746 }
11747
11748 Type *Tys[] = { Addr->getType() };
11749 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11750 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11751
11752 return Builder.CreateTruncOrBitCast(
11753 Builder.CreateCall(Ldrex, Addr),
11754 cast<PointerType>(Addr->getType())->getElementType());
11755}
11756
Ahmed Bougacha81616a72015-09-22 17:22:58 +000011757void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
11758 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000011759 if (!Subtarget->hasV7Ops())
11760 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000011761 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11762 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
11763}
11764
Tim Northover037f26f22014-04-17 18:22:47 +000011765Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11766 Value *Addr,
11767 AtomicOrdering Ord) const {
11768 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011769 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011770
11771 // Since the intrinsics must have legal type, the i64 intrinsics take two
11772 // parameters: "i32, i32". We must marshal Val into the appropriate form
11773 // before the call.
11774 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11775 Intrinsic::ID Int =
11776 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11777 Function *Strex = Intrinsic::getDeclaration(M, Int);
11778 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11779
11780 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11781 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011782 if (!Subtarget->isLittle())
11783 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011784 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011785 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011786 }
11787
11788 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11789 Type *Tys[] = { Addr->getType() };
11790 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11791
David Blaikieff6409d2015-05-18 22:13:54 +000011792 return Builder.CreateCall(
11793 Strex, {Builder.CreateZExtOrBitCast(
11794 Val, Strex->getFunctionType()->getParamType(0)),
11795 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011796}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011797
Hao Liu2cd34bb2015-06-26 02:45:36 +000011798/// \brief Lower an interleaved load into a vldN intrinsic.
11799///
11800/// E.g. Lower an interleaved load (Factor = 2):
11801/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
11802/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
11803/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
11804///
11805/// Into:
11806/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
11807/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
11808/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
11809bool ARMTargetLowering::lowerInterleavedLoad(
11810 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
11811 ArrayRef<unsigned> Indices, unsigned Factor) const {
11812 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11813 "Invalid interleave factor");
11814 assert(!Shuffles.empty() && "Empty shufflevector input");
11815 assert(Shuffles.size() == Indices.size() &&
11816 "Unmatched number of shufflevectors and indices");
11817
11818 VectorType *VecTy = Shuffles[0]->getType();
11819 Type *EltTy = VecTy->getVectorElementType();
11820
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011821 const DataLayout &DL = LI->getModule()->getDataLayout();
11822 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
11823 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011824
11825 // Skip illegal vector types and vector types of i64/f64 element (vldN doesn't
11826 // support i64/f64 element).
11827 if ((VecSize != 64 && VecSize != 128) || EltIs64Bits)
11828 return false;
11829
11830 // A pointer vector can not be the return type of the ldN intrinsics. Need to
11831 // load integer vectors first and then convert to pointer vectors.
11832 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011833 VecTy =
11834 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000011835
11836 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
11837 Intrinsic::arm_neon_vld3,
11838 Intrinsic::arm_neon_vld4};
11839
Hao Liu2cd34bb2015-06-26 02:45:36 +000011840 IRBuilder<> Builder(LI);
11841 SmallVector<Value *, 2> Ops;
11842
11843 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
11844 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
11845 Ops.push_back(Builder.getInt32(LI->getAlignment()));
11846
Jeroen Ketemaab99b592015-09-30 10:56:37 +000011847 Type *Tys[] = { VecTy, Int8Ptr };
11848 Function *VldnFunc =
11849 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011850 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
11851
11852 // Replace uses of each shufflevector with the corresponding vector loaded
11853 // by ldN.
11854 for (unsigned i = 0; i < Shuffles.size(); i++) {
11855 ShuffleVectorInst *SV = Shuffles[i];
11856 unsigned Index = Indices[i];
11857
11858 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
11859
11860 // Convert the integer vector to pointer vector if the element is pointer.
11861 if (EltTy->isPointerTy())
11862 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
11863
11864 SV->replaceAllUsesWith(SubVec);
11865 }
11866
11867 return true;
11868}
11869
11870/// \brief Get a mask consisting of sequential integers starting from \p Start.
11871///
11872/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
11873static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
11874 unsigned NumElts) {
11875 SmallVector<Constant *, 16> Mask;
11876 for (unsigned i = 0; i < NumElts; i++)
11877 Mask.push_back(Builder.getInt32(Start + i));
11878
11879 return ConstantVector::get(Mask);
11880}
11881
11882/// \brief Lower an interleaved store into a vstN intrinsic.
11883///
11884/// E.g. Lower an interleaved store (Factor = 3):
11885/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
11886/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
11887/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
11888///
11889/// Into:
11890/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
11891/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
11892/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
11893/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
11894///
11895/// Note that the new shufflevectors will be removed and we'll only generate one
11896/// vst3 instruction in CodeGen.
11897bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
11898 ShuffleVectorInst *SVI,
11899 unsigned Factor) const {
11900 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11901 "Invalid interleave factor");
11902
11903 VectorType *VecTy = SVI->getType();
11904 assert(VecTy->getVectorNumElements() % Factor == 0 &&
11905 "Invalid interleaved store");
11906
11907 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
11908 Type *EltTy = VecTy->getVectorElementType();
11909 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
11910
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011911 const DataLayout &DL = SI->getModule()->getDataLayout();
11912 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
11913 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011914
11915 // Skip illegal sub vector types and vector types of i64/f64 element (vstN
11916 // doesn't support i64/f64 element).
11917 if ((SubVecSize != 64 && SubVecSize != 128) || EltIs64Bits)
11918 return false;
11919
11920 Value *Op0 = SVI->getOperand(0);
11921 Value *Op1 = SVI->getOperand(1);
11922 IRBuilder<> Builder(SI);
11923
11924 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
11925 // vectors to integer vectors.
11926 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011927 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011928
11929 // Convert to the corresponding integer vector.
11930 Type *IntVecTy =
11931 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
11932 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
11933 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
11934
11935 SubVecTy = VectorType::get(IntTy, NumSubElts);
11936 }
11937
11938 static Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
11939 Intrinsic::arm_neon_vst3,
11940 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000011941 SmallVector<Value *, 6> Ops;
11942
11943 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
11944 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
11945
Jeroen Ketemaab99b592015-09-30 10:56:37 +000011946 Type *Tys[] = { Int8Ptr, SubVecTy };
11947 Function *VstNFunc = Intrinsic::getDeclaration(
11948 SI->getModule(), StoreInts[Factor - 2], Tys);
11949
Hao Liu2cd34bb2015-06-26 02:45:36 +000011950 // Split the shufflevector operands into sub vectors for the new vstN call.
11951 for (unsigned i = 0; i < Factor; i++)
11952 Ops.push_back(Builder.CreateShuffleVector(
11953 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
11954
11955 Ops.push_back(Builder.getInt32(SI->getAlignment()));
11956 Builder.CreateCall(VstNFunc, Ops);
11957 return true;
11958}
11959
Oliver Stannardc24f2172014-05-09 14:01:47 +000011960enum HABaseType {
11961 HA_UNKNOWN = 0,
11962 HA_FLOAT,
11963 HA_DOUBLE,
11964 HA_VECT64,
11965 HA_VECT128
11966};
11967
11968static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11969 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000011970 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011971 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11972 uint64_t SubMembers = 0;
11973 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11974 return false;
11975 Members += SubMembers;
11976 }
Craig Toppere3dcce92015-08-01 22:20:21 +000011977 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011978 uint64_t SubMembers = 0;
11979 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11980 return false;
11981 Members += SubMembers * AT->getNumElements();
11982 } else if (Ty->isFloatTy()) {
11983 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11984 return false;
11985 Members = 1;
11986 Base = HA_FLOAT;
11987 } else if (Ty->isDoubleTy()) {
11988 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11989 return false;
11990 Members = 1;
11991 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000011992 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000011993 Members = 1;
11994 switch (Base) {
11995 case HA_FLOAT:
11996 case HA_DOUBLE:
11997 return false;
11998 case HA_VECT64:
11999 return VT->getBitWidth() == 64;
12000 case HA_VECT128:
12001 return VT->getBitWidth() == 128;
12002 case HA_UNKNOWN:
12003 switch (VT->getBitWidth()) {
12004 case 64:
12005 Base = HA_VECT64;
12006 return true;
12007 case 128:
12008 Base = HA_VECT128;
12009 return true;
12010 default:
12011 return false;
12012 }
12013 }
12014 }
12015
12016 return (Members > 0 && Members <= 4);
12017}
12018
Tim Northovere95c5b32015-02-24 17:22:34 +000012019/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12020/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12021/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012022bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12023 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012024 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12025 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012026 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012027
12028 HABaseType Base = HA_UNKNOWN;
12029 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012030 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12031 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12032
12033 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12034 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012035}