blob: 9cd880d719d0df519a6209d137030f5c32a45c9a [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 },
393 };
394
395 for (const auto &LC : LibraryCalls) {
396 setLibcallName(LC.Op, LC.Name);
397 setLibcallCallingConv(LC.Op, LC.CC);
398 }
399 }
400
Bob Wilsonbc158992011-10-07 16:59:21 +0000401 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000402 if (Subtarget->isTargetWatchOS() ||
403 (Subtarget->isTargetIOS() &&
404 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000405 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
406 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
407 }
408
Oliver Stannard11790b22014-08-11 09:12:32 +0000409 // The half <-> float conversion functions are always soft-float, but are
410 // needed for some targets which use a hard-float calling convention by
411 // default.
412 if (Subtarget->isAAPCS_ABI()) {
413 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
414 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
415 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
416 } else {
417 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
418 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
419 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
420 }
421
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000422 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
423 // a __gnu_ prefix (which is the default).
424 if (Subtarget->isTargetAEABI()) {
425 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
426 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
427 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
428 }
429
David Goodwin22c2fba2009-07-08 23:10:31 +0000430 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000431 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000432 else
Craig Topperc7242e02012-04-20 07:30:17 +0000433 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000434 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000435 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000436 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000437 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000438 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000439
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000440 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000441 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000442 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000443 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
444 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
445 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
446 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000447
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000448 setOperationAction(ISD::MULHS, VT, Expand);
449 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
450 setOperationAction(ISD::MULHU, VT, Expand);
451 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000452
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000453 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000454 }
455
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000456 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000457 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000458
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000459 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
460 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
461
Bob Wilson2e076c42009-06-22 23:27:02 +0000462 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000463 addDRTypeForNEON(MVT::v2f32);
464 addDRTypeForNEON(MVT::v8i8);
465 addDRTypeForNEON(MVT::v4i16);
466 addDRTypeForNEON(MVT::v2i32);
467 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000468
Owen Anderson9f944592009-08-11 20:47:22 +0000469 addQRTypeForNEON(MVT::v4f32);
470 addQRTypeForNEON(MVT::v2f64);
471 addQRTypeForNEON(MVT::v16i8);
472 addQRTypeForNEON(MVT::v8i16);
473 addQRTypeForNEON(MVT::v4i32);
474 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000475
Bob Wilson194a2512009-09-15 23:55:57 +0000476 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
477 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000478 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
479 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000480 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
481 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
482 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000483 // FIXME: Code duplication: FDIV and FREM are expanded always, see
484 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000485 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
486 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000487 // FIXME: Create unittest.
488 // In another words, find a way when "copysign" appears in DAG with vector
489 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000490 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000491 // FIXME: Code duplication: SETCC has custom operation action, see
492 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000493 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000494 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000495 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
496 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
497 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
498 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
499 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
500 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
501 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
502 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
503 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
504 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
505 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
506 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000507 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000508 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
509 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
510 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
511 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
512 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000513 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000514
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000515 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
516 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
517 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
518 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
519 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
520 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
521 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
522 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
523 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
524 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000525 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
526 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
527 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
528 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000529 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000530
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000531 // Mark v2f32 intrinsics.
532 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
533 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
534 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
535 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
536 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
537 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
538 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
539 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
540 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
541 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
542 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
543 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
544 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
545 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
546 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
547
Bob Wilson6cc46572009-09-16 00:32:15 +0000548 // Neon does not support some operations on v1i64 and v2i64 types.
549 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000550 // Custom handling for some quad-vector types to detect VMULL.
551 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
552 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
553 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000554 // Custom handling for some vector types to avoid expensive expansions
555 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
556 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
557 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
558 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000559 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
560 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000561 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000562 // a destination type that is wider than the source, and nor does
563 // it have a FP_TO_[SU]INT instruction with a narrower destination than
564 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000565 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
566 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000567 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
568 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000569
Eli Friedmane6385e62012-11-15 22:44:27 +0000570 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000571 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000572
Evan Chengb4eae132012-12-04 22:41:50 +0000573 // NEON does not have single instruction CTPOP for vectors with element
574 // types wider than 8-bits. However, custom lowering can leverage the
575 // v8i8/v16i8 vcnt instruction.
576 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
577 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
578 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
579 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
580
Logan Chien0a43abc2015-07-13 15:37:30 +0000581 // NEON does not have single instruction CTTZ for vectors.
582 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
583 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
584 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
585 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
586
587 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
588 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
589 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
590 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
591
592 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
593 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
594 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
595 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
596
597 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
598 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
599 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
600 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
601
Jim Grosbach5f215872013-02-27 21:31:12 +0000602 // NEON only has FMA instructions as of VFP4.
603 if (!Subtarget->hasVFP4()) {
604 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
605 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
606 }
607
Bob Wilson06fce872011-02-07 17:43:21 +0000608 setTargetDAGCombine(ISD::INTRINSIC_VOID);
609 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000610 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
611 setTargetDAGCombine(ISD::SHL);
612 setTargetDAGCombine(ISD::SRL);
613 setTargetDAGCombine(ISD::SRA);
614 setTargetDAGCombine(ISD::SIGN_EXTEND);
615 setTargetDAGCombine(ISD::ZERO_EXTEND);
616 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000617 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000618 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000619 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
620 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000621 setTargetDAGCombine(ISD::FP_TO_SINT);
622 setTargetDAGCombine(ISD::FP_TO_UINT);
623 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000624 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000625
James Molloy547d4c02012-02-20 09:24:05 +0000626 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000627 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
628 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000629 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000630 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
631 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
632 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000633 }
James Molloy547d4c02012-02-20 09:24:05 +0000634 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000635 }
636
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000637 // ARM and Thumb2 support UMLAL/SMLAL.
638 if (!Subtarget->isThumb1Only())
639 setTargetDAGCombine(ISD::ADDC);
640
Oliver Stannard51b1d462014-08-21 12:50:31 +0000641 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000642 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000643 // operations, f64 is legal for the few double-precision instructions which
644 // are present However, no double-precision operations other than moves,
645 // loads and stores are provided by the hardware.
646 setOperationAction(ISD::FADD, MVT::f64, Expand);
647 setOperationAction(ISD::FSUB, MVT::f64, Expand);
648 setOperationAction(ISD::FMUL, MVT::f64, Expand);
649 setOperationAction(ISD::FMA, MVT::f64, Expand);
650 setOperationAction(ISD::FDIV, MVT::f64, Expand);
651 setOperationAction(ISD::FREM, MVT::f64, Expand);
652 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
653 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
654 setOperationAction(ISD::FNEG, MVT::f64, Expand);
655 setOperationAction(ISD::FABS, MVT::f64, Expand);
656 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
657 setOperationAction(ISD::FSIN, MVT::f64, Expand);
658 setOperationAction(ISD::FCOS, MVT::f64, Expand);
659 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
660 setOperationAction(ISD::FPOW, MVT::f64, Expand);
661 setOperationAction(ISD::FLOG, MVT::f64, Expand);
662 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
663 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
664 setOperationAction(ISD::FEXP, MVT::f64, Expand);
665 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
666 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
667 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
668 setOperationAction(ISD::FRINT, MVT::f64, Expand);
669 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
670 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000671 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
672 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
673 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
674 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
675 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
676 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000677 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
678 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
679 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000680
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000681 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000682
Tim Northover4e80b582014-07-18 13:01:19 +0000683 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000684 for (MVT VT : MVT::fp_valuetypes()) {
685 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
686 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
687 }
Tim Northover4e80b582014-07-18 13:01:19 +0000688
689 // ... or truncating stores
690 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
691 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
692 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000693
Duncan Sands95d46ef2008-01-23 20:39:46 +0000694 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000695 for (MVT VT : MVT::integer_valuetypes())
696 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000697
Evan Cheng10043e22007-01-19 07:51:42 +0000698 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000699 if (!Subtarget->isThumb1Only()) {
700 for (unsigned im = (unsigned)ISD::PRE_INC;
701 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000702 setIndexedLoadAction(im, MVT::i1, Legal);
703 setIndexedLoadAction(im, MVT::i8, Legal);
704 setIndexedLoadAction(im, MVT::i16, Legal);
705 setIndexedLoadAction(im, MVT::i32, Legal);
706 setIndexedStoreAction(im, MVT::i1, Legal);
707 setIndexedStoreAction(im, MVT::i8, Legal);
708 setIndexedStoreAction(im, MVT::i16, Legal);
709 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000710 }
Evan Cheng10043e22007-01-19 07:51:42 +0000711 }
712
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000713 setOperationAction(ISD::SADDO, MVT::i32, Custom);
714 setOperationAction(ISD::UADDO, MVT::i32, Custom);
715 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
716 setOperationAction(ISD::USUBO, MVT::i32, Custom);
717
Evan Cheng10043e22007-01-19 07:51:42 +0000718 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000719 setOperationAction(ISD::MUL, MVT::i64, Expand);
720 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000721 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000722 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
723 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000724 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000725 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000726 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000727 setOperationAction(ISD::MULHS, MVT::i32, Expand);
728
Jim Grosbach5d994042009-10-31 19:38:01 +0000729 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000730 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000731 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000732 setOperationAction(ISD::SRL, MVT::i64, Custom);
733 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000734
Evan Chenge8916542011-08-30 01:34:54 +0000735 if (!Subtarget->isThumb1Only()) {
736 // FIXME: We should do this for Thumb1 as well.
737 setOperationAction(ISD::ADDC, MVT::i32, Custom);
738 setOperationAction(ISD::ADDE, MVT::i32, Custom);
739 setOperationAction(ISD::SUBC, MVT::i32, Custom);
740 setOperationAction(ISD::SUBE, MVT::i32, Custom);
741 }
742
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000743 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000744 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
745
Evan Cheng10043e22007-01-19 07:51:42 +0000746 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000747 setOperationAction(ISD::ROTL, MVT::i32, Expand);
748 for (MVT VT : MVT::vector_valuetypes()) {
749 setOperationAction(ISD::ROTL, VT, Expand);
750 setOperationAction(ISD::ROTR, VT, Expand);
751 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000752 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000753 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000754 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000755 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000756
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000757 // These just redirect to CTTZ and CTLZ on ARM.
758 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
759 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
760
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000761 // @llvm.readcyclecounter requires the Performance Monitors extension.
762 // Default to the 0 expansion on unsupported platforms.
763 // FIXME: Technically there are older ARM CPUs that have
764 // implementation-specific ways of obtaining this information.
765 if (Subtarget->hasPerfMon())
766 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000767
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000768 // Only ARMv6 has BSWAP.
769 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000770 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000771
Bradley Smith519563e2016-01-15 10:25:35 +0000772 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide()
773 : Subtarget->hasDivideInARMMode();
774 if (!hasDivide) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000775 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000776 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
777 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000778 }
Renato Golin87610692013-07-16 09:32:17 +0000779
Saleem Abdulrasool071a0992016-03-17 14:10:49 +0000780 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
781 setOperationAction(ISD::SDIV, MVT::i32, Custom);
782 setOperationAction(ISD::UDIV, MVT::i32, Custom);
783
784 setOperationAction(ISD::SDIV, MVT::i64, Custom);
785 setOperationAction(ISD::UDIV, MVT::i64, Custom);
786 }
787
Chad Rosierad7c9102014-08-23 18:29:43 +0000788 setOperationAction(ISD::SREM, MVT::i32, Expand);
789 setOperationAction(ISD::UREM, MVT::i32, Expand);
790 // Register based DivRem for AEABI (RTABI 4.2)
Renato Golin6027dd38e2016-02-03 16:10:54 +0000791 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
792 Subtarget->isTargetGNUAEABI()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000793 setOperationAction(ISD::SREM, MVT::i64, Custom);
794 setOperationAction(ISD::UREM, MVT::i64, Custom);
795
Chad Rosierad7c9102014-08-23 18:29:43 +0000796 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
797 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
798 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
799 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
800 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
801 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
802 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
803 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
804
805 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
806 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
807 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
808 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
809 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
810 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
811 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
812 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
813
814 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
815 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Renato Golin175c6d62016-03-04 19:19:36 +0000816 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
817 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Chad Rosierad7c9102014-08-23 18:29:43 +0000818 } else {
Renato Golin87610692013-07-16 09:32:17 +0000819 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
820 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
821 }
Bob Wilson7117a912009-03-20 22:42:55 +0000822
Owen Anderson9f944592009-08-11 20:47:22 +0000823 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
824 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000825 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000826 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000827
Evan Cheng74d92c12011-04-08 21:37:21 +0000828 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000829
Evan Cheng10043e22007-01-19 07:51:42 +0000830 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000831 setOperationAction(ISD::VASTART, MVT::Other, Custom);
832 setOperationAction(ISD::VAARG, MVT::Other, Expand);
833 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
834 setOperationAction(ISD::VAEND, MVT::Other, Expand);
835 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
836 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000837
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000838 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
839 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
840 else
841 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
842
Evan Cheng6e809de2010-08-11 06:22:01 +0000843 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000844 // the default expansion. If we are targeting a single threaded system,
845 // then set them all for expand so we can lower them later into their
846 // non-atomic form.
James Y Knightf44fc522016-03-16 22:12:04 +0000847 InsertFencesForAtomic = false;
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000848 if (TM.Options.ThreadModel == ThreadModel::Single)
849 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Bradley Smith433c22e2016-01-15 10:26:51 +0000850 else if (Subtarget->hasAnyDataBarrier() && (!Subtarget->isThumb() ||
851 Subtarget->hasV8MBaselineOps())) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000852 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
853 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000854 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000855
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000856 // On v8, we have particularly efficient implementations of atomic fences
857 // if they can be combined with nearby atomic loads and stores.
858 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000859 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
James Y Knightf44fc522016-03-16 22:12:04 +0000860 InsertFencesForAtomic = true;
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000861 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000862 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000863 // If there's anything we can use as a barrier, go through custom lowering
864 // for ATOMIC_FENCE.
865 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
866 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
867
Jim Grosbach6860bb72010-06-18 22:35:32 +0000868 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000869 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000870 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000871 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000872 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000873 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000874 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000875 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000876 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000877 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000878 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000879 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000880 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000881 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
882 // Unordered/Monotonic case.
883 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
884 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000885 }
Evan Cheng10043e22007-01-19 07:51:42 +0000886
Evan Cheng21acf9f2010-11-04 05:19:35 +0000887 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000888
Eli Friedman8cfa7712010-06-26 04:36:50 +0000889 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
890 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000891 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
892 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000893 }
Owen Anderson9f944592009-08-11 20:47:22 +0000894 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000895
Eric Christopher824f42f2015-05-12 01:26:05 +0000896 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000897 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000898 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000899 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000900 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000901 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
902 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000903
904 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000905 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000906 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
907 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
908 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +0000909 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +0000910 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000911
Owen Anderson9f944592009-08-11 20:47:22 +0000912 setOperationAction(ISD::SETCC, MVT::i32, Expand);
913 setOperationAction(ISD::SETCC, MVT::f32, Expand);
914 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000915 setOperationAction(ISD::SELECT, MVT::i32, Custom);
916 setOperationAction(ISD::SELECT, MVT::f32, Custom);
917 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000918 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
919 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
920 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000921
Owen Anderson9f944592009-08-11 20:47:22 +0000922 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
923 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
924 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
925 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
926 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000927
Dan Gohman482732a2007-10-11 23:21:31 +0000928 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000929 setOperationAction(ISD::FSIN, MVT::f64, Expand);
930 setOperationAction(ISD::FSIN, MVT::f32, Expand);
931 setOperationAction(ISD::FCOS, MVT::f32, Expand);
932 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000933 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
934 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000935 setOperationAction(ISD::FREM, MVT::f64, Expand);
936 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000937 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000938 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000939 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
940 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000941 }
Owen Anderson9f944592009-08-11 20:47:22 +0000942 setOperationAction(ISD::FPOW, MVT::f64, Expand);
943 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000944
Evan Chengd0007f32012-04-10 21:40:28 +0000945 if (!Subtarget->hasVFP4()) {
946 setOperationAction(ISD::FMA, MVT::f64, Expand);
947 setOperationAction(ISD::FMA, MVT::f32, Expand);
948 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000949
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000950 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000951 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000952 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
953 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000954 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
955 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
956 }
957
958 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000959 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000960 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
961 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000962 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000963 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000964
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000965 // Combine sin / cos into one node or libcall if possible.
966 if (Subtarget->hasSinCos()) {
967 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
968 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover042a6c12016-01-27 19:32:29 +0000969 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000970 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
971 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
972 }
973 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000974 // For iOS, we don't want to the normal expansion of a libcall to
975 // sincos. We want to issue a libcall to __sincos_stret.
976 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
977 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
978 }
979 }
Evan Cheng10043e22007-01-19 07:51:42 +0000980
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000981 // FP-ARMv8 implements a lot of rounding-like FP operations.
982 if (Subtarget->hasFPARMv8()) {
983 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
984 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
985 setOperationAction(ISD::FROUND, MVT::f32, Legal);
986 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
987 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
988 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +0000989 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
990 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +0000991 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
992 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
993 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
994 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
995
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000996 if (!Subtarget->isFPOnlySP()) {
997 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
998 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
999 setOperationAction(ISD::FROUND, MVT::f64, Legal);
1000 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1001 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1002 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001003 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1004 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001005 }
1006 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001007
James Molloy974838f2015-08-17 19:37:12 +00001008 if (Subtarget->hasNEON()) {
1009 // vmin and vmax aren't available in a scalar form, so we use
1010 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001011 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1012 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001013 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1014 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1015 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1016 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1017 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001018
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001019 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001020 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001021 setTargetDAGCombine(ISD::ADD);
1022 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001023 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001024 setTargetDAGCombine(ISD::AND);
1025 setTargetDAGCombine(ISD::OR);
1026 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001027
Evan Chengf258a152012-02-23 02:58:19 +00001028 if (Subtarget->hasV6Ops())
1029 setTargetDAGCombine(ISD::SRL);
1030
Evan Cheng10043e22007-01-19 07:51:42 +00001031 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001032
Eric Christopher824f42f2015-05-12 01:26:05 +00001033 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001034 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001035 setSchedulingPreference(Sched::RegPressure);
1036 else
1037 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001038
Evan Cheng3ae2b792011-01-06 06:52:41 +00001039 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001040 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001041 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001042 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001043 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001044 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001045 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001046
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001047 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1048 // are at least 4 bytes aligned.
1049 setMinStackArgumentAlignment(4);
1050
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001051 // Prefer likely predicted branches to selects on out-of-order cores.
Junmo Park453f4aa2016-02-23 09:56:58 +00001052 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001053
Eli Friedman2518f832011-05-06 20:34:06 +00001054 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001055}
1056
Eric Christopher824f42f2015-05-12 01:26:05 +00001057bool ARMTargetLowering::useSoftFloat() const {
1058 return Subtarget->useSoftFloat();
1059}
1060
Andrew Trick43f25632011-01-19 02:35:27 +00001061// FIXME: It might make sense to define the representative register class as the
1062// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1063// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1064// SPR's representative would be DPR_VFP2. This should work well if register
1065// pressure tracking were modified such that a register use would increment the
1066// pressure of the register class's representative and all of it's super
1067// classes' representatives transitively. We have not implemented this because
1068// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001069// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001070// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001071std::pair<const TargetRegisterClass *, uint8_t>
1072ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1073 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001074 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001075 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001076 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001077 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001078 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001079 // Use DPR as representative register class for all floating point
1080 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1081 // the cost is 1 for both f32 and f64.
1082 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001083 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001084 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001085 // When NEON is used for SP, only half of the register file is available
1086 // because operations that define both SP and DP results will be constrained
1087 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1088 // coalescing by double-counting the SP regs. See the FIXME above.
1089 if (Subtarget->useNEONForSinglePrecisionFP())
1090 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001091 break;
1092 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1093 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001094 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001095 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001096 break;
1097 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001098 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001099 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001100 break;
1101 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001102 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001103 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001104 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001105 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001106 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001107}
1108
Evan Cheng10043e22007-01-19 07:51:42 +00001109const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001110 switch ((ARMISD::NodeType)Opcode) {
1111 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001112 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001113 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001114 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001115 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001116 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001117 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001118 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1119 case ARMISD::tCALL: return "ARMISD::tCALL";
1120 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1121 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001122 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001123 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001124 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001125 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1126 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001127 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001128 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001129 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1130 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001131 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001132 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001133
Evan Cheng10043e22007-01-19 07:51:42 +00001134 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001135
Evan Cheng10043e22007-01-19 07:51:42 +00001136 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1137 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1138 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001139
Evan Chenge8916542011-08-30 01:34:54 +00001140 case ARMISD::ADDC: return "ARMISD::ADDC";
1141 case ARMISD::ADDE: return "ARMISD::ADDE";
1142 case ARMISD::SUBC: return "ARMISD::SUBC";
1143 case ARMISD::SUBE: return "ARMISD::SUBE";
1144
Bob Wilson22806742010-09-22 22:09:21 +00001145 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1146 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001147
Evan Chengec6d7c92009-10-28 06:55:03 +00001148 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001149 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1150 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001151
Dale Johannesend679ff72010-06-03 21:09:53 +00001152 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001153
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001154 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001155
Evan Chengb972e562009-08-07 00:34:42 +00001156 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1157
Bob Wilson7ed59712010-10-30 00:54:37 +00001158 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001159
Evan Cheng8740ee32010-11-03 06:34:55 +00001160 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1161
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001162 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001163 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001164
Bob Wilson2e076c42009-06-22 23:27:02 +00001165 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001166 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001167 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001168 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1169 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001170 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1171 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001172 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1173 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001174 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1175 case ARMISD::VTST: return "ARMISD::VTST";
1176
1177 case ARMISD::VSHL: return "ARMISD::VSHL";
1178 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1179 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001180 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1181 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1182 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1183 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1184 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1185 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1186 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1187 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1188 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1189 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1190 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1191 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001192 case ARMISD::VSLI: return "ARMISD::VSLI";
1193 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001194 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1195 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001196 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001197 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001198 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001199 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001200 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001201 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001202 case ARMISD::VREV64: return "ARMISD::VREV64";
1203 case ARMISD::VREV32: return "ARMISD::VREV32";
1204 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001205 case ARMISD::VZIP: return "ARMISD::VZIP";
1206 case ARMISD::VUZP: return "ARMISD::VUZP";
1207 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001208 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1209 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001210 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1211 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001212 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1213 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001214 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001215 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001216 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1217 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001218 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001219 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001220 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1221 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1222 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001223 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1224 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1225 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1226 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1227 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1228 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1229 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1230 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1231 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1232 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1233 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1234 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1235 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1236 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1237 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1238 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1239 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001240 }
Matthias Braund04893f2015-05-07 21:33:59 +00001241 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001242}
1243
Mehdi Amini44ede332015-07-09 02:09:04 +00001244EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1245 EVT VT) const {
1246 if (!VT.isVector())
1247 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001248 return VT.changeVectorElementTypeToInteger();
1249}
1250
Evan Cheng4cad68e2010-05-15 02:18:07 +00001251/// getRegClassFor - Return the register class that should be used for the
1252/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001253const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001254 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1255 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1256 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001257 if (Subtarget->hasNEON()) {
1258 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001259 return &ARM::QQPRRegClass;
1260 if (VT == MVT::v8i64)
1261 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001262 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001263 return TargetLowering::getRegClassFor(VT);
1264}
1265
John Brawn0dbcd652015-03-18 12:01:59 +00001266// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1267// source/dest is aligned and the copy size is large enough. We therefore want
1268// to align such objects passed to memory intrinsics.
1269bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1270 unsigned &PrefAlign) const {
1271 if (!isa<MemIntrinsic>(CI))
1272 return false;
1273 MinSize = 8;
1274 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1275 // cycle faster than 4-byte aligned LDM.
1276 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1277 return true;
1278}
1279
Eric Christopher84bdfd82010-07-21 22:26:11 +00001280// Create a fast isel object.
1281FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001282ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1283 const TargetLibraryInfo *libInfo) const {
1284 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001285}
1286
Evan Cheng4401f882010-05-20 23:26:43 +00001287Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001288 unsigned NumVals = N->getNumValues();
1289 if (!NumVals)
1290 return Sched::RegPressure;
1291
1292 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001293 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001294 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001295 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001296 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001297 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001298 }
Evan Chengbf914992010-05-28 23:25:23 +00001299
1300 if (!N->isMachineOpcode())
1301 return Sched::RegPressure;
1302
1303 // Load are scheduled for latency even if there instruction itinerary
1304 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001305 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001306 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001307
Evan Cheng6cc775f2011-06-28 19:10:37 +00001308 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001309 return Sched::RegPressure;
1310 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001311 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001312 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001313
Evan Cheng4401f882010-05-20 23:26:43 +00001314 return Sched::RegPressure;
1315}
1316
Evan Cheng10043e22007-01-19 07:51:42 +00001317//===----------------------------------------------------------------------===//
1318// Lowering Code
1319//===----------------------------------------------------------------------===//
1320
Evan Cheng10043e22007-01-19 07:51:42 +00001321/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1322static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1323 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001324 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001325 case ISD::SETNE: return ARMCC::NE;
1326 case ISD::SETEQ: return ARMCC::EQ;
1327 case ISD::SETGT: return ARMCC::GT;
1328 case ISD::SETGE: return ARMCC::GE;
1329 case ISD::SETLT: return ARMCC::LT;
1330 case ISD::SETLE: return ARMCC::LE;
1331 case ISD::SETUGT: return ARMCC::HI;
1332 case ISD::SETUGE: return ARMCC::HS;
1333 case ISD::SETULT: return ARMCC::LO;
1334 case ISD::SETULE: return ARMCC::LS;
1335 }
1336}
1337
Bob Wilsona2e83332009-09-09 23:14:54 +00001338/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1339static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001340 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001341 CondCode2 = ARMCC::AL;
1342 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001343 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001344 case ISD::SETEQ:
1345 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1346 case ISD::SETGT:
1347 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1348 case ISD::SETGE:
1349 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1350 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001351 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001352 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1353 case ISD::SETO: CondCode = ARMCC::VC; break;
1354 case ISD::SETUO: CondCode = ARMCC::VS; break;
1355 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1356 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1357 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1358 case ISD::SETLT:
1359 case ISD::SETULT: CondCode = ARMCC::LT; break;
1360 case ISD::SETLE:
1361 case ISD::SETULE: CondCode = ARMCC::LE; break;
1362 case ISD::SETNE:
1363 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1364 }
Evan Cheng10043e22007-01-19 07:51:42 +00001365}
1366
Bob Wilsona4c22902009-04-17 19:07:39 +00001367//===----------------------------------------------------------------------===//
1368// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001369//===----------------------------------------------------------------------===//
1370
1371#include "ARMGenCallingConv.inc"
1372
Oliver Stannardc24f2172014-05-09 14:01:47 +00001373/// getEffectiveCallingConv - Get the effective calling convention, taking into
1374/// account presence of floating point hardware and calling convention
1375/// limitations, such as support for variadic functions.
1376CallingConv::ID
1377ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1378 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001379 switch (CC) {
1380 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001381 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001382 case CallingConv::ARM_AAPCS:
1383 case CallingConv::ARM_APCS:
1384 case CallingConv::GHC:
1385 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001386 case CallingConv::PreserveMost:
1387 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001388 case CallingConv::ARM_AAPCS_VFP:
1389 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1390 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001391 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001392 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001393 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001394 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1395 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001396 return CallingConv::ARM_AAPCS_VFP;
1397 else
1398 return CallingConv::ARM_AAPCS;
1399 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001400 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001401 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001402 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001403 return CallingConv::Fast;
1404 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001405 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001406 return CallingConv::ARM_AAPCS_VFP;
1407 else
1408 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001409 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001410}
1411
1412/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1413/// CallingConvention.
1414CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1415 bool Return,
1416 bool isVarArg) const {
1417 switch (getEffectiveCallingConv(CC, isVarArg)) {
1418 default:
1419 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001420 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001421 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001422 case CallingConv::ARM_AAPCS:
1423 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1424 case CallingConv::ARM_AAPCS_VFP:
1425 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1426 case CallingConv::Fast:
1427 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001428 case CallingConv::GHC:
1429 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001430 case CallingConv::PreserveMost:
1431 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001432 }
1433}
1434
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001435/// LowerCallResult - Lower the result values of a call into the
1436/// appropriate copies out of appropriate physical registers.
1437SDValue
1438ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001439 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001440 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001441 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001442 SmallVectorImpl<SDValue> &InVals,
1443 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001444
Bob Wilsona4c22902009-04-17 19:07:39 +00001445 // Assign locations to each value returned by this call.
1446 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001447 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1448 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001449 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001450 CCAssignFnForNode(CallConv, /* Return*/ true,
1451 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001452
1453 // Copy all of the result registers out of their specified physreg.
1454 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1455 CCValAssign VA = RVLocs[i];
1456
Stephen Linb8bd2322013-04-20 05:14:40 +00001457 // Pass 'this' value directly from the argument to return value, to avoid
1458 // reg unit interference
1459 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001460 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1461 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001462 InVals.push_back(ThisVal);
1463 continue;
1464 }
1465
Bob Wilson0041bd32009-04-25 00:33:20 +00001466 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001467 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001468 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001469 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001470 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001471 Chain = Lo.getValue(1);
1472 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001473 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001474 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001475 InFlag);
1476 Chain = Hi.getValue(1);
1477 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001478 if (!Subtarget->isLittle())
1479 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001480 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001481
Owen Anderson9f944592009-08-11 20:47:22 +00001482 if (VA.getLocVT() == MVT::v2f64) {
1483 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1484 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001485 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001486
1487 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001488 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001489 Chain = Lo.getValue(1);
1490 InFlag = Lo.getValue(2);
1491 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001492 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001493 Chain = Hi.getValue(1);
1494 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001495 if (!Subtarget->isLittle())
1496 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001497 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001498 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001499 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001500 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001501 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001502 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1503 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001504 Chain = Val.getValue(1);
1505 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001506 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001507
1508 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001509 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001510 case CCValAssign::Full: break;
1511 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001512 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001513 break;
1514 }
1515
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001516 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001517 }
1518
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001519 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001520}
1521
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001522/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001523SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001524ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1525 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001526 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001527 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001528 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001529 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001530 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001531 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1532 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001533 return DAG.getStore(
1534 Chain, dl, Arg, PtrOff,
1535 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
1536 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001537}
1538
Andrew Trickef9de2a2013-05-25 02:42:55 +00001539void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001540 SDValue Chain, SDValue &Arg,
1541 RegsToPassVector &RegsToPass,
1542 CCValAssign &VA, CCValAssign &NextVA,
1543 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001544 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001545 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001546
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001547 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001548 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001549 unsigned id = Subtarget->isLittle() ? 0 : 1;
1550 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001551
1552 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001553 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001554 else {
1555 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001556 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001557 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1558 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001559
Christian Pirkerb5728192014-05-08 14:06:24 +00001560 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001561 dl, DAG, NextVA,
1562 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001563 }
1564}
1565
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001566/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001567/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1568/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001569SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001570ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001571 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001572 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001573 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001574 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1575 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1576 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001577 SDValue Chain = CLI.Chain;
1578 SDValue Callee = CLI.Callee;
1579 bool &isTailCall = CLI.IsTailCall;
1580 CallingConv::ID CallConv = CLI.CallConv;
1581 bool doesNotRet = CLI.DoesNotReturn;
1582 bool isVarArg = CLI.IsVarArg;
1583
Dale Johannesend679ff72010-06-03 21:09:53 +00001584 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001585 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1586 bool isThisReturn = false;
1587 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001588 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001589
Bob Wilson8decdc42011-10-07 17:17:49 +00001590 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001591 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001592 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001593
Dale Johannesend679ff72010-06-03 21:09:53 +00001594 if (isTailCall) {
1595 // Check if it's really possible to do a tail call.
1596 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001597 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001598 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001599 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1600 report_fatal_error("failed to perform tail call elimination on a call "
1601 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001602 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1603 // detected sibcalls.
1604 if (isTailCall) {
1605 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001606 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001607 }
1608 }
Evan Cheng10043e22007-01-19 07:51:42 +00001609
Bob Wilsona4c22902009-04-17 19:07:39 +00001610 // Analyze operands of the call, assigning locations to each operand.
1611 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001612 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1613 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001614 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001615 CCAssignFnForNode(CallConv, /* Return*/ false,
1616 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001617
Bob Wilsona4c22902009-04-17 19:07:39 +00001618 // Get a count of how many bytes are to be pushed on the stack.
1619 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001620
Dale Johannesend679ff72010-06-03 21:09:53 +00001621 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001622 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001623 NumBytes = 0;
1624
Evan Cheng10043e22007-01-19 07:51:42 +00001625 // Adjust the stack pointer for the new arguments...
1626 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001627 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001628 Chain = DAG.getCALLSEQ_START(Chain,
1629 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001630
Mehdi Amini44ede332015-07-09 02:09:04 +00001631 SDValue StackPtr =
1632 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001633
Bob Wilson2e076c42009-06-22 23:27:02 +00001634 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001635 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001636
Bob Wilsona4c22902009-04-17 19:07:39 +00001637 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001638 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001639 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1640 i != e;
1641 ++i, ++realArgIdx) {
1642 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001643 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001644 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001645 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001646
Bob Wilsona4c22902009-04-17 19:07:39 +00001647 // Promote the value if needed.
1648 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001649 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001650 case CCValAssign::Full: break;
1651 case CCValAssign::SExt:
1652 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1653 break;
1654 case CCValAssign::ZExt:
1655 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1656 break;
1657 case CCValAssign::AExt:
1658 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1659 break;
1660 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001661 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001662 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001663 }
1664
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001665 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001666 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001667 if (VA.getLocVT() == MVT::v2f64) {
1668 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001669 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001670 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001671 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001672
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001673 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001674 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1675
1676 VA = ArgLocs[++i]; // skip ahead to next loc
1677 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001678 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001679 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1680 } else {
1681 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001682
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001683 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1684 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001685 }
1686 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001687 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001688 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001689 }
1690 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001691 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1692 assert(VA.getLocVT() == MVT::i32 &&
1693 "unexpected calling convention register assignment");
1694 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001695 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001696 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001697 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001698 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001699 } else if (isByVal) {
1700 assert(VA.isMemLoc());
1701 unsigned offset = 0;
1702
1703 // True if this byval aggregate will be split between registers
1704 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001705 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001706 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001707
1708 if (CurByValIdx < ByValArgsCount) {
1709
1710 unsigned RegBegin, RegEnd;
1711 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1712
Mehdi Amini44ede332015-07-09 02:09:04 +00001713 EVT PtrVT =
1714 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001715 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001716 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001717 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001718 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1719 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1720 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001721 false, false, false,
1722 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001723 MemOpChains.push_back(Load.getValue(1));
1724 RegsToPass.push_back(std::make_pair(j, Load));
1725 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001726
1727 // If parameter size outsides register area, "offset" value
1728 // helps us to calculate stack slot for remained part properly.
1729 offset = RegEnd - RegBegin;
1730
1731 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001732 }
1733
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001734 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001735 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001736 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001737 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001738 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001739 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001740 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001741 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001742 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001743 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1744 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001745
Manman Ren9f911162012-06-01 02:44:42 +00001746 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001747 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001748 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001749 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001750 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001751 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001752 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001753
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001754 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1755 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001756 }
Evan Cheng10043e22007-01-19 07:51:42 +00001757 }
1758
1759 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001760 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001761
1762 // Build a sequence of copy-to-reg nodes chained together with token chain
1763 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001764 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001765 // Tail call byval lowering might overwrite argument registers so in case of
1766 // tail call optimization the copies to registers are lowered later.
1767 if (!isTailCall)
1768 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1769 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1770 RegsToPass[i].second, InFlag);
1771 InFlag = Chain.getValue(1);
1772 }
Evan Cheng10043e22007-01-19 07:51:42 +00001773
Dale Johannesend679ff72010-06-03 21:09:53 +00001774 // For tail calls lower the arguments to the 'real' stack slot.
1775 if (isTailCall) {
1776 // Force all the incoming stack arguments to be loaded from the stack
1777 // before any new outgoing arguments are stored to the stack, because the
1778 // outgoing stack slots may alias the incoming argument stack slots, and
1779 // the alias isn't otherwise explicit. This is slightly more conservative
1780 // than necessary, because it means that each store effectively depends
1781 // on every argument instead of just those arguments it would clobber.
1782
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001783 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001784 InFlag = SDValue();
1785 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1786 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1787 RegsToPass[i].second, InFlag);
1788 InFlag = Chain.getValue(1);
1789 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001790 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001791 }
1792
Bill Wendling24c79f22008-09-16 21:48:12 +00001793 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1794 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1795 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001796 bool isDirect = false;
1797 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001798 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001799 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001800 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001801
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001802 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001803 assert((Subtarget->isTargetWindows() ||
1804 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1805 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001806 // Handle a global address or an external symbol. If it's not one of
1807 // those, the target's already in a register, so we don't need to do
1808 // anything extra.
1809 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001810 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001811 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001812 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001813 ARMConstantPoolValue *CPV =
1814 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1815
Jim Grosbach32bb3622010-04-14 22:28:31 +00001816 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001817 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001818 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001819 Callee = DAG.getLoad(
1820 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1821 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1822 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001823 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1824 const char *Sym = S->getSymbol();
1825
1826 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001827 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001828 ARMConstantPoolValue *CPV =
1829 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1830 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001831 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001832 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001833 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001834 Callee = DAG.getLoad(
1835 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1836 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1837 false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001838 }
1839 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001840 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001841 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001842 bool isDef = GV->isStrongDefinitionForLinker();
1843 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001844 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001845 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001846 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001847 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001848 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001849 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001850 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001851 Callee = DAG.getNode(
1852 ARMISD::WrapperPIC, dl, PtrVt,
1853 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1854 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Alex Lorenze40c8a22015-08-11 23:09:45 +00001855 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1856 false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001857 } else if (Subtarget->isTargetCOFF()) {
1858 assert(Subtarget->isTargetWindows() &&
1859 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001860 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1861 ? ARMII::MO_DLLIMPORT
1862 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001863 Callee =
1864 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001865 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001866 Callee =
1867 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1868 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Alex Lorenze40c8a22015-08-11 23:09:45 +00001869 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
1870 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001871 } else {
1872 // On ELF targets for PIC code, direct calls should go through the PLT
1873 unsigned OpFlags = 0;
1874 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001875 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001876 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001877 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001878 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001879 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001880 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001881 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001882 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001883 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001884 // tBX takes a register source operand.
1885 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001886 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001887 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001888 ARMConstantPoolValue *CPV =
1889 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1890 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001891 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001892 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001893 Callee = DAG.getLoad(
1894 PtrVt, dl, DAG.getEntryNode(), CPAddr,
1895 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
1896 false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001897 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001898 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001899 } else {
1900 unsigned OpFlags = 0;
1901 // On ELF targets for PIC code, direct calls should go through the PLT
1902 if (Subtarget->isTargetELF() &&
1903 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1904 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001905 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001906 }
Evan Cheng10043e22007-01-19 07:51:42 +00001907 }
1908
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001909 // FIXME: handle tail calls differently.
1910 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001911 if (Subtarget->isThumb()) {
1912 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001913 CallOpc = ARMISD::CALL_NOLINK;
1914 else
1915 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1916 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001917 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001918 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001919 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00001920 // Emit regular call when code size is the priority
1921 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00001922 // "mov lr, pc; b _foo" to avoid confusing the RSP
1923 CallOpc = ARMISD::CALL_NOLINK;
1924 else
1925 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001926 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001927
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001928 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001929 Ops.push_back(Chain);
1930 Ops.push_back(Callee);
1931
1932 // Add argument registers to the end of the list so that they are known live
1933 // into the call.
1934 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1935 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1936 RegsToPass[i].second.getValueType()));
1937
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001938 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001939 if (!isTailCall) {
1940 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001941 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001942 if (isThisReturn) {
1943 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001944 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001945 if (!Mask) {
1946 // Set isThisReturn to false if the calling convention is not one that
1947 // allows 'returned' to be modeled in this way, so LowerCallResult does
1948 // not try to pass 'this' straight through
1949 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001950 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001951 }
1952 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001953 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001954
Matthias Braunc22630e2013-10-04 16:52:54 +00001955 assert(Mask && "Missing call preserved mask for calling convention");
1956 Ops.push_back(DAG.getRegisterMask(Mask));
1957 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001958
Gabor Greiff304a7a2008-08-28 21:40:38 +00001959 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001960 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001961
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001962 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001963 if (isTailCall) {
1964 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001965 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001966 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001967
Duncan Sands739a0542008-07-02 17:40:58 +00001968 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001969 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001970 InFlag = Chain.getValue(1);
1971
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001972 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1973 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001974 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001975 InFlag = Chain.getValue(1);
1976
Bob Wilsona4c22902009-04-17 19:07:39 +00001977 // Handle result values, copying them out of physregs into vregs that we
1978 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001979 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001980 InVals, isThisReturn,
1981 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001982}
1983
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001984/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001985/// on the stack. Remember the next parameter register to allocate,
1986/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001987/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001988void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1989 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001990 assert((State->getCallOrPrologue() == Prologue ||
1991 State->getCallOrPrologue() == Call) &&
1992 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001993
Tim Northover8cda34f2015-03-11 18:54:22 +00001994 // Byval (as with any stack) slots are always at least 4 byte aligned.
1995 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001996
Tim Northover8cda34f2015-03-11 18:54:22 +00001997 unsigned Reg = State->AllocateReg(GPRArgRegs);
1998 if (!Reg)
1999 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002000
Tim Northover8cda34f2015-03-11 18:54:22 +00002001 unsigned AlignInRegs = Align / 4;
2002 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2003 for (unsigned i = 0; i < Waste; ++i)
2004 Reg = State->AllocateReg(GPRArgRegs);
2005
2006 if (!Reg)
2007 return;
2008
2009 unsigned Excess = 4 * (ARM::R4 - Reg);
2010
2011 // Special case when NSAA != SP and parameter size greater than size of
2012 // all remained GPR regs. In that case we can't split parameter, we must
2013 // send it to stack. We also must set NCRN to R4, so waste all
2014 // remained registers.
2015 const unsigned NSAAOffset = State->getNextStackOffset();
2016 if (NSAAOffset != 0 && Size > Excess) {
2017 while (State->AllocateReg(GPRArgRegs))
2018 ;
2019 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002020 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002021
2022 // First register for byval parameter is the first register that wasn't
2023 // allocated before this method call, so it would be "reg".
2024 // If parameter is small enough to be saved in range [reg, r4), then
2025 // the end (first after last) register would be reg + param-size-in-regs,
2026 // else parameter would be splitted between registers and stack,
2027 // end register would be r4 in this case.
2028 unsigned ByValRegBegin = Reg;
2029 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2030 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2031 // Note, first register is allocated in the beginning of function already,
2032 // allocate remained amount of registers we need.
2033 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2034 State->AllocateReg(GPRArgRegs);
2035 // A byval parameter that is split between registers and memory needs its
2036 // size truncated here.
2037 // In the case where the entire structure fits in registers, we set the
2038 // size in memory to zero.
2039 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002040}
2041
Dale Johannesend679ff72010-06-03 21:09:53 +00002042/// MatchingStackOffset - Return true if the given stack call argument is
2043/// already available in the same position (relatively) of the caller's
2044/// incoming argument stack.
2045static
2046bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2047 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002048 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002049 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2050 int FI = INT_MAX;
2051 if (Arg.getOpcode() == ISD::CopyFromReg) {
2052 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002053 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002054 return false;
2055 MachineInstr *Def = MRI->getVRegDef(VR);
2056 if (!Def)
2057 return false;
2058 if (!Flags.isByVal()) {
2059 if (!TII->isLoadFromStackSlot(Def, FI))
2060 return false;
2061 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002062 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002063 }
2064 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2065 if (Flags.isByVal())
2066 // ByVal argument is passed in as a pointer but it's now being
2067 // dereferenced. e.g.
2068 // define @foo(%struct.X* %A) {
2069 // tail call @bar(%struct.X* byval %A)
2070 // }
2071 return false;
2072 SDValue Ptr = Ld->getBasePtr();
2073 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2074 if (!FINode)
2075 return false;
2076 FI = FINode->getIndex();
2077 } else
2078 return false;
2079
2080 assert(FI != INT_MAX);
2081 if (!MFI->isFixedObjectIndex(FI))
2082 return false;
2083 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2084}
2085
2086/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2087/// for tail call optimization. Targets which want to do tail call
2088/// optimization should implement this function.
2089bool
2090ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2091 CallingConv::ID CalleeCC,
2092 bool isVarArg,
2093 bool isCalleeStructRet,
2094 bool isCallerStructRet,
2095 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002096 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002097 const SmallVectorImpl<ISD::InputArg> &Ins,
2098 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002099 const Function *CallerF = DAG.getMachineFunction().getFunction();
2100 CallingConv::ID CallerCC = CallerF->getCallingConv();
2101 bool CCMatch = CallerCC == CalleeCC;
2102
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002103 assert(Subtarget->supportsTailCall());
2104
Dale Johannesend679ff72010-06-03 21:09:53 +00002105 // Look for obvious safe cases to perform tail call optimization that do not
2106 // require ABI changes. This is what gcc calls sibcall.
2107
Jim Grosbache3864cc2010-06-16 23:45:49 +00002108 // Do not sibcall optimize vararg calls unless the call site is not passing
2109 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002110 if (isVarArg && !Outs.empty())
2111 return false;
2112
Tim Northoverd8407452013-10-01 14:33:28 +00002113 // Exception-handling functions need a special set of instructions to indicate
2114 // a return to the hardware. Tail-calling another function would probably
2115 // break this.
2116 if (CallerF->hasFnAttribute("interrupt"))
2117 return false;
2118
Dale Johannesend679ff72010-06-03 21:09:53 +00002119 // Also avoid sibcall optimization if either caller or callee uses struct
2120 // return semantics.
2121 if (isCalleeStructRet || isCallerStructRet)
2122 return false;
2123
Oliver Stannard12993dd2014-08-18 12:42:15 +00002124 // Externally-defined functions with weak linkage should not be
2125 // tail-called on ARM when the OS does not support dynamic
2126 // pre-emption of symbols, as the AAELF spec requires normal calls
2127 // to undefined weak functions to be replaced with a NOP or jump to the
2128 // next instruction. The behaviour of branch instructions in this
2129 // situation (as used for tail calls) is implementation-defined, so we
2130 // cannot rely on the linker replacing the tail call with a return.
2131 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2132 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002133 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002134 if (GV->hasExternalWeakLinkage() &&
2135 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002136 return false;
2137 }
2138
Dale Johannesend679ff72010-06-03 21:09:53 +00002139 // If the calling conventions do not match, then we'd better make sure the
2140 // results are returned in the same way as what the caller expects.
2141 if (!CCMatch) {
2142 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002143 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2144 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002145 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2146
2147 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002148 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2149 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002150 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2151
2152 if (RVLocs1.size() != RVLocs2.size())
2153 return false;
2154 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2155 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2156 return false;
2157 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2158 return false;
2159 if (RVLocs1[i].isRegLoc()) {
2160 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2161 return false;
2162 } else {
2163 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2164 return false;
2165 }
2166 }
2167 }
2168
Manman Ren7e48b252012-10-12 23:39:43 +00002169 // If Caller's vararg or byval argument has been split between registers and
2170 // stack, do not perform tail call, since part of the argument is in caller's
2171 // local frame.
2172 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2173 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002174 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002175 return false;
2176
Dale Johannesend679ff72010-06-03 21:09:53 +00002177 // If the callee takes no arguments then go on to check the results of the
2178 // call.
2179 if (!Outs.empty()) {
2180 // Check if stack adjustment is needed. For now, do not do this if any
2181 // argument is passed on the stack.
2182 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002183 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2184 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002185 CCInfo.AnalyzeCallOperands(Outs,
2186 CCAssignFnForNode(CalleeCC, false, isVarArg));
2187 if (CCInfo.getNextStackOffset()) {
2188 MachineFunction &MF = DAG.getMachineFunction();
2189
2190 // Check if the arguments are already laid out in the right way as
2191 // the caller's fixed stack objects.
2192 MachineFrameInfo *MFI = MF.getFrameInfo();
2193 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002194 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002195 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2196 i != e;
2197 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002198 CCValAssign &VA = ArgLocs[i];
2199 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002200 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002201 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002202 if (VA.getLocInfo() == CCValAssign::Indirect)
2203 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002204 if (VA.needsCustom()) {
2205 // f64 and vector types are split into multiple registers or
2206 // register/stack-slot combinations. The types will not match
2207 // the registers; give up on memory f64 refs until we figure
2208 // out what to do about this.
2209 if (!VA.isRegLoc())
2210 return false;
2211 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002212 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002213 if (RegVT == MVT::v2f64) {
2214 if (!ArgLocs[++i].isRegLoc())
2215 return false;
2216 if (!ArgLocs[++i].isRegLoc())
2217 return false;
2218 }
2219 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002220 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2221 MFI, MRI, TII))
2222 return false;
2223 }
2224 }
2225 }
2226 }
2227
2228 return true;
2229}
2230
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002231bool
2232ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2233 MachineFunction &MF, bool isVarArg,
2234 const SmallVectorImpl<ISD::OutputArg> &Outs,
2235 LLVMContext &Context) const {
2236 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002237 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002238 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2239 isVarArg));
2240}
2241
Tim Northoverd8407452013-10-01 14:33:28 +00002242static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2243 SDLoc DL, SelectionDAG &DAG) {
2244 const MachineFunction &MF = DAG.getMachineFunction();
2245 const Function *F = MF.getFunction();
2246
2247 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2248
2249 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2250 // version of the "preferred return address". These offsets affect the return
2251 // instruction if this is a return from PL1 without hypervisor extensions.
2252 // IRQ/FIQ: +4 "subs pc, lr, #4"
2253 // SWI: 0 "subs pc, lr, #0"
2254 // ABORT: +4 "subs pc, lr, #4"
2255 // UNDEF: +4/+2 "subs pc, lr, #0"
2256 // UNDEF varies depending on where the exception came from ARM or Thumb
2257 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2258
2259 int64_t LROffset;
2260 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2261 IntKind == "ABORT")
2262 LROffset = 4;
2263 else if (IntKind == "SWI" || IntKind == "UNDEF")
2264 LROffset = 0;
2265 else
2266 report_fatal_error("Unsupported interrupt attribute. If present, value "
2267 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2268
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002269 RetOps.insert(RetOps.begin() + 1,
2270 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002271
Craig Topper48d114b2014-04-26 18:35:24 +00002272 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002273}
2274
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002275SDValue
2276ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002277 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002278 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002279 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002280 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002281
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002282 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002283 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002284
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002285 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002286 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2287 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002288
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002289 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002290 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2291 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002292
Bob Wilsona4c22902009-04-17 19:07:39 +00002293 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002294 SmallVector<SDValue, 4> RetOps;
2295 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002296 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002297
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002298 MachineFunction &MF = DAG.getMachineFunction();
2299 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2300 AFI->setReturnRegsCount(RVLocs.size());
2301
Bob Wilsona4c22902009-04-17 19:07:39 +00002302 // Copy the result values into the output registers.
2303 for (unsigned i = 0, realRVLocIdx = 0;
2304 i != RVLocs.size();
2305 ++i, ++realRVLocIdx) {
2306 CCValAssign &VA = RVLocs[i];
2307 assert(VA.isRegLoc() && "Can only return in registers!");
2308
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002309 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002310
2311 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002312 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002313 case CCValAssign::Full: break;
2314 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002315 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002316 break;
2317 }
2318
Bob Wilsona4c22902009-04-17 19:07:39 +00002319 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002320 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002321 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002322 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002323 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002324 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002325 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002326
Christian Pirkerb5728192014-05-08 14:06:24 +00002327 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2328 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2329 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002330 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002331 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002332 VA = RVLocs[++i]; // skip ahead to next loc
2333 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002334 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2335 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002336 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002337 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002338 VA = RVLocs[++i]; // skip ahead to next loc
2339
2340 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002341 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002342 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002343 }
2344 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2345 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002346 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002347 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002348 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2349 fmrrd.getValue(isLittleEndian ? 0 : 1),
2350 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002351 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002352 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002353 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002354 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2355 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002356 Flag);
2357 } else
2358 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2359
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002360 // Guarantee that all emitted copies are
2361 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002362 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002363 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002364 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002365 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2366 const MCPhysReg *I =
2367 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2368 if (I) {
2369 for (; *I; ++I) {
2370 if (ARM::GPRRegClass.contains(*I))
2371 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2372 else if (ARM::DPRRegClass.contains(*I))
2373 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2374 else
2375 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2376 }
2377 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002378
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002379 // Update chain and glue.
2380 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002381 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002382 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002383
Tim Northoverd8407452013-10-01 14:33:28 +00002384 // CPUs which aren't M-class use a special sequence to return from
2385 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2386 // though we use "subs pc, lr, #N").
2387 //
2388 // M-class CPUs actually use a normal return sequence with a special
2389 // (hardware-provided) value in LR, so the normal code path works.
2390 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2391 !Subtarget->isMClass()) {
2392 if (Subtarget->isThumb1Only())
2393 report_fatal_error("interrupt attribute is not supported in Thumb1");
2394 return LowerInterruptReturn(RetOps, dl, DAG);
2395 }
2396
Craig Topper48d114b2014-04-26 18:35:24 +00002397 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002398}
2399
Evan Chengf8bad082012-04-10 01:51:00 +00002400bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002401 if (N->getNumValues() != 1)
2402 return false;
2403 if (!N->hasNUsesOfValue(1, 0))
2404 return false;
2405
Evan Chengf8bad082012-04-10 01:51:00 +00002406 SDValue TCChain = Chain;
2407 SDNode *Copy = *N->use_begin();
2408 if (Copy->getOpcode() == ISD::CopyToReg) {
2409 // If the copy has a glue operand, we conservatively assume it isn't safe to
2410 // perform a tail call.
2411 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2412 return false;
2413 TCChain = Copy->getOperand(0);
2414 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2415 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002416 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002417 SmallPtrSet<SDNode*, 2> Copies;
2418 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002419 UI != UE; ++UI) {
2420 if (UI->getOpcode() != ISD::CopyToReg)
2421 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002422 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002423 }
Evan Chengf8bad082012-04-10 01:51:00 +00002424 if (Copies.size() > 2)
2425 return false;
2426
2427 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2428 UI != UE; ++UI) {
2429 SDValue UseChain = UI->getOperand(0);
2430 if (Copies.count(UseChain.getNode()))
2431 // Second CopyToReg
2432 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002433 else {
2434 // We are at the top of this chain.
2435 // If the copy has a glue operand, we conservatively assume it
2436 // isn't safe to perform a tail call.
2437 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2438 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002439 // First CopyToReg
2440 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002441 }
Evan Chengf8bad082012-04-10 01:51:00 +00002442 }
2443 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002444 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002445 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002446 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002447 Copy = *Copy->use_begin();
2448 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002449 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002450 // If the copy has a glue operand, we conservatively assume it isn't safe to
2451 // perform a tail call.
2452 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2453 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002454 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002455 } else {
2456 return false;
2457 }
2458
Evan Cheng419ea282010-12-01 22:59:46 +00002459 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002460 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2461 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002462 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2463 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002464 return false;
2465 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002466 }
2467
Evan Chengf8bad082012-04-10 01:51:00 +00002468 if (!HasRet)
2469 return false;
2470
2471 Chain = TCChain;
2472 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002473}
2474
Evan Cheng0663f232011-03-21 01:19:09 +00002475bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002476 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002477 return false;
2478
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002479 auto Attr =
2480 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2481 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002482 return false;
2483
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002484 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002485}
2486
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002487// Trying to write a 64 bit value so need to split into two 32 bit values first,
2488// and pass the lower and high parts through.
2489static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2490 SDLoc DL(Op);
2491 SDValue WriteValue = Op->getOperand(2);
2492
2493 // This function is only supposed to be called for i64 type argument.
2494 assert(WriteValue.getValueType() == MVT::i64
2495 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2496
2497 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2498 DAG.getConstant(0, DL, MVT::i32));
2499 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2500 DAG.getConstant(1, DL, MVT::i32));
2501 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2502 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2503}
2504
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002505// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2506// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2507// one of the above mentioned nodes. It has to be wrapped because otherwise
2508// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2509// be used to form addressing mode. These wrapped nodes will be selected
2510// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002511static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002512 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002513 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002514 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002515 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002516 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002517 if (CP->isMachineConstantPoolEntry())
2518 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2519 CP->getAlignment());
2520 else
2521 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2522 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002523 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002524}
2525
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002526unsigned ARMTargetLowering::getJumpTableEncoding() const {
2527 return MachineJumpTableInfo::EK_Inline;
2528}
2529
Dan Gohman21cea8a2010-04-17 15:26:15 +00002530SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2531 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002532 MachineFunction &MF = DAG.getMachineFunction();
2533 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2534 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002535 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002536 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002537 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002538 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2539 SDValue CPAddr;
2540 if (RelocM == Reloc::Static) {
2541 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2542 } else {
2543 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002544 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002545 ARMConstantPoolValue *CPV =
2546 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2547 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002548 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2549 }
2550 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002551 SDValue Result =
2552 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
2553 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2554 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002555 if (RelocM == Reloc::Static)
2556 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002557 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002558 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002559}
2560
Tim Northoverbd41cf82016-01-07 09:03:03 +00002561/// \brief Convert a TLS address reference into the correct sequence of loads
2562/// and calls to compute the variable's address for Darwin, and return an
2563/// SDValue containing the final node.
2564
2565/// Darwin only has one TLS scheme which must be capable of dealing with the
2566/// fully general situation, in the worst case. This means:
2567/// + "extern __thread" declaration.
2568/// + Defined in a possibly unknown dynamic library.
2569///
2570/// The general system is that each __thread variable has a [3 x i32] descriptor
2571/// which contains information used by the runtime to calculate the address. The
2572/// only part of this the compiler needs to know about is the first word, which
2573/// contains a function pointer that must be called with the address of the
2574/// entire descriptor in "r0".
2575///
2576/// Since this descriptor may be in a different unit, in general access must
2577/// proceed along the usual ARM rules. A common sequence to produce is:
2578///
2579/// movw rT1, :lower16:_var$non_lazy_ptr
2580/// movt rT1, :upper16:_var$non_lazy_ptr
2581/// ldr r0, [rT1]
2582/// ldr rT2, [r0]
2583/// blx rT2
2584/// [...address now in r0...]
2585SDValue
2586ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2587 SelectionDAG &DAG) const {
2588 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2589 SDLoc DL(Op);
2590
2591 // First step is to get the address of the actua global symbol. This is where
2592 // the TLS descriptor lives.
2593 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2594
2595 // The first entry in the descriptor is a function pointer that we must call
2596 // to obtain the address of the variable.
2597 SDValue Chain = DAG.getEntryNode();
2598 SDValue FuncTLVGet =
2599 DAG.getLoad(MVT::i32, DL, Chain, DescAddr,
2600 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2601 false, true, true, 4);
2602 Chain = FuncTLVGet.getValue(1);
2603
2604 MachineFunction &F = DAG.getMachineFunction();
2605 MachineFrameInfo *MFI = F.getFrameInfo();
2606 MFI->setAdjustsStack(true);
2607
2608 // TLS calls preserve all registers except those that absolutely must be
2609 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2610 // silly).
2611 auto TRI =
2612 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2613 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2614 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2615
2616 // Finally, we can make the call. This is just a degenerate version of a
2617 // normal AArch64 call node: r0 takes the address of the descriptor, and
2618 // returns the address of the variable in this thread.
2619 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2620 Chain =
2621 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2622 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2623 DAG.getRegisterMask(Mask), Chain.getValue(1));
2624 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2625}
2626
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002627SDValue
2628ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2629 SelectionDAG &DAG) const {
2630 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
2631 SDValue Chain = DAG.getEntryNode();
2632 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2633 SDLoc DL(Op);
2634
2635 // Load the current TEB (thread environment block)
2636 SDValue Ops[] = {Chain,
2637 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2638 DAG.getConstant(15, DL, MVT::i32),
2639 DAG.getConstant(0, DL, MVT::i32),
2640 DAG.getConstant(13, DL, MVT::i32),
2641 DAG.getConstant(0, DL, MVT::i32),
2642 DAG.getConstant(2, DL, MVT::i32)};
2643 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2644 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2645
2646 SDValue TEB = CurrentTEB.getValue(0);
2647 Chain = CurrentTEB.getValue(1);
2648
2649 // Load the ThreadLocalStoragePointer from the TEB
2650 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2651 SDValue TLSArray =
2652 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
2653 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo(),
2654 false, false, false, 0);
2655
2656 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2657 // offset into the TLSArray.
2658
2659 // Load the TLS index from the C runtime
2660 SDValue TLSIndex =
2661 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2662 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
2663 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo(),
2664 false, false, false, 0);
2665
2666 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2667 DAG.getConstant(2, DL, MVT::i32));
2668 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2669 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
2670 MachinePointerInfo(), false, false, false, 0);
2671
2672 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS,
2673 LowerGlobalAddressWindows(Op, DAG));
2674}
2675
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002676// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002677SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002678ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002679 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002680 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002681 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002682 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002683 MachineFunction &MF = DAG.getMachineFunction();
2684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002685 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002686 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002687 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2688 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002689 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002690 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002691 Argument =
2692 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
2693 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2694 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002695 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002696
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002697 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002698 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002699
2700 // call __tls_get_addr.
2701 ArgListTy Args;
2702 ArgListEntry Entry;
2703 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002704 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002705 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002706
Dale Johannesen555a3752009-01-30 23:10:59 +00002707 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002708 TargetLowering::CallLoweringInfo CLI(DAG);
2709 CLI.setDebugLoc(dl).setChain(Chain)
2710 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002711 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2712 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002713
Justin Holewinskiaa583972012-05-25 16:35:28 +00002714 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002715 return CallResult.first;
2716}
2717
2718// Lower ISD::GlobalTLSAddress using the "initial exec" or
2719// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002720SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002721ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002722 SelectionDAG &DAG,
2723 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002724 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002725 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002726 SDValue Offset;
2727 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002728 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002729 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002730 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002731
Hans Wennborgaea41202012-05-04 09:40:39 +00002732 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002733 MachineFunction &MF = DAG.getMachineFunction();
2734 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002735 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002736 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002737 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2738 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002739 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2740 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2741 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002742 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002743 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002744 Offset = DAG.getLoad(
2745 PtrVT, dl, Chain, Offset,
2746 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2747 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002748 Chain = Offset.getValue(1);
2749
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002750 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002751 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002752
Alex Lorenze40c8a22015-08-11 23:09:45 +00002753 Offset = DAG.getLoad(
2754 PtrVT, dl, Chain, Offset,
2755 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2756 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002757 } else {
2758 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002759 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002760 ARMConstantPoolValue *CPV =
2761 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002762 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002763 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002764 Offset = DAG.getLoad(
2765 PtrVT, dl, Chain, Offset,
2766 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2767 false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002768 }
2769
2770 // The address of the thread local variable is the add of the thread
2771 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002772 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002773}
2774
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002775SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002776ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002777 if (Subtarget->isTargetDarwin())
2778 return LowerGlobalTLSAddressDarwin(Op, DAG);
2779
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002780 if (Subtarget->isTargetWindows())
2781 return LowerGlobalTLSAddressWindows(Op, DAG);
2782
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002783 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002784 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002785 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002786 if (DAG.getTarget().Options.EmulatedTLS)
2787 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002788
2789 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2790
2791 switch (model) {
2792 case TLSModel::GeneralDynamic:
2793 case TLSModel::LocalDynamic:
2794 return LowerToTLSGeneralDynamicModel(GA, DAG);
2795 case TLSModel::InitialExec:
2796 case TLSModel::LocalExec:
2797 return LowerToTLSExecModels(GA, DAG, model);
2798 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002799 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002800}
2801
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002802SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002803 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002804 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002805 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002806 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002807 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Peter Collingbourne97aae402015-10-26 18:23:16 +00002808 bool UseGOT_PREL =
Peter Collingbourne99fac802015-10-26 20:46:44 +00002809 !(GV->hasHiddenVisibility() || GV->hasLocalLinkage());
Peter Collingbourne97aae402015-10-26 18:23:16 +00002810
2811 MachineFunction &MF = DAG.getMachineFunction();
2812 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2813 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
2814 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2815 SDLoc dl(Op);
2816 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
2817 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
2818 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
2819 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
2820 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002821 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002822 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002823 SDValue Result = DAG.getLoad(
2824 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2825 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2826 false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002827 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00002828 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
2829 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2830 if (UseGOT_PREL)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002831 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002832 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002833 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002834 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002835 }
2836
2837 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002838 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002839 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002840 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002841 // FIXME: Once remat is capable of dealing with instructions with register
2842 // operands, expand this into two nodes.
2843 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2844 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002845 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002846 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2847 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002848 return DAG.getLoad(
2849 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2850 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2851 false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002852 }
2853}
2854
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002855SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002856 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002857 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002858 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002859 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002860 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002861
Eric Christopherc1058df2014-07-04 01:55:26 +00002862 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002863 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002864
Tim Northover72360d22013-12-02 10:35:41 +00002865 // FIXME: Once remat is capable of dealing with instructions with register
2866 // operands, expand this into multiple nodes
2867 unsigned Wrapper =
2868 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002869
Tim Northover72360d22013-12-02 10:35:41 +00002870 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2871 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002872
Evan Cheng1b389522009-09-03 07:04:02 +00002873 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002874 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002875 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2876 false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002877 return Result;
2878}
2879
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002880SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2881 SelectionDAG &DAG) const {
2882 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002883 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2884 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002885
2886 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002887 const ARMII::TOF TargetFlags =
2888 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002889 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002890 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002891 SDLoc DL(Op);
2892
2893 ++NumMovwMovt;
2894
2895 // FIXME: Once remat is capable of dealing with instructions with register
2896 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002897 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2898 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002899 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002900 if (GV->hasDLLImportStorageClass())
2901 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002902 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2903 false, false, false, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002904 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002905}
2906
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002907SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002908ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002909 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002910 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002911 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2912 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002913 Op.getOperand(1), Val);
2914}
2915
2916SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002917ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002918 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002919 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002920 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002921}
2922
Matthias Braun3cd00c12015-07-16 22:34:16 +00002923SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2924 SelectionDAG &DAG) const {
2925 SDLoc dl(Op);
2926 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2927 Op.getOperand(0));
2928}
2929
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002930SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002931ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002932 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002933 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002934 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002935 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002936 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002937 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002938 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002939 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00002940 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002941 }
Bob Wilson17f88782009-08-04 00:25:01 +00002942 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002943 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002944 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2945 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002946 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002947 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002948 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002949 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002950 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002951 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2952 SDValue CPAddr;
2953 unsigned PCAdj = (RelocM != Reloc::PIC_)
2954 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002955 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002956 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2957 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002958 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002959 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002960 SDValue Result = DAG.getLoad(
2961 PtrVT, dl, DAG.getEntryNode(), CPAddr,
2962 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2963 false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002964
2965 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002966 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002967 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2968 }
2969 return Result;
2970 }
Evan Cheng18381b42011-03-29 23:06:19 +00002971 case Intrinsic::arm_neon_vmulls:
2972 case Intrinsic::arm_neon_vmullu: {
2973 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2974 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002975 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002976 Op.getOperand(1), Op.getOperand(2));
2977 }
James Molloyee868b22015-08-11 12:06:25 +00002978 case Intrinsic::arm_neon_vminnm:
2979 case Intrinsic::arm_neon_vmaxnm: {
2980 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
2981 ? ISD::FMINNUM : ISD::FMAXNUM;
2982 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2983 Op.getOperand(1), Op.getOperand(2));
2984 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002985 case Intrinsic::arm_neon_vminu:
2986 case Intrinsic::arm_neon_vmaxu: {
2987 if (Op.getValueType().isFloatingPoint())
2988 return SDValue();
2989 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
2990 ? ISD::UMIN : ISD::UMAX;
2991 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
2992 Op.getOperand(1), Op.getOperand(2));
2993 }
James Molloyd616c642015-08-11 12:06:28 +00002994 case Intrinsic::arm_neon_vmins:
2995 case Intrinsic::arm_neon_vmaxs: {
2996 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00002997 if (!Op.getValueType().isFloatingPoint()) {
2998 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
2999 ? ISD::SMIN : ISD::SMAX;
3000 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3001 Op.getOperand(1), Op.getOperand(2));
3002 }
James Molloyd616c642015-08-11 12:06:28 +00003003 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3004 ? ISD::FMINNAN : ISD::FMAXNAN;
3005 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3006 Op.getOperand(1), Op.getOperand(2));
3007 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003008 }
3009}
3010
Eli Friedman30a49e92011-08-03 21:06:02 +00003011static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3012 const ARMSubtarget *Subtarget) {
3013 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003014 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003015 if (!Subtarget->hasDataBarrier()) {
3016 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3017 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3018 // here.
3019 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003020 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003021 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003022 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003023 }
3024
Tim Northover36b24172013-07-03 09:20:36 +00003025 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3026 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003027 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003028 if (Subtarget->isMClass()) {
3029 // Only a full system barrier exists in the M-class architectures.
3030 Domain = ARM_MB::SY;
3031 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003032 // Swift happens to implement ISHST barriers in a way that's compatible with
3033 // Release semantics but weaker than ISH so we'd be fools not to use
3034 // it. Beware: other processors probably don't!
3035 Domain = ARM_MB::ISHST;
3036 }
3037
Joey Gouly926d3f52013-09-05 15:35:24 +00003038 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003039 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3040 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003041}
3042
Evan Cheng8740ee32010-11-03 06:34:55 +00003043static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3044 const ARMSubtarget *Subtarget) {
3045 // ARM pre v5TE and Thumb1 does not have preload instructions.
3046 if (!(Subtarget->isThumb2() ||
3047 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3048 // Just preserve the chain.
3049 return Op.getOperand(0);
3050
Andrew Trickef9de2a2013-05-25 02:42:55 +00003051 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003052 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3053 if (!isRead &&
3054 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3055 // ARMv7 with MP extension has PLDW.
3056 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003057
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003058 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3059 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003060 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003061 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003062 isData = ~isData & 1;
3063 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003064
3065 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003066 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3067 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003068}
3069
Dan Gohman31ae5862010-04-17 14:41:14 +00003070static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3071 MachineFunction &MF = DAG.getMachineFunction();
3072 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3073
Evan Cheng10043e22007-01-19 07:51:42 +00003074 // vastart just stores the address of the VarArgsFrameIndex slot into the
3075 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003076 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003077 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003078 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003079 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003080 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
3081 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00003082}
3083
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003084SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00003085ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
3086 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003087 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003088 MachineFunction &MF = DAG.getMachineFunction();
3089 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3090
Craig Topper760b1342012-02-22 05:59:10 +00003091 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003092 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003093 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003094 else
Craig Topperc7242e02012-04-20 07:30:17 +00003095 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003096
3097 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003098 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003099 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003100
3101 SDValue ArgValue2;
3102 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003103 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00003104 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003105
3106 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003107 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003108 ArgValue2 = DAG.getLoad(
3109 MVT::i32, dl, Root, FIN,
3110 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
3111 false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00003112 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003113 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003114 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003115 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003116 if (!Subtarget->isLittle())
3117 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003118 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003119}
3120
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003121// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003122// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003123// byval). Either way, we allocate stack slots adjacent to the data
3124// provided by our caller, and store the unallocated registers there.
3125// If this is a variadic function, the va_list pointer will begin with
3126// these values; otherwise, this reassembles a (byval) structure that
3127// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003128// Return: The frame index registers were stored into.
3129int
3130ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003131 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003132 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003133 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00003134 int ArgOffset,
3135 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003136 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003137 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003138 // Setup first unallocated register as first byval register;
3139 // eat all remained registers
3140 // (these two actions are performed by HandleByVal method).
3141 // Then, here, we initialize stack frame with
3142 // "store-reg" instructions.
3143 // Case #2. Var-args function, that doesn't contain byval parameters.
3144 // The same: eat all remained unallocated registers,
3145 // initialize stack frame.
3146
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003147 MachineFunction &MF = DAG.getMachineFunction();
3148 MachineFrameInfo *MFI = MF.getFrameInfo();
3149 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003150 unsigned RBegin, REnd;
3151 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3152 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003153 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003154 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003155 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003156 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003157 }
3158
Tim Northover8cda34f2015-03-11 18:54:22 +00003159 if (REnd != RBegin)
3160 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003161
Mehdi Amini44ede332015-07-09 02:09:04 +00003162 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00003163 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003164 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003165
Tim Northover8cda34f2015-03-11 18:54:22 +00003166 SmallVector<SDValue, 4> MemOps;
3167 const TargetRegisterClass *RC =
3168 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003169
Tim Northover8cda34f2015-03-11 18:54:22 +00003170 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3171 unsigned VReg = MF.addLiveIn(Reg, RC);
3172 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
3173 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003174 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00003175 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
3176 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003177 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003178 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003179
3180 if (!MemOps.empty())
3181 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3182 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003183}
3184
3185// Setup stack frame, the va_list pointer will start from.
3186void
3187ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003188 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003189 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003190 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003191 bool ForceMutable) const {
3192 MachineFunction &MF = DAG.getMachineFunction();
3193 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3194
3195 // Try to store any remaining integer argument regs
3196 // to their spots on the stack so that they may be loaded by deferencing
3197 // the result of va_next.
3198 // If there is no regs to be stored, just point address after last
3199 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003200 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3201 CCInfo.getInRegsParamsCount(),
3202 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003203 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003204}
3205
Bob Wilson2e076c42009-06-22 23:27:02 +00003206SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003207ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003208 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003209 const SmallVectorImpl<ISD::InputArg>
3210 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003211 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003212 SmallVectorImpl<SDValue> &InVals)
3213 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003214 MachineFunction &MF = DAG.getMachineFunction();
3215 MachineFrameInfo *MFI = MF.getFrameInfo();
3216
Bob Wilsona4c22902009-04-17 19:07:39 +00003217 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3218
3219 // Assign locations to all of the incoming arguments.
3220 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003221 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3222 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003223 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003224 CCAssignFnForNode(CallConv, /* Return*/ false,
3225 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003226
Bob Wilsona4c22902009-04-17 19:07:39 +00003227 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003228 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003229 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3230 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003231
3232 // Initially ArgRegsSaveSize is zero.
3233 // Then we increase this value each time we meet byval parameter.
3234 // We also increase this value in case of varargs function.
3235 AFI->setArgRegsSaveSize(0);
3236
Oliver Stannardd55e1152014-03-05 15:25:27 +00003237 // Calculate the amount of stack space that we need to allocate to store
3238 // byval and variadic arguments that are passed in registers.
3239 // We need to know this before we allocate the first byval or variadic
3240 // argument, as they will be allocated a stack slot below the CFA (Canonical
3241 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003242 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003243 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003244 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3245 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003246
Tim Northover8cda34f2015-03-11 18:54:22 +00003247 CCValAssign &VA = ArgLocs[i];
3248 unsigned Index = VA.getValNo();
3249 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3250 if (!Flags.isByVal())
3251 continue;
3252
3253 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3254 unsigned RBegin, REnd;
3255 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3256 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3257
3258 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003259 }
3260 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003261
3262 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003263 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003264 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3265 if (RegIdx != array_lengthof(GPRArgRegs))
3266 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003267 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003268
3269 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3270 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003271 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003272
Bob Wilsona4c22902009-04-17 19:07:39 +00003273 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3274 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003275 if (Ins[VA.getValNo()].isOrigArg()) {
3276 std::advance(CurOrigArg,
3277 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3278 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3279 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003280 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003281 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003282 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003283
Bob Wilsona4c22902009-04-17 19:07:39 +00003284 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003285 // f64 and vector types are split up into multiple registers or
3286 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003287 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003288 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003289 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003290 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003291 SDValue ArgValue2;
3292 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003293 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003294 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003295 ArgValue2 = DAG.getLoad(
3296 MVT::f64, dl, Chain, FIN,
3297 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3298 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003299 } else {
3300 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3301 Chain, DAG, dl);
3302 }
Owen Anderson9f944592009-08-11 20:47:22 +00003303 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3304 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003305 ArgValue, ArgValue1,
3306 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003307 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003308 ArgValue, ArgValue2,
3309 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003310 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003311 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003312
Bob Wilson2e076c42009-06-22 23:27:02 +00003313 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003314 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003315
Owen Anderson9f944592009-08-11 20:47:22 +00003316 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003317 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003318 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003319 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003320 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003321 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003322 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003323 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3324 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003325 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003326 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003327
3328 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003329 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003330 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003331 }
3332
3333 // If this is an 8 or 16-bit value, it is really passed promoted
3334 // to 32 bits. Insert an assert[sz]ext to capture this, then
3335 // truncate to the right size.
3336 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003337 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003338 case CCValAssign::Full: break;
3339 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003340 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003341 break;
3342 case CCValAssign::SExt:
3343 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3344 DAG.getValueType(VA.getValVT()));
3345 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3346 break;
3347 case CCValAssign::ZExt:
3348 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3349 DAG.getValueType(VA.getValVT()));
3350 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3351 break;
3352 }
3353
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003354 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003355
3356 } else { // VA.isRegLoc()
3357
3358 // sanity check
3359 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003360 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003361
Andrew Trick05938a52015-02-16 18:10:47 +00003362 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003363
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003364 // Some Ins[] entries become multiple ArgLoc[] entries.
3365 // Process them only once.
3366 if (index != lastInsIndex)
3367 {
3368 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003369 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003370 // This can be changed with more analysis.
3371 // In case of tail call optimization mark all arguments mutable.
3372 // Since they could be overwritten by lowering of arguments in case of
3373 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003374 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003375 assert(Ins[index].isOrigArg() &&
3376 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003377 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003378
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003379 int FrameIndex = StoreByValRegs(
3380 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3381 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003382 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003383 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003384 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003385 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003386 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003387 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003388
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003389 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003390 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003391 InVals.push_back(DAG.getLoad(
3392 VA.getValVT(), dl, Chain, FIN,
3393 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3394 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003395 }
3396 lastInsIndex = index;
3397 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003398 }
3399 }
3400
3401 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003402 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003403 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003404 CCInfo.getNextStackOffset(),
3405 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003406
Oliver Stannardb14c6252014-04-02 16:10:33 +00003407 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3408
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003409 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003410}
3411
3412/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003413static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003414 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003415 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003416 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003417 // Maybe this has already been legalized into the constant pool?
3418 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003419 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003420 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003421 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003422 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003423 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003424 } else if (Op->getOpcode() == ISD::BITCAST &&
3425 Op->getValueType(0) == MVT::f64) {
3426 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3427 // created by LowerConstantFP().
3428 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003429 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3430 isNullConstant(BitcastOp->getOperand(0)))
3431 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003432 }
3433 return false;
3434}
3435
Evan Cheng10043e22007-01-19 07:51:42 +00003436/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3437/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003438SDValue
3439ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003440 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003441 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003442 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003443 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003444 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003445 // Constant does not fit, try adjusting it by one?
3446 switch (CC) {
3447 default: break;
3448 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003449 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003450 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003451 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003452 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003453 }
3454 break;
3455 case ISD::SETULT:
3456 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003457 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003458 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003459 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003460 }
3461 break;
3462 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003463 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003464 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003465 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003466 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003467 }
3468 break;
3469 case ISD::SETULE:
3470 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003471 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003472 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003473 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003474 }
3475 break;
3476 }
3477 }
3478 }
3479
3480 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003481 ARMISD::NodeType CompareType;
3482 switch (CondCode) {
3483 default:
3484 CompareType = ARMISD::CMP;
3485 break;
3486 case ARMCC::EQ:
3487 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003488 // Uses only Z Flag
3489 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003490 break;
3491 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003492 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003493 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003494}
3495
3496/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003497SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003498ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003499 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003500 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003501 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003502 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003503 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003504 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003505 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3506 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003507}
3508
Bob Wilson45acbd02011-03-08 01:17:20 +00003509/// duplicateCmp - Glue values can have only one use, so this function
3510/// duplicates a comparison node.
3511SDValue
3512ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3513 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003514 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003515 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3516 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3517
3518 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3519 Cmp = Cmp.getOperand(0);
3520 Opc = Cmp.getOpcode();
3521 if (Opc == ARMISD::CMPFP)
3522 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3523 else {
3524 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3525 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3526 }
3527 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3528}
3529
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003530std::pair<SDValue, SDValue>
3531ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3532 SDValue &ARMcc) const {
3533 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3534
3535 SDValue Value, OverflowCmp;
3536 SDValue LHS = Op.getOperand(0);
3537 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003538 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003539
3540 // FIXME: We are currently always generating CMPs because we don't support
3541 // generating CMN through the backend. This is not as good as the natural
3542 // CMP case because it causes a register dependency and cannot be folded
3543 // later.
3544
3545 switch (Op.getOpcode()) {
3546 default:
3547 llvm_unreachable("Unknown overflow instruction!");
3548 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003549 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3550 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3551 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003552 break;
3553 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003554 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3555 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3556 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003557 break;
3558 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003559 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3560 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3561 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003562 break;
3563 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003564 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3565 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3566 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003567 break;
3568 } // switch (...)
3569
3570 return std::make_pair(Value, OverflowCmp);
3571}
3572
3573
3574SDValue
3575ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3576 // Let legalize expand this if it isn't a legal type yet.
3577 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3578 return SDValue();
3579
3580 SDValue Value, OverflowCmp;
3581 SDValue ARMcc;
3582 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3583 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003584 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003585 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003586 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3587 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003588 EVT VT = Op.getValueType();
3589
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003590 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003591 ARMcc, CCR, OverflowCmp);
3592
3593 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003594 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003595}
3596
3597
Bill Wendling6a981312010-08-11 08:43:16 +00003598SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3599 SDValue Cond = Op.getOperand(0);
3600 SDValue SelectTrue = Op.getOperand(1);
3601 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003602 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003603 unsigned Opc = Cond.getOpcode();
3604
3605 if (Cond.getResNo() == 1 &&
3606 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3607 Opc == ISD::USUBO)) {
3608 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3609 return SDValue();
3610
3611 SDValue Value, OverflowCmp;
3612 SDValue ARMcc;
3613 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3614 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3615 EVT VT = Op.getValueType();
3616
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003617 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003618 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003619 }
Bill Wendling6a981312010-08-11 08:43:16 +00003620
3621 // Convert:
3622 //
3623 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3624 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3625 //
3626 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3627 const ConstantSDNode *CMOVTrue =
3628 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3629 const ConstantSDNode *CMOVFalse =
3630 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3631
3632 if (CMOVTrue && CMOVFalse) {
3633 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3634 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3635
3636 SDValue True;
3637 SDValue False;
3638 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3639 True = SelectTrue;
3640 False = SelectFalse;
3641 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3642 True = SelectFalse;
3643 False = SelectTrue;
3644 }
3645
3646 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003647 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003648 SDValue ARMcc = Cond.getOperand(2);
3649 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003650 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003651 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003652 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003653 }
3654 }
3655 }
3656
Dan Gohmand4a77c42012-02-24 00:09:36 +00003657 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3658 // undefined bits before doing a full-word comparison with zero.
3659 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003660 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003661
Bill Wendling6a981312010-08-11 08:43:16 +00003662 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003663 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003664 SelectTrue, SelectFalse, ISD::SETNE);
3665}
3666
Joey Gouly881eab52013-08-22 15:29:11 +00003667static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3668 bool &swpCmpOps, bool &swpVselOps) {
3669 // Start by selecting the GE condition code for opcodes that return true for
3670 // 'equality'
3671 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3672 CC == ISD::SETULE)
3673 CondCode = ARMCC::GE;
3674
3675 // and GT for opcodes that return false for 'equality'.
3676 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3677 CC == ISD::SETULT)
3678 CondCode = ARMCC::GT;
3679
3680 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3681 // to swap the compare operands.
3682 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3683 CC == ISD::SETULT)
3684 swpCmpOps = true;
3685
3686 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3687 // If we have an unordered opcode, we need to swap the operands to the VSEL
3688 // instruction (effectively negating the condition).
3689 //
3690 // This also has the effect of swapping which one of 'less' or 'greater'
3691 // returns true, so we also swap the compare operands. It also switches
3692 // whether we return true for 'equality', so we compensate by picking the
3693 // opposite condition code to our original choice.
3694 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3695 CC == ISD::SETUGT) {
3696 swpCmpOps = !swpCmpOps;
3697 swpVselOps = !swpVselOps;
3698 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3699 }
3700
3701 // 'ordered' is 'anything but unordered', so use the VS condition code and
3702 // swap the VSEL operands.
3703 if (CC == ISD::SETO) {
3704 CondCode = ARMCC::VS;
3705 swpVselOps = true;
3706 }
3707
3708 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3709 // code and swap the VSEL operands.
3710 if (CC == ISD::SETUNE) {
3711 CondCode = ARMCC::EQ;
3712 swpVselOps = true;
3713 }
3714}
3715
Oliver Stannard51b1d462014-08-21 12:50:31 +00003716SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3717 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3718 SDValue Cmp, SelectionDAG &DAG) const {
3719 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3720 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3721 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3722 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3723 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3724
3725 SDValue TrueLow = TrueVal.getValue(0);
3726 SDValue TrueHigh = TrueVal.getValue(1);
3727 SDValue FalseLow = FalseVal.getValue(0);
3728 SDValue FalseHigh = FalseVal.getValue(1);
3729
3730 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3731 ARMcc, CCR, Cmp);
3732 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3733 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3734
3735 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3736 } else {
3737 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3738 Cmp);
3739 }
3740}
3741
Dan Gohman21cea8a2010-04-17 15:26:15 +00003742SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003743 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003744 SDValue LHS = Op.getOperand(0);
3745 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003746 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003747 SDValue TrueVal = Op.getOperand(2);
3748 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003749 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003750
Oliver Stannard51b1d462014-08-21 12:50:31 +00003751 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3752 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3753 dl);
3754
3755 // If softenSetCCOperands only returned one value, we should compare it to
3756 // zero.
3757 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003758 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003759 CC = ISD::SETNE;
3760 }
3761 }
3762
Owen Anderson9f944592009-08-11 20:47:22 +00003763 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003764 // Try to generate VSEL on ARMv8.
3765 // The VSEL instruction can't use all the usual ARM condition
3766 // codes: it only has two bits to select the condition code, so it's
3767 // constrained to use only GE, GT, VS and EQ.
3768 //
3769 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3770 // swap the operands of the previous compare instruction (effectively
3771 // inverting the compare condition, swapping 'less' and 'greater') and
3772 // sometimes need to swap the operands to the VSEL (which inverts the
3773 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003774 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3775 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003776 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3777 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3778 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003779 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003780 std::swap(TrueVal, FalseVal);
3781 }
3782 }
3783
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003784 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003785 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003786 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003787 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003788 }
3789
3790 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003791 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003792
Scott Douglass7ad77922015-04-08 17:18:28 +00003793 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003794 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3795 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003796 bool swpCmpOps = false;
3797 bool swpVselOps = false;
3798 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3799
3800 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3801 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3802 if (swpCmpOps)
3803 std::swap(LHS, RHS);
3804 if (swpVselOps)
3805 std::swap(TrueVal, FalseVal);
3806 }
3807 }
3808
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003809 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003810 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003811 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003812 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003813 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003814 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003815 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003816 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003817 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003818 }
3819 return Result;
3820}
3821
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003822/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3823/// to morph to an integer compare sequence.
3824static bool canChangeToInt(SDValue Op, bool &SeenZero,
3825 const ARMSubtarget *Subtarget) {
3826 SDNode *N = Op.getNode();
3827 if (!N->hasOneUse())
3828 // Otherwise it requires moving the value from fp to integer registers.
3829 return false;
3830 if (!N->getNumValues())
3831 return false;
3832 EVT VT = Op.getValueType();
3833 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3834 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3835 // vmrs are very slow, e.g. cortex-a8.
3836 return false;
3837
3838 if (isFloatingPointZero(Op)) {
3839 SeenZero = true;
3840 return true;
3841 }
3842 return ISD::isNormalLoad(N);
3843}
3844
3845static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3846 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003847 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003848
3849 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003850 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003851 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003852 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003853 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003854
3855 llvm_unreachable("Unknown VFP cmp argument!");
3856}
3857
3858static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3859 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003860 SDLoc dl(Op);
3861
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003862 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003863 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3864 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003865 return;
3866 }
3867
3868 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3869 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003870 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003871 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003872 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003873 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003874 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003875
3876 EVT PtrType = Ptr.getValueType();
3877 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003878 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3879 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3880 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003881 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003882 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003883 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003884 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003885 return;
3886 }
3887
3888 llvm_unreachable("Unknown VFP cmp argument!");
3889}
3890
3891/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3892/// f32 and even f64 comparisons to integer ones.
3893SDValue
3894ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3895 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003896 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003897 SDValue LHS = Op.getOperand(2);
3898 SDValue RHS = Op.getOperand(3);
3899 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003900 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003901
Evan Chengd12af5d2012-03-01 23:27:13 +00003902 bool LHSSeenZero = false;
3903 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3904 bool RHSSeenZero = false;
3905 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3906 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003907 // If unsafe fp math optimization is enabled and there are no other uses of
3908 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003909 // to an integer comparison.
3910 if (CC == ISD::SETOEQ)
3911 CC = ISD::SETEQ;
3912 else if (CC == ISD::SETUNE)
3913 CC = ISD::SETNE;
3914
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003915 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003916 SDValue ARMcc;
3917 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003918 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3919 bitcastf32Toi32(LHS, DAG), Mask);
3920 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3921 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003922 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3923 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3924 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3925 Chain, Dest, ARMcc, CCR, Cmp);
3926 }
3927
3928 SDValue LHS1, LHS2;
3929 SDValue RHS1, RHS2;
3930 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3931 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003932 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3933 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003934 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003935 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003936 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003937 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003938 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003939 }
3940
3941 return SDValue();
3942}
3943
3944SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3945 SDValue Chain = Op.getOperand(0);
3946 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3947 SDValue LHS = Op.getOperand(2);
3948 SDValue RHS = Op.getOperand(3);
3949 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003950 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003951
Oliver Stannard51b1d462014-08-21 12:50:31 +00003952 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3953 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3954 dl);
3955
3956 // If softenSetCCOperands only returned one value, we should compare it to
3957 // zero.
3958 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003959 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003960 CC = ISD::SETNE;
3961 }
3962 }
3963
Owen Anderson9f944592009-08-11 20:47:22 +00003964 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003965 SDValue ARMcc;
3966 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003967 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003968 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003969 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003970 }
3971
Owen Anderson9f944592009-08-11 20:47:22 +00003972 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003973
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003974 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003975 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3976 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00003977 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003978 return Result;
3979 }
3980
Evan Cheng10043e22007-01-19 07:51:42 +00003981 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003982 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003983
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003984 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003985 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003986 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003987 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003988 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003989 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003990 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003991 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003992 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003993 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003994 }
3995 return Res;
3996}
3997
Dan Gohman21cea8a2010-04-17 15:26:15 +00003998SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003999 SDValue Chain = Op.getOperand(0);
4000 SDValue Table = Op.getOperand(1);
4001 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004002 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004003
Mehdi Amini44ede332015-07-09 02:09:04 +00004004 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00004005 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004006 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00004007 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004008 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00004009 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004010 if (Subtarget->isThumb2()) {
4011 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4012 // which does another jump to the destination. This also makes it easier
4013 // to translate it to TBB / TBH later.
4014 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004015 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004016 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004017 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00004018 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004019 Addr =
4020 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
4021 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4022 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004023 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004024 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004025 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004026 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004027 Addr =
4028 DAG.getLoad(PTy, dl, Chain, Addr,
4029 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()),
4030 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004031 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004032 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004033 }
Evan Cheng10043e22007-01-19 07:51:42 +00004034}
4035
Eli Friedman2d4055b2011-11-09 23:36:02 +00004036static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004037 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004038 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004039
James Molloy547d4c02012-02-20 09:24:05 +00004040 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4041 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4042 return Op;
4043 return DAG.UnrollVectorOp(Op.getNode());
4044 }
4045
4046 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4047 "Invalid type for custom lowering!");
4048 if (VT != MVT::v4i16)
4049 return DAG.UnrollVectorOp(Op.getNode());
4050
4051 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4052 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004053}
4054
Oliver Stannard51b1d462014-08-21 12:50:31 +00004055SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004056 EVT VT = Op.getValueType();
4057 if (VT.isVector())
4058 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004059 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4060 RTLIB::Libcall LC;
4061 if (Op.getOpcode() == ISD::FP_TO_SINT)
4062 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4063 Op.getValueType());
4064 else
4065 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4066 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004067 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004068 /*isSigned*/ false, SDLoc(Op)).first;
4069 }
4070
James Molloyfa041152015-03-23 16:15:16 +00004071 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004072}
4073
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004074static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4075 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004076 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004077
Eli Friedman2d4055b2011-11-09 23:36:02 +00004078 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4079 if (VT.getVectorElementType() == MVT::f32)
4080 return Op;
4081 return DAG.UnrollVectorOp(Op.getNode());
4082 }
4083
Duncan Sandsa41634e2011-08-12 14:54:45 +00004084 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4085 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004086 if (VT != MVT::v4f32)
4087 return DAG.UnrollVectorOp(Op.getNode());
4088
4089 unsigned CastOpc;
4090 unsigned Opc;
4091 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004092 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004093 case ISD::SINT_TO_FP:
4094 CastOpc = ISD::SIGN_EXTEND;
4095 Opc = ISD::SINT_TO_FP;
4096 break;
4097 case ISD::UINT_TO_FP:
4098 CastOpc = ISD::ZERO_EXTEND;
4099 Opc = ISD::UINT_TO_FP;
4100 break;
4101 }
4102
4103 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4104 return DAG.getNode(Opc, dl, VT, Op);
4105}
4106
Oliver Stannard51b1d462014-08-21 12:50:31 +00004107SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004108 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004109 if (VT.isVector())
4110 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004111 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4112 RTLIB::Libcall LC;
4113 if (Op.getOpcode() == ISD::SINT_TO_FP)
4114 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4115 Op.getValueType());
4116 else
4117 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4118 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004119 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004120 /*isSigned*/ false, SDLoc(Op)).first;
4121 }
4122
James Molloyfa041152015-03-23 16:15:16 +00004123 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004124}
4125
Evan Cheng25f93642010-07-08 02:08:50 +00004126SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004127 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004128 SDValue Tmp0 = Op.getOperand(0);
4129 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004130 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004131 EVT VT = Op.getValueType();
4132 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004133 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4134 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4135 bool UseNEON = !InGPR && Subtarget->hasNEON();
4136
4137 if (UseNEON) {
4138 // Use VBSL to copy the sign bit.
4139 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4140 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004141 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004142 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4143 if (VT == MVT::f64)
4144 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4145 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004146 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004147 else /*if (VT == MVT::f32)*/
4148 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4149 if (SrcVT == MVT::f32) {
4150 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4151 if (VT == MVT::f64)
4152 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4153 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004154 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004155 } else if (VT == MVT::f32)
4156 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4157 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004158 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004159 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4160 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4161
4162 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004163 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004164 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4165 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4166 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004167
Evan Chengd6b641e2011-02-23 02:24:55 +00004168 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4169 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4170 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004171 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004172 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4173 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004174 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004175 } else {
4176 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4177 }
4178
4179 return Res;
4180 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004181
4182 // Bitcast operand 1 to i32.
4183 if (SrcVT == MVT::f64)
4184 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004185 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004186 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4187
Evan Chengd6b641e2011-02-23 02:24:55 +00004188 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004189 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4190 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004191 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4192 if (VT == MVT::f32) {
4193 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4194 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4195 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4196 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004197 }
4198
Evan Chengd6b641e2011-02-23 02:24:55 +00004199 // f64: Or the high part with signbit and then combine two parts.
4200 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004201 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004202 SDValue Lo = Tmp0.getValue(0);
4203 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4204 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4205 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004206}
4207
Evan Cheng168ced92010-05-22 01:47:14 +00004208SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4209 MachineFunction &MF = DAG.getMachineFunction();
4210 MachineFrameInfo *MFI = MF.getFrameInfo();
4211 MFI->setReturnAddressIsTaken(true);
4212
Bill Wendling908bf812014-01-06 00:43:20 +00004213 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004214 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004215
Evan Cheng168ced92010-05-22 01:47:14 +00004216 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004217 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004218 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4219 if (Depth) {
4220 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004221 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004222 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4223 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004224 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004225 }
4226
4227 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004228 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004229 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4230}
4231
Dan Gohman21cea8a2010-04-17 15:26:15 +00004232SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004233 const ARMBaseRegisterInfo &ARI =
4234 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4235 MachineFunction &MF = DAG.getMachineFunction();
4236 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004237 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004238
Owen Anderson53aa7a92009-08-10 22:56:29 +00004239 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004240 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004241 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004242 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004243 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4244 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004245 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4246 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004247 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004248 return FrameAddr;
4249}
4250
Renato Golinc7aea402014-05-06 16:51:25 +00004251// FIXME? Maybe this could be a TableGen attribute on some registers and
4252// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004253unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4254 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004255 unsigned Reg = StringSwitch<unsigned>(RegName)
4256 .Case("sp", ARM::SP)
4257 .Default(0);
4258 if (Reg)
4259 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004260 report_fatal_error(Twine("Invalid register name \""
4261 + StringRef(RegName) + "\"."));
4262}
4263
4264// Result is 64 bit value so split into two 32 bit values and return as a
4265// pair of values.
4266static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4267 SelectionDAG &DAG) {
4268 SDLoc DL(N);
4269
4270 // This function is only supposed to be called for i64 type destination.
4271 assert(N->getValueType(0) == MVT::i64
4272 && "ExpandREAD_REGISTER called for non-i64 type result.");
4273
4274 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4275 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4276 N->getOperand(0),
4277 N->getOperand(1));
4278
4279 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4280 Read.getValue(1)));
4281 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004282}
4283
Quentin Colombet901f0362015-12-04 01:53:14 +00004284/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4285/// When \p DstVT, the destination type of \p BC, is on the vector
4286/// register bank and the source of bitcast, \p Op, operates on the same bank,
4287/// it might be possible to combine them, such that everything stays on the
4288/// vector register bank.
4289/// \p return The node that would replace \p BT, if the combine
4290/// is possible.
4291static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4292 SelectionDAG &DAG) {
4293 SDValue Op = BC->getOperand(0);
4294 EVT DstVT = BC->getValueType(0);
4295
4296 // The only vector instruction that can produce a scalar (remember,
4297 // since the bitcast was about to be turned into VMOVDRR, the source
4298 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4299 // Moreover, we can do this combine only if there is one use.
4300 // Finally, if the destination type is not a vector, there is not
4301 // much point on forcing everything on the vector bank.
4302 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4303 !Op.hasOneUse())
4304 return SDValue();
4305
4306 // If the index is not constant, we will introduce an additional
4307 // multiply that will stick.
4308 // Give up in that case.
4309 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4310 if (!Index)
4311 return SDValue();
4312 unsigned DstNumElt = DstVT.getVectorNumElements();
4313
4314 // Compute the new index.
4315 const APInt &APIntIndex = Index->getAPIntValue();
4316 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4317 NewIndex *= APIntIndex;
4318 // Check if the new constant index fits into i32.
4319 if (NewIndex.getBitWidth() > 32)
4320 return SDValue();
4321
4322 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4323 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4324 SDLoc dl(Op);
4325 SDValue ExtractSrc = Op.getOperand(0);
4326 EVT VecVT = EVT::getVectorVT(
4327 *DAG.getContext(), DstVT.getScalarType(),
4328 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4329 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4330 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4331 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4332}
4333
Wesley Peck527da1b2010-11-23 03:31:01 +00004334/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004335/// expand a bit convert where either the source or destination type is i64 to
4336/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4337/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4338/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004339static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004340 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004341 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004342 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004343
Bob Wilson59b70ea2010-04-17 05:30:19 +00004344 // This function is only supposed to be called for i64 types, either as the
4345 // source or destination of the bit convert.
4346 EVT SrcVT = Op.getValueType();
4347 EVT DstVT = N->getValueType(0);
4348 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004349 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004350
Bob Wilson59b70ea2010-04-17 05:30:19 +00004351 // Turn i64->f64 into VMOVDRR.
4352 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004353 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4354 // if we can combine the bitcast with its source.
4355 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4356 return Val;
4357
Owen Anderson9f944592009-08-11 20:47:22 +00004358 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004359 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004360 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004361 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004362 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004363 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004364 }
Bob Wilson7117a912009-03-20 22:42:55 +00004365
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004366 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004367 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004368 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004369 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004370 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004371 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4372 DAG.getVTList(MVT::i32, MVT::i32),
4373 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4374 else
4375 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4376 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004377 // Merge the pieces into a single i64 value.
4378 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4379 }
Bob Wilson7117a912009-03-20 22:42:55 +00004380
Bob Wilson59b70ea2010-04-17 05:30:19 +00004381 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004382}
4383
Bob Wilson2e076c42009-06-22 23:27:02 +00004384/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004385/// Zero vectors are used to represent vector negation and in those cases
4386/// will be implemented with the NEON VNEG instruction. However, VNEG does
4387/// not support i64 elements, so sometimes the zero vectors will need to be
4388/// explicitly constructed. Regardless, use a canonical VMOV to create the
4389/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004390static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004391 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004392 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004393 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004394 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4395 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004396 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004397}
4398
Jim Grosbach624fcb22009-10-31 21:00:56 +00004399/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4400/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004401SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4402 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004403 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4404 EVT VT = Op.getValueType();
4405 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004406 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004407 SDValue ShOpLo = Op.getOperand(0);
4408 SDValue ShOpHi = Op.getOperand(1);
4409 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004410 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004411 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004412
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004413 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4414
Jim Grosbach624fcb22009-10-31 21:00:56 +00004415 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004416 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004417 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4418 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004419 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004420 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4421 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004422 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004423
4424 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004425 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4426 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004427 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004428 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004429 CCR, Cmp);
4430
4431 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004432 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004433}
4434
Jim Grosbach5d994042009-10-31 19:38:01 +00004435/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4436/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004437SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4438 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004439 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4440 EVT VT = Op.getValueType();
4441 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004442 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004443 SDValue ShOpLo = Op.getOperand(0);
4444 SDValue ShOpHi = Op.getOperand(1);
4445 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004446 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004447
4448 assert(Op.getOpcode() == ISD::SHL_PARTS);
4449 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004450 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004451 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4452 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004453 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004454 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4455 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4456
4457 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4458 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004459 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4460 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004461 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004462 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004463 CCR, Cmp);
4464
4465 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004466 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004467}
4468
Jim Grosbach535d3b42010-09-08 03:54:02 +00004469SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004470 SelectionDAG &DAG) const {
4471 // The rounding mode is in bits 23:22 of the FPSCR.
4472 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4473 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4474 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004475 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004476 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004477 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004478 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004479 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004480 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004481 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004482 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004483 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004484 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004485}
4486
Jim Grosbach8546ec92010-01-18 19:58:49 +00004487static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4488 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004489 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004490 EVT VT = N->getValueType(0);
4491 if (VT.isVector()) {
4492 assert(ST->hasNEON());
4493
4494 // Compute the least significant set bit: LSB = X & -X
4495 SDValue X = N->getOperand(0);
4496 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4497 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4498
4499 EVT ElemTy = VT.getVectorElementType();
4500
4501 if (ElemTy == MVT::i8) {
4502 // Compute with: cttz(x) = ctpop(lsb - 1)
4503 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4504 DAG.getTargetConstant(1, dl, ElemTy));
4505 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4506 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4507 }
4508
4509 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4510 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4511 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4512 unsigned NumBits = ElemTy.getSizeInBits();
4513 SDValue WidthMinus1 =
4514 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4515 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4516 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4517 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4518 }
4519
4520 // Compute with: cttz(x) = ctpop(lsb - 1)
4521
4522 // Since we can only compute the number of bits in a byte with vcnt.8, we
4523 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4524 // and i64.
4525
4526 // Compute LSB - 1.
4527 SDValue Bits;
4528 if (ElemTy == MVT::i64) {
4529 // Load constant 0xffff'ffff'ffff'ffff to register.
4530 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4531 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4532 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4533 } else {
4534 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4535 DAG.getTargetConstant(1, dl, ElemTy));
4536 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4537 }
4538
4539 // Count #bits with vcnt.8.
4540 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4541 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4542 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4543
4544 // Gather the #bits with vpaddl (pairwise add.)
4545 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4546 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4547 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4548 Cnt8);
4549 if (ElemTy == MVT::i16)
4550 return Cnt16;
4551
4552 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4553 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4554 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4555 Cnt16);
4556 if (ElemTy == MVT::i32)
4557 return Cnt32;
4558
4559 assert(ElemTy == MVT::i64);
4560 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4561 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4562 Cnt32);
4563 return Cnt64;
4564 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004565
4566 if (!ST->hasV6T2Ops())
4567 return SDValue();
4568
James Molloyb5640982015-11-13 16:05:22 +00004569 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00004570 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4571}
4572
Evan Chengb4eae132012-12-04 22:41:50 +00004573/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4574/// for each 16-bit element from operand, repeated. The basic idea is to
4575/// leverage vcnt to get the 8-bit counts, gather and add the results.
4576///
4577/// Trace for v4i16:
4578/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4579/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4580/// 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 +00004581/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004582/// [b0 b1 b2 b3 b4 b5 b6 b7]
4583/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4584/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4585/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4586static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4587 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004588 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004589
4590 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4591 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4592 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4593 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4594 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4595 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4596}
4597
4598/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4599/// bit-count for each 16-bit element from the operand. We need slightly
4600/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4601/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004602///
Evan Chengb4eae132012-12-04 22:41:50 +00004603/// Trace for v4i16:
4604/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4605/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4606/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4607/// v4i16:Extracted = [k0 k1 k2 k3 ]
4608static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4609 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004610 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004611
4612 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4613 if (VT.is64BitVector()) {
4614 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4615 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004616 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004617 } else {
4618 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004619 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004620 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4621 }
4622}
4623
4624/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4625/// bit-count for each 32-bit element from the operand. The idea here is
4626/// to split the vector into 16-bit elements, leverage the 16-bit count
4627/// routine, and then combine the results.
4628///
4629/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4630/// input = [v0 v1 ] (vi: 32-bit elements)
4631/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4632/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004633/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004634/// [k0 k1 k2 k3 ]
4635/// N1 =+[k1 k0 k3 k2 ]
4636/// [k0 k2 k1 k3 ]
4637/// N2 =+[k1 k3 k0 k2 ]
4638/// [k0 k2 k1 k3 ]
4639/// Extended =+[k1 k3 k0 k2 ]
4640/// [k0 k2 ]
4641/// Extracted=+[k1 k3 ]
4642///
4643static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4644 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004645 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004646
4647 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4648
4649 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4650 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4651 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4652 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4653 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4654
4655 if (VT.is64BitVector()) {
4656 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4657 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004658 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004659 } else {
4660 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004661 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004662 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4663 }
4664}
4665
4666static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4667 const ARMSubtarget *ST) {
4668 EVT VT = N->getValueType(0);
4669
4670 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004671 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4672 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004673 "Unexpected type for custom ctpop lowering");
4674
4675 if (VT.getVectorElementType() == MVT::i32)
4676 return lowerCTPOP32BitElements(N, DAG);
4677 else
4678 return lowerCTPOP16BitElements(N, DAG);
4679}
4680
Bob Wilson2e076c42009-06-22 23:27:02 +00004681static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4682 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004683 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004684 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004685
Bob Wilson7d471332010-11-18 21:16:28 +00004686 if (!VT.isVector())
4687 return SDValue();
4688
Bob Wilson2e076c42009-06-22 23:27:02 +00004689 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004690 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004691
Bob Wilson7d471332010-11-18 21:16:28 +00004692 // Left shifts translate directly to the vshiftu intrinsic.
4693 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004694 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004695 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4696 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004697 N->getOperand(0), N->getOperand(1));
4698
4699 assert((N->getOpcode() == ISD::SRA ||
4700 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4701
4702 // NEON uses the same intrinsics for both left and right shifts. For
4703 // right shifts, the shift amounts are negative, so negate the vector of
4704 // shift amounts.
4705 EVT ShiftVT = N->getOperand(1).getValueType();
4706 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4707 getZeroVector(ShiftVT, DAG, dl),
4708 N->getOperand(1));
4709 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4710 Intrinsic::arm_neon_vshifts :
4711 Intrinsic::arm_neon_vshiftu);
4712 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004713 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004714 N->getOperand(0), NegatedCount);
4715}
4716
4717static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4718 const ARMSubtarget *ST) {
4719 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004720 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004721
Eli Friedman682d8c12009-08-22 03:13:10 +00004722 // We can get here for a node like i32 = ISD::SHL i32, i64
4723 if (VT != MVT::i64)
4724 return SDValue();
4725
4726 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004727 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004728
Chris Lattnerf81d5882007-11-24 07:07:01 +00004729 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00004730 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00004731 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004732
Chris Lattnerf81d5882007-11-24 07:07:01 +00004733 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004734 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004735
Chris Lattnerf81d5882007-11-24 07:07:01 +00004736 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004737 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004738 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004739 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004740 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004741
Chris Lattnerf81d5882007-11-24 07:07:01 +00004742 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4743 // captures the result into a carry flag.
4744 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004745 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004746
Chris Lattnerf81d5882007-11-24 07:07:01 +00004747 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004748 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004749
Chris Lattnerf81d5882007-11-24 07:07:01 +00004750 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004751 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004752}
4753
Bob Wilson2e076c42009-06-22 23:27:02 +00004754static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4755 SDValue TmpOp0, TmpOp1;
4756 bool Invert = false;
4757 bool Swap = false;
4758 unsigned Opc = 0;
4759
4760 SDValue Op0 = Op.getOperand(0);
4761 SDValue Op1 = Op.getOperand(1);
4762 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004763 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004764 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004765 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004766 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004767
James Molloybf170092015-08-20 16:33:44 +00004768 if (CmpVT.getVectorElementType() == MVT::i64)
4769 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
4770 // but it's possible that our operands are 64-bit but our result is 32-bit.
4771 // Bail in this case.
4772 return SDValue();
4773
Oliver Stannard51b1d462014-08-21 12:50:31 +00004774 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004775 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004776 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004777 case ISD::SETUNE:
4778 case ISD::SETNE: Invert = true; // Fallthrough
4779 case ISD::SETOEQ:
4780 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4781 case ISD::SETOLT:
4782 case ISD::SETLT: Swap = true; // Fallthrough
4783 case ISD::SETOGT:
4784 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4785 case ISD::SETOLE:
4786 case ISD::SETLE: Swap = true; // Fallthrough
4787 case ISD::SETOGE:
4788 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4789 case ISD::SETUGE: Swap = true; // Fallthrough
4790 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4791 case ISD::SETUGT: Swap = true; // Fallthrough
4792 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4793 case ISD::SETUEQ: Invert = true; // Fallthrough
4794 case ISD::SETONE:
4795 // Expand this to (OLT | OGT).
4796 TmpOp0 = Op0;
4797 TmpOp1 = Op1;
4798 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004799 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4800 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004801 break;
4802 case ISD::SETUO: Invert = true; // Fallthrough
4803 case ISD::SETO:
4804 // Expand this to (OLT | OGE).
4805 TmpOp0 = Op0;
4806 TmpOp1 = Op1;
4807 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004808 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4809 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004810 break;
4811 }
4812 } else {
4813 // Integer comparisons.
4814 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004815 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004816 case ISD::SETNE: Invert = true;
4817 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4818 case ISD::SETLT: Swap = true;
4819 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4820 case ISD::SETLE: Swap = true;
4821 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4822 case ISD::SETULT: Swap = true;
4823 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4824 case ISD::SETULE: Swap = true;
4825 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4826 }
4827
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004828 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004829 if (Opc == ARMISD::VCEQ) {
4830
4831 SDValue AndOp;
4832 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4833 AndOp = Op0;
4834 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4835 AndOp = Op1;
4836
4837 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004838 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004839 AndOp = AndOp.getOperand(0);
4840
4841 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4842 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004843 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4844 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004845 Invert = !Invert;
4846 }
4847 }
4848 }
4849
4850 if (Swap)
4851 std::swap(Op0, Op1);
4852
Owen Andersonc7baee32010-11-08 23:21:22 +00004853 // If one of the operands is a constant vector zero, attempt to fold the
4854 // comparison to a specialized compare-against-zero form.
4855 SDValue SingleOp;
4856 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4857 SingleOp = Op0;
4858 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4859 if (Opc == ARMISD::VCGE)
4860 Opc = ARMISD::VCLEZ;
4861 else if (Opc == ARMISD::VCGT)
4862 Opc = ARMISD::VCLTZ;
4863 SingleOp = Op1;
4864 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004865
Owen Andersonc7baee32010-11-08 23:21:22 +00004866 SDValue Result;
4867 if (SingleOp.getNode()) {
4868 switch (Opc) {
4869 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004870 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004871 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004872 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004873 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004874 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004875 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004876 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004877 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004878 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004879 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004880 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004881 }
4882 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004883 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004884 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004885
Tim Northover45aa89c2015-02-08 00:50:47 +00004886 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4887
Bob Wilson2e076c42009-06-22 23:27:02 +00004888 if (Invert)
4889 Result = DAG.getNOT(dl, Result, VT);
4890
4891 return Result;
4892}
4893
Bob Wilson5b2b5042010-06-14 22:19:57 +00004894/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4895/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004896/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004897static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4898 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004899 SDLoc dl, EVT &VT, bool is128Bits,
4900 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004901 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004902
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004903 // SplatBitSize is set to the smallest size that splats the vector, so a
4904 // zero vector will always have SplatBitSize == 8. However, NEON modified
4905 // immediate instructions others than VMOV do not support the 8-bit encoding
4906 // of a zero vector, and the default encoding of zero is supposed to be the
4907 // 32-bit version.
4908 if (SplatBits == 0)
4909 SplatBitSize = 32;
4910
Bob Wilson2e076c42009-06-22 23:27:02 +00004911 switch (SplatBitSize) {
4912 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004913 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004914 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004915 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004916 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004917 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004918 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004919 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004920 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004921
4922 case 16:
4923 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004924 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004925 if ((SplatBits & ~0xff) == 0) {
4926 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004927 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004928 Imm = SplatBits;
4929 break;
4930 }
4931 if ((SplatBits & ~0xff00) == 0) {
4932 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004933 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004934 Imm = SplatBits >> 8;
4935 break;
4936 }
4937 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004938
4939 case 32:
4940 // NEON's 32-bit VMOV supports splat values where:
4941 // * only one byte is nonzero, or
4942 // * the least significant byte is 0xff and the second byte is nonzero, or
4943 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004944 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004945 if ((SplatBits & ~0xff) == 0) {
4946 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004947 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004948 Imm = SplatBits;
4949 break;
4950 }
4951 if ((SplatBits & ~0xff00) == 0) {
4952 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004953 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004954 Imm = SplatBits >> 8;
4955 break;
4956 }
4957 if ((SplatBits & ~0xff0000) == 0) {
4958 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004959 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004960 Imm = SplatBits >> 16;
4961 break;
4962 }
4963 if ((SplatBits & ~0xff000000) == 0) {
4964 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004965 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004966 Imm = SplatBits >> 24;
4967 break;
4968 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004969
Owen Andersona4076922010-11-05 21:57:54 +00004970 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4971 if (type == OtherModImm) return SDValue();
4972
Bob Wilson2e076c42009-06-22 23:27:02 +00004973 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004974 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4975 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004976 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004977 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004978 break;
4979 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004980
4981 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004982 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4983 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004984 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004985 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004986 break;
4987 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004988
4989 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4990 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4991 // VMOV.I32. A (very) minor optimization would be to replicate the value
4992 // and fall through here to test for a valid 64-bit splat. But, then the
4993 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004994 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004995
4996 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004997 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004998 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004999 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00005000 uint64_t BitMask = 0xff;
5001 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005002 unsigned ImmMask = 1;
5003 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005004 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005005 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005006 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005007 Imm |= ImmMask;
5008 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005009 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005010 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005011 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005012 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005013 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005014
Mehdi Aminiffc14022015-07-08 01:00:38 +00005015 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005016 // swap higher and lower 32 bit word
5017 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5018
Bob Wilson6eae5202010-06-11 21:34:50 +00005019 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005020 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005021 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005022 break;
5023 }
5024
Bob Wilson6eae5202010-06-11 21:34:50 +00005025 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005026 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005027 }
5028
Bob Wilsona3f19012010-07-13 21:16:48 +00005029 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005030 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005031}
5032
Lang Hames591cdaf2012-03-29 21:56:11 +00005033SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5034 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005035 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005036 return SDValue();
5037
Tim Northoverf79c3a52013-08-20 08:57:11 +00005038 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005039 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005040
Oliver Stannard51b1d462014-08-21 12:50:31 +00005041 // Use the default (constant pool) lowering for double constants when we have
5042 // an SP-only FPU
5043 if (IsDouble && Subtarget->isFPOnlySP())
5044 return SDValue();
5045
Lang Hames591cdaf2012-03-29 21:56:11 +00005046 // Try splatting with a VMOV.f32...
5047 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005048 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5049
Lang Hames591cdaf2012-03-29 21:56:11 +00005050 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005051 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5052 // We have code in place to select a valid ConstantFP already, no need to
5053 // do any mangling.
5054 return Op;
5055 }
5056
5057 // It's a float and we are trying to use NEON operations where
5058 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005059 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005060 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005061 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5062 NewVal);
5063 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005064 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005065 }
5066
Tim Northoverf79c3a52013-08-20 08:57:11 +00005067 // The rest of our options are NEON only, make sure that's allowed before
5068 // proceeding..
5069 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5070 return SDValue();
5071
Lang Hames591cdaf2012-03-29 21:56:11 +00005072 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005073 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5074
5075 // It wouldn't really be worth bothering for doubles except for one very
5076 // important value, which does happen to match: 0.0. So make sure we don't do
5077 // anything stupid.
5078 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5079 return SDValue();
5080
5081 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005082 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5083 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005084 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005085 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005086 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5087 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005088 if (IsDouble)
5089 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5090
5091 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005092 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5093 VecConstant);
5094 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005095 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005096 }
5097
5098 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005099 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005100 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005101 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005102 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005103 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005104
5105 if (IsDouble)
5106 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5107
5108 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005109 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5110 VecConstant);
5111 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005112 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005113 }
5114
5115 return SDValue();
5116}
5117
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005118// check if an VEXT instruction can handle the shuffle mask when the
5119// vector sources of the shuffle are the same.
5120static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5121 unsigned NumElts = VT.getVectorNumElements();
5122
5123 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5124 if (M[0] < 0)
5125 return false;
5126
5127 Imm = M[0];
5128
5129 // If this is a VEXT shuffle, the immediate value is the index of the first
5130 // element. The other shuffle indices must be the successive elements after
5131 // the first one.
5132 unsigned ExpectedElt = Imm;
5133 for (unsigned i = 1; i < NumElts; ++i) {
5134 // Increment the expected index. If it wraps around, just follow it
5135 // back to index zero and keep going.
5136 ++ExpectedElt;
5137 if (ExpectedElt == NumElts)
5138 ExpectedElt = 0;
5139
5140 if (M[i] < 0) continue; // ignore UNDEF indices
5141 if (ExpectedElt != static_cast<unsigned>(M[i]))
5142 return false;
5143 }
5144
5145 return true;
5146}
5147
Lang Hames591cdaf2012-03-29 21:56:11 +00005148
Benjamin Kramer339ced42012-01-15 13:16:05 +00005149static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005150 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005151 unsigned NumElts = VT.getVectorNumElements();
5152 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005153
5154 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5155 if (M[0] < 0)
5156 return false;
5157
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005158 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005159
5160 // If this is a VEXT shuffle, the immediate value is the index of the first
5161 // element. The other shuffle indices must be the successive elements after
5162 // the first one.
5163 unsigned ExpectedElt = Imm;
5164 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005165 // Increment the expected index. If it wraps around, it may still be
5166 // a VEXT but the source vectors must be swapped.
5167 ExpectedElt += 1;
5168 if (ExpectedElt == NumElts * 2) {
5169 ExpectedElt = 0;
5170 ReverseVEXT = true;
5171 }
5172
Bob Wilson411dfad2010-08-17 05:54:34 +00005173 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005174 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005175 return false;
5176 }
5177
5178 // Adjust the index value if the source operands will be swapped.
5179 if (ReverseVEXT)
5180 Imm -= NumElts;
5181
Bob Wilson32cd8552009-08-19 17:03:43 +00005182 return true;
5183}
5184
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005185/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5186/// instruction with the specified blocksize. (The order of the elements
5187/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005188static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005189 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5190 "Only possible block sizes for VREV are: 16, 32, 64");
5191
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005192 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005193 if (EltSz == 64)
5194 return false;
5195
5196 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005197 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005198 // If the first shuffle index is UNDEF, be optimistic.
5199 if (M[0] < 0)
5200 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005201
5202 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5203 return false;
5204
5205 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005206 if (M[i] < 0) continue; // ignore UNDEF indices
5207 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005208 return false;
5209 }
5210
5211 return true;
5212}
5213
Benjamin Kramer339ced42012-01-15 13:16:05 +00005214static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005215 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5216 // range, then 0 is placed into the resulting vector. So pretty much any mask
5217 // of 8 elements can work here.
5218 return VT == MVT::v8i8 && M.size() == 8;
5219}
5220
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005221// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5222// checking that pairs of elements in the shuffle mask represent the same index
5223// in each vector, incrementing the expected index by 2 at each step.
5224// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5225// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5226// v2={e,f,g,h}
5227// WhichResult gives the offset for each element in the mask based on which
5228// of the two results it belongs to.
5229//
5230// The transpose can be represented either as:
5231// result1 = shufflevector v1, v2, result1_shuffle_mask
5232// result2 = shufflevector v1, v2, result2_shuffle_mask
5233// where v1/v2 and the shuffle masks have the same number of elements
5234// (here WhichResult (see below) indicates which result is being checked)
5235//
5236// or as:
5237// results = shufflevector v1, v2, shuffle_mask
5238// where both results are returned in one vector and the shuffle mask has twice
5239// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5240// want to check the low half and high half of the shuffle mask as if it were
5241// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005242static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005243 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5244 if (EltSz == 64)
5245 return false;
5246
Bob Wilsona7062312009-08-21 20:54:19 +00005247 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005248 if (M.size() != NumElts && M.size() != NumElts*2)
5249 return false;
5250
James Molloy8c995a92015-09-10 08:42:28 +00005251 // If the mask is twice as long as the input vector then we need to check the
5252 // upper and lower parts of the mask with a matching value for WhichResult
5253 // FIXME: A mask with only even values will be rejected in case the first
5254 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5255 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005256 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005257 if (M.size() == NumElts * 2)
5258 WhichResult = i / NumElts;
5259 else
5260 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005261 for (unsigned j = 0; j < NumElts; j += 2) {
5262 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5263 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5264 return false;
5265 }
Bob Wilsona7062312009-08-21 20:54:19 +00005266 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005267
5268 if (M.size() == NumElts*2)
5269 WhichResult = 0;
5270
Bob Wilsona7062312009-08-21 20:54:19 +00005271 return true;
5272}
5273
Bob Wilson0bbd3072009-12-03 06:40:55 +00005274/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5275/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5276/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005277static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005278 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5279 if (EltSz == 64)
5280 return false;
5281
5282 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005283 if (M.size() != NumElts && M.size() != NumElts*2)
5284 return false;
5285
5286 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005287 if (M.size() == NumElts * 2)
5288 WhichResult = i / NumElts;
5289 else
5290 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005291 for (unsigned j = 0; j < NumElts; j += 2) {
5292 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5293 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5294 return false;
5295 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005296 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005297
5298 if (M.size() == NumElts*2)
5299 WhichResult = 0;
5300
Bob Wilson0bbd3072009-12-03 06:40:55 +00005301 return true;
5302}
5303
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005304// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5305// that the mask elements are either all even and in steps of size 2 or all odd
5306// and in steps of size 2.
5307// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5308// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5309// v2={e,f,g,h}
5310// Requires similar checks to that of isVTRNMask with
5311// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005312static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005313 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5314 if (EltSz == 64)
5315 return false;
5316
Bob Wilsona7062312009-08-21 20:54:19 +00005317 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005318 if (M.size() != NumElts && M.size() != NumElts*2)
5319 return false;
5320
5321 for (unsigned i = 0; i < M.size(); i += NumElts) {
5322 WhichResult = M[i] == 0 ? 0 : 1;
5323 for (unsigned j = 0; j < NumElts; ++j) {
5324 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5325 return false;
5326 }
Bob Wilsona7062312009-08-21 20:54:19 +00005327 }
5328
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005329 if (M.size() == NumElts*2)
5330 WhichResult = 0;
5331
Bob Wilsona7062312009-08-21 20:54:19 +00005332 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005333 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005334 return false;
5335
5336 return true;
5337}
5338
Bob Wilson0bbd3072009-12-03 06:40:55 +00005339/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5340/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5341/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005342static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005343 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5344 if (EltSz == 64)
5345 return false;
5346
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005347 unsigned NumElts = VT.getVectorNumElements();
5348 if (M.size() != NumElts && M.size() != NumElts*2)
5349 return false;
5350
5351 unsigned Half = NumElts / 2;
5352 for (unsigned i = 0; i < M.size(); i += NumElts) {
5353 WhichResult = M[i] == 0 ? 0 : 1;
5354 for (unsigned j = 0; j < NumElts; j += Half) {
5355 unsigned Idx = WhichResult;
5356 for (unsigned k = 0; k < Half; ++k) {
5357 int MIdx = M[i + j + k];
5358 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5359 return false;
5360 Idx += 2;
5361 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005362 }
5363 }
5364
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005365 if (M.size() == NumElts*2)
5366 WhichResult = 0;
5367
Bob Wilson0bbd3072009-12-03 06:40:55 +00005368 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5369 if (VT.is64BitVector() && EltSz == 32)
5370 return false;
5371
5372 return true;
5373}
5374
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005375// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5376// that pairs of elements of the shufflemask represent the same index in each
5377// vector incrementing sequentially through the vectors.
5378// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5379// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5380// v2={e,f,g,h}
5381// Requires similar checks to that of isVTRNMask with respect the how results
5382// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005383static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005384 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5385 if (EltSz == 64)
5386 return false;
5387
Bob Wilsona7062312009-08-21 20:54:19 +00005388 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005389 if (M.size() != NumElts && M.size() != NumElts*2)
5390 return false;
5391
5392 for (unsigned i = 0; i < M.size(); i += NumElts) {
5393 WhichResult = M[i] == 0 ? 0 : 1;
5394 unsigned Idx = WhichResult * NumElts / 2;
5395 for (unsigned j = 0; j < NumElts; j += 2) {
5396 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5397 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5398 return false;
5399 Idx += 1;
5400 }
Bob Wilsona7062312009-08-21 20:54:19 +00005401 }
5402
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005403 if (M.size() == NumElts*2)
5404 WhichResult = 0;
5405
Bob Wilsona7062312009-08-21 20:54:19 +00005406 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005407 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005408 return false;
5409
5410 return true;
5411}
5412
Bob Wilson0bbd3072009-12-03 06:40:55 +00005413/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5414/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5415/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005416static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005417 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5418 if (EltSz == 64)
5419 return false;
5420
5421 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005422 if (M.size() != NumElts && M.size() != NumElts*2)
5423 return false;
5424
5425 for (unsigned i = 0; i < M.size(); i += NumElts) {
5426 WhichResult = M[i] == 0 ? 0 : 1;
5427 unsigned Idx = WhichResult * NumElts / 2;
5428 for (unsigned j = 0; j < NumElts; j += 2) {
5429 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5430 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5431 return false;
5432 Idx += 1;
5433 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005434 }
5435
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005436 if (M.size() == NumElts*2)
5437 WhichResult = 0;
5438
Bob Wilson0bbd3072009-12-03 06:40:55 +00005439 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5440 if (VT.is64BitVector() && EltSz == 32)
5441 return false;
5442
5443 return true;
5444}
5445
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005446/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5447/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5448static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5449 unsigned &WhichResult,
5450 bool &isV_UNDEF) {
5451 isV_UNDEF = false;
5452 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5453 return ARMISD::VTRN;
5454 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5455 return ARMISD::VUZP;
5456 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5457 return ARMISD::VZIP;
5458
5459 isV_UNDEF = true;
5460 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5461 return ARMISD::VTRN;
5462 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5463 return ARMISD::VUZP;
5464 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5465 return ARMISD::VZIP;
5466
5467 return 0;
5468}
5469
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005470/// \return true if this is a reverse operation on an vector.
5471static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5472 unsigned NumElts = VT.getVectorNumElements();
5473 // Make sure the mask has the right size.
5474 if (NumElts != M.size())
5475 return false;
5476
5477 // Look for <15, ..., 3, -1, 1, 0>.
5478 for (unsigned i = 0; i != NumElts; ++i)
5479 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5480 return false;
5481
5482 return true;
5483}
5484
Dale Johannesen2bff5052010-07-29 20:10:08 +00005485// If N is an integer constant that can be moved into a register in one
5486// instruction, return an SDValue of such a constant (will become a MOV
5487// instruction). Otherwise return null.
5488static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005489 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005490 uint64_t Val;
5491 if (!isa<ConstantSDNode>(N))
5492 return SDValue();
5493 Val = cast<ConstantSDNode>(N)->getZExtValue();
5494
5495 if (ST->isThumb1Only()) {
5496 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005497 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005498 } else {
5499 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005500 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005501 }
5502 return SDValue();
5503}
5504
Bob Wilson2e076c42009-06-22 23:27:02 +00005505// If this is a case we can't handle, return null and let the default
5506// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005507SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5508 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005509 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005510 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005511 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005512
5513 APInt SplatBits, SplatUndef;
5514 unsigned SplatBitSize;
5515 bool HasAnyUndefs;
5516 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005517 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005518 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005519 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005520 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005521 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005522 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005523 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005524 if (Val.getNode()) {
5525 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005526 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005527 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005528
5529 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005530 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005531 Val = isNEONModifiedImm(NegatedImm,
5532 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005533 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005534 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005535 if (Val.getNode()) {
5536 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005537 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005538 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005539
5540 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005541 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005542 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005543 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005544 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005545 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5546 }
5547 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005548 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005549 }
5550
Bob Wilson91fdf682010-05-22 00:23:12 +00005551 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005552 //
5553 // As an optimisation, even if more than one value is used it may be more
5554 // profitable to splat with one value then change some lanes.
5555 //
5556 // Heuristically we decide to do this if the vector has a "dominant" value,
5557 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005558 unsigned NumElts = VT.getVectorNumElements();
5559 bool isOnlyLowElement = true;
5560 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005561 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005562 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005563
5564 // Map of the number of times a particular SDValue appears in the
5565 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005566 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005567 SDValue Value;
5568 for (unsigned i = 0; i < NumElts; ++i) {
5569 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005570 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00005571 continue;
5572 if (i > 0)
5573 isOnlyLowElement = false;
5574 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5575 isConstant = false;
5576
James Molloy49bdbce2012-09-06 09:55:02 +00005577 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005578 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005579
James Molloy49bdbce2012-09-06 09:55:02 +00005580 // Is this value dominant? (takes up more than half of the lanes)
5581 if (++Count > (NumElts / 2)) {
5582 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005583 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005584 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005585 }
James Molloy49bdbce2012-09-06 09:55:02 +00005586 if (ValueCounts.size() != 1)
5587 usesOnlyOneValue = false;
5588 if (!Value.getNode() && ValueCounts.size() > 0)
5589 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005590
James Molloy49bdbce2012-09-06 09:55:02 +00005591 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005592 return DAG.getUNDEF(VT);
5593
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005594 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5595 // Keep going if we are hitting this case.
5596 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005597 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5598
Dale Johannesen2bff5052010-07-29 20:10:08 +00005599 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5600
Dale Johannesen710a2d92010-10-19 20:00:17 +00005601 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5602 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005603 if (hasDominantValue && EltSize <= 32) {
5604 if (!isConstant) {
5605 SDValue N;
5606
5607 // If we are VDUPing a value that comes directly from a vector, that will
5608 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005609 // just use VDUPLANE. We can only do this if the lane being extracted
5610 // is at a constant index, as the VDUP from lane instructions only have
5611 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005612 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005613 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00005614 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005615 // We need to create a new undef vector to use for the VDUPLANE if the
5616 // size of the vector from which we get the value is different than the
5617 // size of the vector that we need to create. We will insert the element
5618 // such that the register coalescer will remove unnecessary copies.
5619 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00005620 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5621 VT.getVectorNumElements();
5622 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5623 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005624 Value, DAG.getConstant(index, dl, MVT::i32)),
5625 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005626 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005627 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005628 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005629 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005630 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5631
5632 if (!usesOnlyOneValue) {
5633 // The dominant value was splatted as 'N', but we now have to insert
5634 // all differing elements.
5635 for (unsigned I = 0; I < NumElts; ++I) {
5636 if (Op.getOperand(I) == Value)
5637 continue;
5638 SmallVector<SDValue, 3> Ops;
5639 Ops.push_back(N);
5640 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005641 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005642 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005643 }
5644 }
5645 return N;
5646 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005647 if (VT.getVectorElementType().isFloatingPoint()) {
5648 SmallVector<SDValue, 8> Ops;
5649 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005650 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005651 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005652 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005653 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005654 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5655 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005656 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005657 }
James Molloy49bdbce2012-09-06 09:55:02 +00005658 if (usesOnlyOneValue) {
5659 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5660 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005661 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005662 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005663 }
5664
5665 // If all elements are constants and the case above didn't get hit, fall back
5666 // to the default expansion, which will generate a load from the constant
5667 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005668 if (isConstant)
5669 return SDValue();
5670
Bob Wilson6f2b8962011-01-07 21:37:30 +00005671 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5672 if (NumElts >= 4) {
5673 SDValue shuffle = ReconstructShuffle(Op, DAG);
5674 if (shuffle != SDValue())
5675 return shuffle;
5676 }
5677
Bob Wilson91fdf682010-05-22 00:23:12 +00005678 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005679 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5680 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005681 if (EltSize >= 32) {
5682 // Do the expansion with floating-point types, since that is what the VFP
5683 // registers are defined to use, and since i64 is not legal.
5684 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5685 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005686 SmallVector<SDValue, 8> Ops;
5687 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005688 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005689 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005690 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005691 }
5692
Jim Grosbach24e102a2013-07-08 18:18:52 +00005693 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5694 // know the default expansion would otherwise fall back on something even
5695 // worse. For a vector with one or two non-undef values, that's
5696 // scalar_to_vector for the elements followed by a shuffle (provided the
5697 // shuffle is valid for the target) and materialization element by element
5698 // on the stack followed by a load for everything else.
5699 if (!isConstant && !usesOnlyOneValue) {
5700 SDValue Vec = DAG.getUNDEF(VT);
5701 for (unsigned i = 0 ; i < NumElts; ++i) {
5702 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005703 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00005704 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005705 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005706 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5707 }
5708 return Vec;
5709 }
5710
Bob Wilson2e076c42009-06-22 23:27:02 +00005711 return SDValue();
5712}
5713
Bob Wilson6f2b8962011-01-07 21:37:30 +00005714// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005715// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005716SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5717 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005718 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005719 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005720 EVT VT = Op.getValueType();
5721 unsigned NumElts = VT.getVectorNumElements();
5722
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005723 struct ShuffleSourceInfo {
5724 SDValue Vec;
5725 unsigned MinElt;
5726 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005727
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005728 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
5729 // be compatible with the shuffle we intend to construct. As a result
5730 // ShuffleVec will be some sliding window into the original Vec.
5731 SDValue ShuffleVec;
5732
5733 // Code should guarantee that element i in Vec starts at element "WindowBase
5734 // + i * WindowScale in ShuffleVec".
5735 int WindowBase;
5736 int WindowScale;
5737
5738 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
5739 ShuffleSourceInfo(SDValue Vec)
5740 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
5741 WindowScale(1) {}
5742 };
5743
5744 // First gather all vectors used as an immediate source for this BUILD_VECTOR
5745 // node.
5746 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005747 for (unsigned i = 0; i < NumElts; ++i) {
5748 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005749 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005750 continue;
5751 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5752 // A shuffle can only come from building a vector from various
5753 // elements of other vectors.
5754 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00005755 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
5756 // Furthermore, shuffles require a constant mask, whereas extractelts
5757 // accept variable indices.
5758 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005759 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005760
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005761 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005762 SDValue SourceVec = V.getOperand(0);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005763 auto Source = std::find(Sources.begin(), Sources.end(), SourceVec);
5764 if (Source == Sources.end())
5765 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00005766
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005767 // Update the minimum and maximum lane number seen.
5768 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5769 Source->MinElt = std::min(Source->MinElt, EltNo);
5770 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005771 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005772
Bob Wilson6f2b8962011-01-07 21:37:30 +00005773 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005774 // are involved.
5775 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005776 return SDValue();
5777
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005778 // Find out the smallest element size among result and two sources, and use
5779 // it as element size to build the shuffle_vector.
5780 EVT SmallestEltTy = VT.getVectorElementType();
5781 for (auto &Source : Sources) {
5782 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
5783 if (SrcEltTy.bitsLT(SmallestEltTy))
5784 SmallestEltTy = SrcEltTy;
5785 }
5786 unsigned ResMultiplier =
5787 VT.getVectorElementType().getSizeInBits() / SmallestEltTy.getSizeInBits();
5788 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
5789 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005790
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005791 // If the source vector is too wide or too narrow, we may nevertheless be able
5792 // to construct a compatible shuffle either by concatenating it with UNDEF or
5793 // extracting a suitable range of elements.
5794 for (auto &Src : Sources) {
5795 EVT SrcVT = Src.ShuffleVec.getValueType();
5796
5797 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00005798 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005799
5800 // This stage of the search produces a source with the same element type as
5801 // the original, but with a total width matching the BUILD_VECTOR output.
5802 EVT EltVT = SrcVT.getVectorElementType();
5803 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
5804 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
5805
5806 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
5807 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
5808 return SDValue();
5809 // We can pad out the smaller vector for free, so if it's part of a
5810 // shuffle...
5811 Src.ShuffleVec =
5812 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
5813 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
5814 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005815 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005816
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005817 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
5818 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005819
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005820 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005821 // Span too large for a VEXT to cope
5822 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005823 }
5824
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005825 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005826 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005827 Src.ShuffleVec =
5828 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5829 DAG.getConstant(NumSrcElts, dl, MVT::i32));
5830 Src.WindowBase = -NumSrcElts;
5831 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00005832 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005833 Src.ShuffleVec =
5834 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5835 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005836 } else {
5837 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005838 SDValue VEXTSrc1 =
5839 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5840 DAG.getConstant(0, dl, MVT::i32));
5841 SDValue VEXTSrc2 =
5842 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
5843 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005844
5845 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
5846 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00005847 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005848 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005849 }
5850 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005851
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005852 // Another possible incompatibility occurs from the vector element types. We
5853 // can fix this by bitcasting the source vectors to the same type we intend
5854 // for the shuffle.
5855 for (auto &Src : Sources) {
5856 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
5857 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00005858 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005859 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
5860 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
5861 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
5862 Src.WindowBase *= Src.WindowScale;
5863 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005864
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005865 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00005866 DEBUG(
5867 for (auto Src : Sources)
5868 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
5869 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005870
5871 // The stars all align, our next step is to produce the mask for the shuffle.
5872 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
5873 int BitsPerShuffleLane = ShuffleVT.getVectorElementType().getSizeInBits();
5874 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
5875 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00005876 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005877 continue;
5878
5879 auto Src = std::find(Sources.begin(), Sources.end(), Entry.getOperand(0));
5880 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
5881
5882 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
5883 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
5884 // segment.
5885 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
5886 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
5887 VT.getVectorElementType().getSizeInBits());
5888 int LanesDefined = BitsDefined / BitsPerShuffleLane;
5889
5890 // This source is expected to fill ResMultiplier lanes of the final shuffle,
5891 // starting at the appropriate offset.
5892 int *LaneMask = &Mask[i * ResMultiplier];
5893
5894 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
5895 ExtractBase += NumElts * (Src - Sources.begin());
5896 for (int j = 0; j < LanesDefined; ++j)
5897 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00005898 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005899
Bob Wilson6f2b8962011-01-07 21:37:30 +00005900 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005901 if (!isShuffleMaskLegal(Mask, ShuffleVT))
5902 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005903
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00005904 // We can't handle more than two sources. This should have already
5905 // been checked before this point.
5906 assert(Sources.size() <= 2 && "Too many sources!");
5907
5908 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
5909 for (unsigned i = 0; i < Sources.size(); ++i)
5910 ShuffleOps[i] = Sources[i].ShuffleVec;
5911
5912 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
5913 ShuffleOps[1], &Mask[0]);
5914 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005915}
5916
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005917/// isShuffleMaskLegal - Targets can use this to indicate that they only
5918/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5919/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5920/// are assumed to be legal.
5921bool
5922ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5923 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005924 if (VT.getVectorNumElements() == 4 &&
5925 (VT.is128BitVector() || VT.is64BitVector())) {
5926 unsigned PFIndexes[4];
5927 for (unsigned i = 0; i != 4; ++i) {
5928 if (M[i] < 0)
5929 PFIndexes[i] = 8;
5930 else
5931 PFIndexes[i] = M[i];
5932 }
5933
5934 // Compute the index in the perfect shuffle table.
5935 unsigned PFTableIndex =
5936 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5937 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5938 unsigned Cost = (PFEntry >> 30);
5939
5940 if (Cost <= 4)
5941 return true;
5942 }
5943
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005944 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005945 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005946
Bob Wilson846bd792010-06-07 23:53:38 +00005947 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5948 return (EltSize >= 32 ||
5949 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005950 isVREVMask(M, VT, 64) ||
5951 isVREVMask(M, VT, 32) ||
5952 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005953 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005954 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005955 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005956 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005957}
5958
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005959/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5960/// the specified operations to build the shuffle.
5961static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5962 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005963 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005964 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5965 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5966 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5967
5968 enum {
5969 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5970 OP_VREV,
5971 OP_VDUP0,
5972 OP_VDUP1,
5973 OP_VDUP2,
5974 OP_VDUP3,
5975 OP_VEXT1,
5976 OP_VEXT2,
5977 OP_VEXT3,
5978 OP_VUZPL, // VUZP, left result
5979 OP_VUZPR, // VUZP, right result
5980 OP_VZIPL, // VZIP, left result
5981 OP_VZIPR, // VZIP, right result
5982 OP_VTRNL, // VTRN, left result
5983 OP_VTRNR // VTRN, right result
5984 };
5985
5986 if (OpNum == OP_COPY) {
5987 if (LHSID == (1*9+2)*9+3) return LHS;
5988 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5989 return RHS;
5990 }
5991
5992 SDValue OpLHS, OpRHS;
5993 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5994 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5995 EVT VT = OpLHS.getValueType();
5996
5997 switch (OpNum) {
5998 default: llvm_unreachable("Unknown shuffle opcode!");
5999 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00006000 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00006001 if (VT.getVectorElementType() == MVT::i32 ||
6002 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00006003 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6004 // vrev <4 x i16> -> VREV32
6005 if (VT.getVectorElementType() == MVT::i16)
6006 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6007 // vrev <4 x i8> -> VREV16
6008 assert(VT.getVectorElementType() == MVT::i8);
6009 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006010 case OP_VDUP0:
6011 case OP_VDUP1:
6012 case OP_VDUP2:
6013 case OP_VDUP3:
6014 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006015 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006016 case OP_VEXT1:
6017 case OP_VEXT2:
6018 case OP_VEXT3:
6019 return DAG.getNode(ARMISD::VEXT, dl, VT,
6020 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006021 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006022 case OP_VUZPL:
6023 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006024 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006025 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6026 case OP_VZIPL:
6027 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006028 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006029 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6030 case OP_VTRNL:
6031 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006032 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6033 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006034 }
6035}
6036
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006037static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006038 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006039 SelectionDAG &DAG) {
6040 // Check to see if we can use the VTBL instruction.
6041 SDValue V1 = Op.getOperand(0);
6042 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006043 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006044
6045 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006046 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006047 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006048 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006049
Sanjay Patel57195842016-03-14 17:28:46 +00006050 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006051 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00006052 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006053
Owen Anderson77aa2662011-04-05 21:48:57 +00006054 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00006055 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006056}
6057
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006058static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6059 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006060 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006061 SDValue OpLHS = Op.getOperand(0);
6062 EVT VT = OpLHS.getValueType();
6063
6064 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6065 "Expect an v8i16/v16i8 type");
6066 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6067 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6068 // extract the first 8 bytes into the top double word and the last 8 bytes
6069 // into the bottom double word. The v8i16 case is similar.
6070 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6071 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006072 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006073}
6074
Bob Wilson2e076c42009-06-22 23:27:02 +00006075static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006076 SDValue V1 = Op.getOperand(0);
6077 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006078 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006079 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006080 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006081
Bob Wilsonc6800b52009-08-13 02:13:04 +00006082 // Convert shuffles that are directly supported on NEON to target-specific
6083 // DAG nodes, instead of keeping them as shuffles and matching them again
6084 // during code selection. This is more efficient and avoids the possibility
6085 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006086 // FIXME: floating-point vectors should be canonicalized to integer vectors
6087 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006088 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006089
Bob Wilson846bd792010-06-07 23:53:38 +00006090 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6091 if (EltSize <= 32) {
6092 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
6093 int Lane = SVN->getSplatIndex();
6094 // If this is undef splat, generate it via "just" vdup, if possible.
6095 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006096
Dan Gohman198b7ff2011-11-03 21:49:52 +00006097 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006098 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6099 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6100 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006101 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6102 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6103 // reaches it).
6104 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6105 !isa<ConstantSDNode>(V1.getOperand(0))) {
6106 bool IsScalarToVector = true;
6107 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006108 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006109 IsScalarToVector = false;
6110 break;
6111 }
6112 if (IsScalarToVector)
6113 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6114 }
Bob Wilson846bd792010-06-07 23:53:38 +00006115 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006116 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006117 }
Bob Wilson846bd792010-06-07 23:53:38 +00006118
6119 bool ReverseVEXT;
6120 unsigned Imm;
6121 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6122 if (ReverseVEXT)
6123 std::swap(V1, V2);
6124 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006125 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006126 }
6127
6128 if (isVREVMask(ShuffleMask, VT, 64))
6129 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6130 if (isVREVMask(ShuffleMask, VT, 32))
6131 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6132 if (isVREVMask(ShuffleMask, VT, 16))
6133 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6134
Sanjay Patel57195842016-03-14 17:28:46 +00006135 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006136 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006137 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006138 }
6139
Bob Wilson846bd792010-06-07 23:53:38 +00006140 // Check for Neon shuffles that modify both input vectors in place.
6141 // If both results are used, i.e., if there are two shuffles with the same
6142 // source operands and with masks corresponding to both results of one of
6143 // these operations, DAG memoization will ensure that a single node is
6144 // used for both shuffles.
6145 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006146 bool isV_UNDEF;
6147 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6148 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6149 if (isV_UNDEF)
6150 V2 = V1;
6151 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6152 .getValue(WhichResult);
6153 }
Bob Wilson846bd792010-06-07 23:53:38 +00006154
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006155 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6156 // shuffles that produce a result larger than their operands with:
6157 // shuffle(concat(v1, undef), concat(v2, undef))
6158 // ->
6159 // shuffle(concat(v1, v2), undef)
6160 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6161 //
6162 // This is useful in the general case, but there are special cases where
6163 // native shuffles produce larger results: the two-result ops.
6164 //
6165 // Look through the concat when lowering them:
6166 // shuffle(concat(v1, v2), undef)
6167 // ->
6168 // concat(VZIP(v1, v2):0, :1)
6169 //
Sanjay Patel57195842016-03-14 17:28:46 +00006170 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006171 SDValue SubV1 = V1->getOperand(0);
6172 SDValue SubV2 = V1->getOperand(1);
6173 EVT SubVT = SubV1.getValueType();
6174
6175 // We expect these to have been canonicalized to -1.
6176 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
6177 return i < (int)VT.getVectorNumElements();
6178 }) && "Unexpected shuffle index into UNDEF operand!");
6179
6180 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6181 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6182 if (isV_UNDEF)
6183 SubV2 = SubV1;
6184 assert((WhichResult == 0) &&
6185 "In-place shuffle of concat can only have one result!");
6186 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6187 SubV1, SubV2);
6188 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6189 Res.getValue(1));
6190 }
6191 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006192 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006193
Bob Wilsona7062312009-08-21 20:54:19 +00006194 // If the shuffle is not directly supported and it has 4 elements, use
6195 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006196 unsigned NumElts = VT.getVectorNumElements();
6197 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006198 unsigned PFIndexes[4];
6199 for (unsigned i = 0; i != 4; ++i) {
6200 if (ShuffleMask[i] < 0)
6201 PFIndexes[i] = 8;
6202 else
6203 PFIndexes[i] = ShuffleMask[i];
6204 }
6205
6206 // Compute the index in the perfect shuffle table.
6207 unsigned PFTableIndex =
6208 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006209 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6210 unsigned Cost = (PFEntry >> 30);
6211
6212 if (Cost <= 4)
6213 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6214 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006215
Bob Wilsond8a9a042010-06-04 00:04:02 +00006216 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006217 if (EltSize >= 32) {
6218 // Do the expansion with floating-point types, since that is what the VFP
6219 // registers are defined to use, and since i64 is not legal.
6220 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6221 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006222 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6223 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006224 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006225 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006226 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006227 Ops.push_back(DAG.getUNDEF(EltVT));
6228 else
6229 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6230 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6231 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006232 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006233 }
Craig Topper48d114b2014-04-26 18:35:24 +00006234 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006235 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006236 }
6237
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006238 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6239 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6240
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006241 if (VT == MVT::v8i8)
6242 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006243 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006244
Bob Wilson6f34e272009-08-14 05:16:33 +00006245 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006246}
6247
Eli Friedmana5e244c2011-10-24 23:08:52 +00006248static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6249 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6250 SDValue Lane = Op.getOperand(2);
6251 if (!isa<ConstantSDNode>(Lane))
6252 return SDValue();
6253
6254 return Op;
6255}
6256
Bob Wilson2e076c42009-06-22 23:27:02 +00006257static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006258 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006259 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006260 if (!isa<ConstantSDNode>(Lane))
6261 return SDValue();
6262
6263 SDValue Vec = Op.getOperand(0);
6264 if (Op.getValueType() == MVT::i32 &&
6265 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006266 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006267 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6268 }
6269
6270 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006271}
6272
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006273static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6274 // The only time a CONCAT_VECTORS operation can have legal types is when
6275 // two 64-bit vectors are concatenated to a 128-bit vector.
6276 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6277 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006278 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006279 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006280 SDValue Op0 = Op.getOperand(0);
6281 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006282 if (!Op0.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, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006285 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006286 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006287 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006288 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006289 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006290 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006291}
6292
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006293/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6294/// element has been zero/sign-extended, depending on the isSigned parameter,
6295/// from an integer type half its size.
6296static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6297 bool isSigned) {
6298 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6299 EVT VT = N->getValueType(0);
6300 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6301 SDNode *BVN = N->getOperand(0).getNode();
6302 if (BVN->getValueType(0) != MVT::v4i32 ||
6303 BVN->getOpcode() != ISD::BUILD_VECTOR)
6304 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006305 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006306 unsigned HiElt = 1 - LoElt;
6307 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6308 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6309 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6310 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6311 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6312 return false;
6313 if (isSigned) {
6314 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6315 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6316 return true;
6317 } else {
6318 if (Hi0->isNullValue() && Hi1->isNullValue())
6319 return true;
6320 }
6321 return false;
6322 }
6323
6324 if (N->getOpcode() != ISD::BUILD_VECTOR)
6325 return false;
6326
6327 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6328 SDNode *Elt = N->getOperand(i).getNode();
6329 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6330 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6331 unsigned HalfSize = EltSize / 2;
6332 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006333 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006334 return false;
6335 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006336 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006337 return false;
6338 }
6339 continue;
6340 }
6341 return false;
6342 }
6343
6344 return true;
6345}
6346
6347/// isSignExtended - Check if a node is a vector value that is sign-extended
6348/// or a constant BUILD_VECTOR with sign-extended elements.
6349static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6350 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6351 return true;
6352 if (isExtendedBUILD_VECTOR(N, DAG, true))
6353 return true;
6354 return false;
6355}
6356
6357/// isZeroExtended - Check if a node is a vector value that is zero-extended
6358/// or a constant BUILD_VECTOR with zero-extended elements.
6359static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6360 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6361 return true;
6362 if (isExtendedBUILD_VECTOR(N, DAG, false))
6363 return true;
6364 return false;
6365}
6366
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006367static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6368 if (OrigVT.getSizeInBits() >= 64)
6369 return OrigVT;
6370
6371 assert(OrigVT.isSimple() && "Expecting a simple value type");
6372
6373 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6374 switch (OrigSimpleTy) {
6375 default: llvm_unreachable("Unexpected Vector Type");
6376 case MVT::v2i8:
6377 case MVT::v2i16:
6378 return MVT::v2i32;
6379 case MVT::v4i8:
6380 return MVT::v4i16;
6381 }
6382}
6383
Sebastian Popa204f722012-11-30 19:08:04 +00006384/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6385/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6386/// We insert the required extension here to get the vector to fill a D register.
6387static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6388 const EVT &OrigTy,
6389 const EVT &ExtTy,
6390 unsigned ExtOpcode) {
6391 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6392 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6393 // 64-bits we need to insert a new extension so that it will be 64-bits.
6394 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6395 if (OrigTy.getSizeInBits() >= 64)
6396 return N;
6397
6398 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006399 EVT NewVT = getExtensionTo64Bits(OrigTy);
6400
Andrew Trickef9de2a2013-05-25 02:42:55 +00006401 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006402}
6403
6404/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6405/// does not do any sign/zero extension. If the original vector is less
6406/// than 64 bits, an appropriate extension will be added after the load to
6407/// reach a total size of 64 bits. We have to add the extension separately
6408/// because ARM does not have a sign/zero extending load for vectors.
6409static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006410 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6411
6412 // The load already has the right type.
6413 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006414 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006415 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6416 LD->isNonTemporal(), LD->isInvariant(),
6417 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006418
6419 // We need to create a zextload/sextload. We cannot just create a load
6420 // followed by a zext/zext node because LowerMUL is also run during normal
6421 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006422 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006423 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006424 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006425 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006426}
6427
6428/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6429/// extending load, or BUILD_VECTOR with extended elements, return the
6430/// unextended value. The unextended vector should be 64 bits so that it can
6431/// be used as an operand to a VMULL instruction. If the original vector size
6432/// before extension is less than 64 bits we add a an extension to resize
6433/// the vector to 64 bits.
6434static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006435 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006436 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6437 N->getOperand(0)->getValueType(0),
6438 N->getValueType(0),
6439 N->getOpcode());
6440
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006441 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006442 return SkipLoadExtensionForVMULL(LD, DAG);
6443
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006444 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6445 // have been legalized as a BITCAST from v4i32.
6446 if (N->getOpcode() == ISD::BITCAST) {
6447 SDNode *BVN = N->getOperand(0).getNode();
6448 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6449 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006450 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006451 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006452 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6453 }
6454 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6455 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6456 EVT VT = N->getValueType(0);
6457 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6458 unsigned NumElts = VT.getVectorNumElements();
6459 MVT TruncVT = MVT::getIntegerVT(EltSize);
6460 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006461 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006462 for (unsigned i = 0; i != NumElts; ++i) {
6463 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6464 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006465 // Element types smaller than 32 bits are not legal, so use i32 elements.
6466 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006467 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006468 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006469 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006470 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006471}
6472
Evan Chenge2086e72011-03-29 01:56:09 +00006473static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6474 unsigned Opcode = N->getOpcode();
6475 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6476 SDNode *N0 = N->getOperand(0).getNode();
6477 SDNode *N1 = N->getOperand(1).getNode();
6478 return N0->hasOneUse() && N1->hasOneUse() &&
6479 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6480 }
6481 return false;
6482}
6483
6484static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6485 unsigned Opcode = N->getOpcode();
6486 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6487 SDNode *N0 = N->getOperand(0).getNode();
6488 SDNode *N1 = N->getOperand(1).getNode();
6489 return N0->hasOneUse() && N1->hasOneUse() &&
6490 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6491 }
6492 return false;
6493}
6494
Bob Wilson38ab35a2010-09-01 23:50:19 +00006495static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6496 // Multiplications are only custom-lowered for 128-bit vectors so that
6497 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6498 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006499 assert(VT.is128BitVector() && VT.isInteger() &&
6500 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006501 SDNode *N0 = Op.getOperand(0).getNode();
6502 SDNode *N1 = Op.getOperand(1).getNode();
6503 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006504 bool isMLA = false;
6505 bool isN0SExt = isSignExtended(N0, DAG);
6506 bool isN1SExt = isSignExtended(N1, DAG);
6507 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006508 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006509 else {
6510 bool isN0ZExt = isZeroExtended(N0, DAG);
6511 bool isN1ZExt = isZeroExtended(N1, DAG);
6512 if (isN0ZExt && isN1ZExt)
6513 NewOpc = ARMISD::VMULLu;
6514 else if (isN1SExt || isN1ZExt) {
6515 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6516 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6517 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6518 NewOpc = ARMISD::VMULLs;
6519 isMLA = true;
6520 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6521 NewOpc = ARMISD::VMULLu;
6522 isMLA = true;
6523 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6524 std::swap(N0, N1);
6525 NewOpc = ARMISD::VMULLu;
6526 isMLA = true;
6527 }
6528 }
6529
6530 if (!NewOpc) {
6531 if (VT == MVT::v2i64)
6532 // Fall through to expand this. It is not legal.
6533 return SDValue();
6534 else
6535 // Other vector multiplications are legal.
6536 return Op;
6537 }
6538 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006539
6540 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006541 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006542 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006543 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006544 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006545 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006546 assert(Op0.getValueType().is64BitVector() &&
6547 Op1.getValueType().is64BitVector() &&
6548 "unexpected types for extended operands to VMULL");
6549 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6550 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006551
Evan Chenge2086e72011-03-29 01:56:09 +00006552 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6553 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6554 // vmull q0, d4, d6
6555 // vmlal q0, d5, d6
6556 // is faster than
6557 // vaddl q0, d4, d5
6558 // vmovl q1, d6
6559 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006560 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6561 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006562 EVT Op1VT = Op1.getValueType();
6563 return DAG.getNode(N0->getOpcode(), DL, VT,
6564 DAG.getNode(NewOpc, DL, VT,
6565 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6566 DAG.getNode(NewOpc, DL, VT,
6567 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006568}
6569
Owen Anderson77aa2662011-04-05 21:48:57 +00006570static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006571LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006572 // TODO: Should this propagate fast-math-flags?
6573
Nate Begemanfa62d502011-02-11 20:53:29 +00006574 // Convert to float
6575 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6576 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6577 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6578 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6579 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6580 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6581 // Get reciprocal estimate.
6582 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006583 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006584 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6585 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006586 // Because char has a smaller range than uchar, we can actually get away
6587 // without any newton steps. This requires that we use a weird bias
6588 // of 0xb000, however (again, this has been exhaustively tested).
6589 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6590 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6591 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006592 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006593 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6594 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6595 // Convert back to short.
6596 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6597 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6598 return X;
6599}
6600
Owen Anderson77aa2662011-04-05 21:48:57 +00006601static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006602LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006603 // TODO: Should this propagate fast-math-flags?
6604
Nate Begemanfa62d502011-02-11 20:53:29 +00006605 SDValue N2;
6606 // Convert to float.
6607 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6608 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6609 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6610 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6611 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6612 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006613
Nate Begemanfa62d502011-02-11 20:53:29 +00006614 // Use reciprocal estimate and one refinement step.
6615 // float4 recip = vrecpeq_f32(yf);
6616 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006617 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006618 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6619 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006620 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006621 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006622 N1, N2);
6623 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6624 // Because short has a smaller range than ushort, we can actually get away
6625 // with only a single newton step. This requires that we use a weird bias
6626 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006627 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006628 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6629 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006630 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006631 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6632 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6633 // Convert back to integer and return.
6634 // return vmovn_s32(vcvt_s32_f32(result));
6635 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6636 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6637 return N0;
6638}
6639
6640static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6641 EVT VT = Op.getValueType();
6642 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6643 "unexpected type for custom-lowering ISD::SDIV");
6644
Andrew Trickef9de2a2013-05-25 02:42:55 +00006645 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006646 SDValue N0 = Op.getOperand(0);
6647 SDValue N1 = Op.getOperand(1);
6648 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006649
Nate Begemanfa62d502011-02-11 20:53:29 +00006650 if (VT == MVT::v8i8) {
6651 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6652 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006653
Nate Begemanfa62d502011-02-11 20:53:29 +00006654 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006655 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006656 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006657 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006658 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006659 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006660 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006661 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006662
6663 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6664 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6665
6666 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6667 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006668
Nate Begemanfa62d502011-02-11 20:53:29 +00006669 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6670 return N0;
6671 }
6672 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6673}
6674
6675static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00006676 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00006677 EVT VT = Op.getValueType();
6678 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6679 "unexpected type for custom-lowering ISD::UDIV");
6680
Andrew Trickef9de2a2013-05-25 02:42:55 +00006681 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006682 SDValue N0 = Op.getOperand(0);
6683 SDValue N1 = Op.getOperand(1);
6684 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006685
Nate Begemanfa62d502011-02-11 20:53:29 +00006686 if (VT == MVT::v8i8) {
6687 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6688 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006689
Nate Begemanfa62d502011-02-11 20:53:29 +00006690 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006691 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006692 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006693 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006694 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006695 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006696 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006697 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006698
Nate Begemanfa62d502011-02-11 20:53:29 +00006699 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6700 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006701
Nate Begemanfa62d502011-02-11 20:53:29 +00006702 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6703 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006704
6705 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006706 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6707 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006708 N0);
6709 return N0;
6710 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006711
Nate Begemanfa62d502011-02-11 20:53:29 +00006712 // v4i16 sdiv ... Convert to float.
6713 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6714 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6715 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6716 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6717 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006718 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006719
6720 // Use reciprocal estimate and two refinement steps.
6721 // float4 recip = vrecpeq_f32(yf);
6722 // recip *= vrecpsq_f32(yf, recip);
6723 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006724 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006725 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6726 BN1);
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);
Owen Anderson77aa2662011-04-05 21:48:57 +00006731 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006732 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006733 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006734 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6735 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6736 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6737 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006738 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006739 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6740 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00006741 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006742 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6743 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6744 // Convert back to integer and return.
6745 // return vmovn_u32(vcvt_s32_f32(result));
6746 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6747 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6748 return N0;
6749}
6750
Evan Chenge8916542011-08-30 01:34:54 +00006751static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6752 EVT VT = Op.getNode()->getValueType(0);
6753 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6754
6755 unsigned Opc;
6756 bool ExtraOp = false;
6757 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006758 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006759 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6760 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6761 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6762 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6763 }
6764
6765 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006766 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006767 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006768 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006769 Op.getOperand(1), Op.getOperand(2));
6770}
6771
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006772SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6773 assert(Subtarget->isTargetDarwin());
6774
6775 // For iOS, we want to call an alternative entry point: __sincos_stret,
6776 // return values are passed via sret.
6777 SDLoc dl(Op);
6778 SDValue Arg = Op.getOperand(0);
6779 EVT ArgVT = Arg.getValueType();
6780 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006781 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006782
6783 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00006784 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006785
6786 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00006787 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00006788 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006789
6790 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00006791 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
6792 SDValue SRet;
6793 if (ShouldUseSRet) {
6794 // Create stack object for sret.
6795 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6796 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
6797 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6798 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
6799
6800 ArgListEntry Entry;
6801 Entry.Node = SRet;
6802 Entry.Ty = RetTy->getPointerTo();
6803 Entry.isSExt = false;
6804 Entry.isZExt = false;
6805 Entry.isSRet = true;
6806 Args.push_back(Entry);
6807 RetTy = Type::getVoidTy(*DAG.getContext());
6808 }
6809
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006810 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006811 Entry.Node = Arg;
6812 Entry.Ty = ArgTy;
6813 Entry.isSExt = false;
6814 Entry.isZExt = false;
6815 Args.push_back(Entry);
6816
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00006817 const char *LibcallName =
6818 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00006819 RTLIB::Libcall LC =
6820 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
6821 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00006822 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006823
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006824 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00006825 CLI.setDebugLoc(dl)
6826 .setChain(DAG.getEntryNode())
6827 .setCallee(CC, RetTy, Callee, std::move(Args), 0)
6828 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006829 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6830
Tim Northover8b403662015-10-28 22:51:16 +00006831 if (!ShouldUseSRet)
6832 return CallResult.first;
6833
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006834 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6835 MachinePointerInfo(), false, false, false, 0);
6836
6837 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006838 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006839 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006840 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6841 MachinePointerInfo(), false, false, false, 0);
6842
6843 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6844 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6845 LoadSin.getValue(0), LoadCos.getValue(0));
6846}
6847
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006848SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00006849 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006850 SDValue &Chain) const {
6851 EVT VT = Op.getValueType();
6852 assert((VT == MVT::i32 || VT == MVT::i64) &&
6853 "unexpected type for custom lowering DIV");
6854 SDLoc dl(Op);
6855
6856 const auto &DL = DAG.getDataLayout();
6857 const auto &TLI = DAG.getTargetLoweringInfo();
6858
6859 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00006860 if (Signed)
6861 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
6862 else
6863 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006864
6865 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
6866
6867 ARMTargetLowering::ArgListTy Args;
6868
6869 for (auto AI : {1, 0}) {
6870 ArgListEntry Arg;
6871 Arg.Node = Op.getOperand(AI);
6872 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
6873 Args.push_back(Arg);
6874 }
6875
6876 CallLoweringInfo CLI(DAG);
6877 CLI.setDebugLoc(dl)
6878 .setChain(Chain)
6879 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
6880 ES, std::move(Args), 0);
6881
6882 return LowerCallTo(CLI).first;
6883}
6884
Martell Maloned1229242015-11-26 15:34:03 +00006885SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
6886 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006887 assert(Op.getValueType() == MVT::i32 &&
6888 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006889 SDLoc dl(Op);
6890
6891 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
6892 DAG.getEntryNode(), Op.getOperand(1));
6893
Martell Maloned1229242015-11-26 15:34:03 +00006894 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006895}
6896
6897void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00006898 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006899 SmallVectorImpl<SDValue> &Results) const {
6900 const auto &DL = DAG.getDataLayout();
6901 const auto &TLI = DAG.getTargetLoweringInfo();
6902
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00006903 assert(Op.getValueType() == MVT::i64 &&
6904 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006905 SDLoc dl(Op);
6906
6907 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6908 DAG.getConstant(0, dl, MVT::i32));
6909 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op.getOperand(1),
6910 DAG.getConstant(1, dl, MVT::i32));
6911 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i32, Lo, Hi);
6912
6913 SDValue DBZCHK =
6914 DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other, DAG.getEntryNode(), Or);
6915
Martell Maloned1229242015-11-26 15:34:03 +00006916 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00006917
6918 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
6919 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
6920 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
6921 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
6922
6923 Results.push_back(Lower);
6924 Results.push_back(Upper);
6925}
6926
Eli Friedman10f9ce22011-09-15 22:26:18 +00006927static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006928 // Monotonic load/store is legal for all targets
6929 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6930 return Op;
6931
Alp Tokercb402912014-01-24 17:20:08 +00006932 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006933 // dmb or equivalent available.
6934 return SDValue();
6935}
6936
Tim Northoverbc933082013-05-23 19:11:20 +00006937static void ReplaceREADCYCLECOUNTER(SDNode *N,
6938 SmallVectorImpl<SDValue> &Results,
6939 SelectionDAG &DAG,
6940 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006941 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006942 // Under Power Management extensions, the cycle-count is:
6943 // mrc p15, #0, <Rt>, c9, c13, #0
6944 SDValue Ops[] = { N->getOperand(0), // Chain
6945 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6946 DAG.getConstant(15, DL, MVT::i32),
6947 DAG.getConstant(0, DL, MVT::i32),
6948 DAG.getConstant(9, DL, MVT::i32),
6949 DAG.getConstant(13, DL, MVT::i32),
6950 DAG.getConstant(0, DL, MVT::i32)
6951 };
Tim Northoverbc933082013-05-23 19:11:20 +00006952
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00006953 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
6954 DAG.getVTList(MVT::i32, MVT::Other), Ops);
6955 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
6956 DAG.getConstant(0, DL, MVT::i32)));
6957 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00006958}
6959
Dan Gohman21cea8a2010-04-17 15:26:15 +00006960SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006961 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006962 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006963 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006964 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006965 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006966 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006967 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6968 default: llvm_unreachable("unknown object format");
6969 case Triple::COFF:
6970 return LowerGlobalAddressWindows(Op, DAG);
6971 case Triple::ELF:
6972 return LowerGlobalAddressELF(Op, DAG);
6973 case Triple::MachO:
6974 return LowerGlobalAddressDarwin(Op, DAG);
6975 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006976 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006977 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006978 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6979 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006980 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006981 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006982 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006983 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006984 case ISD::SINT_TO_FP:
6985 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6986 case ISD::FP_TO_SINT:
6987 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006988 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006989 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006990 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006991 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006992 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006993 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006994 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6995 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006996 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006997 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006998 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006999 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00007000 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
7001 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007002 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00007003 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007004 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007005 case ISD::CTTZ:
7006 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007007 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007008 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007009 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007010 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007011 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007012 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007013 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007014 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007015 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007016 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00007017 case ISD::SDIV:
7018 if (Subtarget->isTargetWindows())
7019 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
7020 return LowerSDIV(Op, DAG);
7021 case ISD::UDIV:
7022 if (Subtarget->isTargetWindows())
7023 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
7024 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007025 case ISD::ADDC:
7026 case ISD::ADDE:
7027 case ISD::SUBC:
7028 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007029 case ISD::SADDO:
7030 case ISD::UADDO:
7031 case ISD::SSUBO:
7032 case ISD::USUBO:
7033 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007034 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007035 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007036 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007037 case ISD::SDIVREM:
7038 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007039 case ISD::DYNAMIC_STACKALLOC:
7040 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7041 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7042 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007043 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7044 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007045 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007046 }
Evan Cheng10043e22007-01-19 07:51:42 +00007047}
7048
Duncan Sands6ed40142008-12-01 11:39:25 +00007049/// ReplaceNodeResults - Replace the results of node with an illegal result
7050/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007051void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007052 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007053 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007054 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007055 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007056 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007057 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007058 case ISD::READ_REGISTER:
7059 ExpandREAD_REGISTER(N, Results, DAG);
7060 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007061 case ISD::BITCAST:
7062 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007063 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007064 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007065 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007066 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007067 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007068 case ISD::SREM:
7069 case ISD::UREM:
7070 Res = LowerREM(N, DAG);
7071 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007072 case ISD::SDIVREM:
7073 case ISD::UDIVREM:
7074 Res = LowerDivRem(SDValue(N, 0), DAG);
7075 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7076 Results.push_back(Res.getValue(0));
7077 Results.push_back(Res.getValue(1));
7078 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007079 case ISD::READCYCLECOUNTER:
7080 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7081 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007082 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007083 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007084 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007085 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7086 Results);
Duncan Sands6ed40142008-12-01 11:39:25 +00007087 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007088 if (Res.getNode())
7089 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007090}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007091
Evan Cheng10043e22007-01-19 07:51:42 +00007092//===----------------------------------------------------------------------===//
7093// ARM Scheduler Hooks
7094//===----------------------------------------------------------------------===//
7095
Bill Wendling030b58e2011-10-06 22:18:16 +00007096/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7097/// registers the function context.
7098void ARMTargetLowering::
7099SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
7100 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007101 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00007102 DebugLoc dl = MI->getDebugLoc();
7103 MachineFunction *MF = MBB->getParent();
7104 MachineRegisterInfo *MRI = &MF->getRegInfo();
7105 MachineConstantPool *MCP = MF->getConstantPool();
7106 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7107 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007108
Bill Wendling374ee192011-10-03 21:25:38 +00007109 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007110 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007111
Bill Wendling374ee192011-10-03 21:25:38 +00007112 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007113 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007114 ARMConstantPoolValue *CPV =
7115 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7116 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7117
Craig Topper61e88f42014-11-21 05:58:21 +00007118 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7119 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007120
Bill Wendling030b58e2011-10-06 22:18:16 +00007121 // Grab constant pool and fixed stack memory operands.
7122 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007123 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7124 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007125
7126 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007127 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7128 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007129
7130 // Load the address of the dispatch MBB into the jump buffer.
7131 if (isThumb2) {
7132 // Incoming value: jbuf
7133 // ldr.n r5, LCPI1_1
7134 // orr r5, r5, #1
7135 // add r5, pc
7136 // str r5, [$jbuf, #+4] ; &jbuf[1]
7137 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7138 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7139 .addConstantPoolIndex(CPI)
7140 .addMemOperand(CPMMO));
7141 // Set the low bit because of thumb mode.
7142 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7143 AddDefaultCC(
7144 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7145 .addReg(NewVReg1, RegState::Kill)
7146 .addImm(0x01)));
7147 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7148 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7149 .addReg(NewVReg2, RegState::Kill)
7150 .addImm(PCLabelId);
7151 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7152 .addReg(NewVReg3, RegState::Kill)
7153 .addFrameIndex(FI)
7154 .addImm(36) // &jbuf[1] :: pc
7155 .addMemOperand(FIMMOSt));
7156 } else if (isThumb) {
7157 // Incoming value: jbuf
7158 // ldr.n r1, LCPI1_4
7159 // add r1, pc
7160 // mov r2, #1
7161 // orrs r1, r2
7162 // add r2, $jbuf, #+4 ; &jbuf[1]
7163 // str r1, [r2]
7164 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7165 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7166 .addConstantPoolIndex(CPI)
7167 .addMemOperand(CPMMO));
7168 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7169 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7170 .addReg(NewVReg1, RegState::Kill)
7171 .addImm(PCLabelId);
7172 // Set the low bit because of thumb mode.
7173 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7174 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7175 .addReg(ARM::CPSR, RegState::Define)
7176 .addImm(1));
7177 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7178 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7179 .addReg(ARM::CPSR, RegState::Define)
7180 .addReg(NewVReg2, RegState::Kill)
7181 .addReg(NewVReg3, RegState::Kill));
7182 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007183 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7184 .addFrameIndex(FI)
7185 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007186 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7187 .addReg(NewVReg4, RegState::Kill)
7188 .addReg(NewVReg5, RegState::Kill)
7189 .addImm(0)
7190 .addMemOperand(FIMMOSt));
7191 } else {
7192 // Incoming value: jbuf
7193 // ldr r1, LCPI1_1
7194 // add r1, pc, r1
7195 // str r1, [$jbuf, #+4] ; &jbuf[1]
7196 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7197 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7198 .addConstantPoolIndex(CPI)
7199 .addImm(0)
7200 .addMemOperand(CPMMO));
7201 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7202 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7203 .addReg(NewVReg1, RegState::Kill)
7204 .addImm(PCLabelId));
7205 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7206 .addReg(NewVReg2, RegState::Kill)
7207 .addFrameIndex(FI)
7208 .addImm(36) // &jbuf[1] :: pc
7209 .addMemOperand(FIMMOSt));
7210 }
7211}
7212
Matthias Brauneec4efc2015-04-28 00:37:05 +00007213void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
7214 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007215 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007216 DebugLoc dl = MI->getDebugLoc();
7217 MachineFunction *MF = MBB->getParent();
7218 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00007219 MachineFrameInfo *MFI = MF->getFrameInfo();
7220 int FI = MFI->getFunctionContextIndex();
7221
Craig Topper61e88f42014-11-21 05:58:21 +00007222 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7223 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007224
Bill Wendling362c1b02011-10-06 21:29:56 +00007225 // Get a mapping of the call site numbers to all of the landing pads they're
7226 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007227 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7228 unsigned MaxCSNum = 0;
7229 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007230 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7231 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007232 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007233
7234 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7235 // pad.
7236 for (MachineBasicBlock::iterator
7237 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7238 if (!II->isEHLabel()) continue;
7239
7240 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007241 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007242
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007243 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7244 for (SmallVectorImpl<unsigned>::iterator
7245 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7246 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007247 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007248 MaxCSNum = std::max(MaxCSNum, *CSI);
7249 }
Bill Wendling202803e2011-10-05 00:02:33 +00007250 break;
7251 }
7252 }
7253
7254 // Get an ordered list of the machine basic blocks for the jump table.
7255 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007256 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007257 LPadList.reserve(CallSiteNumToLPad.size());
7258 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7259 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7260 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007261 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007262 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007263 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7264 }
Bill Wendling202803e2011-10-05 00:02:33 +00007265 }
7266
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007267 assert(!LPadList.empty() &&
7268 "No landing pad destinations for the dispatch jump table!");
7269
Bill Wendling362c1b02011-10-06 21:29:56 +00007270 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007271 MachineJumpTableInfo *JTI =
7272 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7273 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00007274 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00007275
Bill Wendling362c1b02011-10-06 21:29:56 +00007276 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007277
7278 // Shove the dispatch's address into the return slot in the function context.
7279 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007280 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007281
Bill Wendling324be982011-10-05 00:39:32 +00007282 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007283 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007284 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007285 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007286 else
7287 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7288
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007289 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007290 DispatchBB->addSuccessor(TrapBB);
7291
7292 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7293 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007294
Bill Wendling510fbcd2011-10-17 21:32:56 +00007295 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007296 MF->insert(MF->end(), DispatchBB);
7297 MF->insert(MF->end(), DispContBB);
7298 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007299
Bill Wendling030b58e2011-10-06 22:18:16 +00007300 // Insert code into the entry block that creates and registers the function
7301 // context.
7302 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7303
Alex Lorenze40c8a22015-08-11 23:09:45 +00007304 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7305 MachinePointerInfo::getFixedStack(*MF, FI),
7306 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007307
Chad Rosier1ec8e402012-11-06 23:05:24 +00007308 MachineInstrBuilder MIB;
7309 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7310
7311 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7312 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7313
7314 // Add a register mask with no preserved registers. This results in all
7315 // registers being marked as clobbered.
7316 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00007317
Bill Wendling85833f72011-10-18 22:49:07 +00007318 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007319 if (Subtarget->isThumb2()) {
7320 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7321 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7322 .addFrameIndex(FI)
7323 .addImm(4)
7324 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007325
Bill Wendling85833f72011-10-18 22:49:07 +00007326 if (NumLPads < 256) {
7327 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7328 .addReg(NewVReg1)
7329 .addImm(LPadList.size()));
7330 } else {
7331 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7332 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007333 .addImm(NumLPads & 0xFFFF));
7334
7335 unsigned VReg2 = VReg1;
7336 if ((NumLPads & 0xFFFF0000) != 0) {
7337 VReg2 = MRI->createVirtualRegister(TRC);
7338 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7339 .addReg(VReg1)
7340 .addImm(NumLPads >> 16));
7341 }
7342
Bill Wendling85833f72011-10-18 22:49:07 +00007343 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7344 .addReg(NewVReg1)
7345 .addReg(VReg2));
7346 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007347
Bill Wendling5626c662011-10-06 22:53:00 +00007348 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7349 .addMBB(TrapBB)
7350 .addImm(ARMCC::HI)
7351 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007352
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007353 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7354 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007355 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007356
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007357 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007358 AddDefaultCC(
7359 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007360 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7361 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007362 .addReg(NewVReg1)
7363 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7364
7365 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007366 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007367 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007368 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007369 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007370 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7371 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7372 .addFrameIndex(FI)
7373 .addImm(1)
7374 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007375
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007376 if (NumLPads < 256) {
7377 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7378 .addReg(NewVReg1)
7379 .addImm(NumLPads));
7380 } else {
7381 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007382 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7383 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7384
7385 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007386 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007387 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007388 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007389 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007390
7391 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7392 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7393 .addReg(VReg1, RegState::Define)
7394 .addConstantPoolIndex(Idx));
7395 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7396 .addReg(NewVReg1)
7397 .addReg(VReg1));
7398 }
7399
Bill Wendlingb3d46782011-10-06 23:37:36 +00007400 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7401 .addMBB(TrapBB)
7402 .addImm(ARMCC::HI)
7403 .addReg(ARM::CPSR);
7404
7405 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7406 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7407 .addReg(ARM::CPSR, RegState::Define)
7408 .addReg(NewVReg1)
7409 .addImm(2));
7410
7411 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007412 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007413 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007414
7415 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7416 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7417 .addReg(ARM::CPSR, RegState::Define)
7418 .addReg(NewVReg2, RegState::Kill)
7419 .addReg(NewVReg3));
7420
Alex Lorenze40c8a22015-08-11 23:09:45 +00007421 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7422 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007423
7424 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7425 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7426 .addReg(NewVReg4, RegState::Kill)
7427 .addImm(0)
7428 .addMemOperand(JTMMOLd));
7429
Chad Rosier96603432013-03-01 18:30:38 +00007430 unsigned NewVReg6 = NewVReg5;
7431 if (RelocM == Reloc::PIC_) {
7432 NewVReg6 = MRI->createVirtualRegister(TRC);
7433 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7434 .addReg(ARM::CPSR, RegState::Define)
7435 .addReg(NewVReg5, RegState::Kill)
7436 .addReg(NewVReg3));
7437 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007438
7439 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7440 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007441 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007442 } else {
7443 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7444 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7445 .addFrameIndex(FI)
7446 .addImm(4)
7447 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007448
Bill Wendling4969dcd2011-10-18 22:52:20 +00007449 if (NumLPads < 256) {
7450 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7451 .addReg(NewVReg1)
7452 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007453 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007454 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7455 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007456 .addImm(NumLPads & 0xFFFF));
7457
7458 unsigned VReg2 = VReg1;
7459 if ((NumLPads & 0xFFFF0000) != 0) {
7460 VReg2 = MRI->createVirtualRegister(TRC);
7461 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7462 .addReg(VReg1)
7463 .addImm(NumLPads >> 16));
7464 }
7465
Bill Wendling4969dcd2011-10-18 22:52:20 +00007466 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7467 .addReg(NewVReg1)
7468 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007469 } else {
7470 MachineConstantPool *ConstantPool = MF->getConstantPool();
7471 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7472 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7473
7474 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007475 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007476 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007477 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007478 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7479
7480 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7481 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7482 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007483 .addConstantPoolIndex(Idx)
7484 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007485 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7486 .addReg(NewVReg1)
7487 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007488 }
7489
Bill Wendling5626c662011-10-06 22:53:00 +00007490 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7491 .addMBB(TrapBB)
7492 .addImm(ARMCC::HI)
7493 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007494
Bill Wendling973c8172011-10-18 22:11:18 +00007495 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007496 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007497 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007498 .addReg(NewVReg1)
7499 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007500 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7501 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007502 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007503
Alex Lorenze40c8a22015-08-11 23:09:45 +00007504 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7505 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007506 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007507 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007508 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7509 .addReg(NewVReg3, RegState::Kill)
7510 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007511 .addImm(0)
7512 .addMemOperand(JTMMOLd));
7513
Chad Rosier96603432013-03-01 18:30:38 +00007514 if (RelocM == Reloc::PIC_) {
7515 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7516 .addReg(NewVReg5, RegState::Kill)
7517 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007518 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007519 } else {
7520 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7521 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007522 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007523 }
Bill Wendling5626c662011-10-06 22:53:00 +00007524 }
Bill Wendling202803e2011-10-05 00:02:33 +00007525
Bill Wendling324be982011-10-05 00:39:32 +00007526 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007527 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007528 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007529 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7530 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007531 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007532 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007533 }
7534
Bill Wendling26d27802011-10-17 05:25:09 +00007535 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007536 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007537 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007538 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007539
7540 // Remove the landing pad successor from the invoke block and replace it
7541 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007542 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7543 BB->succ_end());
7544 while (!Successors.empty()) {
7545 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00007546 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00007547 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007548 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007549 }
7550 }
7551
Cong Houd97c1002015-12-01 05:29:22 +00007552 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00007553 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007554
7555 // Find the invoke call and mark all of the callee-saved registers as
7556 // 'implicit defined' so that they're spilled. This prevents code from
7557 // moving instructions to before the EH block, where they will never be
7558 // executed.
7559 for (MachineBasicBlock::reverse_iterator
7560 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007561 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007562
7563 DenseMap<unsigned, bool> DefRegs;
7564 for (MachineInstr::mop_iterator
7565 OI = II->operands_begin(), OE = II->operands_end();
7566 OI != OE; ++OI) {
7567 if (!OI->isReg()) continue;
7568 DefRegs[OI->getReg()] = true;
7569 }
7570
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007571 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007572
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007573 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007574 unsigned Reg = SavedRegs[i];
7575 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007576 !ARM::tGPRRegClass.contains(Reg) &&
7577 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007578 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007579 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007580 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007581 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007582 continue;
7583 if (!DefRegs[Reg])
7584 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007585 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007586
7587 break;
7588 }
Bill Wendling883ec972011-10-07 23:18:02 +00007589 }
Bill Wendling324be982011-10-05 00:39:32 +00007590
Bill Wendling617075f2011-10-18 18:30:49 +00007591 // Mark all former landing pads as non-landing pads. The dispatch is the only
7592 // landing pad now.
7593 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7594 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00007595 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00007596
Bill Wendling324be982011-10-05 00:39:32 +00007597 // The instruction is gone now.
7598 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007599}
7600
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007601static
7602MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7603 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7604 E = MBB->succ_end(); I != E; ++I)
7605 if (*I != Succ)
7606 return *I;
7607 llvm_unreachable("Expecting a BB with two successors!");
7608}
7609
Manman Renb504f492013-10-29 22:27:32 +00007610/// Return the load opcode for a given load size. If load size >= 8,
7611/// neon opcode will be returned.
7612static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7613 if (LdSize >= 8)
7614 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7615 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7616 if (IsThumb1)
7617 return LdSize == 4 ? ARM::tLDRi
7618 : LdSize == 2 ? ARM::tLDRHi
7619 : LdSize == 1 ? ARM::tLDRBi : 0;
7620 if (IsThumb2)
7621 return LdSize == 4 ? ARM::t2LDR_POST
7622 : LdSize == 2 ? ARM::t2LDRH_POST
7623 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7624 return LdSize == 4 ? ARM::LDR_POST_IMM
7625 : LdSize == 2 ? ARM::LDRH_POST
7626 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7627}
7628
7629/// Return the store opcode for a given store size. If store size >= 8,
7630/// neon opcode will be returned.
7631static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7632 if (StSize >= 8)
7633 return StSize == 16 ? ARM::VST1q32wb_fixed
7634 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7635 if (IsThumb1)
7636 return StSize == 4 ? ARM::tSTRi
7637 : StSize == 2 ? ARM::tSTRHi
7638 : StSize == 1 ? ARM::tSTRBi : 0;
7639 if (IsThumb2)
7640 return StSize == 4 ? ARM::t2STR_POST
7641 : StSize == 2 ? ARM::t2STRH_POST
7642 : StSize == 1 ? ARM::t2STRB_POST : 0;
7643 return StSize == 4 ? ARM::STR_POST_IMM
7644 : StSize == 2 ? ARM::STRH_POST
7645 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7646}
7647
7648/// Emit a post-increment load operation with given size. The instructions
7649/// will be added to BB at Pos.
7650static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7651 const TargetInstrInfo *TII, DebugLoc dl,
7652 unsigned LdSize, unsigned Data, unsigned AddrIn,
7653 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7654 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7655 assert(LdOpc != 0 && "Should have a load opcode");
7656 if (LdSize >= 8) {
7657 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7658 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7659 .addImm(0));
7660 } else if (IsThumb1) {
7661 // load + update AddrIn
7662 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7663 .addReg(AddrIn).addImm(0));
7664 MachineInstrBuilder MIB =
7665 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7666 MIB = AddDefaultT1CC(MIB);
7667 MIB.addReg(AddrIn).addImm(LdSize);
7668 AddDefaultPred(MIB);
7669 } else if (IsThumb2) {
7670 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7671 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7672 .addImm(LdSize));
7673 } else { // arm
7674 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7675 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7676 .addReg(0).addImm(LdSize));
7677 }
7678}
7679
7680/// Emit a post-increment store operation with given size. The instructions
7681/// will be added to BB at Pos.
7682static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7683 const TargetInstrInfo *TII, DebugLoc dl,
7684 unsigned StSize, unsigned Data, unsigned AddrIn,
7685 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7686 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7687 assert(StOpc != 0 && "Should have a store opcode");
7688 if (StSize >= 8) {
7689 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7690 .addReg(AddrIn).addImm(0).addReg(Data));
7691 } else if (IsThumb1) {
7692 // store + update AddrIn
7693 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7694 .addReg(AddrIn).addImm(0));
7695 MachineInstrBuilder MIB =
7696 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7697 MIB = AddDefaultT1CC(MIB);
7698 MIB.addReg(AddrIn).addImm(StSize);
7699 AddDefaultPred(MIB);
7700 } else if (IsThumb2) {
7701 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7702 .addReg(Data).addReg(AddrIn).addImm(StSize));
7703 } else { // arm
7704 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7705 .addReg(Data).addReg(AddrIn).addReg(0)
7706 .addImm(StSize));
7707 }
7708}
7709
David Peixottoc32e24a2013-10-17 19:49:22 +00007710MachineBasicBlock *
7711ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7712 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007713 // This pseudo instruction has 3 operands: dst, src, size
7714 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7715 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007716 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007717 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007718 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00007719
7720 unsigned dest = MI->getOperand(0).getReg();
7721 unsigned src = MI->getOperand(1).getReg();
7722 unsigned SizeVal = MI->getOperand(2).getImm();
7723 unsigned Align = MI->getOperand(3).getImm();
7724 DebugLoc dl = MI->getDebugLoc();
7725
Manman Rene8735522012-06-01 19:33:18 +00007726 MachineFunction *MF = BB->getParent();
7727 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007728 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007729 const TargetRegisterClass *TRC = nullptr;
7730 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007731
7732 bool IsThumb1 = Subtarget->isThumb1Only();
7733 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007734
7735 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007736 UnitSize = 1;
7737 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007738 UnitSize = 2;
7739 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007740 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007741 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007742 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007743 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007744 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007745 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007746 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007747 }
7748 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007749 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007750 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007751 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007752
David Peixottob0653e532013-10-24 16:39:36 +00007753 // Select the correct opcode and register class for unit size load/store
7754 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007755 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007756 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007757 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7758 : UnitSize == 8 ? &ARM::DPRRegClass
7759 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007760
Manman Rene8735522012-06-01 19:33:18 +00007761 unsigned BytesLeft = SizeVal % UnitSize;
7762 unsigned LoopSize = SizeVal - BytesLeft;
7763
7764 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7765 // Use LDR and STR to copy.
7766 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7767 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7768 unsigned srcIn = src;
7769 unsigned destIn = dest;
7770 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007771 unsigned srcOut = MRI.createVirtualRegister(TRC);
7772 unsigned destOut = MRI.createVirtualRegister(TRC);
7773 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007774 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7775 IsThumb1, IsThumb2);
7776 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7777 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007778 srcIn = srcOut;
7779 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007780 }
7781
7782 // Handle the leftover bytes with LDRB and STRB.
7783 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7784 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007785 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007786 unsigned srcOut = MRI.createVirtualRegister(TRC);
7787 unsigned destOut = MRI.createVirtualRegister(TRC);
7788 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007789 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7790 IsThumb1, IsThumb2);
7791 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7792 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007793 srcIn = srcOut;
7794 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007795 }
7796 MI->eraseFromParent(); // The instruction is gone now.
7797 return BB;
7798 }
7799
7800 // Expand the pseudo op to a loop.
7801 // thisMBB:
7802 // ...
7803 // movw varEnd, # --> with thumb2
7804 // movt varEnd, #
7805 // ldrcp varEnd, idx --> without thumb2
7806 // fallthrough --> loopMBB
7807 // loopMBB:
7808 // PHI varPhi, varEnd, varLoop
7809 // PHI srcPhi, src, srcLoop
7810 // PHI destPhi, dst, destLoop
7811 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7812 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7813 // subs varLoop, varPhi, #UnitSize
7814 // bne loopMBB
7815 // fallthrough --> exitMBB
7816 // exitMBB:
7817 // epilogue to handle left-over bytes
7818 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7819 // [destOut] = STRB_POST(scratch, destLoop, 1)
7820 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7821 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7822 MF->insert(It, loopMBB);
7823 MF->insert(It, exitMBB);
7824
7825 // Transfer the remainder of BB and its successor edges to exitMBB.
7826 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007827 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007828 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7829
7830 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007831 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007832 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007833 unsigned Vtmp = varEnd;
7834 if ((LoopSize & 0xFFFF0000) != 0)
7835 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007836 AddDefaultPred(BuildMI(BB, dl,
7837 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7838 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007839
7840 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007841 AddDefaultPred(BuildMI(BB, dl,
7842 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7843 varEnd)
7844 .addReg(Vtmp)
7845 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007846 } else {
7847 MachineConstantPool *ConstantPool = MF->getConstantPool();
7848 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7849 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7850
7851 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007852 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007853 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007854 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007855 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7856
7857 if (IsThumb1)
7858 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7859 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7860 else
7861 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7862 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7863 }
Manman Rene8735522012-06-01 19:33:18 +00007864 BB->addSuccessor(loopMBB);
7865
7866 // Generate the loop body:
7867 // varPhi = PHI(varLoop, varEnd)
7868 // srcPhi = PHI(srcLoop, src)
7869 // destPhi = PHI(destLoop, dst)
7870 MachineBasicBlock *entryBB = BB;
7871 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007872 unsigned varLoop = MRI.createVirtualRegister(TRC);
7873 unsigned varPhi = MRI.createVirtualRegister(TRC);
7874 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7875 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7876 unsigned destLoop = MRI.createVirtualRegister(TRC);
7877 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007878
7879 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7880 .addReg(varLoop).addMBB(loopMBB)
7881 .addReg(varEnd).addMBB(entryBB);
7882 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7883 .addReg(srcLoop).addMBB(loopMBB)
7884 .addReg(src).addMBB(entryBB);
7885 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7886 .addReg(destLoop).addMBB(loopMBB)
7887 .addReg(dest).addMBB(entryBB);
7888
7889 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7890 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007891 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007892 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7893 IsThumb1, IsThumb2);
7894 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7895 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007896
7897 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007898 if (IsThumb1) {
7899 MachineInstrBuilder MIB =
7900 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7901 MIB = AddDefaultT1CC(MIB);
7902 MIB.addReg(varPhi).addImm(UnitSize);
7903 AddDefaultPred(MIB);
7904 } else {
7905 MachineInstrBuilder MIB =
7906 BuildMI(*BB, BB->end(), dl,
7907 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7908 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7909 MIB->getOperand(5).setReg(ARM::CPSR);
7910 MIB->getOperand(5).setIsDef(true);
7911 }
7912 BuildMI(*BB, BB->end(), dl,
7913 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7914 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007915
7916 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7917 BB->addSuccessor(loopMBB);
7918 BB->addSuccessor(exitMBB);
7919
7920 // Add epilogue to handle BytesLeft.
7921 BB = exitMBB;
7922 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007923
7924 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7925 // [destOut] = STRB_POST(scratch, destLoop, 1)
7926 unsigned srcIn = srcLoop;
7927 unsigned destIn = destLoop;
7928 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007929 unsigned srcOut = MRI.createVirtualRegister(TRC);
7930 unsigned destOut = MRI.createVirtualRegister(TRC);
7931 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007932 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7933 IsThumb1, IsThumb2);
7934 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7935 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007936 srcIn = srcOut;
7937 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007938 }
7939
7940 MI->eraseFromParent(); // The instruction is gone now.
7941 return BB;
7942}
7943
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007944MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007945ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7946 MachineBasicBlock *MBB) const {
7947 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007948 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007949 DebugLoc DL = MI->getDebugLoc();
7950
7951 assert(Subtarget->isTargetWindows() &&
7952 "__chkstk is only supported on Windows");
7953 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7954
7955 // __chkstk takes the number of words to allocate on the stack in R4, and
7956 // returns the stack adjustment in number of bytes in R4. This will not
7957 // clober any other registers (other than the obvious lr).
7958 //
7959 // Although, technically, IP should be considered a register which may be
7960 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7961 // thumb-2 environment, so there is no interworking required. As a result, we
7962 // do not expect a veneer to be emitted by the linker, clobbering IP.
7963 //
Alp Toker1d099d92014-06-19 19:41:26 +00007964 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007965 // required, again, ensuring that IP is not clobbered.
7966 //
7967 // Finally, although some linkers may theoretically provide a trampoline for
7968 // out of range calls (which is quite common due to a 32M range limitation of
7969 // branches for Thumb), we can generate the long-call version via
7970 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7971 // IP.
7972
7973 switch (TM.getCodeModel()) {
7974 case CodeModel::Small:
7975 case CodeModel::Medium:
7976 case CodeModel::Default:
7977 case CodeModel::Kernel:
7978 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7979 .addImm((unsigned)ARMCC::AL).addReg(0)
7980 .addExternalSymbol("__chkstk")
7981 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7982 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7983 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7984 break;
7985 case CodeModel::Large:
7986 case CodeModel::JITDefault: {
7987 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7988 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7989
7990 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7991 .addExternalSymbol("__chkstk");
7992 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7993 .addImm((unsigned)ARMCC::AL).addReg(0)
7994 .addReg(Reg, RegState::Kill)
7995 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7996 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7997 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7998 break;
7999 }
8000 }
8001
8002 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
8003 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00008004 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008005
8006 MI->eraseFromParent();
8007 return MBB;
8008}
8009
8010MachineBasicBlock *
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008011ARMTargetLowering::EmitLowered__dbzchk(MachineInstr *MI,
8012 MachineBasicBlock *MBB) const {
8013 DebugLoc DL = MI->getDebugLoc();
8014 MachineFunction *MF = MBB->getParent();
8015 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8016
8017 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
8018 MF->push_back(ContBB);
8019 ContBB->splice(ContBB->begin(), MBB,
8020 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008021 ContBB->transferSuccessorsAndUpdatePHIs(MBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008022
8023 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8024 MF->push_back(TrapBB);
8025 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
8026 MBB->addSuccessor(TrapBB);
8027
8028 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
8029 .addReg(MI->getOperand(0).getReg())
8030 .addMBB(TrapBB);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008031 MBB->addSuccessor(ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008032
8033 MI->eraseFromParent();
8034 return ContBB;
8035}
8036
8037MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00008038ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008039 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008040 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00008041 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008042 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00008043 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008044 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00008045 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00008046 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008047 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008048 // The Thumb2 pre-indexed stores have the same MI operands, they just
8049 // define them differently in the .td files from the isel patterns, so
8050 // they need pseudos.
8051 case ARM::t2STR_preidx:
8052 MI->setDesc(TII->get(ARM::t2STR_PRE));
8053 return BB;
8054 case ARM::t2STRB_preidx:
8055 MI->setDesc(TII->get(ARM::t2STRB_PRE));
8056 return BB;
8057 case ARM::t2STRH_preidx:
8058 MI->setDesc(TII->get(ARM::t2STRH_PRE));
8059 return BB;
8060
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008061 case ARM::STRi_preidx:
8062 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00008063 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008064 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
8065 // Decode the offset.
8066 unsigned Offset = MI->getOperand(4).getImm();
8067 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8068 Offset = ARM_AM::getAM2Offset(Offset);
8069 if (isSub)
8070 Offset = -Offset;
8071
Jim Grosbachf402f692011-08-12 21:02:34 +00008072 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008073 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008074 .addOperand(MI->getOperand(0)) // Rn_wb
8075 .addOperand(MI->getOperand(1)) // Rt
8076 .addOperand(MI->getOperand(2)) // Rn
8077 .addImm(Offset) // offset (skip GPR==zero_reg)
8078 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00008079 .addOperand(MI->getOperand(6))
8080 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008081 MI->eraseFromParent();
8082 return BB;
8083 }
8084 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008085 case ARM::STRBr_preidx:
8086 case ARM::STRH_preidx: {
8087 unsigned NewOpc;
8088 switch (MI->getOpcode()) {
8089 default: llvm_unreachable("unexpected opcode!");
8090 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8091 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8092 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8093 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008094 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
8095 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
8096 MIB.addOperand(MI->getOperand(i));
8097 MI->eraseFromParent();
8098 return BB;
8099 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008100
Evan Chengbb2af352009-08-12 05:17:19 +00008101 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008102 // To "insert" a SELECT_CC instruction, we actually have to insert the
8103 // diamond control-flow pattern. The incoming instruction knows the
8104 // destination vreg to set, the condition code register to branch on, the
8105 // true/false values to select between, and a branch opcode to use.
8106 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008107 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008108
8109 // thisMBB:
8110 // ...
8111 // TrueVal = ...
8112 // cmpTY ccX, r1, r2
8113 // bCC copy1MBB
8114 // fallthrough --> copy0MBB
8115 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008116 MachineFunction *F = BB->getParent();
8117 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8118 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008119 F->insert(It, copy0MBB);
8120 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008121
8122 // Transfer the remainder of BB and its successor edges to sinkMBB.
8123 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008124 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008125 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8126
Dan Gohmanf4f04102010-07-06 15:49:48 +00008127 BB->addSuccessor(copy0MBB);
8128 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008129
Dan Gohman34396292010-07-06 20:24:04 +00008130 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
8131 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
8132
Evan Cheng10043e22007-01-19 07:51:42 +00008133 // copy0MBB:
8134 // %FalseValue = ...
8135 // # fallthrough to sinkMBB
8136 BB = copy0MBB;
8137
8138 // Update machine-CFG edges
8139 BB->addSuccessor(sinkMBB);
8140
8141 // sinkMBB:
8142 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8143 // ...
8144 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00008145 BuildMI(*BB, BB->begin(), dl,
8146 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00008147 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8148 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8149
Dan Gohman34396292010-07-06 20:24:04 +00008150 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008151 return BB;
8152 }
Evan Chengb972e562009-08-07 00:34:42 +00008153
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008154 case ARM::BCCi64:
8155 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008156 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008157 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008158
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008159 // Compare both parts that make up the double comparison separately for
8160 // equality.
8161 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
8162
8163 unsigned LHS1 = MI->getOperand(1).getReg();
8164 unsigned LHS2 = MI->getOperand(2).getReg();
8165 if (RHSisZero) {
8166 AddDefaultPred(BuildMI(BB, dl,
8167 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8168 .addReg(LHS1).addImm(0));
8169 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8170 .addReg(LHS2).addImm(0)
8171 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8172 } else {
8173 unsigned RHS1 = MI->getOperand(3).getReg();
8174 unsigned RHS2 = MI->getOperand(4).getReg();
8175 AddDefaultPred(BuildMI(BB, dl,
8176 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8177 .addReg(LHS1).addReg(RHS1));
8178 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8179 .addReg(LHS2).addReg(RHS2)
8180 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8181 }
8182
8183 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
8184 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
8185 if (MI->getOperand(0).getImm() == ARMCC::NE)
8186 std::swap(destMBB, exitMBB);
8187
8188 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8189 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008190 if (isThumb2)
8191 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8192 else
8193 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008194
8195 MI->eraseFromParent(); // The pseudo instruction is gone now.
8196 return BB;
8197 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008198
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008199 case ARM::Int_eh_sjlj_setjmp:
8200 case ARM::Int_eh_sjlj_setjmp_nofp:
8201 case ARM::tInt_eh_sjlj_setjmp:
8202 case ARM::t2Int_eh_sjlj_setjmp:
8203 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008204 return BB;
8205
8206 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008207 EmitSjLjDispatchBlock(MI, BB);
8208 return BB;
8209
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008210 case ARM::ABS:
8211 case ARM::t2ABS: {
8212 // To insert an ABS instruction, we have to insert the
8213 // diamond control-flow pattern. The incoming instruction knows the
8214 // source vreg to test against 0, the destination vreg to set,
8215 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008216 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008217 // It transforms
8218 // V1 = ABS V0
8219 // into
8220 // V2 = MOVS V0
8221 // BCC (branch to SinkBB if V0 >= 0)
8222 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008223 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008224 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008225 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008226 MachineFunction *Fn = BB->getParent();
8227 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8228 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8229 Fn->insert(BBI, RSBBB);
8230 Fn->insert(BBI, SinkBB);
8231
8232 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
8233 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00008234 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008235 bool isThumb2 = Subtarget->isThumb2();
8236 MachineRegisterInfo &MRI = Fn->getRegInfo();
8237 // In Thumb mode S must not be specified if source register is the SP or
8238 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008239 unsigned NewRsbDstReg =
8240 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008241
8242 // Transfer the remainder of BB and its successor edges to sinkMBB.
8243 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008244 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008245 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8246
8247 BB->addSuccessor(RSBBB);
8248 BB->addSuccessor(SinkBB);
8249
8250 // fall through to SinkMBB
8251 RSBBB->addSuccessor(SinkBB);
8252
Manman Rene0763c72012-06-15 21:32:12 +00008253 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008254 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008255 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8256 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008257
8258 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008259 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008260 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8261 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8262
8263 // insert rsbri in RSBBB
8264 // Note: BCC and rsbri will be converted into predicated rsbmi
8265 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008266 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008267 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008268 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008269 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8270
Andrew Trick3f07c422011-10-18 18:40:53 +00008271 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008272 // reuse ABSDstReg to not change uses of ABS instruction
8273 BuildMI(*SinkBB, SinkBB->begin(), dl,
8274 TII->get(ARM::PHI), ABSDstReg)
8275 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008276 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008277
8278 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00008279 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008280
8281 // return last added BB
8282 return SinkBB;
8283 }
Manman Rene8735522012-06-01 19:33:18 +00008284 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008285 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008286 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008287 case ARM::WIN__CHKSTK:
8288 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008289 case ARM::WIN__DBZCHK:
8290 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008291 }
8292}
8293
Scott Douglass953f9082015-10-05 14:49:54 +00008294/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8295/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8296/// instead of as a custom inserter because we need the use list from the SDNode.
8297static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
8298 MachineInstr *MI, const SDNode *Node) {
8299 bool isThumb1 = Subtarget->isThumb1Only();
8300
8301 DebugLoc DL = MI->getDebugLoc();
8302 MachineFunction *MF = MI->getParent()->getParent();
8303 MachineRegisterInfo &MRI = MF->getRegInfo();
8304 MachineInstrBuilder MIB(*MF, MI);
8305
8306 // If the new dst/src is unused mark it as dead.
8307 if (!Node->hasAnyUseOfValue(0)) {
8308 MI->getOperand(0).setIsDead(true);
8309 }
8310 if (!Node->hasAnyUseOfValue(1)) {
8311 MI->getOperand(1).setIsDead(true);
8312 }
8313
8314 // The MEMCPY both defines and kills the scratch registers.
8315 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
8316 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8317 : &ARM::GPRRegClass);
8318 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8319 }
8320}
8321
Evan Chenge6fba772011-08-30 19:09:48 +00008322void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
8323 SDNode *Node) const {
Scott Douglass953f9082015-10-05 14:49:54 +00008324 if (MI->getOpcode() == ARM::MEMCPY) {
8325 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8326 return;
8327 }
8328
Evan Cheng7f8e5632011-12-07 07:15:52 +00008329 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008330 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8331 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8332 // operand is still set to noreg. If needed, set the optional operand's
8333 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008334 //
Andrew Trick88b24502011-10-18 19:18:52 +00008335 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008336
Andrew Trick924123a2011-09-21 02:20:46 +00008337 // Rename pseudo opcodes.
8338 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
8339 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008340 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008341 MCID = &TII->get(NewOpc);
8342
8343 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
8344 "converted opcode should be the same except for cc_out");
8345
8346 MI->setDesc(*MCID);
8347
8348 // Add the optional cc_out operand
8349 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008350 }
Andrew Trick88b24502011-10-18 19:18:52 +00008351 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008352
8353 // Any ARM instruction that sets the 's' bit should specify an optional
8354 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00008355 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008356 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008357 return;
8358 }
Andrew Trick924123a2011-09-21 02:20:46 +00008359 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8360 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008361 bool definesCPSR = false;
8362 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00008363 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00008364 i != e; ++i) {
8365 const MachineOperand &MO = MI->getOperand(i);
8366 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8367 definesCPSR = true;
8368 if (MO.isDead())
8369 deadCPSR = true;
8370 MI->RemoveOperand(i);
8371 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008372 }
8373 }
Andrew Trick8586e622011-09-20 03:17:40 +00008374 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008375 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008376 return;
8377 }
8378 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008379 if (deadCPSR) {
8380 assert(!MI->getOperand(ccOutIdx).getReg() &&
8381 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008382 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008383 }
Andrew Trick8586e622011-09-20 03:17:40 +00008384
Andrew Trick924123a2011-09-21 02:20:46 +00008385 // If this instruction was defined with an optional CPSR def and its dag node
8386 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008387 MachineOperand &MO = MI->getOperand(ccOutIdx);
8388 MO.setReg(ARM::CPSR);
8389 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008390}
8391
Evan Cheng10043e22007-01-19 07:51:42 +00008392//===----------------------------------------------------------------------===//
8393// ARM Optimization Hooks
8394//===----------------------------------------------------------------------===//
8395
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008396// Helper function that checks if N is a null or all ones constant.
8397static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008398 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008399}
8400
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008401// Return true if N is conditionally 0 or all ones.
8402// Detects these expressions where cc is an i1 value:
8403//
8404// (select cc 0, y) [AllOnes=0]
8405// (select cc y, 0) [AllOnes=0]
8406// (zext cc) [AllOnes=0]
8407// (sext cc) [AllOnes=0/1]
8408// (select cc -1, y) [AllOnes=1]
8409// (select cc y, -1) [AllOnes=1]
8410//
8411// Invert is set when N is the null/all ones constant when CC is false.
8412// OtherOp is set to the alternative value of N.
8413static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8414 SDValue &CC, bool &Invert,
8415 SDValue &OtherOp,
8416 SelectionDAG &DAG) {
8417 switch (N->getOpcode()) {
8418 default: return false;
8419 case ISD::SELECT: {
8420 CC = N->getOperand(0);
8421 SDValue N1 = N->getOperand(1);
8422 SDValue N2 = N->getOperand(2);
8423 if (isZeroOrAllOnes(N1, AllOnes)) {
8424 Invert = false;
8425 OtherOp = N2;
8426 return true;
8427 }
8428 if (isZeroOrAllOnes(N2, AllOnes)) {
8429 Invert = true;
8430 OtherOp = N1;
8431 return true;
8432 }
8433 return false;
8434 }
8435 case ISD::ZERO_EXTEND:
8436 // (zext cc) can never be the all ones value.
8437 if (AllOnes)
8438 return false;
8439 // Fall through.
8440 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008441 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008442 EVT VT = N->getValueType(0);
8443 CC = N->getOperand(0);
8444 if (CC.getValueType() != MVT::i1)
8445 return false;
8446 Invert = !AllOnes;
8447 if (AllOnes)
8448 // When looking for an AllOnes constant, N is an sext, and the 'other'
8449 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008450 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008451 else if (N->getOpcode() == ISD::ZERO_EXTEND)
8452 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008453 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008454 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008455 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
8456 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008457 return true;
8458 }
8459 }
8460}
8461
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008462// Combine a constant select operand into its use:
8463//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008464// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
8465// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
8466// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
8467// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8468// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008469//
8470// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008471// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008472//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008473// Also recognize sext/zext from i1:
8474//
8475// (add (zext cc), x) -> (select cc (add x, 1), x)
8476// (add (sext cc), x) -> (select cc (add x, -1), x)
8477//
8478// These transformations eventually create predicated instructions.
8479//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008480// @param N The node to transform.
8481// @param Slct The N operand that is a select.
8482// @param OtherOp The other N operand (x above).
8483// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008484// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008485// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008486static
8487SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008488 TargetLowering::DAGCombinerInfo &DCI,
8489 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008490 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008491 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008492 SDValue NonConstantVal;
8493 SDValue CCOp;
8494 bool SwapSelectOps;
8495 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8496 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008497 return SDValue();
8498
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008499 // Slct is now know to be the desired identity constant when CC is true.
8500 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008501 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008502 OtherOp, NonConstantVal);
8503 // Unless SwapSelectOps says CC should be false.
8504 if (SwapSelectOps)
8505 std::swap(TrueVal, FalseVal);
8506
Andrew Trickef9de2a2013-05-25 02:42:55 +00008507 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008508 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008509}
8510
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008511// Attempt combineSelectAndUse on each operand of a commutative operator N.
8512static
8513SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8514 TargetLowering::DAGCombinerInfo &DCI) {
8515 SDValue N0 = N->getOperand(0);
8516 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008517 if (N0.getNode()->hasOneUse())
8518 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008519 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008520 if (N1.getNode()->hasOneUse())
8521 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008522 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008523 return SDValue();
8524}
8525
Eric Christopher1b8b94192011-06-29 21:10:36 +00008526// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008527// (only after legalization).
8528static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8529 TargetLowering::DAGCombinerInfo &DCI,
8530 const ARMSubtarget *Subtarget) {
8531
8532 // Only perform optimization if after legalize, and if NEON is available. We
8533 // also expected both operands to be BUILD_VECTORs.
8534 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8535 || N0.getOpcode() != ISD::BUILD_VECTOR
8536 || N1.getOpcode() != ISD::BUILD_VECTOR)
8537 return SDValue();
8538
8539 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8540 EVT VT = N->getValueType(0);
8541 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8542 return SDValue();
8543
8544 // Check that the vector operands are of the right form.
8545 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8546 // operands, where N is the size of the formed vector.
8547 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8548 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008549
8550 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008551 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008552 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008553 SDValue Vec = N0->getOperand(0)->getOperand(0);
8554 SDNode *V = Vec.getNode();
8555 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008556
Eric Christopher1b8b94192011-06-29 21:10:36 +00008557 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008558 // check to see if each of their operands are an EXTRACT_VECTOR with
8559 // the same vector and appropriate index.
8560 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8561 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8562 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008563
Tanya Lattnere9e67052011-06-14 23:48:48 +00008564 SDValue ExtVec0 = N0->getOperand(i);
8565 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008566
Tanya Lattnere9e67052011-06-14 23:48:48 +00008567 // First operand is the vector, verify its the same.
8568 if (V != ExtVec0->getOperand(0).getNode() ||
8569 V != ExtVec1->getOperand(0).getNode())
8570 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008571
Tanya Lattnere9e67052011-06-14 23:48:48 +00008572 // Second is the constant, verify its correct.
8573 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8574 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008575
Tanya Lattnere9e67052011-06-14 23:48:48 +00008576 // For the constant, we want to see all the even or all the odd.
8577 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8578 || C1->getZExtValue() != nextIndex+1)
8579 return SDValue();
8580
8581 // Increment index.
8582 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008583 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008584 return SDValue();
8585 }
8586
8587 // Create VPADDL node.
8588 SelectionDAG &DAG = DCI.DAG;
8589 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008590
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008591 SDLoc dl(N);
8592
Tanya Lattnere9e67052011-06-14 23:48:48 +00008593 // Build operand list.
8594 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008595 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008596 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008597
8598 // Input is the vector.
8599 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008600
Tanya Lattnere9e67052011-06-14 23:48:48 +00008601 // Get widened type and narrowed type.
8602 MVT widenType;
8603 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00008604
Silviu Barangaa3106e62014-04-03 10:44:27 +00008605 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8606 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008607 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8608 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8609 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8610 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008611 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008612 }
8613
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008614 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008615 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008616 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008617}
8618
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008619static SDValue findMUL_LOHI(SDValue V) {
8620 if (V->getOpcode() == ISD::UMUL_LOHI ||
8621 V->getOpcode() == ISD::SMUL_LOHI)
8622 return V;
8623 return SDValue();
8624}
8625
8626static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8627 TargetLowering::DAGCombinerInfo &DCI,
8628 const ARMSubtarget *Subtarget) {
8629
8630 if (Subtarget->isThumb1Only()) return SDValue();
8631
8632 // Only perform the checks after legalize when the pattern is available.
8633 if (DCI.isBeforeLegalize()) return SDValue();
8634
8635 // Look for multiply add opportunities.
8636 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8637 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8638 // a glue link from the first add to the second add.
8639 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8640 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008641 // UMUL_LOHI
8642 // / :lo \ :hi
8643 // / \ [no multiline comment]
8644 // loAdd -> ADDE |
8645 // \ :glue /
8646 // \ /
8647 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008648 //
8649 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8650 SDValue AddcOp0 = AddcNode->getOperand(0);
8651 SDValue AddcOp1 = AddcNode->getOperand(1);
8652
8653 // Check if the two operands are from the same mul_lohi node.
8654 if (AddcOp0.getNode() == AddcOp1.getNode())
8655 return SDValue();
8656
8657 assert(AddcNode->getNumValues() == 2 &&
8658 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008659 "Expect ADDC with two result values. First: i32");
8660
8661 // Check that we have a glued ADDC node.
8662 if (AddcNode->getValueType(1) != MVT::Glue)
8663 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008664
8665 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8666 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8667 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8668 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8669 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8670 return SDValue();
8671
8672 // Look for the glued ADDE.
8673 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008674 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008675 return SDValue();
8676
8677 // Make sure it is really an ADDE.
8678 if (AddeNode->getOpcode() != ISD::ADDE)
8679 return SDValue();
8680
8681 assert(AddeNode->getNumOperands() == 3 &&
8682 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8683 "ADDE node has the wrong inputs");
8684
8685 // Check for the triangle shape.
8686 SDValue AddeOp0 = AddeNode->getOperand(0);
8687 SDValue AddeOp1 = AddeNode->getOperand(1);
8688
8689 // Make sure that the ADDE operands are not coming from the same node.
8690 if (AddeOp0.getNode() == AddeOp1.getNode())
8691 return SDValue();
8692
8693 // Find the MUL_LOHI node walking up ADDE's operands.
8694 bool IsLeftOperandMUL = false;
8695 SDValue MULOp = findMUL_LOHI(AddeOp0);
8696 if (MULOp == SDValue())
8697 MULOp = findMUL_LOHI(AddeOp1);
8698 else
8699 IsLeftOperandMUL = true;
8700 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008701 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008702
8703 // Figure out the right opcode.
8704 unsigned Opc = MULOp->getOpcode();
8705 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8706
8707 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008708 SDValue* HiAdd = nullptr;
8709 SDValue* LoMul = nullptr;
8710 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008711
Jyoti Allurf1d70502015-01-23 09:10:03 +00008712 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8713 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8714 return SDValue();
8715
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008716 if (IsLeftOperandMUL)
8717 HiAdd = &AddeOp1;
8718 else
8719 HiAdd = &AddeOp0;
8720
8721
Jyoti Allurf1d70502015-01-23 09:10:03 +00008722 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8723 // whose low result is fed to the ADDC we are checking.
8724
8725 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008726 LoMul = &AddcOp0;
8727 LowAdd = &AddcOp1;
8728 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008729 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008730 LoMul = &AddcOp1;
8731 LowAdd = &AddcOp0;
8732 }
8733
Craig Topper062a2ba2014-04-25 05:30:21 +00008734 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008735 return SDValue();
8736
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008737 // Create the merged node.
8738 SelectionDAG &DAG = DCI.DAG;
8739
8740 // Build operand list.
8741 SmallVector<SDValue, 8> Ops;
8742 Ops.push_back(LoMul->getOperand(0));
8743 Ops.push_back(LoMul->getOperand(1));
8744 Ops.push_back(*LowAdd);
8745 Ops.push_back(*HiAdd);
8746
Andrew Trickef9de2a2013-05-25 02:42:55 +00008747 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008748 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008749
8750 // Replace the ADDs' nodes uses by the MLA node's values.
8751 SDValue HiMLALResult(MLALNode.getNode(), 1);
8752 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8753
8754 SDValue LoMLALResult(MLALNode.getNode(), 0);
8755 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8756
8757 // Return original node to notify the driver to stop replacing.
8758 SDValue resNode(AddcNode, 0);
8759 return resNode;
8760}
8761
8762/// PerformADDCCombine - Target-specific dag combine transform from
8763/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8764static SDValue PerformADDCCombine(SDNode *N,
8765 TargetLowering::DAGCombinerInfo &DCI,
8766 const ARMSubtarget *Subtarget) {
8767
8768 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8769
8770}
8771
Bob Wilson728eb292010-07-29 20:34:14 +00008772/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8773/// operands N0 and N1. This is a helper for PerformADDCombine that is
8774/// called with the default operands, and if that fails, with commuted
8775/// operands.
8776static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008777 TargetLowering::DAGCombinerInfo &DCI,
8778 const ARMSubtarget *Subtarget){
8779
8780 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008781 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00008782 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008783
Chris Lattner4147f082009-03-12 06:52:53 +00008784 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008785 if (N0.getNode()->hasOneUse())
8786 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
8787 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00008788 return SDValue();
8789}
8790
Bob Wilson728eb292010-07-29 20:34:14 +00008791/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8792///
8793static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008794 TargetLowering::DAGCombinerInfo &DCI,
8795 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008796 SDValue N0 = N->getOperand(0);
8797 SDValue N1 = N->getOperand(1);
8798
8799 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008800 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00008801 return Result;
8802
8803 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008804 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008805}
8806
Chris Lattner4147f082009-03-12 06:52:53 +00008807/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008808///
Chris Lattner4147f082009-03-12 06:52:53 +00008809static SDValue PerformSUBCombine(SDNode *N,
8810 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008811 SDValue N0 = N->getOperand(0);
8812 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008813
Chris Lattner4147f082009-03-12 06:52:53 +00008814 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008815 if (N1.getNode()->hasOneUse())
8816 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
8817 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00008818
Chris Lattner4147f082009-03-12 06:52:53 +00008819 return SDValue();
8820}
8821
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008822/// PerformVMULCombine
8823/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8824/// special multiplier accumulator forwarding.
8825/// vmul d3, d0, d2
8826/// vmla d3, d1, d2
8827/// is faster than
8828/// vadd d3, d0, d1
8829/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008830// However, for (A + B) * (A + B),
8831// vadd d2, d0, d1
8832// vmul d3, d0, d2
8833// vmla d3, d1, d2
8834// is slower than
8835// vadd d2, d0, d1
8836// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008837static SDValue PerformVMULCombine(SDNode *N,
8838 TargetLowering::DAGCombinerInfo &DCI,
8839 const ARMSubtarget *Subtarget) {
8840 if (!Subtarget->hasVMLxForwarding())
8841 return SDValue();
8842
8843 SelectionDAG &DAG = DCI.DAG;
8844 SDValue N0 = N->getOperand(0);
8845 SDValue N1 = N->getOperand(1);
8846 unsigned Opcode = N0.getOpcode();
8847 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8848 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008849 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008850 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8851 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8852 return SDValue();
8853 std::swap(N0, N1);
8854 }
8855
Weiming Zhao2052f482013-09-25 23:12:06 +00008856 if (N0 == N1)
8857 return SDValue();
8858
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008859 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008860 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008861 SDValue N00 = N0->getOperand(0);
8862 SDValue N01 = N0->getOperand(1);
8863 return DAG.getNode(Opcode, DL, VT,
8864 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8865 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8866}
8867
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008868static SDValue PerformMULCombine(SDNode *N,
8869 TargetLowering::DAGCombinerInfo &DCI,
8870 const ARMSubtarget *Subtarget) {
8871 SelectionDAG &DAG = DCI.DAG;
8872
8873 if (Subtarget->isThumb1Only())
8874 return SDValue();
8875
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008876 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8877 return SDValue();
8878
8879 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008880 if (VT.is64BitVector() || VT.is128BitVector())
8881 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008882 if (VT != MVT::i32)
8883 return SDValue();
8884
8885 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8886 if (!C)
8887 return SDValue();
8888
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008889 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008890 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008891
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008892 ShiftAmt = ShiftAmt & (32 - 1);
8893 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008894 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008895
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008896 SDValue Res;
8897 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008898
8899 if (MulAmt >= 0) {
8900 if (isPowerOf2_32(MulAmt - 1)) {
8901 // (mul x, 2^N + 1) => (add (shl x, N), x)
8902 Res = DAG.getNode(ISD::ADD, DL, VT,
8903 V,
8904 DAG.getNode(ISD::SHL, DL, VT,
8905 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008906 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008907 MVT::i32)));
8908 } else if (isPowerOf2_32(MulAmt + 1)) {
8909 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8910 Res = DAG.getNode(ISD::SUB, DL, VT,
8911 DAG.getNode(ISD::SHL, DL, VT,
8912 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008913 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008914 MVT::i32)),
8915 V);
8916 } else
8917 return SDValue();
8918 } else {
8919 uint64_t MulAmtAbs = -MulAmt;
8920 if (isPowerOf2_32(MulAmtAbs + 1)) {
8921 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8922 Res = DAG.getNode(ISD::SUB, DL, VT,
8923 V,
8924 DAG.getNode(ISD::SHL, DL, VT,
8925 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008926 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008927 MVT::i32)));
8928 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8929 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8930 Res = DAG.getNode(ISD::ADD, DL, VT,
8931 V,
8932 DAG.getNode(ISD::SHL, DL, VT,
8933 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008934 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008935 MVT::i32)));
8936 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008937 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008938
8939 } else
8940 return SDValue();
8941 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008942
8943 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008944 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008945 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008946
8947 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008948 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008949 return SDValue();
8950}
8951
Owen Anderson30c48922010-11-05 19:27:46 +00008952static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008953 TargetLowering::DAGCombinerInfo &DCI,
8954 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008955
Owen Anderson30c48922010-11-05 19:27:46 +00008956 // Attempt to use immediate-form VBIC
8957 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008958 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008959 EVT VT = N->getValueType(0);
8960 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008961
Tanya Lattner266792a2011-04-07 15:24:20 +00008962 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8963 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008964
Owen Anderson30c48922010-11-05 19:27:46 +00008965 APInt SplatBits, SplatUndef;
8966 unsigned SplatBitSize;
8967 bool HasAnyUndefs;
8968 if (BVN &&
8969 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8970 if (SplatBitSize <= 64) {
8971 EVT VbicVT;
8972 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8973 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008974 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008975 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008976 if (Val.getNode()) {
8977 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008978 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008979 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008980 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008981 }
8982 }
8983 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008984
Evan Chenge87681c2012-02-23 01:19:06 +00008985 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008986 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00008987 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008988 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008989 }
8990
Owen Anderson30c48922010-11-05 19:27:46 +00008991 return SDValue();
8992}
8993
Jim Grosbach11013ed2010-07-16 23:05:05 +00008994/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8995static SDValue PerformORCombine(SDNode *N,
8996 TargetLowering::DAGCombinerInfo &DCI,
8997 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008998 // Attempt to use immediate-form VORR
8999 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009000 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009001 EVT VT = N->getValueType(0);
9002 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009003
Tanya Lattner266792a2011-04-07 15:24:20 +00009004 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9005 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009006
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009007 APInt SplatBits, SplatUndef;
9008 unsigned SplatBitSize;
9009 bool HasAnyUndefs;
9010 if (BVN && Subtarget->hasNEON() &&
9011 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9012 if (SplatBitSize <= 64) {
9013 EVT VorrVT;
9014 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9015 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009016 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009017 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009018 if (Val.getNode()) {
9019 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009020 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009021 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009022 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009023 }
9024 }
9025 }
9026
Evan Chenge87681c2012-02-23 01:19:06 +00009027 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009028 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009029 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009030 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009031 }
9032
Nadav Rotem3a94c542012-08-13 18:52:44 +00009033 // The code below optimizes (or (and X, Y), Z).
9034 // The AND operand needs to have a single user to make these optimizations
9035 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009036 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009037 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009038 return SDValue();
9039 SDValue N1 = N->getOperand(1);
9040
9041 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9042 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9043 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9044 APInt SplatUndef;
9045 unsigned SplatBitSize;
9046 bool HasAnyUndefs;
9047
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009048 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009049 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009050 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9051 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009052 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009053 HasAnyUndefs) && !HasAnyUndefs) {
9054 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9055 HasAnyUndefs) && !HasAnyUndefs) {
9056 // Ensure that the bit width of the constants are the same and that
9057 // the splat arguments are logical inverses as per the pattern we
9058 // are trying to simplify.
9059 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9060 SplatBits0 == ~SplatBits1) {
9061 // Canonicalize the vector type to make instruction selection
9062 // simpler.
9063 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9064 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9065 N0->getOperand(1),
9066 N0->getOperand(0),
9067 N1->getOperand(0));
9068 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9069 }
9070 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009071 }
9072 }
9073
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009074 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9075 // reasonable.
9076
Jim Grosbach11013ed2010-07-16 23:05:05 +00009077 // BFI is only available on V6T2+
9078 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9079 return SDValue();
9080
Andrew Trickef9de2a2013-05-25 02:42:55 +00009081 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009082 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009083 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009084 //
9085 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009086 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009087 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009088 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009089 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009090 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009091
Jim Grosbach11013ed2010-07-16 23:05:05 +00009092 if (VT != MVT::i32)
9093 return SDValue();
9094
Evan Cheng2e51bb42010-12-13 20:32:54 +00009095 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009096
Jim Grosbach11013ed2010-07-16 23:05:05 +00009097 // The value and the mask need to be constants so we can verify this is
9098 // actually a bitfield set. If the mask is 0xffff, we can do better
9099 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009100 SDValue MaskOp = N0.getOperand(1);
9101 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9102 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009103 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009104 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009105 if (Mask == 0xffff)
9106 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009107 SDValue Res;
9108 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009109 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9110 if (N1C) {
9111 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009112 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009113 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009114
Evan Cheng34345752010-12-11 04:11:38 +00009115 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009116 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009117
Evan Cheng2e51bb42010-12-13 20:32:54 +00009118 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009119 DAG.getConstant(Val, DL, MVT::i32),
9120 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009121
9122 // Do not add new nodes to DAG combiner worklist.
9123 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009124 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009125 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009126 } else if (N1.getOpcode() == ISD::AND) {
9127 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009128 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9129 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009130 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009131 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009132
Eric Christopherd5530962011-03-26 01:21:03 +00009133 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9134 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009135 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009136 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009137 // The pack halfword instruction works better for masks that fit it,
9138 // so use that when it's available.
9139 if (Subtarget->hasT2ExtractPack() &&
9140 (Mask == 0xffff || Mask == 0xffff0000))
9141 return SDValue();
9142 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009143 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009144 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009145 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009146 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009147 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009148 // Do not add new nodes to DAG combiner worklist.
9149 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009150 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009151 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009152 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009153 // The pack halfword instruction works better for masks that fit it,
9154 // so use that when it's available.
9155 if (Subtarget->hasT2ExtractPack() &&
9156 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9157 return SDValue();
9158 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009159 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009160 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009161 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009162 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009163 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009164 // Do not add new nodes to DAG combiner worklist.
9165 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009166 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009167 }
9168 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009169
Evan Cheng2e51bb42010-12-13 20:32:54 +00009170 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9171 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9172 ARM::isBitFieldInvertedMask(~Mask)) {
9173 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9174 // where lsb(mask) == #shamt and masked bits of B are known zero.
9175 SDValue ShAmt = N00.getOperand(1);
9176 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009177 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009178 if (ShAmtC != LSB)
9179 return SDValue();
9180
9181 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009182 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009183
9184 // Do not add new nodes to DAG combiner worklist.
9185 DCI.CombineTo(N, Res, false);
9186 }
9187
Jim Grosbach11013ed2010-07-16 23:05:05 +00009188 return SDValue();
9189}
9190
Evan Chenge87681c2012-02-23 01:19:06 +00009191static SDValue PerformXORCombine(SDNode *N,
9192 TargetLowering::DAGCombinerInfo &DCI,
9193 const ARMSubtarget *Subtarget) {
9194 EVT VT = N->getValueType(0);
9195 SelectionDAG &DAG = DCI.DAG;
9196
9197 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9198 return SDValue();
9199
9200 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009201 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009202 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009203 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009204 }
9205
9206 return SDValue();
9207}
9208
James Molloyce12c922015-11-11 15:40:40 +00009209// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9210// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9211// their position in "to" (Rd).
9212static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9213 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009214
James Molloyce12c922015-11-11 15:40:40 +00009215 SDValue From = N->getOperand(1);
9216 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9217 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9218
9219 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9220 // #C in the base of the SHR.
9221 if (From->getOpcode() == ISD::SRL &&
9222 isa<ConstantSDNode>(From->getOperand(1))) {
9223 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9224 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9225 FromMask <<= Shift.getLimitedValue(31);
9226 From = From->getOperand(0);
9227 }
9228
9229 return From;
9230}
9231
9232// If A and B contain one contiguous set of bits, does A | B == A . B?
9233//
9234// Neither A nor B must be zero.
9235static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9236 unsigned LastActiveBitInA = A.countTrailingZeros();
9237 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9238 return LastActiveBitInA - 1 == FirstActiveBitInB;
9239}
9240
9241static SDValue FindBFIToCombineWith(SDNode *N) {
9242 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9243 // if one exists.
9244 APInt ToMask, FromMask;
9245 SDValue From = ParseBFI(N, ToMask, FromMask);
9246 SDValue To = N->getOperand(0);
9247
9248 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9249 // aren't compatible, but not if they set the same bit in their destination as
9250 // we do (or that of any BFI we're going to combine with).
9251 SDValue V = To;
9252 APInt CombinedToMask = ToMask;
9253 while (V.getOpcode() == ARMISD::BFI) {
9254 APInt NewToMask, NewFromMask;
9255 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9256 if (NewFrom != From) {
9257 // This BFI has a different base. Keep going.
9258 CombinedToMask |= NewToMask;
9259 V = V.getOperand(0);
9260 continue;
9261 }
9262
9263 // Do the written bits conflict with any we've seen so far?
9264 if ((NewToMask & CombinedToMask).getBoolValue())
9265 // Conflicting bits - bail out because going further is unsafe.
9266 return SDValue();
9267
9268 // Are the new bits contiguous when combined with the old bits?
9269 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9270 BitsProperlyConcatenate(FromMask, NewFromMask))
9271 return V;
9272 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9273 BitsProperlyConcatenate(NewFromMask, FromMask))
9274 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009275
James Molloyce12c922015-11-11 15:40:40 +00009276 // We've seen a write to some bits, so track it.
9277 CombinedToMask |= NewToMask;
9278 // Keep going...
9279 V = V.getOperand(0);
9280 }
9281
9282 return SDValue();
9283}
9284
Evan Chengc1778132010-12-14 03:22:07 +00009285static SDValue PerformBFICombine(SDNode *N,
9286 TargetLowering::DAGCombinerInfo &DCI) {
9287 SDValue N1 = N->getOperand(1);
9288 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009289 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9290 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009291 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9292 if (!N11C)
9293 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009294 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009295 unsigned LSB = countTrailingZeros(~InvMask);
9296 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009297 assert(Width <
9298 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009299 "undefined behavior");
9300 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009301 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009302 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009303 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009304 N->getOperand(0), N1.getOperand(0),
9305 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009306 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9307 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9308 // Keep track of any consecutive bits set that all come from the same base
9309 // value. We can combine these together into a single BFI.
9310 SDValue CombineBFI = FindBFIToCombineWith(N);
9311 if (CombineBFI == SDValue())
9312 return SDValue();
9313
9314 // We've found a BFI.
9315 APInt ToMask1, FromMask1;
9316 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9317
9318 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009319 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9320 assert(From1 == From2);
9321 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009322
James Molloyce12c922015-11-11 15:40:40 +00009323 // First, unlink CombineBFI.
9324 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9325 // Then create a new BFI, combining the two together.
9326 APInt NewFromMask = FromMask1 | FromMask2;
9327 APInt NewToMask = ToMask1 | ToMask2;
9328
9329 EVT VT = N->getValueType(0);
9330 SDLoc dl(N);
9331
9332 if (NewFromMask[0] == 0)
9333 From1 = DCI.DAG.getNode(
9334 ISD::SRL, dl, VT, From1,
9335 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9336 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9337 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009338 }
9339 return SDValue();
9340}
9341
Bob Wilson22806742010-09-22 22:09:21 +00009342/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9343/// ARMISD::VMOVRRD.
9344static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009345 TargetLowering::DAGCombinerInfo &DCI,
9346 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00009347 // vmovrrd(vmovdrr x, y) -> x,y
9348 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009349 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00009350 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009351
9352 // vmovrrd(load f64) -> (load i32), (load i32)
9353 SDNode *InNode = InDouble.getNode();
9354 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
9355 InNode->getValueType(0) == MVT::f64 &&
9356 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
9357 !cast<LoadSDNode>(InNode)->isVolatile()) {
9358 // TODO: Should this be done for non-FrameIndex operands?
9359 LoadSDNode *LD = cast<LoadSDNode>(InNode);
9360
9361 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009362 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009363 SDValue BasePtr = LD->getBasePtr();
9364 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
9365 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009366 LD->isNonTemporal(), LD->isInvariant(),
9367 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009368
9369 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009370 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009371 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
9372 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00009373 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009374 std::min(4U, LD->getAlignment() / 2));
9375
9376 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00009377 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00009378 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009379 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00009380 return Result;
9381 }
9382
Bob Wilson22806742010-09-22 22:09:21 +00009383 return SDValue();
9384}
9385
9386/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
9387/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
9388static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
9389 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
9390 SDValue Op0 = N->getOperand(0);
9391 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00009392 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009393 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00009394 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00009395 Op1 = Op1.getOperand(0);
9396 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
9397 Op0.getNode() == Op1.getNode() &&
9398 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009399 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00009400 N->getValueType(0), Op0.getOperand(0));
9401 return SDValue();
9402}
9403
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009404/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
9405/// are normal, non-volatile loads. If so, it is profitable to bitcast an
9406/// i64 vector to have f64 elements, since the value can then be loaded
9407/// directly into a VFP register.
9408static bool hasNormalLoadOperand(SDNode *N) {
9409 unsigned NumElts = N->getValueType(0).getVectorNumElements();
9410 for (unsigned i = 0; i < NumElts; ++i) {
9411 SDNode *Elt = N->getOperand(i).getNode();
9412 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
9413 return true;
9414 }
9415 return false;
9416}
9417
Bob Wilsoncb6db982010-09-17 22:59:05 +00009418/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
9419/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009420static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00009421 TargetLowering::DAGCombinerInfo &DCI,
9422 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00009423 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
9424 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
9425 // into a pair of GPRs, which is fine when the value is used as a scalar,
9426 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009427 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009428 if (N->getNumOperands() == 2)
9429 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009430 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +00009431
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009432 // Load i64 elements as f64 values so that type legalization does not split
9433 // them up into i32 values.
9434 EVT VT = N->getValueType(0);
9435 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
9436 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009437 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009438 SmallVector<SDValue, 8> Ops;
9439 unsigned NumElts = VT.getVectorNumElements();
9440 for (unsigned i = 0; i < NumElts; ++i) {
9441 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
9442 Ops.push_back(V);
9443 // Make the DAGCombiner fold the bitcast.
9444 DCI.AddToWorklist(V.getNode());
9445 }
9446 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00009447 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009448 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9449}
9450
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009451/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
9452static SDValue
9453PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9454 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
9455 // At that time, we may have inserted bitcasts from integer to float.
9456 // If these bitcasts have survived DAGCombine, change the lowering of this
9457 // BUILD_VECTOR in something more vector friendly, i.e., that does not
9458 // force to use floating point types.
9459
9460 // Make sure we can change the type of the vector.
9461 // This is possible iff:
9462 // 1. The vector is only used in a bitcast to a integer type. I.e.,
9463 // 1.1. Vector is used only once.
9464 // 1.2. Use is a bit convert to an integer type.
9465 // 2. The size of its operands are 32-bits (64-bits are not legal).
9466 EVT VT = N->getValueType(0);
9467 EVT EltVT = VT.getVectorElementType();
9468
9469 // Check 1.1. and 2.
9470 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
9471 return SDValue();
9472
9473 // By construction, the input type must be float.
9474 assert(EltVT == MVT::f32 && "Unexpected type!");
9475
9476 // Check 1.2.
9477 SDNode *Use = *N->use_begin();
9478 if (Use->getOpcode() != ISD::BITCAST ||
9479 Use->getValueType(0).isFloatingPoint())
9480 return SDValue();
9481
9482 // Check profitability.
9483 // Model is, if more than half of the relevant operands are bitcast from
9484 // i32, turn the build_vector into a sequence of insert_vector_elt.
9485 // Relevant operands are everything that is not statically
9486 // (i.e., at compile time) bitcasted.
9487 unsigned NumOfBitCastedElts = 0;
9488 unsigned NumElts = VT.getVectorNumElements();
9489 unsigned NumOfRelevantElts = NumElts;
9490 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
9491 SDValue Elt = N->getOperand(Idx);
9492 if (Elt->getOpcode() == ISD::BITCAST) {
9493 // Assume only bit cast to i32 will go away.
9494 if (Elt->getOperand(0).getValueType() == MVT::i32)
9495 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +00009496 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009497 // Constants are statically casted, thus do not count them as
9498 // relevant operands.
9499 --NumOfRelevantElts;
9500 }
9501
9502 // Check if more than half of the elements require a non-free bitcast.
9503 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
9504 return SDValue();
9505
9506 SelectionDAG &DAG = DCI.DAG;
9507 // Create the new vector type.
9508 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
9509 // Check if the type is legal.
9510 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9511 if (!TLI.isTypeLegal(VecVT))
9512 return SDValue();
9513
9514 // Combine:
9515 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
9516 // => BITCAST INSERT_VECTOR_ELT
9517 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
9518 // (BITCAST EN), N.
9519 SDValue Vec = DAG.getUNDEF(VecVT);
9520 SDLoc dl(N);
9521 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
9522 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +00009523 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009524 continue;
9525 if (V.getOpcode() == ISD::BITCAST &&
9526 V->getOperand(0).getValueType() == MVT::i32)
9527 // Fold obvious case.
9528 V = V.getOperand(0);
9529 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00009530 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009531 // Make the DAGCombiner fold the bitcasts.
9532 DCI.AddToWorklist(V.getNode());
9533 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009534 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009535 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
9536 }
9537 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
9538 // Make the DAGCombiner fold the bitcasts.
9539 DCI.AddToWorklist(Vec.getNode());
9540 return Vec;
9541}
9542
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009543/// PerformInsertEltCombine - Target-specific dag combine xforms for
9544/// ISD::INSERT_VECTOR_ELT.
9545static SDValue PerformInsertEltCombine(SDNode *N,
9546 TargetLowering::DAGCombinerInfo &DCI) {
9547 // Bitcast an i64 load inserted into a vector to f64.
9548 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9549 EVT VT = N->getValueType(0);
9550 SDNode *Elt = N->getOperand(1).getNode();
9551 if (VT.getVectorElementType() != MVT::i64 ||
9552 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
9553 return SDValue();
9554
9555 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009556 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009557 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9558 VT.getVectorNumElements());
9559 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
9560 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
9561 // Make the DAGCombiner fold the bitcasts.
9562 DCI.AddToWorklist(Vec.getNode());
9563 DCI.AddToWorklist(V.getNode());
9564 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
9565 Vec, V, N->getOperand(2));
9566 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00009567}
9568
Bob Wilsonc7334a12010-10-27 20:38:28 +00009569/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
9570/// ISD::VECTOR_SHUFFLE.
9571static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
9572 // The LLVM shufflevector instruction does not require the shuffle mask
9573 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
9574 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
9575 // operands do not match the mask length, they are extended by concatenating
9576 // them with undef vectors. That is probably the right thing for other
9577 // targets, but for NEON it is better to concatenate two double-register
9578 // size vector operands into a single quad-register size vector. Do that
9579 // transformation here:
9580 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9581 // shuffle(concat(v1, v2), undef)
9582 SDValue Op0 = N->getOperand(0);
9583 SDValue Op1 = N->getOperand(1);
9584 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9585 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9586 Op0.getNumOperands() != 2 ||
9587 Op1.getNumOperands() != 2)
9588 return SDValue();
9589 SDValue Concat0Op1 = Op0.getOperand(1);
9590 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00009591 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +00009592 return SDValue();
9593 // Skip the transformation if any of the types are illegal.
9594 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9595 EVT VT = N->getValueType(0);
9596 if (!TLI.isTypeLegal(VT) ||
9597 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9598 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9599 return SDValue();
9600
Andrew Trickef9de2a2013-05-25 02:42:55 +00009601 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009602 Op0.getOperand(0), Op1.getOperand(0));
9603 // Translate the shuffle mask.
9604 SmallVector<int, 16> NewMask;
9605 unsigned NumElts = VT.getVectorNumElements();
9606 unsigned HalfElts = NumElts/2;
9607 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9608 for (unsigned n = 0; n < NumElts; ++n) {
9609 int MaskElt = SVN->getMaskElt(n);
9610 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009611 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009612 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009613 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009614 NewElt = HalfElts + MaskElt - NumElts;
9615 NewMask.push_back(NewElt);
9616 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009617 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009618 DAG.getUNDEF(VT), NewMask.data());
9619}
9620
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009621/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9622/// NEON load/store intrinsics, and generic vector load/stores, to merge
9623/// base address updates.
9624/// For generic load/stores, the memory type is assumed to be a vector.
9625/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009626static SDValue CombineBaseUpdate(SDNode *N,
9627 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009628 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009629 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9630 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009631 const bool isStore = N->getOpcode() == ISD::STORE;
9632 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009633 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009634 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009635 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009636
9637 // Search for a use of the address operand that is an increment.
9638 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9639 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9640 SDNode *User = *UI;
9641 if (User->getOpcode() != ISD::ADD ||
9642 UI.getUse().getResNo() != Addr.getResNo())
9643 continue;
9644
9645 // Check that the add is independent of the load/store. Otherwise, folding
9646 // it would create a cycle.
9647 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9648 continue;
9649
9650 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009651 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009652 bool isLaneOp = false;
9653 unsigned NewOpc = 0;
9654 unsigned NumVecs = 0;
9655 if (isIntrinsic) {
9656 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9657 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009658 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009659 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9660 NumVecs = 1; break;
9661 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9662 NumVecs = 2; break;
9663 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9664 NumVecs = 3; break;
9665 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9666 NumVecs = 4; break;
9667 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9668 NumVecs = 2; isLaneOp = true; break;
9669 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9670 NumVecs = 3; isLaneOp = true; break;
9671 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9672 NumVecs = 4; isLaneOp = true; break;
9673 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009674 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009675 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009676 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009677 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009678 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009679 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009680 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009681 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009682 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009683 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009684 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009685 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009686 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009687 }
9688 } else {
9689 isLaneOp = true;
9690 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009691 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009692 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9693 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9694 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009695 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9696 NumVecs = 1; isLaneOp = false; break;
9697 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9698 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009699 }
9700 }
9701
9702 // Find the size of memory referenced by the load/store.
9703 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009704 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009705 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009706 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009707 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009708 } else {
9709 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9710 VecTy = N->getOperand(1).getValueType();
9711 }
9712
Bob Wilson06fce872011-02-07 17:43:21 +00009713 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9714 if (isLaneOp)
9715 NumBytes /= VecTy.getVectorNumElements();
9716
9717 // If the increment is a constant, it must match the memory ref size.
9718 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9719 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9720 uint64_t IncVal = CInc->getZExtValue();
9721 if (IncVal != NumBytes)
9722 continue;
9723 } else if (NumBytes >= 3 * 16) {
9724 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9725 // separate instructions that make it harder to use a non-constant update.
9726 continue;
9727 }
9728
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009729 // OK, we found an ADD we can fold into the base update.
9730 // Now, create a _UPD node, taking care of not breaking alignment.
9731
9732 EVT AlignedVecTy = VecTy;
9733 unsigned Alignment = MemN->getAlignment();
9734
9735 // If this is a less-than-standard-aligned load/store, change the type to
9736 // match the standard alignment.
9737 // The alignment is overlooked when selecting _UPD variants; and it's
9738 // easier to introduce bitcasts here than fix that.
9739 // There are 3 ways to get to this base-update combine:
9740 // - intrinsics: they are assumed to be properly aligned (to the standard
9741 // alignment of the memory type), so we don't need to do anything.
9742 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9743 // intrinsics, so, likewise, there's nothing to do.
9744 // - generic load/store instructions: the alignment is specified as an
9745 // explicit operand, rather than implicitly as the standard alignment
9746 // of the memory type (like the intrisics). We need to change the
9747 // memory type to match the explicit alignment. That way, we don't
9748 // generate non-standard-aligned ARMISD::VLDx nodes.
9749 if (isa<LSBaseSDNode>(N)) {
9750 if (Alignment == 0)
9751 Alignment = 1;
9752 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9753 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9754 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9755 assert(!isLaneOp && "Unexpected generic load/store lane.");
9756 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9757 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9758 }
9759 // Don't set an explicit alignment on regular load/stores that we want
9760 // to transform to VLD/VST 1_UPD nodes.
9761 // This matches the behavior of regular load/stores, which only get an
9762 // explicit alignment if the MMO alignment is larger than the standard
9763 // alignment of the memory type.
9764 // Intrinsics, however, always get an explicit alignment, set to the
9765 // alignment of the MMO.
9766 Alignment = 1;
9767 }
9768
Bob Wilson06fce872011-02-07 17:43:21 +00009769 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009770 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009771 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009772 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009773 unsigned n;
9774 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009775 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009776 Tys[n++] = MVT::i32;
9777 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009778 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009779
9780 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009781 SmallVector<SDValue, 8> Ops;
9782 Ops.push_back(N->getOperand(0)); // incoming chain
9783 Ops.push_back(N->getOperand(AddrOpIdx));
9784 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009785
9786 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9787 // Try to match the intrinsic's signature
9788 Ops.push_back(StN->getValue());
9789 } else {
9790 // Loads (and of course intrinsics) match the intrinsics' signature,
9791 // so just add all but the alignment operand.
9792 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9793 Ops.push_back(N->getOperand(i));
9794 }
9795
9796 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009797 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009798
9799 // If this is a non-standard-aligned STORE, the penultimate operand is the
9800 // stored value. Bitcast it to the aligned type.
9801 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9802 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009803 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009804 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009805
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009806 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009807 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009808 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009809
9810 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009811 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009812 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009813 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009814
9815 // If this is an non-standard-aligned LOAD, the first result is the loaded
9816 // value. Bitcast it to the expected result type.
9817 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9818 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009819 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009820 }
9821
Bob Wilson06fce872011-02-07 17:43:21 +00009822 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9823 DCI.CombineTo(N, NewResults);
9824 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9825
9826 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009827 }
Bob Wilson06fce872011-02-07 17:43:21 +00009828 return SDValue();
9829}
9830
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009831static SDValue PerformVLDCombine(SDNode *N,
9832 TargetLowering::DAGCombinerInfo &DCI) {
9833 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9834 return SDValue();
9835
9836 return CombineBaseUpdate(N, DCI);
9837}
9838
Bob Wilson2d790df2010-11-28 06:51:26 +00009839/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9840/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9841/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9842/// return true.
9843static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9844 SelectionDAG &DAG = DCI.DAG;
9845 EVT VT = N->getValueType(0);
9846 // vldN-dup instructions only support 64-bit vectors for N > 1.
9847 if (!VT.is64BitVector())
9848 return false;
9849
9850 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9851 SDNode *VLD = N->getOperand(0).getNode();
9852 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9853 return false;
9854 unsigned NumVecs = 0;
9855 unsigned NewOpc = 0;
9856 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9857 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9858 NumVecs = 2;
9859 NewOpc = ARMISD::VLD2DUP;
9860 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9861 NumVecs = 3;
9862 NewOpc = ARMISD::VLD3DUP;
9863 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9864 NumVecs = 4;
9865 NewOpc = ARMISD::VLD4DUP;
9866 } else {
9867 return false;
9868 }
9869
9870 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9871 // numbers match the load.
9872 unsigned VLDLaneNo =
9873 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9874 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9875 UI != UE; ++UI) {
9876 // Ignore uses of the chain result.
9877 if (UI.getUse().getResNo() == NumVecs)
9878 continue;
9879 SDNode *User = *UI;
9880 if (User->getOpcode() != ARMISD::VDUPLANE ||
9881 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9882 return false;
9883 }
9884
9885 // Create the vldN-dup node.
9886 EVT Tys[5];
9887 unsigned n;
9888 for (n = 0; n < NumVecs; ++n)
9889 Tys[n] = VT;
9890 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009891 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009892 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9893 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009894 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009895 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009896 VLDMemInt->getMemOperand());
9897
9898 // Update the uses.
9899 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9900 UI != UE; ++UI) {
9901 unsigned ResNo = UI.getUse().getResNo();
9902 // Ignore uses of the chain result.
9903 if (ResNo == NumVecs)
9904 continue;
9905 SDNode *User = *UI;
9906 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9907 }
9908
9909 // Now the vldN-lane intrinsic is dead except for its chain result.
9910 // Update uses of the chain.
9911 std::vector<SDValue> VLDDupResults;
9912 for (unsigned n = 0; n < NumVecs; ++n)
9913 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9914 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9915 DCI.CombineTo(VLD, VLDDupResults);
9916
9917 return true;
9918}
9919
Bob Wilson103a0dc2010-07-14 01:22:12 +00009920/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9921/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009922static SDValue PerformVDUPLANECombine(SDNode *N,
9923 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009924 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009925
Bob Wilson2d790df2010-11-28 06:51:26 +00009926 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9927 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9928 if (CombineVLDDUP(N, DCI))
9929 return SDValue(N, 0);
9930
9931 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9932 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009933 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009934 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009935 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009936 return SDValue();
9937
9938 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9939 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9940 // The canonical VMOV for a zero vector uses a 32-bit element size.
9941 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9942 unsigned EltBits;
9943 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9944 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009945 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009946 if (EltSize > VT.getVectorElementType().getSizeInBits())
9947 return SDValue();
9948
Andrew Trickef9de2a2013-05-25 02:42:55 +00009949 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009950}
9951
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009952static SDValue PerformLOADCombine(SDNode *N,
9953 TargetLowering::DAGCombinerInfo &DCI) {
9954 EVT VT = N->getValueType(0);
9955
9956 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9957 if (ISD::isNormalLoad(N) && VT.isVector() &&
9958 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9959 return CombineBaseUpdate(N, DCI);
9960
9961 return SDValue();
9962}
9963
Ahmed Bougacha23167462014-12-09 21:26:53 +00009964/// PerformSTORECombine - Target-specific dag combine xforms for
9965/// ISD::STORE.
9966static SDValue PerformSTORECombine(SDNode *N,
9967 TargetLowering::DAGCombinerInfo &DCI) {
9968 StoreSDNode *St = cast<StoreSDNode>(N);
9969 if (St->isVolatile())
9970 return SDValue();
9971
9972 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9973 // pack all of the elements in one place. Next, store to memory in fewer
9974 // chunks.
9975 SDValue StVal = St->getValue();
9976 EVT VT = StVal.getValueType();
9977 if (St->isTruncatingStore() && VT.isVector()) {
9978 SelectionDAG &DAG = DCI.DAG;
9979 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9980 EVT StVT = St->getMemoryVT();
9981 unsigned NumElems = VT.getVectorNumElements();
9982 assert(StVT != VT && "Cannot truncate to the same type");
9983 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9984 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9985
9986 // From, To sizes and ElemCount must be pow of two
9987 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9988
9989 // We are going to use the original vector elt for storing.
9990 // Accumulated smaller vector elements must be a multiple of the store size.
9991 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9992
9993 unsigned SizeRatio = FromEltSz / ToEltSz;
9994 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9995
9996 // Create a type on which we perform the shuffle.
9997 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9998 NumElems*SizeRatio);
9999 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
10000
10001 SDLoc DL(St);
10002 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
10003 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
10004 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +000010005 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
10006 ? (i + 1) * SizeRatio - 1
10007 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +000010008
10009 // Can't shuffle using an illegal type.
10010 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10011
10012 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10013 DAG.getUNDEF(WideVec.getValueType()),
10014 ShuffleVec.data());
10015 // At this point all of the data is stored at the bottom of the
10016 // register. We now need to save it to mem.
10017
10018 // Find the largest store unit
10019 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010020 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010021 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10022 StoreType = Tp;
10023 }
10024 // Didn't find a legal store type.
10025 if (!TLI.isTypeLegal(StoreType))
10026 return SDValue();
10027
10028 // Bitcast the original vector into a vector of store-size units
10029 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10030 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10031 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10032 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10033 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010034 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10035 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010036 SDValue BasePtr = St->getBasePtr();
10037
10038 // Perform one or more big stores into memory.
10039 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10040 for (unsigned I = 0; I < E; I++) {
10041 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10042 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010043 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010044 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
10045 St->getPointerInfo(), St->isVolatile(),
10046 St->isNonTemporal(), St->getAlignment());
10047 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10048 Increment);
10049 Chains.push_back(Ch);
10050 }
10051 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10052 }
10053
10054 if (!ISD::isNormalStore(St))
10055 return SDValue();
10056
10057 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10058 // ARM stores of arguments in the same cache line.
10059 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10060 StVal.getNode()->hasOneUse()) {
10061 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010062 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010063 SDLoc DL(St);
10064 SDValue BasePtr = St->getBasePtr();
10065 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
10066 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
10067 BasePtr, St->getPointerInfo(), St->isVolatile(),
10068 St->isNonTemporal(), St->getAlignment());
10069
10070 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010071 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010072 return DAG.getStore(NewST1.getValue(0), DL,
10073 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
10074 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
10075 St->isNonTemporal(),
10076 std::min(4U, St->getAlignment() / 2));
10077 }
10078
10079 if (StVal.getValueType() == MVT::i64 &&
10080 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10081
10082 // Bitcast an i64 store extracted from a vector to f64.
10083 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10084 SelectionDAG &DAG = DCI.DAG;
10085 SDLoc dl(StVal);
10086 SDValue IntVec = StVal.getOperand(0);
10087 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10088 IntVec.getValueType().getVectorNumElements());
10089 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10090 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10091 Vec, StVal.getOperand(1));
10092 dl = SDLoc(N);
10093 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10094 // Make the DAGCombiner fold the bitcasts.
10095 DCI.AddToWorklist(Vec.getNode());
10096 DCI.AddToWorklist(ExtElt.getNode());
10097 DCI.AddToWorklist(V.getNode());
10098 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
10099 St->getPointerInfo(), St->isVolatile(),
10100 St->isNonTemporal(), St->getAlignment(),
10101 St->getAAInfo());
10102 }
10103
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010104 // If this is a legal vector store, try to combine it into a VST1_UPD.
10105 if (ISD::isNormalStore(N) && VT.isVector() &&
10106 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10107 return CombineBaseUpdate(N, DCI);
10108
Ahmed Bougacha23167462014-12-09 21:26:53 +000010109 return SDValue();
10110}
10111
Chad Rosierfa8d8932011-06-24 19:23:04 +000010112/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10113/// can replace combinations of VMUL and VCVT (floating-point to integer)
10114/// when the VMUL has a constant operand that is a power of 2.
10115///
10116/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10117/// vmul.f32 d16, d17, d16
10118/// vcvt.s32.f32 d16, d16
10119/// becomes:
10120/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010121static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010122 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010123 if (!Subtarget->hasNEON())
10124 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010125
Chad Rosiera087fd22015-10-06 20:23:42 +000010126 SDValue Op = N->getOperand(0);
Tim Northover498c56c2016-03-17 20:10:28 +000010127 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
10128 Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010129 return SDValue();
10130
Chad Rosierfa8d8932011-06-24 19:23:04 +000010131 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010132 if (!isa<BuildVectorSDNode>(ConstVec))
10133 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010134
Tim Northover7cbc2152013-06-28 15:29:25 +000010135 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010136 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010137 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010138 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010139 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010140 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010141 // These instructions only exist converting from f32 to i32. We can handle
10142 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010143 // be lossy. We also can't handle more then 4 lanes, since these intructions
10144 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010145 return SDValue();
10146 }
10147
Chad Rosier169865f2015-10-07 17:28:58 +000010148 BitVector UndefElements;
10149 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10150 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10151 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010152 return SDValue();
10153
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010154 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010155 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010156 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10157 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010158 SDValue FixConv = DAG.getNode(
10159 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10160 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010161 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010162
Chad Rosier9df4aff2015-10-06 20:45:45 +000010163 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010164 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010165
10166 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010167}
10168
10169/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10170/// can replace combinations of VCVT (integer to floating-point) and VDIV
10171/// when the VDIV has a constant operand that is a power of 2.
10172///
10173/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10174/// vcvt.f32.s32 d16, d16
10175/// vdiv.f32 d16, d17, d16
10176/// becomes:
10177/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010178static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010179 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010180 if (!Subtarget->hasNEON())
10181 return SDValue();
10182
Chad Rosierfa8d8932011-06-24 19:23:04 +000010183 SDValue Op = N->getOperand(0);
10184 unsigned OpOpcode = Op.getNode()->getOpcode();
Tim Northover498c56c2016-03-17 20:10:28 +000010185 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010186 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10187 return SDValue();
10188
Chad Rosierfa8d8932011-06-24 19:23:04 +000010189 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010190 if (!isa<BuildVectorSDNode>(ConstVec))
10191 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010192
Tim Northover7cbc2152013-06-28 15:29:25 +000010193 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010194 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010195 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010196 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010197 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10198 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010199 // These instructions only exist converting from i32 to f32. We can handle
10200 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010201 // be lossy. We also can't handle more then 4 lanes, since these intructions
10202 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010203 return SDValue();
10204 }
10205
Chad Rosier169865f2015-10-07 17:28:58 +000010206 BitVector UndefElements;
10207 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10208 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10209 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010210 return SDValue();
10211
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010212 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010213 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010214 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010215 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010216 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010217 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010218 ConvInput);
10219
Eric Christopher1b8b94192011-06-29 21:10:36 +000010220 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010221 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010222 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010223 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010224 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010225 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010226}
10227
10228/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010229/// operand of a vector shift operation, where all the elements of the
10230/// build_vector must have the same constant integer value.
10231static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10232 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010233 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010234 Op = Op.getOperand(0);
10235 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10236 APInt SplatBits, SplatUndef;
10237 unsigned SplatBitSize;
10238 bool HasAnyUndefs;
10239 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10240 HasAnyUndefs, ElementBits) ||
10241 SplatBitSize > ElementBits)
10242 return false;
10243 Cnt = SplatBits.getSExtValue();
10244 return true;
10245}
10246
10247/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10248/// operand of a vector shift left operation. That value must be in the range:
10249/// 0 <= Value < ElementBits for a left shift; or
10250/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010251static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010252 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010253 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010254 if (! getVShiftImm(Op, ElementBits, Cnt))
10255 return false;
10256 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10257}
10258
10259/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10260/// operand of a vector shift right operation. For a shift opcode, the value
10261/// is positive, but for an intrinsic the value count must be negative. The
10262/// absolute value must be in the range:
10263/// 1 <= |Value| <= ElementBits for a right shift; or
10264/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010265static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010266 int64_t &Cnt) {
10267 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010268 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010269 if (! getVShiftImm(Op, ElementBits, Cnt))
10270 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010271 if (!isIntrinsic)
10272 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10273 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010274 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010275 return true;
10276 }
10277 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010278}
10279
10280/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10281static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10282 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10283 switch (IntNo) {
10284 default:
10285 // Don't do anything for most intrinsics.
10286 break;
10287
10288 // Vector shifts: check for immediate versions and lower them.
10289 // Note: This is done during DAG combining instead of DAG legalizing because
10290 // the build_vectors for 64-bit vector element shift counts are generally
10291 // not legal, and it is hard to see their values after they get legalized to
10292 // loads from a constant pool.
10293 case Intrinsic::arm_neon_vshifts:
10294 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010295 case Intrinsic::arm_neon_vrshifts:
10296 case Intrinsic::arm_neon_vrshiftu:
10297 case Intrinsic::arm_neon_vrshiftn:
10298 case Intrinsic::arm_neon_vqshifts:
10299 case Intrinsic::arm_neon_vqshiftu:
10300 case Intrinsic::arm_neon_vqshiftsu:
10301 case Intrinsic::arm_neon_vqshiftns:
10302 case Intrinsic::arm_neon_vqshiftnu:
10303 case Intrinsic::arm_neon_vqshiftnsu:
10304 case Intrinsic::arm_neon_vqrshiftns:
10305 case Intrinsic::arm_neon_vqrshiftnu:
10306 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010307 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010308 int64_t Cnt;
10309 unsigned VShiftOpc = 0;
10310
10311 switch (IntNo) {
10312 case Intrinsic::arm_neon_vshifts:
10313 case Intrinsic::arm_neon_vshiftu:
10314 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10315 VShiftOpc = ARMISD::VSHL;
10316 break;
10317 }
10318 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10319 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10320 ARMISD::VSHRs : ARMISD::VSHRu);
10321 break;
10322 }
10323 return SDValue();
10324
Bob Wilson2e076c42009-06-22 23:27:02 +000010325 case Intrinsic::arm_neon_vrshifts:
10326 case Intrinsic::arm_neon_vrshiftu:
10327 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10328 break;
10329 return SDValue();
10330
10331 case Intrinsic::arm_neon_vqshifts:
10332 case Intrinsic::arm_neon_vqshiftu:
10333 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10334 break;
10335 return SDValue();
10336
10337 case Intrinsic::arm_neon_vqshiftsu:
10338 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10339 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010340 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010341
Bob Wilson2e076c42009-06-22 23:27:02 +000010342 case Intrinsic::arm_neon_vrshiftn:
10343 case Intrinsic::arm_neon_vqshiftns:
10344 case Intrinsic::arm_neon_vqshiftnu:
10345 case Intrinsic::arm_neon_vqshiftnsu:
10346 case Intrinsic::arm_neon_vqrshiftns:
10347 case Intrinsic::arm_neon_vqrshiftnu:
10348 case Intrinsic::arm_neon_vqrshiftnsu:
10349 // Narrowing shifts require an immediate right shift.
10350 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
10351 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000010352 llvm_unreachable("invalid shift count for narrowing vector shift "
10353 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010354
10355 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000010356 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000010357 }
10358
10359 switch (IntNo) {
10360 case Intrinsic::arm_neon_vshifts:
10361 case Intrinsic::arm_neon_vshiftu:
10362 // Opcode already set above.
10363 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000010364 case Intrinsic::arm_neon_vrshifts:
10365 VShiftOpc = ARMISD::VRSHRs; break;
10366 case Intrinsic::arm_neon_vrshiftu:
10367 VShiftOpc = ARMISD::VRSHRu; break;
10368 case Intrinsic::arm_neon_vrshiftn:
10369 VShiftOpc = ARMISD::VRSHRN; break;
10370 case Intrinsic::arm_neon_vqshifts:
10371 VShiftOpc = ARMISD::VQSHLs; break;
10372 case Intrinsic::arm_neon_vqshiftu:
10373 VShiftOpc = ARMISD::VQSHLu; break;
10374 case Intrinsic::arm_neon_vqshiftsu:
10375 VShiftOpc = ARMISD::VQSHLsu; break;
10376 case Intrinsic::arm_neon_vqshiftns:
10377 VShiftOpc = ARMISD::VQSHRNs; break;
10378 case Intrinsic::arm_neon_vqshiftnu:
10379 VShiftOpc = ARMISD::VQSHRNu; break;
10380 case Intrinsic::arm_neon_vqshiftnsu:
10381 VShiftOpc = ARMISD::VQSHRNsu; break;
10382 case Intrinsic::arm_neon_vqrshiftns:
10383 VShiftOpc = ARMISD::VQRSHRNs; break;
10384 case Intrinsic::arm_neon_vqrshiftnu:
10385 VShiftOpc = ARMISD::VQRSHRNu; break;
10386 case Intrinsic::arm_neon_vqrshiftnsu:
10387 VShiftOpc = ARMISD::VQRSHRNsu; break;
10388 }
10389
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010390 SDLoc dl(N);
10391 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
10392 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010393 }
10394
10395 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010396 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010397 int64_t Cnt;
10398 unsigned VShiftOpc = 0;
10399
10400 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
10401 VShiftOpc = ARMISD::VSLI;
10402 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
10403 VShiftOpc = ARMISD::VSRI;
10404 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010405 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010406 }
10407
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010408 SDLoc dl(N);
10409 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000010410 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010411 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010412 }
10413
10414 case Intrinsic::arm_neon_vqrshifts:
10415 case Intrinsic::arm_neon_vqrshiftu:
10416 // No immediate versions of these to check for.
10417 break;
10418 }
10419
10420 return SDValue();
10421}
10422
10423/// PerformShiftCombine - Checks for immediate versions of vector shifts and
10424/// lowers them. As with the vector shift intrinsics, this is done during DAG
10425/// combining instead of DAG legalizing because the build_vectors for 64-bit
10426/// vector element shift counts are generally not legal, and it is hard to see
10427/// their values after they get legalized to loads from a constant pool.
10428static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
10429 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010430 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000010431 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
10432 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
10433 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
10434 SDValue N1 = N->getOperand(1);
10435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
10436 SDValue N0 = N->getOperand(0);
10437 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
10438 DAG.MaskedValueIsZero(N0.getOperand(0),
10439 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010440 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000010441 }
10442 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010443
10444 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000010445 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10446 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000010447 return SDValue();
10448
10449 assert(ST->hasNEON() && "unexpected vector shift");
10450 int64_t Cnt;
10451
10452 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010453 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010454
10455 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010456 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
10457 SDLoc dl(N);
10458 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
10459 DAG.getConstant(Cnt, dl, MVT::i32));
10460 }
Bob Wilson2e076c42009-06-22 23:27:02 +000010461 break;
10462
10463 case ISD::SRA:
10464 case ISD::SRL:
10465 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
10466 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
10467 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010468 SDLoc dl(N);
10469 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
10470 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000010471 }
10472 }
10473 return SDValue();
10474}
10475
10476/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
10477/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
10478static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
10479 const ARMSubtarget *ST) {
10480 SDValue N0 = N->getOperand(0);
10481
10482 // Check for sign- and zero-extensions of vector extract operations of 8-
10483 // and 16-bit vector elements. NEON supports these directly. They are
10484 // handled during DAG combining because type legalization will promote them
10485 // to 32-bit types and it is messy to recognize the operations after that.
10486 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10487 SDValue Vec = N0.getOperand(0);
10488 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000010489 EVT VT = N->getValueType(0);
10490 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010491 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10492
Owen Anderson9f944592009-08-11 20:47:22 +000010493 if (VT == MVT::i32 &&
10494 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000010495 TLI.isTypeLegal(Vec.getValueType()) &&
10496 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010497
10498 unsigned Opc = 0;
10499 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000010500 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000010501 case ISD::SIGN_EXTEND:
10502 Opc = ARMISD::VGETLANEs;
10503 break;
10504 case ISD::ZERO_EXTEND:
10505 case ISD::ANY_EXTEND:
10506 Opc = ARMISD::VGETLANEu;
10507 break;
10508 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010509 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000010510 }
10511 }
10512
10513 return SDValue();
10514}
10515
James Molloy9d55f192015-11-10 14:22:05 +000010516static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
10517 APInt &KnownOne) {
10518 if (Op.getOpcode() == ARMISD::BFI) {
10519 // Conservatively, we can recurse down the first operand
10520 // and just mask out all affected bits.
10521 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
10522
10523 // The operand to BFI is already a mask suitable for removing the bits it
10524 // sets.
10525 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
10526 APInt Mask = CI->getAPIntValue();
10527 KnownZero &= Mask;
10528 KnownOne &= Mask;
10529 return;
10530 }
10531 if (Op.getOpcode() == ARMISD::CMOV) {
10532 APInt KZ2(KnownZero.getBitWidth(), 0);
10533 APInt KO2(KnownOne.getBitWidth(), 0);
10534 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
10535 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
10536
10537 KnownZero &= KZ2;
10538 KnownOne &= KO2;
10539 return;
10540 }
10541 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
10542}
10543
10544SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
10545 // If we have a CMOV, OR and AND combination such as:
10546 // if (x & CN)
10547 // y |= CM;
10548 //
10549 // And:
10550 // * CN is a single bit;
10551 // * All bits covered by CM are known zero in y
10552 //
10553 // Then we can convert this into a sequence of BFI instructions. This will
10554 // always be a win if CM is a single bit, will always be no worse than the
10555 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
10556 // three bits (due to the extra IT instruction).
10557
10558 SDValue Op0 = CMOV->getOperand(0);
10559 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000010560 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
10561 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000010562 SDValue CmpZ = CMOV->getOperand(4);
10563
James Molloy20180912015-11-16 10:49:25 +000010564 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000010565 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000010566 return SDValue();
10567
James Molloy9d55f192015-11-10 14:22:05 +000010568 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
10569 SDValue And = CmpZ->getOperand(0);
10570 if (And->getOpcode() != ISD::AND)
10571 return SDValue();
10572 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
10573 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
10574 return SDValue();
10575 SDValue X = And->getOperand(0);
10576
James Molloy8e99e972015-11-12 13:49:17 +000010577 if (CC == ARMCC::EQ) {
10578 // We're performing an "equal to zero" compare. Swap the operands so we
10579 // canonicalize on a "not equal to zero" compare.
10580 std::swap(Op0, Op1);
10581 } else {
10582 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
10583 }
Junmo Park1108ab02016-02-19 01:46:04 +000010584
James Molloy9d55f192015-11-10 14:22:05 +000010585 if (Op1->getOpcode() != ISD::OR)
10586 return SDValue();
10587
10588 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
10589 if (!OrC)
10590 return SDValue();
10591 SDValue Y = Op1->getOperand(0);
10592
10593 if (Op0 != Y)
10594 return SDValue();
10595
10596 // Now, is it profitable to continue?
10597 APInt OrCI = OrC->getAPIntValue();
10598 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
10599 if (OrCI.countPopulation() > Heuristic)
10600 return SDValue();
10601
10602 // Lastly, can we determine that the bits defined by OrCI
10603 // are zero in Y?
10604 APInt KnownZero, KnownOne;
10605 computeKnownBits(DAG, Y, KnownZero, KnownOne);
10606 if ((OrCI & KnownZero) != OrCI)
10607 return SDValue();
10608
10609 // OK, we can do the combine.
10610 SDValue V = Y;
10611 SDLoc dl(X);
10612 EVT VT = X.getValueType();
10613 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000010614
James Molloy9d55f192015-11-10 14:22:05 +000010615 if (BitInX != 0) {
10616 // We must shift X first.
10617 X = DAG.getNode(ISD::SRL, dl, VT, X,
10618 DAG.getConstant(BitInX, dl, VT));
10619 }
10620
10621 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
10622 BitInY < NumActiveBits; ++BitInY) {
10623 if (OrCI[BitInY] == 0)
10624 continue;
10625 APInt Mask(VT.getSizeInBits(), 0);
10626 Mask.setBit(BitInY);
10627 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
10628 // Confusingly, the operand is an *inverted* mask.
10629 DAG.getConstant(~Mask, dl, VT));
10630 }
10631
10632 return V;
10633}
10634
Evan Chengf863e3f2011-07-13 00:42:17 +000010635/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10636SDValue
10637ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10638 SDValue Cmp = N->getOperand(4);
10639 if (Cmp.getOpcode() != ARMISD::CMPZ)
10640 // Only looking at EQ and NE cases.
10641 return SDValue();
10642
10643 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010644 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010645 SDValue LHS = Cmp.getOperand(0);
10646 SDValue RHS = Cmp.getOperand(1);
10647 SDValue FalseVal = N->getOperand(0);
10648 SDValue TrueVal = N->getOperand(1);
10649 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010650 ARMCC::CondCodes CC =
10651 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010652
James Molloy9d55f192015-11-10 14:22:05 +000010653 // BFI is only available on V6T2+.
10654 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
10655 SDValue R = PerformCMOVToBFICombine(N, DAG);
10656 if (R)
10657 return R;
10658 }
10659
Evan Chengf863e3f2011-07-13 00:42:17 +000010660 // Simplify
10661 // mov r1, r0
10662 // cmp r1, x
10663 // mov r0, y
10664 // moveq r0, x
10665 // to
10666 // cmp r0, x
10667 // movne r0, y
10668 //
10669 // mov r1, r0
10670 // cmp r1, x
10671 // mov r0, x
10672 // movne r0, y
10673 // to
10674 // cmp r0, x
10675 // movne r0, y
10676 /// FIXME: Turn this into a target neutral optimization?
10677 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010678 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010679 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10680 N->getOperand(3), Cmp);
10681 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10682 SDValue ARMcc;
10683 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10684 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10685 N->getOperand(3), NewCmp);
10686 }
10687
10688 if (Res.getNode()) {
10689 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010690 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010691 // Capture demanded bits information that would be otherwise lost.
10692 if (KnownZero == 0xfffffffe)
10693 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10694 DAG.getValueType(MVT::i1));
10695 else if (KnownZero == 0xffffff00)
10696 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10697 DAG.getValueType(MVT::i8));
10698 else if (KnownZero == 0xffff0000)
10699 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10700 DAG.getValueType(MVT::i16));
10701 }
10702
10703 return Res;
10704}
10705
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010706SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010707 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010708 switch (N->getOpcode()) {
10709 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010710 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010711 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010712 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010713 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010714 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010715 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10716 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010717 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010718 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010719 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010720 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010721 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010722 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010723 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010724 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010725 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000010726 case ISD::FP_TO_UINT:
10727 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
10728 case ISD::FDIV:
10729 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010730 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010731 case ISD::SHL:
10732 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010733 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010734 case ISD::SIGN_EXTEND:
10735 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010736 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010737 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010738 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010739 case ARMISD::VLD2DUP:
10740 case ARMISD::VLD3DUP:
10741 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010742 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010743 case ARMISD::BUILD_VECTOR:
10744 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010745 case ISD::INTRINSIC_VOID:
10746 case ISD::INTRINSIC_W_CHAIN:
10747 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10748 case Intrinsic::arm_neon_vld1:
10749 case Intrinsic::arm_neon_vld2:
10750 case Intrinsic::arm_neon_vld3:
10751 case Intrinsic::arm_neon_vld4:
10752 case Intrinsic::arm_neon_vld2lane:
10753 case Intrinsic::arm_neon_vld3lane:
10754 case Intrinsic::arm_neon_vld4lane:
10755 case Intrinsic::arm_neon_vst1:
10756 case Intrinsic::arm_neon_vst2:
10757 case Intrinsic::arm_neon_vst3:
10758 case Intrinsic::arm_neon_vst4:
10759 case Intrinsic::arm_neon_vst2lane:
10760 case Intrinsic::arm_neon_vst3lane:
10761 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010762 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010763 default: break;
10764 }
10765 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010766 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010767 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010768}
10769
Evan Chengd42641c2011-02-02 01:06:55 +000010770bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10771 EVT VT) const {
10772 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10773}
10774
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010775bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10776 unsigned,
10777 unsigned,
10778 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010779 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010780 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010781
10782 switch (VT.getSimpleVT().SimpleTy) {
10783 default:
10784 return false;
10785 case MVT::i8:
10786 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010787 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010788 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010789 if (AllowsUnaligned) {
10790 if (Fast)
10791 *Fast = Subtarget->hasV7Ops();
10792 return true;
10793 }
10794 return false;
10795 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010796 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010797 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010798 // For any little-endian targets with neon, we can support unaligned ld/st
10799 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010800 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010801 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010802 if (Fast)
10803 *Fast = true;
10804 return true;
10805 }
10806 return false;
10807 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010808 }
10809}
10810
Lang Hames9929c422011-11-02 22:52:45 +000010811static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10812 unsigned AlignCheck) {
10813 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10814 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10815}
10816
10817EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10818 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010819 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010820 bool MemcpyStrSrc,
10821 MachineFunction &MF) const {
10822 const Function *F = MF.getFunction();
10823
10824 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010825 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10826 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010827 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010828 if (Size >= 16 &&
10829 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010830 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010831 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010832 } else if (Size >= 8 &&
10833 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010834 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10835 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010836 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010837 }
10838 }
10839
Lang Hamesb85fcd02011-11-08 18:56:23 +000010840 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010841 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010842 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010843 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010844 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010845
Lang Hames9929c422011-11-02 22:52:45 +000010846 // Let the target-independent logic figure it out.
10847 return MVT::Other;
10848}
10849
Evan Cheng9ec512d2012-12-06 19:13:27 +000010850bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10851 if (Val.getOpcode() != ISD::LOAD)
10852 return false;
10853
10854 EVT VT1 = Val.getValueType();
10855 if (!VT1.isSimple() || !VT1.isInteger() ||
10856 !VT2.isSimple() || !VT2.isInteger())
10857 return false;
10858
10859 switch (VT1.getSimpleVT().SimpleTy) {
10860 default: break;
10861 case MVT::i1:
10862 case MVT::i8:
10863 case MVT::i16:
10864 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10865 return true;
10866 }
10867
10868 return false;
10869}
10870
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010871bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10872 EVT VT = ExtVal.getValueType();
10873
10874 if (!isTypeLegal(VT))
10875 return false;
10876
10877 // Don't create a loadext if we can fold the extension into a wide/long
10878 // instruction.
10879 // If there's more than one user instruction, the loadext is desirable no
10880 // matter what. There can be two uses by the same instruction.
10881 if (ExtVal->use_empty() ||
10882 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10883 return true;
10884
10885 SDNode *U = *ExtVal->use_begin();
10886 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10887 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10888 return false;
10889
10890 return true;
10891}
10892
Tim Northovercc2e9032013-08-06 13:58:03 +000010893bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10894 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10895 return false;
10896
10897 if (!isTypeLegal(EVT::getEVT(Ty1)))
10898 return false;
10899
10900 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10901
10902 // Assuming the caller doesn't have a zeroext or signext return parameter,
10903 // truncation all the way down to i1 is valid.
10904 return true;
10905}
10906
10907
Evan Chengdc49a8d2009-08-14 20:09:37 +000010908static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10909 if (V < 0)
10910 return false;
10911
10912 unsigned Scale = 1;
10913 switch (VT.getSimpleVT().SimpleTy) {
10914 default: return false;
10915 case MVT::i1:
10916 case MVT::i8:
10917 // Scale == 1;
10918 break;
10919 case MVT::i16:
10920 // Scale == 2;
10921 Scale = 2;
10922 break;
10923 case MVT::i32:
10924 // Scale == 4;
10925 Scale = 4;
10926 break;
10927 }
10928
10929 if ((V & (Scale - 1)) != 0)
10930 return false;
10931 V /= Scale;
10932 return V == (V & ((1LL << 5) - 1));
10933}
10934
10935static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10936 const ARMSubtarget *Subtarget) {
10937 bool isNeg = false;
10938 if (V < 0) {
10939 isNeg = true;
10940 V = - V;
10941 }
10942
10943 switch (VT.getSimpleVT().SimpleTy) {
10944 default: return false;
10945 case MVT::i1:
10946 case MVT::i8:
10947 case MVT::i16:
10948 case MVT::i32:
10949 // + imm12 or - imm8
10950 if (isNeg)
10951 return V == (V & ((1LL << 8) - 1));
10952 return V == (V & ((1LL << 12) - 1));
10953 case MVT::f32:
10954 case MVT::f64:
10955 // Same as ARM mode. FIXME: NEON?
10956 if (!Subtarget->hasVFP2())
10957 return false;
10958 if ((V & 3) != 0)
10959 return false;
10960 V >>= 2;
10961 return V == (V & ((1LL << 8) - 1));
10962 }
10963}
10964
Evan Cheng2150b922007-03-12 23:30:29 +000010965/// isLegalAddressImmediate - Return true if the integer value can be used
10966/// as the offset of the target addressing mode for load / store of the
10967/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010968static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010969 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010970 if (V == 0)
10971 return true;
10972
Evan Chengce5dfb62009-03-09 19:15:00 +000010973 if (!VT.isSimple())
10974 return false;
10975
Evan Chengdc49a8d2009-08-14 20:09:37 +000010976 if (Subtarget->isThumb1Only())
10977 return isLegalT1AddressImmediate(V, VT);
10978 else if (Subtarget->isThumb2())
10979 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010980
Evan Chengdc49a8d2009-08-14 20:09:37 +000010981 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010982 if (V < 0)
10983 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010984 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010985 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010986 case MVT::i1:
10987 case MVT::i8:
10988 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010989 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010990 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010991 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010992 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010993 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010994 case MVT::f32:
10995 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010996 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010997 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010998 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010999 return false;
11000 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011001 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000011002 }
Evan Cheng10043e22007-01-19 07:51:42 +000011003}
11004
Evan Chengdc49a8d2009-08-14 20:09:37 +000011005bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
11006 EVT VT) const {
11007 int Scale = AM.Scale;
11008 if (Scale < 0)
11009 return false;
11010
11011 switch (VT.getSimpleVT().SimpleTy) {
11012 default: return false;
11013 case MVT::i1:
11014 case MVT::i8:
11015 case MVT::i16:
11016 case MVT::i32:
11017 if (Scale == 1)
11018 return true;
11019 // r + r << imm
11020 Scale = Scale & ~1;
11021 return Scale == 2 || Scale == 4 || Scale == 8;
11022 case MVT::i64:
11023 // r + r
11024 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11025 return true;
11026 return false;
11027 case MVT::isVoid:
11028 // Note, we allow "void" uses (basically, uses that aren't loads or
11029 // stores), because arm allows folding a scale into many arithmetic
11030 // operations. This should be made more precise and revisited later.
11031
11032 // Allow r << imm, but the imm has to be a multiple of two.
11033 if (Scale & 1) return false;
11034 return isPowerOf2_32(Scale);
11035 }
11036}
11037
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011038/// isLegalAddressingMode - Return true if the addressing mode represented
11039/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011040bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11041 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011042 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011043 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011044 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011045 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011046
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011047 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011048 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011049 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011050
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011051 switch (AM.Scale) {
11052 case 0: // no scale reg, must be "r+i" or "r", or "i".
11053 break;
11054 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011055 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011056 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000011057 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011058 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011059 // ARM doesn't support any R+R*scale+imm addr modes.
11060 if (AM.BaseOffs)
11061 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011062
Bob Wilson866c1742009-04-08 17:55:28 +000011063 if (!VT.isSimple())
11064 return false;
11065
Evan Chengdc49a8d2009-08-14 20:09:37 +000011066 if (Subtarget->isThumb2())
11067 return isLegalT2ScaledAddressingMode(AM, VT);
11068
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011069 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011070 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011071 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011072 case MVT::i1:
11073 case MVT::i8:
11074 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011075 if (Scale < 0) Scale = -Scale;
11076 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011077 return true;
11078 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011079 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011080 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011081 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011082 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011083 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011084 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011085 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011086
Owen Anderson9f944592009-08-11 20:47:22 +000011087 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011088 // Note, we allow "void" uses (basically, uses that aren't loads or
11089 // stores), because arm allows folding a scale into many arithmetic
11090 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011091
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011092 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011093 if (Scale & 1) return false;
11094 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011095 }
Evan Cheng2150b922007-03-12 23:30:29 +000011096 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011097 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011098}
11099
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011100/// isLegalICmpImmediate - Return true if the specified immediate is legal
11101/// icmp immediate, that is the target has icmp instructions which can compare
11102/// a register against the immediate without having to materialize the
11103/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011104bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011105 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011106 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011107 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011108 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011109 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011110 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011111 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011112}
11113
Andrew Tricka22cdb72012-07-18 18:34:27 +000011114/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11115/// *or sub* immediate, that is the target has add or sub instructions which can
11116/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011117/// immediate into a register.
11118bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011119 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011120 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011121 if (!Subtarget->isThumb())
11122 return ARM_AM::getSOImmVal(AbsImm) != -1;
11123 if (Subtarget->isThumb2())
11124 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11125 // Thumb1 only has 8-bit unsigned immediate.
11126 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011127}
11128
Owen Anderson53aa7a92009-08-10 22:56:29 +000011129static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011130 bool isSEXTLoad, SDValue &Base,
11131 SDValue &Offset, bool &isInc,
11132 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011133 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11134 return false;
11135
Owen Anderson9f944592009-08-11 20:47:22 +000011136 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011137 // AddressingMode 3
11138 Base = Ptr->getOperand(0);
11139 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011140 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011141 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011142 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011143 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011144 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011145 return true;
11146 }
11147 }
11148 isInc = (Ptr->getOpcode() == ISD::ADD);
11149 Offset = Ptr->getOperand(1);
11150 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011151 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011152 // AddressingMode 2
11153 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011154 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011155 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011156 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011157 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011158 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011159 Base = Ptr->getOperand(0);
11160 return true;
11161 }
11162 }
11163
11164 if (Ptr->getOpcode() == ISD::ADD) {
11165 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011166 ARM_AM::ShiftOpc ShOpcVal=
11167 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011168 if (ShOpcVal != ARM_AM::no_shift) {
11169 Base = Ptr->getOperand(1);
11170 Offset = Ptr->getOperand(0);
11171 } else {
11172 Base = Ptr->getOperand(0);
11173 Offset = Ptr->getOperand(1);
11174 }
11175 return true;
11176 }
11177
11178 isInc = (Ptr->getOpcode() == ISD::ADD);
11179 Base = Ptr->getOperand(0);
11180 Offset = Ptr->getOperand(1);
11181 return true;
11182 }
11183
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011184 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011185 return false;
11186}
11187
Owen Anderson53aa7a92009-08-10 22:56:29 +000011188static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011189 bool isSEXTLoad, SDValue &Base,
11190 SDValue &Offset, bool &isInc,
11191 SelectionDAG &DAG) {
11192 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11193 return false;
11194
11195 Base = Ptr->getOperand(0);
11196 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11197 int RHSC = (int)RHS->getZExtValue();
11198 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11199 assert(Ptr->getOpcode() == ISD::ADD);
11200 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011201 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011202 return true;
11203 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11204 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011205 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011206 return true;
11207 }
11208 }
11209
11210 return false;
11211}
11212
Evan Cheng10043e22007-01-19 07:51:42 +000011213/// getPreIndexedAddressParts - returns true by value, base pointer and
11214/// offset pointer and addressing mode by reference if the node's address
11215/// can be legally represented as pre-indexed load / store address.
11216bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011217ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11218 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011219 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011220 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011221 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011222 return false;
11223
Owen Anderson53aa7a92009-08-10 22:56:29 +000011224 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011225 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011226 bool isSEXTLoad = false;
11227 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11228 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011229 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011230 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11231 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11232 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011233 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011234 } else
11235 return false;
11236
11237 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011238 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011239 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011240 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11241 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011242 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011243 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011244 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011245 if (!isLegal)
11246 return false;
11247
11248 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11249 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011250}
11251
11252/// getPostIndexedAddressParts - returns true by value, base pointer and
11253/// offset pointer and addressing mode by reference if this node can be
11254/// combined with a load / store to form a post-indexed load / store.
11255bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011256 SDValue &Base,
11257 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011258 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011259 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011260 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011261 return false;
11262
Owen Anderson53aa7a92009-08-10 22:56:29 +000011263 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011264 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011265 bool isSEXTLoad = false;
11266 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011267 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011268 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011269 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11270 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011271 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011272 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011273 } else
11274 return false;
11275
11276 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011277 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011278 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011279 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000011280 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011281 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011282 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
11283 isInc, DAG);
11284 if (!isLegal)
11285 return false;
11286
Evan Chengf19384d2010-05-18 21:31:17 +000011287 if (Ptr != Base) {
11288 // Swap base ptr and offset to catch more post-index load / store when
11289 // it's legal. In Thumb2 mode, offset must be an immediate.
11290 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
11291 !Subtarget->isThumb2())
11292 std::swap(Base, Offset);
11293
11294 // Post-indexed load / store update the base pointer.
11295 if (Ptr != Base)
11296 return false;
11297 }
11298
Evan Cheng84c6cda2009-07-02 07:28:31 +000011299 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
11300 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011301}
11302
Jay Foada0653a32014-05-14 21:14:37 +000011303void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
11304 APInt &KnownZero,
11305 APInt &KnownOne,
11306 const SelectionDAG &DAG,
11307 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000011308 unsigned BitWidth = KnownOne.getBitWidth();
11309 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000011310 switch (Op.getOpcode()) {
11311 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000011312 case ARMISD::ADDC:
11313 case ARMISD::ADDE:
11314 case ARMISD::SUBC:
11315 case ARMISD::SUBE:
11316 // These nodes' second result is a boolean
11317 if (Op.getResNo() == 0)
11318 break;
11319 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
11320 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011321 case ARMISD::CMOV: {
11322 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000011323 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011324 if (KnownZero == 0 && KnownOne == 0) return;
11325
Dan Gohmanf990faf2008-02-13 00:35:47 +000011326 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000011327 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000011328 KnownZero &= KnownZeroRHS;
11329 KnownOne &= KnownOneRHS;
11330 return;
11331 }
Tim Northover01b4aa92014-04-03 15:10:35 +000011332 case ISD::INTRINSIC_W_CHAIN: {
11333 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
11334 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
11335 switch (IntID) {
11336 default: return;
11337 case Intrinsic::arm_ldaex:
11338 case Intrinsic::arm_ldrex: {
11339 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
11340 unsigned MemBits = VT.getScalarType().getSizeInBits();
11341 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
11342 return;
11343 }
11344 }
11345 }
Evan Cheng10043e22007-01-19 07:51:42 +000011346 }
11347}
11348
11349//===----------------------------------------------------------------------===//
11350// ARM Inline Assembly Support
11351//===----------------------------------------------------------------------===//
11352
Evan Cheng078b0b02011-01-08 01:24:27 +000011353bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
11354 // Looking for "rev" which is V6+.
11355 if (!Subtarget->hasV6Ops())
11356 return false;
11357
11358 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
11359 std::string AsmStr = IA->getAsmString();
11360 SmallVector<StringRef, 4> AsmPieces;
11361 SplitString(AsmStr, AsmPieces, ";\n");
11362
11363 switch (AsmPieces.size()) {
11364 default: return false;
11365 case 1:
11366 AsmStr = AsmPieces[0];
11367 AsmPieces.clear();
11368 SplitString(AsmStr, AsmPieces, " \t,");
11369
11370 // rev $0, $1
11371 if (AsmPieces.size() == 3 &&
11372 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
11373 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000011374 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000011375 if (Ty && Ty->getBitWidth() == 32)
11376 return IntrinsicLowering::LowerToByteSwap(CI);
11377 }
11378 break;
11379 }
11380
11381 return false;
11382}
11383
Evan Cheng10043e22007-01-19 07:51:42 +000011384/// getConstraintType - Given a constraint letter, return the type of
11385/// constraint it is for this target.
11386ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011387ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000011388 if (Constraint.size() == 1) {
11389 switch (Constraint[0]) {
11390 default: break;
11391 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000011392 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000011393 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000011394 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000011395 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000011396 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000011397 // An address with a single base register. Due to the way we
11398 // currently handle addresses it is the same as an 'r' memory constraint.
11399 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000011400 }
Eric Christophere256cd02011-06-21 22:10:57 +000011401 } else if (Constraint.size() == 2) {
11402 switch (Constraint[0]) {
11403 default: break;
11404 // All 'U+' constraints are addresses.
11405 case 'U': return C_Memory;
11406 }
Evan Cheng10043e22007-01-19 07:51:42 +000011407 }
Chris Lattnerd6855142007-03-25 02:14:49 +000011408 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000011409}
11410
John Thompsone8360b72010-10-29 17:29:13 +000011411/// Examine constraint type and operand type and determine a weight value.
11412/// This object must already have been set up with the operand type
11413/// and the current alternative constraint selected.
11414TargetLowering::ConstraintWeight
11415ARMTargetLowering::getSingleConstraintMatchWeight(
11416 AsmOperandInfo &info, const char *constraint) const {
11417 ConstraintWeight weight = CW_Invalid;
11418 Value *CallOperandVal = info.CallOperandVal;
11419 // If we don't have a value, we can't do a match,
11420 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000011421 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000011422 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000011423 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000011424 // Look at the constraint type.
11425 switch (*constraint) {
11426 default:
11427 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11428 break;
11429 case 'l':
11430 if (type->isIntegerTy()) {
11431 if (Subtarget->isThumb())
11432 weight = CW_SpecificReg;
11433 else
11434 weight = CW_Register;
11435 }
11436 break;
11437 case 'w':
11438 if (type->isFloatingPointTy())
11439 weight = CW_Register;
11440 break;
11441 }
11442 return weight;
11443}
11444
Eric Christophercf2007c2011-06-30 23:50:52 +000011445typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000011446RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
11447 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000011448 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011449 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000011450 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000011451 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000011452 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011453 return RCPair(0U, &ARM::tGPRRegClass);
11454 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000011455 case 'h': // High regs or no regs.
11456 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000011457 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000011458 break;
Chris Lattner6223e832007-04-02 17:24:08 +000011459 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000011460 if (Subtarget->isThumb1Only())
11461 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000011462 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011463 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000011464 if (VT == MVT::Other)
11465 break;
Owen Anderson9f944592009-08-11 20:47:22 +000011466 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011467 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000011468 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011469 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000011470 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011471 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000011472 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011473 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000011474 if (VT == MVT::Other)
11475 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000011476 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011477 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011478 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000011479 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011480 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000011481 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000011482 break;
Eric Christopherc011d312011-07-01 00:30:46 +000011483 case 't':
11484 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000011485 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000011486 break;
Evan Cheng10043e22007-01-19 07:51:42 +000011487 }
11488 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000011489 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000011490 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000011491
Eric Christopher11e4df72015-02-26 22:38:43 +000011492 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000011493}
11494
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011495/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
11496/// vector. If it is invalid, don't add anything to Ops.
11497void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000011498 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011499 std::vector<SDValue>&Ops,
11500 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000011501 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011502
Eric Christopherde9399b2011-06-02 23:16:42 +000011503 // Currently only support length 1 constraints.
11504 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000011505
Eric Christopherde9399b2011-06-02 23:16:42 +000011506 char ConstraintLetter = Constraint[0];
11507 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011508 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000011509 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011510 case 'I': case 'J': case 'K': case 'L':
11511 case 'M': case 'N': case 'O':
11512 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
11513 if (!C)
11514 return;
11515
11516 int64_t CVal64 = C->getSExtValue();
11517 int CVal = (int) CVal64;
11518 // None of these constraints allow values larger than 32 bits. Check
11519 // that the value fits in an int.
11520 if (CVal != CVal64)
11521 return;
11522
Eric Christopherde9399b2011-06-02 23:16:42 +000011523 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000011524 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000011525 // Constant suitable for movw, must be between 0 and
11526 // 65535.
11527 if (Subtarget->hasV6T2Ops())
11528 if (CVal >= 0 && CVal <= 65535)
11529 break;
11530 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011531 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000011532 if (Subtarget->isThumb1Only()) {
11533 // This must be a constant between 0 and 255, for ADD
11534 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011535 if (CVal >= 0 && CVal <= 255)
11536 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011537 } else if (Subtarget->isThumb2()) {
11538 // A constant that can be used as an immediate value in a
11539 // data-processing instruction.
11540 if (ARM_AM::getT2SOImmVal(CVal) != -1)
11541 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011542 } else {
11543 // A constant that can be used as an immediate value in a
11544 // data-processing instruction.
11545 if (ARM_AM::getSOImmVal(CVal) != -1)
11546 break;
11547 }
11548 return;
11549
11550 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000011551 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011552 // This must be a constant between -255 and -1, for negated ADD
11553 // immediates. This can be used in GCC with an "n" modifier that
11554 // prints the negated value, for use with SUB instructions. It is
11555 // not useful otherwise but is implemented for compatibility.
11556 if (CVal >= -255 && CVal <= -1)
11557 break;
11558 } else {
11559 // This must be a constant between -4095 and 4095. It is not clear
11560 // what this constraint is intended for. Implemented for
11561 // compatibility with GCC.
11562 if (CVal >= -4095 && CVal <= 4095)
11563 break;
11564 }
11565 return;
11566
11567 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000011568 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011569 // A 32-bit value where only one byte has a nonzero value. Exclude
11570 // zero to match GCC. This constraint is used by GCC internally for
11571 // constants that can be loaded with a move/shift combination.
11572 // It is not useful otherwise but is implemented for compatibility.
11573 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
11574 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011575 } else if (Subtarget->isThumb2()) {
11576 // A constant whose bitwise inverse can be used as an immediate
11577 // value in a data-processing instruction. This can be used in GCC
11578 // with a "B" modifier that prints the inverted value, for use with
11579 // BIC and MVN instructions. It is not useful otherwise but is
11580 // implemented for compatibility.
11581 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
11582 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011583 } else {
11584 // A constant whose bitwise inverse can be used as an immediate
11585 // value in a data-processing instruction. This can be used in GCC
11586 // with a "B" modifier that prints the inverted value, for use with
11587 // BIC and MVN instructions. It is not useful otherwise but is
11588 // implemented for compatibility.
11589 if (ARM_AM::getSOImmVal(~CVal) != -1)
11590 break;
11591 }
11592 return;
11593
11594 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011595 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011596 // This must be a constant between -7 and 7,
11597 // for 3-operand ADD/SUB immediate instructions.
11598 if (CVal >= -7 && CVal < 7)
11599 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011600 } else if (Subtarget->isThumb2()) {
11601 // A constant whose negation can be used as an immediate value in a
11602 // data-processing instruction. This can be used in GCC with an "n"
11603 // modifier that prints the negated value, for use with SUB
11604 // instructions. It is not useful otherwise but is implemented for
11605 // compatibility.
11606 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11607 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011608 } else {
11609 // A constant whose negation can be used as an immediate value in a
11610 // data-processing instruction. This can be used in GCC with an "n"
11611 // modifier that prints the negated value, for use with SUB
11612 // instructions. It is not useful otherwise but is implemented for
11613 // compatibility.
11614 if (ARM_AM::getSOImmVal(-CVal) != -1)
11615 break;
11616 }
11617 return;
11618
11619 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000011620 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011621 // This must be a multiple of 4 between 0 and 1020, for
11622 // ADD sp + immediate.
11623 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11624 break;
11625 } else {
11626 // A power of two or a constant between 0 and 32. This is used in
11627 // GCC for the shift amount on shifted register operands, but it is
11628 // useful in general for any shift amounts.
11629 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11630 break;
11631 }
11632 return;
11633
11634 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011635 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011636 // This must be a constant between 0 and 31, for shift amounts.
11637 if (CVal >= 0 && CVal <= 31)
11638 break;
11639 }
11640 return;
11641
11642 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011643 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011644 // This must be a multiple of 4 between -508 and 508, for
11645 // ADD/SUB sp = sp + immediate.
11646 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11647 break;
11648 }
11649 return;
11650 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011651 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011652 break;
11653 }
11654
11655 if (Result.getNode()) {
11656 Ops.push_back(Result);
11657 return;
11658 }
Dale Johannesence97d552010-06-25 21:55:36 +000011659 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011660}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011661
Scott Douglassd2974a62015-08-24 09:17:11 +000011662static RTLIB::Libcall getDivRemLibcall(
11663 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011664 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11665 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011666 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000011667 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11668 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011669 RTLIB::Libcall LC;
11670 switch (SVT) {
11671 default: llvm_unreachable("Unexpected request for libcall!");
11672 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11673 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11674 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11675 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
11676 }
11677 return LC;
11678}
11679
11680static TargetLowering::ArgListTy getDivRemArgList(
11681 const SDNode *N, LLVMContext *Context) {
Scott Douglassbdef6042015-08-24 09:17:18 +000011682 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
11683 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000011684 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000011685 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
11686 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000011687 TargetLowering::ArgListTy Args;
11688 TargetLowering::ArgListEntry Entry;
11689 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
11690 EVT ArgVT = N->getOperand(i).getValueType();
11691 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
11692 Entry.Node = N->getOperand(i);
11693 Entry.Ty = ArgTy;
11694 Entry.isSExt = isSigned;
11695 Entry.isZExt = !isSigned;
11696 Args.push_back(Entry);
11697 }
11698 return Args;
11699}
11700
Renato Golin87610692013-07-16 09:32:17 +000011701SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000011702 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
11703 Subtarget->isTargetGNUAEABI()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000011704 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000011705 unsigned Opcode = Op->getOpcode();
11706 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011707 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011708 bool isSigned = (Opcode == ISD::SDIVREM);
11709 EVT VT = Op->getValueType(0);
11710 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11711
Scott Douglassd2974a62015-08-24 09:17:11 +000011712 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
11713 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000011714 SDValue InChain = DAG.getEntryNode();
11715
Scott Douglassd2974a62015-08-24 09:17:11 +000011716 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
11717 DAG.getContext());
Renato Golin87610692013-07-16 09:32:17 +000011718
11719 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011720 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011721
Reid Kleckner343c3952014-11-20 23:51:47 +000011722 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011723
11724 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011725 TargetLowering::CallLoweringInfo CLI(DAG);
11726 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011727 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011728 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011729
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011730 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011731 return CallInfo.first;
11732}
11733
Scott Douglassbdef6042015-08-24 09:17:18 +000011734// Lowers REM using divmod helpers
11735// see RTABI section 4.2/4.3
11736SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
11737 // Build return types (div and rem)
11738 std::vector<Type*> RetTyParams;
11739 Type *RetTyElement;
11740
11741 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
11742 default: llvm_unreachable("Unexpected request for libcall!");
11743 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
11744 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
11745 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
11746 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
11747 }
11748
11749 RetTyParams.push_back(RetTyElement);
11750 RetTyParams.push_back(RetTyElement);
11751 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
11752 Type *RetTy = StructType::get(*DAG.getContext(), ret);
11753
11754 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
11755 SimpleTy);
11756 SDValue InChain = DAG.getEntryNode();
11757 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext());
11758 bool isSigned = N->getOpcode() == ISD::SREM;
11759 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
11760 getPointerTy(DAG.getDataLayout()));
11761
11762 // Lower call
11763 CallLoweringInfo CLI(DAG);
11764 CLI.setChain(InChain)
11765 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args), 0)
11766 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
11767 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
11768
11769 // Return second (rem) result operand (first contains div)
11770 SDNode *ResNode = CallResult.first.getNode();
11771 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
11772 return ResNode->getOperand(1);
11773}
11774
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011775SDValue
11776ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11777 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11778 SDLoc DL(Op);
11779
11780 // Get the inputs.
11781 SDValue Chain = Op.getOperand(0);
11782 SDValue Size = Op.getOperand(1);
11783
11784 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011785 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011786
11787 SDValue Flag;
11788 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11789 Flag = Chain.getValue(1);
11790
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011791 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011792 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11793
11794 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11795 Chain = NewSP.getValue(1);
11796
11797 SDValue Ops[2] = { NewSP, Chain };
11798 return DAG.getMergeValues(Ops, DL);
11799}
11800
Oliver Stannard51b1d462014-08-21 12:50:31 +000011801SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11802 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11803 "Unexpected type for custom-lowering FP_EXTEND");
11804
11805 RTLIB::Libcall LC;
11806 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11807
11808 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011809 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11810 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011811}
11812
11813SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11814 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11815 Subtarget->isFPOnlySP() &&
11816 "Unexpected type for custom-lowering FP_ROUND");
11817
11818 RTLIB::Libcall LC;
11819 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11820
11821 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000011822 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
11823 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011824}
11825
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011826bool
11827ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11828 // The ARM target isn't yet aware of offsets.
11829 return false;
11830}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011831
Jim Grosbach11013ed2010-07-16 23:05:05 +000011832bool ARM::isBitFieldInvertedMask(unsigned v) {
11833 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011834 return false;
11835
Jim Grosbach11013ed2010-07-16 23:05:05 +000011836 // there can be 1's on either or both "outsides", all the "inside"
11837 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011838 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011839}
11840
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011841/// isFPImmLegal - Returns true if the target can instruction select the
11842/// specified FP immediate natively. If false, the legalizer will
11843/// materialize the FP immediate as a load from a constant pool.
11844bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11845 if (!Subtarget->hasVFP3())
11846 return false;
11847 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011848 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011849 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011850 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011851 return false;
11852}
Bob Wilson5549d492010-09-21 17:56:22 +000011853
Wesley Peck527da1b2010-11-23 03:31:01 +000011854/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011855/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11856/// specified in the intrinsic calls.
11857bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11858 const CallInst &I,
11859 unsigned Intrinsic) const {
11860 switch (Intrinsic) {
11861 case Intrinsic::arm_neon_vld1:
11862 case Intrinsic::arm_neon_vld2:
11863 case Intrinsic::arm_neon_vld3:
11864 case Intrinsic::arm_neon_vld4:
11865 case Intrinsic::arm_neon_vld2lane:
11866 case Intrinsic::arm_neon_vld3lane:
11867 case Intrinsic::arm_neon_vld4lane: {
11868 Info.opc = ISD::INTRINSIC_W_CHAIN;
11869 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011870 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011871 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011872 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11873 Info.ptrVal = I.getArgOperand(0);
11874 Info.offset = 0;
11875 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11876 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11877 Info.vol = false; // volatile loads with NEON intrinsics not supported
11878 Info.readMem = true;
11879 Info.writeMem = false;
11880 return true;
11881 }
11882 case Intrinsic::arm_neon_vst1:
11883 case Intrinsic::arm_neon_vst2:
11884 case Intrinsic::arm_neon_vst3:
11885 case Intrinsic::arm_neon_vst4:
11886 case Intrinsic::arm_neon_vst2lane:
11887 case Intrinsic::arm_neon_vst3lane:
11888 case Intrinsic::arm_neon_vst4lane: {
11889 Info.opc = ISD::INTRINSIC_VOID;
11890 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011891 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011892 unsigned NumElts = 0;
11893 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011894 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011895 if (!ArgTy->isVectorTy())
11896 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000011897 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000011898 }
11899 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11900 Info.ptrVal = I.getArgOperand(0);
11901 Info.offset = 0;
11902 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11903 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11904 Info.vol = false; // volatile stores with NEON intrinsics not supported
11905 Info.readMem = false;
11906 Info.writeMem = true;
11907 return true;
11908 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011909 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011910 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011911 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011912 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11913 Info.opc = ISD::INTRINSIC_W_CHAIN;
11914 Info.memVT = MVT::getVT(PtrTy->getElementType());
11915 Info.ptrVal = I.getArgOperand(0);
11916 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011917 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011918 Info.vol = true;
11919 Info.readMem = true;
11920 Info.writeMem = false;
11921 return true;
11922 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011923 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011924 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011925 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011926 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11927 Info.opc = ISD::INTRINSIC_W_CHAIN;
11928 Info.memVT = MVT::getVT(PtrTy->getElementType());
11929 Info.ptrVal = I.getArgOperand(1);
11930 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011931 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011932 Info.vol = true;
11933 Info.readMem = false;
11934 Info.writeMem = true;
11935 return true;
11936 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011937 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011938 case Intrinsic::arm_strexd: {
11939 Info.opc = ISD::INTRINSIC_W_CHAIN;
11940 Info.memVT = MVT::i64;
11941 Info.ptrVal = I.getArgOperand(2);
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 = false;
11946 Info.writeMem = true;
11947 return true;
11948 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011949 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011950 case Intrinsic::arm_ldrexd: {
11951 Info.opc = ISD::INTRINSIC_W_CHAIN;
11952 Info.memVT = MVT::i64;
11953 Info.ptrVal = I.getArgOperand(0);
11954 Info.offset = 0;
11955 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011956 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011957 Info.readMem = true;
11958 Info.writeMem = false;
11959 return true;
11960 }
Bob Wilson5549d492010-09-21 17:56:22 +000011961 default:
11962 break;
11963 }
11964
11965 return false;
11966}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011967
11968/// \brief Returns true if it is beneficial to convert a load of a constant
11969/// to just the constant itself.
11970bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11971 Type *Ty) const {
11972 assert(Ty->isIntegerTy());
11973
11974 unsigned Bits = Ty->getPrimitiveSizeInBits();
11975 if (Bits == 0 || Bits > 32)
11976 return false;
11977 return true;
11978}
Tim Northover037f26f22014-04-17 18:22:47 +000011979
Robin Morisset5349e8e2014-09-18 18:56:04 +000011980Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11981 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011982 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011983
11984 // First, if the target has no DMB, see what fallback we can use.
11985 if (!Subtarget->hasDataBarrier()) {
11986 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11987 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11988 // here.
11989 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11990 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11991 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11992 Builder.getInt32(0), Builder.getInt32(7),
11993 Builder.getInt32(10), Builder.getInt32(5)};
11994 return Builder.CreateCall(MCR, args);
11995 } else {
11996 // Instead of using barriers, atomic accesses on these subtargets use
11997 // libcalls.
11998 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11999 }
12000 } else {
12001 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
12002 // Only a full system barrier exists in the M-class architectures.
12003 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
12004 Constant *CDomain = Builder.getInt32(Domain);
12005 return Builder.CreateCall(DMB, CDomain);
12006 }
Robin Morisseta47cb412014-09-03 21:01:03 +000012007}
12008
12009// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000012010Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012011 AtomicOrdering Ord, bool IsStore,
12012 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012013 switch (Ord) {
12014 case NotAtomic:
12015 case Unordered:
12016 llvm_unreachable("Invalid fence: unordered/non-atomic");
12017 case Monotonic:
12018 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012019 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000012020 case SequentiallyConsistent:
12021 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012022 return nullptr; // Nothing to do
12023 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000012024 case Release:
12025 case AcquireRelease:
12026 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000012027 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012028 // FIXME: add a comment with a link to documentation justifying this.
12029 else
Robin Morissetdedef332014-09-23 20:31:14 +000012030 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012031 }
Robin Morissetdedef332014-09-23 20:31:14 +000012032 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012033}
12034
Robin Morissetdedef332014-09-23 20:31:14 +000012035Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012036 AtomicOrdering Ord, bool IsStore,
12037 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012038 switch (Ord) {
12039 case NotAtomic:
12040 case Unordered:
12041 llvm_unreachable("Invalid fence: unordered/not-atomic");
12042 case Monotonic:
12043 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012044 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000012045 case Acquire:
12046 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000012047 case SequentiallyConsistent:
12048 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012049 }
Robin Morissetdedef332014-09-23 20:31:14 +000012050 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012051}
12052
Robin Morisseted3d48f2014-09-03 21:29:59 +000012053// Loads and stores less than 64-bits are already atomic; ones above that
12054// are doomed anyway, so defer to the default libcall and blame the OS when
12055// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12056// anything for those.
12057bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12058 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12059 return (Size == 64) && !Subtarget->isMClass();
12060}
Tim Northover037f26f22014-04-17 18:22:47 +000012061
Robin Morisseted3d48f2014-09-03 21:29:59 +000012062// Loads and stores less than 64-bits are already atomic; ones above that
12063// are doomed anyway, so defer to the default libcall and blame the OS when
12064// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12065// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012066// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12067// guarantee, see DDI0406C ARM architecture reference manual,
12068// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012069TargetLowering::AtomicExpansionKind
12070ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012071 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012072 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012073 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012074}
12075
12076// For the real atomic operations, we have ldrex/strex up to 32 bits,
12077// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012078TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012079ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012080 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012081 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012082 ? AtomicExpansionKind::LLSC
12083 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012084}
12085
Ahmed Bougacha52468672015-09-11 17:08:28 +000012086bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12087 AtomicCmpXchgInst *AI) const {
12088 return true;
12089}
12090
James Y Knightf44fc522016-03-16 22:12:04 +000012091bool ARMTargetLowering::shouldInsertFencesForAtomic(
12092 const Instruction *I) const {
12093 return InsertFencesForAtomic;
12094}
12095
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012096// This has so far only been implemented for MachO.
12097bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012098 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012099}
12100
Quentin Colombetc32615d2014-10-31 17:52:53 +000012101bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12102 unsigned &Cost) const {
12103 // If we do not have NEON, vector types are not natively supported.
12104 if (!Subtarget->hasNEON())
12105 return false;
12106
12107 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012108 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012109 // better to leave at float as we have more freedom in the addressing mode for
12110 // those.
12111 if (VectorTy->isFPOrFPVectorTy())
12112 return false;
12113
12114 // If the index is unknown at compile time, this is very expensive to lower
12115 // and it is not possible to combine the store with the extract.
12116 if (!isa<ConstantInt>(Idx))
12117 return false;
12118
12119 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12120 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12121 // We can do a store + vector extract on any vector that fits perfectly in a D
12122 // or Q register.
12123 if (BitWidth == 64 || BitWidth == 128) {
12124 Cost = 0;
12125 return true;
12126 }
12127 return false;
12128}
12129
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012130bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12131 return Subtarget->hasV6T2Ops();
12132}
12133
12134bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12135 return Subtarget->hasV6T2Ops();
12136}
12137
Tim Northover037f26f22014-04-17 18:22:47 +000012138Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12139 AtomicOrdering Ord) const {
12140 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12141 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000012142 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012143
12144 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12145 // intrinsic must return {i32, i32} and we have to recombine them into a
12146 // single i64 here.
12147 if (ValTy->getPrimitiveSizeInBits() == 64) {
12148 Intrinsic::ID Int =
12149 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12150 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12151
12152 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12153 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12154
12155 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12156 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012157 if (!Subtarget->isLittle())
12158 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012159 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12160 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12161 return Builder.CreateOr(
12162 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12163 }
12164
12165 Type *Tys[] = { Addr->getType() };
12166 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12167 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12168
12169 return Builder.CreateTruncOrBitCast(
12170 Builder.CreateCall(Ldrex, Addr),
12171 cast<PointerType>(Addr->getType())->getElementType());
12172}
12173
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012174void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12175 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012176 if (!Subtarget->hasV7Ops())
12177 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012178 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12179 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12180}
12181
Tim Northover037f26f22014-04-17 18:22:47 +000012182Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12183 Value *Addr,
12184 AtomicOrdering Ord) const {
12185 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000012186 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012187
12188 // Since the intrinsics must have legal type, the i64 intrinsics take two
12189 // parameters: "i32, i32". We must marshal Val into the appropriate form
12190 // before the call.
12191 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12192 Intrinsic::ID Int =
12193 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12194 Function *Strex = Intrinsic::getDeclaration(M, Int);
12195 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12196
12197 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12198 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012199 if (!Subtarget->isLittle())
12200 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012201 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012202 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012203 }
12204
12205 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12206 Type *Tys[] = { Addr->getType() };
12207 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12208
David Blaikieff6409d2015-05-18 22:13:54 +000012209 return Builder.CreateCall(
12210 Strex, {Builder.CreateZExtOrBitCast(
12211 Val, Strex->getFunctionType()->getParamType(0)),
12212 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012213}
Oliver Stannardc24f2172014-05-09 14:01:47 +000012214
Hao Liu2cd34bb2015-06-26 02:45:36 +000012215/// \brief Lower an interleaved load into a vldN intrinsic.
12216///
12217/// E.g. Lower an interleaved load (Factor = 2):
12218/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
12219/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
12220/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
12221///
12222/// Into:
12223/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
12224/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
12225/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
12226bool ARMTargetLowering::lowerInterleavedLoad(
12227 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
12228 ArrayRef<unsigned> Indices, unsigned Factor) const {
12229 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12230 "Invalid interleave factor");
12231 assert(!Shuffles.empty() && "Empty shufflevector input");
12232 assert(Shuffles.size() == Indices.size() &&
12233 "Unmatched number of shufflevectors and indices");
12234
12235 VectorType *VecTy = Shuffles[0]->getType();
12236 Type *EltTy = VecTy->getVectorElementType();
12237
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012238 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012239 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
12240 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012241
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012242 // Skip if we do not have NEON and skip illegal vector types and vector types
12243 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
12244 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012245 return false;
12246
12247 // A pointer vector can not be the return type of the ldN intrinsics. Need to
12248 // load integer vectors first and then convert to pointer vectors.
12249 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012250 VecTy =
12251 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000012252
12253 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
12254 Intrinsic::arm_neon_vld3,
12255 Intrinsic::arm_neon_vld4};
12256
Hao Liu2cd34bb2015-06-26 02:45:36 +000012257 IRBuilder<> Builder(LI);
12258 SmallVector<Value *, 2> Ops;
12259
12260 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
12261 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
12262 Ops.push_back(Builder.getInt32(LI->getAlignment()));
12263
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012264 Type *Tys[] = { VecTy, Int8Ptr };
12265 Function *VldnFunc =
12266 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012267 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
12268
12269 // Replace uses of each shufflevector with the corresponding vector loaded
12270 // by ldN.
12271 for (unsigned i = 0; i < Shuffles.size(); i++) {
12272 ShuffleVectorInst *SV = Shuffles[i];
12273 unsigned Index = Indices[i];
12274
12275 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
12276
12277 // Convert the integer vector to pointer vector if the element is pointer.
12278 if (EltTy->isPointerTy())
12279 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
12280
12281 SV->replaceAllUsesWith(SubVec);
12282 }
12283
12284 return true;
12285}
12286
12287/// \brief Get a mask consisting of sequential integers starting from \p Start.
12288///
12289/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
12290static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
12291 unsigned NumElts) {
12292 SmallVector<Constant *, 16> Mask;
12293 for (unsigned i = 0; i < NumElts; i++)
12294 Mask.push_back(Builder.getInt32(Start + i));
12295
12296 return ConstantVector::get(Mask);
12297}
12298
12299/// \brief Lower an interleaved store into a vstN intrinsic.
12300///
12301/// E.g. Lower an interleaved store (Factor = 3):
12302/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
12303/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
12304/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
12305///
12306/// Into:
12307/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
12308/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
12309/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
12310/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
12311///
12312/// Note that the new shufflevectors will be removed and we'll only generate one
12313/// vst3 instruction in CodeGen.
12314bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
12315 ShuffleVectorInst *SVI,
12316 unsigned Factor) const {
12317 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
12318 "Invalid interleave factor");
12319
12320 VectorType *VecTy = SVI->getType();
12321 assert(VecTy->getVectorNumElements() % Factor == 0 &&
12322 "Invalid interleaved store");
12323
12324 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
12325 Type *EltTy = VecTy->getVectorElementType();
12326 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
12327
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012328 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012329 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
12330 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000012331
Jeroen Ketemaaebca092015-10-07 14:53:29 +000012332 // Skip if we do not have NEON and skip illegal vector types and vector types
12333 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
12334 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
12335 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000012336 return false;
12337
12338 Value *Op0 = SVI->getOperand(0);
12339 Value *Op1 = SVI->getOperand(1);
12340 IRBuilder<> Builder(SI);
12341
12342 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
12343 // vectors to integer vectors.
12344 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012345 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000012346
12347 // Convert to the corresponding integer vector.
12348 Type *IntVecTy =
12349 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
12350 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
12351 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
12352
12353 SubVecTy = VectorType::get(IntTy, NumSubElts);
12354 }
12355
Craig Topper26260942015-10-18 05:15:34 +000012356 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
12357 Intrinsic::arm_neon_vst3,
12358 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000012359 SmallVector<Value *, 6> Ops;
12360
12361 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
12362 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
12363
Jeroen Ketemaab99b592015-09-30 10:56:37 +000012364 Type *Tys[] = { Int8Ptr, SubVecTy };
12365 Function *VstNFunc = Intrinsic::getDeclaration(
12366 SI->getModule(), StoreInts[Factor - 2], Tys);
12367
Hao Liu2cd34bb2015-06-26 02:45:36 +000012368 // Split the shufflevector operands into sub vectors for the new vstN call.
12369 for (unsigned i = 0; i < Factor; i++)
12370 Ops.push_back(Builder.CreateShuffleVector(
12371 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
12372
12373 Ops.push_back(Builder.getInt32(SI->getAlignment()));
12374 Builder.CreateCall(VstNFunc, Ops);
12375 return true;
12376}
12377
Oliver Stannardc24f2172014-05-09 14:01:47 +000012378enum HABaseType {
12379 HA_UNKNOWN = 0,
12380 HA_FLOAT,
12381 HA_DOUBLE,
12382 HA_VECT64,
12383 HA_VECT128
12384};
12385
12386static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
12387 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000012388 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012389 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
12390 uint64_t SubMembers = 0;
12391 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
12392 return false;
12393 Members += SubMembers;
12394 }
Craig Toppere3dcce92015-08-01 22:20:21 +000012395 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012396 uint64_t SubMembers = 0;
12397 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
12398 return false;
12399 Members += SubMembers * AT->getNumElements();
12400 } else if (Ty->isFloatTy()) {
12401 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
12402 return false;
12403 Members = 1;
12404 Base = HA_FLOAT;
12405 } else if (Ty->isDoubleTy()) {
12406 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
12407 return false;
12408 Members = 1;
12409 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000012410 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000012411 Members = 1;
12412 switch (Base) {
12413 case HA_FLOAT:
12414 case HA_DOUBLE:
12415 return false;
12416 case HA_VECT64:
12417 return VT->getBitWidth() == 64;
12418 case HA_VECT128:
12419 return VT->getBitWidth() == 128;
12420 case HA_UNKNOWN:
12421 switch (VT->getBitWidth()) {
12422 case 64:
12423 Base = HA_VECT64;
12424 return true;
12425 case 128:
12426 Base = HA_VECT128;
12427 return true;
12428 default:
12429 return false;
12430 }
12431 }
12432 }
12433
12434 return (Members > 0 && Members <= 4);
12435}
12436
Tim Northovere95c5b32015-02-24 17:22:34 +000012437/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
12438/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
12439/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000012440bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
12441 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000012442 if (getEffectiveCallingConv(CallConv, isVarArg) !=
12443 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000012444 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000012445
12446 HABaseType Base = HA_UNKNOWN;
12447 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000012448 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
12449 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
12450
12451 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
12452 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000012453}
Joseph Tremouletf748c892015-11-07 01:11:31 +000012454
12455unsigned ARMTargetLowering::getExceptionPointerRegister(
12456 const Constant *PersonalityFn) const {
12457 // Platforms which do not use SjLj EH may return values in these registers
12458 // via the personality function.
12459 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
12460}
12461
12462unsigned ARMTargetLowering::getExceptionSelectorRegister(
12463 const Constant *PersonalityFn) const {
12464 // Platforms which do not use SjLj EH may return values in these registers
12465 // via the personality function.
12466 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
12467}
Manman Ren5e9e65e2016-01-12 00:47:18 +000012468
12469void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
12470 // Update IsSplitCSR in ARMFunctionInfo.
12471 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
12472 AFI->setIsSplitCSR(true);
12473}
12474
12475void ARMTargetLowering::insertCopiesSplitCSR(
12476 MachineBasicBlock *Entry,
12477 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
12478 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
12479 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
12480 if (!IStart)
12481 return;
12482
12483 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
12484 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000012485 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000012486 for (const MCPhysReg *I = IStart; *I; ++I) {
12487 const TargetRegisterClass *RC = nullptr;
12488 if (ARM::GPRRegClass.contains(*I))
12489 RC = &ARM::GPRRegClass;
12490 else if (ARM::DPRRegClass.contains(*I))
12491 RC = &ARM::DPRRegClass;
12492 else
12493 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
12494
12495 unsigned NewVR = MRI->createVirtualRegister(RC);
12496 // Create copy from CSR to a virtual register.
12497 // FIXME: this currently does not emit CFI pseudo-instructions, it works
12498 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
12499 // nounwind. If we want to generalize this later, we may need to emit
12500 // CFI pseudo-instructions.
12501 assert(Entry->getParent()->getFunction()->hasFnAttribute(
12502 Attribute::NoUnwind) &&
12503 "Function should be nounwind in insertCopiesSplitCSR!");
12504 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000012505 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012506 .addReg(*I);
12507
Manman Rene5f807f2016-01-15 20:24:11 +000012508 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000012509 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000012510 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
12511 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000012512 .addReg(NewVR);
12513 }
12514}