blob: de18bafd3827d7715418a5e3f142eaf01f1fd9fb [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
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000146 if (!VT.isFloatingPoint() &&
147 VT != MVT::v2i64 && VT != MVT::v1i64)
148 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
149 setOperationAction(Opcode, VT, Legal);
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000158 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Eric Christopher1889fdc2015-01-29 00:19:39 +0000162ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
163 const ARMSubtarget &STI)
164 : TargetLowering(TM), Subtarget(&STI) {
165 RegInfo = Subtarget->getRegisterInfo();
166 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000167
Duncan Sandsf2641e12011-09-06 19:07:46 +0000168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
169
Tim Northoverd6a729b2014-01-06 14:28:05 +0000170 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000172 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000174 static const struct {
175 const RTLIB::Libcall Op;
176 const char * const Name;
177 const ISD::CondCode Cond;
178 } LibraryCalls[] = {
179 // Single-precision floating-point arithmetic.
180 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
181 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
182 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
183 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000184
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000185 // Double-precision floating-point arithmetic.
186 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
187 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
188 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
189 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000190
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000191 // Single-precision comparisons.
192 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
193 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
194 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
195 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
196 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
197 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
198 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
199 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000200
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000201 // Double-precision comparisons.
202 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
203 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
204 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
205 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
206 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
207 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
208 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
209 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000210
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000211 // Floating-point to integer conversions.
212 // i64 conversions are done via library routines even when generating VFP
213 // instructions, so use the same ones.
214 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
215 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
216 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
217 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000219 // Conversions between floating types.
220 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
221 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000222
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000223 // Integer to floating-point conversions.
224 // i64 conversions are done via library routines even when generating VFP
225 // instructions, so use the same ones.
226 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
227 // e.g., __floatunsidf vs. __floatunssidfvfp.
228 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
229 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
230 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
231 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
232 };
Evan Cheng10043e22007-01-19 07:51:42 +0000233
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000234 for (const auto &LC : LibraryCalls) {
235 setLibcallName(LC.Op, LC.Name);
236 if (LC.Cond != ISD::SETCC_INVALID)
237 setCmpLibcallCC(LC.Op, LC.Cond);
238 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000239 }
Tim Northover8b403662015-10-28 22:51:16 +0000240
241 // Set the correct calling convention for ARMv7k WatchOS. It's just
242 // AAPCS_VFP for functions as simple as libcalls.
Tim Northover042a6c12016-01-27 19:32:29 +0000243 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000244 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i)
245 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP);
246 }
Evan Cheng10043e22007-01-19 07:51:42 +0000247 }
248
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000249 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000250 setLibcallName(RTLIB::SHL_I128, nullptr);
251 setLibcallName(RTLIB::SRL_I128, nullptr);
252 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000253
Renato Golin6d435f12015-11-09 12:40:30 +0000254 // RTLIB
255 if (Subtarget->isAAPCS_ABI() &&
256 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
257 Subtarget->isTargetAndroid())) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000258 static const struct {
259 const RTLIB::Libcall Op;
260 const char * const Name;
261 const CallingConv::ID CC;
262 const ISD::CondCode Cond;
263 } LibraryCalls[] = {
264 // Double-precision floating-point arithmetic helper functions
265 // RTABI chapter 4.1.2, Table 2
266 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
267 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
268 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000270
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000271 // Double-precision floating-point comparison helper functions
272 // RTABI chapter 4.1.2, Table 3
273 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
275 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000281
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000282 // Single-precision floating-point arithmetic helper functions
283 // RTABI chapter 4.1.2, Table 4
284 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
285 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
286 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000288
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000289 // Single-precision floating-point comparison helper functions
290 // RTABI chapter 4.1.2, Table 5
291 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
293 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000299
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000300 // Floating-point to integer conversions.
301 // RTABI chapter 4.1.2, Table 6
302 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000310
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000311 // Conversions between floating types.
312 // RTABI chapter 4.1.2, Table 7
313 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000314 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000315 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000316
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000317 // Integer to floating-point conversions.
318 // RTABI chapter 4.1.2, Table 8
319 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000327
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000328 // Long long helper functions
329 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000330 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000334
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000335 // Integer division functions
336 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000337 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000345 };
346
347 for (const auto &LC : LibraryCalls) {
348 setLibcallName(LC.Op, LC.Name);
349 setLibcallCallingConv(LC.Op, LC.CC);
350 if (LC.Cond != ISD::SETCC_INVALID)
351 setCmpLibcallCC(LC.Op, LC.Cond);
352 }
Renato Golin6d435f12015-11-09 12:40:30 +0000353
354 // EABI dependent RTLIB
355 if (TM.Options.EABIVersion == EABI::EABI4 ||
356 TM.Options.EABIVersion == EABI::EABI5) {
357 static const struct {
358 const RTLIB::Libcall Op;
359 const char *const Name;
360 const CallingConv::ID CC;
361 const ISD::CondCode Cond;
362 } MemOpsLibraryCalls[] = {
363 // Memory operations
364 // RTABI chapter 4.3.4
365 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
366 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
367 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
368 };
369
370 for (const auto &LC : MemOpsLibraryCalls) {
371 setLibcallName(LC.Op, LC.Name);
372 setLibcallCallingConv(LC.Op, LC.CC);
373 if (LC.Cond != ISD::SETCC_INVALID)
374 setCmpLibcallCC(LC.Op, LC.Cond);
375 }
376 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000377 }
378
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000379 if (Subtarget->isTargetWindows()) {
380 static const struct {
381 const RTLIB::Libcall Op;
382 const char * const Name;
383 const CallingConv::ID CC;
384 } LibraryCalls[] = {
385 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
386 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
387 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
388 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
389 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
390 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
391 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
392 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
Martell Malonea6b867e2015-11-23 13:11:39 +0000393 { RTLIB::SDIV_I32, "__rt_sdiv", CallingConv::ARM_AAPCS_VFP },
Martell Maloned1229242015-11-26 15:34:03 +0000394 { RTLIB::UDIV_I32, "__rt_udiv", CallingConv::ARM_AAPCS_VFP },
Martell Malonea6b867e2015-11-23 13:11:39 +0000395 { RTLIB::SDIV_I64, "__rt_sdiv64", CallingConv::ARM_AAPCS_VFP },
Martell Maloned1229242015-11-26 15:34:03 +0000396 { RTLIB::UDIV_I64, "__rt_udiv64", CallingConv::ARM_AAPCS_VFP },
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000397 };
398
399 for (const auto &LC : LibraryCalls) {
400 setLibcallName(LC.Op, LC.Name);
401 setLibcallCallingConv(LC.Op, LC.CC);
402 }
403 }
404
Bob Wilsonbc158992011-10-07 16:59:21 +0000405 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000406 if (Subtarget->isTargetWatchOS() ||
407 (Subtarget->isTargetIOS() &&
408 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000409 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
410 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
411 }
412
Oliver Stannard11790b22014-08-11 09:12:32 +0000413 // The half <-> float conversion functions are always soft-float, but are
414 // needed for some targets which use a hard-float calling convention by
415 // default.
416 if (Subtarget->isAAPCS_ABI()) {
417 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
419 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
420 } else {
421 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
422 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
423 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
424 }
425
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000426 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
427 // a __gnu_ prefix (which is the default).
428 if (Subtarget->isTargetAEABI()) {
429 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
430 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
431 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
432 }
433
David Goodwin22c2fba2009-07-08 23:10:31 +0000434 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000435 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000436 else
Craig Topperc7242e02012-04-20 07:30:17 +0000437 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000438 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000439 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000440 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000441 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000442 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000443
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000444 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000445 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000446 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000447 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
448 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
449 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
450 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000451
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000452 setOperationAction(ISD::MULHS, VT, Expand);
453 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
454 setOperationAction(ISD::MULHU, VT, Expand);
455 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000456
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000457 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000458 }
459
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000460 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000461 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000462
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000463 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
464 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
465
Bob Wilson2e076c42009-06-22 23:27:02 +0000466 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000467 addDRTypeForNEON(MVT::v2f32);
468 addDRTypeForNEON(MVT::v8i8);
469 addDRTypeForNEON(MVT::v4i16);
470 addDRTypeForNEON(MVT::v2i32);
471 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000472
Owen Anderson9f944592009-08-11 20:47:22 +0000473 addQRTypeForNEON(MVT::v4f32);
474 addQRTypeForNEON(MVT::v2f64);
475 addQRTypeForNEON(MVT::v16i8);
476 addQRTypeForNEON(MVT::v8i16);
477 addQRTypeForNEON(MVT::v4i32);
478 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000479
Bob Wilson194a2512009-09-15 23:55:57 +0000480 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
481 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000482 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
483 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000484 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
485 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
486 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000487 // FIXME: Code duplication: FDIV and FREM are expanded always, see
488 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000489 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
490 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000491 // FIXME: Create unittest.
492 // In another words, find a way when "copysign" appears in DAG with vector
493 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000494 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000495 // FIXME: Code duplication: SETCC has custom operation action, see
496 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000497 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000498 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000499 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
500 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
501 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
502 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
503 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
504 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
505 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
506 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
507 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
508 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
509 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
510 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000511 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000512 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
513 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
514 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
515 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
516 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000517 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000518
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000519 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
520 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
521 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
522 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
523 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
524 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
525 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
526 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
527 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
528 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000529 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
530 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
531 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
532 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000533 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000534
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000535 // Mark v2f32 intrinsics.
536 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
537 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
538 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
539 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
540 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
541 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
542 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
543 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
544 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
545 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
546 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
547 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
548 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
549 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
550 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
551
Bob Wilson6cc46572009-09-16 00:32:15 +0000552 // Neon does not support some operations on v1i64 and v2i64 types.
553 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000554 // Custom handling for some quad-vector types to detect VMULL.
555 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
556 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
557 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000558 // Custom handling for some vector types to avoid expensive expansions
559 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
560 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
561 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
562 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000563 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
564 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000565 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000566 // a destination type that is wider than the source, and nor does
567 // it have a FP_TO_[SU]INT instruction with a narrower destination than
568 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000569 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
570 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000571 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
572 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000573
Eli Friedmane6385e62012-11-15 22:44:27 +0000574 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000575 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000576
Evan Chengb4eae132012-12-04 22:41:50 +0000577 // NEON does not have single instruction CTPOP for vectors with element
578 // types wider than 8-bits. However, custom lowering can leverage the
579 // v8i8/v16i8 vcnt instruction.
580 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
581 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
582 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
583 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
584
Logan Chien0a43abc2015-07-13 15:37:30 +0000585 // NEON does not have single instruction CTTZ for vectors.
586 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
587 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
588 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
589 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
590
591 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
592 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
593 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
594 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
595
596 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
597 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
598 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
600
601 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
602 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
603 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
604 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
605
Jim Grosbach5f215872013-02-27 21:31:12 +0000606 // NEON only has FMA instructions as of VFP4.
607 if (!Subtarget->hasVFP4()) {
608 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
609 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
610 }
611
Bob Wilson06fce872011-02-07 17:43:21 +0000612 setTargetDAGCombine(ISD::INTRINSIC_VOID);
613 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000614 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
615 setTargetDAGCombine(ISD::SHL);
616 setTargetDAGCombine(ISD::SRL);
617 setTargetDAGCombine(ISD::SRA);
618 setTargetDAGCombine(ISD::SIGN_EXTEND);
619 setTargetDAGCombine(ISD::ZERO_EXTEND);
620 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000621 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000622 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000623 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
624 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000625 setTargetDAGCombine(ISD::FP_TO_SINT);
626 setTargetDAGCombine(ISD::FP_TO_UINT);
627 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000628 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000629
James Molloy547d4c02012-02-20 09:24:05 +0000630 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000631 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
632 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000633 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000634 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
635 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
636 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000637 }
James Molloy547d4c02012-02-20 09:24:05 +0000638 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000639 }
640
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000641 // ARM and Thumb2 support UMLAL/SMLAL.
642 if (!Subtarget->isThumb1Only())
643 setTargetDAGCombine(ISD::ADDC);
644
Oliver Stannard51b1d462014-08-21 12:50:31 +0000645 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000646 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000647 // operations, f64 is legal for the few double-precision instructions which
648 // are present However, no double-precision operations other than moves,
649 // loads and stores are provided by the hardware.
650 setOperationAction(ISD::FADD, MVT::f64, Expand);
651 setOperationAction(ISD::FSUB, MVT::f64, Expand);
652 setOperationAction(ISD::FMUL, MVT::f64, Expand);
653 setOperationAction(ISD::FMA, MVT::f64, Expand);
654 setOperationAction(ISD::FDIV, MVT::f64, Expand);
655 setOperationAction(ISD::FREM, MVT::f64, Expand);
656 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
657 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
658 setOperationAction(ISD::FNEG, MVT::f64, Expand);
659 setOperationAction(ISD::FABS, MVT::f64, Expand);
660 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
661 setOperationAction(ISD::FSIN, MVT::f64, Expand);
662 setOperationAction(ISD::FCOS, MVT::f64, Expand);
663 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
664 setOperationAction(ISD::FPOW, MVT::f64, Expand);
665 setOperationAction(ISD::FLOG, MVT::f64, Expand);
666 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
667 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
668 setOperationAction(ISD::FEXP, MVT::f64, Expand);
669 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
670 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
671 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
672 setOperationAction(ISD::FRINT, MVT::f64, Expand);
673 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
674 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000675 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
676 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
677 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
678 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
679 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
680 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000681 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
682 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
683 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000684
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000685 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000686
Tim Northover4e80b582014-07-18 13:01:19 +0000687 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000688 for (MVT VT : MVT::fp_valuetypes()) {
689 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
690 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
691 }
Tim Northover4e80b582014-07-18 13:01:19 +0000692
693 // ... or truncating stores
694 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
695 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
696 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000697
Duncan Sands95d46ef2008-01-23 20:39:46 +0000698 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000699 for (MVT VT : MVT::integer_valuetypes())
700 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000701
Evan Cheng10043e22007-01-19 07:51:42 +0000702 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000703 if (!Subtarget->isThumb1Only()) {
704 for (unsigned im = (unsigned)ISD::PRE_INC;
705 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000706 setIndexedLoadAction(im, MVT::i1, Legal);
707 setIndexedLoadAction(im, MVT::i8, Legal);
708 setIndexedLoadAction(im, MVT::i16, Legal);
709 setIndexedLoadAction(im, MVT::i32, Legal);
710 setIndexedStoreAction(im, MVT::i1, Legal);
711 setIndexedStoreAction(im, MVT::i8, Legal);
712 setIndexedStoreAction(im, MVT::i16, Legal);
713 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000714 }
Evan Cheng10043e22007-01-19 07:51:42 +0000715 }
716
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000717 setOperationAction(ISD::SADDO, MVT::i32, Custom);
718 setOperationAction(ISD::UADDO, MVT::i32, Custom);
719 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
720 setOperationAction(ISD::USUBO, MVT::i32, Custom);
721
Evan Cheng10043e22007-01-19 07:51:42 +0000722 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000723 setOperationAction(ISD::MUL, MVT::i64, Expand);
724 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000725 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000726 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
727 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000728 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000729 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000730 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000731 setOperationAction(ISD::MULHS, MVT::i32, Expand);
732
Jim Grosbach5d994042009-10-31 19:38:01 +0000733 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000734 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000735 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000736 setOperationAction(ISD::SRL, MVT::i64, Custom);
737 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000738
Evan Chenge8916542011-08-30 01:34:54 +0000739 if (!Subtarget->isThumb1Only()) {
740 // FIXME: We should do this for Thumb1 as well.
741 setOperationAction(ISD::ADDC, MVT::i32, Custom);
742 setOperationAction(ISD::ADDE, MVT::i32, Custom);
743 setOperationAction(ISD::SUBC, MVT::i32, Custom);
744 setOperationAction(ISD::SUBE, MVT::i32, Custom);
745 }
746
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000747 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000748 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
749
Evan Cheng10043e22007-01-19 07:51:42 +0000750 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000751 setOperationAction(ISD::ROTL, MVT::i32, Expand);
752 for (MVT VT : MVT::vector_valuetypes()) {
753 setOperationAction(ISD::ROTL, VT, Expand);
754 setOperationAction(ISD::ROTR, VT, Expand);
755 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000756 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000757 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000758 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000759 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000760
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000761 // These just redirect to CTTZ and CTLZ on ARM.
762 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
763 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
764
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000765 // @llvm.readcyclecounter requires the Performance Monitors extension.
766 // Default to the 0 expansion on unsupported platforms.
767 // FIXME: Technically there are older ARM CPUs that have
768 // implementation-specific ways of obtaining this information.
769 if (Subtarget->hasPerfMon())
770 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000771
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000772 // Only ARMv6 has BSWAP.
773 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000774 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000775
Bradley Smith519563e2016-01-15 10:25:35 +0000776 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide()
777 : Subtarget->hasDivideInARMMode();
778 if (!hasDivide) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000779 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000780 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
781 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000782 }
Renato Golin87610692013-07-16 09:32:17 +0000783
Chad Rosierad7c9102014-08-23 18:29:43 +0000784 setOperationAction(ISD::SREM, MVT::i32, Expand);
785 setOperationAction(ISD::UREM, MVT::i32, Expand);
786 // Register based DivRem for AEABI (RTABI 4.2)
Renato Golin6027dd38e2016-02-03 16:10:54 +0000787 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
788 Subtarget->isTargetGNUAEABI()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000789 setOperationAction(ISD::SREM, MVT::i64, Custom);
790 setOperationAction(ISD::UREM, MVT::i64, Custom);
791
Chad Rosierad7c9102014-08-23 18:29:43 +0000792 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
793 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
794 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
795 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
796 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
797 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
798 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
799 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
800
801 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
802 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
803 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
804 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
805 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
806 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
807 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
808 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
809
810 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
811 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Renato Golin175c6d62016-03-04 19:19:36 +0000812 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
813 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Chad Rosierad7c9102014-08-23 18:29:43 +0000814 } else {
Renato Golin87610692013-07-16 09:32:17 +0000815 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
816 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
817 }
Bob Wilson7117a912009-03-20 22:42:55 +0000818
Owen Anderson9f944592009-08-11 20:47:22 +0000819 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
820 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000821 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000822 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000823
Evan Cheng74d92c12011-04-08 21:37:21 +0000824 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000825
Evan Cheng10043e22007-01-19 07:51:42 +0000826 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000827 setOperationAction(ISD::VASTART, MVT::Other, Custom);
828 setOperationAction(ISD::VAARG, MVT::Other, Expand);
829 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
830 setOperationAction(ISD::VAEND, MVT::Other, Expand);
831 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
832 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000833
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000834 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
835 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
836 else
837 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
838
Evan Cheng6e809de2010-08-11 06:22:01 +0000839 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000840 // the default expansion. If we are targeting a single threaded system,
841 // then set them all for expand so we can lower them later into their
842 // non-atomic form.
James Y Knightf44fc522016-03-16 22:12:04 +0000843 InsertFencesForAtomic = false;
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000844 if (TM.Options.ThreadModel == ThreadModel::Single)
845 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Bradley Smith433c22e2016-01-15 10:26:51 +0000846 else if (Subtarget->hasAnyDataBarrier() && (!Subtarget->isThumb() ||
847 Subtarget->hasV8MBaselineOps())) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000848 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
849 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000850 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000851
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000852 // On v8, we have particularly efficient implementations of atomic fences
853 // if they can be combined with nearby atomic loads and stores.
854 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000855 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
James Y Knightf44fc522016-03-16 22:12:04 +0000856 InsertFencesForAtomic = true;
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000857 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000858 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000859 // If there's anything we can use as a barrier, go through custom lowering
860 // for ATOMIC_FENCE.
861 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
862 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
863
Jim Grosbach6860bb72010-06-18 22:35:32 +0000864 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000865 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000866 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000867 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000868 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000869 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000870 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000871 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000872 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000873 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000874 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000875 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000876 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000877 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
878 // Unordered/Monotonic case.
879 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
880 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000881 }
Evan Cheng10043e22007-01-19 07:51:42 +0000882
Evan Cheng21acf9f2010-11-04 05:19:35 +0000883 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000884
Eli Friedman8cfa7712010-06-26 04:36:50 +0000885 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
886 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000887 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
888 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000889 }
Owen Anderson9f944592009-08-11 20:47:22 +0000890 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000891
Eric Christopher824f42f2015-05-12 01:26:05 +0000892 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000893 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000894 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000895 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000896 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000897 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
898 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000899
900 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000901 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000902 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
903 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
904 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +0000905 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +0000906 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000907
Owen Anderson9f944592009-08-11 20:47:22 +0000908 setOperationAction(ISD::SETCC, MVT::i32, Expand);
909 setOperationAction(ISD::SETCC, MVT::f32, Expand);
910 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000911 setOperationAction(ISD::SELECT, MVT::i32, Custom);
912 setOperationAction(ISD::SELECT, MVT::f32, Custom);
913 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000914 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
915 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
916 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000917
Owen Anderson9f944592009-08-11 20:47:22 +0000918 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
919 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
920 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
921 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
922 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000923
Dan Gohman482732a2007-10-11 23:21:31 +0000924 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000925 setOperationAction(ISD::FSIN, MVT::f64, Expand);
926 setOperationAction(ISD::FSIN, MVT::f32, Expand);
927 setOperationAction(ISD::FCOS, MVT::f32, Expand);
928 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000929 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
930 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000931 setOperationAction(ISD::FREM, MVT::f64, Expand);
932 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000933 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000934 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000935 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
936 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000937 }
Owen Anderson9f944592009-08-11 20:47:22 +0000938 setOperationAction(ISD::FPOW, MVT::f64, Expand);
939 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000940
Evan Chengd0007f32012-04-10 21:40:28 +0000941 if (!Subtarget->hasVFP4()) {
942 setOperationAction(ISD::FMA, MVT::f64, Expand);
943 setOperationAction(ISD::FMA, MVT::f32, Expand);
944 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000945
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000946 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000947 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000948 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
949 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000950 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
951 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
952 }
953
954 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000955 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000956 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
957 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000958 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000959 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000960
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000961 // Combine sin / cos into one node or libcall if possible.
962 if (Subtarget->hasSinCos()) {
963 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
964 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover042a6c12016-01-27 19:32:29 +0000965 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000966 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
967 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
968 }
969 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000970 // For iOS, we don't want to the normal expansion of a libcall to
971 // sincos. We want to issue a libcall to __sincos_stret.
972 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
973 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
974 }
975 }
Evan Cheng10043e22007-01-19 07:51:42 +0000976
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000977 // FP-ARMv8 implements a lot of rounding-like FP operations.
978 if (Subtarget->hasFPARMv8()) {
979 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
980 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
981 setOperationAction(ISD::FROUND, MVT::f32, Legal);
982 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
983 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
984 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000985 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
986 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000987 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
988 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
989 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
990 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
991
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000992 if (!Subtarget->isFPOnlySP()) {
993 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
994 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
995 setOperationAction(ISD::FROUND, MVT::f64, Legal);
996 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
997 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
998 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000999 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1000 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001001 }
1002 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001003
James Molloy974838f2015-08-17 19:37:12 +00001004 if (Subtarget->hasNEON()) {
1005 // vmin and vmax aren't available in a scalar form, so we use
1006 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001007 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1008 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001009 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1010 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1011 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1012 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1013 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001014
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001015 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001016 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001017 setTargetDAGCombine(ISD::ADD);
1018 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001019 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001020 setTargetDAGCombine(ISD::AND);
1021 setTargetDAGCombine(ISD::OR);
1022 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001023
Evan Chengf258a152012-02-23 02:58:19 +00001024 if (Subtarget->hasV6Ops())
1025 setTargetDAGCombine(ISD::SRL);
1026
Evan Cheng10043e22007-01-19 07:51:42 +00001027 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001028
Eric Christopher824f42f2015-05-12 01:26:05 +00001029 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001030 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001031 setSchedulingPreference(Sched::RegPressure);
1032 else
1033 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001034
Evan Cheng3ae2b792011-01-06 06:52:41 +00001035 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001036 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001037 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001038 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001039 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001040 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001041 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001042
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001043 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1044 // are at least 4 bytes aligned.
1045 setMinStackArgumentAlignment(4);
1046
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001047 // Prefer likely predicted branches to selects on out-of-order cores.
Junmo Park453f4aa2016-02-23 09:56:58 +00001048 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001049
Eli Friedman2518f832011-05-06 20:34:06 +00001050 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001051}
1052
Eric Christopher824f42f2015-05-12 01:26:05 +00001053bool ARMTargetLowering::useSoftFloat() const {
1054 return Subtarget->useSoftFloat();
1055}
1056
Andrew Trick43f25632011-01-19 02:35:27 +00001057// FIXME: It might make sense to define the representative register class as the
1058// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1059// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1060// SPR's representative would be DPR_VFP2. This should work well if register
1061// pressure tracking were modified such that a register use would increment the
1062// pressure of the register class's representative and all of it's super
1063// classes' representatives transitively. We have not implemented this because
1064// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001065// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001066// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001067std::pair<const TargetRegisterClass *, uint8_t>
1068ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1069 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001070 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001071 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001072 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001073 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001074 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001075 // Use DPR as representative register class for all floating point
1076 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1077 // the cost is 1 for both f32 and f64.
1078 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001079 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001080 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001081 // When NEON is used for SP, only half of the register file is available
1082 // because operations that define both SP and DP results will be constrained
1083 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1084 // coalescing by double-counting the SP regs. See the FIXME above.
1085 if (Subtarget->useNEONForSinglePrecisionFP())
1086 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001087 break;
1088 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1089 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001090 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001091 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001092 break;
1093 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001094 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001095 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001096 break;
1097 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001098 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001099 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001100 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001101 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001102 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001103}
1104
Evan Cheng10043e22007-01-19 07:51:42 +00001105const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001106 switch ((ARMISD::NodeType)Opcode) {
1107 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001108 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001109 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001110 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001111 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001112 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001113 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001114 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1115 case ARMISD::tCALL: return "ARMISD::tCALL";
1116 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1117 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001118 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001119 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001120 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001121 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1122 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001123 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001124 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001125 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1126 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001127 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001128 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001129
Evan Cheng10043e22007-01-19 07:51:42 +00001130 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001131
Evan Cheng10043e22007-01-19 07:51:42 +00001132 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1133 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1134 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001135
Evan Chenge8916542011-08-30 01:34:54 +00001136 case ARMISD::ADDC: return "ARMISD::ADDC";
1137 case ARMISD::ADDE: return "ARMISD::ADDE";
1138 case ARMISD::SUBC: return "ARMISD::SUBC";
1139 case ARMISD::SUBE: return "ARMISD::SUBE";
1140
Bob Wilson22806742010-09-22 22:09:21 +00001141 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1142 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001143
Evan Chengec6d7c92009-10-28 06:55:03 +00001144 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001145 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1146 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001147
Dale Johannesend679ff72010-06-03 21:09:53 +00001148 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001149
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001150 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001151
Evan Chengb972e562009-08-07 00:34:42 +00001152 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1153
Bob Wilson7ed59712010-10-30 00:54:37 +00001154 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001155
Evan Cheng8740ee32010-11-03 06:34:55 +00001156 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1157
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001158 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001159 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001160
Bob Wilson2e076c42009-06-22 23:27:02 +00001161 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001162 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001163 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001164 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1165 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001166 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1167 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001168 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1169 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001170 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1171 case ARMISD::VTST: return "ARMISD::VTST";
1172
1173 case ARMISD::VSHL: return "ARMISD::VSHL";
1174 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1175 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001176 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1177 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1178 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1179 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1180 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1181 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1182 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1183 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1184 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1185 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1186 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1187 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001188 case ARMISD::VSLI: return "ARMISD::VSLI";
1189 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001190 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1191 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001192 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001193 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001194 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001195 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001196 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001197 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001198 case ARMISD::VREV64: return "ARMISD::VREV64";
1199 case ARMISD::VREV32: return "ARMISD::VREV32";
1200 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001201 case ARMISD::VZIP: return "ARMISD::VZIP";
1202 case ARMISD::VUZP: return "ARMISD::VUZP";
1203 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001204 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1205 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001206 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1207 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001208 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1209 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001210 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001211 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001212 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1213 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001214 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001215 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001216 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1217 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1218 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001219 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1220 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1221 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1222 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1223 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1224 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1225 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1226 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1227 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1228 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1229 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1230 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1231 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1232 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1233 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1234 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1235 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001236 }
Matthias Braund04893f2015-05-07 21:33:59 +00001237 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001238}
1239
Mehdi Amini44ede332015-07-09 02:09:04 +00001240EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1241 EVT VT) const {
1242 if (!VT.isVector())
1243 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001244 return VT.changeVectorElementTypeToInteger();
1245}
1246
Evan Cheng4cad68e2010-05-15 02:18:07 +00001247/// getRegClassFor - Return the register class that should be used for the
1248/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001249const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001250 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1251 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1252 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001253 if (Subtarget->hasNEON()) {
1254 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001255 return &ARM::QQPRRegClass;
1256 if (VT == MVT::v8i64)
1257 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001258 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001259 return TargetLowering::getRegClassFor(VT);
1260}
1261
John Brawn0dbcd652015-03-18 12:01:59 +00001262// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1263// source/dest is aligned and the copy size is large enough. We therefore want
1264// to align such objects passed to memory intrinsics.
1265bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1266 unsigned &PrefAlign) const {
1267 if (!isa<MemIntrinsic>(CI))
1268 return false;
1269 MinSize = 8;
1270 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1271 // cycle faster than 4-byte aligned LDM.
1272 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1273 return true;
1274}
1275
Eric Christopher84bdfd82010-07-21 22:26:11 +00001276// Create a fast isel object.
1277FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001278ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1279 const TargetLibraryInfo *libInfo) const {
1280 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001281}
1282
Evan Cheng4401f882010-05-20 23:26:43 +00001283Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001284 unsigned NumVals = N->getNumValues();
1285 if (!NumVals)
1286 return Sched::RegPressure;
1287
1288 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001289 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001290 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001291 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001292 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001293 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001294 }
Evan Chengbf914992010-05-28 23:25:23 +00001295
1296 if (!N->isMachineOpcode())
1297 return Sched::RegPressure;
1298
1299 // Load are scheduled for latency even if there instruction itinerary
1300 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001301 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001302 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001303
Evan Cheng6cc775f2011-06-28 19:10:37 +00001304 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001305 return Sched::RegPressure;
1306 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001307 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001308 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001309
Evan Cheng4401f882010-05-20 23:26:43 +00001310 return Sched::RegPressure;
1311}
1312
Evan Cheng10043e22007-01-19 07:51:42 +00001313//===----------------------------------------------------------------------===//
1314// Lowering Code
1315//===----------------------------------------------------------------------===//
1316
Evan Cheng10043e22007-01-19 07:51:42 +00001317/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1318static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1319 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001320 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001321 case ISD::SETNE: return ARMCC::NE;
1322 case ISD::SETEQ: return ARMCC::EQ;
1323 case ISD::SETGT: return ARMCC::GT;
1324 case ISD::SETGE: return ARMCC::GE;
1325 case ISD::SETLT: return ARMCC::LT;
1326 case ISD::SETLE: return ARMCC::LE;
1327 case ISD::SETUGT: return ARMCC::HI;
1328 case ISD::SETUGE: return ARMCC::HS;
1329 case ISD::SETULT: return ARMCC::LO;
1330 case ISD::SETULE: return ARMCC::LS;
1331 }
1332}
1333
Bob Wilsona2e83332009-09-09 23:14:54 +00001334/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1335static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001336 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001337 CondCode2 = ARMCC::AL;
1338 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001339 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001340 case ISD::SETEQ:
1341 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1342 case ISD::SETGT:
1343 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1344 case ISD::SETGE:
1345 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1346 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001347 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001348 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1349 case ISD::SETO: CondCode = ARMCC::VC; break;
1350 case ISD::SETUO: CondCode = ARMCC::VS; break;
1351 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1352 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1353 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1354 case ISD::SETLT:
1355 case ISD::SETULT: CondCode = ARMCC::LT; break;
1356 case ISD::SETLE:
1357 case ISD::SETULE: CondCode = ARMCC::LE; break;
1358 case ISD::SETNE:
1359 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1360 }
Evan Cheng10043e22007-01-19 07:51:42 +00001361}
1362
Bob Wilsona4c22902009-04-17 19:07:39 +00001363//===----------------------------------------------------------------------===//
1364// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001365//===----------------------------------------------------------------------===//
1366
1367#include "ARMGenCallingConv.inc"
1368
Oliver Stannardc24f2172014-05-09 14:01:47 +00001369/// getEffectiveCallingConv - Get the effective calling convention, taking into
1370/// account presence of floating point hardware and calling convention
1371/// limitations, such as support for variadic functions.
1372CallingConv::ID
1373ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1374 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001375 switch (CC) {
1376 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001377 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001378 case CallingConv::ARM_AAPCS:
1379 case CallingConv::ARM_APCS:
1380 case CallingConv::GHC:
1381 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001382 case CallingConv::PreserveMost:
1383 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001384 case CallingConv::ARM_AAPCS_VFP:
1385 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1386 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001387 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001388 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001389 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001390 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1391 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001392 return CallingConv::ARM_AAPCS_VFP;
1393 else
1394 return CallingConv::ARM_AAPCS;
1395 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001396 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001397 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001398 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001399 return CallingConv::Fast;
1400 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001401 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001402 return CallingConv::ARM_AAPCS_VFP;
1403 else
1404 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001405 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001406}
1407
1408/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1409/// CallingConvention.
1410CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1411 bool Return,
1412 bool isVarArg) const {
1413 switch (getEffectiveCallingConv(CC, isVarArg)) {
1414 default:
1415 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001416 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001417 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001418 case CallingConv::ARM_AAPCS:
1419 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1420 case CallingConv::ARM_AAPCS_VFP:
1421 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1422 case CallingConv::Fast:
1423 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001424 case CallingConv::GHC:
1425 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001426 case CallingConv::PreserveMost:
1427 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001428 }
1429}
1430
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001431/// LowerCallResult - Lower the result values of a call into the
1432/// appropriate copies out of appropriate physical registers.
1433SDValue
1434ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001435 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001436 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001437 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001438 SmallVectorImpl<SDValue> &InVals,
1439 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001440
Bob Wilsona4c22902009-04-17 19:07:39 +00001441 // Assign locations to each value returned by this call.
1442 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001443 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1444 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001445 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001446 CCAssignFnForNode(CallConv, /* Return*/ true,
1447 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001448
1449 // Copy all of the result registers out of their specified physreg.
1450 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1451 CCValAssign VA = RVLocs[i];
1452
Stephen Linb8bd2322013-04-20 05:14:40 +00001453 // Pass 'this' value directly from the argument to return value, to avoid
1454 // reg unit interference
1455 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001456 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1457 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001458 InVals.push_back(ThisVal);
1459 continue;
1460 }
1461
Bob Wilson0041bd32009-04-25 00:33:20 +00001462 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001463 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001464 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001465 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001466 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001467 Chain = Lo.getValue(1);
1468 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001469 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001470 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001471 InFlag);
1472 Chain = Hi.getValue(1);
1473 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001474 if (!Subtarget->isLittle())
1475 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001476 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001477
Owen Anderson9f944592009-08-11 20:47:22 +00001478 if (VA.getLocVT() == MVT::v2f64) {
1479 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1480 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001481 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001482
1483 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001484 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001485 Chain = Lo.getValue(1);
1486 InFlag = Lo.getValue(2);
1487 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001488 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001489 Chain = Hi.getValue(1);
1490 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001491 if (!Subtarget->isLittle())
1492 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001493 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001494 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001495 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001496 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001497 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001498 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1499 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001500 Chain = Val.getValue(1);
1501 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001502 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001503
1504 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001505 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001506 case CCValAssign::Full: break;
1507 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001508 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001509 break;
1510 }
1511
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001512 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001513 }
1514
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001515 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001516}
1517
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001518/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001519SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001520ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1521 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001522 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001523 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001524 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001525 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001526 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001527 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1528 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001529 return DAG.getStore(
1530 Chain, dl, Arg, PtrOff,
1531 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1532 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001533}
1534
Andrew Trickef9de2a2013-05-25 02:42:55 +00001535void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001536 SDValue Chain, SDValue &Arg,
1537 RegsToPassVector &RegsToPass,
1538 CCValAssign &VA, CCValAssign &NextVA,
1539 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001540 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001541 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001542
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001543 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001544 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001545 unsigned id = Subtarget->isLittle() ? 0 : 1;
1546 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001547
1548 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001549 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001550 else {
1551 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001552 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001553 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1554 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001555
Christian Pirkerb5728192014-05-08 14:06:24 +00001556 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001557 dl, DAG, NextVA,
1558 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001559 }
1560}
1561
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001562/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001563/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1564/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001565SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001566ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001567 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001568 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001569 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001570 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1571 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1572 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001573 SDValue Chain = CLI.Chain;
1574 SDValue Callee = CLI.Callee;
1575 bool &isTailCall = CLI.IsTailCall;
1576 CallingConv::ID CallConv = CLI.CallConv;
1577 bool doesNotRet = CLI.DoesNotReturn;
1578 bool isVarArg = CLI.IsVarArg;
1579
Dale Johannesend679ff72010-06-03 21:09:53 +00001580 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001581 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1582 bool isThisReturn = false;
1583 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001584 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001585
Bob Wilson8decdc42011-10-07 17:17:49 +00001586 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001587 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001588 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001589
Dale Johannesend679ff72010-06-03 21:09:53 +00001590 if (isTailCall) {
1591 // Check if it's really possible to do a tail call.
1592 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001593 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001594 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001595 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1596 report_fatal_error("failed to perform tail call elimination on a call "
1597 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001598 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1599 // detected sibcalls.
1600 if (isTailCall) {
1601 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001602 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001603 }
1604 }
Evan Cheng10043e22007-01-19 07:51:42 +00001605
Bob Wilsona4c22902009-04-17 19:07:39 +00001606 // Analyze operands of the call, assigning locations to each operand.
1607 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001608 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1609 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001610 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001611 CCAssignFnForNode(CallConv, /* Return*/ false,
1612 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001613
Bob Wilsona4c22902009-04-17 19:07:39 +00001614 // Get a count of how many bytes are to be pushed on the stack.
1615 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001616
Dale Johannesend679ff72010-06-03 21:09:53 +00001617 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001618 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001619 NumBytes = 0;
1620
Evan Cheng10043e22007-01-19 07:51:42 +00001621 // Adjust the stack pointer for the new arguments...
1622 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001623 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001624 Chain = DAG.getCALLSEQ_START(Chain,
1625 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001626
Mehdi Amini44ede332015-07-09 02:09:04 +00001627 SDValue StackPtr =
1628 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001629
Bob Wilson2e076c42009-06-22 23:27:02 +00001630 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001631 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001632
Bob Wilsona4c22902009-04-17 19:07:39 +00001633 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001634 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001635 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1636 i != e;
1637 ++i, ++realArgIdx) {
1638 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001639 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001640 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001641 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001642
Bob Wilsona4c22902009-04-17 19:07:39 +00001643 // Promote the value if needed.
1644 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001645 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001646 case CCValAssign::Full: break;
1647 case CCValAssign::SExt:
1648 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1649 break;
1650 case CCValAssign::ZExt:
1651 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1652 break;
1653 case CCValAssign::AExt:
1654 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1655 break;
1656 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001657 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001658 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001659 }
1660
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001661 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001662 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001663 if (VA.getLocVT() == MVT::v2f64) {
1664 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001665 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001666 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001667 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001668
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001669 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001670 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1671
1672 VA = ArgLocs[++i]; // skip ahead to next loc
1673 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001674 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001675 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1676 } else {
1677 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001678
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001679 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1680 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001681 }
1682 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001683 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001684 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001685 }
1686 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001687 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1688 assert(VA.getLocVT() == MVT::i32 &&
1689 "unexpected calling convention register assignment");
1690 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001691 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001692 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001693 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001694 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001695 } else if (isByVal) {
1696 assert(VA.isMemLoc());
1697 unsigned offset = 0;
1698
1699 // True if this byval aggregate will be split between registers
1700 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001701 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001702 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001703
1704 if (CurByValIdx < ByValArgsCount) {
1705
1706 unsigned RegBegin, RegEnd;
1707 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1708
Mehdi Amini44ede332015-07-09 02:09:04 +00001709 EVT PtrVT =
1710 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001711 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001712 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001713 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001714 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1715 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1716 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001717 false, false, false,
1718 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001719 MemOpChains.push_back(Load.getValue(1));
1720 RegsToPass.push_back(std::make_pair(j, Load));
1721 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001722
1723 // If parameter size outsides register area, "offset" value
1724 // helps us to calculate stack slot for remained part properly.
1725 offset = RegEnd - RegBegin;
1726
1727 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001728 }
1729
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001730 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001731 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001732 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001733 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001734 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001735 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001736 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001737 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001738 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001739 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1740 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001741
Manman Ren9f911162012-06-01 02:44:42 +00001742 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001743 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001744 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001745 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001746 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001747 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001748 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001749
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001750 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1751 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001752 }
Evan Cheng10043e22007-01-19 07:51:42 +00001753 }
1754
1755 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001756 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001757
1758 // Build a sequence of copy-to-reg nodes chained together with token chain
1759 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001760 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001761 // Tail call byval lowering might overwrite argument registers so in case of
1762 // tail call optimization the copies to registers are lowered later.
1763 if (!isTailCall)
1764 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1765 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1766 RegsToPass[i].second, InFlag);
1767 InFlag = Chain.getValue(1);
1768 }
Evan Cheng10043e22007-01-19 07:51:42 +00001769
Dale Johannesend679ff72010-06-03 21:09:53 +00001770 // For tail calls lower the arguments to the 'real' stack slot.
1771 if (isTailCall) {
1772 // Force all the incoming stack arguments to be loaded from the stack
1773 // before any new outgoing arguments are stored to the stack, because the
1774 // outgoing stack slots may alias the incoming argument stack slots, and
1775 // the alias isn't otherwise explicit. This is slightly more conservative
1776 // than necessary, because it means that each store effectively depends
1777 // on every argument instead of just those arguments it would clobber.
1778
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001779 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001780 InFlag = SDValue();
1781 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1782 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1783 RegsToPass[i].second, InFlag);
1784 InFlag = Chain.getValue(1);
1785 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001786 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001787 }
1788
Bill Wendling24c79f22008-09-16 21:48:12 +00001789 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1790 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1791 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001792 bool isDirect = false;
1793 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001794 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001795 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001796 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001797
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001798 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001799 assert((Subtarget->isTargetWindows() ||
1800 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1801 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001802 // Handle a global address or an external symbol. If it's not one of
1803 // those, the target's already in a register, so we don't need to do
1804 // anything extra.
1805 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001806 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001807 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001808 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001809 ARMConstantPoolValue *CPV =
1810 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1811
Jim Grosbach32bb3622010-04-14 22:28:31 +00001812 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001813 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001814 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001815 Callee = DAG.getLoad(
1816 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1817 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1818 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001819 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1820 const char *Sym = S->getSymbol();
1821
1822 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001823 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001824 ARMConstantPoolValue *CPV =
1825 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1826 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001827 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001828 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001829 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001830 Callee = DAG.getLoad(
1831 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1832 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1833 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001834 }
1835 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001836 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001837 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001838 bool isDef = GV->isStrongDefinitionForLinker();
1839 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001840 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001841 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001842 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001843 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001844 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001845 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001846 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001847 Callee = DAG.getNode(
1848 ARMISD::WrapperPIC, dl, PtrVt,
1849 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1850 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001851 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1852 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001853 } else if (Subtarget->isTargetCOFF()) {
1854 assert(Subtarget->isTargetWindows() &&
1855 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001856 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1857 ? ARMII::MO_DLLIMPORT
1858 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001859 Callee =
1860 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001861 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001862 Callee =
1863 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1864 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001865 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1866 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001867 } else {
1868 // On ELF targets for PIC code, direct calls should go through the PLT
1869 unsigned OpFlags = 0;
1870 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001871 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001872 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001873 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001874 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001875 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001876 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001877 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001878 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001879 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001880 // tBX takes a register source operand.
1881 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001882 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001883 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001884 ARMConstantPoolValue *CPV =
1885 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1886 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001887 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001888 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001889 Callee = DAG.getLoad(
1890 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1891 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1892 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001893 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001894 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001895 } else {
1896 unsigned OpFlags = 0;
1897 // On ELF targets for PIC code, direct calls should go through the PLT
1898 if (Subtarget->isTargetELF() &&
1899 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1900 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001901 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001902 }
Evan Cheng10043e22007-01-19 07:51:42 +00001903 }
1904
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001905 // FIXME: handle tail calls differently.
1906 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001907 if (Subtarget->isThumb()) {
1908 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001909 CallOpc = ARMISD::CALL_NOLINK;
1910 else
1911 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1912 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001913 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001914 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001915 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001916 // Emit regular call when code size is the priority
1917 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001918 // "mov lr, pc; b _foo" to avoid confusing the RSP
1919 CallOpc = ARMISD::CALL_NOLINK;
1920 else
1921 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001922 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001923
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001924 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001925 Ops.push_back(Chain);
1926 Ops.push_back(Callee);
1927
1928 // Add argument registers to the end of the list so that they are known live
1929 // into the call.
1930 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1931 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1932 RegsToPass[i].second.getValueType()));
1933
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001934 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001935 if (!isTailCall) {
1936 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001937 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001938 if (isThisReturn) {
1939 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001940 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001941 if (!Mask) {
1942 // Set isThisReturn to false if the calling convention is not one that
1943 // allows 'returned' to be modeled in this way, so LowerCallResult does
1944 // not try to pass 'this' straight through
1945 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001946 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001947 }
1948 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001949 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001950
Matthias Braunc22630e2013-10-04 16:52:54 +00001951 assert(Mask && "Missing call preserved mask for calling convention");
1952 Ops.push_back(DAG.getRegisterMask(Mask));
1953 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001954
Gabor Greiff304a7a2008-08-28 21:40:38 +00001955 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001956 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001957
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001958 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001959 if (isTailCall) {
1960 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001961 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001962 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001963
Duncan Sands739a0542008-07-02 17:40:58 +00001964 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001965 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001966 InFlag = Chain.getValue(1);
1967
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001968 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1969 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001970 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001971 InFlag = Chain.getValue(1);
1972
Bob Wilsona4c22902009-04-17 19:07:39 +00001973 // Handle result values, copying them out of physregs into vregs that we
1974 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001975 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001976 InVals, isThisReturn,
1977 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001978}
1979
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001980/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001981/// on the stack. Remember the next parameter register to allocate,
1982/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001983/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001984void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1985 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001986 assert((State->getCallOrPrologue() == Prologue ||
1987 State->getCallOrPrologue() == Call) &&
1988 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001989
Tim Northover8cda34f2015-03-11 18:54:22 +00001990 // Byval (as with any stack) slots are always at least 4 byte aligned.
1991 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001992
Tim Northover8cda34f2015-03-11 18:54:22 +00001993 unsigned Reg = State->AllocateReg(GPRArgRegs);
1994 if (!Reg)
1995 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001996
Tim Northover8cda34f2015-03-11 18:54:22 +00001997 unsigned AlignInRegs = Align / 4;
1998 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1999 for (unsigned i = 0; i < Waste; ++i)
2000 Reg = State->AllocateReg(GPRArgRegs);
2001
2002 if (!Reg)
2003 return;
2004
2005 unsigned Excess = 4 * (ARM::R4 - Reg);
2006
2007 // Special case when NSAA != SP and parameter size greater than size of
2008 // all remained GPR regs. In that case we can't split parameter, we must
2009 // send it to stack. We also must set NCRN to R4, so waste all
2010 // remained registers.
2011 const unsigned NSAAOffset = State->getNextStackOffset();
2012 if (NSAAOffset != 0 && Size > Excess) {
2013 while (State->AllocateReg(GPRArgRegs))
2014 ;
2015 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002016 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002017
2018 // First register for byval parameter is the first register that wasn't
2019 // allocated before this method call, so it would be "reg".
2020 // If parameter is small enough to be saved in range [reg, r4), then
2021 // the end (first after last) register would be reg + param-size-in-regs,
2022 // else parameter would be splitted between registers and stack,
2023 // end register would be r4 in this case.
2024 unsigned ByValRegBegin = Reg;
2025 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2026 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2027 // Note, first register is allocated in the beginning of function already,
2028 // allocate remained amount of registers we need.
2029 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2030 State->AllocateReg(GPRArgRegs);
2031 // A byval parameter that is split between registers and memory needs its
2032 // size truncated here.
2033 // In the case where the entire structure fits in registers, we set the
2034 // size in memory to zero.
2035 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002036}
2037
Dale Johannesend679ff72010-06-03 21:09:53 +00002038/// MatchingStackOffset - Return true if the given stack call argument is
2039/// already available in the same position (relatively) of the caller's
2040/// incoming argument stack.
2041static
2042bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2043 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002044 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002045 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2046 int FI = INT_MAX;
2047 if (Arg.getOpcode() == ISD::CopyFromReg) {
2048 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002049 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002050 return false;
2051 MachineInstr *Def = MRI->getVRegDef(VR);
2052 if (!Def)
2053 return false;
2054 if (!Flags.isByVal()) {
2055 if (!TII->isLoadFromStackSlot(Def, FI))
2056 return false;
2057 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002058 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002059 }
2060 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2061 if (Flags.isByVal())
2062 // ByVal argument is passed in as a pointer but it's now being
2063 // dereferenced. e.g.
2064 // define @foo(%struct.X* %A) {
2065 // tail call @bar(%struct.X* byval %A)
2066 // }
2067 return false;
2068 SDValue Ptr = Ld->getBasePtr();
2069 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2070 if (!FINode)
2071 return false;
2072 FI = FINode->getIndex();
2073 } else
2074 return false;
2075
2076 assert(FI != INT_MAX);
2077 if (!MFI->isFixedObjectIndex(FI))
2078 return false;
2079 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2080}
2081
2082/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2083/// for tail call optimization. Targets which want to do tail call
2084/// optimization should implement this function.
2085bool
2086ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2087 CallingConv::ID CalleeCC,
2088 bool isVarArg,
2089 bool isCalleeStructRet,
2090 bool isCallerStructRet,
2091 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002092 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002093 const SmallVectorImpl<ISD::InputArg> &Ins,
2094 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002095 const Function *CallerF = DAG.getMachineFunction().getFunction();
2096 CallingConv::ID CallerCC = CallerF->getCallingConv();
2097 bool CCMatch = CallerCC == CalleeCC;
2098
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002099 assert(Subtarget->supportsTailCall());
2100
Dale Johannesend679ff72010-06-03 21:09:53 +00002101 // Look for obvious safe cases to perform tail call optimization that do not
2102 // require ABI changes. This is what gcc calls sibcall.
2103
Jim Grosbache3864cc2010-06-16 23:45:49 +00002104 // Do not sibcall optimize vararg calls unless the call site is not passing
2105 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002106 if (isVarArg && !Outs.empty())
2107 return false;
2108
Tim Northoverd8407452013-10-01 14:33:28 +00002109 // Exception-handling functions need a special set of instructions to indicate
2110 // a return to the hardware. Tail-calling another function would probably
2111 // break this.
2112 if (CallerF->hasFnAttribute("interrupt"))
2113 return false;
2114
Dale Johannesend679ff72010-06-03 21:09:53 +00002115 // Also avoid sibcall optimization if either caller or callee uses struct
2116 // return semantics.
2117 if (isCalleeStructRet || isCallerStructRet)
2118 return false;
2119
Oliver Stannard12993dd2014-08-18 12:42:15 +00002120 // Externally-defined functions with weak linkage should not be
2121 // tail-called on ARM when the OS does not support dynamic
2122 // pre-emption of symbols, as the AAELF spec requires normal calls
2123 // to undefined weak functions to be replaced with a NOP or jump to the
2124 // next instruction. The behaviour of branch instructions in this
2125 // situation (as used for tail calls) is implementation-defined, so we
2126 // cannot rely on the linker replacing the tail call with a return.
2127 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2128 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002129 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002130 if (GV->hasExternalWeakLinkage() &&
2131 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002132 return false;
2133 }
2134
Dale Johannesend679ff72010-06-03 21:09:53 +00002135 // If the calling conventions do not match, then we'd better make sure the
2136 // results are returned in the same way as what the caller expects.
2137 if (!CCMatch) {
2138 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002139 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2140 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002141 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2142
2143 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002144 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2145 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002146 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2147
2148 if (RVLocs1.size() != RVLocs2.size())
2149 return false;
2150 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2151 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2152 return false;
2153 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2154 return false;
2155 if (RVLocs1[i].isRegLoc()) {
2156 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2157 return false;
2158 } else {
2159 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2160 return false;
2161 }
2162 }
2163 }
2164
Manman Ren7e48b252012-10-12 23:39:43 +00002165 // If Caller's vararg or byval argument has been split between registers and
2166 // stack, do not perform tail call, since part of the argument is in caller's
2167 // local frame.
2168 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2169 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002170 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002171 return false;
2172
Dale Johannesend679ff72010-06-03 21:09:53 +00002173 // If the callee takes no arguments then go on to check the results of the
2174 // call.
2175 if (!Outs.empty()) {
2176 // Check if stack adjustment is needed. For now, do not do this if any
2177 // argument is passed on the stack.
2178 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002179 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2180 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002181 CCInfo.AnalyzeCallOperands(Outs,
2182 CCAssignFnForNode(CalleeCC, false, isVarArg));
2183 if (CCInfo.getNextStackOffset()) {
2184 MachineFunction &MF = DAG.getMachineFunction();
2185
2186 // Check if the arguments are already laid out in the right way as
2187 // the caller's fixed stack objects.
2188 MachineFrameInfo *MFI = MF.getFrameInfo();
2189 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002190 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002191 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2192 i != e;
2193 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002194 CCValAssign &VA = ArgLocs[i];
2195 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002196 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002197 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002198 if (VA.getLocInfo() == CCValAssign::Indirect)
2199 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002200 if (VA.needsCustom()) {
2201 // f64 and vector types are split into multiple registers or
2202 // register/stack-slot combinations. The types will not match
2203 // the registers; give up on memory f64 refs until we figure
2204 // out what to do about this.
2205 if (!VA.isRegLoc())
2206 return false;
2207 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002208 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002209 if (RegVT == MVT::v2f64) {
2210 if (!ArgLocs[++i].isRegLoc())
2211 return false;
2212 if (!ArgLocs[++i].isRegLoc())
2213 return false;
2214 }
2215 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002216 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2217 MFI, MRI, TII))
2218 return false;
2219 }
2220 }
2221 }
2222 }
2223
2224 return true;
2225}
2226
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002227bool
2228ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2229 MachineFunction &MF, bool isVarArg,
2230 const SmallVectorImpl<ISD::OutputArg> &Outs,
2231 LLVMContext &Context) const {
2232 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002233 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002234 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2235 isVarArg));
2236}
2237
Tim Northoverd8407452013-10-01 14:33:28 +00002238static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2239 SDLoc DL, SelectionDAG &DAG) {
2240 const MachineFunction &MF = DAG.getMachineFunction();
2241 const Function *F = MF.getFunction();
2242
2243 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2244
2245 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2246 // version of the "preferred return address". These offsets affect the return
2247 // instruction if this is a return from PL1 without hypervisor extensions.
2248 // IRQ/FIQ: +4 "subs pc, lr, #4"
2249 // SWI: 0 "subs pc, lr, #0"
2250 // ABORT: +4 "subs pc, lr, #4"
2251 // UNDEF: +4/+2 "subs pc, lr, #0"
2252 // UNDEF varies depending on where the exception came from ARM or Thumb
2253 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2254
2255 int64_t LROffset;
2256 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2257 IntKind == "ABORT")
2258 LROffset = 4;
2259 else if (IntKind == "SWI" || IntKind == "UNDEF")
2260 LROffset = 0;
2261 else
2262 report_fatal_error("Unsupported interrupt attribute. If present, value "
2263 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2264
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002265 RetOps.insert(RetOps.begin() + 1,
2266 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002267
Craig Topper48d114b2014-04-26 18:35:24 +00002268 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002269}
2270
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002271SDValue
2272ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002273 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002274 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002275 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002276 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002277
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002278 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002279 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002280
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002281 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002282 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2283 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002284
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002285 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002286 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2287 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002288
Bob Wilsona4c22902009-04-17 19:07:39 +00002289 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002290 SmallVector<SDValue, 4> RetOps;
2291 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002292 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002293
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002294 MachineFunction &MF = DAG.getMachineFunction();
2295 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2296 AFI->setReturnRegsCount(RVLocs.size());
2297
Bob Wilsona4c22902009-04-17 19:07:39 +00002298 // Copy the result values into the output registers.
2299 for (unsigned i = 0, realRVLocIdx = 0;
2300 i != RVLocs.size();
2301 ++i, ++realRVLocIdx) {
2302 CCValAssign &VA = RVLocs[i];
2303 assert(VA.isRegLoc() && "Can only return in registers!");
2304
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002305 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002306
2307 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002308 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002309 case CCValAssign::Full: break;
2310 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002311 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002312 break;
2313 }
2314
Bob Wilsona4c22902009-04-17 19:07:39 +00002315 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002316 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002317 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002318 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002319 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002320 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002321 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002322
Christian Pirkerb5728192014-05-08 14:06:24 +00002323 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2324 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2325 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002326 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002327 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002328 VA = RVLocs[++i]; // skip ahead to next loc
2329 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002330 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2331 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002332 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002333 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002334 VA = RVLocs[++i]; // skip ahead to next loc
2335
2336 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002337 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002338 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002339 }
2340 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2341 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002342 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002343 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002344 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2345 fmrrd.getValue(isLittleEndian ? 0 : 1),
2346 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002347 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002348 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002349 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002350 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2351 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002352 Flag);
2353 } else
2354 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2355
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002356 // Guarantee that all emitted copies are
2357 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002358 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002359 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002360 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002361 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2362 const MCPhysReg *I =
2363 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2364 if (I) {
2365 for (; *I; ++I) {
2366 if (ARM::GPRRegClass.contains(*I))
2367 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2368 else if (ARM::DPRRegClass.contains(*I))
2369 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2370 else
2371 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2372 }
2373 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002374
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002375 // Update chain and glue.
2376 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002377 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002378 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002379
Tim Northoverd8407452013-10-01 14:33:28 +00002380 // CPUs which aren't M-class use a special sequence to return from
2381 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2382 // though we use "subs pc, lr, #N").
2383 //
2384 // M-class CPUs actually use a normal return sequence with a special
2385 // (hardware-provided) value in LR, so the normal code path works.
2386 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2387 !Subtarget->isMClass()) {
2388 if (Subtarget->isThumb1Only())
2389 report_fatal_error("interrupt attribute is not supported in Thumb1");
2390 return LowerInterruptReturn(RetOps, dl, DAG);
2391 }
2392
Craig Topper48d114b2014-04-26 18:35:24 +00002393 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002394}
2395
Evan Chengf8bad082012-04-10 01:51:00 +00002396bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002397 if (N->getNumValues() != 1)
2398 return false;
2399 if (!N->hasNUsesOfValue(1, 0))
2400 return false;
2401
Evan Chengf8bad082012-04-10 01:51:00 +00002402 SDValue TCChain = Chain;
2403 SDNode *Copy = *N->use_begin();
2404 if (Copy->getOpcode() == ISD::CopyToReg) {
2405 // If the copy has a glue operand, we conservatively assume it isn't safe to
2406 // perform a tail call.
2407 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2408 return false;
2409 TCChain = Copy->getOperand(0);
2410 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2411 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002412 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002413 SmallPtrSet<SDNode*, 2> Copies;
2414 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002415 UI != UE; ++UI) {
2416 if (UI->getOpcode() != ISD::CopyToReg)
2417 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002418 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002419 }
Evan Chengf8bad082012-04-10 01:51:00 +00002420 if (Copies.size() > 2)
2421 return false;
2422
2423 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2424 UI != UE; ++UI) {
2425 SDValue UseChain = UI->getOperand(0);
2426 if (Copies.count(UseChain.getNode()))
2427 // Second CopyToReg
2428 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002429 else {
2430 // We are at the top of this chain.
2431 // If the copy has a glue operand, we conservatively assume it
2432 // isn't safe to perform a tail call.
2433 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2434 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002435 // First CopyToReg
2436 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002437 }
Evan Chengf8bad082012-04-10 01:51:00 +00002438 }
2439 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002440 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002441 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002442 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002443 Copy = *Copy->use_begin();
2444 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002445 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002446 // If the copy has a glue operand, we conservatively assume it isn't safe to
2447 // perform a tail call.
2448 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2449 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002450 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002451 } else {
2452 return false;
2453 }
2454
Evan Cheng419ea282010-12-01 22:59:46 +00002455 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002456 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2457 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002458 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2459 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002460 return false;
2461 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002462 }
2463
Evan Chengf8bad082012-04-10 01:51:00 +00002464 if (!HasRet)
2465 return false;
2466
2467 Chain = TCChain;
2468 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002469}
2470
Evan Cheng0663f232011-03-21 01:19:09 +00002471bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002472 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002473 return false;
2474
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002475 auto Attr =
2476 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2477 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002478 return false;
2479
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002480 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002481}
2482
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002483// Trying to write a 64 bit value so need to split into two 32 bit values first,
2484// and pass the lower and high parts through.
2485static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2486 SDLoc DL(Op);
2487 SDValue WriteValue = Op->getOperand(2);
2488
2489 // This function is only supposed to be called for i64 type argument.
2490 assert(WriteValue.getValueType() == MVT::i64
2491 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2492
2493 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2494 DAG.getConstant(0, DL, MVT::i32));
2495 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2496 DAG.getConstant(1, DL, MVT::i32));
2497 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2498 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2499}
2500
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002501// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2502// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2503// one of the above mentioned nodes. It has to be wrapped because otherwise
2504// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2505// be used to form addressing mode. These wrapped nodes will be selected
2506// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002507static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002508 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002509 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002510 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002511 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002512 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002513 if (CP->isMachineConstantPoolEntry())
2514 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2515 CP->getAlignment());
2516 else
2517 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2518 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002519 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002520}
2521
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002522unsigned ARMTargetLowering::getJumpTableEncoding() const {
2523 return MachineJumpTableInfo::EK_Inline;
2524}
2525
Dan Gohman21cea8a2010-04-17 15:26:15 +00002526SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2527 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002528 MachineFunction &MF = DAG.getMachineFunction();
2529 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2530 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002531 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002532 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002533 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002534 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2535 SDValue CPAddr;
2536 if (RelocM == Reloc::Static) {
2537 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2538 } else {
2539 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002540 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002541 ARMConstantPoolValue *CPV =
2542 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2543 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002544 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2545 }
2546 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002547 SDValue Result =
2548 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2549 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2550 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002551 if (RelocM == Reloc::Static)
2552 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002553 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002554 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002555}
2556
Tim Northoverbd41cf82016-01-07 09:03:03 +00002557/// \brief Convert a TLS address reference into the correct sequence of loads
2558/// and calls to compute the variable's address for Darwin, and return an
2559/// SDValue containing the final node.
2560
2561/// Darwin only has one TLS scheme which must be capable of dealing with the
2562/// fully general situation, in the worst case. This means:
2563/// + "extern __thread" declaration.
2564/// + Defined in a possibly unknown dynamic library.
2565///
2566/// The general system is that each __thread variable has a [3 x i32] descriptor
2567/// which contains information used by the runtime to calculate the address. The
2568/// only part of this the compiler needs to know about is the first word, which
2569/// contains a function pointer that must be called with the address of the
2570/// entire descriptor in "r0".
2571///
2572/// Since this descriptor may be in a different unit, in general access must
2573/// proceed along the usual ARM rules. A common sequence to produce is:
2574///
2575/// movw rT1, :lower16:_var$non_lazy_ptr
2576/// movt rT1, :upper16:_var$non_lazy_ptr
2577/// ldr r0, [rT1]
2578/// ldr rT2, [r0]
2579/// blx rT2
2580/// [...address now in r0...]
2581SDValue
2582ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2583 SelectionDAG &DAG) const {
2584 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2585 SDLoc DL(Op);
2586
2587 // First step is to get the address of the actua global symbol. This is where
2588 // the TLS descriptor lives.
2589 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2590
2591 // The first entry in the descriptor is a function pointer that we must call
2592 // to obtain the address of the variable.
2593 SDValue Chain = DAG.getEntryNode();
2594 SDValue FuncTLVGet =
2595 DAG.getLoad(MVT::i32, DL, Chain, DescAddr,
2596 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2597 false, true, true, 4);
2598 Chain = FuncTLVGet.getValue(1);
2599
2600 MachineFunction &F = DAG.getMachineFunction();
2601 MachineFrameInfo *MFI = F.getFrameInfo();
2602 MFI->setAdjustsStack(true);
2603
2604 // TLS calls preserve all registers except those that absolutely must be
2605 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2606 // silly).
2607 auto TRI =
2608 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2609 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2610 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2611
2612 // Finally, we can make the call. This is just a degenerate version of a
2613 // normal AArch64 call node: r0 takes the address of the descriptor, and
2614 // returns the address of the variable in this thread.
2615 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2616 Chain =
2617 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2618 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2619 DAG.getRegisterMask(Mask), Chain.getValue(1));
2620 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2621}
2622
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002623SDValue
2624ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2625 SelectionDAG &DAG) const {
2626 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
2627 SDValue Chain = DAG.getEntryNode();
2628 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2629 SDLoc DL(Op);
2630
2631 // Load the current TEB (thread environment block)
2632 SDValue Ops[] = {Chain,
2633 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2634 DAG.getConstant(15, DL, MVT::i32),
2635 DAG.getConstant(0, DL, MVT::i32),
2636 DAG.getConstant(13, DL, MVT::i32),
2637 DAG.getConstant(0, DL, MVT::i32),
2638 DAG.getConstant(2, DL, MVT::i32)};
2639 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2640 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2641
2642 SDValue TEB = CurrentTEB.getValue(0);
2643 Chain = CurrentTEB.getValue(1);
2644
2645 // Load the ThreadLocalStoragePointer from the TEB
2646 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2647 SDValue TLSArray =
2648 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
2649 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo(),
2650 false, false, false, 0);
2651
2652 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2653 // offset into the TLSArray.
2654
2655 // Load the TLS index from the C runtime
2656 SDValue TLSIndex =
2657 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2658 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
2659 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo(),
2660 false, false, false, 0);
2661
2662 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2663 DAG.getConstant(2, DL, MVT::i32));
2664 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2665 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
2666 MachinePointerInfo(), false, false, false, 0);
2667
2668 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS,
2669 LowerGlobalAddressWindows(Op, DAG));
2670}
2671
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002672// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002673SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002674ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002675 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002676 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002677 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002678 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002679 MachineFunction &MF = DAG.getMachineFunction();
2680 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002681 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002682 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002683 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2684 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002685 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002686 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002687 Argument =
2688 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2689 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2690 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002691 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002692
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002693 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002694 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002695
2696 // call __tls_get_addr.
2697 ArgListTy Args;
2698 ArgListEntry Entry;
2699 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002700 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002701 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002702
Dale Johannesen555a3752009-01-30 23:10:59 +00002703 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002704 TargetLowering::CallLoweringInfo CLI(DAG);
2705 CLI.setDebugLoc(dl).setChain(Chain)
2706 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002707 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2708 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002709
Justin Holewinskiaa583972012-05-25 16:35:28 +00002710 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002711 return CallResult.first;
2712}
2713
2714// Lower ISD::GlobalTLSAddress using the "initial exec" or
2715// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002716SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002717ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002718 SelectionDAG &DAG,
2719 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002720 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002721 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002722 SDValue Offset;
2723 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002724 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002725 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002726 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002727
Hans Wennborgaea41202012-05-04 09:40:39 +00002728 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002729 MachineFunction &MF = DAG.getMachineFunction();
2730 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002731 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002732 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002733 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2734 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002735 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2736 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2737 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002738 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002739 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002740 Offset = DAG.getLoad(
2741 PtrVT, dl, Chain, Offset,
2742 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2743 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002744 Chain = Offset.getValue(1);
2745
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002746 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002747 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002748
Alex Lorenze40c8a22015-08-11 23:09:45 +00002749 Offset = DAG.getLoad(
2750 PtrVT, dl, Chain, Offset,
2751 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2752 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002753 } else {
2754 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002755 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002756 ARMConstantPoolValue *CPV =
2757 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002758 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002759 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002760 Offset = DAG.getLoad(
2761 PtrVT, dl, Chain, Offset,
2762 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2763 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002764 }
2765
2766 // The address of the thread local variable is the add of the thread
2767 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002768 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002769}
2770
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002771SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002772ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002773 if (Subtarget->isTargetDarwin())
2774 return LowerGlobalTLSAddressDarwin(Op, DAG);
2775
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002776 if (Subtarget->isTargetWindows())
2777 return LowerGlobalTLSAddressWindows(Op, DAG);
2778
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002779 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002780 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002781 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002782 if (DAG.getTarget().Options.EmulatedTLS)
2783 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002784
2785 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2786
2787 switch (model) {
2788 case TLSModel::GeneralDynamic:
2789 case TLSModel::LocalDynamic:
2790 return LowerToTLSGeneralDynamicModel(GA, DAG);
2791 case TLSModel::InitialExec:
2792 case TLSModel::LocalExec:
2793 return LowerToTLSExecModels(GA, DAG, model);
2794 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002795 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002796}
2797
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002798SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002799 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002800 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002801 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002802 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002803 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002804 bool UseGOT_PREL =
Peter Collingbourne99fac802015-10-26 20:46:44 +00002805 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage());
Peter Collingbourne97aae402015-10-26 18:23:16 +00002806
2807 MachineFunction &MF = DAG.getMachineFunction();
2808 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2809 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2810 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2811 SDLoc dl(Op);
2812 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2813 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2814 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2815 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2816 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002817 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002818 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002819 SDValue Result = DAG.getLoad(
2820 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2821 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2822 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002823 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002824 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2825 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2826 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002827 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002828 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002829 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002830 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002831 }
2832
2833 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002834 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002835 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002836 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002837 // FIXME: Once remat is capable of dealing with instructions with register
2838 // operands, expand this into two nodes.
2839 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2840 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002841 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002842 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2843 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002844 return DAG.getLoad(
2845 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2846 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2847 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002848 }
2849}
2850
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002851SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002852 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002853 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002854 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002855 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002856 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002857
Eric Christopherc1058df2014-07-04 01:55:26 +00002858 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002859 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002860
Tim Northover72360d22013-12-02 10:35:41 +00002861 // FIXME: Once remat is capable of dealing with instructions with register
2862 // operands, expand this into multiple nodes
2863 unsigned Wrapper =
2864 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002865
Tim Northover72360d22013-12-02 10:35:41 +00002866 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2867 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002868
Evan Cheng1b389522009-09-03 07:04:02 +00002869 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002870 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002871 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2872 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002873 return Result;
2874}
2875
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002876SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2877 SelectionDAG &DAG) const {
2878 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002879 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2880 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002881
2882 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002883 const ARMII::TOF TargetFlags =
2884 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002885 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002886 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002887 SDLoc DL(Op);
2888
2889 ++NumMovwMovt;
2890
2891 // FIXME: Once remat is capable of dealing with instructions with register
2892 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002893 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2894 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002895 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002896 if (GV->hasDLLImportStorageClass())
2897 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002898 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2899 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002900 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002901}
2902
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002903SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002904ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002905 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002906 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002907 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2908 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002909 Op.getOperand(1), Val);
2910}
2911
2912SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002913ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002914 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002915 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002916 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002917}
2918
Matthias Braun3cd00c12015-07-16 22:34:16 +00002919SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2920 SelectionDAG &DAG) const {
2921 SDLoc dl(Op);
2922 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2923 Op.getOperand(0));
2924}
2925
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002926SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002927ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002928 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002929 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002930 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002931 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002932 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002933 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002934 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002935 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002936 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002937 }
Bob Wilson17f88782009-08-04 00:25:01 +00002938 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002939 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002940 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2941 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002942 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002943 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002944 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002945 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002946 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002947 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2948 SDValue CPAddr;
2949 unsigned PCAdj = (RelocM != Reloc::PIC_)
2950 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002951 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002952 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2953 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002954 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002955 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002956 SDValue Result = DAG.getLoad(
2957 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2958 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2959 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002960
2961 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002962 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002963 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2964 }
2965 return Result;
2966 }
Evan Cheng18381b42011-03-29 23:06:19 +00002967 case Intrinsic::arm_neon_vmulls:
2968 case Intrinsic::arm_neon_vmullu: {
2969 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2970 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002971 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002972 Op.getOperand(1), Op.getOperand(2));
2973 }
James Molloyee868b22015-08-11 12:06:25 +00002974 case Intrinsic::arm_neon_vminnm:
2975 case Intrinsic::arm_neon_vmaxnm: {
2976 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2977 ? ISD::FMINNUM : ISD::FMAXNUM;
2978 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2979 Op.getOperand(1), Op.getOperand(2));
2980 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002981 case Intrinsic::arm_neon_vminu:
2982 case Intrinsic::arm_neon_vmaxu: {
2983 if (Op.getValueType().isFloatingPoint())
2984 return SDValue();
2985 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2986 ? ISD::UMIN : ISD::UMAX;
2987 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2988 Op.getOperand(1), Op.getOperand(2));
2989 }
James Molloyd616c642015-08-11 12:06:28 +00002990 case Intrinsic::arm_neon_vmins:
2991 case Intrinsic::arm_neon_vmaxs: {
2992 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002993 if (!Op.getValueType().isFloatingPoint()) {
2994 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2995 ? ISD::SMIN : ISD::SMAX;
2996 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2997 Op.getOperand(1), Op.getOperand(2));
2998 }
James Molloyd616c642015-08-11 12:06:28 +00002999 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3000 ? ISD::FMINNAN : ISD::FMAXNAN;
3001 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3002 Op.getOperand(1), Op.getOperand(2));
3003 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003004 }
3005}
3006
Eli Friedman30a49e92011-08-03 21:06:02 +00003007static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3008 const ARMSubtarget *Subtarget) {
3009 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003010 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003011 if (!Subtarget->hasDataBarrier()) {
3012 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3013 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3014 // here.
3015 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003016 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003017 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003018 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003019 }
3020
Tim Northover36b24172013-07-03 09:20:36 +00003021 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3022 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003023 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003024 if (Subtarget->isMClass()) {
3025 // Only a full system barrier exists in the M-class architectures.
3026 Domain = ARM_MB::SY;
3027 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003028 // Swift happens to implement ISHST barriers in a way that's compatible with
3029 // Release semantics but weaker than ISH so we'd be fools not to use
3030 // it. Beware: other processors probably don't!
3031 Domain = ARM_MB::ISHST;
3032 }
3033
Joey Gouly926d3f52013-09-05 15:35:24 +00003034 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003035 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3036 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003037}
3038
Evan Cheng8740ee32010-11-03 06:34:55 +00003039static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3040 const ARMSubtarget *Subtarget) {
3041 // ARM pre v5TE and Thumb1 does not have preload instructions.
3042 if (!(Subtarget->isThumb2() ||
3043 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3044 // Just preserve the chain.
3045 return Op.getOperand(0);
3046
Andrew Trickef9de2a2013-05-25 02:42:55 +00003047 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003048 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3049 if (!isRead &&
3050 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3051 // ARMv7 with MP extension has PLDW.
3052 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003053
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003054 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3055 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003056 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003057 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003058 isData = ~isData & 1;
3059 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003060
3061 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003062 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3063 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003064}
3065
Dan Gohman31ae5862010-04-17 14:41:14 +00003066static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3067 MachineFunction &MF = DAG.getMachineFunction();
3068 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3069
Evan Cheng10043e22007-01-19 07:51:42 +00003070 // vastart just stores the address of the VarArgsFrameIndex slot into the
3071 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003072 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003073 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003074 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003075 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003076 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3077 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00003078}
3079
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003080SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00003081ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
3082 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003083 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003084 MachineFunction &MF = DAG.getMachineFunction();
3085 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3086
Craig Topper760b1342012-02-22 05:59:10 +00003087 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003088 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003089 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003090 else
Craig Topperc7242e02012-04-20 07:30:17 +00003091 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003092
3093 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003094 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003095 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003096
3097 SDValue ArgValue2;
3098 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003099 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00003100 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003101
3102 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003103 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003104 ArgValue2 = DAG.getLoad(
3105 MVT::i32, dl, Root, FIN,
3106 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
3107 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00003108 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003109 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003110 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003111 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003112 if (!Subtarget->isLittle())
3113 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003114 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003115}
3116
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003117// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003118// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003119// byval). Either way, we allocate stack slots adjacent to the data
3120// provided by our caller, and store the unallocated registers there.
3121// If this is a variadic function, the va_list pointer will begin with
3122// these values; otherwise, this reassembles a (byval) structure that
3123// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003124// Return: The frame index registers were stored into.
3125int
3126ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003127 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003128 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003129 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00003130 int ArgOffset,
3131 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003132 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003133 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003134 // Setup first unallocated register as first byval register;
3135 // eat all remained registers
3136 // (these two actions are performed by HandleByVal method).
3137 // Then, here, we initialize stack frame with
3138 // "store-reg" instructions.
3139 // Case #2. Var-args function, that doesn't contain byval parameters.
3140 // The same: eat all remained unallocated registers,
3141 // initialize stack frame.
3142
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003143 MachineFunction &MF = DAG.getMachineFunction();
3144 MachineFrameInfo *MFI = MF.getFrameInfo();
3145 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003146 unsigned RBegin, REnd;
3147 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3148 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003149 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003150 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003151 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003152 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003153 }
3154
Tim Northover8cda34f2015-03-11 18:54:22 +00003155 if (REnd != RBegin)
3156 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003157
Mehdi Amini44ede332015-07-09 02:09:04 +00003158 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003159 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003160 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003161
Tim Northover8cda34f2015-03-11 18:54:22 +00003162 SmallVector<SDValue, 4> MemOps;
3163 const TargetRegisterClass *RC =
3164 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003165
Tim Northover8cda34f2015-03-11 18:54:22 +00003166 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3167 unsigned VReg = MF.addLiveIn(Reg, RC);
3168 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3169 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003170 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003171 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3172 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003173 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003174 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003175
3176 if (!MemOps.empty())
3177 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3178 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003179}
3180
3181// Setup stack frame, the va_list pointer will start from.
3182void
3183ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003184 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003185 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003186 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003187 bool ForceMutable) const {
3188 MachineFunction &MF = DAG.getMachineFunction();
3189 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3190
3191 // Try to store any remaining integer argument regs
3192 // to their spots on the stack so that they may be loaded by deferencing
3193 // the result of va_next.
3194 // If there is no regs to be stored, just point address after last
3195 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003196 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3197 CCInfo.getInRegsParamsCount(),
3198 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003199 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003200}
3201
Bob Wilson2e076c42009-06-22 23:27:02 +00003202SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003203ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003204 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003205 const SmallVectorImpl<ISD::InputArg>
3206 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003207 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003208 SmallVectorImpl<SDValue> &InVals)
3209 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003210 MachineFunction &MF = DAG.getMachineFunction();
3211 MachineFrameInfo *MFI = MF.getFrameInfo();
3212
Bob Wilsona4c22902009-04-17 19:07:39 +00003213 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3214
3215 // Assign locations to all of the incoming arguments.
3216 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003217 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3218 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003219 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003220 CCAssignFnForNode(CallConv, /* Return*/ false,
3221 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003222
Bob Wilsona4c22902009-04-17 19:07:39 +00003223 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003224 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003225 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3226 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003227
3228 // Initially ArgRegsSaveSize is zero.
3229 // Then we increase this value each time we meet byval parameter.
3230 // We also increase this value in case of varargs function.
3231 AFI->setArgRegsSaveSize(0);
3232
Oliver Stannardd55e1152014-03-05 15:25:27 +00003233 // Calculate the amount of stack space that we need to allocate to store
3234 // byval and variadic arguments that are passed in registers.
3235 // We need to know this before we allocate the first byval or variadic
3236 // argument, as they will be allocated a stack slot below the CFA (Canonical
3237 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003238 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003239 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003240 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3241 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003242
Tim Northover8cda34f2015-03-11 18:54:22 +00003243 CCValAssign &VA = ArgLocs[i];
3244 unsigned Index = VA.getValNo();
3245 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3246 if (!Flags.isByVal())
3247 continue;
3248
3249 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3250 unsigned RBegin, REnd;
3251 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3252 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3253
3254 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003255 }
3256 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003257
3258 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003259 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003260 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3261 if (RegIdx != array_lengthof(GPRArgRegs))
3262 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003263 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003264
3265 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3266 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003267 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003268
Bob Wilsona4c22902009-04-17 19:07:39 +00003269 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3270 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003271 if (Ins[VA.getValNo()].isOrigArg()) {
3272 std::advance(CurOrigArg,
3273 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3274 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3275 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003276 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003277 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003278 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003279
Bob Wilsona4c22902009-04-17 19:07:39 +00003280 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003281 // f64 and vector types are split up into multiple registers or
3282 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003283 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003284 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003285 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003286 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003287 SDValue ArgValue2;
3288 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003289 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003290 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003291 ArgValue2 = DAG.getLoad(
3292 MVT::f64, dl, Chain, FIN,
3293 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3294 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003295 } else {
3296 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3297 Chain, DAG, dl);
3298 }
Owen Anderson9f944592009-08-11 20:47:22 +00003299 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3300 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003301 ArgValue, ArgValue1,
3302 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003303 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003304 ArgValue, ArgValue2,
3305 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003306 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003307 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003308
Bob Wilson2e076c42009-06-22 23:27:02 +00003309 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003310 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003311
Owen Anderson9f944592009-08-11 20:47:22 +00003312 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003313 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003314 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003315 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003316 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003317 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003318 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003319 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3320 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003321 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003322 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003323
3324 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003325 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003326 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003327 }
3328
3329 // If this is an 8 or 16-bit value, it is really passed promoted
3330 // to 32 bits. Insert an assert[sz]ext to capture this, then
3331 // truncate to the right size.
3332 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003333 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003334 case CCValAssign::Full: break;
3335 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003336 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003337 break;
3338 case CCValAssign::SExt:
3339 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3340 DAG.getValueType(VA.getValVT()));
3341 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3342 break;
3343 case CCValAssign::ZExt:
3344 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3345 DAG.getValueType(VA.getValVT()));
3346 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3347 break;
3348 }
3349
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003350 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003351
3352 } else { // VA.isRegLoc()
3353
3354 // sanity check
3355 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003356 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003357
Andrew Trick05938a52015-02-16 18:10:47 +00003358 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003359
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003360 // Some Ins[] entries become multiple ArgLoc[] entries.
3361 // Process them only once.
3362 if (index != lastInsIndex)
3363 {
3364 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003365 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003366 // This can be changed with more analysis.
3367 // In case of tail call optimization mark all arguments mutable.
3368 // Since they could be overwritten by lowering of arguments in case of
3369 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003370 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003371 assert(Ins[index].isOrigArg() &&
3372 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003373 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003374
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003375 int FrameIndex = StoreByValRegs(
3376 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3377 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003378 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003379 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003380 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003381 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003382 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003383 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003384
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003385 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003386 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003387 InVals.push_back(DAG.getLoad(
3388 VA.getValVT(), dl, Chain, FIN,
3389 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3390 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003391 }
3392 lastInsIndex = index;
3393 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003394 }
3395 }
3396
3397 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003398 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003399 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003400 CCInfo.getNextStackOffset(),
3401 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003402
Oliver Stannardb14c6252014-04-02 16:10:33 +00003403 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3404
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003405 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003406}
3407
3408/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003409static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003410 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003411 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003412 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003413 // Maybe this has already been legalized into the constant pool?
3414 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003415 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003416 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003417 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003418 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003419 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003420 } else if (Op->getOpcode() == ISD::BITCAST &&
3421 Op->getValueType(0) == MVT::f64) {
3422 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3423 // created by LowerConstantFP().
3424 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003425 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3426 isNullConstant(BitcastOp->getOperand(0)))
3427 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003428 }
3429 return false;
3430}
3431
Evan Cheng10043e22007-01-19 07:51:42 +00003432/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3433/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003434SDValue
3435ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003436 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003437 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003438 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003439 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003440 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003441 // Constant does not fit, try adjusting it by one?
3442 switch (CC) {
3443 default: break;
3444 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003445 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003446 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003447 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003448 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003449 }
3450 break;
3451 case ISD::SETULT:
3452 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003453 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003454 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003455 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003456 }
3457 break;
3458 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003459 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003460 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003461 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003462 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003463 }
3464 break;
3465 case ISD::SETULE:
3466 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003467 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003468 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003469 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003470 }
3471 break;
3472 }
3473 }
3474 }
3475
3476 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003477 ARMISD::NodeType CompareType;
3478 switch (CondCode) {
3479 default:
3480 CompareType = ARMISD::CMP;
3481 break;
3482 case ARMCC::EQ:
3483 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003484 // Uses only Z Flag
3485 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003486 break;
3487 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003488 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003489 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003490}
3491
3492/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003493SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003494ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003495 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003496 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003497 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003498 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003499 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003500 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003501 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3502 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003503}
3504
Bob Wilson45acbd02011-03-08 01:17:20 +00003505/// duplicateCmp - Glue values can have only one use, so this function
3506/// duplicates a comparison node.
3507SDValue
3508ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3509 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003510 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003511 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3512 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3513
3514 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3515 Cmp = Cmp.getOperand(0);
3516 Opc = Cmp.getOpcode();
3517 if (Opc == ARMISD::CMPFP)
3518 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3519 else {
3520 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3521 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3522 }
3523 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3524}
3525
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003526std::pair<SDValue, SDValue>
3527ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3528 SDValue &ARMcc) const {
3529 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3530
3531 SDValue Value, OverflowCmp;
3532 SDValue LHS = Op.getOperand(0);
3533 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003534 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003535
3536 // FIXME: We are currently always generating CMPs because we don't support
3537 // generating CMN through the backend. This is not as good as the natural
3538 // CMP case because it causes a register dependency and cannot be folded
3539 // later.
3540
3541 switch (Op.getOpcode()) {
3542 default:
3543 llvm_unreachable("Unknown overflow instruction!");
3544 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003545 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3546 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3547 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003548 break;
3549 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003550 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3551 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3552 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003553 break;
3554 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003555 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3556 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3557 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003558 break;
3559 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003560 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3561 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3562 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003563 break;
3564 } // switch (...)
3565
3566 return std::make_pair(Value, OverflowCmp);
3567}
3568
3569
3570SDValue
3571ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3572 // Let legalize expand this if it isn't a legal type yet.
3573 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3574 return SDValue();
3575
3576 SDValue Value, OverflowCmp;
3577 SDValue ARMcc;
3578 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3579 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003580 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003581 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003582 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3583 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003584 EVT VT = Op.getValueType();
3585
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003586 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003587 ARMcc, CCR, OverflowCmp);
3588
3589 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003590 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003591}
3592
3593
Bill Wendling6a981312010-08-11 08:43:16 +00003594SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3595 SDValue Cond = Op.getOperand(0);
3596 SDValue SelectTrue = Op.getOperand(1);
3597 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003598 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003599 unsigned Opc = Cond.getOpcode();
3600
3601 if (Cond.getResNo() == 1 &&
3602 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3603 Opc == ISD::USUBO)) {
3604 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3605 return SDValue();
3606
3607 SDValue Value, OverflowCmp;
3608 SDValue ARMcc;
3609 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3610 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3611 EVT VT = Op.getValueType();
3612
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003613 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003614 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003615 }
Bill Wendling6a981312010-08-11 08:43:16 +00003616
3617 // Convert:
3618 //
3619 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3620 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3621 //
3622 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3623 const ConstantSDNode *CMOVTrue =
3624 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3625 const ConstantSDNode *CMOVFalse =
3626 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3627
3628 if (CMOVTrue && CMOVFalse) {
3629 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3630 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3631
3632 SDValue True;
3633 SDValue False;
3634 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3635 True = SelectTrue;
3636 False = SelectFalse;
3637 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3638 True = SelectFalse;
3639 False = SelectTrue;
3640 }
3641
3642 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003643 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003644 SDValue ARMcc = Cond.getOperand(2);
3645 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003646 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003647 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003648 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003649 }
3650 }
3651 }
3652
Dan Gohmand4a77c42012-02-24 00:09:36 +00003653 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3654 // undefined bits before doing a full-word comparison with zero.
3655 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003656 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003657
Bill Wendling6a981312010-08-11 08:43:16 +00003658 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003659 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003660 SelectTrue, SelectFalse, ISD::SETNE);
3661}
3662
Joey Gouly881eab52013-08-22 15:29:11 +00003663static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3664 bool &swpCmpOps, bool &swpVselOps) {
3665 // Start by selecting the GE condition code for opcodes that return true for
3666 // 'equality'
3667 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3668 CC == ISD::SETULE)
3669 CondCode = ARMCC::GE;
3670
3671 // and GT for opcodes that return false for 'equality'.
3672 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3673 CC == ISD::SETULT)
3674 CondCode = ARMCC::GT;
3675
3676 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3677 // to swap the compare operands.
3678 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3679 CC == ISD::SETULT)
3680 swpCmpOps = true;
3681
3682 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3683 // If we have an unordered opcode, we need to swap the operands to the VSEL
3684 // instruction (effectively negating the condition).
3685 //
3686 // This also has the effect of swapping which one of 'less' or 'greater'
3687 // returns true, so we also swap the compare operands. It also switches
3688 // whether we return true for 'equality', so we compensate by picking the
3689 // opposite condition code to our original choice.
3690 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3691 CC == ISD::SETUGT) {
3692 swpCmpOps = !swpCmpOps;
3693 swpVselOps = !swpVselOps;
3694 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3695 }
3696
3697 // 'ordered' is 'anything but unordered', so use the VS condition code and
3698 // swap the VSEL operands.
3699 if (CC == ISD::SETO) {
3700 CondCode = ARMCC::VS;
3701 swpVselOps = true;
3702 }
3703
3704 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3705 // code and swap the VSEL operands.
3706 if (CC == ISD::SETUNE) {
3707 CondCode = ARMCC::EQ;
3708 swpVselOps = true;
3709 }
3710}
3711
Oliver Stannard51b1d462014-08-21 12:50:31 +00003712SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3713 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3714 SDValue Cmp, SelectionDAG &DAG) const {
3715 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3716 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3717 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3718 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3719 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3720
3721 SDValue TrueLow = TrueVal.getValue(0);
3722 SDValue TrueHigh = TrueVal.getValue(1);
3723 SDValue FalseLow = FalseVal.getValue(0);
3724 SDValue FalseHigh = FalseVal.getValue(1);
3725
3726 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3727 ARMcc, CCR, Cmp);
3728 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3729 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3730
3731 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3732 } else {
3733 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3734 Cmp);
3735 }
3736}
3737
Dan Gohman21cea8a2010-04-17 15:26:15 +00003738SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003739 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003740 SDValue LHS = Op.getOperand(0);
3741 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003742 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003743 SDValue TrueVal = Op.getOperand(2);
3744 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003745 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003746
Oliver Stannard51b1d462014-08-21 12:50:31 +00003747 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3748 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3749 dl);
3750
3751 // If softenSetCCOperands only returned one value, we should compare it to
3752 // zero.
3753 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003754 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003755 CC = ISD::SETNE;
3756 }
3757 }
3758
Owen Anderson9f944592009-08-11 20:47:22 +00003759 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003760 // Try to generate VSEL on ARMv8.
3761 // The VSEL instruction can't use all the usual ARM condition
3762 // codes: it only has two bits to select the condition code, so it's
3763 // constrained to use only GE, GT, VS and EQ.
3764 //
3765 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3766 // swap the operands of the previous compare instruction (effectively
3767 // inverting the compare condition, swapping 'less' and 'greater') and
3768 // sometimes need to swap the operands to the VSEL (which inverts the
3769 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003770 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3771 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003772 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3773 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3774 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003775 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003776 std::swap(TrueVal, FalseVal);
3777 }
3778 }
3779
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003780 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003781 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003782 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003783 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003784 }
3785
3786 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003787 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003788
Scott Douglass7ad77922015-04-08 17:18:28 +00003789 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003790 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3791 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003792 bool swpCmpOps = false;
3793 bool swpVselOps = false;
3794 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3795
3796 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3797 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3798 if (swpCmpOps)
3799 std::swap(LHS, RHS);
3800 if (swpVselOps)
3801 std::swap(TrueVal, FalseVal);
3802 }
3803 }
3804
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003805 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003806 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003807 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003808 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003809 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003810 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003811 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003812 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003813 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003814 }
3815 return Result;
3816}
3817
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003818/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3819/// to morph to an integer compare sequence.
3820static bool canChangeToInt(SDValue Op, bool &SeenZero,
3821 const ARMSubtarget *Subtarget) {
3822 SDNode *N = Op.getNode();
3823 if (!N->hasOneUse())
3824 // Otherwise it requires moving the value from fp to integer registers.
3825 return false;
3826 if (!N->getNumValues())
3827 return false;
3828 EVT VT = Op.getValueType();
3829 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3830 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3831 // vmrs are very slow, e.g. cortex-a8.
3832 return false;
3833
3834 if (isFloatingPointZero(Op)) {
3835 SeenZero = true;
3836 return true;
3837 }
3838 return ISD::isNormalLoad(N);
3839}
3840
3841static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3842 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003843 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003844
3845 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003846 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003847 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003848 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003849 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003850
3851 llvm_unreachable("Unknown VFP cmp argument!");
3852}
3853
3854static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3855 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003856 SDLoc dl(Op);
3857
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003858 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003859 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3860 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003861 return;
3862 }
3863
3864 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3865 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003866 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003867 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003868 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003869 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003870 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003871
3872 EVT PtrType = Ptr.getValueType();
3873 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003874 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3875 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3876 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003877 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003878 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003879 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003880 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003881 return;
3882 }
3883
3884 llvm_unreachable("Unknown VFP cmp argument!");
3885}
3886
3887/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3888/// f32 and even f64 comparisons to integer ones.
3889SDValue
3890ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3891 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003892 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003893 SDValue LHS = Op.getOperand(2);
3894 SDValue RHS = Op.getOperand(3);
3895 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003896 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003897
Evan Chengd12af5d2012-03-01 23:27:13 +00003898 bool LHSSeenZero = false;
3899 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3900 bool RHSSeenZero = false;
3901 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3902 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003903 // If unsafe fp math optimization is enabled and there are no other uses of
3904 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003905 // to an integer comparison.
3906 if (CC == ISD::SETOEQ)
3907 CC = ISD::SETEQ;
3908 else if (CC == ISD::SETUNE)
3909 CC = ISD::SETNE;
3910
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003911 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003912 SDValue ARMcc;
3913 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003914 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3915 bitcastf32Toi32(LHS, DAG), Mask);
3916 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3917 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003918 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3919 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3920 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3921 Chain, Dest, ARMcc, CCR, Cmp);
3922 }
3923
3924 SDValue LHS1, LHS2;
3925 SDValue RHS1, RHS2;
3926 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3927 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003928 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3929 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003930 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003931 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003932 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003933 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003934 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003935 }
3936
3937 return SDValue();
3938}
3939
3940SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3941 SDValue Chain = Op.getOperand(0);
3942 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3943 SDValue LHS = Op.getOperand(2);
3944 SDValue RHS = Op.getOperand(3);
3945 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003946 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003947
Oliver Stannard51b1d462014-08-21 12:50:31 +00003948 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3949 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3950 dl);
3951
3952 // If softenSetCCOperands only returned one value, we should compare it to
3953 // zero.
3954 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003955 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003956 CC = ISD::SETNE;
3957 }
3958 }
3959
Owen Anderson9f944592009-08-11 20:47:22 +00003960 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003961 SDValue ARMcc;
3962 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003963 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003964 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003965 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003966 }
3967
Owen Anderson9f944592009-08-11 20:47:22 +00003968 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003969
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003970 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003971 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3972 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003973 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003974 return Result;
3975 }
3976
Evan Cheng10043e22007-01-19 07:51:42 +00003977 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003978 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003979
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003980 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003981 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003982 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003983 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003984 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003985 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003986 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003987 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003988 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003989 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003990 }
3991 return Res;
3992}
3993
Dan Gohman21cea8a2010-04-17 15:26:15 +00003994SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003995 SDValue Chain = Op.getOperand(0);
3996 SDValue Table = Op.getOperand(1);
3997 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003998 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003999
Mehdi Amini44ede332015-07-09 02:09:04 +00004000 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00004001 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004002 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00004003 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004004 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00004005 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004006 if (Subtarget->isThumb2()) {
4007 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4008 // which does another jump to the destination. This also makes it easier
4009 // to translate it to TBB / TBH later.
4010 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004011 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004012 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004013 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00004014 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004015 Addr =
4016 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
4017 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4018 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004019 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004020 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004021 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004022 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004023 Addr =
4024 DAG.getLoad(PTy, dl, Chain, Addr,
4025 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4026 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004027 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004028 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004029 }
Evan Cheng10043e22007-01-19 07:51:42 +00004030}
4031
Eli Friedman2d4055b2011-11-09 23:36:02 +00004032static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004033 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004034 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004035
James Molloy547d4c02012-02-20 09:24:05 +00004036 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4037 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4038 return Op;
4039 return DAG.UnrollVectorOp(Op.getNode());
4040 }
4041
4042 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4043 "Invalid type for custom lowering!");
4044 if (VT != MVT::v4i16)
4045 return DAG.UnrollVectorOp(Op.getNode());
4046
4047 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4048 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004049}
4050
Oliver Stannard51b1d462014-08-21 12:50:31 +00004051SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004052 EVT VT = Op.getValueType();
4053 if (VT.isVector())
4054 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004055 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4056 RTLIB::Libcall LC;
4057 if (Op.getOpcode() == ISD::FP_TO_SINT)
4058 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4059 Op.getValueType());
4060 else
4061 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4062 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004063 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004064 /*isSigned*/ false, SDLoc(Op)).first;
4065 }
4066
James Molloyfa041152015-03-23 16:15:16 +00004067 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004068}
4069
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004070static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4071 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004072 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004073
Eli Friedman2d4055b2011-11-09 23:36:02 +00004074 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4075 if (VT.getVectorElementType() == MVT::f32)
4076 return Op;
4077 return DAG.UnrollVectorOp(Op.getNode());
4078 }
4079
Duncan Sandsa41634e2011-08-12 14:54:45 +00004080 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4081 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004082 if (VT != MVT::v4f32)
4083 return DAG.UnrollVectorOp(Op.getNode());
4084
4085 unsigned CastOpc;
4086 unsigned Opc;
4087 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004088 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004089 case ISD::SINT_TO_FP:
4090 CastOpc = ISD::SIGN_EXTEND;
4091 Opc = ISD::SINT_TO_FP;
4092 break;
4093 case ISD::UINT_TO_FP:
4094 CastOpc = ISD::ZERO_EXTEND;
4095 Opc = ISD::UINT_TO_FP;
4096 break;
4097 }
4098
4099 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4100 return DAG.getNode(Opc, dl, VT, Op);
4101}
4102
Oliver Stannard51b1d462014-08-21 12:50:31 +00004103SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004104 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004105 if (VT.isVector())
4106 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004107 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4108 RTLIB::Libcall LC;
4109 if (Op.getOpcode() == ISD::SINT_TO_FP)
4110 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4111 Op.getValueType());
4112 else
4113 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4114 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004115 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004116 /*isSigned*/ false, SDLoc(Op)).first;
4117 }
4118
James Molloyfa041152015-03-23 16:15:16 +00004119 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004120}
4121
Evan Cheng25f93642010-07-08 02:08:50 +00004122SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004123 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004124 SDValue Tmp0 = Op.getOperand(0);
4125 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004126 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004127 EVT VT = Op.getValueType();
4128 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004129 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4130 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4131 bool UseNEON = !InGPR && Subtarget->hasNEON();
4132
4133 if (UseNEON) {
4134 // Use VBSL to copy the sign bit.
4135 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4136 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004137 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004138 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4139 if (VT == MVT::f64)
4140 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4141 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004142 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004143 else /*if (VT == MVT::f32)*/
4144 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4145 if (SrcVT == MVT::f32) {
4146 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4147 if (VT == MVT::f64)
4148 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4149 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004150 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004151 } else if (VT == MVT::f32)
4152 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4153 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004154 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004155 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4156 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4157
4158 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004159 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004160 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4161 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4162 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004163
Evan Chengd6b641e2011-02-23 02:24:55 +00004164 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4165 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4166 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004167 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004168 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4169 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004170 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004171 } else {
4172 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4173 }
4174
4175 return Res;
4176 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004177
4178 // Bitcast operand 1 to i32.
4179 if (SrcVT == MVT::f64)
4180 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004181 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004182 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4183
Evan Chengd6b641e2011-02-23 02:24:55 +00004184 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004185 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4186 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004187 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4188 if (VT == MVT::f32) {
4189 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4190 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4191 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4192 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004193 }
4194
Evan Chengd6b641e2011-02-23 02:24:55 +00004195 // f64: Or the high part with signbit and then combine two parts.
4196 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004197 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004198 SDValue Lo = Tmp0.getValue(0);
4199 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4200 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4201 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004202}
4203
Evan Cheng168ced92010-05-22 01:47:14 +00004204SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4205 MachineFunction &MF = DAG.getMachineFunction();
4206 MachineFrameInfo *MFI = MF.getFrameInfo();
4207 MFI->setReturnAddressIsTaken(true);
4208
Bill Wendling908bf812014-01-06 00:43:20 +00004209 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004210 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004211
Evan Cheng168ced92010-05-22 01:47:14 +00004212 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004213 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004214 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4215 if (Depth) {
4216 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004217 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004218 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4219 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004220 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004221 }
4222
4223 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004224 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004225 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4226}
4227
Dan Gohman21cea8a2010-04-17 15:26:15 +00004228SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004229 const ARMBaseRegisterInfo &ARI =
4230 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4231 MachineFunction &MF = DAG.getMachineFunction();
4232 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004233 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004234
Owen Anderson53aa7a92009-08-10 22:56:29 +00004235 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004237 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004238 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004239 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4240 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004241 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4242 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004243 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004244 return FrameAddr;
4245}
4246
Renato Golinc7aea402014-05-06 16:51:25 +00004247// FIXME? Maybe this could be a TableGen attribute on some registers and
4248// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004249unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4250 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004251 unsigned Reg = StringSwitch<unsigned>(RegName)
4252 .Case("sp", ARM::SP)
4253 .Default(0);
4254 if (Reg)
4255 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004256 report_fatal_error(Twine("Invalid register name \""
4257 + StringRef(RegName) + "\"."));
4258}
4259
4260// Result is 64 bit value so split into two 32 bit values and return as a
4261// pair of values.
4262static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4263 SelectionDAG &DAG) {
4264 SDLoc DL(N);
4265
4266 // This function is only supposed to be called for i64 type destination.
4267 assert(N->getValueType(0) == MVT::i64
4268 && "ExpandREAD_REGISTER called for non-i64 type result.");
4269
4270 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4271 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4272 N->getOperand(0),
4273 N->getOperand(1));
4274
4275 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4276 Read.getValue(1)));
4277 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004278}
4279
Quentin Colombet901f0362015-12-04 01:53:14 +00004280/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4281/// When \p DstVT, the destination type of \p BC, is on the vector
4282/// register bank and the source of bitcast, \p Op, operates on the same bank,
4283/// it might be possible to combine them, such that everything stays on the
4284/// vector register bank.
4285/// \p return The node that would replace \p BT, if the combine
4286/// is possible.
4287static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4288 SelectionDAG &DAG) {
4289 SDValue Op = BC->getOperand(0);
4290 EVT DstVT = BC->getValueType(0);
4291
4292 // The only vector instruction that can produce a scalar (remember,
4293 // since the bitcast was about to be turned into VMOVDRR, the source
4294 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4295 // Moreover, we can do this combine only if there is one use.
4296 // Finally, if the destination type is not a vector, there is not
4297 // much point on forcing everything on the vector bank.
4298 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4299 !Op.hasOneUse())
4300 return SDValue();
4301
4302 // If the index is not constant, we will introduce an additional
4303 // multiply that will stick.
4304 // Give up in that case.
4305 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4306 if (!Index)
4307 return SDValue();
4308 unsigned DstNumElt = DstVT.getVectorNumElements();
4309
4310 // Compute the new index.
4311 const APInt &APIntIndex = Index->getAPIntValue();
4312 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4313 NewIndex *= APIntIndex;
4314 // Check if the new constant index fits into i32.
4315 if (NewIndex.getBitWidth() > 32)
4316 return SDValue();
4317
4318 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4319 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4320 SDLoc dl(Op);
4321 SDValue ExtractSrc = Op.getOperand(0);
4322 EVT VecVT = EVT::getVectorVT(
4323 *DAG.getContext(), DstVT.getScalarType(),
4324 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4325 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4326 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4327 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4328}
4329
Wesley Peck527da1b2010-11-23 03:31:01 +00004330/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004331/// expand a bit convert where either the source or destination type is i64 to
4332/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4333/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4334/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004335static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004337 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004338 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004339
Bob Wilson59b70ea2010-04-17 05:30:19 +00004340 // This function is only supposed to be called for i64 types, either as the
4341 // source or destination of the bit convert.
4342 EVT SrcVT = Op.getValueType();
4343 EVT DstVT = N->getValueType(0);
4344 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004345 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004346
Bob Wilson59b70ea2010-04-17 05:30:19 +00004347 // Turn i64->f64 into VMOVDRR.
4348 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004349 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4350 // if we can combine the bitcast with its source.
4351 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4352 return Val;
4353
Owen Anderson9f944592009-08-11 20:47:22 +00004354 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004355 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004356 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004357 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004358 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004359 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004360 }
Bob Wilson7117a912009-03-20 22:42:55 +00004361
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004362 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004363 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004364 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004365 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004366 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004367 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4368 DAG.getVTList(MVT::i32, MVT::i32),
4369 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4370 else
4371 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4372 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004373 // Merge the pieces into a single i64 value.
4374 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4375 }
Bob Wilson7117a912009-03-20 22:42:55 +00004376
Bob Wilson59b70ea2010-04-17 05:30:19 +00004377 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004378}
4379
Bob Wilson2e076c42009-06-22 23:27:02 +00004380/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004381/// Zero vectors are used to represent vector negation and in those cases
4382/// will be implemented with the NEON VNEG instruction. However, VNEG does
4383/// not support i64 elements, so sometimes the zero vectors will need to be
4384/// explicitly constructed. Regardless, use a canonical VMOV to create the
4385/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004386static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004387 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004388 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004389 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004390 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4391 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004392 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004393}
4394
Jim Grosbach624fcb22009-10-31 21:00:56 +00004395/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4396/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004397SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4398 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004399 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4400 EVT VT = Op.getValueType();
4401 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004402 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004403 SDValue ShOpLo = Op.getOperand(0);
4404 SDValue ShOpHi = Op.getOperand(1);
4405 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004406 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004407 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004408
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004409 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4410
Jim Grosbach624fcb22009-10-31 21:00:56 +00004411 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004412 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004413 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4414 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004415 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004416 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4417 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004418 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004419
4420 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004421 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4422 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004423 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004424 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004425 CCR, Cmp);
4426
4427 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004428 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004429}
4430
Jim Grosbach5d994042009-10-31 19:38:01 +00004431/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4432/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004433SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4434 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004435 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4436 EVT VT = Op.getValueType();
4437 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004438 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004439 SDValue ShOpLo = Op.getOperand(0);
4440 SDValue ShOpHi = Op.getOperand(1);
4441 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004442 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004443
4444 assert(Op.getOpcode() == ISD::SHL_PARTS);
4445 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004446 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004447 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4448 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004449 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004450 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4451 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4452
4453 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4454 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004455 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4456 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004457 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004458 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004459 CCR, Cmp);
4460
4461 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004462 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004463}
4464
Jim Grosbach535d3b42010-09-08 03:54:02 +00004465SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004466 SelectionDAG &DAG) const {
4467 // The rounding mode is in bits 23:22 of the FPSCR.
4468 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4469 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4470 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004471 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004472 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004473 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004474 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004475 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004476 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004477 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004478 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004479 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004480 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004481}
4482
Jim Grosbach8546ec92010-01-18 19:58:49 +00004483static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4484 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004485 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004486 EVT VT = N->getValueType(0);
4487 if (VT.isVector()) {
4488 assert(ST->hasNEON());
4489
4490 // Compute the least significant set bit: LSB = X & -X
4491 SDValue X = N->getOperand(0);
4492 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4493 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4494
4495 EVT ElemTy = VT.getVectorElementType();
4496
4497 if (ElemTy == MVT::i8) {
4498 // Compute with: cttz(x) = ctpop(lsb - 1)
4499 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4500 DAG.getTargetConstant(1, dl, ElemTy));
4501 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4502 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4503 }
4504
4505 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4506 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4507 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4508 unsigned NumBits = ElemTy.getSizeInBits();
4509 SDValue WidthMinus1 =
4510 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4511 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4512 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4513 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4514 }
4515
4516 // Compute with: cttz(x) = ctpop(lsb - 1)
4517
4518 // Since we can only compute the number of bits in a byte with vcnt.8, we
4519 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4520 // and i64.
4521
4522 // Compute LSB - 1.
4523 SDValue Bits;
4524 if (ElemTy == MVT::i64) {
4525 // Load constant 0xffff'ffff'ffff'ffff to register.
4526 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4527 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4528 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4529 } else {
4530 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4531 DAG.getTargetConstant(1, dl, ElemTy));
4532 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4533 }
4534
4535 // Count #bits with vcnt.8.
4536 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4537 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4538 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4539
4540 // Gather the #bits with vpaddl (pairwise add.)
4541 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4542 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4543 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4544 Cnt8);
4545 if (ElemTy == MVT::i16)
4546 return Cnt16;
4547
4548 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4549 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4550 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4551 Cnt16);
4552 if (ElemTy == MVT::i32)
4553 return Cnt32;
4554
4555 assert(ElemTy == MVT::i64);
4556 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4557 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4558 Cnt32);
4559 return Cnt64;
4560 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004561
4562 if (!ST->hasV6T2Ops())
4563 return SDValue();
4564
James Molloyb5640982015-11-13 16:05:22 +00004565 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004566 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4567}
4568
Evan Chengb4eae132012-12-04 22:41:50 +00004569/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4570/// for each 16-bit element from operand, repeated. The basic idea is to
4571/// leverage vcnt to get the 8-bit counts, gather and add the results.
4572///
4573/// Trace for v4i16:
4574/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4575/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4576/// 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 +00004577/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004578/// [b0 b1 b2 b3 b4 b5 b6 b7]
4579/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4580/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4581/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4582static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4583 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004584 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004585
4586 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4587 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4588 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4589 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4590 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4591 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4592}
4593
4594/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4595/// bit-count for each 16-bit element from the operand. We need slightly
4596/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4597/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004598///
Evan Chengb4eae132012-12-04 22:41:50 +00004599/// Trace for v4i16:
4600/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4601/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4602/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4603/// v4i16:Extracted = [k0 k1 k2 k3 ]
4604static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4605 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004606 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004607
4608 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4609 if (VT.is64BitVector()) {
4610 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4611 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004612 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004613 } else {
4614 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004615 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004616 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4617 }
4618}
4619
4620/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4621/// bit-count for each 32-bit element from the operand. The idea here is
4622/// to split the vector into 16-bit elements, leverage the 16-bit count
4623/// routine, and then combine the results.
4624///
4625/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4626/// input = [v0 v1 ] (vi: 32-bit elements)
4627/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4628/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004629/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004630/// [k0 k1 k2 k3 ]
4631/// N1 =+[k1 k0 k3 k2 ]
4632/// [k0 k2 k1 k3 ]
4633/// N2 =+[k1 k3 k0 k2 ]
4634/// [k0 k2 k1 k3 ]
4635/// Extended =+[k1 k3 k0 k2 ]
4636/// [k0 k2 ]
4637/// Extracted=+[k1 k3 ]
4638///
4639static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4640 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004641 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004642
4643 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4644
4645 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4646 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4647 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4648 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4649 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4650
4651 if (VT.is64BitVector()) {
4652 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4653 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004654 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004655 } else {
4656 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004657 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004658 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4659 }
4660}
4661
4662static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4663 const ARMSubtarget *ST) {
4664 EVT VT = N->getValueType(0);
4665
4666 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004667 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4668 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004669 "Unexpected type for custom ctpop lowering");
4670
4671 if (VT.getVectorElementType() == MVT::i32)
4672 return lowerCTPOP32BitElements(N, DAG);
4673 else
4674 return lowerCTPOP16BitElements(N, DAG);
4675}
4676
Bob Wilson2e076c42009-06-22 23:27:02 +00004677static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4678 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004679 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004680 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004681
Bob Wilson7d471332010-11-18 21:16:28 +00004682 if (!VT.isVector())
4683 return SDValue();
4684
Bob Wilson2e076c42009-06-22 23:27:02 +00004685 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004686 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004687
Bob Wilson7d471332010-11-18 21:16:28 +00004688 // Left shifts translate directly to the vshiftu intrinsic.
4689 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004690 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004691 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4692 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004693 N->getOperand(0), N->getOperand(1));
4694
4695 assert((N->getOpcode() == ISD::SRA ||
4696 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4697
4698 // NEON uses the same intrinsics for both left and right shifts. For
4699 // right shifts, the shift amounts are negative, so negate the vector of
4700 // shift amounts.
4701 EVT ShiftVT = N->getOperand(1).getValueType();
4702 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4703 getZeroVector(ShiftVT, DAG, dl),
4704 N->getOperand(1));
4705 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4706 Intrinsic::arm_neon_vshifts :
4707 Intrinsic::arm_neon_vshiftu);
4708 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004709 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004710 N->getOperand(0), NegatedCount);
4711}
4712
4713static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4714 const ARMSubtarget *ST) {
4715 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004716 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004717
Eli Friedman682d8c12009-08-22 03:13:10 +00004718 // We can get here for a node like i32 = ISD::SHL i32, i64
4719 if (VT != MVT::i64)
4720 return SDValue();
4721
4722 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004723 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004724
Chris Lattnerf81d5882007-11-24 07:07:01 +00004725 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004726 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004727 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004728
Chris Lattnerf81d5882007-11-24 07:07:01 +00004729 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004730 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004731
Chris Lattnerf81d5882007-11-24 07:07:01 +00004732 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004733 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004734 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004735 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004736 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004737
Chris Lattnerf81d5882007-11-24 07:07:01 +00004738 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4739 // captures the result into a carry flag.
4740 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004741 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004742
Chris Lattnerf81d5882007-11-24 07:07:01 +00004743 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004744 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004745
Chris Lattnerf81d5882007-11-24 07:07:01 +00004746 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004747 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004748}
4749
Bob Wilson2e076c42009-06-22 23:27:02 +00004750static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4751 SDValue TmpOp0, TmpOp1;
4752 bool Invert = false;
4753 bool Swap = false;
4754 unsigned Opc = 0;
4755
4756 SDValue Op0 = Op.getOperand(0);
4757 SDValue Op1 = Op.getOperand(1);
4758 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004759 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004760 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004761 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004762 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004763
James Molloybf170092015-08-20 16:33:44 +00004764 if (CmpVT.getVectorElementType() == MVT::i64)
4765 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4766 // but it's possible that our operands are 64-bit but our result is 32-bit.
4767 // Bail in this case.
4768 return SDValue();
4769
Oliver Stannard51b1d462014-08-21 12:50:31 +00004770 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004771 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004772 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004773 case ISD::SETUNE:
4774 case ISD::SETNE: Invert = true; // Fallthrough
4775 case ISD::SETOEQ:
4776 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4777 case ISD::SETOLT:
4778 case ISD::SETLT: Swap = true; // Fallthrough
4779 case ISD::SETOGT:
4780 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4781 case ISD::SETOLE:
4782 case ISD::SETLE: Swap = true; // Fallthrough
4783 case ISD::SETOGE:
4784 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4785 case ISD::SETUGE: Swap = true; // Fallthrough
4786 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4787 case ISD::SETUGT: Swap = true; // Fallthrough
4788 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4789 case ISD::SETUEQ: Invert = true; // Fallthrough
4790 case ISD::SETONE:
4791 // Expand this to (OLT | OGT).
4792 TmpOp0 = Op0;
4793 TmpOp1 = Op1;
4794 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004795 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4796 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004797 break;
4798 case ISD::SETUO: Invert = true; // Fallthrough
4799 case ISD::SETO:
4800 // Expand this to (OLT | OGE).
4801 TmpOp0 = Op0;
4802 TmpOp1 = Op1;
4803 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004804 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4805 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004806 break;
4807 }
4808 } else {
4809 // Integer comparisons.
4810 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004811 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004812 case ISD::SETNE: Invert = true;
4813 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4814 case ISD::SETLT: Swap = true;
4815 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4816 case ISD::SETLE: Swap = true;
4817 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4818 case ISD::SETULT: Swap = true;
4819 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4820 case ISD::SETULE: Swap = true;
4821 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4822 }
4823
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004824 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004825 if (Opc == ARMISD::VCEQ) {
4826
4827 SDValue AndOp;
4828 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4829 AndOp = Op0;
4830 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4831 AndOp = Op1;
4832
4833 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004834 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004835 AndOp = AndOp.getOperand(0);
4836
4837 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4838 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004839 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4840 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004841 Invert = !Invert;
4842 }
4843 }
4844 }
4845
4846 if (Swap)
4847 std::swap(Op0, Op1);
4848
Owen Andersonc7baee32010-11-08 23:21:22 +00004849 // If one of the operands is a constant vector zero, attempt to fold the
4850 // comparison to a specialized compare-against-zero form.
4851 SDValue SingleOp;
4852 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4853 SingleOp = Op0;
4854 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4855 if (Opc == ARMISD::VCGE)
4856 Opc = ARMISD::VCLEZ;
4857 else if (Opc == ARMISD::VCGT)
4858 Opc = ARMISD::VCLTZ;
4859 SingleOp = Op1;
4860 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004861
Owen Andersonc7baee32010-11-08 23:21:22 +00004862 SDValue Result;
4863 if (SingleOp.getNode()) {
4864 switch (Opc) {
4865 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004866 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004867 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004868 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004869 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004870 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004871 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004872 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004873 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004874 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004875 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004876 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004877 }
4878 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004879 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004880 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004881
Tim Northover45aa89c2015-02-08 00:50:47 +00004882 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4883
Bob Wilson2e076c42009-06-22 23:27:02 +00004884 if (Invert)
4885 Result = DAG.getNOT(dl, Result, VT);
4886
4887 return Result;
4888}
4889
Bob Wilson5b2b5042010-06-14 22:19:57 +00004890/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4891/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004892/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004893static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4894 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004895 SDLoc dl, EVT &VT, bool is128Bits,
4896 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004897 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004898
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004899 // SplatBitSize is set to the smallest size that splats the vector, so a
4900 // zero vector will always have SplatBitSize == 8. However, NEON modified
4901 // immediate instructions others than VMOV do not support the 8-bit encoding
4902 // of a zero vector, and the default encoding of zero is supposed to be the
4903 // 32-bit version.
4904 if (SplatBits == 0)
4905 SplatBitSize = 32;
4906
Bob Wilson2e076c42009-06-22 23:27:02 +00004907 switch (SplatBitSize) {
4908 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004909 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004910 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004911 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004912 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004913 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004914 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004915 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004916 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004917
4918 case 16:
4919 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004920 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004921 if ((SplatBits & ~0xff) == 0) {
4922 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004923 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004924 Imm = SplatBits;
4925 break;
4926 }
4927 if ((SplatBits & ~0xff00) == 0) {
4928 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004929 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004930 Imm = SplatBits >> 8;
4931 break;
4932 }
4933 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004934
4935 case 32:
4936 // NEON's 32-bit VMOV supports splat values where:
4937 // * only one byte is nonzero, or
4938 // * the least significant byte is 0xff and the second byte is nonzero, or
4939 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004940 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004941 if ((SplatBits & ~0xff) == 0) {
4942 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004943 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004944 Imm = SplatBits;
4945 break;
4946 }
4947 if ((SplatBits & ~0xff00) == 0) {
4948 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004949 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004950 Imm = SplatBits >> 8;
4951 break;
4952 }
4953 if ((SplatBits & ~0xff0000) == 0) {
4954 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004955 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004956 Imm = SplatBits >> 16;
4957 break;
4958 }
4959 if ((SplatBits & ~0xff000000) == 0) {
4960 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004961 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004962 Imm = SplatBits >> 24;
4963 break;
4964 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004965
Owen Andersona4076922010-11-05 21:57:54 +00004966 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4967 if (type == OtherModImm) return SDValue();
4968
Bob Wilson2e076c42009-06-22 23:27:02 +00004969 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004970 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4971 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004972 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004973 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004974 break;
4975 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004976
4977 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004978 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4979 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004980 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004981 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004982 break;
4983 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004984
4985 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4986 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4987 // VMOV.I32. A (very) minor optimization would be to replicate the value
4988 // and fall through here to test for a valid 64-bit splat. But, then the
4989 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004990 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004991
4992 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004993 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004994 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004995 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004996 uint64_t BitMask = 0xff;
4997 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004998 unsigned ImmMask = 1;
4999 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005000 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005001 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005002 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005003 Imm |= ImmMask;
5004 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005005 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005006 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005007 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005008 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005009 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005010
Mehdi Aminiffc14022015-07-08 01:00:38 +00005011 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005012 // swap higher and lower 32 bit word
5013 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5014
Bob Wilson6eae5202010-06-11 21:34:50 +00005015 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005016 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005017 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005018 break;
5019 }
5020
Bob Wilson6eae5202010-06-11 21:34:50 +00005021 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005022 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005023 }
5024
Bob Wilsona3f19012010-07-13 21:16:48 +00005025 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005026 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005027}
5028
Lang Hames591cdaf2012-03-29 21:56:11 +00005029SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5030 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005031 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005032 return SDValue();
5033
Tim Northoverf79c3a52013-08-20 08:57:11 +00005034 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005035 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005036
Oliver Stannard51b1d462014-08-21 12:50:31 +00005037 // Use the default (constant pool) lowering for double constants when we have
5038 // an SP-only FPU
5039 if (IsDouble && Subtarget->isFPOnlySP())
5040 return SDValue();
5041
Lang Hames591cdaf2012-03-29 21:56:11 +00005042 // Try splatting with a VMOV.f32...
5043 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005044 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5045
Lang Hames591cdaf2012-03-29 21:56:11 +00005046 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005047 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5048 // We have code in place to select a valid ConstantFP already, no need to
5049 // do any mangling.
5050 return Op;
5051 }
5052
5053 // It's a float and we are trying to use NEON operations where
5054 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005055 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005056 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005057 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5058 NewVal);
5059 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005060 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005061 }
5062
Tim Northoverf79c3a52013-08-20 08:57:11 +00005063 // The rest of our options are NEON only, make sure that's allowed before
5064 // proceeding..
5065 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5066 return SDValue();
5067
Lang Hames591cdaf2012-03-29 21:56:11 +00005068 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005069 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5070
5071 // It wouldn't really be worth bothering for doubles except for one very
5072 // important value, which does happen to match: 0.0. So make sure we don't do
5073 // anything stupid.
5074 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5075 return SDValue();
5076
5077 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005078 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5079 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005080 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005081 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005082 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5083 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005084 if (IsDouble)
5085 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5086
5087 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005088 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5089 VecConstant);
5090 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005091 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005092 }
5093
5094 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005095 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005096 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005097 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005098 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005099 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005100
5101 if (IsDouble)
5102 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5103
5104 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005105 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5106 VecConstant);
5107 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005108 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005109 }
5110
5111 return SDValue();
5112}
5113
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005114// check if an VEXT instruction can handle the shuffle mask when the
5115// vector sources of the shuffle are the same.
5116static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5117 unsigned NumElts = VT.getVectorNumElements();
5118
5119 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5120 if (M[0] < 0)
5121 return false;
5122
5123 Imm = M[0];
5124
5125 // If this is a VEXT shuffle, the immediate value is the index of the first
5126 // element. The other shuffle indices must be the successive elements after
5127 // the first one.
5128 unsigned ExpectedElt = Imm;
5129 for (unsigned i = 1; i < NumElts; ++i) {
5130 // Increment the expected index. If it wraps around, just follow it
5131 // back to index zero and keep going.
5132 ++ExpectedElt;
5133 if (ExpectedElt == NumElts)
5134 ExpectedElt = 0;
5135
5136 if (M[i] < 0) continue; // ignore UNDEF indices
5137 if (ExpectedElt != static_cast<unsigned>(M[i]))
5138 return false;
5139 }
5140
5141 return true;
5142}
5143
Lang Hames591cdaf2012-03-29 21:56:11 +00005144
Benjamin Kramer339ced42012-01-15 13:16:05 +00005145static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005146 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005147 unsigned NumElts = VT.getVectorNumElements();
5148 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005149
5150 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5151 if (M[0] < 0)
5152 return false;
5153
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005154 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005155
5156 // If this is a VEXT shuffle, the immediate value is the index of the first
5157 // element. The other shuffle indices must be the successive elements after
5158 // the first one.
5159 unsigned ExpectedElt = Imm;
5160 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005161 // Increment the expected index. If it wraps around, it may still be
5162 // a VEXT but the source vectors must be swapped.
5163 ExpectedElt += 1;
5164 if (ExpectedElt == NumElts * 2) {
5165 ExpectedElt = 0;
5166 ReverseVEXT = true;
5167 }
5168
Bob Wilson411dfad2010-08-17 05:54:34 +00005169 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005170 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005171 return false;
5172 }
5173
5174 // Adjust the index value if the source operands will be swapped.
5175 if (ReverseVEXT)
5176 Imm -= NumElts;
5177
Bob Wilson32cd8552009-08-19 17:03:43 +00005178 return true;
5179}
5180
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005181/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5182/// instruction with the specified blocksize. (The order of the elements
5183/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005184static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005185 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5186 "Only possible block sizes for VREV are: 16, 32, 64");
5187
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005188 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005189 if (EltSz == 64)
5190 return false;
5191
5192 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005193 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005194 // If the first shuffle index is UNDEF, be optimistic.
5195 if (M[0] < 0)
5196 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005197
5198 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5199 return false;
5200
5201 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005202 if (M[i] < 0) continue; // ignore UNDEF indices
5203 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005204 return false;
5205 }
5206
5207 return true;
5208}
5209
Benjamin Kramer339ced42012-01-15 13:16:05 +00005210static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005211 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5212 // range, then 0 is placed into the resulting vector. So pretty much any mask
5213 // of 8 elements can work here.
5214 return VT == MVT::v8i8 && M.size() == 8;
5215}
5216
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005217// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5218// checking that pairs of elements in the shuffle mask represent the same index
5219// in each vector, incrementing the expected index by 2 at each step.
5220// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5221// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5222// v2={e,f,g,h}
5223// WhichResult gives the offset for each element in the mask based on which
5224// of the two results it belongs to.
5225//
5226// The transpose can be represented either as:
5227// result1 = shufflevector v1, v2, result1_shuffle_mask
5228// result2 = shufflevector v1, v2, result2_shuffle_mask
5229// where v1/v2 and the shuffle masks have the same number of elements
5230// (here WhichResult (see below) indicates which result is being checked)
5231//
5232// or as:
5233// results = shufflevector v1, v2, shuffle_mask
5234// where both results are returned in one vector and the shuffle mask has twice
5235// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5236// want to check the low half and high half of the shuffle mask as if it were
5237// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005238static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005239 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5240 if (EltSz == 64)
5241 return false;
5242
Bob Wilsona7062312009-08-21 20:54:19 +00005243 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005244 if (M.size() != NumElts && M.size() != NumElts*2)
5245 return false;
5246
James Molloy8c995a92015-09-10 08:42:28 +00005247 // If the mask is twice as long as the input vector then we need to check the
5248 // upper and lower parts of the mask with a matching value for WhichResult
5249 // FIXME: A mask with only even values will be rejected in case the first
5250 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5251 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005252 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005253 if (M.size() == NumElts * 2)
5254 WhichResult = i / NumElts;
5255 else
5256 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005257 for (unsigned j = 0; j < NumElts; j += 2) {
5258 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5259 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5260 return false;
5261 }
Bob Wilsona7062312009-08-21 20:54:19 +00005262 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005263
5264 if (M.size() == NumElts*2)
5265 WhichResult = 0;
5266
Bob Wilsona7062312009-08-21 20:54:19 +00005267 return true;
5268}
5269
Bob Wilson0bbd3072009-12-03 06:40:55 +00005270/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5271/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5272/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005273static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005274 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5275 if (EltSz == 64)
5276 return false;
5277
5278 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005279 if (M.size() != NumElts && M.size() != NumElts*2)
5280 return false;
5281
5282 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005283 if (M.size() == NumElts * 2)
5284 WhichResult = i / NumElts;
5285 else
5286 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005287 for (unsigned j = 0; j < NumElts; j += 2) {
5288 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5289 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5290 return false;
5291 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005292 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005293
5294 if (M.size() == NumElts*2)
5295 WhichResult = 0;
5296
Bob Wilson0bbd3072009-12-03 06:40:55 +00005297 return true;
5298}
5299
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005300// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5301// that the mask elements are either all even and in steps of size 2 or all odd
5302// and in steps of size 2.
5303// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5304// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5305// v2={e,f,g,h}
5306// Requires similar checks to that of isVTRNMask with
5307// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005308static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005309 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5310 if (EltSz == 64)
5311 return false;
5312
Bob Wilsona7062312009-08-21 20:54:19 +00005313 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005314 if (M.size() != NumElts && M.size() != NumElts*2)
5315 return false;
5316
5317 for (unsigned i = 0; i < M.size(); i += NumElts) {
5318 WhichResult = M[i] == 0 ? 0 : 1;
5319 for (unsigned j = 0; j < NumElts; ++j) {
5320 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5321 return false;
5322 }
Bob Wilsona7062312009-08-21 20:54:19 +00005323 }
5324
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005325 if (M.size() == NumElts*2)
5326 WhichResult = 0;
5327
Bob Wilsona7062312009-08-21 20:54:19 +00005328 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005329 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005330 return false;
5331
5332 return true;
5333}
5334
Bob Wilson0bbd3072009-12-03 06:40:55 +00005335/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5336/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5337/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005338static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005339 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5340 if (EltSz == 64)
5341 return false;
5342
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005343 unsigned NumElts = VT.getVectorNumElements();
5344 if (M.size() != NumElts && M.size() != NumElts*2)
5345 return false;
5346
5347 unsigned Half = NumElts / 2;
5348 for (unsigned i = 0; i < M.size(); i += NumElts) {
5349 WhichResult = M[i] == 0 ? 0 : 1;
5350 for (unsigned j = 0; j < NumElts; j += Half) {
5351 unsigned Idx = WhichResult;
5352 for (unsigned k = 0; k < Half; ++k) {
5353 int MIdx = M[i + j + k];
5354 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5355 return false;
5356 Idx += 2;
5357 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005358 }
5359 }
5360
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005361 if (M.size() == NumElts*2)
5362 WhichResult = 0;
5363
Bob Wilson0bbd3072009-12-03 06:40:55 +00005364 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5365 if (VT.is64BitVector() && EltSz == 32)
5366 return false;
5367
5368 return true;
5369}
5370
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005371// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5372// that pairs of elements of the shufflemask represent the same index in each
5373// vector incrementing sequentially through the vectors.
5374// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5375// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5376// v2={e,f,g,h}
5377// Requires similar checks to that of isVTRNMask with respect the how results
5378// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005379static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005380 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5381 if (EltSz == 64)
5382 return false;
5383
Bob Wilsona7062312009-08-21 20:54:19 +00005384 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005385 if (M.size() != NumElts && M.size() != NumElts*2)
5386 return false;
5387
5388 for (unsigned i = 0; i < M.size(); i += NumElts) {
5389 WhichResult = M[i] == 0 ? 0 : 1;
5390 unsigned Idx = WhichResult * NumElts / 2;
5391 for (unsigned j = 0; j < NumElts; j += 2) {
5392 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5393 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5394 return false;
5395 Idx += 1;
5396 }
Bob Wilsona7062312009-08-21 20:54:19 +00005397 }
5398
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005399 if (M.size() == NumElts*2)
5400 WhichResult = 0;
5401
Bob Wilsona7062312009-08-21 20:54:19 +00005402 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005403 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005404 return false;
5405
5406 return true;
5407}
5408
Bob Wilson0bbd3072009-12-03 06:40:55 +00005409/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5410/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5411/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005412static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005413 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5414 if (EltSz == 64)
5415 return false;
5416
5417 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005418 if (M.size() != NumElts && M.size() != NumElts*2)
5419 return false;
5420
5421 for (unsigned i = 0; i < M.size(); i += NumElts) {
5422 WhichResult = M[i] == 0 ? 0 : 1;
5423 unsigned Idx = WhichResult * NumElts / 2;
5424 for (unsigned j = 0; j < NumElts; j += 2) {
5425 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5426 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5427 return false;
5428 Idx += 1;
5429 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005430 }
5431
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005432 if (M.size() == NumElts*2)
5433 WhichResult = 0;
5434
Bob Wilson0bbd3072009-12-03 06:40:55 +00005435 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5436 if (VT.is64BitVector() && EltSz == 32)
5437 return false;
5438
5439 return true;
5440}
5441
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005442/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5443/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5444static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5445 unsigned &WhichResult,
5446 bool &isV_UNDEF) {
5447 isV_UNDEF = false;
5448 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5449 return ARMISD::VTRN;
5450 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5451 return ARMISD::VUZP;
5452 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5453 return ARMISD::VZIP;
5454
5455 isV_UNDEF = true;
5456 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5457 return ARMISD::VTRN;
5458 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5459 return ARMISD::VUZP;
5460 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5461 return ARMISD::VZIP;
5462
5463 return 0;
5464}
5465
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005466/// \return true if this is a reverse operation on an vector.
5467static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5468 unsigned NumElts = VT.getVectorNumElements();
5469 // Make sure the mask has the right size.
5470 if (NumElts != M.size())
5471 return false;
5472
5473 // Look for <15, ..., 3, -1, 1, 0>.
5474 for (unsigned i = 0; i != NumElts; ++i)
5475 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5476 return false;
5477
5478 return true;
5479}
5480
Dale Johannesen2bff5052010-07-29 20:10:08 +00005481// If N is an integer constant that can be moved into a register in one
5482// instruction, return an SDValue of such a constant (will become a MOV
5483// instruction). Otherwise return null.
5484static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005485 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005486 uint64_t Val;
5487 if (!isa<ConstantSDNode>(N))
5488 return SDValue();
5489 Val = cast<ConstantSDNode>(N)->getZExtValue();
5490
5491 if (ST->isThumb1Only()) {
5492 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005493 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005494 } else {
5495 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005496 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005497 }
5498 return SDValue();
5499}
5500
Bob Wilson2e076c42009-06-22 23:27:02 +00005501// If this is a case we can't handle, return null and let the default
5502// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005503SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5504 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005505 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005506 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005507 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005508
5509 APInt SplatBits, SplatUndef;
5510 unsigned SplatBitSize;
5511 bool HasAnyUndefs;
5512 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005513 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005514 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005515 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005516 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005517 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005518 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005519 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005520 if (Val.getNode()) {
5521 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005522 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005523 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005524
5525 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005526 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005527 Val = isNEONModifiedImm(NegatedImm,
5528 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005529 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005530 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005531 if (Val.getNode()) {
5532 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005533 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005534 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005535
5536 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005537 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005538 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005539 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005540 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005541 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5542 }
5543 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005544 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005545 }
5546
Bob Wilson91fdf682010-05-22 00:23:12 +00005547 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005548 //
5549 // As an optimisation, even if more than one value is used it may be more
5550 // profitable to splat with one value then change some lanes.
5551 //
5552 // Heuristically we decide to do this if the vector has a "dominant" value,
5553 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005554 unsigned NumElts = VT.getVectorNumElements();
5555 bool isOnlyLowElement = true;
5556 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005557 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005558 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005559
5560 // Map of the number of times a particular SDValue appears in the
5561 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005562 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005563 SDValue Value;
5564 for (unsigned i = 0; i < NumElts; ++i) {
5565 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005566 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00005567 continue;
5568 if (i > 0)
5569 isOnlyLowElement = false;
5570 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5571 isConstant = false;
5572
James Molloy49bdbce2012-09-06 09:55:02 +00005573 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005574 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005575
James Molloy49bdbce2012-09-06 09:55:02 +00005576 // Is this value dominant? (takes up more than half of the lanes)
5577 if (++Count > (NumElts / 2)) {
5578 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005579 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005580 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005581 }
James Molloy49bdbce2012-09-06 09:55:02 +00005582 if (ValueCounts.size() != 1)
5583 usesOnlyOneValue = false;
5584 if (!Value.getNode() && ValueCounts.size() > 0)
5585 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005586
James Molloy49bdbce2012-09-06 09:55:02 +00005587 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005588 return DAG.getUNDEF(VT);
5589
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005590 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5591 // Keep going if we are hitting this case.
5592 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005593 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5594
Dale Johannesen2bff5052010-07-29 20:10:08 +00005595 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5596
Dale Johannesen710a2d92010-10-19 20:00:17 +00005597 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5598 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005599 if (hasDominantValue && EltSize <= 32) {
5600 if (!isConstant) {
5601 SDValue N;
5602
5603 // If we are VDUPing a value that comes directly from a vector, that will
5604 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005605 // just use VDUPLANE. We can only do this if the lane being extracted
5606 // is at a constant index, as the VDUP from lane instructions only have
5607 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005608 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005609 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005610 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005611 // We need to create a new undef vector to use for the VDUPLANE if the
5612 // size of the vector from which we get the value is different than the
5613 // size of the vector that we need to create. We will insert the element
5614 // such that the register coalescer will remove unnecessary copies.
5615 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005616 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5617 VT.getVectorNumElements();
5618 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5619 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005620 Value, DAG.getConstant(index, dl, MVT::i32)),
5621 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005622 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005623 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005624 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005625 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005626 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5627
5628 if (!usesOnlyOneValue) {
5629 // The dominant value was splatted as 'N', but we now have to insert
5630 // all differing elements.
5631 for (unsigned I = 0; I < NumElts; ++I) {
5632 if (Op.getOperand(I) == Value)
5633 continue;
5634 SmallVector<SDValue, 3> Ops;
5635 Ops.push_back(N);
5636 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005637 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005638 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005639 }
5640 }
5641 return N;
5642 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005643 if (VT.getVectorElementType().isFloatingPoint()) {
5644 SmallVector<SDValue, 8> Ops;
5645 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005646 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005647 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005648 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005649 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005650 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5651 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005652 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005653 }
James Molloy49bdbce2012-09-06 09:55:02 +00005654 if (usesOnlyOneValue) {
5655 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5656 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005657 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005658 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005659 }
5660
5661 // If all elements are constants and the case above didn't get hit, fall back
5662 // to the default expansion, which will generate a load from the constant
5663 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005664 if (isConstant)
5665 return SDValue();
5666
Bob Wilson6f2b8962011-01-07 21:37:30 +00005667 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5668 if (NumElts >= 4) {
5669 SDValue shuffle = ReconstructShuffle(Op, DAG);
5670 if (shuffle != SDValue())
5671 return shuffle;
5672 }
5673
Bob Wilson91fdf682010-05-22 00:23:12 +00005674 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005675 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5676 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005677 if (EltSize >= 32) {
5678 // Do the expansion with floating-point types, since that is what the VFP
5679 // registers are defined to use, and since i64 is not legal.
5680 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5681 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005682 SmallVector<SDValue, 8> Ops;
5683 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005684 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005685 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005686 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005687 }
5688
Jim Grosbach24e102a2013-07-08 18:18:52 +00005689 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5690 // know the default expansion would otherwise fall back on something even
5691 // worse. For a vector with one or two non-undef values, that's
5692 // scalar_to_vector for the elements followed by a shuffle (provided the
5693 // shuffle is valid for the target) and materialization element by element
5694 // on the stack followed by a load for everything else.
5695 if (!isConstant && !usesOnlyOneValue) {
5696 SDValue Vec = DAG.getUNDEF(VT);
5697 for (unsigned i = 0 ; i < NumElts; ++i) {
5698 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005699 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00005700 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005701 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005702 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5703 }
5704 return Vec;
5705 }
5706
Bob Wilson2e076c42009-06-22 23:27:02 +00005707 return SDValue();
5708}
5709
Bob Wilson6f2b8962011-01-07 21:37:30 +00005710// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005711// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005712SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5713 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005714 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005715 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005716 EVT VT = Op.getValueType();
5717 unsigned NumElts = VT.getVectorNumElements();
5718
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005719 struct ShuffleSourceInfo {
5720 SDValue Vec;
5721 unsigned MinElt;
5722 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005723
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005724 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5725 // be compatible with the shuffle we intend to construct. As a result
5726 // ShuffleVec will be some sliding window into the original Vec.
5727 SDValue ShuffleVec;
5728
5729 // Code should guarantee that element i in Vec starts at element "WindowBase
5730 // + i * WindowScale in ShuffleVec".
5731 int WindowBase;
5732 int WindowScale;
5733
5734 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5735 ShuffleSourceInfo(SDValue Vec)
5736 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5737 WindowScale(1) {}
5738 };
5739
5740 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5741 // node.
5742 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005743 for (unsigned i = 0; i < NumElts; ++i) {
5744 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005745 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005746 continue;
5747 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5748 // A shuffle can only come from building a vector from various
5749 // elements of other vectors.
5750 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005751 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5752 // Furthermore, shuffles require a constant mask, whereas extractelts
5753 // accept variable indices.
5754 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005755 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005756
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005757 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005758 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005759 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5760 if (Source == Sources.end())
5761 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005762
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005763 // Update the minimum and maximum lane number seen.
5764 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5765 Source->MinElt = std::min(Source->MinElt, EltNo);
5766 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005767 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005768
Bob Wilson6f2b8962011-01-07 21:37:30 +00005769 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005770 // are involved.
5771 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005772 return SDValue();
5773
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005774 // Find out the smallest element size among result and two sources, and use
5775 // it as element size to build the shuffle_vector.
5776 EVT SmallestEltTy = VT.getVectorElementType();
5777 for (auto &Source : Sources) {
5778 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5779 if (SrcEltTy.bitsLT(SmallestEltTy))
5780 SmallestEltTy = SrcEltTy;
5781 }
5782 unsigned ResMultiplier =
5783 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5784 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5785 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005786
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005787 // If the source vector is too wide or too narrow, we may nevertheless be able
5788 // to construct a compatible shuffle either by concatenating it with UNDEF or
5789 // extracting a suitable range of elements.
5790 for (auto &Src : Sources) {
5791 EVT SrcVT = Src.ShuffleVec.getValueType();
5792
5793 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005794 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005795
5796 // This stage of the search produces a source with the same element type as
5797 // the original, but with a total width matching the BUILD_VECTOR output.
5798 EVT EltVT = SrcVT.getVectorElementType();
5799 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5800 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5801
5802 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5803 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5804 return SDValue();
5805 // We can pad out the smaller vector for free, so if it's part of a
5806 // shuffle...
5807 Src.ShuffleVec =
5808 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5809 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5810 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005811 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005812
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005813 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5814 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005815
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005816 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005817 // Span too large for a VEXT to cope
5818 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005819 }
5820
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005821 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005822 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005823 Src.ShuffleVec =
5824 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5825 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5826 Src.WindowBase = -NumSrcElts;
5827 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005828 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005829 Src.ShuffleVec =
5830 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5831 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005832 } else {
5833 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005834 SDValue VEXTSrc1 =
5835 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5836 DAG.getConstant(0, dl, MVT::i32));
5837 SDValue VEXTSrc2 =
5838 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5839 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005840
5841 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5842 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005843 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005844 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005845 }
5846 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005847
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005848 // Another possible incompatibility occurs from the vector element types. We
5849 // can fix this by bitcasting the source vectors to the same type we intend
5850 // for the shuffle.
5851 for (auto &Src : Sources) {
5852 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5853 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005854 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005855 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5856 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5857 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5858 Src.WindowBase *= Src.WindowScale;
5859 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005860
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005861 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005862 DEBUG(
5863 for (auto Src : Sources)
5864 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5865 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005866
5867 // The stars all align, our next step is to produce the mask for the shuffle.
5868 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5869 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5870 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5871 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005872 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005873 continue;
5874
5875 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5876 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5877
5878 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5879 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5880 // segment.
5881 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5882 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5883 VT.getVectorElementType().getSizeInBits());
5884 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5885
5886 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5887 // starting at the appropriate offset.
5888 int *LaneMask = &Mask[i * ResMultiplier];
5889
5890 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5891 ExtractBase += NumElts * (Src - Sources.begin());
5892 for (int j = 0; j < LanesDefined; ++j)
5893 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005894 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005895
Bob Wilson6f2b8962011-01-07 21:37:30 +00005896 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005897 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5898 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005899
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005900 // We can't handle more than two sources. This should have already
5901 // been checked before this point.
5902 assert(Sources.size() <= 2 && "Too many sources!");
5903
5904 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5905 for (unsigned i = 0; i < Sources.size(); ++i)
5906 ShuffleOps[i] = Sources[i].ShuffleVec;
5907
5908 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5909 ShuffleOps[1], &Mask[0]);
5910 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005911}
5912
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005913/// isShuffleMaskLegal - Targets can use this to indicate that they only
5914/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5915/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5916/// are assumed to be legal.
5917bool
5918ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5919 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005920 if (VT.getVectorNumElements() == 4 &&
5921 (VT.is128BitVector() || VT.is64BitVector())) {
5922 unsigned PFIndexes[4];
5923 for (unsigned i = 0; i != 4; ++i) {
5924 if (M[i] < 0)
5925 PFIndexes[i] = 8;
5926 else
5927 PFIndexes[i] = M[i];
5928 }
5929
5930 // Compute the index in the perfect shuffle table.
5931 unsigned PFTableIndex =
5932 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5933 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5934 unsigned Cost = (PFEntry >> 30);
5935
5936 if (Cost <= 4)
5937 return true;
5938 }
5939
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005940 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005941 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005942
Bob Wilson846bd792010-06-07 23:53:38 +00005943 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5944 return (EltSize >= 32 ||
5945 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005946 isVREVMask(M, VT, 64) ||
5947 isVREVMask(M, VT, 32) ||
5948 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005949 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005950 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005951 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005952 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005953}
5954
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005955/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5956/// the specified operations to build the shuffle.
5957static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5958 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005959 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005960 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5961 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5962 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5963
5964 enum {
5965 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5966 OP_VREV,
5967 OP_VDUP0,
5968 OP_VDUP1,
5969 OP_VDUP2,
5970 OP_VDUP3,
5971 OP_VEXT1,
5972 OP_VEXT2,
5973 OP_VEXT3,
5974 OP_VUZPL, // VUZP, left result
5975 OP_VUZPR, // VUZP, right result
5976 OP_VZIPL, // VZIP, left result
5977 OP_VZIPR, // VZIP, right result
5978 OP_VTRNL, // VTRN, left result
5979 OP_VTRNR // VTRN, right result
5980 };
5981
5982 if (OpNum == OP_COPY) {
5983 if (LHSID == (1*9+2)*9+3) return LHS;
5984 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5985 return RHS;
5986 }
5987
5988 SDValue OpLHS, OpRHS;
5989 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5990 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5991 EVT VT = OpLHS.getValueType();
5992
5993 switch (OpNum) {
5994 default: llvm_unreachable("Unknown shuffle opcode!");
5995 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005996 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005997 if (VT.getVectorElementType() == MVT::i32 ||
5998 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005999 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6000 // vrev <4 x i16> -> VREV32
6001 if (VT.getVectorElementType() == MVT::i16)
6002 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6003 // vrev <4 x i8> -> VREV16
6004 assert(VT.getVectorElementType() == MVT::i8);
6005 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006006 case OP_VDUP0:
6007 case OP_VDUP1:
6008 case OP_VDUP2:
6009 case OP_VDUP3:
6010 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006011 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006012 case OP_VEXT1:
6013 case OP_VEXT2:
6014 case OP_VEXT3:
6015 return DAG.getNode(ARMISD::VEXT, dl, VT,
6016 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006017 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006018 case OP_VUZPL:
6019 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006020 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006021 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6022 case OP_VZIPL:
6023 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006024 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006025 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6026 case OP_VTRNL:
6027 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006028 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6029 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006030 }
6031}
6032
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006033static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006034 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006035 SelectionDAG &DAG) {
6036 // Check to see if we can use the VTBL instruction.
6037 SDValue V1 = Op.getOperand(0);
6038 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006039 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006040
6041 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006042 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006043 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006044 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006045
Sanjay Patel57195842016-03-14 17:28:46 +00006046 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006047 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00006048 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006049
Owen Anderson77aa2662011-04-05 21:48:57 +00006050 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00006051 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006052}
6053
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006054static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6055 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006056 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006057 SDValue OpLHS = Op.getOperand(0);
6058 EVT VT = OpLHS.getValueType();
6059
6060 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6061 "Expect an v8i16/v16i8 type");
6062 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6063 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6064 // extract the first 8 bytes into the top double word and the last 8 bytes
6065 // into the bottom double word. The v8i16 case is similar.
6066 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6067 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006068 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006069}
6070
Bob Wilson2e076c42009-06-22 23:27:02 +00006071static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006072 SDValue V1 = Op.getOperand(0);
6073 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006075 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006076 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006077
Bob Wilsonc6800b52009-08-13 02:13:04 +00006078 // Convert shuffles that are directly supported on NEON to target-specific
6079 // DAG nodes, instead of keeping them as shuffles and matching them again
6080 // during code selection. This is more efficient and avoids the possibility
6081 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006082 // FIXME: floating-point vectors should be canonicalized to integer vectors
6083 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006084 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006085
Bob Wilson846bd792010-06-07 23:53:38 +00006086 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6087 if (EltSize <= 32) {
6088 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
6089 int Lane = SVN->getSplatIndex();
6090 // If this is undef splat, generate it via "just" vdup, if possible.
6091 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006092
Dan Gohman198b7ff2011-11-03 21:49:52 +00006093 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006094 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6095 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6096 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006097 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6098 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6099 // reaches it).
6100 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6101 !isa<ConstantSDNode>(V1.getOperand(0))) {
6102 bool IsScalarToVector = true;
6103 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006104 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006105 IsScalarToVector = false;
6106 break;
6107 }
6108 if (IsScalarToVector)
6109 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6110 }
Bob Wilson846bd792010-06-07 23:53:38 +00006111 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006112 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006113 }
Bob Wilson846bd792010-06-07 23:53:38 +00006114
6115 bool ReverseVEXT;
6116 unsigned Imm;
6117 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6118 if (ReverseVEXT)
6119 std::swap(V1, V2);
6120 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006121 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006122 }
6123
6124 if (isVREVMask(ShuffleMask, VT, 64))
6125 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6126 if (isVREVMask(ShuffleMask, VT, 32))
6127 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6128 if (isVREVMask(ShuffleMask, VT, 16))
6129 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6130
Sanjay Patel57195842016-03-14 17:28:46 +00006131 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006132 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006133 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006134 }
6135
Bob Wilson846bd792010-06-07 23:53:38 +00006136 // Check for Neon shuffles that modify both input vectors in place.
6137 // If both results are used, i.e., if there are two shuffles with the same
6138 // source operands and with masks corresponding to both results of one of
6139 // these operations, DAG memoization will ensure that a single node is
6140 // used for both shuffles.
6141 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006142 bool isV_UNDEF;
6143 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6144 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6145 if (isV_UNDEF)
6146 V2 = V1;
6147 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6148 .getValue(WhichResult);
6149 }
Bob Wilson846bd792010-06-07 23:53:38 +00006150
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006151 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6152 // shuffles that produce a result larger than their operands with:
6153 // shuffle(concat(v1, undef), concat(v2, undef))
6154 // ->
6155 // shuffle(concat(v1, v2), undef)
6156 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6157 //
6158 // This is useful in the general case, but there are special cases where
6159 // native shuffles produce larger results: the two-result ops.
6160 //
6161 // Look through the concat when lowering them:
6162 // shuffle(concat(v1, v2), undef)
6163 // ->
6164 // concat(VZIP(v1, v2):0, :1)
6165 //
Sanjay Patel57195842016-03-14 17:28:46 +00006166 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006167 SDValue SubV1 = V1->getOperand(0);
6168 SDValue SubV2 = V1->getOperand(1);
6169 EVT SubVT = SubV1.getValueType();
6170
6171 // We expect these to have been canonicalized to -1.
6172 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
6173 return i < (int)VT.getVectorNumElements();
6174 }) && "Unexpected shuffle index into UNDEF operand!");
6175
6176 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6177 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6178 if (isV_UNDEF)
6179 SubV2 = SubV1;
6180 assert((WhichResult == 0) &&
6181 "In-place shuffle of concat can only have one result!");
6182 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6183 SubV1, SubV2);
6184 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6185 Res.getValue(1));
6186 }
6187 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006188 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006189
Bob Wilsona7062312009-08-21 20:54:19 +00006190 // If the shuffle is not directly supported and it has 4 elements, use
6191 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006192 unsigned NumElts = VT.getVectorNumElements();
6193 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006194 unsigned PFIndexes[4];
6195 for (unsigned i = 0; i != 4; ++i) {
6196 if (ShuffleMask[i] < 0)
6197 PFIndexes[i] = 8;
6198 else
6199 PFIndexes[i] = ShuffleMask[i];
6200 }
6201
6202 // Compute the index in the perfect shuffle table.
6203 unsigned PFTableIndex =
6204 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006205 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6206 unsigned Cost = (PFEntry >> 30);
6207
6208 if (Cost <= 4)
6209 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6210 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006211
Bob Wilsond8a9a042010-06-04 00:04:02 +00006212 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006213 if (EltSize >= 32) {
6214 // Do the expansion with floating-point types, since that is what the VFP
6215 // registers are defined to use, and since i64 is not legal.
6216 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6217 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006218 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6219 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006220 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006221 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006222 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006223 Ops.push_back(DAG.getUNDEF(EltVT));
6224 else
6225 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6226 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6227 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006228 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006229 }
Craig Topper48d114b2014-04-26 18:35:24 +00006230 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006231 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006232 }
6233
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006234 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6235 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6236
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006237 if (VT == MVT::v8i8)
6238 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006239 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006240
Bob Wilson6f34e272009-08-14 05:16:33 +00006241 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006242}
6243
Eli Friedmana5e244c2011-10-24 23:08:52 +00006244static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6245 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6246 SDValue Lane = Op.getOperand(2);
6247 if (!isa<ConstantSDNode>(Lane))
6248 return SDValue();
6249
6250 return Op;
6251}
6252
Bob Wilson2e076c42009-06-22 23:27:02 +00006253static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006254 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006255 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006256 if (!isa<ConstantSDNode>(Lane))
6257 return SDValue();
6258
6259 SDValue Vec = Op.getOperand(0);
6260 if (Op.getValueType() == MVT::i32 &&
6261 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006262 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006263 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6264 }
6265
6266 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006267}
6268
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006269static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6270 // The only time a CONCAT_VECTORS operation can have legal types is when
6271 // two 64-bit vectors are concatenated to a 128-bit vector.
6272 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6273 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006274 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006275 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006276 SDValue Op0 = Op.getOperand(0);
6277 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006278 if (!Op0.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006279 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006280 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006281 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006282 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006283 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006284 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006285 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006286 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006287}
6288
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006289/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6290/// element has been zero/sign-extended, depending on the isSigned parameter,
6291/// from an integer type half its size.
6292static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6293 bool isSigned) {
6294 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6295 EVT VT = N->getValueType(0);
6296 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6297 SDNode *BVN = N->getOperand(0).getNode();
6298 if (BVN->getValueType(0) != MVT::v4i32 ||
6299 BVN->getOpcode() != ISD::BUILD_VECTOR)
6300 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006301 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006302 unsigned HiElt = 1 - LoElt;
6303 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6304 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6305 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6306 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6307 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6308 return false;
6309 if (isSigned) {
6310 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6311 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6312 return true;
6313 } else {
6314 if (Hi0->isNullValue() && Hi1->isNullValue())
6315 return true;
6316 }
6317 return false;
6318 }
6319
6320 if (N->getOpcode() != ISD::BUILD_VECTOR)
6321 return false;
6322
6323 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6324 SDNode *Elt = N->getOperand(i).getNode();
6325 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6326 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6327 unsigned HalfSize = EltSize / 2;
6328 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006329 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006330 return false;
6331 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006332 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006333 return false;
6334 }
6335 continue;
6336 }
6337 return false;
6338 }
6339
6340 return true;
6341}
6342
6343/// isSignExtended - Check if a node is a vector value that is sign-extended
6344/// or a constant BUILD_VECTOR with sign-extended elements.
6345static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6346 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6347 return true;
6348 if (isExtendedBUILD_VECTOR(N, DAG, true))
6349 return true;
6350 return false;
6351}
6352
6353/// isZeroExtended - Check if a node is a vector value that is zero-extended
6354/// or a constant BUILD_VECTOR with zero-extended elements.
6355static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6356 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6357 return true;
6358 if (isExtendedBUILD_VECTOR(N, DAG, false))
6359 return true;
6360 return false;
6361}
6362
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006363static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6364 if (OrigVT.getSizeInBits() >= 64)
6365 return OrigVT;
6366
6367 assert(OrigVT.isSimple() && "Expecting a simple value type");
6368
6369 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6370 switch (OrigSimpleTy) {
6371 default: llvm_unreachable("Unexpected Vector Type");
6372 case MVT::v2i8:
6373 case MVT::v2i16:
6374 return MVT::v2i32;
6375 case MVT::v4i8:
6376 return MVT::v4i16;
6377 }
6378}
6379
Sebastian Popa204f722012-11-30 19:08:04 +00006380/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6381/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6382/// We insert the required extension here to get the vector to fill a D register.
6383static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6384 const EVT &OrigTy,
6385 const EVT &ExtTy,
6386 unsigned ExtOpcode) {
6387 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6388 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6389 // 64-bits we need to insert a new extension so that it will be 64-bits.
6390 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6391 if (OrigTy.getSizeInBits() >= 64)
6392 return N;
6393
6394 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006395 EVT NewVT = getExtensionTo64Bits(OrigTy);
6396
Andrew Trickef9de2a2013-05-25 02:42:55 +00006397 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006398}
6399
6400/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6401/// does not do any sign/zero extension. If the original vector is less
6402/// than 64 bits, an appropriate extension will be added after the load to
6403/// reach a total size of 64 bits. We have to add the extension separately
6404/// because ARM does not have a sign/zero extending load for vectors.
6405static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006406 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6407
6408 // The load already has the right type.
6409 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006410 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006411 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6412 LD->isNonTemporal(), LD->isInvariant(),
6413 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006414
6415 // We need to create a zextload/sextload. We cannot just create a load
6416 // followed by a zext/zext node because LowerMUL is also run during normal
6417 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006418 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006419 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006420 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006421 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006422}
6423
6424/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6425/// extending load, or BUILD_VECTOR with extended elements, return the
6426/// unextended value. The unextended vector should be 64 bits so that it can
6427/// be used as an operand to a VMULL instruction. If the original vector size
6428/// before extension is less than 64 bits we add a an extension to resize
6429/// the vector to 64 bits.
6430static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006431 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006432 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6433 N->getOperand(0)->getValueType(0),
6434 N->getValueType(0),
6435 N->getOpcode());
6436
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006437 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006438 return SkipLoadExtensionForVMULL(LD, DAG);
6439
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006440 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6441 // have been legalized as a BITCAST from v4i32.
6442 if (N->getOpcode() == ISD::BITCAST) {
6443 SDNode *BVN = N->getOperand(0).getNode();
6444 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6445 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006446 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006447 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006448 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6449 }
6450 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6451 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6452 EVT VT = N->getValueType(0);
6453 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6454 unsigned NumElts = VT.getVectorNumElements();
6455 MVT TruncVT = MVT::getIntegerVT(EltSize);
6456 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006457 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006458 for (unsigned i = 0; i != NumElts; ++i) {
6459 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6460 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006461 // Element types smaller than 32 bits are not legal, so use i32 elements.
6462 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006463 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006464 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006465 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006466 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006467}
6468
Evan Chenge2086e72011-03-29 01:56:09 +00006469static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6470 unsigned Opcode = N->getOpcode();
6471 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6472 SDNode *N0 = N->getOperand(0).getNode();
6473 SDNode *N1 = N->getOperand(1).getNode();
6474 return N0->hasOneUse() && N1->hasOneUse() &&
6475 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6476 }
6477 return false;
6478}
6479
6480static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6481 unsigned Opcode = N->getOpcode();
6482 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6483 SDNode *N0 = N->getOperand(0).getNode();
6484 SDNode *N1 = N->getOperand(1).getNode();
6485 return N0->hasOneUse() && N1->hasOneUse() &&
6486 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6487 }
6488 return false;
6489}
6490
Bob Wilson38ab35a2010-09-01 23:50:19 +00006491static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6492 // Multiplications are only custom-lowered for 128-bit vectors so that
6493 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6494 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006495 assert(VT.is128BitVector() && VT.isInteger() &&
6496 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006497 SDNode *N0 = Op.getOperand(0).getNode();
6498 SDNode *N1 = Op.getOperand(1).getNode();
6499 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006500 bool isMLA = false;
6501 bool isN0SExt = isSignExtended(N0, DAG);
6502 bool isN1SExt = isSignExtended(N1, DAG);
6503 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006504 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006505 else {
6506 bool isN0ZExt = isZeroExtended(N0, DAG);
6507 bool isN1ZExt = isZeroExtended(N1, DAG);
6508 if (isN0ZExt && isN1ZExt)
6509 NewOpc = ARMISD::VMULLu;
6510 else if (isN1SExt || isN1ZExt) {
6511 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6512 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6513 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6514 NewOpc = ARMISD::VMULLs;
6515 isMLA = true;
6516 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6517 NewOpc = ARMISD::VMULLu;
6518 isMLA = true;
6519 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6520 std::swap(N0, N1);
6521 NewOpc = ARMISD::VMULLu;
6522 isMLA = true;
6523 }
6524 }
6525
6526 if (!NewOpc) {
6527 if (VT == MVT::v2i64)
6528 // Fall through to expand this. It is not legal.
6529 return SDValue();
6530 else
6531 // Other vector multiplications are legal.
6532 return Op;
6533 }
6534 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006535
6536 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006537 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006538 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006539 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006540 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006541 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006542 assert(Op0.getValueType().is64BitVector() &&
6543 Op1.getValueType().is64BitVector() &&
6544 "unexpected types for extended operands to VMULL");
6545 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6546 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006547
Evan Chenge2086e72011-03-29 01:56:09 +00006548 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6549 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6550 // vmull q0, d4, d6
6551 // vmlal q0, d5, d6
6552 // is faster than
6553 // vaddl q0, d4, d5
6554 // vmovl q1, d6
6555 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006556 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6557 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006558 EVT Op1VT = Op1.getValueType();
6559 return DAG.getNode(N0->getOpcode(), DL, VT,
6560 DAG.getNode(NewOpc, DL, VT,
6561 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6562 DAG.getNode(NewOpc, DL, VT,
6563 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006564}
6565
Owen Anderson77aa2662011-04-05 21:48:57 +00006566static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006567LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006568 // TODO: Should this propagate fast-math-flags?
6569
Nate Begemanfa62d502011-02-11 20:53:29 +00006570 // Convert to float
6571 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6572 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6573 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6574 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6575 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6576 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6577 // Get reciprocal estimate.
6578 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006579 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006580 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6581 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006582 // Because char has a smaller range than uchar, we can actually get away
6583 // without any newton steps. This requires that we use a weird bias
6584 // of 0xb000, however (again, this has been exhaustively tested).
6585 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6586 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6587 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006588 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006589 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6590 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6591 // Convert back to short.
6592 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6593 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6594 return X;
6595}
6596
Owen Anderson77aa2662011-04-05 21:48:57 +00006597static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006598LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006599 // TODO: Should this propagate fast-math-flags?
6600
Nate Begemanfa62d502011-02-11 20:53:29 +00006601 SDValue N2;
6602 // Convert to float.
6603 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6604 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6605 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6606 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6607 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6608 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006609
Nate Begemanfa62d502011-02-11 20:53:29 +00006610 // Use reciprocal estimate and one refinement step.
6611 // float4 recip = vrecpeq_f32(yf);
6612 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006613 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006614 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6615 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006616 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006617 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006618 N1, N2);
6619 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6620 // Because short has a smaller range than ushort, we can actually get away
6621 // with only a single newton step. This requires that we use a weird bias
6622 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006623 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006624 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6625 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006626 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006627 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6628 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6629 // Convert back to integer and return.
6630 // return vmovn_s32(vcvt_s32_f32(result));
6631 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6632 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6633 return N0;
6634}
6635
6636static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6637 EVT VT = Op.getValueType();
6638 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6639 "unexpected type for custom-lowering ISD::SDIV");
6640
Andrew Trickef9de2a2013-05-25 02:42:55 +00006641 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006642 SDValue N0 = Op.getOperand(0);
6643 SDValue N1 = Op.getOperand(1);
6644 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006645
Nate Begemanfa62d502011-02-11 20:53:29 +00006646 if (VT == MVT::v8i8) {
6647 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6648 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006649
Nate Begemanfa62d502011-02-11 20:53:29 +00006650 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006651 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006652 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006653 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006654 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006655 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006656 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006657 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006658
6659 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6660 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6661
6662 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6663 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006664
Nate Begemanfa62d502011-02-11 20:53:29 +00006665 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6666 return N0;
6667 }
6668 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6669}
6670
6671static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006672 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006673 EVT VT = Op.getValueType();
6674 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6675 "unexpected type for custom-lowering ISD::UDIV");
6676
Andrew Trickef9de2a2013-05-25 02:42:55 +00006677 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006678 SDValue N0 = Op.getOperand(0);
6679 SDValue N1 = Op.getOperand(1);
6680 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006681
Nate Begemanfa62d502011-02-11 20:53:29 +00006682 if (VT == MVT::v8i8) {
6683 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6684 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006685
Nate Begemanfa62d502011-02-11 20:53:29 +00006686 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006687 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006688 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006689 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006690 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006691 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006692 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006693 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006694
Nate Begemanfa62d502011-02-11 20:53:29 +00006695 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6696 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006697
Nate Begemanfa62d502011-02-11 20:53:29 +00006698 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6699 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006700
6701 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006702 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6703 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006704 N0);
6705 return N0;
6706 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006707
Nate Begemanfa62d502011-02-11 20:53:29 +00006708 // v4i16 sdiv ... Convert to float.
6709 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6710 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6711 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6712 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6713 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006714 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006715
6716 // Use reciprocal estimate and two refinement steps.
6717 // float4 recip = vrecpeq_f32(yf);
6718 // recip *= vrecpsq_f32(yf, recip);
6719 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006720 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006721 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6722 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006723 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006724 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006725 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006726 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006727 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006728 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006729 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006730 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6731 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6732 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6733 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006734 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006735 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6736 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006737 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006738 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6739 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6740 // Convert back to integer and return.
6741 // return vmovn_u32(vcvt_s32_f32(result));
6742 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6743 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6744 return N0;
6745}
6746
Evan Chenge8916542011-08-30 01:34:54 +00006747static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6748 EVT VT = Op.getNode()->getValueType(0);
6749 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6750
6751 unsigned Opc;
6752 bool ExtraOp = false;
6753 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006754 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006755 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6756 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6757 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6758 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6759 }
6760
6761 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006762 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006763 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006764 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006765 Op.getOperand(1), Op.getOperand(2));
6766}
6767
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006768SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6769 assert(Subtarget->isTargetDarwin());
6770
6771 // For iOS, we want to call an alternative entry point: __sincos_stret,
6772 // return values are passed via sret.
6773 SDLoc dl(Op);
6774 SDValue Arg = Op.getOperand(0);
6775 EVT ArgVT = Arg.getValueType();
6776 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006777 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006778
6779 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006780 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006781
6782 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006783 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006784 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006785
6786 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006787 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6788 SDValue SRet;
6789 if (ShouldUseSRet) {
6790 // Create stack object for sret.
6791 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6792 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6793 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6794 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6795
6796 ArgListEntry Entry;
6797 Entry.Node = SRet;
6798 Entry.Ty = RetTy->getPointerTo();
6799 Entry.isSExt = false;
6800 Entry.isZExt = false;
6801 Entry.isSRet = true;
6802 Args.push_back(Entry);
6803 RetTy = Type::getVoidTy(*DAG.getContext());
6804 }
6805
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006806 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006807 Entry.Node = Arg;
6808 Entry.Ty = ArgTy;
6809 Entry.isSExt = false;
6810 Entry.isZExt = false;
6811 Args.push_back(Entry);
6812
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006813 const char *LibcallName =
6814 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006815 RTLIB::Libcall LC =
6816 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6817 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006818 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006819
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006820 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006821 CLI.setDebugLoc(dl)
6822 .setChain(DAG.getEntryNode())
6823 .setCallee(CC, RetTy, Callee, std::move(Args), 0)
6824 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006825 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6826
Tim Northover8b403662015-10-28 22:51:16 +00006827 if (!ShouldUseSRet)
6828 return CallResult.first;
6829
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006830 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6831 MachinePointerInfo(), false, false, false, 0);
6832
6833 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006834 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006835 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006836 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6837 MachinePointerInfo(), false, false, false, 0);
6838
6839 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6840 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6841 LoadSin.getValue(0), LoadCos.getValue(0));
6842}
6843
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006844SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006845 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006846 SDValue &Chain) const {
6847 EVT VT = Op.getValueType();
6848 assert((VT == MVT::i32 || VT == MVT::i64) &&
6849 "unexpected type for custom lowering DIV");
6850 SDLoc dl(Op);
6851
6852 const auto &DL = DAG.getDataLayout();
6853 const auto &TLI = DAG.getTargetLoweringInfo();
6854
6855 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006856 if (Signed)
6857 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6858 else
6859 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006860
6861 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6862
6863 ARMTargetLowering::ArgListTy Args;
6864
6865 for (auto AI : {1, 0}) {
6866 ArgListEntry Arg;
6867 Arg.Node = Op.getOperand(AI);
6868 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6869 Args.push_back(Arg);
6870 }
6871
6872 CallLoweringInfo CLI(DAG);
6873 CLI.setDebugLoc(dl)
6874 .setChain(Chain)
6875 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6876 ES, std::move(Args), 0);
6877
6878 return LowerCallTo(CLI).first;
6879}
6880
Martell Maloned1229242015-11-26 15:34:03 +00006881SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6882 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006883 assert(Op.getValueType() == MVT::i32 &&
6884 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006885 SDLoc dl(Op);
6886
6887 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6888 DAG.getEntryNode(), Op.getOperand(1));
6889
Martell Maloned1229242015-11-26 15:34:03 +00006890 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006891}
6892
6893void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00006894 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006895 SmallVectorImpl<SDValue> &Results) const {
6896 const auto &DL = DAG.getDataLayout();
6897 const auto &TLI = DAG.getTargetLoweringInfo();
6898
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006899 assert(Op.getValueType() == MVT::i64 &&
6900 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006901 SDLoc dl(Op);
6902
6903 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6904 DAG.getConstant(0, dl, MVT::i32));
6905 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6906 DAG.getConstant(1, dl, MVT::i32));
6907 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6908
6909 SDValue DBZCHK =
6910 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6911
Martell Maloned1229242015-11-26 15:34:03 +00006912 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006913
6914 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6915 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6916 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6917 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6918
6919 Results.push_back(Lower);
6920 Results.push_back(Upper);
6921}
6922
Eli Friedman10f9ce22011-09-15 22:26:18 +00006923static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006924 // Monotonic load/store is legal for all targets
6925 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6926 return Op;
6927
Alp Tokercb402912014-01-24 17:20:08 +00006928 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006929 // dmb or equivalent available.
6930 return SDValue();
6931}
6932
Tim Northoverbc933082013-05-23 19:11:20 +00006933static void ReplaceREADCYCLECOUNTER(SDNode *N,
6934 SmallVectorImpl<SDValue> &Results,
6935 SelectionDAG &DAG,
6936 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006937 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006938 // Under Power Management extensions, the cycle-count is:
6939 // mrc p15, #0, <Rt>, c9, c13, #0
6940 SDValue Ops[] = { N->getOperand(0), // Chain
6941 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6942 DAG.getConstant(15, DL, MVT::i32),
6943 DAG.getConstant(0, DL, MVT::i32),
6944 DAG.getConstant(9, DL, MVT::i32),
6945 DAG.getConstant(13, DL, MVT::i32),
6946 DAG.getConstant(0, DL, MVT::i32)
6947 };
Tim Northoverbc933082013-05-23 19:11:20 +00006948
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006949 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6950 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6951 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6952 DAG.getConstant(0, DL, MVT::i32)));
6953 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006954}
6955
Dan Gohman21cea8a2010-04-17 15:26:15 +00006956SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006957 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006958 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006959 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006960 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006961 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006962 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006963 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6964 default: llvm_unreachable("unknown object format");
6965 case Triple::COFF:
6966 return LowerGlobalAddressWindows(Op, DAG);
6967 case Triple::ELF:
6968 return LowerGlobalAddressELF(Op, DAG);
6969 case Triple::MachO:
6970 return LowerGlobalAddressDarwin(Op, DAG);
6971 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006972 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006973 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006974 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6975 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006976 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006977 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006978 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006979 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006980 case ISD::SINT_TO_FP:
6981 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6982 case ISD::FP_TO_SINT:
6983 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006984 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006985 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006986 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006987 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006988 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006989 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006990 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6991 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006992 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006993 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006994 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006995 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00006996 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
6997 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006998 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006999 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007000 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007001 case ISD::CTTZ:
7002 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007003 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007004 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007005 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007006 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007007 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007008 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007009 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007010 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007011 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007012 case ISD::MUL: return LowerMUL(Op, DAG);
Martell Malonea6b867e2015-11-23 13:11:39 +00007013 case ISD::SDIV: return LowerSDIV(Op, DAG);
Martell Maloned1229242015-11-26 15:34:03 +00007014 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007015 case ISD::ADDC:
7016 case ISD::ADDE:
7017 case ISD::SUBC:
7018 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007019 case ISD::SADDO:
7020 case ISD::UADDO:
7021 case ISD::SSUBO:
7022 case ISD::USUBO:
7023 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007024 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007025 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007026 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007027 case ISD::SDIVREM:
7028 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007029 case ISD::DYNAMIC_STACKALLOC:
7030 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7031 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7032 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007033 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7034 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007035 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007036 }
Evan Cheng10043e22007-01-19 07:51:42 +00007037}
7038
Duncan Sands6ed40142008-12-01 11:39:25 +00007039/// ReplaceNodeResults - Replace the results of node with an illegal result
7040/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007041void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007042 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007043 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007044 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007045 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007046 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007047 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007048 case ISD::READ_REGISTER:
7049 ExpandREAD_REGISTER(N, Results, DAG);
7050 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007051 case ISD::BITCAST:
7052 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007053 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007054 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007055 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007056 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007057 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007058 case ISD::SREM:
7059 case ISD::UREM:
7060 Res = LowerREM(N, DAG);
7061 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007062 case ISD::SDIVREM:
7063 case ISD::UDIVREM:
7064 Res = LowerDivRem(SDValue(N, 0), DAG);
7065 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7066 Results.push_back(Res.getValue(0));
7067 Results.push_back(Res.getValue(1));
7068 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007069 case ISD::READCYCLECOUNTER:
7070 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7071 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007072 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007073 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007074 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007075 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7076 Results);
Duncan Sands6ed40142008-12-01 11:39:25 +00007077 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007078 if (Res.getNode())
7079 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007080}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007081
Evan Cheng10043e22007-01-19 07:51:42 +00007082//===----------------------------------------------------------------------===//
7083// ARM Scheduler Hooks
7084//===----------------------------------------------------------------------===//
7085
Bill Wendling030b58e2011-10-06 22:18:16 +00007086/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7087/// registers the function context.
7088void ARMTargetLowering::
7089SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
7090 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007091 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00007092 DebugLoc dl = MI->getDebugLoc();
7093 MachineFunction *MF = MBB->getParent();
7094 MachineRegisterInfo *MRI = &MF->getRegInfo();
7095 MachineConstantPool *MCP = MF->getConstantPool();
7096 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7097 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007098
Bill Wendling374ee192011-10-03 21:25:38 +00007099 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007100 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007101
Bill Wendling374ee192011-10-03 21:25:38 +00007102 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007103 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007104 ARMConstantPoolValue *CPV =
7105 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7106 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7107
Craig Topper61e88f42014-11-21 05:58:21 +00007108 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7109 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007110
Bill Wendling030b58e2011-10-06 22:18:16 +00007111 // Grab constant pool and fixed stack memory operands.
7112 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007113 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7114 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007115
7116 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007117 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7118 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007119
7120 // Load the address of the dispatch MBB into the jump buffer.
7121 if (isThumb2) {
7122 // Incoming value: jbuf
7123 // ldr.n r5, LCPI1_1
7124 // orr r5, r5, #1
7125 // add r5, pc
7126 // str r5, [$jbuf, #+4] ; &jbuf[1]
7127 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7128 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7129 .addConstantPoolIndex(CPI)
7130 .addMemOperand(CPMMO));
7131 // Set the low bit because of thumb mode.
7132 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7133 AddDefaultCC(
7134 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7135 .addReg(NewVReg1, RegState::Kill)
7136 .addImm(0x01)));
7137 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7138 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7139 .addReg(NewVReg2, RegState::Kill)
7140 .addImm(PCLabelId);
7141 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7142 .addReg(NewVReg3, RegState::Kill)
7143 .addFrameIndex(FI)
7144 .addImm(36) // &jbuf[1] :: pc
7145 .addMemOperand(FIMMOSt));
7146 } else if (isThumb) {
7147 // Incoming value: jbuf
7148 // ldr.n r1, LCPI1_4
7149 // add r1, pc
7150 // mov r2, #1
7151 // orrs r1, r2
7152 // add r2, $jbuf, #+4 ; &jbuf[1]
7153 // str r1, [r2]
7154 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7155 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7156 .addConstantPoolIndex(CPI)
7157 .addMemOperand(CPMMO));
7158 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7159 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7160 .addReg(NewVReg1, RegState::Kill)
7161 .addImm(PCLabelId);
7162 // Set the low bit because of thumb mode.
7163 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7164 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7165 .addReg(ARM::CPSR, RegState::Define)
7166 .addImm(1));
7167 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7168 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7169 .addReg(ARM::CPSR, RegState::Define)
7170 .addReg(NewVReg2, RegState::Kill)
7171 .addReg(NewVReg3, RegState::Kill));
7172 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007173 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7174 .addFrameIndex(FI)
7175 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007176 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7177 .addReg(NewVReg4, RegState::Kill)
7178 .addReg(NewVReg5, RegState::Kill)
7179 .addImm(0)
7180 .addMemOperand(FIMMOSt));
7181 } else {
7182 // Incoming value: jbuf
7183 // ldr r1, LCPI1_1
7184 // add r1, pc, r1
7185 // str r1, [$jbuf, #+4] ; &jbuf[1]
7186 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7187 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7188 .addConstantPoolIndex(CPI)
7189 .addImm(0)
7190 .addMemOperand(CPMMO));
7191 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7192 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7193 .addReg(NewVReg1, RegState::Kill)
7194 .addImm(PCLabelId));
7195 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7196 .addReg(NewVReg2, RegState::Kill)
7197 .addFrameIndex(FI)
7198 .addImm(36) // &jbuf[1] :: pc
7199 .addMemOperand(FIMMOSt));
7200 }
7201}
7202
Matthias Brauneec4efc2015-04-28 00:37:05 +00007203void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
7204 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007205 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007206 DebugLoc dl = MI->getDebugLoc();
7207 MachineFunction *MF = MBB->getParent();
7208 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007209 MachineFrameInfo *MFI = MF->getFrameInfo();
7210 int FI = MFI->getFunctionContextIndex();
7211
Craig Topper61e88f42014-11-21 05:58:21 +00007212 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7213 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007214
Bill Wendling362c1b02011-10-06 21:29:56 +00007215 // Get a mapping of the call site numbers to all of the landing pads they're
7216 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007217 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7218 unsigned MaxCSNum = 0;
7219 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007220 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7221 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007222 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007223
7224 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7225 // pad.
7226 for (MachineBasicBlock::iterator
7227 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7228 if (!II->isEHLabel()) continue;
7229
7230 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007231 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007232
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007233 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7234 for (SmallVectorImpl<unsigned>::iterator
7235 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7236 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007237 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007238 MaxCSNum = std::max(MaxCSNum, *CSI);
7239 }
Bill Wendling202803e2011-10-05 00:02:33 +00007240 break;
7241 }
7242 }
7243
7244 // Get an ordered list of the machine basic blocks for the jump table.
7245 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007246 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007247 LPadList.reserve(CallSiteNumToLPad.size());
7248 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7249 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7250 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007251 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007252 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007253 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7254 }
Bill Wendling202803e2011-10-05 00:02:33 +00007255 }
7256
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007257 assert(!LPadList.empty() &&
7258 "No landing pad destinations for the dispatch jump table!");
7259
Bill Wendling362c1b02011-10-06 21:29:56 +00007260 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007261 MachineJumpTableInfo *JTI =
7262 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7263 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007264 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007265
Bill Wendling362c1b02011-10-06 21:29:56 +00007266 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007267
7268 // Shove the dispatch's address into the return slot in the function context.
7269 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007270 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007271
Bill Wendling324be982011-10-05 00:39:32 +00007272 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007273 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007274 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007275 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007276 else
7277 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7278
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007279 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007280 DispatchBB->addSuccessor(TrapBB);
7281
7282 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7283 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007284
Bill Wendling510fbcd2011-10-17 21:32:56 +00007285 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007286 MF->insert(MF->end(), DispatchBB);
7287 MF->insert(MF->end(), DispContBB);
7288 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007289
Bill Wendling030b58e2011-10-06 22:18:16 +00007290 // Insert code into the entry block that creates and registers the function
7291 // context.
7292 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7293
Alex Lorenze40c8a22015-08-11 23:09:45 +00007294 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7295 MachinePointerInfo::getFixedStack(*MF, FI),
7296 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007297
Chad Rosier1ec8e402012-11-06 23:05:24 +00007298 MachineInstrBuilder MIB;
7299 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7300
7301 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7302 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7303
7304 // Add a register mask with no preserved registers. This results in all
7305 // registers being marked as clobbered.
7306 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007307
Bill Wendling85833f72011-10-18 22:49:07 +00007308 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007309 if (Subtarget->isThumb2()) {
7310 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7311 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7312 .addFrameIndex(FI)
7313 .addImm(4)
7314 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007315
Bill Wendling85833f72011-10-18 22:49:07 +00007316 if (NumLPads < 256) {
7317 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7318 .addReg(NewVReg1)
7319 .addImm(LPadList.size()));
7320 } else {
7321 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7322 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007323 .addImm(NumLPads & 0xFFFF));
7324
7325 unsigned VReg2 = VReg1;
7326 if ((NumLPads & 0xFFFF0000) != 0) {
7327 VReg2 = MRI->createVirtualRegister(TRC);
7328 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7329 .addReg(VReg1)
7330 .addImm(NumLPads >> 16));
7331 }
7332
Bill Wendling85833f72011-10-18 22:49:07 +00007333 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7334 .addReg(NewVReg1)
7335 .addReg(VReg2));
7336 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007337
Bill Wendling5626c662011-10-06 22:53:00 +00007338 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7339 .addMBB(TrapBB)
7340 .addImm(ARMCC::HI)
7341 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007342
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007343 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7344 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007345 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007346
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007347 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007348 AddDefaultCC(
7349 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007350 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7351 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007352 .addReg(NewVReg1)
7353 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7354
7355 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007356 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007357 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007358 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007359 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007360 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7361 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7362 .addFrameIndex(FI)
7363 .addImm(1)
7364 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007365
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007366 if (NumLPads < 256) {
7367 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7368 .addReg(NewVReg1)
7369 .addImm(NumLPads));
7370 } else {
7371 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007372 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7373 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7374
7375 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007376 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007377 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007378 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007379 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007380
7381 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7382 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7383 .addReg(VReg1, RegState::Define)
7384 .addConstantPoolIndex(Idx));
7385 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7386 .addReg(NewVReg1)
7387 .addReg(VReg1));
7388 }
7389
Bill Wendlingb3d46782011-10-06 23:37:36 +00007390 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7391 .addMBB(TrapBB)
7392 .addImm(ARMCC::HI)
7393 .addReg(ARM::CPSR);
7394
7395 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7396 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7397 .addReg(ARM::CPSR, RegState::Define)
7398 .addReg(NewVReg1)
7399 .addImm(2));
7400
7401 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007402 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007403 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007404
7405 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7406 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7407 .addReg(ARM::CPSR, RegState::Define)
7408 .addReg(NewVReg2, RegState::Kill)
7409 .addReg(NewVReg3));
7410
Alex Lorenze40c8a22015-08-11 23:09:45 +00007411 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7412 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007413
7414 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7415 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7416 .addReg(NewVReg4, RegState::Kill)
7417 .addImm(0)
7418 .addMemOperand(JTMMOLd));
7419
Chad Rosier96603432013-03-01 18:30:38 +00007420 unsigned NewVReg6 = NewVReg5;
7421 if (RelocM == Reloc::PIC_) {
7422 NewVReg6 = MRI->createVirtualRegister(TRC);
7423 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7424 .addReg(ARM::CPSR, RegState::Define)
7425 .addReg(NewVReg5, RegState::Kill)
7426 .addReg(NewVReg3));
7427 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007428
7429 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7430 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007431 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007432 } else {
7433 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7434 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7435 .addFrameIndex(FI)
7436 .addImm(4)
7437 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007438
Bill Wendling4969dcd2011-10-18 22:52:20 +00007439 if (NumLPads < 256) {
7440 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7441 .addReg(NewVReg1)
7442 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007443 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007444 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7445 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007446 .addImm(NumLPads & 0xFFFF));
7447
7448 unsigned VReg2 = VReg1;
7449 if ((NumLPads & 0xFFFF0000) != 0) {
7450 VReg2 = MRI->createVirtualRegister(TRC);
7451 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7452 .addReg(VReg1)
7453 .addImm(NumLPads >> 16));
7454 }
7455
Bill Wendling4969dcd2011-10-18 22:52:20 +00007456 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7457 .addReg(NewVReg1)
7458 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007459 } else {
7460 MachineConstantPool *ConstantPool = MF->getConstantPool();
7461 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7462 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7463
7464 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007465 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007466 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007467 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007468 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7469
7470 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7471 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7472 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007473 .addConstantPoolIndex(Idx)
7474 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007475 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7476 .addReg(NewVReg1)
7477 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007478 }
7479
Bill Wendling5626c662011-10-06 22:53:00 +00007480 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7481 .addMBB(TrapBB)
7482 .addImm(ARMCC::HI)
7483 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007484
Bill Wendling973c8172011-10-18 22:11:18 +00007485 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007486 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007487 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007488 .addReg(NewVReg1)
7489 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007490 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7491 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007492 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007493
Alex Lorenze40c8a22015-08-11 23:09:45 +00007494 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7495 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007496 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007497 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007498 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7499 .addReg(NewVReg3, RegState::Kill)
7500 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007501 .addImm(0)
7502 .addMemOperand(JTMMOLd));
7503
Chad Rosier96603432013-03-01 18:30:38 +00007504 if (RelocM == Reloc::PIC_) {
7505 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7506 .addReg(NewVReg5, RegState::Kill)
7507 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007508 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007509 } else {
7510 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7511 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007512 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007513 }
Bill Wendling5626c662011-10-06 22:53:00 +00007514 }
Bill Wendling202803e2011-10-05 00:02:33 +00007515
Bill Wendling324be982011-10-05 00:39:32 +00007516 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007517 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007518 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007519 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7520 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007521 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007522 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007523 }
7524
Bill Wendling26d27802011-10-17 05:25:09 +00007525 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007526 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007527 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007528 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007529
7530 // Remove the landing pad successor from the invoke block and replace it
7531 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007532 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7533 BB->succ_end());
7534 while (!Successors.empty()) {
7535 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007536 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007537 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007538 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007539 }
7540 }
7541
Cong Houd97c1002015-12-01 05:29:22 +00007542 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00007543 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007544
7545 // Find the invoke call and mark all of the callee-saved registers as
7546 // 'implicit defined' so that they're spilled. This prevents code from
7547 // moving instructions to before the EH block, where they will never be
7548 // executed.
7549 for (MachineBasicBlock::reverse_iterator
7550 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007551 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007552
7553 DenseMap<unsigned, bool> DefRegs;
7554 for (MachineInstr::mop_iterator
7555 OI = II->operands_begin(), OE = II->operands_end();
7556 OI != OE; ++OI) {
7557 if (!OI->isReg()) continue;
7558 DefRegs[OI->getReg()] = true;
7559 }
7560
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007561 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007562
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007563 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007564 unsigned Reg = SavedRegs[i];
7565 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007566 !ARM::tGPRRegClass.contains(Reg) &&
7567 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007568 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007569 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007570 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007571 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007572 continue;
7573 if (!DefRegs[Reg])
7574 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007575 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007576
7577 break;
7578 }
Bill Wendling883ec972011-10-07 23:18:02 +00007579 }
Bill Wendling324be982011-10-05 00:39:32 +00007580
Bill Wendling617075f2011-10-18 18:30:49 +00007581 // Mark all former landing pads as non-landing pads. The dispatch is the only
7582 // landing pad now.
7583 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7584 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007585 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007586
Bill Wendling324be982011-10-05 00:39:32 +00007587 // The instruction is gone now.
7588 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007589}
7590
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007591static
7592MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7593 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7594 E = MBB->succ_end(); I != E; ++I)
7595 if (*I != Succ)
7596 return *I;
7597 llvm_unreachable("Expecting a BB with two successors!");
7598}
7599
Manman Renb504f492013-10-29 22:27:32 +00007600/// Return the load opcode for a given load size. If load size >= 8,
7601/// neon opcode will be returned.
7602static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7603 if (LdSize >= 8)
7604 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7605 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7606 if (IsThumb1)
7607 return LdSize == 4 ? ARM::tLDRi
7608 : LdSize == 2 ? ARM::tLDRHi
7609 : LdSize == 1 ? ARM::tLDRBi : 0;
7610 if (IsThumb2)
7611 return LdSize == 4 ? ARM::t2LDR_POST
7612 : LdSize == 2 ? ARM::t2LDRH_POST
7613 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7614 return LdSize == 4 ? ARM::LDR_POST_IMM
7615 : LdSize == 2 ? ARM::LDRH_POST
7616 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7617}
7618
7619/// Return the store opcode for a given store size. If store size >= 8,
7620/// neon opcode will be returned.
7621static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7622 if (StSize >= 8)
7623 return StSize == 16 ? ARM::VST1q32wb_fixed
7624 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7625 if (IsThumb1)
7626 return StSize == 4 ? ARM::tSTRi
7627 : StSize == 2 ? ARM::tSTRHi
7628 : StSize == 1 ? ARM::tSTRBi : 0;
7629 if (IsThumb2)
7630 return StSize == 4 ? ARM::t2STR_POST
7631 : StSize == 2 ? ARM::t2STRH_POST
7632 : StSize == 1 ? ARM::t2STRB_POST : 0;
7633 return StSize == 4 ? ARM::STR_POST_IMM
7634 : StSize == 2 ? ARM::STRH_POST
7635 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7636}
7637
7638/// Emit a post-increment load operation with given size. The instructions
7639/// will be added to BB at Pos.
7640static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7641 const TargetInstrInfo *TII, DebugLoc dl,
7642 unsigned LdSize, unsigned Data, unsigned AddrIn,
7643 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7644 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7645 assert(LdOpc != 0 && "Should have a load opcode");
7646 if (LdSize >= 8) {
7647 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7648 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7649 .addImm(0));
7650 } else if (IsThumb1) {
7651 // load + update AddrIn
7652 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7653 .addReg(AddrIn).addImm(0));
7654 MachineInstrBuilder MIB =
7655 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7656 MIB = AddDefaultT1CC(MIB);
7657 MIB.addReg(AddrIn).addImm(LdSize);
7658 AddDefaultPred(MIB);
7659 } else if (IsThumb2) {
7660 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7661 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7662 .addImm(LdSize));
7663 } else { // arm
7664 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7665 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7666 .addReg(0).addImm(LdSize));
7667 }
7668}
7669
7670/// Emit a post-increment store operation with given size. The instructions
7671/// will be added to BB at Pos.
7672static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7673 const TargetInstrInfo *TII, DebugLoc dl,
7674 unsigned StSize, unsigned Data, unsigned AddrIn,
7675 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7676 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7677 assert(StOpc != 0 && "Should have a store opcode");
7678 if (StSize >= 8) {
7679 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7680 .addReg(AddrIn).addImm(0).addReg(Data));
7681 } else if (IsThumb1) {
7682 // store + update AddrIn
7683 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7684 .addReg(AddrIn).addImm(0));
7685 MachineInstrBuilder MIB =
7686 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7687 MIB = AddDefaultT1CC(MIB);
7688 MIB.addReg(AddrIn).addImm(StSize);
7689 AddDefaultPred(MIB);
7690 } else if (IsThumb2) {
7691 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7692 .addReg(Data).addReg(AddrIn).addImm(StSize));
7693 } else { // arm
7694 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7695 .addReg(Data).addReg(AddrIn).addReg(0)
7696 .addImm(StSize));
7697 }
7698}
7699
David Peixottoc32e24a2013-10-17 19:49:22 +00007700MachineBasicBlock *
7701ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7702 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007703 // This pseudo instruction has 3 operands: dst, src, size
7704 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7705 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007706 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007707 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007708 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007709
7710 unsigned dest = MI->getOperand(0).getReg();
7711 unsigned src = MI->getOperand(1).getReg();
7712 unsigned SizeVal = MI->getOperand(2).getImm();
7713 unsigned Align = MI->getOperand(3).getImm();
7714 DebugLoc dl = MI->getDebugLoc();
7715
Manman Rene8735522012-06-01 19:33:18 +00007716 MachineFunction *MF = BB->getParent();
7717 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007718 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007719 const TargetRegisterClass *TRC = nullptr;
7720 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007721
7722 bool IsThumb1 = Subtarget->isThumb1Only();
7723 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007724
7725 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007726 UnitSize = 1;
7727 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007728 UnitSize = 2;
7729 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007730 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007731 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007732 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007733 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007734 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007735 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007736 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007737 }
7738 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007739 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007740 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007741 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007742
David Peixottob0653e532013-10-24 16:39:36 +00007743 // Select the correct opcode and register class for unit size load/store
7744 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007745 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007746 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007747 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7748 : UnitSize == 8 ? &ARM::DPRRegClass
7749 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007750
Manman Rene8735522012-06-01 19:33:18 +00007751 unsigned BytesLeft = SizeVal % UnitSize;
7752 unsigned LoopSize = SizeVal - BytesLeft;
7753
7754 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7755 // Use LDR and STR to copy.
7756 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7757 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7758 unsigned srcIn = src;
7759 unsigned destIn = dest;
7760 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007761 unsigned srcOut = MRI.createVirtualRegister(TRC);
7762 unsigned destOut = MRI.createVirtualRegister(TRC);
7763 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007764 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7765 IsThumb1, IsThumb2);
7766 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7767 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007768 srcIn = srcOut;
7769 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007770 }
7771
7772 // Handle the leftover bytes with LDRB and STRB.
7773 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7774 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007775 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007776 unsigned srcOut = MRI.createVirtualRegister(TRC);
7777 unsigned destOut = MRI.createVirtualRegister(TRC);
7778 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007779 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7780 IsThumb1, IsThumb2);
7781 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7782 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007783 srcIn = srcOut;
7784 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007785 }
7786 MI->eraseFromParent(); // The instruction is gone now.
7787 return BB;
7788 }
7789
7790 // Expand the pseudo op to a loop.
7791 // thisMBB:
7792 // ...
7793 // movw varEnd, # --> with thumb2
7794 // movt varEnd, #
7795 // ldrcp varEnd, idx --> without thumb2
7796 // fallthrough --> loopMBB
7797 // loopMBB:
7798 // PHI varPhi, varEnd, varLoop
7799 // PHI srcPhi, src, srcLoop
7800 // PHI destPhi, dst, destLoop
7801 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7802 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7803 // subs varLoop, varPhi, #UnitSize
7804 // bne loopMBB
7805 // fallthrough --> exitMBB
7806 // exitMBB:
7807 // epilogue to handle left-over bytes
7808 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7809 // [destOut] = STRB_POST(scratch, destLoop, 1)
7810 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7811 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7812 MF->insert(It, loopMBB);
7813 MF->insert(It, exitMBB);
7814
7815 // Transfer the remainder of BB and its successor edges to exitMBB.
7816 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007817 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007818 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7819
7820 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007821 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007822 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007823 unsigned Vtmp = varEnd;
7824 if ((LoopSize & 0xFFFF0000) != 0)
7825 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007826 AddDefaultPred(BuildMI(BB, dl,
7827 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7828 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007829
7830 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007831 AddDefaultPred(BuildMI(BB, dl,
7832 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7833 varEnd)
7834 .addReg(Vtmp)
7835 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007836 } else {
7837 MachineConstantPool *ConstantPool = MF->getConstantPool();
7838 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7839 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7840
7841 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007842 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007843 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007844 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007845 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7846
7847 if (IsThumb1)
7848 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7849 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7850 else
7851 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7852 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7853 }
Manman Rene8735522012-06-01 19:33:18 +00007854 BB->addSuccessor(loopMBB);
7855
7856 // Generate the loop body:
7857 // varPhi = PHI(varLoop, varEnd)
7858 // srcPhi = PHI(srcLoop, src)
7859 // destPhi = PHI(destLoop, dst)
7860 MachineBasicBlock *entryBB = BB;
7861 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007862 unsigned varLoop = MRI.createVirtualRegister(TRC);
7863 unsigned varPhi = MRI.createVirtualRegister(TRC);
7864 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7865 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7866 unsigned destLoop = MRI.createVirtualRegister(TRC);
7867 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007868
7869 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7870 .addReg(varLoop).addMBB(loopMBB)
7871 .addReg(varEnd).addMBB(entryBB);
7872 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7873 .addReg(srcLoop).addMBB(loopMBB)
7874 .addReg(src).addMBB(entryBB);
7875 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7876 .addReg(destLoop).addMBB(loopMBB)
7877 .addReg(dest).addMBB(entryBB);
7878
7879 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7880 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007881 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007882 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7883 IsThumb1, IsThumb2);
7884 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7885 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007886
7887 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007888 if (IsThumb1) {
7889 MachineInstrBuilder MIB =
7890 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7891 MIB = AddDefaultT1CC(MIB);
7892 MIB.addReg(varPhi).addImm(UnitSize);
7893 AddDefaultPred(MIB);
7894 } else {
7895 MachineInstrBuilder MIB =
7896 BuildMI(*BB, BB->end(), dl,
7897 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7898 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7899 MIB->getOperand(5).setReg(ARM::CPSR);
7900 MIB->getOperand(5).setIsDef(true);
7901 }
7902 BuildMI(*BB, BB->end(), dl,
7903 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7904 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007905
7906 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7907 BB->addSuccessor(loopMBB);
7908 BB->addSuccessor(exitMBB);
7909
7910 // Add epilogue to handle BytesLeft.
7911 BB = exitMBB;
7912 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007913
7914 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7915 // [destOut] = STRB_POST(scratch, destLoop, 1)
7916 unsigned srcIn = srcLoop;
7917 unsigned destIn = destLoop;
7918 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007919 unsigned srcOut = MRI.createVirtualRegister(TRC);
7920 unsigned destOut = MRI.createVirtualRegister(TRC);
7921 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007922 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7923 IsThumb1, IsThumb2);
7924 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7925 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007926 srcIn = srcOut;
7927 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007928 }
7929
7930 MI->eraseFromParent(); // The instruction is gone now.
7931 return BB;
7932}
7933
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007934MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007935ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7936 MachineBasicBlock *MBB) const {
7937 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007938 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007939 DebugLoc DL = MI->getDebugLoc();
7940
7941 assert(Subtarget->isTargetWindows() &&
7942 "__chkstk is only supported on Windows");
7943 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7944
7945 // __chkstk takes the number of words to allocate on the stack in R4, and
7946 // returns the stack adjustment in number of bytes in R4. This will not
7947 // clober any other registers (other than the obvious lr).
7948 //
7949 // Although, technically, IP should be considered a register which may be
7950 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7951 // thumb-2 environment, so there is no interworking required. As a result, we
7952 // do not expect a veneer to be emitted by the linker, clobbering IP.
7953 //
Alp Toker1d099d92014-06-19 19:41:26 +00007954 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007955 // required, again, ensuring that IP is not clobbered.
7956 //
7957 // Finally, although some linkers may theoretically provide a trampoline for
7958 // out of range calls (which is quite common due to a 32M range limitation of
7959 // branches for Thumb), we can generate the long-call version via
7960 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7961 // IP.
7962
7963 switch (TM.getCodeModel()) {
7964 case CodeModel::Small:
7965 case CodeModel::Medium:
7966 case CodeModel::Default:
7967 case CodeModel::Kernel:
7968 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7969 .addImm((unsigned)ARMCC::AL).addReg(0)
7970 .addExternalSymbol("__chkstk")
7971 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7972 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7973 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7974 break;
7975 case CodeModel::Large:
7976 case CodeModel::JITDefault: {
7977 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7978 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7979
7980 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7981 .addExternalSymbol("__chkstk");
7982 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7983 .addImm((unsigned)ARMCC::AL).addReg(0)
7984 .addReg(Reg, RegState::Kill)
7985 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7986 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7987 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7988 break;
7989 }
7990 }
7991
7992 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7993 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007994 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007995
7996 MI->eraseFromParent();
7997 return MBB;
7998}
7999
8000MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008001ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
8002 MachineBasicBlock *MBB) const {
8003 DebugLoc DL = MI->getDebugLoc();
8004 MachineFunction *MF = MBB->getParent();
8005 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8006
8007 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
8008 MF->push_back(ContBB);
8009 ContBB->splice(ContBB->begin(), MBB,
8010 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
8011 MBB->addSuccessor(ContBB);
8012
8013 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8014 MF->push_back(TrapBB);
8015 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
8016 MBB->addSuccessor(TrapBB);
8017
8018 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
8019 .addReg(MI->getOperand(0).getReg())
8020 .addMBB(TrapBB);
8021
8022 MI->eraseFromParent();
8023 return ContBB;
8024}
8025
8026MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008027ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008028 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008029 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00008030 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008031 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00008032 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008033 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00008034 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00008035 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008036 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008037 // The Thumb2 pre-indexed stores have the same MI operands, they just
8038 // define them differently in the .td files from the isel patterns, so
8039 // they need pseudos.
8040 case ARM::t2STR_preidx:
8041 MI->setDesc(TII->get(ARM::t2STR_PRE));
8042 return BB;
8043 case ARM::t2STRB_preidx:
8044 MI->setDesc(TII->get(ARM::t2STRB_PRE));
8045 return BB;
8046 case ARM::t2STRH_preidx:
8047 MI->setDesc(TII->get(ARM::t2STRH_PRE));
8048 return BB;
8049
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008050 case ARM::STRi_preidx:
8051 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00008052 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008053 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
8054 // Decode the offset.
8055 unsigned Offset = MI->getOperand(4).getImm();
8056 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8057 Offset = ARM_AM::getAM2Offset(Offset);
8058 if (isSub)
8059 Offset = -Offset;
8060
Jim Grosbachf402f692011-08-12 21:02:34 +00008061 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008062 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008063 .addOperand(MI->getOperand(0)) // Rn_wb
8064 .addOperand(MI->getOperand(1)) // Rt
8065 .addOperand(MI->getOperand(2)) // Rn
8066 .addImm(Offset) // offset (skip GPR==zero_reg)
8067 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00008068 .addOperand(MI->getOperand(6))
8069 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008070 MI->eraseFromParent();
8071 return BB;
8072 }
8073 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008074 case ARM::STRBr_preidx:
8075 case ARM::STRH_preidx: {
8076 unsigned NewOpc;
8077 switch (MI->getOpcode()) {
8078 default: llvm_unreachable("unexpected opcode!");
8079 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8080 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8081 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8082 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008083 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
8084 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
8085 MIB.addOperand(MI->getOperand(i));
8086 MI->eraseFromParent();
8087 return BB;
8088 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008089
Evan Chengbb2af352009-08-12 05:17:19 +00008090 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008091 // To "insert" a SELECT_CC instruction, we actually have to insert the
8092 // diamond control-flow pattern. The incoming instruction knows the
8093 // destination vreg to set, the condition code register to branch on, the
8094 // true/false values to select between, and a branch opcode to use.
8095 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008096 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008097
8098 // thisMBB:
8099 // ...
8100 // TrueVal = ...
8101 // cmpTY ccX, r1, r2
8102 // bCC copy1MBB
8103 // fallthrough --> copy0MBB
8104 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008105 MachineFunction *F = BB->getParent();
8106 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8107 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008108 F->insert(It, copy0MBB);
8109 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008110
8111 // Transfer the remainder of BB and its successor edges to sinkMBB.
8112 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008113 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008114 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8115
Dan Gohmanf4f04102010-07-06 15:49:48 +00008116 BB->addSuccessor(copy0MBB);
8117 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008118
Dan Gohman34396292010-07-06 20:24:04 +00008119 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
8120 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
8121
Evan Cheng10043e22007-01-19 07:51:42 +00008122 // copy0MBB:
8123 // %FalseValue = ...
8124 // # fallthrough to sinkMBB
8125 BB = copy0MBB;
8126
8127 // Update machine-CFG edges
8128 BB->addSuccessor(sinkMBB);
8129
8130 // sinkMBB:
8131 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8132 // ...
8133 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008134 BuildMI(*BB, BB->begin(), dl,
8135 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00008136 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8137 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8138
Dan Gohman34396292010-07-06 20:24:04 +00008139 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008140 return BB;
8141 }
Evan Chengb972e562009-08-07 00:34:42 +00008142
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008143 case ARM::BCCi64:
8144 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008145 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008146 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008147
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008148 // Compare both parts that make up the double comparison separately for
8149 // equality.
8150 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
8151
8152 unsigned LHS1 = MI->getOperand(1).getReg();
8153 unsigned LHS2 = MI->getOperand(2).getReg();
8154 if (RHSisZero) {
8155 AddDefaultPred(BuildMI(BB, dl,
8156 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8157 .addReg(LHS1).addImm(0));
8158 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8159 .addReg(LHS2).addImm(0)
8160 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8161 } else {
8162 unsigned RHS1 = MI->getOperand(3).getReg();
8163 unsigned RHS2 = MI->getOperand(4).getReg();
8164 AddDefaultPred(BuildMI(BB, dl,
8165 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8166 .addReg(LHS1).addReg(RHS1));
8167 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8168 .addReg(LHS2).addReg(RHS2)
8169 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8170 }
8171
8172 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
8173 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
8174 if (MI->getOperand(0).getImm() == ARMCC::NE)
8175 std::swap(destMBB, exitMBB);
8176
8177 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8178 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008179 if (isThumb2)
8180 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8181 else
8182 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008183
8184 MI->eraseFromParent(); // The pseudo instruction is gone now.
8185 return BB;
8186 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008187
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008188 case ARM::Int_eh_sjlj_setjmp:
8189 case ARM::Int_eh_sjlj_setjmp_nofp:
8190 case ARM::tInt_eh_sjlj_setjmp:
8191 case ARM::t2Int_eh_sjlj_setjmp:
8192 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008193 return BB;
8194
8195 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008196 EmitSjLjDispatchBlock(MI, BB);
8197 return BB;
8198
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008199 case ARM::ABS:
8200 case ARM::t2ABS: {
8201 // To insert an ABS instruction, we have to insert the
8202 // diamond control-flow pattern. The incoming instruction knows the
8203 // source vreg to test against 0, the destination vreg to set,
8204 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008205 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008206 // It transforms
8207 // V1 = ABS V0
8208 // into
8209 // V2 = MOVS V0
8210 // BCC (branch to SinkBB if V0 >= 0)
8211 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008212 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008213 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008214 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008215 MachineFunction *Fn = BB->getParent();
8216 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8217 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8218 Fn->insert(BBI, RSBBB);
8219 Fn->insert(BBI, SinkBB);
8220
8221 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8222 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008223 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008224 bool isThumb2 = Subtarget->isThumb2();
8225 MachineRegisterInfo &MRI = Fn->getRegInfo();
8226 // In Thumb mode S must not be specified if source register is the SP or
8227 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008228 unsigned NewRsbDstReg =
8229 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008230
8231 // Transfer the remainder of BB and its successor edges to sinkMBB.
8232 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008233 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008234 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8235
8236 BB->addSuccessor(RSBBB);
8237 BB->addSuccessor(SinkBB);
8238
8239 // fall through to SinkMBB
8240 RSBBB->addSuccessor(SinkBB);
8241
Manman Rene0763c72012-06-15 21:32:12 +00008242 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008243 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008244 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8245 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008246
8247 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008248 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008249 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8250 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8251
8252 // insert rsbri in RSBBB
8253 // Note: BCC and rsbri will be converted into predicated rsbmi
8254 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008255 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008256 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008257 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008258 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8259
Andrew Trick3f07c422011-10-18 18:40:53 +00008260 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008261 // reuse ABSDstReg to not change uses of ABS instruction
8262 BuildMI(*SinkBB, SinkBB->begin(), dl,
8263 TII->get(ARM::PHI), ABSDstReg)
8264 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008265 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008266
8267 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008268 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008269
8270 // return last added BB
8271 return SinkBB;
8272 }
Manman Rene8735522012-06-01 19:33:18 +00008273 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008274 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008275 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008276 case ARM::WIN__CHKSTK:
8277 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008278 case ARM::WIN__DBZCHK:
8279 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008280 }
8281}
8282
Scott Douglass953f9082015-10-05 14:49:54 +00008283/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8284/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8285/// instead of as a custom inserter because we need the use list from the SDNode.
8286static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8287 MachineInstr *MI, const SDNode *Node) {
8288 bool isThumb1 = Subtarget->isThumb1Only();
8289
8290 DebugLoc DL = MI->getDebugLoc();
8291 MachineFunction *MF = MI->getParent()->getParent();
8292 MachineRegisterInfo &MRI = MF->getRegInfo();
8293 MachineInstrBuilder MIB(*MF, MI);
8294
8295 // If the new dst/src is unused mark it as dead.
8296 if (!Node->hasAnyUseOfValue(0)) {
8297 MI->getOperand(0).setIsDead(true);
8298 }
8299 if (!Node->hasAnyUseOfValue(1)) {
8300 MI->getOperand(1).setIsDead(true);
8301 }
8302
8303 // The MEMCPY both defines and kills the scratch registers.
8304 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8305 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8306 : &ARM::GPRRegClass);
8307 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8308 }
8309}
8310
Evan Chenge6fba772011-08-30 19:09:48 +00008311void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8312 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008313 if (MI->getOpcode() == ARM::MEMCPY) {
8314 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8315 return;
8316 }
8317
Evan Cheng7f8e5632011-12-07 07:15:52 +00008318 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008319 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8320 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8321 // operand is still set to noreg. If needed, set the optional operand's
8322 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008323 //
Andrew Trick88b24502011-10-18 19:18:52 +00008324 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008325
Andrew Trick924123a2011-09-21 02:20:46 +00008326 // Rename pseudo opcodes.
8327 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8328 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008329 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008330 MCID = &TII->get(NewOpc);
8331
8332 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8333 "converted opcode should be the same except for cc_out");
8334
8335 MI->setDesc(*MCID);
8336
8337 // Add the optional cc_out operand
8338 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008339 }
Andrew Trick88b24502011-10-18 19:18:52 +00008340 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008341
8342 // Any ARM instruction that sets the 's' bit should specify an optional
8343 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008344 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008345 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008346 return;
8347 }
Andrew Trick924123a2011-09-21 02:20:46 +00008348 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8349 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008350 bool definesCPSR = false;
8351 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008352 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008353 i != e; ++i) {
8354 const MachineOperand &MO = MI->getOperand(i);
8355 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8356 definesCPSR = true;
8357 if (MO.isDead())
8358 deadCPSR = true;
8359 MI->RemoveOperand(i);
8360 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008361 }
8362 }
Andrew Trick8586e622011-09-20 03:17:40 +00008363 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008364 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008365 return;
8366 }
8367 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008368 if (deadCPSR) {
8369 assert(!MI->getOperand(ccOutIdx).getReg() &&
8370 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008371 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008372 }
Andrew Trick8586e622011-09-20 03:17:40 +00008373
Andrew Trick924123a2011-09-21 02:20:46 +00008374 // If this instruction was defined with an optional CPSR def and its dag node
8375 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008376 MachineOperand &MO = MI->getOperand(ccOutIdx);
8377 MO.setReg(ARM::CPSR);
8378 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008379}
8380
Evan Cheng10043e22007-01-19 07:51:42 +00008381//===----------------------------------------------------------------------===//
8382// ARM Optimization Hooks
8383//===----------------------------------------------------------------------===//
8384
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008385// Helper function that checks if N is a null or all ones constant.
8386static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008387 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008388}
8389
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008390// Return true if N is conditionally 0 or all ones.
8391// Detects these expressions where cc is an i1 value:
8392//
8393// (select cc 0, y) [AllOnes=0]
8394// (select cc y, 0) [AllOnes=0]
8395// (zext cc) [AllOnes=0]
8396// (sext cc) [AllOnes=0/1]
8397// (select cc -1, y) [AllOnes=1]
8398// (select cc y, -1) [AllOnes=1]
8399//
8400// Invert is set when N is the null/all ones constant when CC is false.
8401// OtherOp is set to the alternative value of N.
8402static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8403 SDValue &CC, bool &Invert,
8404 SDValue &OtherOp,
8405 SelectionDAG &DAG) {
8406 switch (N->getOpcode()) {
8407 default: return false;
8408 case ISD::SELECT: {
8409 CC = N->getOperand(0);
8410 SDValue N1 = N->getOperand(1);
8411 SDValue N2 = N->getOperand(2);
8412 if (isZeroOrAllOnes(N1, AllOnes)) {
8413 Invert = false;
8414 OtherOp = N2;
8415 return true;
8416 }
8417 if (isZeroOrAllOnes(N2, AllOnes)) {
8418 Invert = true;
8419 OtherOp = N1;
8420 return true;
8421 }
8422 return false;
8423 }
8424 case ISD::ZERO_EXTEND:
8425 // (zext cc) can never be the all ones value.
8426 if (AllOnes)
8427 return false;
8428 // Fall through.
8429 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008430 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008431 EVT VT = N->getValueType(0);
8432 CC = N->getOperand(0);
8433 if (CC.getValueType() != MVT::i1)
8434 return false;
8435 Invert = !AllOnes;
8436 if (AllOnes)
8437 // When looking for an AllOnes constant, N is an sext, and the 'other'
8438 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008439 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008440 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8441 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008442 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008443 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008444 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8445 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008446 return true;
8447 }
8448 }
8449}
8450
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008451// Combine a constant select operand into its use:
8452//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008453// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8454// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8455// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8456// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8457// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008458//
8459// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008460// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008461//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008462// Also recognize sext/zext from i1:
8463//
8464// (add (zext cc), x) -> (select cc (add x, 1), x)
8465// (add (sext cc), x) -> (select cc (add x, -1), x)
8466//
8467// These transformations eventually create predicated instructions.
8468//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008469// @param N The node to transform.
8470// @param Slct The N operand that is a select.
8471// @param OtherOp The other N operand (x above).
8472// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008473// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008474// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008475static
8476SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008477 TargetLowering::DAGCombinerInfo &DCI,
8478 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008479 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008480 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008481 SDValue NonConstantVal;
8482 SDValue CCOp;
8483 bool SwapSelectOps;
8484 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8485 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008486 return SDValue();
8487
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008488 // Slct is now know to be the desired identity constant when CC is true.
8489 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008490 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008491 OtherOp, NonConstantVal);
8492 // Unless SwapSelectOps says CC should be false.
8493 if (SwapSelectOps)
8494 std::swap(TrueVal, FalseVal);
8495
Andrew Trickef9de2a2013-05-25 02:42:55 +00008496 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008497 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008498}
8499
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008500// Attempt combineSelectAndUse on each operand of a commutative operator N.
8501static
8502SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8503 TargetLowering::DAGCombinerInfo &DCI) {
8504 SDValue N0 = N->getOperand(0);
8505 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008506 if (N0.getNode()->hasOneUse())
8507 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008508 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008509 if (N1.getNode()->hasOneUse())
8510 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008511 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008512 return SDValue();
8513}
8514
Eric Christopher1b8b94192011-06-29 21:10:36 +00008515// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008516// (only after legalization).
8517static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8518 TargetLowering::DAGCombinerInfo &DCI,
8519 const ARMSubtarget *Subtarget) {
8520
8521 // Only perform optimization if after legalize, and if NEON is available. We
8522 // also expected both operands to be BUILD_VECTORs.
8523 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8524 || N0.getOpcode() != ISD::BUILD_VECTOR
8525 || N1.getOpcode() != ISD::BUILD_VECTOR)
8526 return SDValue();
8527
8528 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8529 EVT VT = N->getValueType(0);
8530 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8531 return SDValue();
8532
8533 // Check that the vector operands are of the right form.
8534 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8535 // operands, where N is the size of the formed vector.
8536 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8537 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008538
8539 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008540 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008541 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008542 SDValue Vec = N0->getOperand(0)->getOperand(0);
8543 SDNode *V = Vec.getNode();
8544 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008545
Eric Christopher1b8b94192011-06-29 21:10:36 +00008546 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008547 // check to see if each of their operands are an EXTRACT_VECTOR with
8548 // the same vector and appropriate index.
8549 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8550 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8551 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008552
Tanya Lattnere9e67052011-06-14 23:48:48 +00008553 SDValue ExtVec0 = N0->getOperand(i);
8554 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008555
Tanya Lattnere9e67052011-06-14 23:48:48 +00008556 // First operand is the vector, verify its the same.
8557 if (V != ExtVec0->getOperand(0).getNode() ||
8558 V != ExtVec1->getOperand(0).getNode())
8559 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008560
Tanya Lattnere9e67052011-06-14 23:48:48 +00008561 // Second is the constant, verify its correct.
8562 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8563 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008564
Tanya Lattnere9e67052011-06-14 23:48:48 +00008565 // For the constant, we want to see all the even or all the odd.
8566 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8567 || C1->getZExtValue() != nextIndex+1)
8568 return SDValue();
8569
8570 // Increment index.
8571 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008572 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008573 return SDValue();
8574 }
8575
8576 // Create VPADDL node.
8577 SelectionDAG &DAG = DCI.DAG;
8578 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008579
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008580 SDLoc dl(N);
8581
Tanya Lattnere9e67052011-06-14 23:48:48 +00008582 // Build operand list.
8583 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008584 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008585 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008586
8587 // Input is the vector.
8588 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008589
Tanya Lattnere9e67052011-06-14 23:48:48 +00008590 // Get widened type and narrowed type.
8591 MVT widenType;
8592 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00008593
Silviu Barangaa3106e62014-04-03 10:44:27 +00008594 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8595 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008596 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8597 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8598 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8599 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008600 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008601 }
8602
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008603 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008604 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008605 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008606}
8607
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008608static SDValue findMUL_LOHI(SDValue V) {
8609 if (V->getOpcode() == ISD::UMUL_LOHI ||
8610 V->getOpcode() == ISD::SMUL_LOHI)
8611 return V;
8612 return SDValue();
8613}
8614
8615static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8616 TargetLowering::DAGCombinerInfo &DCI,
8617 const ARMSubtarget *Subtarget) {
8618
8619 if (Subtarget->isThumb1Only()) return SDValue();
8620
8621 // Only perform the checks after legalize when the pattern is available.
8622 if (DCI.isBeforeLegalize()) return SDValue();
8623
8624 // Look for multiply add opportunities.
8625 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8626 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8627 // a glue link from the first add to the second add.
8628 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8629 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008630 // UMUL_LOHI
8631 // / :lo \ :hi
8632 // / \ [no multiline comment]
8633 // loAdd -> ADDE |
8634 // \ :glue /
8635 // \ /
8636 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008637 //
8638 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8639 SDValue AddcOp0 = AddcNode->getOperand(0);
8640 SDValue AddcOp1 = AddcNode->getOperand(1);
8641
8642 // Check if the two operands are from the same mul_lohi node.
8643 if (AddcOp0.getNode() == AddcOp1.getNode())
8644 return SDValue();
8645
8646 assert(AddcNode->getNumValues() == 2 &&
8647 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008648 "Expect ADDC with two result values. First: i32");
8649
8650 // Check that we have a glued ADDC node.
8651 if (AddcNode->getValueType(1) != MVT::Glue)
8652 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008653
8654 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8655 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8656 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8657 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8658 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8659 return SDValue();
8660
8661 // Look for the glued ADDE.
8662 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008663 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008664 return SDValue();
8665
8666 // Make sure it is really an ADDE.
8667 if (AddeNode->getOpcode() != ISD::ADDE)
8668 return SDValue();
8669
8670 assert(AddeNode->getNumOperands() == 3 &&
8671 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8672 "ADDE node has the wrong inputs");
8673
8674 // Check for the triangle shape.
8675 SDValue AddeOp0 = AddeNode->getOperand(0);
8676 SDValue AddeOp1 = AddeNode->getOperand(1);
8677
8678 // Make sure that the ADDE operands are not coming from the same node.
8679 if (AddeOp0.getNode() == AddeOp1.getNode())
8680 return SDValue();
8681
8682 // Find the MUL_LOHI node walking up ADDE's operands.
8683 bool IsLeftOperandMUL = false;
8684 SDValue MULOp = findMUL_LOHI(AddeOp0);
8685 if (MULOp == SDValue())
8686 MULOp = findMUL_LOHI(AddeOp1);
8687 else
8688 IsLeftOperandMUL = true;
8689 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008690 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008691
8692 // Figure out the right opcode.
8693 unsigned Opc = MULOp->getOpcode();
8694 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8695
8696 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008697 SDValue* HiAdd = nullptr;
8698 SDValue* LoMul = nullptr;
8699 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008700
Jyoti Allurf1d70502015-01-23 09:10:03 +00008701 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8702 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8703 return SDValue();
8704
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008705 if (IsLeftOperandMUL)
8706 HiAdd = &AddeOp1;
8707 else
8708 HiAdd = &AddeOp0;
8709
8710
Jyoti Allurf1d70502015-01-23 09:10:03 +00008711 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8712 // whose low result is fed to the ADDC we are checking.
8713
8714 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008715 LoMul = &AddcOp0;
8716 LowAdd = &AddcOp1;
8717 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008718 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008719 LoMul = &AddcOp1;
8720 LowAdd = &AddcOp0;
8721 }
8722
Craig Topper062a2ba2014-04-25 05:30:21 +00008723 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008724 return SDValue();
8725
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008726 // Create the merged node.
8727 SelectionDAG &DAG = DCI.DAG;
8728
8729 // Build operand list.
8730 SmallVector<SDValue, 8> Ops;
8731 Ops.push_back(LoMul->getOperand(0));
8732 Ops.push_back(LoMul->getOperand(1));
8733 Ops.push_back(*LowAdd);
8734 Ops.push_back(*HiAdd);
8735
Andrew Trickef9de2a2013-05-25 02:42:55 +00008736 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008737 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008738
8739 // Replace the ADDs' nodes uses by the MLA node's values.
8740 SDValue HiMLALResult(MLALNode.getNode(), 1);
8741 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8742
8743 SDValue LoMLALResult(MLALNode.getNode(), 0);
8744 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8745
8746 // Return original node to notify the driver to stop replacing.
8747 SDValue resNode(AddcNode, 0);
8748 return resNode;
8749}
8750
8751/// PerformADDCCombine - Target-specific dag combine transform from
8752/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8753static SDValue PerformADDCCombine(SDNode *N,
8754 TargetLowering::DAGCombinerInfo &DCI,
8755 const ARMSubtarget *Subtarget) {
8756
8757 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8758
8759}
8760
Bob Wilson728eb292010-07-29 20:34:14 +00008761/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8762/// operands N0 and N1. This is a helper for PerformADDCombine that is
8763/// called with the default operands, and if that fails, with commuted
8764/// operands.
8765static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008766 TargetLowering::DAGCombinerInfo &DCI,
8767 const ARMSubtarget *Subtarget){
8768
8769 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008770 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00008771 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008772
Chris Lattner4147f082009-03-12 06:52:53 +00008773 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008774 if (N0.getNode()->hasOneUse())
8775 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
8776 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00008777 return SDValue();
8778}
8779
Bob Wilson728eb292010-07-29 20:34:14 +00008780/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8781///
8782static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008783 TargetLowering::DAGCombinerInfo &DCI,
8784 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008785 SDValue N0 = N->getOperand(0);
8786 SDValue N1 = N->getOperand(1);
8787
8788 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008789 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00008790 return Result;
8791
8792 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008793 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008794}
8795
Chris Lattner4147f082009-03-12 06:52:53 +00008796/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008797///
Chris Lattner4147f082009-03-12 06:52:53 +00008798static SDValue PerformSUBCombine(SDNode *N,
8799 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008800 SDValue N0 = N->getOperand(0);
8801 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008802
Chris Lattner4147f082009-03-12 06:52:53 +00008803 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008804 if (N1.getNode()->hasOneUse())
8805 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
8806 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00008807
Chris Lattner4147f082009-03-12 06:52:53 +00008808 return SDValue();
8809}
8810
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008811/// PerformVMULCombine
8812/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8813/// special multiplier accumulator forwarding.
8814/// vmul d3, d0, d2
8815/// vmla d3, d1, d2
8816/// is faster than
8817/// vadd d3, d0, d1
8818/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008819// However, for (A + B) * (A + B),
8820// vadd d2, d0, d1
8821// vmul d3, d0, d2
8822// vmla d3, d1, d2
8823// is slower than
8824// vadd d2, d0, d1
8825// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008826static SDValue PerformVMULCombine(SDNode *N,
8827 TargetLowering::DAGCombinerInfo &DCI,
8828 const ARMSubtarget *Subtarget) {
8829 if (!Subtarget->hasVMLxForwarding())
8830 return SDValue();
8831
8832 SelectionDAG &DAG = DCI.DAG;
8833 SDValue N0 = N->getOperand(0);
8834 SDValue N1 = N->getOperand(1);
8835 unsigned Opcode = N0.getOpcode();
8836 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8837 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008838 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008839 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8840 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8841 return SDValue();
8842 std::swap(N0, N1);
8843 }
8844
Weiming Zhao2052f482013-09-25 23:12:06 +00008845 if (N0 == N1)
8846 return SDValue();
8847
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008848 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008849 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008850 SDValue N00 = N0->getOperand(0);
8851 SDValue N01 = N0->getOperand(1);
8852 return DAG.getNode(Opcode, DL, VT,
8853 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8854 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8855}
8856
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008857static SDValue PerformMULCombine(SDNode *N,
8858 TargetLowering::DAGCombinerInfo &DCI,
8859 const ARMSubtarget *Subtarget) {
8860 SelectionDAG &DAG = DCI.DAG;
8861
8862 if (Subtarget->isThumb1Only())
8863 return SDValue();
8864
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008865 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8866 return SDValue();
8867
8868 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008869 if (VT.is64BitVector() || VT.is128BitVector())
8870 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008871 if (VT != MVT::i32)
8872 return SDValue();
8873
8874 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8875 if (!C)
8876 return SDValue();
8877
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008878 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008879 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008880
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008881 ShiftAmt = ShiftAmt & (32 - 1);
8882 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008883 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008884
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008885 SDValue Res;
8886 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008887
8888 if (MulAmt >= 0) {
8889 if (isPowerOf2_32(MulAmt - 1)) {
8890 // (mul x, 2^N + 1) => (add (shl x, N), x)
8891 Res = DAG.getNode(ISD::ADD, DL, VT,
8892 V,
8893 DAG.getNode(ISD::SHL, DL, VT,
8894 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008895 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008896 MVT::i32)));
8897 } else if (isPowerOf2_32(MulAmt + 1)) {
8898 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8899 Res = DAG.getNode(ISD::SUB, DL, VT,
8900 DAG.getNode(ISD::SHL, DL, VT,
8901 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008902 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008903 MVT::i32)),
8904 V);
8905 } else
8906 return SDValue();
8907 } else {
8908 uint64_t MulAmtAbs = -MulAmt;
8909 if (isPowerOf2_32(MulAmtAbs + 1)) {
8910 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8911 Res = DAG.getNode(ISD::SUB, DL, VT,
8912 V,
8913 DAG.getNode(ISD::SHL, DL, VT,
8914 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008915 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008916 MVT::i32)));
8917 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8918 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8919 Res = DAG.getNode(ISD::ADD, DL, VT,
8920 V,
8921 DAG.getNode(ISD::SHL, DL, VT,
8922 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008923 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008924 MVT::i32)));
8925 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008926 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008927
8928 } else
8929 return SDValue();
8930 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008931
8932 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008933 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008934 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008935
8936 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008937 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008938 return SDValue();
8939}
8940
Owen Anderson30c48922010-11-05 19:27:46 +00008941static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008942 TargetLowering::DAGCombinerInfo &DCI,
8943 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008944
Owen Anderson30c48922010-11-05 19:27:46 +00008945 // Attempt to use immediate-form VBIC
8946 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008947 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008948 EVT VT = N->getValueType(0);
8949 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008950
Tanya Lattner266792a2011-04-07 15:24:20 +00008951 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8952 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008953
Owen Anderson30c48922010-11-05 19:27:46 +00008954 APInt SplatBits, SplatUndef;
8955 unsigned SplatBitSize;
8956 bool HasAnyUndefs;
8957 if (BVN &&
8958 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8959 if (SplatBitSize <= 64) {
8960 EVT VbicVT;
8961 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8962 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008963 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008964 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008965 if (Val.getNode()) {
8966 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008967 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008968 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008969 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008970 }
8971 }
8972 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008973
Evan Chenge87681c2012-02-23 01:19:06 +00008974 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008975 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008976 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008977 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008978 }
8979
Owen Anderson30c48922010-11-05 19:27:46 +00008980 return SDValue();
8981}
8982
Jim Grosbach11013ed2010-07-16 23:05:05 +00008983/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8984static SDValue PerformORCombine(SDNode *N,
8985 TargetLowering::DAGCombinerInfo &DCI,
8986 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008987 // Attempt to use immediate-form VORR
8988 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008989 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008990 EVT VT = N->getValueType(0);
8991 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008992
Tanya Lattner266792a2011-04-07 15:24:20 +00008993 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8994 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008995
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008996 APInt SplatBits, SplatUndef;
8997 unsigned SplatBitSize;
8998 bool HasAnyUndefs;
8999 if (BVN && Subtarget->hasNEON() &&
9000 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9001 if (SplatBitSize <= 64) {
9002 EVT VorrVT;
9003 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9004 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009005 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009006 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009007 if (Val.getNode()) {
9008 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009009 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009010 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009011 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009012 }
9013 }
9014 }
9015
Evan Chenge87681c2012-02-23 01:19:06 +00009016 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009017 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009018 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009019 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009020 }
9021
Nadav Rotem3a94c542012-08-13 18:52:44 +00009022 // The code below optimizes (or (and X, Y), Z).
9023 // The AND operand needs to have a single user to make these optimizations
9024 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009025 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009026 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009027 return SDValue();
9028 SDValue N1 = N->getOperand(1);
9029
9030 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9031 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9032 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9033 APInt SplatUndef;
9034 unsigned SplatBitSize;
9035 bool HasAnyUndefs;
9036
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009037 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009038 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009039 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9040 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009041 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009042 HasAnyUndefs) && !HasAnyUndefs) {
9043 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9044 HasAnyUndefs) && !HasAnyUndefs) {
9045 // Ensure that the bit width of the constants are the same and that
9046 // the splat arguments are logical inverses as per the pattern we
9047 // are trying to simplify.
9048 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9049 SplatBits0 == ~SplatBits1) {
9050 // Canonicalize the vector type to make instruction selection
9051 // simpler.
9052 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9053 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9054 N0->getOperand(1),
9055 N0->getOperand(0),
9056 N1->getOperand(0));
9057 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9058 }
9059 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009060 }
9061 }
9062
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009063 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9064 // reasonable.
9065
Jim Grosbach11013ed2010-07-16 23:05:05 +00009066 // BFI is only available on V6T2+
9067 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9068 return SDValue();
9069
Andrew Trickef9de2a2013-05-25 02:42:55 +00009070 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009071 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009072 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009073 //
9074 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009075 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009076 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009077 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009078 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009079 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009080
Jim Grosbach11013ed2010-07-16 23:05:05 +00009081 if (VT != MVT::i32)
9082 return SDValue();
9083
Evan Cheng2e51bb42010-12-13 20:32:54 +00009084 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009085
Jim Grosbach11013ed2010-07-16 23:05:05 +00009086 // The value and the mask need to be constants so we can verify this is
9087 // actually a bitfield set. If the mask is 0xffff, we can do better
9088 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009089 SDValue MaskOp = N0.getOperand(1);
9090 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9091 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009092 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009093 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009094 if (Mask == 0xffff)
9095 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009096 SDValue Res;
9097 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009098 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9099 if (N1C) {
9100 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009101 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009102 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009103
Evan Cheng34345752010-12-11 04:11:38 +00009104 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009105 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009106
Evan Cheng2e51bb42010-12-13 20:32:54 +00009107 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009108 DAG.getConstant(Val, DL, MVT::i32),
9109 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009110
9111 // Do not add new nodes to DAG combiner worklist.
9112 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009113 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009114 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009115 } else if (N1.getOpcode() == ISD::AND) {
9116 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009117 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9118 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009119 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009120 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009121
Eric Christopherd5530962011-03-26 01:21:03 +00009122 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9123 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009124 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009125 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009126 // The pack halfword instruction works better for masks that fit it,
9127 // so use that when it's available.
9128 if (Subtarget->hasT2ExtractPack() &&
9129 (Mask == 0xffff || Mask == 0xffff0000))
9130 return SDValue();
9131 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009132 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009133 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009134 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009135 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009136 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009137 // Do not add new nodes to DAG combiner worklist.
9138 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009139 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009140 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009141 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009142 // The pack halfword instruction works better for masks that fit it,
9143 // so use that when it's available.
9144 if (Subtarget->hasT2ExtractPack() &&
9145 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9146 return SDValue();
9147 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009148 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009149 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009150 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009151 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009152 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009153 // Do not add new nodes to DAG combiner worklist.
9154 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009155 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009156 }
9157 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009158
Evan Cheng2e51bb42010-12-13 20:32:54 +00009159 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9160 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9161 ARM::isBitFieldInvertedMask(~Mask)) {
9162 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9163 // where lsb(mask) == #shamt and masked bits of B are known zero.
9164 SDValue ShAmt = N00.getOperand(1);
9165 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009166 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009167 if (ShAmtC != LSB)
9168 return SDValue();
9169
9170 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009171 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009172
9173 // Do not add new nodes to DAG combiner worklist.
9174 DCI.CombineTo(N, Res, false);
9175 }
9176
Jim Grosbach11013ed2010-07-16 23:05:05 +00009177 return SDValue();
9178}
9179
Evan Chenge87681c2012-02-23 01:19:06 +00009180static SDValue PerformXORCombine(SDNode *N,
9181 TargetLowering::DAGCombinerInfo &DCI,
9182 const ARMSubtarget *Subtarget) {
9183 EVT VT = N->getValueType(0);
9184 SelectionDAG &DAG = DCI.DAG;
9185
9186 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9187 return SDValue();
9188
9189 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009190 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009191 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009192 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009193 }
9194
9195 return SDValue();
9196}
9197
James Molloyce12c922015-11-11 15:40:40 +00009198// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9199// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9200// their position in "to" (Rd).
9201static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9202 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009203
James Molloyce12c922015-11-11 15:40:40 +00009204 SDValue From = N->getOperand(1);
9205 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9206 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9207
9208 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9209 // #C in the base of the SHR.
9210 if (From->getOpcode() == ISD::SRL &&
9211 isa<ConstantSDNode>(From->getOperand(1))) {
9212 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9213 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9214 FromMask <<= Shift.getLimitedValue(31);
9215 From = From->getOperand(0);
9216 }
9217
9218 return From;
9219}
9220
9221// If A and B contain one contiguous set of bits, does A | B == A . B?
9222//
9223// Neither A nor B must be zero.
9224static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9225 unsigned LastActiveBitInA = A.countTrailingZeros();
9226 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9227 return LastActiveBitInA - 1 == FirstActiveBitInB;
9228}
9229
9230static SDValue FindBFIToCombineWith(SDNode *N) {
9231 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9232 // if one exists.
9233 APInt ToMask, FromMask;
9234 SDValue From = ParseBFI(N, ToMask, FromMask);
9235 SDValue To = N->getOperand(0);
9236
9237 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9238 // aren't compatible, but not if they set the same bit in their destination as
9239 // we do (or that of any BFI we're going to combine with).
9240 SDValue V = To;
9241 APInt CombinedToMask = ToMask;
9242 while (V.getOpcode() == ARMISD::BFI) {
9243 APInt NewToMask, NewFromMask;
9244 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9245 if (NewFrom != From) {
9246 // This BFI has a different base. Keep going.
9247 CombinedToMask |= NewToMask;
9248 V = V.getOperand(0);
9249 continue;
9250 }
9251
9252 // Do the written bits conflict with any we've seen so far?
9253 if ((NewToMask & CombinedToMask).getBoolValue())
9254 // Conflicting bits - bail out because going further is unsafe.
9255 return SDValue();
9256
9257 // Are the new bits contiguous when combined with the old bits?
9258 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9259 BitsProperlyConcatenate(FromMask, NewFromMask))
9260 return V;
9261 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9262 BitsProperlyConcatenate(NewFromMask, FromMask))
9263 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009264
James Molloyce12c922015-11-11 15:40:40 +00009265 // We've seen a write to some bits, so track it.
9266 CombinedToMask |= NewToMask;
9267 // Keep going...
9268 V = V.getOperand(0);
9269 }
9270
9271 return SDValue();
9272}
9273
Evan Chengc1778132010-12-14 03:22:07 +00009274static SDValue PerformBFICombine(SDNode *N,
9275 TargetLowering::DAGCombinerInfo &DCI) {
9276 SDValue N1 = N->getOperand(1);
9277 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009278 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9279 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009280 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9281 if (!N11C)
9282 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009283 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009284 unsigned LSB = countTrailingZeros(~InvMask);
9285 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009286 assert(Width <
9287 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009288 "undefined behavior");
9289 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009290 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009291 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009292 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009293 N->getOperand(0), N1.getOperand(0),
9294 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009295 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9296 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9297 // Keep track of any consecutive bits set that all come from the same base
9298 // value. We can combine these together into a single BFI.
9299 SDValue CombineBFI = FindBFIToCombineWith(N);
9300 if (CombineBFI == SDValue())
9301 return SDValue();
9302
9303 // We've found a BFI.
9304 APInt ToMask1, FromMask1;
9305 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9306
9307 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009308 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9309 assert(From1 == From2);
9310 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009311
James Molloyce12c922015-11-11 15:40:40 +00009312 // First, unlink CombineBFI.
9313 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9314 // Then create a new BFI, combining the two together.
9315 APInt NewFromMask = FromMask1 | FromMask2;
9316 APInt NewToMask = ToMask1 | ToMask2;
9317
9318 EVT VT = N->getValueType(0);
9319 SDLoc dl(N);
9320
9321 if (NewFromMask[0] == 0)
9322 From1 = DCI.DAG.getNode(
9323 ISD::SRL, dl, VT, From1,
9324 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9325 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9326 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009327 }
9328 return SDValue();
9329}
9330
Bob Wilson22806742010-09-22 22:09:21 +00009331/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9332/// ARMISD::VMOVRRD.
9333static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009334 TargetLowering::DAGCombinerInfo &DCI,
9335 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009336 // vmovrrd(vmovdrr x, y) -> x,y
9337 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009338 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009339 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009340
9341 // vmovrrd(load f64) -> (load i32), (load i32)
9342 SDNode *InNode = InDouble.getNode();
9343 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9344 InNode->getValueType(0) == MVT::f64 &&
9345 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9346 !cast<LoadSDNode>(InNode)->isVolatile()) {
9347 // TODO: Should this be done for non-FrameIndex operands?
9348 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9349
9350 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009351 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009352 SDValue BasePtr = LD->getBasePtr();
9353 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9354 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009355 LD->isNonTemporal(), LD->isInvariant(),
9356 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009357
9358 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009359 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009360 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9361 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009362 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009363 std::min(4U, LD->getAlignment() / 2));
9364
9365 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009366 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009367 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009368 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009369 return Result;
9370 }
9371
Bob Wilson22806742010-09-22 22:09:21 +00009372 return SDValue();
9373}
9374
9375/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9376/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9377static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9378 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9379 SDValue Op0 = N->getOperand(0);
9380 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009381 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009382 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009383 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009384 Op1 = Op1.getOperand(0);
9385 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9386 Op0.getNode() == Op1.getNode() &&
9387 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009388 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009389 N->getValueType(0), Op0.getOperand(0));
9390 return SDValue();
9391}
9392
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009393/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9394/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9395/// i64 vector to have f64 elements, since the value can then be loaded
9396/// directly into a VFP register.
9397static bool hasNormalLoadOperand(SDNode *N) {
9398 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9399 for (unsigned i = 0; i < NumElts; ++i) {
9400 SDNode *Elt = N->getOperand(i).getNode();
9401 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9402 return true;
9403 }
9404 return false;
9405}
9406
Bob Wilsoncb6db982010-09-17 22:59:05 +00009407/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9408/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009409static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009410 TargetLowering::DAGCombinerInfo &DCI,
9411 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009412 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9413 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9414 // into a pair of GPRs, which is fine when the value is used as a scalar,
9415 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009416 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009417 if (N->getNumOperands() == 2)
9418 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009419 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +00009420
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009421 // Load i64 elements as f64 values so that type legalization does not split
9422 // them up into i32 values.
9423 EVT VT = N->getValueType(0);
9424 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9425 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009426 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009427 SmallVector<SDValue, 8> Ops;
9428 unsigned NumElts = VT.getVectorNumElements();
9429 for (unsigned i = 0; i < NumElts; ++i) {
9430 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9431 Ops.push_back(V);
9432 // Make the DAGCombiner fold the bitcast.
9433 DCI.AddToWorklist(V.getNode());
9434 }
9435 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009436 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009437 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9438}
9439
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009440/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9441static SDValue
9442PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9443 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9444 // At that time, we may have inserted bitcasts from integer to float.
9445 // If these bitcasts have survived DAGCombine, change the lowering of this
9446 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9447 // force to use floating point types.
9448
9449 // Make sure we can change the type of the vector.
9450 // This is possible iff:
9451 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9452 // 1.1. Vector is used only once.
9453 // 1.2. Use is a bit convert to an integer type.
9454 // 2. The size of its operands are 32-bits (64-bits are not legal).
9455 EVT VT = N->getValueType(0);
9456 EVT EltVT = VT.getVectorElementType();
9457
9458 // Check 1.1. and 2.
9459 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9460 return SDValue();
9461
9462 // By construction, the input type must be float.
9463 assert(EltVT == MVT::f32 && "Unexpected type!");
9464
9465 // Check 1.2.
9466 SDNode *Use = *N->use_begin();
9467 if (Use->getOpcode() != ISD::BITCAST ||
9468 Use->getValueType(0).isFloatingPoint())
9469 return SDValue();
9470
9471 // Check profitability.
9472 // Model is, if more than half of the relevant operands are bitcast from
9473 // i32, turn the build_vector into a sequence of insert_vector_elt.
9474 // Relevant operands are everything that is not statically
9475 // (i.e., at compile time) bitcasted.
9476 unsigned NumOfBitCastedElts = 0;
9477 unsigned NumElts = VT.getVectorNumElements();
9478 unsigned NumOfRelevantElts = NumElts;
9479 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9480 SDValue Elt = N->getOperand(Idx);
9481 if (Elt->getOpcode() == ISD::BITCAST) {
9482 // Assume only bit cast to i32 will go away.
9483 if (Elt->getOperand(0).getValueType() == MVT::i32)
9484 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +00009485 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009486 // Constants are statically casted, thus do not count them as
9487 // relevant operands.
9488 --NumOfRelevantElts;
9489 }
9490
9491 // Check if more than half of the elements require a non-free bitcast.
9492 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9493 return SDValue();
9494
9495 SelectionDAG &DAG = DCI.DAG;
9496 // Create the new vector type.
9497 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9498 // Check if the type is legal.
9499 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9500 if (!TLI.isTypeLegal(VecVT))
9501 return SDValue();
9502
9503 // Combine:
9504 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9505 // => BITCAST INSERT_VECTOR_ELT
9506 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9507 // (BITCAST EN), N.
9508 SDValue Vec = DAG.getUNDEF(VecVT);
9509 SDLoc dl(N);
9510 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9511 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +00009512 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009513 continue;
9514 if (V.getOpcode() == ISD::BITCAST &&
9515 V->getOperand(0).getValueType() == MVT::i32)
9516 // Fold obvious case.
9517 V = V.getOperand(0);
9518 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009519 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009520 // Make the DAGCombiner fold the bitcasts.
9521 DCI.AddToWorklist(V.getNode());
9522 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009523 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009524 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9525 }
9526 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9527 // Make the DAGCombiner fold the bitcasts.
9528 DCI.AddToWorklist(Vec.getNode());
9529 return Vec;
9530}
9531
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009532/// PerformInsertEltCombine - Target-specific dag combine xforms for
9533/// ISD::INSERT_VECTOR_ELT.
9534static SDValue PerformInsertEltCombine(SDNode *N,
9535 TargetLowering::DAGCombinerInfo &DCI) {
9536 // Bitcast an i64 load inserted into a vector to f64.
9537 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9538 EVT VT = N->getValueType(0);
9539 SDNode *Elt = N->getOperand(1).getNode();
9540 if (VT.getVectorElementType() != MVT::i64 ||
9541 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9542 return SDValue();
9543
9544 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009545 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009546 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9547 VT.getVectorNumElements());
9548 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9549 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9550 // Make the DAGCombiner fold the bitcasts.
9551 DCI.AddToWorklist(Vec.getNode());
9552 DCI.AddToWorklist(V.getNode());
9553 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9554 Vec, V, N->getOperand(2));
9555 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009556}
9557
Bob Wilsonc7334a12010-10-27 20:38:28 +00009558/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9559/// ISD::VECTOR_SHUFFLE.
9560static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9561 // The LLVM shufflevector instruction does not require the shuffle mask
9562 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9563 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9564 // operands do not match the mask length, they are extended by concatenating
9565 // them with undef vectors. That is probably the right thing for other
9566 // targets, but for NEON it is better to concatenate two double-register
9567 // size vector operands into a single quad-register size vector. Do that
9568 // transformation here:
9569 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9570 // shuffle(concat(v1, v2), undef)
9571 SDValue Op0 = N->getOperand(0);
9572 SDValue Op1 = N->getOperand(1);
9573 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9574 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9575 Op0.getNumOperands() != 2 ||
9576 Op1.getNumOperands() != 2)
9577 return SDValue();
9578 SDValue Concat0Op1 = Op0.getOperand(1);
9579 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00009580 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +00009581 return SDValue();
9582 // Skip the transformation if any of the types are illegal.
9583 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9584 EVT VT = N->getValueType(0);
9585 if (!TLI.isTypeLegal(VT) ||
9586 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9587 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9588 return SDValue();
9589
Andrew Trickef9de2a2013-05-25 02:42:55 +00009590 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009591 Op0.getOperand(0), Op1.getOperand(0));
9592 // Translate the shuffle mask.
9593 SmallVector<int, 16> NewMask;
9594 unsigned NumElts = VT.getVectorNumElements();
9595 unsigned HalfElts = NumElts/2;
9596 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9597 for (unsigned n = 0; n < NumElts; ++n) {
9598 int MaskElt = SVN->getMaskElt(n);
9599 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009600 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009601 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009602 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009603 NewElt = HalfElts + MaskElt - NumElts;
9604 NewMask.push_back(NewElt);
9605 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009606 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009607 DAG.getUNDEF(VT), NewMask.data());
9608}
9609
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009610/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9611/// NEON load/store intrinsics, and generic vector load/stores, to merge
9612/// base address updates.
9613/// For generic load/stores, the memory type is assumed to be a vector.
9614/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009615static SDValue CombineBaseUpdate(SDNode *N,
9616 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009617 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009618 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9619 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009620 const bool isStore = N->getOpcode() == ISD::STORE;
9621 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009622 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009623 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009624 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009625
9626 // Search for a use of the address operand that is an increment.
9627 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9628 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9629 SDNode *User = *UI;
9630 if (User->getOpcode() != ISD::ADD ||
9631 UI.getUse().getResNo() != Addr.getResNo())
9632 continue;
9633
9634 // Check that the add is independent of the load/store. Otherwise, folding
9635 // it would create a cycle.
9636 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9637 continue;
9638
9639 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009640 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009641 bool isLaneOp = false;
9642 unsigned NewOpc = 0;
9643 unsigned NumVecs = 0;
9644 if (isIntrinsic) {
9645 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9646 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009647 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009648 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9649 NumVecs = 1; break;
9650 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9651 NumVecs = 2; break;
9652 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9653 NumVecs = 3; break;
9654 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9655 NumVecs = 4; break;
9656 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9657 NumVecs = 2; isLaneOp = true; break;
9658 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9659 NumVecs = 3; isLaneOp = true; break;
9660 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9661 NumVecs = 4; isLaneOp = true; break;
9662 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009663 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009664 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009665 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009666 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009667 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009668 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009669 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009670 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009671 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009672 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009673 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009674 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009675 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009676 }
9677 } else {
9678 isLaneOp = true;
9679 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009680 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009681 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9682 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9683 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009684 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9685 NumVecs = 1; isLaneOp = false; break;
9686 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9687 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009688 }
9689 }
9690
9691 // Find the size of memory referenced by the load/store.
9692 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009693 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009694 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009695 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009696 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009697 } else {
9698 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9699 VecTy = N->getOperand(1).getValueType();
9700 }
9701
Bob Wilson06fce872011-02-07 17:43:21 +00009702 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9703 if (isLaneOp)
9704 NumBytes /= VecTy.getVectorNumElements();
9705
9706 // If the increment is a constant, it must match the memory ref size.
9707 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9708 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9709 uint64_t IncVal = CInc->getZExtValue();
9710 if (IncVal != NumBytes)
9711 continue;
9712 } else if (NumBytes >= 3 * 16) {
9713 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9714 // separate instructions that make it harder to use a non-constant update.
9715 continue;
9716 }
9717
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009718 // OK, we found an ADD we can fold into the base update.
9719 // Now, create a _UPD node, taking care of not breaking alignment.
9720
9721 EVT AlignedVecTy = VecTy;
9722 unsigned Alignment = MemN->getAlignment();
9723
9724 // If this is a less-than-standard-aligned load/store, change the type to
9725 // match the standard alignment.
9726 // The alignment is overlooked when selecting _UPD variants; and it's
9727 // easier to introduce bitcasts here than fix that.
9728 // There are 3 ways to get to this base-update combine:
9729 // - intrinsics: they are assumed to be properly aligned (to the standard
9730 // alignment of the memory type), so we don't need to do anything.
9731 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9732 // intrinsics, so, likewise, there's nothing to do.
9733 // - generic load/store instructions: the alignment is specified as an
9734 // explicit operand, rather than implicitly as the standard alignment
9735 // of the memory type (like the intrisics). We need to change the
9736 // memory type to match the explicit alignment. That way, we don't
9737 // generate non-standard-aligned ARMISD::VLDx nodes.
9738 if (isa<LSBaseSDNode>(N)) {
9739 if (Alignment == 0)
9740 Alignment = 1;
9741 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9742 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9743 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9744 assert(!isLaneOp && "Unexpected generic load/store lane.");
9745 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9746 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9747 }
9748 // Don't set an explicit alignment on regular load/stores that we want
9749 // to transform to VLD/VST 1_UPD nodes.
9750 // This matches the behavior of regular load/stores, which only get an
9751 // explicit alignment if the MMO alignment is larger than the standard
9752 // alignment of the memory type.
9753 // Intrinsics, however, always get an explicit alignment, set to the
9754 // alignment of the MMO.
9755 Alignment = 1;
9756 }
9757
Bob Wilson06fce872011-02-07 17:43:21 +00009758 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009759 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009760 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009761 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009762 unsigned n;
9763 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009764 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009765 Tys[n++] = MVT::i32;
9766 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009767 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009768
9769 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009770 SmallVector<SDValue, 8> Ops;
9771 Ops.push_back(N->getOperand(0)); // incoming chain
9772 Ops.push_back(N->getOperand(AddrOpIdx));
9773 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009774
9775 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9776 // Try to match the intrinsic's signature
9777 Ops.push_back(StN->getValue());
9778 } else {
9779 // Loads (and of course intrinsics) match the intrinsics' signature,
9780 // so just add all but the alignment operand.
9781 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9782 Ops.push_back(N->getOperand(i));
9783 }
9784
9785 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009786 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009787
9788 // If this is a non-standard-aligned STORE, the penultimate operand is the
9789 // stored value. Bitcast it to the aligned type.
9790 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9791 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009792 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009793 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009794
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009795 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009796 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009797 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009798
9799 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009800 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009801 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009802 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009803
9804 // If this is an non-standard-aligned LOAD, the first result is the loaded
9805 // value. Bitcast it to the expected result type.
9806 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9807 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009808 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009809 }
9810
Bob Wilson06fce872011-02-07 17:43:21 +00009811 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9812 DCI.CombineTo(N, NewResults);
9813 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9814
9815 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009816 }
Bob Wilson06fce872011-02-07 17:43:21 +00009817 return SDValue();
9818}
9819
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009820static SDValue PerformVLDCombine(SDNode *N,
9821 TargetLowering::DAGCombinerInfo &DCI) {
9822 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9823 return SDValue();
9824
9825 return CombineBaseUpdate(N, DCI);
9826}
9827
Bob Wilson2d790df2010-11-28 06:51:26 +00009828/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9829/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9830/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9831/// return true.
9832static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9833 SelectionDAG &DAG = DCI.DAG;
9834 EVT VT = N->getValueType(0);
9835 // vldN-dup instructions only support 64-bit vectors for N > 1.
9836 if (!VT.is64BitVector())
9837 return false;
9838
9839 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9840 SDNode *VLD = N->getOperand(0).getNode();
9841 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9842 return false;
9843 unsigned NumVecs = 0;
9844 unsigned NewOpc = 0;
9845 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9846 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9847 NumVecs = 2;
9848 NewOpc = ARMISD::VLD2DUP;
9849 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9850 NumVecs = 3;
9851 NewOpc = ARMISD::VLD3DUP;
9852 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9853 NumVecs = 4;
9854 NewOpc = ARMISD::VLD4DUP;
9855 } else {
9856 return false;
9857 }
9858
9859 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9860 // numbers match the load.
9861 unsigned VLDLaneNo =
9862 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9863 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9864 UI != UE; ++UI) {
9865 // Ignore uses of the chain result.
9866 if (UI.getUse().getResNo() == NumVecs)
9867 continue;
9868 SDNode *User = *UI;
9869 if (User->getOpcode() != ARMISD::VDUPLANE ||
9870 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9871 return false;
9872 }
9873
9874 // Create the vldN-dup node.
9875 EVT Tys[5];
9876 unsigned n;
9877 for (n = 0; n < NumVecs; ++n)
9878 Tys[n] = VT;
9879 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009880 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009881 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9882 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009883 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009884 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009885 VLDMemInt->getMemOperand());
9886
9887 // Update the uses.
9888 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9889 UI != UE; ++UI) {
9890 unsigned ResNo = UI.getUse().getResNo();
9891 // Ignore uses of the chain result.
9892 if (ResNo == NumVecs)
9893 continue;
9894 SDNode *User = *UI;
9895 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9896 }
9897
9898 // Now the vldN-lane intrinsic is dead except for its chain result.
9899 // Update uses of the chain.
9900 std::vector<SDValue> VLDDupResults;
9901 for (unsigned n = 0; n < NumVecs; ++n)
9902 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9903 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9904 DCI.CombineTo(VLD, VLDDupResults);
9905
9906 return true;
9907}
9908
Bob Wilson103a0dc2010-07-14 01:22:12 +00009909/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9910/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009911static SDValue PerformVDUPLANECombine(SDNode *N,
9912 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009913 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009914
Bob Wilson2d790df2010-11-28 06:51:26 +00009915 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9916 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9917 if (CombineVLDDUP(N, DCI))
9918 return SDValue(N, 0);
9919
9920 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9921 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009922 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009923 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009924 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009925 return SDValue();
9926
9927 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9928 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9929 // The canonical VMOV for a zero vector uses a 32-bit element size.
9930 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9931 unsigned EltBits;
9932 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9933 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009934 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009935 if (EltSize > VT.getVectorElementType().getSizeInBits())
9936 return SDValue();
9937
Andrew Trickef9de2a2013-05-25 02:42:55 +00009938 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009939}
9940
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009941static SDValue PerformLOADCombine(SDNode *N,
9942 TargetLowering::DAGCombinerInfo &DCI) {
9943 EVT VT = N->getValueType(0);
9944
9945 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9946 if (ISD::isNormalLoad(N) && VT.isVector() &&
9947 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9948 return CombineBaseUpdate(N, DCI);
9949
9950 return SDValue();
9951}
9952
Ahmed Bougacha23167462014-12-09 21:26:53 +00009953/// PerformSTORECombine - Target-specific dag combine xforms for
9954/// ISD::STORE.
9955static SDValue PerformSTORECombine(SDNode *N,
9956 TargetLowering::DAGCombinerInfo &DCI) {
9957 StoreSDNode *St = cast<StoreSDNode>(N);
9958 if (St->isVolatile())
9959 return SDValue();
9960
9961 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9962 // pack all of the elements in one place. Next, store to memory in fewer
9963 // chunks.
9964 SDValue StVal = St->getValue();
9965 EVT VT = StVal.getValueType();
9966 if (St->isTruncatingStore() && VT.isVector()) {
9967 SelectionDAG &DAG = DCI.DAG;
9968 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9969 EVT StVT = St->getMemoryVT();
9970 unsigned NumElems = VT.getVectorNumElements();
9971 assert(StVT != VT && "Cannot truncate to the same type");
9972 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9973 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9974
9975 // From, To sizes and ElemCount must be pow of two
9976 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9977
9978 // We are going to use the original vector elt for storing.
9979 // Accumulated smaller vector elements must be a multiple of the store size.
9980 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9981
9982 unsigned SizeRatio = FromEltSz / ToEltSz;
9983 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9984
9985 // Create a type on which we perform the shuffle.
9986 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9987 NumElems*SizeRatio);
9988 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9989
9990 SDLoc DL(St);
9991 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9992 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9993 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009994 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9995 ? (i + 1) * SizeRatio - 1
9996 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009997
9998 // Can't shuffle using an illegal type.
9999 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10000
10001 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10002 DAG.getUNDEF(WideVec.getValueType()),
10003 ShuffleVec.data());
10004 // At this point all of the data is stored at the bottom of the
10005 // register. We now need to save it to mem.
10006
10007 // Find the largest store unit
10008 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010009 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010010 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10011 StoreType = Tp;
10012 }
10013 // Didn't find a legal store type.
10014 if (!TLI.isTypeLegal(StoreType))
10015 return SDValue();
10016
10017 // Bitcast the original vector into a vector of store-size units
10018 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10019 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10020 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10021 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10022 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010023 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10024 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010025 SDValue BasePtr = St->getBasePtr();
10026
10027 // Perform one or more big stores into memory.
10028 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10029 for (unsigned I = 0; I < E; I++) {
10030 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10031 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010032 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010033 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
10034 St->getPointerInfo(), St->isVolatile(),
10035 St->isNonTemporal(), St->getAlignment());
10036 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10037 Increment);
10038 Chains.push_back(Ch);
10039 }
10040 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10041 }
10042
10043 if (!ISD::isNormalStore(St))
10044 return SDValue();
10045
10046 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10047 // ARM stores of arguments in the same cache line.
10048 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10049 StVal.getNode()->hasOneUse()) {
10050 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010051 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010052 SDLoc DL(St);
10053 SDValue BasePtr = St->getBasePtr();
10054 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
10055 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
10056 BasePtr, St->getPointerInfo(), St->isVolatile(),
10057 St->isNonTemporal(), St->getAlignment());
10058
10059 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010060 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010061 return DAG.getStore(NewST1.getValue(0), DL,
10062 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
10063 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
10064 St->isNonTemporal(),
10065 std::min(4U, St->getAlignment() / 2));
10066 }
10067
10068 if (StVal.getValueType() == MVT::i64 &&
10069 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10070
10071 // Bitcast an i64 store extracted from a vector to f64.
10072 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10073 SelectionDAG &DAG = DCI.DAG;
10074 SDLoc dl(StVal);
10075 SDValue IntVec = StVal.getOperand(0);
10076 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10077 IntVec.getValueType().getVectorNumElements());
10078 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10079 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10080 Vec, StVal.getOperand(1));
10081 dl = SDLoc(N);
10082 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10083 // Make the DAGCombiner fold the bitcasts.
10084 DCI.AddToWorklist(Vec.getNode());
10085 DCI.AddToWorklist(ExtElt.getNode());
10086 DCI.AddToWorklist(V.getNode());
10087 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
10088 St->getPointerInfo(), St->isVolatile(),
10089 St->isNonTemporal(), St->getAlignment(),
10090 St->getAAInfo());
10091 }
10092
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010093 // If this is a legal vector store, try to combine it into a VST1_UPD.
10094 if (ISD::isNormalStore(N) && VT.isVector() &&
10095 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10096 return CombineBaseUpdate(N, DCI);
10097
Ahmed Bougacha23167462014-12-09 21:26:53 +000010098 return SDValue();
10099}
10100
Chad Rosierfa8d8932011-06-24 19:23:04 +000010101/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10102/// can replace combinations of VMUL and VCVT (floating-point to integer)
10103/// when the VMUL has a constant operand that is a power of 2.
10104///
10105/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10106/// vmul.f32 d16, d17, d16
10107/// vcvt.s32.f32 d16, d16
10108/// becomes:
10109/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010110static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010111 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010112 if (!Subtarget->hasNEON())
10113 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010114
Chad Rosiera087fd22015-10-06 20:23:42 +000010115 SDValue Op = N->getOperand(0);
10116 if (!Op.getValueType().isVector() || Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010117 return SDValue();
10118
Chad Rosierfa8d8932011-06-24 19:23:04 +000010119 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010120 if (!isa<BuildVectorSDNode>(ConstVec))
10121 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010122
Tim Northover7cbc2152013-06-28 15:29:25 +000010123 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010124 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010125 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010126 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010127 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010128 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010129 // These instructions only exist converting from f32 to i32. We can handle
10130 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010131 // be lossy. We also can't handle more then 4 lanes, since these intructions
10132 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010133 return SDValue();
10134 }
10135
Chad Rosier169865f2015-10-07 17:28:58 +000010136 BitVector UndefElements;
10137 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10138 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10139 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010140 return SDValue();
10141
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010142 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010143 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010144 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10145 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010146 SDValue FixConv = DAG.getNode(
10147 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10148 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010149 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010150
Chad Rosier9df4aff2015-10-06 20:45:45 +000010151 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010152 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010153
10154 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010155}
10156
10157/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10158/// can replace combinations of VCVT (integer to floating-point) and VDIV
10159/// when the VDIV has a constant operand that is a power of 2.
10160///
10161/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10162/// vcvt.f32.s32 d16, d16
10163/// vdiv.f32 d16, d17, d16
10164/// becomes:
10165/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010166static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010167 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010168 if (!Subtarget->hasNEON())
10169 return SDValue();
10170
Chad Rosierfa8d8932011-06-24 19:23:04 +000010171 SDValue Op = N->getOperand(0);
10172 unsigned OpOpcode = Op.getNode()->getOpcode();
Chad Rosiera087fd22015-10-06 20:23:42 +000010173 if (!N->getValueType(0).isVector() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010174 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10175 return SDValue();
10176
Chad Rosierfa8d8932011-06-24 19:23:04 +000010177 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010178 if (!isa<BuildVectorSDNode>(ConstVec))
10179 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010180
Tim Northover7cbc2152013-06-28 15:29:25 +000010181 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010182 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010183 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010184 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010185 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10186 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010187 // These instructions only exist converting from i32 to f32. We can handle
10188 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010189 // be lossy. We also can't handle more then 4 lanes, since these intructions
10190 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010191 return SDValue();
10192 }
10193
Chad Rosier169865f2015-10-07 17:28:58 +000010194 BitVector UndefElements;
10195 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10196 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10197 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010198 return SDValue();
10199
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010200 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010201 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010202 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010203 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010204 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010205 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010206 ConvInput);
10207
Eric Christopher1b8b94192011-06-29 21:10:36 +000010208 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010209 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010210 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010211 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010212 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010213 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010214}
10215
10216/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010217/// operand of a vector shift operation, where all the elements of the
10218/// build_vector must have the same constant integer value.
10219static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10220 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010221 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010222 Op = Op.getOperand(0);
10223 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10224 APInt SplatBits, SplatUndef;
10225 unsigned SplatBitSize;
10226 bool HasAnyUndefs;
10227 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10228 HasAnyUndefs, ElementBits) ||
10229 SplatBitSize > ElementBits)
10230 return false;
10231 Cnt = SplatBits.getSExtValue();
10232 return true;
10233}
10234
10235/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10236/// operand of a vector shift left operation. That value must be in the range:
10237/// 0 <= Value < ElementBits for a left shift; or
10238/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010239static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010240 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010241 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010242 if (! getVShiftImm(Op, ElementBits, Cnt))
10243 return false;
10244 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10245}
10246
10247/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10248/// operand of a vector shift right operation. For a shift opcode, the value
10249/// is positive, but for an intrinsic the value count must be negative. The
10250/// absolute value must be in the range:
10251/// 1 <= |Value| <= ElementBits for a right shift; or
10252/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010253static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010254 int64_t &Cnt) {
10255 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010256 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010257 if (! getVShiftImm(Op, ElementBits, Cnt))
10258 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010259 if (!isIntrinsic)
10260 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10261 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010262 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010263 return true;
10264 }
10265 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010266}
10267
10268/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10269static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10270 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10271 switch (IntNo) {
10272 default:
10273 // Don't do anything for most intrinsics.
10274 break;
10275
10276 // Vector shifts: check for immediate versions and lower them.
10277 // Note: This is done during DAG combining instead of DAG legalizing because
10278 // the build_vectors for 64-bit vector element shift counts are generally
10279 // not legal, and it is hard to see their values after they get legalized to
10280 // loads from a constant pool.
10281 case Intrinsic::arm_neon_vshifts:
10282 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010283 case Intrinsic::arm_neon_vrshifts:
10284 case Intrinsic::arm_neon_vrshiftu:
10285 case Intrinsic::arm_neon_vrshiftn:
10286 case Intrinsic::arm_neon_vqshifts:
10287 case Intrinsic::arm_neon_vqshiftu:
10288 case Intrinsic::arm_neon_vqshiftsu:
10289 case Intrinsic::arm_neon_vqshiftns:
10290 case Intrinsic::arm_neon_vqshiftnu:
10291 case Intrinsic::arm_neon_vqshiftnsu:
10292 case Intrinsic::arm_neon_vqrshiftns:
10293 case Intrinsic::arm_neon_vqrshiftnu:
10294 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010295 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010296 int64_t Cnt;
10297 unsigned VShiftOpc = 0;
10298
10299 switch (IntNo) {
10300 case Intrinsic::arm_neon_vshifts:
10301 case Intrinsic::arm_neon_vshiftu:
10302 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10303 VShiftOpc = ARMISD::VSHL;
10304 break;
10305 }
10306 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10307 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10308 ARMISD::VSHRs : ARMISD::VSHRu);
10309 break;
10310 }
10311 return SDValue();
10312
Bob Wilson2e076c42009-06-22 23:27:02 +000010313 case Intrinsic::arm_neon_vrshifts:
10314 case Intrinsic::arm_neon_vrshiftu:
10315 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10316 break;
10317 return SDValue();
10318
10319 case Intrinsic::arm_neon_vqshifts:
10320 case Intrinsic::arm_neon_vqshiftu:
10321 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10322 break;
10323 return SDValue();
10324
10325 case Intrinsic::arm_neon_vqshiftsu:
10326 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10327 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010328 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010329
Bob Wilson2e076c42009-06-22 23:27:02 +000010330 case Intrinsic::arm_neon_vrshiftn:
10331 case Intrinsic::arm_neon_vqshiftns:
10332 case Intrinsic::arm_neon_vqshiftnu:
10333 case Intrinsic::arm_neon_vqshiftnsu:
10334 case Intrinsic::arm_neon_vqrshiftns:
10335 case Intrinsic::arm_neon_vqrshiftnu:
10336 case Intrinsic::arm_neon_vqrshiftnsu:
10337 // Narrowing shifts require an immediate right shift.
10338 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10339 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010340 llvm_unreachable("invalid shift count for narrowing vector shift "
10341 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010342
10343 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010344 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010345 }
10346
10347 switch (IntNo) {
10348 case Intrinsic::arm_neon_vshifts:
10349 case Intrinsic::arm_neon_vshiftu:
10350 // Opcode already set above.
10351 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010352 case Intrinsic::arm_neon_vrshifts:
10353 VShiftOpc = ARMISD::VRSHRs; break;
10354 case Intrinsic::arm_neon_vrshiftu:
10355 VShiftOpc = ARMISD::VRSHRu; break;
10356 case Intrinsic::arm_neon_vrshiftn:
10357 VShiftOpc = ARMISD::VRSHRN; break;
10358 case Intrinsic::arm_neon_vqshifts:
10359 VShiftOpc = ARMISD::VQSHLs; break;
10360 case Intrinsic::arm_neon_vqshiftu:
10361 VShiftOpc = ARMISD::VQSHLu; break;
10362 case Intrinsic::arm_neon_vqshiftsu:
10363 VShiftOpc = ARMISD::VQSHLsu; break;
10364 case Intrinsic::arm_neon_vqshiftns:
10365 VShiftOpc = ARMISD::VQSHRNs; break;
10366 case Intrinsic::arm_neon_vqshiftnu:
10367 VShiftOpc = ARMISD::VQSHRNu; break;
10368 case Intrinsic::arm_neon_vqshiftnsu:
10369 VShiftOpc = ARMISD::VQSHRNsu; break;
10370 case Intrinsic::arm_neon_vqrshiftns:
10371 VShiftOpc = ARMISD::VQRSHRNs; break;
10372 case Intrinsic::arm_neon_vqrshiftnu:
10373 VShiftOpc = ARMISD::VQRSHRNu; break;
10374 case Intrinsic::arm_neon_vqrshiftnsu:
10375 VShiftOpc = ARMISD::VQRSHRNsu; break;
10376 }
10377
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010378 SDLoc dl(N);
10379 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10380 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010381 }
10382
10383 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010384 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010385 int64_t Cnt;
10386 unsigned VShiftOpc = 0;
10387
10388 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10389 VShiftOpc = ARMISD::VSLI;
10390 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10391 VShiftOpc = ARMISD::VSRI;
10392 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010393 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010394 }
10395
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010396 SDLoc dl(N);
10397 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010398 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010399 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010400 }
10401
10402 case Intrinsic::arm_neon_vqrshifts:
10403 case Intrinsic::arm_neon_vqrshiftu:
10404 // No immediate versions of these to check for.
10405 break;
10406 }
10407
10408 return SDValue();
10409}
10410
10411/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10412/// lowers them. As with the vector shift intrinsics, this is done during DAG
10413/// combining instead of DAG legalizing because the build_vectors for 64-bit
10414/// vector element shift counts are generally not legal, and it is hard to see
10415/// their values after they get legalized to loads from a constant pool.
10416static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10417 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010418 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010419 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10420 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10421 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10422 SDValue N1 = N->getOperand(1);
10423 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10424 SDValue N0 = N->getOperand(0);
10425 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10426 DAG.MaskedValueIsZero(N0.getOperand(0),
10427 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010428 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010429 }
10430 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010431
10432 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010433 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10434 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010435 return SDValue();
10436
10437 assert(ST->hasNEON() && "unexpected vector shift");
10438 int64_t Cnt;
10439
10440 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010441 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010442
10443 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010444 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10445 SDLoc dl(N);
10446 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10447 DAG.getConstant(Cnt, dl, MVT::i32));
10448 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010449 break;
10450
10451 case ISD::SRA:
10452 case ISD::SRL:
10453 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10454 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10455 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010456 SDLoc dl(N);
10457 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10458 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010459 }
10460 }
10461 return SDValue();
10462}
10463
10464/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10465/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10466static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10467 const ARMSubtarget *ST) {
10468 SDValue N0 = N->getOperand(0);
10469
10470 // Check for sign- and zero-extensions of vector extract operations of 8-
10471 // and 16-bit vector elements. NEON supports these directly. They are
10472 // handled during DAG combining because type legalization will promote them
10473 // to 32-bit types and it is messy to recognize the operations after that.
10474 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10475 SDValue Vec = N0.getOperand(0);
10476 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010477 EVT VT = N->getValueType(0);
10478 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010479 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10480
Owen Anderson9f944592009-08-11 20:47:22 +000010481 if (VT == MVT::i32 &&
10482 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010483 TLI.isTypeLegal(Vec.getValueType()) &&
10484 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010485
10486 unsigned Opc = 0;
10487 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010488 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010489 case ISD::SIGN_EXTEND:
10490 Opc = ARMISD::VGETLANEs;
10491 break;
10492 case ISD::ZERO_EXTEND:
10493 case ISD::ANY_EXTEND:
10494 Opc = ARMISD::VGETLANEu;
10495 break;
10496 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010497 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010498 }
10499 }
10500
10501 return SDValue();
10502}
10503
James Molloy9d55f192015-11-10 14:22:05 +000010504static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10505 APInt &KnownOne) {
10506 if (Op.getOpcode() == ARMISD::BFI) {
10507 // Conservatively, we can recurse down the first operand
10508 // and just mask out all affected bits.
10509 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10510
10511 // The operand to BFI is already a mask suitable for removing the bits it
10512 // sets.
10513 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
10514 APInt Mask = CI->getAPIntValue();
10515 KnownZero &= Mask;
10516 KnownOne &= Mask;
10517 return;
10518 }
10519 if (Op.getOpcode() == ARMISD::CMOV) {
10520 APInt KZ2(KnownZero.getBitWidth(), 0);
10521 APInt KO2(KnownOne.getBitWidth(), 0);
10522 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10523 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10524
10525 KnownZero &= KZ2;
10526 KnownOne &= KO2;
10527 return;
10528 }
10529 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10530}
10531
10532SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10533 // If we have a CMOV, OR and AND combination such as:
10534 // if (x & CN)
10535 // y |= CM;
10536 //
10537 // And:
10538 // * CN is a single bit;
10539 // * All bits covered by CM are known zero in y
10540 //
10541 // Then we can convert this into a sequence of BFI instructions. This will
10542 // always be a win if CM is a single bit, will always be no worse than the
10543 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10544 // three bits (due to the extra IT instruction).
10545
10546 SDValue Op0 = CMOV->getOperand(0);
10547 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010548 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10549 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010550 SDValue CmpZ = CMOV->getOperand(4);
10551
James Molloy20180912015-11-16 10:49:25 +000010552 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010553 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010554 return SDValue();
10555
James Molloy9d55f192015-11-10 14:22:05 +000010556 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10557 SDValue And = CmpZ->getOperand(0);
10558 if (And->getOpcode() != ISD::AND)
10559 return SDValue();
10560 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10561 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10562 return SDValue();
10563 SDValue X = And->getOperand(0);
10564
James Molloy8e99e972015-11-12 13:49:17 +000010565 if (CC == ARMCC::EQ) {
10566 // We're performing an "equal to zero" compare. Swap the operands so we
10567 // canonicalize on a "not equal to zero" compare.
10568 std::swap(Op0, Op1);
10569 } else {
10570 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10571 }
Junmo Park1108ab02016-02-19 01:46:04 +000010572
James Molloy9d55f192015-11-10 14:22:05 +000010573 if (Op1->getOpcode() != ISD::OR)
10574 return SDValue();
10575
10576 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10577 if (!OrC)
10578 return SDValue();
10579 SDValue Y = Op1->getOperand(0);
10580
10581 if (Op0 != Y)
10582 return SDValue();
10583
10584 // Now, is it profitable to continue?
10585 APInt OrCI = OrC->getAPIntValue();
10586 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10587 if (OrCI.countPopulation() > Heuristic)
10588 return SDValue();
10589
10590 // Lastly, can we determine that the bits defined by OrCI
10591 // are zero in Y?
10592 APInt KnownZero, KnownOne;
10593 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10594 if ((OrCI & KnownZero) != OrCI)
10595 return SDValue();
10596
10597 // OK, we can do the combine.
10598 SDValue V = Y;
10599 SDLoc dl(X);
10600 EVT VT = X.getValueType();
10601 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000010602
James Molloy9d55f192015-11-10 14:22:05 +000010603 if (BitInX != 0) {
10604 // We must shift X first.
10605 X = DAG.getNode(ISD::SRL, dl, VT, X,
10606 DAG.getConstant(BitInX, dl, VT));
10607 }
10608
10609 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10610 BitInY < NumActiveBits; ++BitInY) {
10611 if (OrCI[BitInY] == 0)
10612 continue;
10613 APInt Mask(VT.getSizeInBits(), 0);
10614 Mask.setBit(BitInY);
10615 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10616 // Confusingly, the operand is an *inverted* mask.
10617 DAG.getConstant(~Mask, dl, VT));
10618 }
10619
10620 return V;
10621}
10622
Evan Chengf863e3f2011-07-13 00:42:17 +000010623/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10624SDValue
10625ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10626 SDValue Cmp = N->getOperand(4);
10627 if (Cmp.getOpcode() != ARMISD::CMPZ)
10628 // Only looking at EQ and NE cases.
10629 return SDValue();
10630
10631 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010632 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010633 SDValue LHS = Cmp.getOperand(0);
10634 SDValue RHS = Cmp.getOperand(1);
10635 SDValue FalseVal = N->getOperand(0);
10636 SDValue TrueVal = N->getOperand(1);
10637 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010638 ARMCC::CondCodes CC =
10639 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010640
James Molloy9d55f192015-11-10 14:22:05 +000010641 // BFI is only available on V6T2+.
10642 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10643 SDValue R = PerformCMOVToBFICombine(N, DAG);
10644 if (R)
10645 return R;
10646 }
10647
Evan Chengf863e3f2011-07-13 00:42:17 +000010648 // Simplify
10649 // mov r1, r0
10650 // cmp r1, x
10651 // mov r0, y
10652 // moveq r0, x
10653 // to
10654 // cmp r0, x
10655 // movne r0, y
10656 //
10657 // mov r1, r0
10658 // cmp r1, x
10659 // mov r0, x
10660 // movne r0, y
10661 // to
10662 // cmp r0, x
10663 // movne r0, y
10664 /// FIXME: Turn this into a target neutral optimization?
10665 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010666 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010667 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10668 N->getOperand(3), Cmp);
10669 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10670 SDValue ARMcc;
10671 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10672 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10673 N->getOperand(3), NewCmp);
10674 }
10675
10676 if (Res.getNode()) {
10677 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010678 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010679 // Capture demanded bits information that would be otherwise lost.
10680 if (KnownZero == 0xfffffffe)
10681 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10682 DAG.getValueType(MVT::i1));
10683 else if (KnownZero == 0xffffff00)
10684 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10685 DAG.getValueType(MVT::i8));
10686 else if (KnownZero == 0xffff0000)
10687 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10688 DAG.getValueType(MVT::i16));
10689 }
10690
10691 return Res;
10692}
10693
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010694SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010695 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010696 switch (N->getOpcode()) {
10697 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010698 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010699 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010700 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010701 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010702 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010703 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10704 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010705 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010706 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010707 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010708 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010709 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010710 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010711 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010712 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010713 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010714 case ISD::FP_TO_UINT:
10715 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10716 case ISD::FDIV:
10717 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010718 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010719 case ISD::SHL:
10720 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010721 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010722 case ISD::SIGN_EXTEND:
10723 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010724 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010725 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010726 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010727 case ARMISD::VLD2DUP:
10728 case ARMISD::VLD3DUP:
10729 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010730 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010731 case ARMISD::BUILD_VECTOR:
10732 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010733 case ISD::INTRINSIC_VOID:
10734 case ISD::INTRINSIC_W_CHAIN:
10735 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10736 case Intrinsic::arm_neon_vld1:
10737 case Intrinsic::arm_neon_vld2:
10738 case Intrinsic::arm_neon_vld3:
10739 case Intrinsic::arm_neon_vld4:
10740 case Intrinsic::arm_neon_vld2lane:
10741 case Intrinsic::arm_neon_vld3lane:
10742 case Intrinsic::arm_neon_vld4lane:
10743 case Intrinsic::arm_neon_vst1:
10744 case Intrinsic::arm_neon_vst2:
10745 case Intrinsic::arm_neon_vst3:
10746 case Intrinsic::arm_neon_vst4:
10747 case Intrinsic::arm_neon_vst2lane:
10748 case Intrinsic::arm_neon_vst3lane:
10749 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010750 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010751 default: break;
10752 }
10753 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010754 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010755 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010756}
10757
Evan Chengd42641c2011-02-02 01:06:55 +000010758bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10759 EVT VT) const {
10760 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10761}
10762
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010763bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10764 unsigned,
10765 unsigned,
10766 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010767 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010768 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010769
10770 switch (VT.getSimpleVT().SimpleTy) {
10771 default:
10772 return false;
10773 case MVT::i8:
10774 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010775 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010776 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010777 if (AllowsUnaligned) {
10778 if (Fast)
10779 *Fast = Subtarget->hasV7Ops();
10780 return true;
10781 }
10782 return false;
10783 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010784 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010785 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010786 // For any little-endian targets with neon, we can support unaligned ld/st
10787 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010788 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010789 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010790 if (Fast)
10791 *Fast = true;
10792 return true;
10793 }
10794 return false;
10795 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010796 }
10797}
10798
Lang Hames9929c422011-11-02 22:52:45 +000010799static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10800 unsigned AlignCheck) {
10801 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10802 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10803}
10804
10805EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10806 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010807 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010808 bool MemcpyStrSrc,
10809 MachineFunction &MF) const {
10810 const Function *F = MF.getFunction();
10811
10812 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010813 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10814 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010815 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010816 if (Size >= 16 &&
10817 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010818 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010819 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010820 } else if (Size >= 8 &&
10821 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010822 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10823 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010824 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010825 }
10826 }
10827
Lang Hamesb85fcd02011-11-08 18:56:23 +000010828 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010829 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010830 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010831 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010832 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010833
Lang Hames9929c422011-11-02 22:52:45 +000010834 // Let the target-independent logic figure it out.
10835 return MVT::Other;
10836}
10837
Evan Cheng9ec512d2012-12-06 19:13:27 +000010838bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10839 if (Val.getOpcode() != ISD::LOAD)
10840 return false;
10841
10842 EVT VT1 = Val.getValueType();
10843 if (!VT1.isSimple() || !VT1.isInteger() ||
10844 !VT2.isSimple() || !VT2.isInteger())
10845 return false;
10846
10847 switch (VT1.getSimpleVT().SimpleTy) {
10848 default: break;
10849 case MVT::i1:
10850 case MVT::i8:
10851 case MVT::i16:
10852 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10853 return true;
10854 }
10855
10856 return false;
10857}
10858
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010859bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10860 EVT VT = ExtVal.getValueType();
10861
10862 if (!isTypeLegal(VT))
10863 return false;
10864
10865 // Don't create a loadext if we can fold the extension into a wide/long
10866 // instruction.
10867 // If there's more than one user instruction, the loadext is desirable no
10868 // matter what. There can be two uses by the same instruction.
10869 if (ExtVal->use_empty() ||
10870 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10871 return true;
10872
10873 SDNode *U = *ExtVal->use_begin();
10874 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10875 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10876 return false;
10877
10878 return true;
10879}
10880
Tim Northovercc2e9032013-08-06 13:58:03 +000010881bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10882 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10883 return false;
10884
10885 if (!isTypeLegal(EVT::getEVT(Ty1)))
10886 return false;
10887
10888 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10889
10890 // Assuming the caller doesn't have a zeroext or signext return parameter,
10891 // truncation all the way down to i1 is valid.
10892 return true;
10893}
10894
10895
Evan Chengdc49a8d2009-08-14 20:09:37 +000010896static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10897 if (V < 0)
10898 return false;
10899
10900 unsigned Scale = 1;
10901 switch (VT.getSimpleVT().SimpleTy) {
10902 default: return false;
10903 case MVT::i1:
10904 case MVT::i8:
10905 // Scale == 1;
10906 break;
10907 case MVT::i16:
10908 // Scale == 2;
10909 Scale = 2;
10910 break;
10911 case MVT::i32:
10912 // Scale == 4;
10913 Scale = 4;
10914 break;
10915 }
10916
10917 if ((V & (Scale - 1)) != 0)
10918 return false;
10919 V /= Scale;
10920 return V == (V & ((1LL << 5) - 1));
10921}
10922
10923static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10924 const ARMSubtarget *Subtarget) {
10925 bool isNeg = false;
10926 if (V < 0) {
10927 isNeg = true;
10928 V = - V;
10929 }
10930
10931 switch (VT.getSimpleVT().SimpleTy) {
10932 default: return false;
10933 case MVT::i1:
10934 case MVT::i8:
10935 case MVT::i16:
10936 case MVT::i32:
10937 // + imm12 or - imm8
10938 if (isNeg)
10939 return V == (V & ((1LL << 8) - 1));
10940 return V == (V & ((1LL << 12) - 1));
10941 case MVT::f32:
10942 case MVT::f64:
10943 // Same as ARM mode. FIXME: NEON?
10944 if (!Subtarget->hasVFP2())
10945 return false;
10946 if ((V & 3) != 0)
10947 return false;
10948 V >>= 2;
10949 return V == (V & ((1LL << 8) - 1));
10950 }
10951}
10952
Evan Cheng2150b922007-03-12 23:30:29 +000010953/// isLegalAddressImmediate - Return true if the integer value can be used
10954/// as the offset of the target addressing mode for load / store of the
10955/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010956static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010957 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010958 if (V == 0)
10959 return true;
10960
Evan Chengce5dfb62009-03-09 19:15:00 +000010961 if (!VT.isSimple())
10962 return false;
10963
Evan Chengdc49a8d2009-08-14 20:09:37 +000010964 if (Subtarget->isThumb1Only())
10965 return isLegalT1AddressImmediate(V, VT);
10966 else if (Subtarget->isThumb2())
10967 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010968
Evan Chengdc49a8d2009-08-14 20:09:37 +000010969 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010970 if (V < 0)
10971 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010972 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010973 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010974 case MVT::i1:
10975 case MVT::i8:
10976 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010977 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010978 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010979 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010980 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010981 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010982 case MVT::f32:
10983 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010984 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010985 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010986 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010987 return false;
10988 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010989 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010990 }
Evan Cheng10043e22007-01-19 07:51:42 +000010991}
10992
Evan Chengdc49a8d2009-08-14 20:09:37 +000010993bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10994 EVT VT) const {
10995 int Scale = AM.Scale;
10996 if (Scale < 0)
10997 return false;
10998
10999 switch (VT.getSimpleVT().SimpleTy) {
11000 default: return false;
11001 case MVT::i1:
11002 case MVT::i8:
11003 case MVT::i16:
11004 case MVT::i32:
11005 if (Scale == 1)
11006 return true;
11007 // r + r << imm
11008 Scale = Scale & ~1;
11009 return Scale == 2 || Scale == 4 || Scale == 8;
11010 case MVT::i64:
11011 // r + r
11012 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11013 return true;
11014 return false;
11015 case MVT::isVoid:
11016 // Note, we allow "void" uses (basically, uses that aren't loads or
11017 // stores), because arm allows folding a scale into many arithmetic
11018 // operations. This should be made more precise and revisited later.
11019
11020 // Allow r << imm, but the imm has to be a multiple of two.
11021 if (Scale & 1) return false;
11022 return isPowerOf2_32(Scale);
11023 }
11024}
11025
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011026/// isLegalAddressingMode - Return true if the addressing mode represented
11027/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011028bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11029 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011030 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011031 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011032 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011033 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011034
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011035 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011036 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011037 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011038
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011039 switch (AM.Scale) {
11040 case 0: // no scale reg, must be "r+i" or "r", or "i".
11041 break;
11042 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011043 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011044 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000011045 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011046 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011047 // ARM doesn't support any R+R*scale+imm addr modes.
11048 if (AM.BaseOffs)
11049 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011050
Bob Wilson866c1742009-04-08 17:55:28 +000011051 if (!VT.isSimple())
11052 return false;
11053
Evan Chengdc49a8d2009-08-14 20:09:37 +000011054 if (Subtarget->isThumb2())
11055 return isLegalT2ScaledAddressingMode(AM, VT);
11056
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011057 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011058 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011059 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011060 case MVT::i1:
11061 case MVT::i8:
11062 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011063 if (Scale < 0) Scale = -Scale;
11064 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011065 return true;
11066 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011067 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011068 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011069 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011070 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011071 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011072 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011073 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011074
Owen Anderson9f944592009-08-11 20:47:22 +000011075 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011076 // Note, we allow "void" uses (basically, uses that aren't loads or
11077 // stores), because arm allows folding a scale into many arithmetic
11078 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011079
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011080 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011081 if (Scale & 1) return false;
11082 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011083 }
Evan Cheng2150b922007-03-12 23:30:29 +000011084 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011085 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011086}
11087
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011088/// isLegalICmpImmediate - Return true if the specified immediate is legal
11089/// icmp immediate, that is the target has icmp instructions which can compare
11090/// a register against the immediate without having to materialize the
11091/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011092bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011093 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011094 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011095 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011096 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011097 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011098 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011099 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011100}
11101
Andrew Tricka22cdb72012-07-18 18:34:27 +000011102/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11103/// *or sub* immediate, that is the target has add or sub instructions which can
11104/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011105/// immediate into a register.
11106bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011107 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011108 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011109 if (!Subtarget->isThumb())
11110 return ARM_AM::getSOImmVal(AbsImm) != -1;
11111 if (Subtarget->isThumb2())
11112 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11113 // Thumb1 only has 8-bit unsigned immediate.
11114 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011115}
11116
Owen Anderson53aa7a92009-08-10 22:56:29 +000011117static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011118 bool isSEXTLoad, SDValue &Base,
11119 SDValue &Offset, bool &isInc,
11120 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011121 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11122 return false;
11123
Owen Anderson9f944592009-08-11 20:47:22 +000011124 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011125 // AddressingMode 3
11126 Base = Ptr->getOperand(0);
11127 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011128 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011129 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011130 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011131 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011132 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011133 return true;
11134 }
11135 }
11136 isInc = (Ptr->getOpcode() == ISD::ADD);
11137 Offset = Ptr->getOperand(1);
11138 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011139 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011140 // AddressingMode 2
11141 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011142 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011143 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011144 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011145 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011146 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011147 Base = Ptr->getOperand(0);
11148 return true;
11149 }
11150 }
11151
11152 if (Ptr->getOpcode() == ISD::ADD) {
11153 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011154 ARM_AM::ShiftOpc ShOpcVal=
11155 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011156 if (ShOpcVal != ARM_AM::no_shift) {
11157 Base = Ptr->getOperand(1);
11158 Offset = Ptr->getOperand(0);
11159 } else {
11160 Base = Ptr->getOperand(0);
11161 Offset = Ptr->getOperand(1);
11162 }
11163 return true;
11164 }
11165
11166 isInc = (Ptr->getOpcode() == ISD::ADD);
11167 Base = Ptr->getOperand(0);
11168 Offset = Ptr->getOperand(1);
11169 return true;
11170 }
11171
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011172 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011173 return false;
11174}
11175
Owen Anderson53aa7a92009-08-10 22:56:29 +000011176static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011177 bool isSEXTLoad, SDValue &Base,
11178 SDValue &Offset, bool &isInc,
11179 SelectionDAG &DAG) {
11180 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11181 return false;
11182
11183 Base = Ptr->getOperand(0);
11184 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11185 int RHSC = (int)RHS->getZExtValue();
11186 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11187 assert(Ptr->getOpcode() == ISD::ADD);
11188 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011189 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011190 return true;
11191 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11192 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011193 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011194 return true;
11195 }
11196 }
11197
11198 return false;
11199}
11200
Evan Cheng10043e22007-01-19 07:51:42 +000011201/// getPreIndexedAddressParts - returns true by value, base pointer and
11202/// offset pointer and addressing mode by reference if the node's address
11203/// can be legally represented as pre-indexed load / store address.
11204bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011205ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11206 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011207 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011208 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011209 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011210 return false;
11211
Owen Anderson53aa7a92009-08-10 22:56:29 +000011212 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011213 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011214 bool isSEXTLoad = false;
11215 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11216 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011217 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011218 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11219 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11220 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011221 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011222 } else
11223 return false;
11224
11225 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011226 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011227 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011228 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11229 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011230 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011231 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011232 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011233 if (!isLegal)
11234 return false;
11235
11236 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11237 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011238}
11239
11240/// getPostIndexedAddressParts - returns true by value, base pointer and
11241/// offset pointer and addressing mode by reference if this node can be
11242/// combined with a load / store to form a post-indexed load / store.
11243bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011244 SDValue &Base,
11245 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011246 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011247 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011248 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011249 return false;
11250
Owen Anderson53aa7a92009-08-10 22:56:29 +000011251 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011252 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011253 bool isSEXTLoad = false;
11254 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011255 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011256 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011257 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11258 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011259 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011260 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011261 } else
11262 return false;
11263
11264 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011265 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011266 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011267 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011268 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011269 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011270 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11271 isInc, DAG);
11272 if (!isLegal)
11273 return false;
11274
Evan Chengf19384d2010-05-18 21:31:17 +000011275 if (Ptr != Base) {
11276 // Swap base ptr and offset to catch more post-index load / store when
11277 // it's legal. In Thumb2 mode, offset must be an immediate.
11278 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11279 !Subtarget->isThumb2())
11280 std::swap(Base, Offset);
11281
11282 // Post-indexed load / store update the base pointer.
11283 if (Ptr != Base)
11284 return false;
11285 }
11286
Evan Cheng84c6cda2009-07-02 07:28:31 +000011287 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11288 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011289}
11290
Jay Foada0653a32014-05-14 21:14:37 +000011291void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11292 APInt &KnownZero,
11293 APInt &KnownOne,
11294 const SelectionDAG &DAG,
11295 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011296 unsigned BitWidth = KnownOne.getBitWidth();
11297 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011298 switch (Op.getOpcode()) {
11299 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011300 case ARMISD::ADDC:
11301 case ARMISD::ADDE:
11302 case ARMISD::SUBC:
11303 case ARMISD::SUBE:
11304 // These nodes' second result is a boolean
11305 if (Op.getResNo() == 0)
11306 break;
11307 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11308 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011309 case ARMISD::CMOV: {
11310 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011311 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011312 if (KnownZero == 0 && KnownOne == 0) return;
11313
Dan Gohmanf990faf2008-02-13 00:35:47 +000011314 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011315 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011316 KnownZero &= KnownZeroRHS;
11317 KnownOne &= KnownOneRHS;
11318 return;
11319 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011320 case ISD::INTRINSIC_W_CHAIN: {
11321 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11322 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11323 switch (IntID) {
11324 default: return;
11325 case Intrinsic::arm_ldaex:
11326 case Intrinsic::arm_ldrex: {
11327 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11328 unsigned MemBits = VT.getScalarType().getSizeInBits();
11329 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11330 return;
11331 }
11332 }
11333 }
Evan Cheng10043e22007-01-19 07:51:42 +000011334 }
11335}
11336
11337//===----------------------------------------------------------------------===//
11338// ARM Inline Assembly Support
11339//===----------------------------------------------------------------------===//
11340
Evan Cheng078b0b02011-01-08 01:24:27 +000011341bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11342 // Looking for "rev" which is V6+.
11343 if (!Subtarget->hasV6Ops())
11344 return false;
11345
11346 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11347 std::string AsmStr = IA->getAsmString();
11348 SmallVector<StringRef, 4> AsmPieces;
11349 SplitString(AsmStr, AsmPieces, ";\n");
11350
11351 switch (AsmPieces.size()) {
11352 default: return false;
11353 case 1:
11354 AsmStr = AsmPieces[0];
11355 AsmPieces.clear();
11356 SplitString(AsmStr, AsmPieces, " \t,");
11357
11358 // rev $0, $1
11359 if (AsmPieces.size() == 3 &&
11360 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11361 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011362 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011363 if (Ty && Ty->getBitWidth() == 32)
11364 return IntrinsicLowering::LowerToByteSwap(CI);
11365 }
11366 break;
11367 }
11368
11369 return false;
11370}
11371
Evan Cheng10043e22007-01-19 07:51:42 +000011372/// getConstraintType - Given a constraint letter, return the type of
11373/// constraint it is for this target.
11374ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011375ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011376 if (Constraint.size() == 1) {
11377 switch (Constraint[0]) {
11378 default: break;
11379 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011380 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011381 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011382 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011383 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011384 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011385 // An address with a single base register. Due to the way we
11386 // currently handle addresses it is the same as an 'r' memory constraint.
11387 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011388 }
Eric Christophere256cd02011-06-21 22:10:57 +000011389 } else if (Constraint.size() == 2) {
11390 switch (Constraint[0]) {
11391 default: break;
11392 // All 'U+' constraints are addresses.
11393 case 'U': return C_Memory;
11394 }
Evan Cheng10043e22007-01-19 07:51:42 +000011395 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011396 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011397}
11398
John Thompsone8360b72010-10-29 17:29:13 +000011399/// Examine constraint type and operand type and determine a weight value.
11400/// This object must already have been set up with the operand type
11401/// and the current alternative constraint selected.
11402TargetLowering::ConstraintWeight
11403ARMTargetLowering::getSingleConstraintMatchWeight(
11404 AsmOperandInfo &info, const char *constraint) const {
11405 ConstraintWeight weight = CW_Invalid;
11406 Value *CallOperandVal = info.CallOperandVal;
11407 // If we don't have a value, we can't do a match,
11408 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011409 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011410 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011411 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011412 // Look at the constraint type.
11413 switch (*constraint) {
11414 default:
11415 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11416 break;
11417 case 'l':
11418 if (type->isIntegerTy()) {
11419 if (Subtarget->isThumb())
11420 weight = CW_SpecificReg;
11421 else
11422 weight = CW_Register;
11423 }
11424 break;
11425 case 'w':
11426 if (type->isFloatingPointTy())
11427 weight = CW_Register;
11428 break;
11429 }
11430 return weight;
11431}
11432
Eric Christophercf2007c2011-06-30 23:50:52 +000011433typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011434RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11435 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011436 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011437 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011438 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011439 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011440 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011441 return RCPair(0U, &ARM::tGPRRegClass);
11442 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011443 case 'h': // High regs or no regs.
11444 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011445 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011446 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011447 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011448 if (Subtarget->isThumb1Only())
11449 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011450 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011451 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011452 if (VT == MVT::Other)
11453 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011454 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011455 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011456 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011457 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011458 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011459 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011460 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011461 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011462 if (VT == MVT::Other)
11463 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011464 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011465 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011466 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011467 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011468 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011469 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011470 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011471 case 't':
11472 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011473 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011474 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011475 }
11476 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011477 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011478 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011479
Eric Christopher11e4df72015-02-26 22:38:43 +000011480 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011481}
11482
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011483/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11484/// vector. If it is invalid, don't add anything to Ops.
11485void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011486 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011487 std::vector<SDValue>&Ops,
11488 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011489 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011490
Eric Christopherde9399b2011-06-02 23:16:42 +000011491 // Currently only support length 1 constraints.
11492 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011493
Eric Christopherde9399b2011-06-02 23:16:42 +000011494 char ConstraintLetter = Constraint[0];
11495 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011496 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011497 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011498 case 'I': case 'J': case 'K': case 'L':
11499 case 'M': case 'N': case 'O':
11500 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11501 if (!C)
11502 return;
11503
11504 int64_t CVal64 = C->getSExtValue();
11505 int CVal = (int) CVal64;
11506 // None of these constraints allow values larger than 32 bits. Check
11507 // that the value fits in an int.
11508 if (CVal != CVal64)
11509 return;
11510
Eric Christopherde9399b2011-06-02 23:16:42 +000011511 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011512 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011513 // Constant suitable for movw, must be between 0 and
11514 // 65535.
11515 if (Subtarget->hasV6T2Ops())
11516 if (CVal >= 0 && CVal <= 65535)
11517 break;
11518 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011519 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011520 if (Subtarget->isThumb1Only()) {
11521 // This must be a constant between 0 and 255, for ADD
11522 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011523 if (CVal >= 0 && CVal <= 255)
11524 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011525 } else if (Subtarget->isThumb2()) {
11526 // A constant that can be used as an immediate value in a
11527 // data-processing instruction.
11528 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11529 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011530 } else {
11531 // A constant that can be used as an immediate value in a
11532 // data-processing instruction.
11533 if (ARM_AM::getSOImmVal(CVal) != -1)
11534 break;
11535 }
11536 return;
11537
11538 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000011539 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011540 // This must be a constant between -255 and -1, for negated ADD
11541 // immediates. This can be used in GCC with an "n" modifier that
11542 // prints the negated value, for use with SUB instructions. It is
11543 // not useful otherwise but is implemented for compatibility.
11544 if (CVal >= -255 && CVal <= -1)
11545 break;
11546 } else {
11547 // This must be a constant between -4095 and 4095. It is not clear
11548 // what this constraint is intended for. Implemented for
11549 // compatibility with GCC.
11550 if (CVal >= -4095 && CVal <= 4095)
11551 break;
11552 }
11553 return;
11554
11555 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011556 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011557 // A 32-bit value where only one byte has a nonzero value. Exclude
11558 // zero to match GCC. This constraint is used by GCC internally for
11559 // constants that can be loaded with a move/shift combination.
11560 // It is not useful otherwise but is implemented for compatibility.
11561 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11562 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011563 } else if (Subtarget->isThumb2()) {
11564 // A constant whose bitwise inverse can be used as an immediate
11565 // value in a data-processing instruction. This can be used in GCC
11566 // with a "B" modifier that prints the inverted value, for use with
11567 // BIC and MVN instructions. It is not useful otherwise but is
11568 // implemented for compatibility.
11569 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11570 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011571 } else {
11572 // A constant whose bitwise inverse can be used as an immediate
11573 // value in a data-processing instruction. This can be used in GCC
11574 // with a "B" modifier that prints the inverted value, for use with
11575 // BIC and MVN instructions. It is not useful otherwise but is
11576 // implemented for compatibility.
11577 if (ARM_AM::getSOImmVal(~CVal) != -1)
11578 break;
11579 }
11580 return;
11581
11582 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011583 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011584 // This must be a constant between -7 and 7,
11585 // for 3-operand ADD/SUB immediate instructions.
11586 if (CVal >= -7 && CVal < 7)
11587 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011588 } else if (Subtarget->isThumb2()) {
11589 // A constant whose negation can be used as an immediate value in a
11590 // data-processing instruction. This can be used in GCC with an "n"
11591 // modifier that prints the negated value, for use with SUB
11592 // instructions. It is not useful otherwise but is implemented for
11593 // compatibility.
11594 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11595 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011596 } else {
11597 // A constant whose negation can be used as an immediate value in a
11598 // data-processing instruction. This can be used in GCC with an "n"
11599 // modifier that prints the negated value, for use with SUB
11600 // instructions. It is not useful otherwise but is implemented for
11601 // compatibility.
11602 if (ARM_AM::getSOImmVal(-CVal) != -1)
11603 break;
11604 }
11605 return;
11606
11607 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000011608 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011609 // This must be a multiple of 4 between 0 and 1020, for
11610 // ADD sp + immediate.
11611 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11612 break;
11613 } else {
11614 // A power of two or a constant between 0 and 32. This is used in
11615 // GCC for the shift amount on shifted register operands, but it is
11616 // useful in general for any shift amounts.
11617 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11618 break;
11619 }
11620 return;
11621
11622 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011623 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011624 // This must be a constant between 0 and 31, for shift amounts.
11625 if (CVal >= 0 && CVal <= 31)
11626 break;
11627 }
11628 return;
11629
11630 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011631 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011632 // This must be a multiple of 4 between -508 and 508, for
11633 // ADD/SUB sp = sp + immediate.
11634 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11635 break;
11636 }
11637 return;
11638 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011639 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011640 break;
11641 }
11642
11643 if (Result.getNode()) {
11644 Ops.push_back(Result);
11645 return;
11646 }
Dale Johannesence97d552010-06-25 21:55:36 +000011647 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011648}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011649
Scott Douglassd2974a62015-08-24 09:17:11 +000011650static RTLIB::Libcall getDivRemLibcall(
11651 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011652 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11653 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011654 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011655 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11656 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011657 RTLIB::Libcall LC;
11658 switch (SVT) {
11659 default: llvm_unreachable("Unexpected request for libcall!");
11660 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11661 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11662 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11663 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11664 }
11665 return LC;
11666}
11667
11668static TargetLowering::ArgListTy getDivRemArgList(
11669 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011670 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11671 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011672 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011673 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11674 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011675 TargetLowering::ArgListTy Args;
11676 TargetLowering::ArgListEntry Entry;
11677 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11678 EVT ArgVT = N->getOperand(i).getValueType();
11679 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11680 Entry.Node = N->getOperand(i);
11681 Entry.Ty = ArgTy;
11682 Entry.isSExt = isSigned;
11683 Entry.isZExt = !isSigned;
11684 Args.push_back(Entry);
11685 }
11686 return Args;
11687}
11688
Renato Golin87610692013-07-16 09:32:17 +000011689SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000011690 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
11691 Subtarget->isTargetGNUAEABI()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011692 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011693 unsigned Opcode = Op->getOpcode();
11694 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011695 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011696 bool isSigned = (Opcode == ISD::SDIVREM);
11697 EVT VT = Op->getValueType(0);
11698 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11699
Scott Douglassd2974a62015-08-24 09:17:11 +000011700 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11701 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011702 SDValue InChain = DAG.getEntryNode();
11703
Scott Douglassd2974a62015-08-24 09:17:11 +000011704 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11705 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011706
11707 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011708 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011709
Reid Kleckner343c3952014-11-20 23:51:47 +000011710 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011711
11712 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011713 TargetLowering::CallLoweringInfo CLI(DAG);
11714 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011715 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011716 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011717
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011718 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011719 return CallInfo.first;
11720}
11721
Scott Douglassbdef6042015-08-24 09:17:18 +000011722// Lowers REM using divmod helpers
11723// see RTABI section 4.2/4.3
11724SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11725 // Build return types (div and rem)
11726 std::vector<Type*> RetTyParams;
11727 Type *RetTyElement;
11728
11729 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11730 default: llvm_unreachable("Unexpected request for libcall!");
11731 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11732 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11733 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11734 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11735 }
11736
11737 RetTyParams.push_back(RetTyElement);
11738 RetTyParams.push_back(RetTyElement);
11739 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11740 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11741
11742 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11743 SimpleTy);
11744 SDValue InChain = DAG.getEntryNode();
11745 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11746 bool isSigned = N->getOpcode() == ISD::SREM;
11747 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11748 getPointerTy(DAG.getDataLayout()));
11749
11750 // Lower call
11751 CallLoweringInfo CLI(DAG);
11752 CLI.setChain(InChain)
11753 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11754 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11755 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11756
11757 // Return second (rem) result operand (first contains div)
11758 SDNode *ResNode = CallResult.first.getNode();
11759 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11760 return ResNode->getOperand(1);
11761}
11762
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011763SDValue
11764ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11765 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11766 SDLoc DL(Op);
11767
11768 // Get the inputs.
11769 SDValue Chain = Op.getOperand(0);
11770 SDValue Size = Op.getOperand(1);
11771
11772 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011773 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011774
11775 SDValue Flag;
11776 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11777 Flag = Chain.getValue(1);
11778
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011779 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011780 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11781
11782 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11783 Chain = NewSP.getValue(1);
11784
11785 SDValue Ops[2] = { NewSP, Chain };
11786 return DAG.getMergeValues(Ops, DL);
11787}
11788
Oliver Stannard51b1d462014-08-21 12:50:31 +000011789SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11790 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11791 "Unexpected type for custom-lowering FP_EXTEND");
11792
11793 RTLIB::Libcall LC;
11794 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11795
11796 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011797 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11798 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011799}
11800
11801SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11802 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11803 Subtarget->isFPOnlySP() &&
11804 "Unexpected type for custom-lowering FP_ROUND");
11805
11806 RTLIB::Libcall LC;
11807 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11808
11809 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011810 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11811 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011812}
11813
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011814bool
11815ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11816 // The ARM target isn't yet aware of offsets.
11817 return false;
11818}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011819
Jim Grosbach11013ed2010-07-16 23:05:05 +000011820bool ARM::isBitFieldInvertedMask(unsigned v) {
11821 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011822 return false;
11823
Jim Grosbach11013ed2010-07-16 23:05:05 +000011824 // there can be 1's on either or both "outsides", all the "inside"
11825 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011826 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011827}
11828
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011829/// isFPImmLegal - Returns true if the target can instruction select the
11830/// specified FP immediate natively. If false, the legalizer will
11831/// materialize the FP immediate as a load from a constant pool.
11832bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11833 if (!Subtarget->hasVFP3())
11834 return false;
11835 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011836 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011837 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011838 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011839 return false;
11840}
Bob Wilson5549d492010-09-21 17:56:22 +000011841
Wesley Peck527da1b2010-11-23 03:31:01 +000011842/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011843/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11844/// specified in the intrinsic calls.
11845bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11846 const CallInst &I,
11847 unsigned Intrinsic) const {
11848 switch (Intrinsic) {
11849 case Intrinsic::arm_neon_vld1:
11850 case Intrinsic::arm_neon_vld2:
11851 case Intrinsic::arm_neon_vld3:
11852 case Intrinsic::arm_neon_vld4:
11853 case Intrinsic::arm_neon_vld2lane:
11854 case Intrinsic::arm_neon_vld3lane:
11855 case Intrinsic::arm_neon_vld4lane: {
11856 Info.opc = ISD::INTRINSIC_W_CHAIN;
11857 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011858 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011859 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011860 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11861 Info.ptrVal = I.getArgOperand(0);
11862 Info.offset = 0;
11863 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11864 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11865 Info.vol = false; // volatile loads with NEON intrinsics not supported
11866 Info.readMem = true;
11867 Info.writeMem = false;
11868 return true;
11869 }
11870 case Intrinsic::arm_neon_vst1:
11871 case Intrinsic::arm_neon_vst2:
11872 case Intrinsic::arm_neon_vst3:
11873 case Intrinsic::arm_neon_vst4:
11874 case Intrinsic::arm_neon_vst2lane:
11875 case Intrinsic::arm_neon_vst3lane:
11876 case Intrinsic::arm_neon_vst4lane: {
11877 Info.opc = ISD::INTRINSIC_VOID;
11878 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011879 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011880 unsigned NumElts = 0;
11881 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011882 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011883 if (!ArgTy->isVectorTy())
11884 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011885 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011886 }
11887 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11888 Info.ptrVal = I.getArgOperand(0);
11889 Info.offset = 0;
11890 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11891 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11892 Info.vol = false; // volatile stores with NEON intrinsics not supported
11893 Info.readMem = false;
11894 Info.writeMem = true;
11895 return true;
11896 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011897 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011898 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011899 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011900 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11901 Info.opc = ISD::INTRINSIC_W_CHAIN;
11902 Info.memVT = MVT::getVT(PtrTy->getElementType());
11903 Info.ptrVal = I.getArgOperand(0);
11904 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011905 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011906 Info.vol = true;
11907 Info.readMem = true;
11908 Info.writeMem = false;
11909 return true;
11910 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011911 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011912 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011913 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011914 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11915 Info.opc = ISD::INTRINSIC_W_CHAIN;
11916 Info.memVT = MVT::getVT(PtrTy->getElementType());
11917 Info.ptrVal = I.getArgOperand(1);
11918 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011919 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011920 Info.vol = true;
11921 Info.readMem = false;
11922 Info.writeMem = true;
11923 return true;
11924 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011925 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011926 case Intrinsic::arm_strexd: {
11927 Info.opc = ISD::INTRINSIC_W_CHAIN;
11928 Info.memVT = MVT::i64;
11929 Info.ptrVal = I.getArgOperand(2);
11930 Info.offset = 0;
11931 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011932 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011933 Info.readMem = false;
11934 Info.writeMem = true;
11935 return true;
11936 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011937 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011938 case Intrinsic::arm_ldrexd: {
11939 Info.opc = ISD::INTRINSIC_W_CHAIN;
11940 Info.memVT = MVT::i64;
11941 Info.ptrVal = I.getArgOperand(0);
11942 Info.offset = 0;
11943 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011944 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011945 Info.readMem = true;
11946 Info.writeMem = false;
11947 return true;
11948 }
Bob Wilson5549d492010-09-21 17:56:22 +000011949 default:
11950 break;
11951 }
11952
11953 return false;
11954}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011955
11956/// \brief Returns true if it is beneficial to convert a load of a constant
11957/// to just the constant itself.
11958bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11959 Type *Ty) const {
11960 assert(Ty->isIntegerTy());
11961
11962 unsigned Bits = Ty->getPrimitiveSizeInBits();
11963 if (Bits == 0 || Bits > 32)
11964 return false;
11965 return true;
11966}
Tim Northover037f26f22014-04-17 18:22:47 +000011967
Robin Morisset5349e8e2014-09-18 18:56:04 +000011968Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11969 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011970 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011971
11972 // First, if the target has no DMB, see what fallback we can use.
11973 if (!Subtarget->hasDataBarrier()) {
11974 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11975 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11976 // here.
11977 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11978 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11979 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11980 Builder.getInt32(0), Builder.getInt32(7),
11981 Builder.getInt32(10), Builder.getInt32(5)};
11982 return Builder.CreateCall(MCR, args);
11983 } else {
11984 // Instead of using barriers, atomic accesses on these subtargets use
11985 // libcalls.
11986 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11987 }
11988 } else {
11989 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11990 // Only a full system barrier exists in the M-class architectures.
11991 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11992 Constant *CDomain = Builder.getInt32(Domain);
11993 return Builder.CreateCall(DMB, CDomain);
11994 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011995}
11996
11997// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011998Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011999 AtomicOrdering Ord, bool IsStore,
12000 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012001 switch (Ord) {
12002 case NotAtomic:
12003 case Unordered:
12004 llvm_unreachable("Invalid fence: unordered/non-atomic");
12005 case Monotonic:
12006 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012007 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000012008 case SequentiallyConsistent:
12009 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012010 return nullptr; // Nothing to do
12011 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000012012 case Release:
12013 case AcquireRelease:
12014 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000012015 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012016 // FIXME: add a comment with a link to documentation justifying this.
12017 else
Robin Morissetdedef332014-09-23 20:31:14 +000012018 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012019 }
Robin Morissetdedef332014-09-23 20:31:14 +000012020 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012021}
12022
Robin Morissetdedef332014-09-23 20:31:14 +000012023Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012024 AtomicOrdering Ord, bool IsStore,
12025 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012026 switch (Ord) {
12027 case NotAtomic:
12028 case Unordered:
12029 llvm_unreachable("Invalid fence: unordered/not-atomic");
12030 case Monotonic:
12031 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012032 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000012033 case Acquire:
12034 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000012035 case SequentiallyConsistent:
12036 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012037 }
Robin Morissetdedef332014-09-23 20:31:14 +000012038 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012039}
12040
Robin Morisseted3d48f2014-09-03 21:29:59 +000012041// Loads and stores less than 64-bits are already atomic; ones above that
12042// are doomed anyway, so defer to the default libcall and blame the OS when
12043// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12044// anything for those.
12045bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12046 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12047 return (Size == 64) && !Subtarget->isMClass();
12048}
Tim Northover037f26f22014-04-17 18:22:47 +000012049
Robin Morisseted3d48f2014-09-03 21:29:59 +000012050// Loads and stores less than 64-bits are already atomic; ones above that
12051// are doomed anyway, so defer to the default libcall and blame the OS when
12052// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12053// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012054// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12055// guarantee, see DDI0406C ARM architecture reference manual,
12056// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012057TargetLowering::AtomicExpansionKind
12058ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012059 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012060 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012061 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012062}
12063
12064// For the real atomic operations, we have ldrex/strex up to 32 bits,
12065// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012066TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012067ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012068 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012069 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012070 ? AtomicExpansionKind::LLSC
12071 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012072}
12073
Ahmed Bougacha52468672015-09-11 17:08:28 +000012074bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12075 AtomicCmpXchgInst *AI) const {
12076 return true;
12077}
12078
James Y Knightf44fc522016-03-16 22:12:04 +000012079bool ARMTargetLowering::shouldInsertFencesForAtomic(
12080 const Instruction *I) const {
12081 return InsertFencesForAtomic;
12082}
12083
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012084// This has so far only been implemented for MachO.
12085bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012086 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012087}
12088
Quentin Colombetc32615d2014-10-31 17:52:53 +000012089bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12090 unsigned &Cost) const {
12091 // If we do not have NEON, vector types are not natively supported.
12092 if (!Subtarget->hasNEON())
12093 return false;
12094
12095 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012096 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012097 // better to leave at float as we have more freedom in the addressing mode for
12098 // those.
12099 if (VectorTy->isFPOrFPVectorTy())
12100 return false;
12101
12102 // If the index is unknown at compile time, this is very expensive to lower
12103 // and it is not possible to combine the store with the extract.
12104 if (!isa<ConstantInt>(Idx))
12105 return false;
12106
12107 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12108 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12109 // We can do a store + vector extract on any vector that fits perfectly in a D
12110 // or Q register.
12111 if (BitWidth == 64 || BitWidth == 128) {
12112 Cost = 0;
12113 return true;
12114 }
12115 return false;
12116}
12117
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012118bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12119 return Subtarget->hasV6T2Ops();
12120}
12121
12122bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12123 return Subtarget->hasV6T2Ops();
12124}
12125
Tim Northover037f26f22014-04-17 18:22:47 +000012126Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12127 AtomicOrdering Ord) const {
12128 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12129 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000012130 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012131
12132 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12133 // intrinsic must return {i32, i32} and we have to recombine them into a
12134 // single i64 here.
12135 if (ValTy->getPrimitiveSizeInBits() == 64) {
12136 Intrinsic::ID Int =
12137 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12138 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12139
12140 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12141 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12142
12143 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12144 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012145 if (!Subtarget->isLittle())
12146 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012147 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12148 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12149 return Builder.CreateOr(
12150 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12151 }
12152
12153 Type *Tys[] = { Addr->getType() };
12154 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12155 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12156
12157 return Builder.CreateTruncOrBitCast(
12158 Builder.CreateCall(Ldrex, Addr),
12159 cast<PointerType>(Addr->getType())->getElementType());
12160}
12161
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012162void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12163 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012164 if (!Subtarget->hasV7Ops())
12165 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012166 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12167 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12168}
12169
Tim Northover037f26f22014-04-17 18:22:47 +000012170Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12171 Value *Addr,
12172 AtomicOrdering Ord) const {
12173 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000012174 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012175
12176 // Since the intrinsics must have legal type, the i64 intrinsics take two
12177 // parameters: "i32, i32". We must marshal Val into the appropriate form
12178 // before the call.
12179 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12180 Intrinsic::ID Int =
12181 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12182 Function *Strex = Intrinsic::getDeclaration(M, Int);
12183 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12184
12185 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12186 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012187 if (!Subtarget->isLittle())
12188 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012189 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012190 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012191 }
12192
12193 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12194 Type *Tys[] = { Addr->getType() };
12195 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12196
David Blaikieff6409d2015-05-18 22:13:54 +000012197 return Builder.CreateCall(
12198 Strex, {Builder.CreateZExtOrBitCast(
12199 Val, Strex->getFunctionType()->getParamType(0)),
12200 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012201}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012202
Hao Liu2cd34bb2015-06-26 02:45:36 +000012203/// \brief Lower an interleaved load into a vldN intrinsic.
12204///
12205/// E.g. Lower an interleaved load (Factor = 2):
12206/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12207/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12208/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12209///
12210/// Into:
12211/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12212/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12213/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12214bool ARMTargetLowering::lowerInterleavedLoad(
12215 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12216 ArrayRef<unsigned> Indices, unsigned Factor) const {
12217 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12218 "Invalid interleave factor");
12219 assert(!Shuffles.empty() && "Empty shufflevector input");
12220 assert(Shuffles.size() == Indices.size() &&
12221 "Unmatched number of shufflevectors and indices");
12222
12223 VectorType *VecTy = Shuffles[0]->getType();
12224 Type *EltTy = VecTy->getVectorElementType();
12225
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012226 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012227 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
12228 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012229
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012230 // Skip if we do not have NEON and skip illegal vector types and vector types
12231 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12232 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012233 return false;
12234
12235 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12236 // load integer vectors first and then convert to pointer vectors.
12237 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012238 VecTy =
12239 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012240
12241 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12242 Intrinsic::arm_neon_vld3,
12243 Intrinsic::arm_neon_vld4};
12244
Hao Liu2cd34bb2015-06-26 02:45:36 +000012245 IRBuilder<> Builder(LI);
12246 SmallVector<Value *, 2> Ops;
12247
12248 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12249 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12250 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12251
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012252 Type *Tys[] = { VecTy, Int8Ptr };
12253 Function *VldnFunc =
12254 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012255 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12256
12257 // Replace uses of each shufflevector with the corresponding vector loaded
12258 // by ldN.
12259 for (unsigned i = 0; i < Shuffles.size(); i++) {
12260 ShuffleVectorInst *SV = Shuffles[i];
12261 unsigned Index = Indices[i];
12262
12263 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12264
12265 // Convert the integer vector to pointer vector if the element is pointer.
12266 if (EltTy->isPointerTy())
12267 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12268
12269 SV->replaceAllUsesWith(SubVec);
12270 }
12271
12272 return true;
12273}
12274
12275/// \brief Get a mask consisting of sequential integers starting from \p Start.
12276///
12277/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12278static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12279 unsigned NumElts) {
12280 SmallVector<Constant *, 16> Mask;
12281 for (unsigned i = 0; i < NumElts; i++)
12282 Mask.push_back(Builder.getInt32(Start + i));
12283
12284 return ConstantVector::get(Mask);
12285}
12286
12287/// \brief Lower an interleaved store into a vstN intrinsic.
12288///
12289/// E.g. Lower an interleaved store (Factor = 3):
12290/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12291/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12292/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12293///
12294/// Into:
12295/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12296/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12297/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12298/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12299///
12300/// Note that the new shufflevectors will be removed and we'll only generate one
12301/// vst3 instruction in CodeGen.
12302bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12303 ShuffleVectorInst *SVI,
12304 unsigned Factor) const {
12305 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12306 "Invalid interleave factor");
12307
12308 VectorType *VecTy = SVI->getType();
12309 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12310 "Invalid interleaved store");
12311
12312 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12313 Type *EltTy = VecTy->getVectorElementType();
12314 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12315
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012316 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012317 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
12318 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012319
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012320 // Skip if we do not have NEON and skip illegal vector types and vector types
12321 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12322 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12323 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012324 return false;
12325
12326 Value *Op0 = SVI->getOperand(0);
12327 Value *Op1 = SVI->getOperand(1);
12328 IRBuilder<> Builder(SI);
12329
12330 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12331 // vectors to integer vectors.
12332 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012333 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012334
12335 // Convert to the corresponding integer vector.
12336 Type *IntVecTy =
12337 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12338 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12339 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12340
12341 SubVecTy = VectorType::get(IntTy, NumSubElts);
12342 }
12343
Craig Topper26260942015-10-18 05:15:34 +000012344 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12345 Intrinsic::arm_neon_vst3,
12346 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012347 SmallVector<Value *, 6> Ops;
12348
12349 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12350 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12351
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012352 Type *Tys[] = { Int8Ptr, SubVecTy };
12353 Function *VstNFunc = Intrinsic::getDeclaration(
12354 SI->getModule(), StoreInts[Factor - 2], Tys);
12355
Hao Liu2cd34bb2015-06-26 02:45:36 +000012356 // Split the shufflevector operands into sub vectors for the new vstN call.
12357 for (unsigned i = 0; i < Factor; i++)
12358 Ops.push_back(Builder.CreateShuffleVector(
12359 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12360
12361 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12362 Builder.CreateCall(VstNFunc, Ops);
12363 return true;
12364}
12365
Oliver Stannardc24f2172014-05-09 14:01:47 +000012366enum HABaseType {
12367 HA_UNKNOWN = 0,
12368 HA_FLOAT,
12369 HA_DOUBLE,
12370 HA_VECT64,
12371 HA_VECT128
12372};
12373
12374static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12375 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012376 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012377 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12378 uint64_t SubMembers = 0;
12379 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12380 return false;
12381 Members += SubMembers;
12382 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012383 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012384 uint64_t SubMembers = 0;
12385 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12386 return false;
12387 Members += SubMembers * AT->getNumElements();
12388 } else if (Ty->isFloatTy()) {
12389 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12390 return false;
12391 Members = 1;
12392 Base = HA_FLOAT;
12393 } else if (Ty->isDoubleTy()) {
12394 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12395 return false;
12396 Members = 1;
12397 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012398 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012399 Members = 1;
12400 switch (Base) {
12401 case HA_FLOAT:
12402 case HA_DOUBLE:
12403 return false;
12404 case HA_VECT64:
12405 return VT->getBitWidth() == 64;
12406 case HA_VECT128:
12407 return VT->getBitWidth() == 128;
12408 case HA_UNKNOWN:
12409 switch (VT->getBitWidth()) {
12410 case 64:
12411 Base = HA_VECT64;
12412 return true;
12413 case 128:
12414 Base = HA_VECT128;
12415 return true;
12416 default:
12417 return false;
12418 }
12419 }
12420 }
12421
12422 return (Members > 0 && Members <= 4);
12423}
12424
Tim Northovere95c5b32015-02-24 17:22:34 +000012425/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12426/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12427/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012428bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12429 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012430 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12431 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012432 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012433
12434 HABaseType Base = HA_UNKNOWN;
12435 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012436 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12437 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12438
12439 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12440 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012441}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012442
12443unsigned ARMTargetLowering::getExceptionPointerRegister(
12444 const Constant *PersonalityFn) const {
12445 // Platforms which do not use SjLj EH may return values in these registers
12446 // via the personality function.
12447 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12448}
12449
12450unsigned ARMTargetLowering::getExceptionSelectorRegister(
12451 const Constant *PersonalityFn) const {
12452 // Platforms which do not use SjLj EH may return values in these registers
12453 // via the personality function.
12454 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12455}
Manman Ren5e9e65e2016-01-12 00:47:18 +000012456
12457void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
12458 // Update IsSplitCSR in ARMFunctionInfo.
12459 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
12460 AFI->setIsSplitCSR(true);
12461}
12462
12463void ARMTargetLowering::insertCopiesSplitCSR(
12464 MachineBasicBlock *Entry,
12465 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
12466 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
12467 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
12468 if (!IStart)
12469 return;
12470
12471 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
12472 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000012473 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000012474 for (const MCPhysReg *I = IStart; *I; ++I) {
12475 const TargetRegisterClass *RC = nullptr;
12476 if (ARM::GPRRegClass.contains(*I))
12477 RC = &ARM::GPRRegClass;
12478 else if (ARM::DPRRegClass.contains(*I))
12479 RC = &ARM::DPRRegClass;
12480 else
12481 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12482
12483 unsigned NewVR = MRI->createVirtualRegister(RC);
12484 // Create copy from CSR to a virtual register.
12485 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12486 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12487 // nounwind. If we want to generalize this later, we may need to emit
12488 // CFI pseudo-instructions.
12489 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12490 Attribute::NoUnwind) &&
12491 "Function should be nounwind in insertCopiesSplitCSR!");
12492 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000012493 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012494 .addReg(*I);
12495
Manman Rene5f807f2016-01-15 20:24:11 +000012496 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000012497 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000012498 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12499 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012500 .addReg(NewVR);
12501 }
12502}