blob: d77b722e62abb242fd18630e561dfbf7a2b66510 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000026#include "llvm/ADT/StringSwitch.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000027#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000028#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000041#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
John Brawn0dbcd652015-03-18 12:01:59 +000044#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000045#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000047#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000049#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000053#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000054#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000055using namespace llvm;
56
Chandler Carruth84e68b22014-04-22 02:41:26 +000057#define DEBUG_TYPE "arm-isel"
58
Dale Johannesend679ff72010-06-03 21:09:53 +000059STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000060STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000061STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000062
Evan Chengf128bdc2010-06-16 07:35:02 +000063static cl::opt<bool>
64ARMInterworking("arm-interworking", cl::Hidden,
65 cl::desc("Enable / disable ARM interworking (for debugging only)"),
66 cl::init(true));
67
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000068namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000069 class ARMCCState : public CCState {
70 public:
71 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000072 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
73 ParmContext PC)
74 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000075 assert(((PC == Call) || (PC == Prologue)) &&
76 "ARMCCState users must specify whether their context is call"
77 "or prologue generation.");
78 CallOrPrologue = PC;
79 }
80 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000081}
Cameron Zwarich89019782011-06-10 20:59:24 +000082
Stuart Hastings45fe3c32011-04-20 16:47:52 +000083// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000084static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085 ARM::R0, ARM::R1, ARM::R2, ARM::R3
86};
87
Craig Topper4fa625f2012-08-12 03:16:37 +000088void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
89 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000090 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000091 setOperationAction(ISD::LOAD, VT, Promote);
92 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000093
Craig Topper4fa625f2012-08-12 03:16:37 +000094 setOperationAction(ISD::STORE, VT, Promote);
95 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000096 }
97
Craig Topper4fa625f2012-08-12 03:16:37 +000098 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +000099 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 setOperationAction(ISD::SETCC, VT, Custom);
101 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
102 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000103 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000104 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
105 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
107 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000108 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
110 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
112 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000113 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000114 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
115 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
116 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
117 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
118 setOperationAction(ISD::SELECT, VT, Expand);
119 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000120 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000122 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SHL, VT, Custom);
124 setOperationAction(ISD::SRA, VT, Custom);
125 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000126 }
127
128 // Promote all bit-wise operations.
129 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000130 setOperationAction(ISD::AND, VT, Promote);
131 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
132 setOperationAction(ISD::OR, VT, Promote);
133 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::XOR, VT, Promote);
135 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000136 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000137
138 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000139 setOperationAction(ISD::SDIV, VT, Expand);
140 setOperationAction(ISD::UDIV, VT, Expand);
141 setOperationAction(ISD::FDIV, VT, Expand);
142 setOperationAction(ISD::SREM, VT, Expand);
143 setOperationAction(ISD::UREM, VT, Expand);
144 setOperationAction(ISD::FREM, VT, Expand);
James Molloya6702e22015-07-17 17:10:55 +0000145
146 if (VT.isInteger()) {
147 setOperationAction(ISD::SABSDIFF, VT, Legal);
148 setOperationAction(ISD::UABSDIFF, VT, Legal);
149 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000158 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Eric Christopher1889fdc2015-01-29 00:19:39 +0000162ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
163 const ARMSubtarget &STI)
164 : TargetLowering(TM), Subtarget(&STI) {
165 RegInfo = Subtarget->getRegisterInfo();
166 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000167
Duncan Sandsf2641e12011-09-06 19:07:46 +0000168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
169
Tim Northoverd6a729b2014-01-06 14:28:05 +0000170 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000172 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000174 // Single-precision floating-point arithmetic.
175 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
176 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
177 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
178 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000179
Evan Chengc9f22fd12007-04-27 08:15:43 +0000180 // Double-precision floating-point arithmetic.
181 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
182 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
183 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
184 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000185
Evan Chengc9f22fd12007-04-27 08:15:43 +0000186 // Single-precision comparisons.
187 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
188 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
189 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
190 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
191 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
192 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
193 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
194 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000195
Evan Chengc9f22fd12007-04-27 08:15:43 +0000196 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000204
Evan Chengc9f22fd12007-04-27 08:15:43 +0000205 // Double-precision comparisons.
206 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
207 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
208 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
209 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
210 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
211 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
212 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
213 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000214
Evan Chengc9f22fd12007-04-27 08:15:43 +0000215 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000223
Evan Chengc9f22fd12007-04-27 08:15:43 +0000224 // Floating-point to integer conversions.
225 // i64 conversions are done via library routines even when generating VFP
226 // instructions, so use the same ones.
227 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
228 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
229 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
230 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000231
Evan Chengc9f22fd12007-04-27 08:15:43 +0000232 // Conversions between floating types.
233 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
234 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
235
236 // Integer to floating-point conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000239 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
240 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000241 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
242 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
243 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
244 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
245 }
Evan Cheng10043e22007-01-19 07:51:42 +0000246 }
247
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000248 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000249 setLibcallName(RTLIB::SHL_I128, nullptr);
250 setLibcallName(RTLIB::SRL_I128, nullptr);
251 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000252
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000253 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
254 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000255 static const struct {
256 const RTLIB::Libcall Op;
257 const char * const Name;
258 const CallingConv::ID CC;
259 const ISD::CondCode Cond;
260 } LibraryCalls[] = {
261 // Double-precision floating-point arithmetic helper functions
262 // RTABI chapter 4.1.2, Table 2
263 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
264 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
265 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
266 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000267
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000268 // Double-precision floating-point comparison helper functions
269 // RTABI chapter 4.1.2, Table 3
270 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
272 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000278
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000279 // Single-precision floating-point arithmetic helper functions
280 // RTABI chapter 4.1.2, Table 4
281 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
282 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
283 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
284 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000285
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000286 // Single-precision floating-point comparison helper functions
287 // RTABI chapter 4.1.2, Table 5
288 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
290 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000296
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000297 // Floating-point to integer conversions.
298 // RTABI chapter 4.1.2, Table 6
299 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000307
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000308 // Conversions between floating types.
309 // RTABI chapter 4.1.2, Table 7
310 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000311 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000312 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000313
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000314 // Integer to floating-point conversions.
315 // RTABI chapter 4.1.2, Table 8
316 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000324
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000325 // Long long helper functions
326 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000327 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
329 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
330 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000331
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000332 // Integer division functions
333 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000334 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000342
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000343 // Memory operations
344 // RTABI chapter 4.3.4
345 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
347 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
348 };
349
350 for (const auto &LC : LibraryCalls) {
351 setLibcallName(LC.Op, LC.Name);
352 setLibcallCallingConv(LC.Op, LC.CC);
353 if (LC.Cond != ISD::SETCC_INVALID)
354 setCmpLibcallCC(LC.Op, LC.Cond);
355 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000356 }
357
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000358 if (Subtarget->isTargetWindows()) {
359 static const struct {
360 const RTLIB::Libcall Op;
361 const char * const Name;
362 const CallingConv::ID CC;
363 } LibraryCalls[] = {
364 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
371 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
372 };
373
374 for (const auto &LC : LibraryCalls) {
375 setLibcallName(LC.Op, LC.Name);
376 setLibcallCallingConv(LC.Op, LC.CC);
377 }
378 }
379
Bob Wilsonbc158992011-10-07 16:59:21 +0000380 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000381 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000382 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
383 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
384 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
385 }
386
Oliver Stannard11790b22014-08-11 09:12:32 +0000387 // The half <-> float conversion functions are always soft-float, but are
388 // needed for some targets which use a hard-float calling convention by
389 // default.
390 if (Subtarget->isAAPCS_ABI()) {
391 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
394 } else {
395 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
396 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
397 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
398 }
399
David Goodwin22c2fba2009-07-08 23:10:31 +0000400 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000401 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000402 else
Craig Topperc7242e02012-04-20 07:30:17 +0000403 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000404 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000405 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000406 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000407 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000408 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000409
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000410 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000411 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000412 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000413 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
414 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
415 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
416 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000417
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000418 setOperationAction(ISD::MULHS, VT, Expand);
419 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
420 setOperationAction(ISD::MULHU, VT, Expand);
421 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000422
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000423 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000424 }
425
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000426 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000427 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000428
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000429 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
430 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
431
Bob Wilson2e076c42009-06-22 23:27:02 +0000432 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000433 addDRTypeForNEON(MVT::v2f32);
434 addDRTypeForNEON(MVT::v8i8);
435 addDRTypeForNEON(MVT::v4i16);
436 addDRTypeForNEON(MVT::v2i32);
437 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000438
Owen Anderson9f944592009-08-11 20:47:22 +0000439 addQRTypeForNEON(MVT::v4f32);
440 addQRTypeForNEON(MVT::v2f64);
441 addQRTypeForNEON(MVT::v16i8);
442 addQRTypeForNEON(MVT::v8i16);
443 addQRTypeForNEON(MVT::v4i32);
444 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000445
Bob Wilson194a2512009-09-15 23:55:57 +0000446 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
447 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000448 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
449 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000450 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
451 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
452 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000453 // FIXME: Code duplication: FDIV and FREM are expanded always, see
454 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000455 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
456 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000457 // FIXME: Create unittest.
458 // In another words, find a way when "copysign" appears in DAG with vector
459 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000460 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000461 // FIXME: Code duplication: SETCC has custom operation action, see
462 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000463 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000464 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000465 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
466 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
467 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
468 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
469 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
470 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
471 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
472 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
473 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
474 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
475 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
476 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000477 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000478 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
479 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
480 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
481 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
482 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000483 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000484
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000485 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
486 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
487 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
488 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
489 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
490 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
491 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
492 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
493 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
494 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000495 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
496 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
497 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
498 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000499 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000500
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000501 // Mark v2f32 intrinsics.
502 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
503 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
504 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
505 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
506 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
507 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
508 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
509 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
510 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
511 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
512 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
513 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
514 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
515 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
516 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
517
Bob Wilson6cc46572009-09-16 00:32:15 +0000518 // Neon does not support some operations on v1i64 and v2i64 types.
519 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000520 // Custom handling for some quad-vector types to detect VMULL.
521 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
522 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
523 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000524 // Custom handling for some vector types to avoid expensive expansions
525 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
526 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
527 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
528 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000529 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
530 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000531 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000532 // a destination type that is wider than the source, and nor does
533 // it have a FP_TO_[SU]INT instruction with a narrower destination than
534 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000535 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
536 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000537 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
538 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000539
Eli Friedmane6385e62012-11-15 22:44:27 +0000540 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000541 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000542
Evan Chengb4eae132012-12-04 22:41:50 +0000543 // NEON does not have single instruction CTPOP for vectors with element
544 // types wider than 8-bits. However, custom lowering can leverage the
545 // v8i8/v16i8 vcnt instruction.
546 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
547 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
548 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
549 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
550
Logan Chien0a43abc2015-07-13 15:37:30 +0000551 // NEON does not have single instruction CTTZ for vectors.
552 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
553 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
554 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
555 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
556
557 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
558 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
559 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
560 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
561
562 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
563 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
564 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
565 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
566
567 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
568 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
569 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
570 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
571
Jim Grosbach5f215872013-02-27 21:31:12 +0000572 // NEON only has FMA instructions as of VFP4.
573 if (!Subtarget->hasVFP4()) {
574 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
575 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
576 }
577
Bob Wilson06fce872011-02-07 17:43:21 +0000578 setTargetDAGCombine(ISD::INTRINSIC_VOID);
579 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000580 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
581 setTargetDAGCombine(ISD::SHL);
582 setTargetDAGCombine(ISD::SRL);
583 setTargetDAGCombine(ISD::SRA);
584 setTargetDAGCombine(ISD::SIGN_EXTEND);
585 setTargetDAGCombine(ISD::ZERO_EXTEND);
586 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000587 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000588 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000589 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000590 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
591 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000592 setTargetDAGCombine(ISD::FP_TO_SINT);
593 setTargetDAGCombine(ISD::FP_TO_UINT);
594 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000595 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000596
James Molloy547d4c02012-02-20 09:24:05 +0000597 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000598 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
599 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000600 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000601 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
602 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
603 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000604 }
James Molloy547d4c02012-02-20 09:24:05 +0000605 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000606 }
607
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000608 // ARM and Thumb2 support UMLAL/SMLAL.
609 if (!Subtarget->isThumb1Only())
610 setTargetDAGCombine(ISD::ADDC);
611
Oliver Stannard51b1d462014-08-21 12:50:31 +0000612 if (Subtarget->isFPOnlySP()) {
613 // When targetting a floating-point unit with only single-precision
614 // operations, f64 is legal for the few double-precision instructions which
615 // are present However, no double-precision operations other than moves,
616 // loads and stores are provided by the hardware.
617 setOperationAction(ISD::FADD, MVT::f64, Expand);
618 setOperationAction(ISD::FSUB, MVT::f64, Expand);
619 setOperationAction(ISD::FMUL, MVT::f64, Expand);
620 setOperationAction(ISD::FMA, MVT::f64, Expand);
621 setOperationAction(ISD::FDIV, MVT::f64, Expand);
622 setOperationAction(ISD::FREM, MVT::f64, Expand);
623 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
624 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
625 setOperationAction(ISD::FNEG, MVT::f64, Expand);
626 setOperationAction(ISD::FABS, MVT::f64, Expand);
627 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
628 setOperationAction(ISD::FSIN, MVT::f64, Expand);
629 setOperationAction(ISD::FCOS, MVT::f64, Expand);
630 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
631 setOperationAction(ISD::FPOW, MVT::f64, Expand);
632 setOperationAction(ISD::FLOG, MVT::f64, Expand);
633 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
634 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
635 setOperationAction(ISD::FEXP, MVT::f64, Expand);
636 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
637 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
638 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
639 setOperationAction(ISD::FRINT, MVT::f64, Expand);
640 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
641 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000642 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
643 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
644 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
645 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
646 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
647 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000648 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
649 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
650 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000651
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000652 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000653
Tim Northover4e80b582014-07-18 13:01:19 +0000654 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000655 for (MVT VT : MVT::fp_valuetypes()) {
656 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
657 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
658 }
Tim Northover4e80b582014-07-18 13:01:19 +0000659
660 // ... or truncating stores
661 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
662 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
663 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000664
Duncan Sands95d46ef2008-01-23 20:39:46 +0000665 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000666 for (MVT VT : MVT::integer_valuetypes())
667 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000668
Evan Cheng10043e22007-01-19 07:51:42 +0000669 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000670 if (!Subtarget->isThumb1Only()) {
671 for (unsigned im = (unsigned)ISD::PRE_INC;
672 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000673 setIndexedLoadAction(im, MVT::i1, Legal);
674 setIndexedLoadAction(im, MVT::i8, Legal);
675 setIndexedLoadAction(im, MVT::i16, Legal);
676 setIndexedLoadAction(im, MVT::i32, Legal);
677 setIndexedStoreAction(im, MVT::i1, Legal);
678 setIndexedStoreAction(im, MVT::i8, Legal);
679 setIndexedStoreAction(im, MVT::i16, Legal);
680 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000681 }
Evan Cheng10043e22007-01-19 07:51:42 +0000682 }
683
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000684 setOperationAction(ISD::SADDO, MVT::i32, Custom);
685 setOperationAction(ISD::UADDO, MVT::i32, Custom);
686 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
687 setOperationAction(ISD::USUBO, MVT::i32, Custom);
688
Evan Cheng10043e22007-01-19 07:51:42 +0000689 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000690 setOperationAction(ISD::MUL, MVT::i64, Expand);
691 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000692 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000693 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
694 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000695 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000696 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
697 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000698 setOperationAction(ISD::MULHS, MVT::i32, Expand);
699
Jim Grosbach5d994042009-10-31 19:38:01 +0000700 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000701 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000702 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000703 setOperationAction(ISD::SRL, MVT::i64, Custom);
704 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000705
Evan Chenge8916542011-08-30 01:34:54 +0000706 if (!Subtarget->isThumb1Only()) {
707 // FIXME: We should do this for Thumb1 as well.
708 setOperationAction(ISD::ADDC, MVT::i32, Custom);
709 setOperationAction(ISD::ADDE, MVT::i32, Custom);
710 setOperationAction(ISD::SUBC, MVT::i32, Custom);
711 setOperationAction(ISD::SUBE, MVT::i32, Custom);
712 }
713
Evan Cheng10043e22007-01-19 07:51:42 +0000714 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000715 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000716 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000717 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000718 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000719 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000720
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000721 // These just redirect to CTTZ and CTLZ on ARM.
722 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
723 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
724
Tim Northoverbc933082013-05-23 19:11:20 +0000725 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
726
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000727 // Only ARMv6 has BSWAP.
728 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000729 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000730
Bob Wilsone8a549c2012-09-29 21:43:49 +0000731 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
732 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
733 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000734 setOperationAction(ISD::SDIV, MVT::i32, Expand);
735 setOperationAction(ISD::UDIV, MVT::i32, Expand);
736 }
Renato Golin87610692013-07-16 09:32:17 +0000737
738 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000739 setOperationAction(ISD::SREM, MVT::i32, Expand);
740 setOperationAction(ISD::UREM, MVT::i32, Expand);
741 // Register based DivRem for AEABI (RTABI 4.2)
742 if (Subtarget->isTargetAEABI()) {
743 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
744 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
745 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
746 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
747 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
748 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
749 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
750 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
751
752 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
753 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
754 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
755 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
756 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
757 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
758 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
759 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
760
761 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
762 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
763 } else {
Renato Golin87610692013-07-16 09:32:17 +0000764 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
765 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
766 }
Bob Wilson7117a912009-03-20 22:42:55 +0000767
Owen Anderson9f944592009-08-11 20:47:22 +0000768 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
769 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
770 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
771 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000772 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000773
Evan Cheng74d92c12011-04-08 21:37:21 +0000774 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000775
Evan Cheng10043e22007-01-19 07:51:42 +0000776 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000777 setOperationAction(ISD::VASTART, MVT::Other, Custom);
778 setOperationAction(ISD::VAARG, MVT::Other, Expand);
779 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
780 setOperationAction(ISD::VAEND, MVT::Other, Expand);
781 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
782 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000783
Tim Northoverd6a729b2014-01-06 14:28:05 +0000784 if (!Subtarget->isTargetMachO()) {
785 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000786 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000787 setExceptionPointerRegister(ARM::R0);
788 setExceptionSelectorRegister(ARM::R1);
789 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000790
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000791 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
792 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
793 else
794 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
795
Evan Cheng6e809de2010-08-11 06:22:01 +0000796 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000797 // the default expansion. If we are targeting a single threaded system,
798 // then set them all for expand so we can lower them later into their
799 // non-atomic form.
800 if (TM.Options.ThreadModel == ThreadModel::Single)
801 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
802 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000803 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
804 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000805 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000806
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000807 // On v8, we have particularly efficient implementations of atomic fences
808 // if they can be combined with nearby atomic loads and stores.
809 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000810 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000811 setInsertFencesForAtomic(true);
812 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000813 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000814 // If there's anything we can use as a barrier, go through custom lowering
815 // for ATOMIC_FENCE.
816 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
817 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
818
Jim Grosbach6860bb72010-06-18 22:35:32 +0000819 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000820 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000821 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000822 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000823 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000824 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000825 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000826 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000827 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000828 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000829 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000830 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000831 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000832 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
833 // Unordered/Monotonic case.
834 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
835 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000836 }
Evan Cheng10043e22007-01-19 07:51:42 +0000837
Evan Cheng21acf9f2010-11-04 05:19:35 +0000838 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000839
Eli Friedman8cfa7712010-06-26 04:36:50 +0000840 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
841 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000842 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
843 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000844 }
Owen Anderson9f944592009-08-11 20:47:22 +0000845 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000846
Eric Christopher824f42f2015-05-12 01:26:05 +0000847 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000848 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000849 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000850 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000851 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000852 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
853 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000854
855 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000856 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +0000857 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
858 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
859 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
860 if (Subtarget->isTargetDarwin())
John McCall7d84ece2011-05-29 19:50:32 +0000861 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000862
Owen Anderson9f944592009-08-11 20:47:22 +0000863 setOperationAction(ISD::SETCC, MVT::i32, Expand);
864 setOperationAction(ISD::SETCC, MVT::f32, Expand);
865 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000866 setOperationAction(ISD::SELECT, MVT::i32, Custom);
867 setOperationAction(ISD::SELECT, MVT::f32, Custom);
868 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000869 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
870 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
871 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000872
Owen Anderson9f944592009-08-11 20:47:22 +0000873 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
874 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
875 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
876 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
877 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000878
Dan Gohman482732a2007-10-11 23:21:31 +0000879 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000880 setOperationAction(ISD::FSIN, MVT::f64, Expand);
881 setOperationAction(ISD::FSIN, MVT::f32, Expand);
882 setOperationAction(ISD::FCOS, MVT::f32, Expand);
883 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000884 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
885 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000886 setOperationAction(ISD::FREM, MVT::f64, Expand);
887 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000888 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000889 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000890 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
891 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000892 }
Owen Anderson9f944592009-08-11 20:47:22 +0000893 setOperationAction(ISD::FPOW, MVT::f64, Expand);
894 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000895
Evan Chengd0007f32012-04-10 21:40:28 +0000896 if (!Subtarget->hasVFP4()) {
897 setOperationAction(ISD::FMA, MVT::f64, Expand);
898 setOperationAction(ISD::FMA, MVT::f32, Expand);
899 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000900
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000901 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000902 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000903 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
904 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000905 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
906 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
907 }
908
909 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000910 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000911 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
912 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000913 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000914 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000915
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000916 // Combine sin / cos into one node or libcall if possible.
917 if (Subtarget->hasSinCos()) {
918 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
919 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000920 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000921 // For iOS, we don't want to the normal expansion of a libcall to
922 // sincos. We want to issue a libcall to __sincos_stret.
923 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
924 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
925 }
926 }
Evan Cheng10043e22007-01-19 07:51:42 +0000927
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000928 // FP-ARMv8 implements a lot of rounding-like FP operations.
929 if (Subtarget->hasFPARMv8()) {
930 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
931 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
932 setOperationAction(ISD::FROUND, MVT::f32, Legal);
933 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
934 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
935 setOperationAction(ISD::FRINT, MVT::f32, Legal);
936 if (!Subtarget->isFPOnlySP()) {
937 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
938 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
939 setOperationAction(ISD::FROUND, MVT::f64, Legal);
940 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
941 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
942 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000943 }
944 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000945 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000946 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000947 setTargetDAGCombine(ISD::ADD);
948 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000949 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000950 setTargetDAGCombine(ISD::AND);
951 setTargetDAGCombine(ISD::OR);
952 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000953
Evan Chengf258a152012-02-23 02:58:19 +0000954 if (Subtarget->hasV6Ops())
955 setTargetDAGCombine(ISD::SRL);
956
Evan Cheng10043e22007-01-19 07:51:42 +0000957 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000958
Eric Christopher824f42f2015-05-12 01:26:05 +0000959 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000960 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000961 setSchedulingPreference(Sched::RegPressure);
962 else
963 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000964
Evan Cheng3ae2b792011-01-06 06:52:41 +0000965 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000966 MaxStoresPerMemset = 8;
967 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
968 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
969 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
970 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
971 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000972
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000973 // On ARM arguments smaller than 4 bytes are extended, so all arguments
974 // are at least 4 bytes aligned.
975 setMinStackArgumentAlignment(4);
976
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000977 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000978 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000979
Eli Friedman2518f832011-05-06 20:34:06 +0000980 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000981}
982
Eric Christopher824f42f2015-05-12 01:26:05 +0000983bool ARMTargetLowering::useSoftFloat() const {
984 return Subtarget->useSoftFloat();
985}
986
Andrew Trick43f25632011-01-19 02:35:27 +0000987// FIXME: It might make sense to define the representative register class as the
988// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
989// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
990// SPR's representative would be DPR_VFP2. This should work well if register
991// pressure tracking were modified such that a register use would increment the
992// pressure of the register class's representative and all of it's super
993// classes' representatives transitively. We have not implemented this because
994// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000995// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000996// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000997std::pair<const TargetRegisterClass *, uint8_t>
998ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
999 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001000 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001001 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001002 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001003 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001004 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001005 // Use DPR as representative register class for all floating point
1006 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1007 // the cost is 1 for both f32 and f64.
1008 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001009 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001010 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001011 // When NEON is used for SP, only half of the register file is available
1012 // because operations that define both SP and DP results will be constrained
1013 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1014 // coalescing by double-counting the SP regs. See the FIXME above.
1015 if (Subtarget->useNEONForSinglePrecisionFP())
1016 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001017 break;
1018 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1019 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001020 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001021 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001022 break;
1023 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001024 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001025 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001026 break;
1027 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001028 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001029 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001030 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001031 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001032 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001033}
1034
Evan Cheng10043e22007-01-19 07:51:42 +00001035const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001036 switch ((ARMISD::NodeType)Opcode) {
1037 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001038 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001039 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001040 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001041 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001042 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001043 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001044 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1045 case ARMISD::tCALL: return "ARMISD::tCALL";
1046 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1047 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001048 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001049 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001050 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001051 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1052 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001053 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001054 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001055 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1056 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001057 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001058 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001059
Evan Cheng10043e22007-01-19 07:51:42 +00001060 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001061
Jim Grosbach8546ec92010-01-18 19:58:49 +00001062 case ARMISD::RBIT: return "ARMISD::RBIT";
1063
Evan Cheng10043e22007-01-19 07:51:42 +00001064 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1065 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1066 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001067
Evan Chenge8916542011-08-30 01:34:54 +00001068 case ARMISD::ADDC: return "ARMISD::ADDC";
1069 case ARMISD::ADDE: return "ARMISD::ADDE";
1070 case ARMISD::SUBC: return "ARMISD::SUBC";
1071 case ARMISD::SUBE: return "ARMISD::SUBE";
1072
Bob Wilson22806742010-09-22 22:09:21 +00001073 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1074 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001075
Evan Chengec6d7c92009-10-28 06:55:03 +00001076 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001077 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1078 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001079
Dale Johannesend679ff72010-06-03 21:09:53 +00001080 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001081
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001082 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001083
Evan Chengb972e562009-08-07 00:34:42 +00001084 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1085
Bob Wilson7ed59712010-10-30 00:54:37 +00001086 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001087
Evan Cheng8740ee32010-11-03 06:34:55 +00001088 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1089
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001090 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1091
Bob Wilson2e076c42009-06-22 23:27:02 +00001092 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001093 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001094 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001095 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1096 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001097 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1098 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001099 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1100 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001101 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1102 case ARMISD::VTST: return "ARMISD::VTST";
1103
1104 case ARMISD::VSHL: return "ARMISD::VSHL";
1105 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1106 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001107 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1108 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1109 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1110 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1111 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1112 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1113 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1114 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1115 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1116 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1117 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1118 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001119 case ARMISD::VSLI: return "ARMISD::VSLI";
1120 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001121 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1122 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001123 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001124 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001125 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001126 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001127 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001128 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001129 case ARMISD::VREV64: return "ARMISD::VREV64";
1130 case ARMISD::VREV32: return "ARMISD::VREV32";
1131 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001132 case ARMISD::VZIP: return "ARMISD::VZIP";
1133 case ARMISD::VUZP: return "ARMISD::VUZP";
1134 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001135 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1136 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001137 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1138 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001139 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1140 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001141 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001142 case ARMISD::FMAX: return "ARMISD::FMAX";
1143 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001144 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1145 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001146 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001147 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1148 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001149 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001150 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1151 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1152 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001153 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1154 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1155 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1156 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1157 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1158 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1159 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1160 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1161 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1162 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1163 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1164 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1165 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1166 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1167 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1168 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1169 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001170 }
Matthias Braund04893f2015-05-07 21:33:59 +00001171 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001172}
1173
Mehdi Amini44ede332015-07-09 02:09:04 +00001174EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1175 EVT VT) const {
1176 if (!VT.isVector())
1177 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001178 return VT.changeVectorElementTypeToInteger();
1179}
1180
Evan Cheng4cad68e2010-05-15 02:18:07 +00001181/// getRegClassFor - Return the register class that should be used for the
1182/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001183const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001184 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1185 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1186 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001187 if (Subtarget->hasNEON()) {
1188 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001189 return &ARM::QQPRRegClass;
1190 if (VT == MVT::v8i64)
1191 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001192 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001193 return TargetLowering::getRegClassFor(VT);
1194}
1195
John Brawn0dbcd652015-03-18 12:01:59 +00001196// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1197// source/dest is aligned and the copy size is large enough. We therefore want
1198// to align such objects passed to memory intrinsics.
1199bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1200 unsigned &PrefAlign) const {
1201 if (!isa<MemIntrinsic>(CI))
1202 return false;
1203 MinSize = 8;
1204 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1205 // cycle faster than 4-byte aligned LDM.
1206 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1207 return true;
1208}
1209
Eric Christopher84bdfd82010-07-21 22:26:11 +00001210// Create a fast isel object.
1211FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001212ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1213 const TargetLibraryInfo *libInfo) const {
1214 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001215}
1216
Evan Cheng4401f882010-05-20 23:26:43 +00001217Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001218 unsigned NumVals = N->getNumValues();
1219 if (!NumVals)
1220 return Sched::RegPressure;
1221
1222 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001223 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001224 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001225 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001226 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001227 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001228 }
Evan Chengbf914992010-05-28 23:25:23 +00001229
1230 if (!N->isMachineOpcode())
1231 return Sched::RegPressure;
1232
1233 // Load are scheduled for latency even if there instruction itinerary
1234 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001235 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001236 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001237
Evan Cheng6cc775f2011-06-28 19:10:37 +00001238 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001239 return Sched::RegPressure;
1240 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001241 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001242 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001243
Evan Cheng4401f882010-05-20 23:26:43 +00001244 return Sched::RegPressure;
1245}
1246
Evan Cheng10043e22007-01-19 07:51:42 +00001247//===----------------------------------------------------------------------===//
1248// Lowering Code
1249//===----------------------------------------------------------------------===//
1250
Evan Cheng10043e22007-01-19 07:51:42 +00001251/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1252static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1253 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001254 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001255 case ISD::SETNE: return ARMCC::NE;
1256 case ISD::SETEQ: return ARMCC::EQ;
1257 case ISD::SETGT: return ARMCC::GT;
1258 case ISD::SETGE: return ARMCC::GE;
1259 case ISD::SETLT: return ARMCC::LT;
1260 case ISD::SETLE: return ARMCC::LE;
1261 case ISD::SETUGT: return ARMCC::HI;
1262 case ISD::SETUGE: return ARMCC::HS;
1263 case ISD::SETULT: return ARMCC::LO;
1264 case ISD::SETULE: return ARMCC::LS;
1265 }
1266}
1267
Bob Wilsona2e83332009-09-09 23:14:54 +00001268/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1269static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001270 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001271 CondCode2 = ARMCC::AL;
1272 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001273 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001274 case ISD::SETEQ:
1275 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1276 case ISD::SETGT:
1277 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1278 case ISD::SETGE:
1279 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1280 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001281 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001282 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1283 case ISD::SETO: CondCode = ARMCC::VC; break;
1284 case ISD::SETUO: CondCode = ARMCC::VS; break;
1285 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1286 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1287 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1288 case ISD::SETLT:
1289 case ISD::SETULT: CondCode = ARMCC::LT; break;
1290 case ISD::SETLE:
1291 case ISD::SETULE: CondCode = ARMCC::LE; break;
1292 case ISD::SETNE:
1293 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1294 }
Evan Cheng10043e22007-01-19 07:51:42 +00001295}
1296
Bob Wilsona4c22902009-04-17 19:07:39 +00001297//===----------------------------------------------------------------------===//
1298// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001299//===----------------------------------------------------------------------===//
1300
1301#include "ARMGenCallingConv.inc"
1302
Oliver Stannardc24f2172014-05-09 14:01:47 +00001303/// getEffectiveCallingConv - Get the effective calling convention, taking into
1304/// account presence of floating point hardware and calling convention
1305/// limitations, such as support for variadic functions.
1306CallingConv::ID
1307ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1308 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001309 switch (CC) {
1310 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001311 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001312 case CallingConv::ARM_AAPCS:
1313 case CallingConv::ARM_APCS:
1314 case CallingConv::GHC:
1315 return CC;
1316 case CallingConv::ARM_AAPCS_VFP:
1317 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1318 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001319 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001320 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001321 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001322 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1323 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001324 return CallingConv::ARM_AAPCS_VFP;
1325 else
1326 return CallingConv::ARM_AAPCS;
1327 case CallingConv::Fast:
1328 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001329 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001330 return CallingConv::Fast;
1331 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001332 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001333 return CallingConv::ARM_AAPCS_VFP;
1334 else
1335 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001336 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001337}
1338
1339/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1340/// CallingConvention.
1341CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1342 bool Return,
1343 bool isVarArg) const {
1344 switch (getEffectiveCallingConv(CC, isVarArg)) {
1345 default:
1346 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001347 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001348 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001349 case CallingConv::ARM_AAPCS:
1350 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1351 case CallingConv::ARM_AAPCS_VFP:
1352 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1353 case CallingConv::Fast:
1354 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001355 case CallingConv::GHC:
1356 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001357 }
1358}
1359
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001360/// LowerCallResult - Lower the result values of a call into the
1361/// appropriate copies out of appropriate physical registers.
1362SDValue
1363ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001364 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001365 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001366 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001367 SmallVectorImpl<SDValue> &InVals,
1368 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001369
Bob Wilsona4c22902009-04-17 19:07:39 +00001370 // Assign locations to each value returned by this call.
1371 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001372 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1373 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001374 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001375 CCAssignFnForNode(CallConv, /* Return*/ true,
1376 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001377
1378 // Copy all of the result registers out of their specified physreg.
1379 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1380 CCValAssign VA = RVLocs[i];
1381
Stephen Linb8bd2322013-04-20 05:14:40 +00001382 // Pass 'this' value directly from the argument to return value, to avoid
1383 // reg unit interference
1384 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001385 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1386 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001387 InVals.push_back(ThisVal);
1388 continue;
1389 }
1390
Bob Wilson0041bd32009-04-25 00:33:20 +00001391 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001392 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001393 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001394 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001395 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001396 Chain = Lo.getValue(1);
1397 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001398 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001399 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001400 InFlag);
1401 Chain = Hi.getValue(1);
1402 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001403 if (!Subtarget->isLittle())
1404 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001405 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001406
Owen Anderson9f944592009-08-11 20:47:22 +00001407 if (VA.getLocVT() == MVT::v2f64) {
1408 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1409 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001410 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001411
1412 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001413 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001414 Chain = Lo.getValue(1);
1415 InFlag = Lo.getValue(2);
1416 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001417 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001418 Chain = Hi.getValue(1);
1419 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001420 if (!Subtarget->isLittle())
1421 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001422 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001423 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001424 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001425 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001426 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001427 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1428 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001429 Chain = Val.getValue(1);
1430 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001431 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001432
1433 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001434 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001435 case CCValAssign::Full: break;
1436 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001437 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001438 break;
1439 }
1440
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001441 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001442 }
1443
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001444 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001445}
1446
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001447/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001448SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001449ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1450 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001451 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001452 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001453 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001454 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001455 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001456 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1457 StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001458 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001459 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001460 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001461}
1462
Andrew Trickef9de2a2013-05-25 02:42:55 +00001463void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001464 SDValue Chain, SDValue &Arg,
1465 RegsToPassVector &RegsToPass,
1466 CCValAssign &VA, CCValAssign &NextVA,
1467 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001468 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001469 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001470
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001471 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001472 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001473 unsigned id = Subtarget->isLittle() ? 0 : 1;
1474 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001475
1476 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001477 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001478 else {
1479 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001480 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001481 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1482 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001483
Christian Pirkerb5728192014-05-08 14:06:24 +00001484 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001485 dl, DAG, NextVA,
1486 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001487 }
1488}
1489
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001490/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001491/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1492/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001493SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001494ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001495 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001496 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001497 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001498 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1499 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1500 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001501 SDValue Chain = CLI.Chain;
1502 SDValue Callee = CLI.Callee;
1503 bool &isTailCall = CLI.IsTailCall;
1504 CallingConv::ID CallConv = CLI.CallConv;
1505 bool doesNotRet = CLI.DoesNotReturn;
1506 bool isVarArg = CLI.IsVarArg;
1507
Dale Johannesend679ff72010-06-03 21:09:53 +00001508 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001509 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1510 bool isThisReturn = false;
1511 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001512 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001513
Bob Wilson8decdc42011-10-07 17:17:49 +00001514 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001515 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001516 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001517
Dale Johannesend679ff72010-06-03 21:09:53 +00001518 if (isTailCall) {
1519 // Check if it's really possible to do a tail call.
1520 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001521 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001522 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001523 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1524 report_fatal_error("failed to perform tail call elimination on a call "
1525 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001526 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1527 // detected sibcalls.
1528 if (isTailCall) {
1529 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001530 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001531 }
1532 }
Evan Cheng10043e22007-01-19 07:51:42 +00001533
Bob Wilsona4c22902009-04-17 19:07:39 +00001534 // Analyze operands of the call, assigning locations to each operand.
1535 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001536 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1537 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001538 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001539 CCAssignFnForNode(CallConv, /* Return*/ false,
1540 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001541
Bob Wilsona4c22902009-04-17 19:07:39 +00001542 // Get a count of how many bytes are to be pushed on the stack.
1543 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001544
Dale Johannesend679ff72010-06-03 21:09:53 +00001545 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001546 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001547 NumBytes = 0;
1548
Evan Cheng10043e22007-01-19 07:51:42 +00001549 // Adjust the stack pointer for the new arguments...
1550 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001551 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001552 Chain = DAG.getCALLSEQ_START(Chain,
1553 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001554
Mehdi Amini44ede332015-07-09 02:09:04 +00001555 SDValue StackPtr =
1556 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001557
Bob Wilson2e076c42009-06-22 23:27:02 +00001558 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001559 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001560
Bob Wilsona4c22902009-04-17 19:07:39 +00001561 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001562 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001563 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1564 i != e;
1565 ++i, ++realArgIdx) {
1566 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001567 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001568 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001569 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001570
Bob Wilsona4c22902009-04-17 19:07:39 +00001571 // Promote the value if needed.
1572 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001573 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001574 case CCValAssign::Full: break;
1575 case CCValAssign::SExt:
1576 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1577 break;
1578 case CCValAssign::ZExt:
1579 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1580 break;
1581 case CCValAssign::AExt:
1582 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1583 break;
1584 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001585 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001586 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001587 }
1588
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001589 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001590 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001591 if (VA.getLocVT() == MVT::v2f64) {
1592 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001593 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001594 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001595 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001596
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001597 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001598 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1599
1600 VA = ArgLocs[++i]; // skip ahead to next loc
1601 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001602 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001603 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1604 } else {
1605 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001606
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001607 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1608 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001609 }
1610 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001611 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001612 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001613 }
1614 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001615 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1616 assert(VA.getLocVT() == MVT::i32 &&
1617 "unexpected calling convention register assignment");
1618 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001619 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001620 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001621 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001622 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001623 } else if (isByVal) {
1624 assert(VA.isMemLoc());
1625 unsigned offset = 0;
1626
1627 // True if this byval aggregate will be split between registers
1628 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001629 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001630 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001631
1632 if (CurByValIdx < ByValArgsCount) {
1633
1634 unsigned RegBegin, RegEnd;
1635 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1636
Mehdi Amini44ede332015-07-09 02:09:04 +00001637 EVT PtrVT =
1638 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001639 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001640 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001641 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001642 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1643 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1644 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001645 false, false, false,
1646 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001647 MemOpChains.push_back(Load.getValue(1));
1648 RegsToPass.push_back(std::make_pair(j, Load));
1649 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001650
1651 // If parameter size outsides register area, "offset" value
1652 // helps us to calculate stack slot for remained part properly.
1653 offset = RegEnd - RegBegin;
1654
1655 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001656 }
1657
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001658 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001659 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001660 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001661 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001662 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001663 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001664 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001665 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001666 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001667 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1668 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001669
Manman Ren9f911162012-06-01 02:44:42 +00001670 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001671 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001672 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001673 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001674 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001675 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001676 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001677
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001678 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1679 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001680 }
Evan Cheng10043e22007-01-19 07:51:42 +00001681 }
1682
1683 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001684 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001685
1686 // Build a sequence of copy-to-reg nodes chained together with token chain
1687 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001688 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001689 // Tail call byval lowering might overwrite argument registers so in case of
1690 // tail call optimization the copies to registers are lowered later.
1691 if (!isTailCall)
1692 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1693 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1694 RegsToPass[i].second, InFlag);
1695 InFlag = Chain.getValue(1);
1696 }
Evan Cheng10043e22007-01-19 07:51:42 +00001697
Dale Johannesend679ff72010-06-03 21:09:53 +00001698 // For tail calls lower the arguments to the 'real' stack slot.
1699 if (isTailCall) {
1700 // Force all the incoming stack arguments to be loaded from the stack
1701 // before any new outgoing arguments are stored to the stack, because the
1702 // outgoing stack slots may alias the incoming argument stack slots, and
1703 // the alias isn't otherwise explicit. This is slightly more conservative
1704 // than necessary, because it means that each store effectively depends
1705 // on every argument instead of just those arguments it would clobber.
1706
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001707 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001708 InFlag = SDValue();
1709 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1710 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1711 RegsToPass[i].second, InFlag);
1712 InFlag = Chain.getValue(1);
1713 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001714 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001715 }
1716
Bill Wendling24c79f22008-09-16 21:48:12 +00001717 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1718 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1719 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001720 bool isDirect = false;
1721 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001722 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001723 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001724 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001725
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001726 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001727 assert((Subtarget->isTargetWindows() ||
1728 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1729 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001730 // Handle a global address or an external symbol. If it's not one of
1731 // those, the target's already in a register, so we don't need to do
1732 // anything extra.
1733 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001734 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001735 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001736 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001737 ARMConstantPoolValue *CPV =
1738 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1739
Jim Grosbach32bb3622010-04-14 22:28:31 +00001740 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001741 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001742 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001743 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1744 MachinePointerInfo::getConstantPool(), false, false,
1745 false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001746 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1747 const char *Sym = S->getSymbol();
1748
1749 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001750 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001751 ARMConstantPoolValue *CPV =
1752 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1753 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001754 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001755 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001756 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001757 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1758 MachinePointerInfo::getConstantPool(), false, false,
1759 false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001760 }
1761 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001762 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001763 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001764 bool isDef = GV->isStrongDefinitionForLinker();
1765 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001766 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001767 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001768 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001769 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001770 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001771 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001772 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001773 Callee = DAG.getNode(
1774 ARMISD::WrapperPIC, dl, PtrVt,
1775 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1776 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Tim Northoverd4d294d2014-08-06 11:13:06 +00001777 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001778 } else if (Subtarget->isTargetCOFF()) {
1779 assert(Subtarget->isTargetWindows() &&
1780 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001781 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1782 ? ARMII::MO_DLLIMPORT
1783 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001784 Callee =
1785 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001786 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001787 Callee =
1788 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1789 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
1790 MachinePointerInfo::getGOT(), false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001791 } else {
1792 // On ELF targets for PIC code, direct calls should go through the PLT
1793 unsigned OpFlags = 0;
1794 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001795 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001796 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001797 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001798 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001799 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001800 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001801 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001802 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001803 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001804 // tBX takes a register source operand.
1805 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001806 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001807 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001808 ARMConstantPoolValue *CPV =
1809 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1810 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001811 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001812 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001813 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1814 MachinePointerInfo::getConstantPool(), false, false,
1815 false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001816 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001817 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001818 } else {
1819 unsigned OpFlags = 0;
1820 // On ELF targets for PIC code, direct calls should go through the PLT
1821 if (Subtarget->isTargetELF() &&
1822 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1823 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001824 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001825 }
Evan Cheng10043e22007-01-19 07:51:42 +00001826 }
1827
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001828 // FIXME: handle tail calls differently.
1829 unsigned CallOpc;
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001830 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001831 if (Subtarget->isThumb()) {
1832 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001833 CallOpc = ARMISD::CALL_NOLINK;
1834 else
1835 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1836 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001837 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001838 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001839 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001840 // Emit regular call when code size is the priority
1841 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001842 // "mov lr, pc; b _foo" to avoid confusing the RSP
1843 CallOpc = ARMISD::CALL_NOLINK;
1844 else
1845 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001846 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001847
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001848 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001849 Ops.push_back(Chain);
1850 Ops.push_back(Callee);
1851
1852 // Add argument registers to the end of the list so that they are known live
1853 // into the call.
1854 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1855 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1856 RegsToPass[i].second.getValueType()));
1857
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001858 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001859 if (!isTailCall) {
1860 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001861 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001862 if (isThisReturn) {
1863 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001864 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001865 if (!Mask) {
1866 // Set isThisReturn to false if the calling convention is not one that
1867 // allows 'returned' to be modeled in this way, so LowerCallResult does
1868 // not try to pass 'this' straight through
1869 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001870 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001871 }
1872 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001873 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001874
Matthias Braunc22630e2013-10-04 16:52:54 +00001875 assert(Mask && "Missing call preserved mask for calling convention");
1876 Ops.push_back(DAG.getRegisterMask(Mask));
1877 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001878
Gabor Greiff304a7a2008-08-28 21:40:38 +00001879 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001880 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001881
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001882 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001883 if (isTailCall) {
1884 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001885 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001886 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001887
Duncan Sands739a0542008-07-02 17:40:58 +00001888 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001889 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001890 InFlag = Chain.getValue(1);
1891
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001892 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1893 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001894 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001895 InFlag = Chain.getValue(1);
1896
Bob Wilsona4c22902009-04-17 19:07:39 +00001897 // Handle result values, copying them out of physregs into vregs that we
1898 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001899 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001900 InVals, isThisReturn,
1901 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001902}
1903
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001904/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001905/// on the stack. Remember the next parameter register to allocate,
1906/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001907/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001908void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1909 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001910 assert((State->getCallOrPrologue() == Prologue ||
1911 State->getCallOrPrologue() == Call) &&
1912 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001913
Tim Northover8cda34f2015-03-11 18:54:22 +00001914 // Byval (as with any stack) slots are always at least 4 byte aligned.
1915 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001916
Tim Northover8cda34f2015-03-11 18:54:22 +00001917 unsigned Reg = State->AllocateReg(GPRArgRegs);
1918 if (!Reg)
1919 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001920
Tim Northover8cda34f2015-03-11 18:54:22 +00001921 unsigned AlignInRegs = Align / 4;
1922 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1923 for (unsigned i = 0; i < Waste; ++i)
1924 Reg = State->AllocateReg(GPRArgRegs);
1925
1926 if (!Reg)
1927 return;
1928
1929 unsigned Excess = 4 * (ARM::R4 - Reg);
1930
1931 // Special case when NSAA != SP and parameter size greater than size of
1932 // all remained GPR regs. In that case we can't split parameter, we must
1933 // send it to stack. We also must set NCRN to R4, so waste all
1934 // remained registers.
1935 const unsigned NSAAOffset = State->getNextStackOffset();
1936 if (NSAAOffset != 0 && Size > Excess) {
1937 while (State->AllocateReg(GPRArgRegs))
1938 ;
1939 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001940 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001941
1942 // First register for byval parameter is the first register that wasn't
1943 // allocated before this method call, so it would be "reg".
1944 // If parameter is small enough to be saved in range [reg, r4), then
1945 // the end (first after last) register would be reg + param-size-in-regs,
1946 // else parameter would be splitted between registers and stack,
1947 // end register would be r4 in this case.
1948 unsigned ByValRegBegin = Reg;
1949 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1950 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1951 // Note, first register is allocated in the beginning of function already,
1952 // allocate remained amount of registers we need.
1953 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1954 State->AllocateReg(GPRArgRegs);
1955 // A byval parameter that is split between registers and memory needs its
1956 // size truncated here.
1957 // In the case where the entire structure fits in registers, we set the
1958 // size in memory to zero.
1959 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001960}
1961
Dale Johannesend679ff72010-06-03 21:09:53 +00001962/// MatchingStackOffset - Return true if the given stack call argument is
1963/// already available in the same position (relatively) of the caller's
1964/// incoming argument stack.
1965static
1966bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1967 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001968 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001969 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1970 int FI = INT_MAX;
1971 if (Arg.getOpcode() == ISD::CopyFromReg) {
1972 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001973 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001974 return false;
1975 MachineInstr *Def = MRI->getVRegDef(VR);
1976 if (!Def)
1977 return false;
1978 if (!Flags.isByVal()) {
1979 if (!TII->isLoadFromStackSlot(Def, FI))
1980 return false;
1981 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001982 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001983 }
1984 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1985 if (Flags.isByVal())
1986 // ByVal argument is passed in as a pointer but it's now being
1987 // dereferenced. e.g.
1988 // define @foo(%struct.X* %A) {
1989 // tail call @bar(%struct.X* byval %A)
1990 // }
1991 return false;
1992 SDValue Ptr = Ld->getBasePtr();
1993 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1994 if (!FINode)
1995 return false;
1996 FI = FINode->getIndex();
1997 } else
1998 return false;
1999
2000 assert(FI != INT_MAX);
2001 if (!MFI->isFixedObjectIndex(FI))
2002 return false;
2003 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
2004}
2005
2006/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2007/// for tail call optimization. Targets which want to do tail call
2008/// optimization should implement this function.
2009bool
2010ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2011 CallingConv::ID CalleeCC,
2012 bool isVarArg,
2013 bool isCalleeStructRet,
2014 bool isCallerStructRet,
2015 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002016 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002017 const SmallVectorImpl<ISD::InputArg> &Ins,
2018 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002019 const Function *CallerF = DAG.getMachineFunction().getFunction();
2020 CallingConv::ID CallerCC = CallerF->getCallingConv();
2021 bool CCMatch = CallerCC == CalleeCC;
2022
2023 // Look for obvious safe cases to perform tail call optimization that do not
2024 // require ABI changes. This is what gcc calls sibcall.
2025
Jim Grosbache3864cc2010-06-16 23:45:49 +00002026 // Do not sibcall optimize vararg calls unless the call site is not passing
2027 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002028 if (isVarArg && !Outs.empty())
2029 return false;
2030
Tim Northoverd8407452013-10-01 14:33:28 +00002031 // Exception-handling functions need a special set of instructions to indicate
2032 // a return to the hardware. Tail-calling another function would probably
2033 // break this.
2034 if (CallerF->hasFnAttribute("interrupt"))
2035 return false;
2036
Dale Johannesend679ff72010-06-03 21:09:53 +00002037 // Also avoid sibcall optimization if either caller or callee uses struct
2038 // return semantics.
2039 if (isCalleeStructRet || isCallerStructRet)
2040 return false;
2041
Eric Christopherae326492015-03-12 22:48:50 +00002042 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002043 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2044 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2045 // support in the assembler and linker to be used. This would need to be
2046 // fixed to fully support tail calls in Thumb1.
2047 //
Dale Johannesene2289282010-07-08 01:18:23 +00002048 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2049 // LR. This means if we need to reload LR, it takes an extra instructions,
2050 // which outweighs the value of the tail call; but here we don't know yet
2051 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002052 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002053 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002054
2055 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2056 // but we need to make sure there are enough registers; the only valid
2057 // registers are the 4 used for parameters. We don't currently do this
2058 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002059 if (Subtarget->isThumb1Only())
2060 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002061
Oliver Stannard12993dd2014-08-18 12:42:15 +00002062 // Externally-defined functions with weak linkage should not be
2063 // tail-called on ARM when the OS does not support dynamic
2064 // pre-emption of symbols, as the AAELF spec requires normal calls
2065 // to undefined weak functions to be replaced with a NOP or jump to the
2066 // next instruction. The behaviour of branch instructions in this
2067 // situation (as used for tail calls) is implementation-defined, so we
2068 // cannot rely on the linker replacing the tail call with a return.
2069 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2070 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002071 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002072 if (GV->hasExternalWeakLinkage() &&
2073 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002074 return false;
2075 }
2076
Dale Johannesend679ff72010-06-03 21:09:53 +00002077 // If the calling conventions do not match, then we'd better make sure the
2078 // results are returned in the same way as what the caller expects.
2079 if (!CCMatch) {
2080 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002081 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2082 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002083 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2084
2085 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002086 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2087 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002088 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2089
2090 if (RVLocs1.size() != RVLocs2.size())
2091 return false;
2092 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2093 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2094 return false;
2095 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2096 return false;
2097 if (RVLocs1[i].isRegLoc()) {
2098 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2099 return false;
2100 } else {
2101 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2102 return false;
2103 }
2104 }
2105 }
2106
Manman Ren7e48b252012-10-12 23:39:43 +00002107 // If Caller's vararg or byval argument has been split between registers and
2108 // stack, do not perform tail call, since part of the argument is in caller's
2109 // local frame.
2110 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2111 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002112 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002113 return false;
2114
Dale Johannesend679ff72010-06-03 21:09:53 +00002115 // If the callee takes no arguments then go on to check the results of the
2116 // call.
2117 if (!Outs.empty()) {
2118 // Check if stack adjustment is needed. For now, do not do this if any
2119 // argument is passed on the stack.
2120 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002121 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2122 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002123 CCInfo.AnalyzeCallOperands(Outs,
2124 CCAssignFnForNode(CalleeCC, false, isVarArg));
2125 if (CCInfo.getNextStackOffset()) {
2126 MachineFunction &MF = DAG.getMachineFunction();
2127
2128 // Check if the arguments are already laid out in the right way as
2129 // the caller's fixed stack objects.
2130 MachineFrameInfo *MFI = MF.getFrameInfo();
2131 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002132 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002133 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2134 i != e;
2135 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002136 CCValAssign &VA = ArgLocs[i];
2137 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002138 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002139 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002140 if (VA.getLocInfo() == CCValAssign::Indirect)
2141 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002142 if (VA.needsCustom()) {
2143 // f64 and vector types are split into multiple registers or
2144 // register/stack-slot combinations. The types will not match
2145 // the registers; give up on memory f64 refs until we figure
2146 // out what to do about this.
2147 if (!VA.isRegLoc())
2148 return false;
2149 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002150 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002151 if (RegVT == MVT::v2f64) {
2152 if (!ArgLocs[++i].isRegLoc())
2153 return false;
2154 if (!ArgLocs[++i].isRegLoc())
2155 return false;
2156 }
2157 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002158 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2159 MFI, MRI, TII))
2160 return false;
2161 }
2162 }
2163 }
2164 }
2165
2166 return true;
2167}
2168
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002169bool
2170ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2171 MachineFunction &MF, bool isVarArg,
2172 const SmallVectorImpl<ISD::OutputArg> &Outs,
2173 LLVMContext &Context) const {
2174 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002175 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002176 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2177 isVarArg));
2178}
2179
Tim Northoverd8407452013-10-01 14:33:28 +00002180static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2181 SDLoc DL, SelectionDAG &DAG) {
2182 const MachineFunction &MF = DAG.getMachineFunction();
2183 const Function *F = MF.getFunction();
2184
2185 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2186
2187 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2188 // version of the "preferred return address". These offsets affect the return
2189 // instruction if this is a return from PL1 without hypervisor extensions.
2190 // IRQ/FIQ: +4 "subs pc, lr, #4"
2191 // SWI: 0 "subs pc, lr, #0"
2192 // ABORT: +4 "subs pc, lr, #4"
2193 // UNDEF: +4/+2 "subs pc, lr, #0"
2194 // UNDEF varies depending on where the exception came from ARM or Thumb
2195 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2196
2197 int64_t LROffset;
2198 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2199 IntKind == "ABORT")
2200 LROffset = 4;
2201 else if (IntKind == "SWI" || IntKind == "UNDEF")
2202 LROffset = 0;
2203 else
2204 report_fatal_error("Unsupported interrupt attribute. If present, value "
2205 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2206
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002207 RetOps.insert(RetOps.begin() + 1,
2208 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002209
Craig Topper48d114b2014-04-26 18:35:24 +00002210 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002211}
2212
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002213SDValue
2214ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002215 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002216 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002217 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002218 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002219
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002220 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002221 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002222
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002223 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002224 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2225 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002226
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002227 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002228 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2229 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002230
Bob Wilsona4c22902009-04-17 19:07:39 +00002231 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002232 SmallVector<SDValue, 4> RetOps;
2233 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002234 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002235
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002236 MachineFunction &MF = DAG.getMachineFunction();
2237 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2238 AFI->setReturnRegsCount(RVLocs.size());
2239
Bob Wilsona4c22902009-04-17 19:07:39 +00002240 // Copy the result values into the output registers.
2241 for (unsigned i = 0, realRVLocIdx = 0;
2242 i != RVLocs.size();
2243 ++i, ++realRVLocIdx) {
2244 CCValAssign &VA = RVLocs[i];
2245 assert(VA.isRegLoc() && "Can only return in registers!");
2246
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002247 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002248
2249 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002250 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002251 case CCValAssign::Full: break;
2252 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002253 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002254 break;
2255 }
2256
Bob Wilsona4c22902009-04-17 19:07:39 +00002257 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002258 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002259 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002260 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002261 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002262 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002263 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002264
Christian Pirkerb5728192014-05-08 14:06:24 +00002265 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2266 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2267 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002268 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002269 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002270 VA = RVLocs[++i]; // skip ahead to next loc
2271 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002272 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2273 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002274 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002275 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002276 VA = RVLocs[++i]; // skip ahead to next loc
2277
2278 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002279 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002280 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002281 }
2282 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2283 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002284 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002285 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002286 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2287 fmrrd.getValue(isLittleEndian ? 0 : 1),
2288 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002289 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002290 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002291 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002292 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2293 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002294 Flag);
2295 } else
2296 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2297
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002298 // Guarantee that all emitted copies are
2299 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002300 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002301 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002302 }
2303
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002304 // Update chain and glue.
2305 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002306 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002307 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002308
Tim Northoverd8407452013-10-01 14:33:28 +00002309 // CPUs which aren't M-class use a special sequence to return from
2310 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2311 // though we use "subs pc, lr, #N").
2312 //
2313 // M-class CPUs actually use a normal return sequence with a special
2314 // (hardware-provided) value in LR, so the normal code path works.
2315 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2316 !Subtarget->isMClass()) {
2317 if (Subtarget->isThumb1Only())
2318 report_fatal_error("interrupt attribute is not supported in Thumb1");
2319 return LowerInterruptReturn(RetOps, dl, DAG);
2320 }
2321
Craig Topper48d114b2014-04-26 18:35:24 +00002322 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002323}
2324
Evan Chengf8bad082012-04-10 01:51:00 +00002325bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002326 if (N->getNumValues() != 1)
2327 return false;
2328 if (!N->hasNUsesOfValue(1, 0))
2329 return false;
2330
Evan Chengf8bad082012-04-10 01:51:00 +00002331 SDValue TCChain = Chain;
2332 SDNode *Copy = *N->use_begin();
2333 if (Copy->getOpcode() == ISD::CopyToReg) {
2334 // If the copy has a glue operand, we conservatively assume it isn't safe to
2335 // perform a tail call.
2336 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2337 return false;
2338 TCChain = Copy->getOperand(0);
2339 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2340 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002341 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002342 SmallPtrSet<SDNode*, 2> Copies;
2343 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002344 UI != UE; ++UI) {
2345 if (UI->getOpcode() != ISD::CopyToReg)
2346 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002347 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002348 }
Evan Chengf8bad082012-04-10 01:51:00 +00002349 if (Copies.size() > 2)
2350 return false;
2351
2352 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2353 UI != UE; ++UI) {
2354 SDValue UseChain = UI->getOperand(0);
2355 if (Copies.count(UseChain.getNode()))
2356 // Second CopyToReg
2357 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002358 else {
2359 // We are at the top of this chain.
2360 // If the copy has a glue operand, we conservatively assume it
2361 // isn't safe to perform a tail call.
2362 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2363 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002364 // First CopyToReg
2365 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002366 }
Evan Chengf8bad082012-04-10 01:51:00 +00002367 }
2368 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002369 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002370 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002371 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002372 Copy = *Copy->use_begin();
2373 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002374 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002375 // If the copy has a glue operand, we conservatively assume it isn't safe to
2376 // perform a tail call.
2377 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2378 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002379 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002380 } else {
2381 return false;
2382 }
2383
Evan Cheng419ea282010-12-01 22:59:46 +00002384 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002385 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2386 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002387 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2388 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002389 return false;
2390 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002391 }
2392
Evan Chengf8bad082012-04-10 01:51:00 +00002393 if (!HasRet)
2394 return false;
2395
2396 Chain = TCChain;
2397 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002398}
2399
Evan Cheng0663f232011-03-21 01:19:09 +00002400bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002401 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002402 return false;
2403
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002404 auto Attr =
2405 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2406 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002407 return false;
2408
2409 return !Subtarget->isThumb1Only();
2410}
2411
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002412// Trying to write a 64 bit value so need to split into two 32 bit values first,
2413// and pass the lower and high parts through.
2414static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2415 SDLoc DL(Op);
2416 SDValue WriteValue = Op->getOperand(2);
2417
2418 // This function is only supposed to be called for i64 type argument.
2419 assert(WriteValue.getValueType() == MVT::i64
2420 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2421
2422 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2423 DAG.getConstant(0, DL, MVT::i32));
2424 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2425 DAG.getConstant(1, DL, MVT::i32));
2426 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2427 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2428}
2429
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002430// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2431// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2432// one of the above mentioned nodes. It has to be wrapped because otherwise
2433// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2434// be used to form addressing mode. These wrapped nodes will be selected
2435// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002436static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002437 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002438 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002439 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002440 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002441 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002442 if (CP->isMachineConstantPoolEntry())
2443 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2444 CP->getAlignment());
2445 else
2446 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2447 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002448 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002449}
2450
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002451unsigned ARMTargetLowering::getJumpTableEncoding() const {
2452 return MachineJumpTableInfo::EK_Inline;
2453}
2454
Dan Gohman21cea8a2010-04-17 15:26:15 +00002455SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2456 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002457 MachineFunction &MF = DAG.getMachineFunction();
2458 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2459 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002460 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002461 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002462 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002463 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2464 SDValue CPAddr;
2465 if (RelocM == Reloc::Static) {
2466 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2467 } else {
2468 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002469 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002470 ARMConstantPoolValue *CPV =
2471 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2472 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002473 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2474 }
2475 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2476 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002477 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002478 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002479 if (RelocM == Reloc::Static)
2480 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002481 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002482 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002483}
2484
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002485// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002486SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002487ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002488 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002489 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002490 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002491 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002492 MachineFunction &MF = DAG.getMachineFunction();
2493 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002494 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002495 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002496 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2497 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002498 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002499 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002500 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002501 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002502 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002503 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002504
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002505 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002506 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002507
2508 // call __tls_get_addr.
2509 ArgListTy Args;
2510 ArgListEntry Entry;
2511 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002512 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002513 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002514
Dale Johannesen555a3752009-01-30 23:10:59 +00002515 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002516 TargetLowering::CallLoweringInfo CLI(DAG);
2517 CLI.setDebugLoc(dl).setChain(Chain)
2518 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002519 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2520 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002521
Justin Holewinskiaa583972012-05-25 16:35:28 +00002522 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002523 return CallResult.first;
2524}
2525
2526// Lower ISD::GlobalTLSAddress using the "initial exec" or
2527// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002528SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002529ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002530 SelectionDAG &DAG,
2531 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002532 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002533 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002534 SDValue Offset;
2535 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002536 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002537 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002538 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002539
Hans Wennborgaea41202012-05-04 09:40:39 +00002540 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002541 MachineFunction &MF = DAG.getMachineFunction();
2542 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002543 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002544 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002545 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2546 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002547 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2548 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2549 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002550 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002551 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002552 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002553 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002554 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002555 Chain = Offset.getValue(1);
2556
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002557 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002558 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002559
Evan Chengcdbb70c2009-10-31 03:39:36 +00002560 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002561 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002562 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002563 } else {
2564 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002565 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002566 ARMConstantPoolValue *CPV =
2567 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002568 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002569 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002570 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002571 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002572 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002573 }
2574
2575 // The address of the thread local variable is the add of the thread
2576 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002577 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002578}
2579
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002580SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002581ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002582 // TODO: implement the "local dynamic" model
2583 assert(Subtarget->isTargetELF() &&
2584 "TLS not implemented for non-ELF targets");
2585 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002586
2587 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2588
2589 switch (model) {
2590 case TLSModel::GeneralDynamic:
2591 case TLSModel::LocalDynamic:
2592 return LowerToTLSGeneralDynamicModel(GA, DAG);
2593 case TLSModel::InitialExec:
2594 case TLSModel::LocalExec:
2595 return LowerToTLSExecModels(GA, DAG, model);
2596 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002597 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002598}
2599
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002600SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002601 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002602 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002603 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002604 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002605 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002606 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002607 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002608 ARMConstantPoolConstant::Create(GV,
2609 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002610 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002611 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002612 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002613 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002614 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002615 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002616 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002617 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002618 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002619 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002620 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002621 MachinePointerInfo::getGOT(),
2622 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002623 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002624 }
2625
2626 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002627 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002628 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002629 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002630 // FIXME: Once remat is capable of dealing with instructions with register
2631 // operands, expand this into two nodes.
2632 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2633 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002634 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002635 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2636 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2637 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2638 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002639 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002640 }
2641}
2642
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002643SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002644 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002645 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002646 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002647 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002648 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002649
Eric Christopherc1058df2014-07-04 01:55:26 +00002650 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002651 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002652
Tim Northover72360d22013-12-02 10:35:41 +00002653 // FIXME: Once remat is capable of dealing with instructions with register
2654 // operands, expand this into multiple nodes
2655 unsigned Wrapper =
2656 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002657
Tim Northover72360d22013-12-02 10:35:41 +00002658 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2659 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002660
Evan Cheng1b389522009-09-03 07:04:02 +00002661 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002662 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2663 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002664 return Result;
2665}
2666
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002667SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2668 SelectionDAG &DAG) const {
2669 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002670 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2671 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002672
2673 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002674 const ARMII::TOF TargetFlags =
2675 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002676 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002677 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002678 SDLoc DL(Op);
2679
2680 ++NumMovwMovt;
2681
2682 // FIXME: Once remat is capable of dealing with instructions with register
2683 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002684 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2685 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002686 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002687 if (GV->hasDLLImportStorageClass())
2688 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2689 MachinePointerInfo::getGOT(), false, false, false, 0);
2690 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002691}
2692
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002693SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002694 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002695 assert(Subtarget->isTargetELF() &&
2696 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002697 MachineFunction &MF = DAG.getMachineFunction();
2698 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002699 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002700 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002701 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002702 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002703 ARMConstantPoolValue *CPV =
2704 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2705 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002706 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002707 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002708 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002709 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002710 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002711 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002712 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002713}
2714
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002715SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002716ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002717 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002718 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002719 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2720 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002721 Op.getOperand(1), Val);
2722}
2723
2724SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002725ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002726 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002727 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002728 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002729}
2730
Matthias Braun3cd00c12015-07-16 22:34:16 +00002731SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
2732 SelectionDAG &DAG) const {
2733 SDLoc dl(Op);
2734 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
2735 Op.getOperand(0));
2736}
2737
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002738SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002739ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002740 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002741 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002742 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002743 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002744 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002745 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002746 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002747 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002748 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002749 }
Bob Wilson17f88782009-08-04 00:25:01 +00002750 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002751 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002752 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2753 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002754 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002755 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002756 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002757 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002758 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002759 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2760 SDValue CPAddr;
2761 unsigned PCAdj = (RelocM != Reloc::PIC_)
2762 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002763 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002764 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2765 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002766 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002767 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002768 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002769 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002770 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002771 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002772
2773 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002774 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002775 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2776 }
2777 return Result;
2778 }
Evan Cheng18381b42011-03-29 23:06:19 +00002779 case Intrinsic::arm_neon_vmulls:
2780 case Intrinsic::arm_neon_vmullu: {
2781 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2782 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002783 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002784 Op.getOperand(1), Op.getOperand(2));
2785 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002786 }
2787}
2788
Eli Friedman30a49e92011-08-03 21:06:02 +00002789static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2790 const ARMSubtarget *Subtarget) {
2791 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002792 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002793 if (!Subtarget->hasDataBarrier()) {
2794 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2795 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2796 // here.
2797 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002798 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002799 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002800 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002801 }
2802
Tim Northover36b24172013-07-03 09:20:36 +00002803 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2804 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002805 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002806 if (Subtarget->isMClass()) {
2807 // Only a full system barrier exists in the M-class architectures.
2808 Domain = ARM_MB::SY;
2809 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002810 // Swift happens to implement ISHST barriers in a way that's compatible with
2811 // Release semantics but weaker than ISH so we'd be fools not to use
2812 // it. Beware: other processors probably don't!
2813 Domain = ARM_MB::ISHST;
2814 }
2815
Joey Gouly926d3f52013-09-05 15:35:24 +00002816 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002817 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2818 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002819}
2820
Evan Cheng8740ee32010-11-03 06:34:55 +00002821static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2822 const ARMSubtarget *Subtarget) {
2823 // ARM pre v5TE and Thumb1 does not have preload instructions.
2824 if (!(Subtarget->isThumb2() ||
2825 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2826 // Just preserve the chain.
2827 return Op.getOperand(0);
2828
Andrew Trickef9de2a2013-05-25 02:42:55 +00002829 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002830 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2831 if (!isRead &&
2832 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2833 // ARMv7 with MP extension has PLDW.
2834 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002835
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002836 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2837 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002838 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002839 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002840 isData = ~isData & 1;
2841 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002842
2843 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002844 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2845 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002846}
2847
Dan Gohman31ae5862010-04-17 14:41:14 +00002848static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2849 MachineFunction &MF = DAG.getMachineFunction();
2850 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2851
Evan Cheng10043e22007-01-19 07:51:42 +00002852 // vastart just stores the address of the VarArgsFrameIndex slot into the
2853 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002854 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002855 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002856 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002857 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002858 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2859 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002860}
2861
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002862SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002863ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2864 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002865 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002866 MachineFunction &MF = DAG.getMachineFunction();
2867 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2868
Craig Topper760b1342012-02-22 05:59:10 +00002869 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002870 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002871 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002872 else
Craig Topperc7242e02012-04-20 07:30:17 +00002873 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002874
2875 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002876 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002877 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002878
2879 SDValue ArgValue2;
2880 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002881 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002882 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002883
2884 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002885 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Evan Chengcdbb70c2009-10-31 03:39:36 +00002886 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002887 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002888 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002889 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002890 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002891 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002892 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002893 if (!Subtarget->isLittle())
2894 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002895 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002896}
2897
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002898// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002899// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002900// byval). Either way, we allocate stack slots adjacent to the data
2901// provided by our caller, and store the unallocated registers there.
2902// If this is a variadic function, the va_list pointer will begin with
2903// these values; otherwise, this reassembles a (byval) structure that
2904// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002905// Return: The frame index registers were stored into.
2906int
2907ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002908 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002909 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002910 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002911 int ArgOffset,
2912 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002913 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002914 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002915 // Setup first unallocated register as first byval register;
2916 // eat all remained registers
2917 // (these two actions are performed by HandleByVal method).
2918 // Then, here, we initialize stack frame with
2919 // "store-reg" instructions.
2920 // Case #2. Var-args function, that doesn't contain byval parameters.
2921 // The same: eat all remained unallocated registers,
2922 // initialize stack frame.
2923
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002924 MachineFunction &MF = DAG.getMachineFunction();
2925 MachineFrameInfo *MFI = MF.getFrameInfo();
2926 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002927 unsigned RBegin, REnd;
2928 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2929 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002930 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002931 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00002932 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00002933 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002934 }
2935
Tim Northover8cda34f2015-03-11 18:54:22 +00002936 if (REnd != RBegin)
2937 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002938
Mehdi Amini44ede332015-07-09 02:09:04 +00002939 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00002940 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002941 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002942
Tim Northover8cda34f2015-03-11 18:54:22 +00002943 SmallVector<SDValue, 4> MemOps;
2944 const TargetRegisterClass *RC =
2945 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002946
Tim Northover8cda34f2015-03-11 18:54:22 +00002947 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
2948 unsigned VReg = MF.addLiveIn(Reg, RC);
2949 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2950 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002951 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00002952 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
2953 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00002954 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00002955 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002956
2957 if (!MemOps.empty())
2958 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
2959 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002960}
2961
2962// Setup stack frame, the va_list pointer will start from.
2963void
2964ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002965 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002966 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002967 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002968 bool ForceMutable) const {
2969 MachineFunction &MF = DAG.getMachineFunction();
2970 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2971
2972 // Try to store any remaining integer argument regs
2973 // to their spots on the stack so that they may be loaded by deferencing
2974 // the result of va_next.
2975 // If there is no regs to be stored, just point address after last
2976 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00002977 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2978 CCInfo.getInRegsParamsCount(),
2979 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002980 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002981}
2982
Bob Wilson2e076c42009-06-22 23:27:02 +00002983SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002984ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002985 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002986 const SmallVectorImpl<ISD::InputArg>
2987 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002988 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002989 SmallVectorImpl<SDValue> &InVals)
2990 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002991 MachineFunction &MF = DAG.getMachineFunction();
2992 MachineFrameInfo *MFI = MF.getFrameInfo();
2993
Bob Wilsona4c22902009-04-17 19:07:39 +00002994 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2995
2996 // Assign locations to all of the incoming arguments.
2997 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002998 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2999 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003000 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003001 CCAssignFnForNode(CallConv, /* Return*/ false,
3002 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003003
Bob Wilsona4c22902009-04-17 19:07:39 +00003004 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003005 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003006 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3007 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003008
3009 // Initially ArgRegsSaveSize is zero.
3010 // Then we increase this value each time we meet byval parameter.
3011 // We also increase this value in case of varargs function.
3012 AFI->setArgRegsSaveSize(0);
3013
Oliver Stannardd55e1152014-03-05 15:25:27 +00003014 // Calculate the amount of stack space that we need to allocate to store
3015 // byval and variadic arguments that are passed in registers.
3016 // We need to know this before we allocate the first byval or variadic
3017 // argument, as they will be allocated a stack slot below the CFA (Canonical
3018 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003019 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003020 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003021 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3022 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003023
Tim Northover8cda34f2015-03-11 18:54:22 +00003024 CCValAssign &VA = ArgLocs[i];
3025 unsigned Index = VA.getValNo();
3026 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3027 if (!Flags.isByVal())
3028 continue;
3029
3030 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3031 unsigned RBegin, REnd;
3032 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3033 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3034
3035 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003036 }
3037 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003038
3039 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003040 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003041 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3042 if (RegIdx != array_lengthof(GPRArgRegs))
3043 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003044 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003045
3046 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3047 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003048 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003049
Bob Wilsona4c22902009-04-17 19:07:39 +00003050 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3051 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003052 if (Ins[VA.getValNo()].isOrigArg()) {
3053 std::advance(CurOrigArg,
3054 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3055 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3056 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003057 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003058 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003059 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003060
Bob Wilsona4c22902009-04-17 19:07:39 +00003061 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003062 // f64 and vector types are split up into multiple registers or
3063 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003064 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003065 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003066 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003067 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003068 SDValue ArgValue2;
3069 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003070 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003071 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bob Wilson699bdf72010-04-13 22:03:22 +00003072 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003073 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003074 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003075 } else {
3076 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3077 Chain, DAG, dl);
3078 }
Owen Anderson9f944592009-08-11 20:47:22 +00003079 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3080 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003081 ArgValue, ArgValue1,
3082 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003083 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003084 ArgValue, ArgValue2,
3085 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003086 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003087 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003088
Bob Wilson2e076c42009-06-22 23:27:02 +00003089 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003090 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003091
Owen Anderson9f944592009-08-11 20:47:22 +00003092 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003093 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003094 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003095 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003096 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003097 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003098 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003099 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3100 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003101 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003102 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003103
3104 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003105 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003106 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003107 }
3108
3109 // If this is an 8 or 16-bit value, it is really passed promoted
3110 // to 32 bits. Insert an assert[sz]ext to capture this, then
3111 // truncate to the right size.
3112 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003113 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003114 case CCValAssign::Full: break;
3115 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003116 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003117 break;
3118 case CCValAssign::SExt:
3119 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3120 DAG.getValueType(VA.getValVT()));
3121 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3122 break;
3123 case CCValAssign::ZExt:
3124 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3125 DAG.getValueType(VA.getValVT()));
3126 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3127 break;
3128 }
3129
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003130 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003131
3132 } else { // VA.isRegLoc()
3133
3134 // sanity check
3135 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003136 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003137
Andrew Trick05938a52015-02-16 18:10:47 +00003138 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003139
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003140 // Some Ins[] entries become multiple ArgLoc[] entries.
3141 // Process them only once.
3142 if (index != lastInsIndex)
3143 {
3144 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003145 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003146 // This can be changed with more analysis.
3147 // In case of tail call optimization mark all arguments mutable.
3148 // Since they could be overwritten by lowering of arguments in case of
3149 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003150 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003151 assert(Ins[index].isOrigArg() &&
3152 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003153 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003154
Tim Northover8cda34f2015-03-11 18:54:22 +00003155 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3156 CurByValIndex, VA.getLocMemOffset(),
3157 Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003158 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003159 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003160 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003161 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003162 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003163 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003164
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003165 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003166 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003167 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3168 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003169 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003170 }
3171 lastInsIndex = index;
3172 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003173 }
3174 }
3175
3176 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003177 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003178 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003179 CCInfo.getNextStackOffset(),
3180 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003181
Oliver Stannardb14c6252014-04-02 16:10:33 +00003182 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3183
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003184 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003185}
3186
3187/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003188static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003189 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003190 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003191 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003192 // Maybe this has already been legalized into the constant pool?
3193 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003194 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003195 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003196 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003197 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003198 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003199 } else if (Op->getOpcode() == ISD::BITCAST &&
3200 Op->getValueType(0) == MVT::f64) {
3201 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3202 // created by LowerConstantFP().
3203 SDValue BitcastOp = Op->getOperand(0);
3204 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3205 SDValue MoveOp = BitcastOp->getOperand(0);
3206 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3207 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3208 return true;
3209 }
3210 }
Evan Cheng10043e22007-01-19 07:51:42 +00003211 }
3212 return false;
3213}
3214
Evan Cheng10043e22007-01-19 07:51:42 +00003215/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3216/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003217SDValue
3218ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003219 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003220 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003221 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003222 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003223 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003224 // Constant does not fit, try adjusting it by one?
3225 switch (CC) {
3226 default: break;
3227 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003228 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003229 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003230 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003231 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003232 }
3233 break;
3234 case ISD::SETULT:
3235 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003236 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003237 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003238 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003239 }
3240 break;
3241 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003242 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003243 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003244 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003245 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003246 }
3247 break;
3248 case ISD::SETULE:
3249 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003250 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003251 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003252 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003253 }
3254 break;
3255 }
3256 }
3257 }
3258
3259 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003260 ARMISD::NodeType CompareType;
3261 switch (CondCode) {
3262 default:
3263 CompareType = ARMISD::CMP;
3264 break;
3265 case ARMCC::EQ:
3266 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003267 // Uses only Z Flag
3268 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003269 break;
3270 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003271 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003272 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003273}
3274
3275/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003276SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003277ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003278 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003279 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003280 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003281 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003282 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003283 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003284 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3285 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003286}
3287
Bob Wilson45acbd02011-03-08 01:17:20 +00003288/// duplicateCmp - Glue values can have only one use, so this function
3289/// duplicates a comparison node.
3290SDValue
3291ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3292 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003293 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003294 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3295 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3296
3297 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3298 Cmp = Cmp.getOperand(0);
3299 Opc = Cmp.getOpcode();
3300 if (Opc == ARMISD::CMPFP)
3301 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3302 else {
3303 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3304 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3305 }
3306 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3307}
3308
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003309std::pair<SDValue, SDValue>
3310ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3311 SDValue &ARMcc) const {
3312 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3313
3314 SDValue Value, OverflowCmp;
3315 SDValue LHS = Op.getOperand(0);
3316 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003317 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003318
3319 // FIXME: We are currently always generating CMPs because we don't support
3320 // generating CMN through the backend. This is not as good as the natural
3321 // CMP case because it causes a register dependency and cannot be folded
3322 // later.
3323
3324 switch (Op.getOpcode()) {
3325 default:
3326 llvm_unreachable("Unknown overflow instruction!");
3327 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003328 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3329 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3330 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003331 break;
3332 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003333 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3334 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3335 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003336 break;
3337 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003338 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3339 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3340 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003341 break;
3342 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003343 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3344 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3345 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003346 break;
3347 } // switch (...)
3348
3349 return std::make_pair(Value, OverflowCmp);
3350}
3351
3352
3353SDValue
3354ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3355 // Let legalize expand this if it isn't a legal type yet.
3356 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3357 return SDValue();
3358
3359 SDValue Value, OverflowCmp;
3360 SDValue ARMcc;
3361 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3362 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003363 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003364 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003365 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3366 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003367 EVT VT = Op.getValueType();
3368
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003369 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003370 ARMcc, CCR, OverflowCmp);
3371
3372 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003373 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003374}
3375
3376
Bill Wendling6a981312010-08-11 08:43:16 +00003377SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3378 SDValue Cond = Op.getOperand(0);
3379 SDValue SelectTrue = Op.getOperand(1);
3380 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003381 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003382 unsigned Opc = Cond.getOpcode();
3383
3384 if (Cond.getResNo() == 1 &&
3385 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3386 Opc == ISD::USUBO)) {
3387 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3388 return SDValue();
3389
3390 SDValue Value, OverflowCmp;
3391 SDValue ARMcc;
3392 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3393 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3394 EVT VT = Op.getValueType();
3395
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003396 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003397 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003398 }
Bill Wendling6a981312010-08-11 08:43:16 +00003399
3400 // Convert:
3401 //
3402 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3403 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3404 //
3405 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3406 const ConstantSDNode *CMOVTrue =
3407 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3408 const ConstantSDNode *CMOVFalse =
3409 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3410
3411 if (CMOVTrue && CMOVFalse) {
3412 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3413 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3414
3415 SDValue True;
3416 SDValue False;
3417 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3418 True = SelectTrue;
3419 False = SelectFalse;
3420 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3421 True = SelectFalse;
3422 False = SelectTrue;
3423 }
3424
3425 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003426 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003427 SDValue ARMcc = Cond.getOperand(2);
3428 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003429 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003430 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003431 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003432 }
3433 }
3434 }
3435
Dan Gohmand4a77c42012-02-24 00:09:36 +00003436 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3437 // undefined bits before doing a full-word comparison with zero.
3438 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003439 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003440
Bill Wendling6a981312010-08-11 08:43:16 +00003441 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003442 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003443 SelectTrue, SelectFalse, ISD::SETNE);
3444}
3445
Joey Gouly881eab52013-08-22 15:29:11 +00003446static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3447 bool &swpCmpOps, bool &swpVselOps) {
3448 // Start by selecting the GE condition code for opcodes that return true for
3449 // 'equality'
3450 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3451 CC == ISD::SETULE)
3452 CondCode = ARMCC::GE;
3453
3454 // and GT for opcodes that return false for 'equality'.
3455 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3456 CC == ISD::SETULT)
3457 CondCode = ARMCC::GT;
3458
3459 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3460 // to swap the compare operands.
3461 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3462 CC == ISD::SETULT)
3463 swpCmpOps = true;
3464
3465 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3466 // If we have an unordered opcode, we need to swap the operands to the VSEL
3467 // instruction (effectively negating the condition).
3468 //
3469 // This also has the effect of swapping which one of 'less' or 'greater'
3470 // returns true, so we also swap the compare operands. It also switches
3471 // whether we return true for 'equality', so we compensate by picking the
3472 // opposite condition code to our original choice.
3473 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3474 CC == ISD::SETUGT) {
3475 swpCmpOps = !swpCmpOps;
3476 swpVselOps = !swpVselOps;
3477 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3478 }
3479
3480 // 'ordered' is 'anything but unordered', so use the VS condition code and
3481 // swap the VSEL operands.
3482 if (CC == ISD::SETO) {
3483 CondCode = ARMCC::VS;
3484 swpVselOps = true;
3485 }
3486
3487 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3488 // code and swap the VSEL operands.
3489 if (CC == ISD::SETUNE) {
3490 CondCode = ARMCC::EQ;
3491 swpVselOps = true;
3492 }
3493}
3494
Oliver Stannard51b1d462014-08-21 12:50:31 +00003495SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3496 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3497 SDValue Cmp, SelectionDAG &DAG) const {
3498 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3499 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3500 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3501 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3502 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3503
3504 SDValue TrueLow = TrueVal.getValue(0);
3505 SDValue TrueHigh = TrueVal.getValue(1);
3506 SDValue FalseLow = FalseVal.getValue(0);
3507 SDValue FalseHigh = FalseVal.getValue(1);
3508
3509 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3510 ARMcc, CCR, Cmp);
3511 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3512 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3513
3514 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3515 } else {
3516 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3517 Cmp);
3518 }
3519}
3520
Dan Gohman21cea8a2010-04-17 15:26:15 +00003521SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003522 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003523 SDValue LHS = Op.getOperand(0);
3524 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003525 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003526 SDValue TrueVal = Op.getOperand(2);
3527 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003528 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003529
Oliver Stannard51b1d462014-08-21 12:50:31 +00003530 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3531 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3532 dl);
3533
3534 // If softenSetCCOperands only returned one value, we should compare it to
3535 // zero.
3536 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003537 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003538 CC = ISD::SETNE;
3539 }
3540 }
3541
Owen Anderson9f944592009-08-11 20:47:22 +00003542 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003543 // Try to generate VSEL on ARMv8.
3544 // The VSEL instruction can't use all the usual ARM condition
3545 // codes: it only has two bits to select the condition code, so it's
3546 // constrained to use only GE, GT, VS and EQ.
3547 //
3548 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3549 // swap the operands of the previous compare instruction (effectively
3550 // inverting the compare condition, swapping 'less' and 'greater') and
3551 // sometimes need to swap the operands to the VSEL (which inverts the
3552 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003553 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3554 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003555 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3556 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3557 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003558 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003559 std::swap(TrueVal, FalseVal);
3560 }
3561 }
3562
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003563 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003564 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003565 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003566 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003567 }
3568
3569 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003570 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003571
Scott Douglass7ad77922015-04-08 17:18:28 +00003572 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003573 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3574 TrueVal.getValueType() == MVT::f64)) {
Scott Douglass7ad77922015-04-08 17:18:28 +00003575 // We can use VMAXNM/VMINNM for a compare followed by a select with the
Joey Goulye3dd6842013-08-23 12:01:13 +00003576 // same operands, as follows:
Scott Douglass7ad77922015-04-08 17:18:28 +00003577 // c = fcmp [?gt, ?ge, ?lt, ?le] a, b
Joey Goulye3dd6842013-08-23 12:01:13 +00003578 // select c, a, b
Scott Douglass7ad77922015-04-08 17:18:28 +00003579 // In NoNaNsFPMath the CC will have been changed from, e.g., 'ogt' to 'gt'.
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003580 bool swapSides = false;
3581 if (!getTargetMachine().Options.NoNaNsFPMath) {
3582 // transformability may depend on which way around we compare
3583 switch (CC) {
3584 default:
3585 break;
3586 case ISD::SETOGT:
3587 case ISD::SETOGE:
3588 case ISD::SETOLT:
3589 case ISD::SETOLE:
3590 // the non-NaN should be RHS
3591 swapSides = DAG.isKnownNeverNaN(LHS) && !DAG.isKnownNeverNaN(RHS);
3592 break;
3593 case ISD::SETUGT:
3594 case ISD::SETUGE:
3595 case ISD::SETULT:
3596 case ISD::SETULE:
3597 // the non-NaN should be LHS
3598 swapSides = DAG.isKnownNeverNaN(RHS) && !DAG.isKnownNeverNaN(LHS);
3599 break;
3600 }
3601 }
3602 swapSides = swapSides || (LHS == FalseVal && RHS == TrueVal);
3603 if (swapSides) {
3604 CC = ISD::getSetCCSwappedOperands(CC);
3605 std::swap(LHS, RHS);
3606 }
3607 if (LHS == TrueVal && RHS == FalseVal) {
3608 bool canTransform = true;
3609 // FIXME: FastMathFlags::noSignedZeros() doesn't appear reachable from here
3610 if (!getTargetMachine().Options.UnsafeFPMath &&
3611 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
3612 const ConstantFPSDNode *Zero;
3613 switch (CC) {
3614 default:
3615 break;
3616 case ISD::SETOGT:
3617 case ISD::SETUGT:
3618 case ISD::SETGT:
3619 // RHS must not be -0
3620 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3621 !Zero->isNegative();
3622 break;
3623 case ISD::SETOGE:
3624 case ISD::SETUGE:
3625 case ISD::SETGE:
3626 // LHS must not be -0
3627 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3628 !Zero->isNegative();
3629 break;
3630 case ISD::SETOLT:
3631 case ISD::SETULT:
3632 case ISD::SETLT:
3633 // RHS must not be +0
3634 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3635 Zero->isNegative();
3636 break;
3637 case ISD::SETOLE:
3638 case ISD::SETULE:
3639 case ISD::SETLE:
3640 // LHS must not be +0
3641 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3642 Zero->isNegative();
3643 break;
3644 }
3645 }
3646 if (canTransform) {
3647 // Note: If one of the elements in a pair is a number and the other
3648 // element is NaN, the corresponding result element is the number.
3649 // This is consistent with the IEEE 754-2008 standard.
3650 // Therefore, a > b ? a : b <=> vmax(a,b), if b is constant and a is NaN
3651 switch (CC) {
3652 default:
3653 break;
3654 case ISD::SETOGT:
3655 case ISD::SETOGE:
3656 if (!DAG.isKnownNeverNaN(RHS))
3657 break;
3658 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3659 case ISD::SETUGT:
3660 case ISD::SETUGE:
3661 if (!DAG.isKnownNeverNaN(LHS))
3662 break;
3663 case ISD::SETGT:
3664 case ISD::SETGE:
3665 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3666 case ISD::SETOLT:
3667 case ISD::SETOLE:
3668 if (!DAG.isKnownNeverNaN(RHS))
3669 break;
3670 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3671 case ISD::SETULT:
3672 case ISD::SETULE:
3673 if (!DAG.isKnownNeverNaN(LHS))
3674 break;
3675 case ISD::SETLT:
3676 case ISD::SETLE:
3677 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3678 }
Oliver Stannard79efe412014-10-27 09:23:02 +00003679 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003680 }
3681
Joey Gouly881eab52013-08-22 15:29:11 +00003682 bool swpCmpOps = false;
3683 bool swpVselOps = false;
3684 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3685
3686 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3687 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3688 if (swpCmpOps)
3689 std::swap(LHS, RHS);
3690 if (swpVselOps)
3691 std::swap(TrueVal, FalseVal);
3692 }
3693 }
3694
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003695 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003696 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003697 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003698 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003699 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003700 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003701 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003702 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003703 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003704 }
3705 return Result;
3706}
3707
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003708/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3709/// to morph to an integer compare sequence.
3710static bool canChangeToInt(SDValue Op, bool &SeenZero,
3711 const ARMSubtarget *Subtarget) {
3712 SDNode *N = Op.getNode();
3713 if (!N->hasOneUse())
3714 // Otherwise it requires moving the value from fp to integer registers.
3715 return false;
3716 if (!N->getNumValues())
3717 return false;
3718 EVT VT = Op.getValueType();
3719 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3720 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3721 // vmrs are very slow, e.g. cortex-a8.
3722 return false;
3723
3724 if (isFloatingPointZero(Op)) {
3725 SeenZero = true;
3726 return true;
3727 }
3728 return ISD::isNormalLoad(N);
3729}
3730
3731static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3732 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003733 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003734
3735 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003736 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003737 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003738 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003739 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003740
3741 llvm_unreachable("Unknown VFP cmp argument!");
3742}
3743
3744static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3745 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003746 SDLoc dl(Op);
3747
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003748 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003749 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3750 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003751 return;
3752 }
3753
3754 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3755 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003756 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003757 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003758 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003759 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003760 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003761
3762 EVT PtrType = Ptr.getValueType();
3763 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003764 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3765 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3766 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003767 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003768 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003769 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003770 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003771 return;
3772 }
3773
3774 llvm_unreachable("Unknown VFP cmp argument!");
3775}
3776
3777/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3778/// f32 and even f64 comparisons to integer ones.
3779SDValue
3780ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3781 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003782 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003783 SDValue LHS = Op.getOperand(2);
3784 SDValue RHS = Op.getOperand(3);
3785 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003786 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003787
Evan Chengd12af5d2012-03-01 23:27:13 +00003788 bool LHSSeenZero = false;
3789 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3790 bool RHSSeenZero = false;
3791 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3792 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003793 // If unsafe fp math optimization is enabled and there are no other uses of
3794 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003795 // to an integer comparison.
3796 if (CC == ISD::SETOEQ)
3797 CC = ISD::SETEQ;
3798 else if (CC == ISD::SETUNE)
3799 CC = ISD::SETNE;
3800
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003801 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003802 SDValue ARMcc;
3803 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003804 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3805 bitcastf32Toi32(LHS, DAG), Mask);
3806 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3807 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003808 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3809 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3810 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3811 Chain, Dest, ARMcc, CCR, Cmp);
3812 }
3813
3814 SDValue LHS1, LHS2;
3815 SDValue RHS1, RHS2;
3816 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3817 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003818 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3819 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003820 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003821 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003822 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003823 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003824 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003825 }
3826
3827 return SDValue();
3828}
3829
3830SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3831 SDValue Chain = Op.getOperand(0);
3832 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3833 SDValue LHS = Op.getOperand(2);
3834 SDValue RHS = Op.getOperand(3);
3835 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003836 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003837
Oliver Stannard51b1d462014-08-21 12:50:31 +00003838 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3839 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3840 dl);
3841
3842 // If softenSetCCOperands only returned one value, we should compare it to
3843 // zero.
3844 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003845 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003846 CC = ISD::SETNE;
3847 }
3848 }
3849
Owen Anderson9f944592009-08-11 20:47:22 +00003850 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003851 SDValue ARMcc;
3852 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003853 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003854 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003855 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003856 }
3857
Owen Anderson9f944592009-08-11 20:47:22 +00003858 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003859
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003860 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003861 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3862 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3863 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3864 if (Result.getNode())
3865 return Result;
3866 }
3867
Evan Cheng10043e22007-01-19 07:51:42 +00003868 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003869 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003870
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003871 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003872 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003873 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003874 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003875 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003876 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003877 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003878 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003879 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003880 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003881 }
3882 return Res;
3883}
3884
Dan Gohman21cea8a2010-04-17 15:26:15 +00003885SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003886 SDValue Chain = Op.getOperand(0);
3887 SDValue Table = Op.getOperand(1);
3888 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003889 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003890
Mehdi Amini44ede332015-07-09 02:09:04 +00003891 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003892 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003893 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003894 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003895 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003896 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003897 if (Subtarget->isThumb2()) {
3898 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3899 // which does another jump to the destination. This also makes it easier
3900 // to translate it to TBB / TBH later.
3901 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003902 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003903 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003904 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003905 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003906 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003907 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003908 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003909 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003910 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003911 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003912 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003913 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003914 MachinePointerInfo::getJumpTable(),
3915 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003916 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003917 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003918 }
Evan Cheng10043e22007-01-19 07:51:42 +00003919}
3920
Eli Friedman2d4055b2011-11-09 23:36:02 +00003921static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003922 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003923 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003924
James Molloy547d4c02012-02-20 09:24:05 +00003925 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3926 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3927 return Op;
3928 return DAG.UnrollVectorOp(Op.getNode());
3929 }
3930
3931 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3932 "Invalid type for custom lowering!");
3933 if (VT != MVT::v4i16)
3934 return DAG.UnrollVectorOp(Op.getNode());
3935
3936 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3937 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003938}
3939
Oliver Stannard51b1d462014-08-21 12:50:31 +00003940SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003941 EVT VT = Op.getValueType();
3942 if (VT.isVector())
3943 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003944 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3945 RTLIB::Libcall LC;
3946 if (Op.getOpcode() == ISD::FP_TO_SINT)
3947 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3948 Op.getValueType());
3949 else
3950 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3951 Op.getValueType());
3952 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3953 /*isSigned*/ false, SDLoc(Op)).first;
3954 }
3955
James Molloyfa041152015-03-23 16:15:16 +00003956 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003957}
3958
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003959static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3960 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003961 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003962
Eli Friedman2d4055b2011-11-09 23:36:02 +00003963 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3964 if (VT.getVectorElementType() == MVT::f32)
3965 return Op;
3966 return DAG.UnrollVectorOp(Op.getNode());
3967 }
3968
Duncan Sandsa41634e2011-08-12 14:54:45 +00003969 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3970 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003971 if (VT != MVT::v4f32)
3972 return DAG.UnrollVectorOp(Op.getNode());
3973
3974 unsigned CastOpc;
3975 unsigned Opc;
3976 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003977 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003978 case ISD::SINT_TO_FP:
3979 CastOpc = ISD::SIGN_EXTEND;
3980 Opc = ISD::SINT_TO_FP;
3981 break;
3982 case ISD::UINT_TO_FP:
3983 CastOpc = ISD::ZERO_EXTEND;
3984 Opc = ISD::UINT_TO_FP;
3985 break;
3986 }
3987
3988 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3989 return DAG.getNode(Opc, dl, VT, Op);
3990}
3991
Oliver Stannard51b1d462014-08-21 12:50:31 +00003992SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003993 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003994 if (VT.isVector())
3995 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003996 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3997 RTLIB::Libcall LC;
3998 if (Op.getOpcode() == ISD::SINT_TO_FP)
3999 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4000 Op.getValueType());
4001 else
4002 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4003 Op.getValueType());
4004 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
4005 /*isSigned*/ false, SDLoc(Op)).first;
4006 }
4007
James Molloyfa041152015-03-23 16:15:16 +00004008 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004009}
4010
Evan Cheng25f93642010-07-08 02:08:50 +00004011SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004012 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004013 SDValue Tmp0 = Op.getOperand(0);
4014 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004015 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004016 EVT VT = Op.getValueType();
4017 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004018 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4019 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4020 bool UseNEON = !InGPR && Subtarget->hasNEON();
4021
4022 if (UseNEON) {
4023 // Use VBSL to copy the sign bit.
4024 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4025 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004026 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004027 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4028 if (VT == MVT::f64)
4029 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4030 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004031 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004032 else /*if (VT == MVT::f32)*/
4033 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4034 if (SrcVT == MVT::f32) {
4035 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4036 if (VT == MVT::f64)
4037 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4038 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004039 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004040 } else if (VT == MVT::f32)
4041 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4042 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004043 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004044 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4045 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4046
4047 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004048 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004049 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4050 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4051 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004052
Evan Chengd6b641e2011-02-23 02:24:55 +00004053 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4054 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4055 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004056 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004057 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4058 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004059 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004060 } else {
4061 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4062 }
4063
4064 return Res;
4065 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004066
4067 // Bitcast operand 1 to i32.
4068 if (SrcVT == MVT::f64)
4069 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004070 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004071 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4072
Evan Chengd6b641e2011-02-23 02:24:55 +00004073 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004074 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4075 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004076 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4077 if (VT == MVT::f32) {
4078 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4079 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4080 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4081 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004082 }
4083
Evan Chengd6b641e2011-02-23 02:24:55 +00004084 // f64: Or the high part with signbit and then combine two parts.
4085 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004086 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004087 SDValue Lo = Tmp0.getValue(0);
4088 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4089 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4090 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004091}
4092
Evan Cheng168ced92010-05-22 01:47:14 +00004093SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4094 MachineFunction &MF = DAG.getMachineFunction();
4095 MachineFrameInfo *MFI = MF.getFrameInfo();
4096 MFI->setReturnAddressIsTaken(true);
4097
Bill Wendling908bf812014-01-06 00:43:20 +00004098 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004099 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004100
Evan Cheng168ced92010-05-22 01:47:14 +00004101 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004102 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004103 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4104 if (Depth) {
4105 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004106 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004107 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4108 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004109 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004110 }
4111
4112 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004113 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004114 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4115}
4116
Dan Gohman21cea8a2010-04-17 15:26:15 +00004117SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004118 const ARMBaseRegisterInfo &ARI =
4119 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4120 MachineFunction &MF = DAG.getMachineFunction();
4121 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004122 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004123
Owen Anderson53aa7a92009-08-10 22:56:29 +00004124 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004125 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004126 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004127 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004128 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4129 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004130 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4131 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004132 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004133 return FrameAddr;
4134}
4135
Renato Golinc7aea402014-05-06 16:51:25 +00004136// FIXME? Maybe this could be a TableGen attribute on some registers and
4137// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004138unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4139 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004140 unsigned Reg = StringSwitch<unsigned>(RegName)
4141 .Case("sp", ARM::SP)
4142 .Default(0);
4143 if (Reg)
4144 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004145 report_fatal_error(Twine("Invalid register name \""
4146 + StringRef(RegName) + "\"."));
4147}
4148
4149// Result is 64 bit value so split into two 32 bit values and return as a
4150// pair of values.
4151static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4152 SelectionDAG &DAG) {
4153 SDLoc DL(N);
4154
4155 // This function is only supposed to be called for i64 type destination.
4156 assert(N->getValueType(0) == MVT::i64
4157 && "ExpandREAD_REGISTER called for non-i64 type result.");
4158
4159 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4160 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4161 N->getOperand(0),
4162 N->getOperand(1));
4163
4164 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4165 Read.getValue(1)));
4166 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004167}
4168
Wesley Peck527da1b2010-11-23 03:31:01 +00004169/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004170/// expand a bit convert where either the source or destination type is i64 to
4171/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4172/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4173/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004174static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004175 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004176 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004177 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004178
Bob Wilson59b70ea2010-04-17 05:30:19 +00004179 // This function is only supposed to be called for i64 types, either as the
4180 // source or destination of the bit convert.
4181 EVT SrcVT = Op.getValueType();
4182 EVT DstVT = N->getValueType(0);
4183 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004184 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004185
Bob Wilson59b70ea2010-04-17 05:30:19 +00004186 // Turn i64->f64 into VMOVDRR.
4187 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004188 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004189 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004190 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004191 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004192 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004193 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004194 }
Bob Wilson7117a912009-03-20 22:42:55 +00004195
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004196 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004197 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004198 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004199 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004200 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004201 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4202 DAG.getVTList(MVT::i32, MVT::i32),
4203 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4204 else
4205 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4206 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004207 // Merge the pieces into a single i64 value.
4208 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4209 }
Bob Wilson7117a912009-03-20 22:42:55 +00004210
Bob Wilson59b70ea2010-04-17 05:30:19 +00004211 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004212}
4213
Bob Wilson2e076c42009-06-22 23:27:02 +00004214/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004215/// Zero vectors are used to represent vector negation and in those cases
4216/// will be implemented with the NEON VNEG instruction. However, VNEG does
4217/// not support i64 elements, so sometimes the zero vectors will need to be
4218/// explicitly constructed. Regardless, use a canonical VMOV to create the
4219/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004220static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004221 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004222 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004223 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004224 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4225 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004226 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004227}
4228
Jim Grosbach624fcb22009-10-31 21:00:56 +00004229/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4230/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004231SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4232 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004233 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4234 EVT VT = Op.getValueType();
4235 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004236 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004237 SDValue ShOpLo = Op.getOperand(0);
4238 SDValue ShOpHi = Op.getOperand(1);
4239 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004240 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004241 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004242
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004243 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4244
Jim Grosbach624fcb22009-10-31 21:00:56 +00004245 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004246 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004247 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4248 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004249 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004250 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4251 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004252 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004253
4254 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004255 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4256 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004257 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004258 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004259 CCR, Cmp);
4260
4261 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004262 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004263}
4264
Jim Grosbach5d994042009-10-31 19:38:01 +00004265/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4266/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004267SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4268 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004269 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4270 EVT VT = Op.getValueType();
4271 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004272 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004273 SDValue ShOpLo = Op.getOperand(0);
4274 SDValue ShOpHi = Op.getOperand(1);
4275 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004276 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004277
4278 assert(Op.getOpcode() == ISD::SHL_PARTS);
4279 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004280 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004281 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4282 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004283 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004284 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4285 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4286
4287 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4288 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004289 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4290 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004291 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004292 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004293 CCR, Cmp);
4294
4295 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004296 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004297}
4298
Jim Grosbach535d3b42010-09-08 03:54:02 +00004299SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004300 SelectionDAG &DAG) const {
4301 // The rounding mode is in bits 23:22 of the FPSCR.
4302 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4303 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4304 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004305 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004306 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004307 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004308 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004309 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004310 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004311 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004312 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004313 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004314 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004315}
4316
Jim Grosbach8546ec92010-01-18 19:58:49 +00004317static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4318 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004319 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004320 EVT VT = N->getValueType(0);
4321 if (VT.isVector()) {
4322 assert(ST->hasNEON());
4323
4324 // Compute the least significant set bit: LSB = X & -X
4325 SDValue X = N->getOperand(0);
4326 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4327 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4328
4329 EVT ElemTy = VT.getVectorElementType();
4330
4331 if (ElemTy == MVT::i8) {
4332 // Compute with: cttz(x) = ctpop(lsb - 1)
4333 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4334 DAG.getTargetConstant(1, dl, ElemTy));
4335 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4336 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4337 }
4338
4339 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4340 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4341 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4342 unsigned NumBits = ElemTy.getSizeInBits();
4343 SDValue WidthMinus1 =
4344 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4345 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4346 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4347 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4348 }
4349
4350 // Compute with: cttz(x) = ctpop(lsb - 1)
4351
4352 // Since we can only compute the number of bits in a byte with vcnt.8, we
4353 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4354 // and i64.
4355
4356 // Compute LSB - 1.
4357 SDValue Bits;
4358 if (ElemTy == MVT::i64) {
4359 // Load constant 0xffff'ffff'ffff'ffff to register.
4360 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4361 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4362 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4363 } else {
4364 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4365 DAG.getTargetConstant(1, dl, ElemTy));
4366 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4367 }
4368
4369 // Count #bits with vcnt.8.
4370 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4371 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4372 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4373
4374 // Gather the #bits with vpaddl (pairwise add.)
4375 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4376 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4377 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4378 Cnt8);
4379 if (ElemTy == MVT::i16)
4380 return Cnt16;
4381
4382 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4383 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4384 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4385 Cnt16);
4386 if (ElemTy == MVT::i32)
4387 return Cnt32;
4388
4389 assert(ElemTy == MVT::i64);
4390 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4391 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4392 Cnt32);
4393 return Cnt64;
4394 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004395
4396 if (!ST->hasV6T2Ops())
4397 return SDValue();
4398
4399 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4400 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4401}
4402
Evan Chengb4eae132012-12-04 22:41:50 +00004403/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4404/// for each 16-bit element from operand, repeated. The basic idea is to
4405/// leverage vcnt to get the 8-bit counts, gather and add the results.
4406///
4407/// Trace for v4i16:
4408/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4409/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4410/// vcnt: N1 = [b0 b1 b2 b3 b4 b5 b6 b7] (bi = bit-count of 8-bit element wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004411/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004412/// [b0 b1 b2 b3 b4 b5 b6 b7]
4413/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4414/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4415/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4416static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4417 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004418 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004419
4420 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4421 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4422 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4423 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4424 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4425 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4426}
4427
4428/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4429/// bit-count for each 16-bit element from the operand. We need slightly
4430/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4431/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004432///
Evan Chengb4eae132012-12-04 22:41:50 +00004433/// Trace for v4i16:
4434/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4435/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4436/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4437/// v4i16:Extracted = [k0 k1 k2 k3 ]
4438static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4439 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004440 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004441
4442 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4443 if (VT.is64BitVector()) {
4444 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4445 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004446 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004447 } else {
4448 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004449 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004450 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4451 }
4452}
4453
4454/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4455/// bit-count for each 32-bit element from the operand. The idea here is
4456/// to split the vector into 16-bit elements, leverage the 16-bit count
4457/// routine, and then combine the results.
4458///
4459/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4460/// input = [v0 v1 ] (vi: 32-bit elements)
4461/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4462/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004463/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004464/// [k0 k1 k2 k3 ]
4465/// N1 =+[k1 k0 k3 k2 ]
4466/// [k0 k2 k1 k3 ]
4467/// N2 =+[k1 k3 k0 k2 ]
4468/// [k0 k2 k1 k3 ]
4469/// Extended =+[k1 k3 k0 k2 ]
4470/// [k0 k2 ]
4471/// Extracted=+[k1 k3 ]
4472///
4473static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4474 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004475 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004476
4477 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4478
4479 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4480 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4481 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4482 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4483 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4484
4485 if (VT.is64BitVector()) {
4486 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4487 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004488 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004489 } else {
4490 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004491 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004492 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4493 }
4494}
4495
4496static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4497 const ARMSubtarget *ST) {
4498 EVT VT = N->getValueType(0);
4499
4500 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004501 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4502 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004503 "Unexpected type for custom ctpop lowering");
4504
4505 if (VT.getVectorElementType() == MVT::i32)
4506 return lowerCTPOP32BitElements(N, DAG);
4507 else
4508 return lowerCTPOP16BitElements(N, DAG);
4509}
4510
Bob Wilson2e076c42009-06-22 23:27:02 +00004511static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4512 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004513 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004514 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004515
Bob Wilson7d471332010-11-18 21:16:28 +00004516 if (!VT.isVector())
4517 return SDValue();
4518
Bob Wilson2e076c42009-06-22 23:27:02 +00004519 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004520 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004521
Bob Wilson7d471332010-11-18 21:16:28 +00004522 // Left shifts translate directly to the vshiftu intrinsic.
4523 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004524 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004525 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4526 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004527 N->getOperand(0), N->getOperand(1));
4528
4529 assert((N->getOpcode() == ISD::SRA ||
4530 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4531
4532 // NEON uses the same intrinsics for both left and right shifts. For
4533 // right shifts, the shift amounts are negative, so negate the vector of
4534 // shift amounts.
4535 EVT ShiftVT = N->getOperand(1).getValueType();
4536 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4537 getZeroVector(ShiftVT, DAG, dl),
4538 N->getOperand(1));
4539 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4540 Intrinsic::arm_neon_vshifts :
4541 Intrinsic::arm_neon_vshiftu);
4542 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004543 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004544 N->getOperand(0), NegatedCount);
4545}
4546
4547static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4548 const ARMSubtarget *ST) {
4549 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004550 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004551
Eli Friedman682d8c12009-08-22 03:13:10 +00004552 // We can get here for a node like i32 = ISD::SHL i32, i64
4553 if (VT != MVT::i64)
4554 return SDValue();
4555
4556 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004557 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004558
Chris Lattnerf81d5882007-11-24 07:07:01 +00004559 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4560 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004561 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004562 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004563
Chris Lattnerf81d5882007-11-24 07:07:01 +00004564 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004565 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004566
Chris Lattnerf81d5882007-11-24 07:07:01 +00004567 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004568 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004569 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004570 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004571 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004572
Chris Lattnerf81d5882007-11-24 07:07:01 +00004573 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4574 // captures the result into a carry flag.
4575 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004576 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004577
Chris Lattnerf81d5882007-11-24 07:07:01 +00004578 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004579 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004580
Chris Lattnerf81d5882007-11-24 07:07:01 +00004581 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004582 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004583}
4584
Bob Wilson2e076c42009-06-22 23:27:02 +00004585static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4586 SDValue TmpOp0, TmpOp1;
4587 bool Invert = false;
4588 bool Swap = false;
4589 unsigned Opc = 0;
4590
4591 SDValue Op0 = Op.getOperand(0);
4592 SDValue Op1 = Op.getOperand(1);
4593 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004594 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004595 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004596 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004597 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004598
Oliver Stannard51b1d462014-08-21 12:50:31 +00004599 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004600 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004601 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004602 case ISD::SETUNE:
4603 case ISD::SETNE: Invert = true; // Fallthrough
4604 case ISD::SETOEQ:
4605 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4606 case ISD::SETOLT:
4607 case ISD::SETLT: Swap = true; // Fallthrough
4608 case ISD::SETOGT:
4609 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4610 case ISD::SETOLE:
4611 case ISD::SETLE: Swap = true; // Fallthrough
4612 case ISD::SETOGE:
4613 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4614 case ISD::SETUGE: Swap = true; // Fallthrough
4615 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4616 case ISD::SETUGT: Swap = true; // Fallthrough
4617 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4618 case ISD::SETUEQ: Invert = true; // Fallthrough
4619 case ISD::SETONE:
4620 // Expand this to (OLT | OGT).
4621 TmpOp0 = Op0;
4622 TmpOp1 = Op1;
4623 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004624 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4625 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004626 break;
4627 case ISD::SETUO: Invert = true; // Fallthrough
4628 case ISD::SETO:
4629 // Expand this to (OLT | OGE).
4630 TmpOp0 = Op0;
4631 TmpOp1 = Op1;
4632 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004633 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4634 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004635 break;
4636 }
4637 } else {
4638 // Integer comparisons.
4639 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004640 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004641 case ISD::SETNE: Invert = true;
4642 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4643 case ISD::SETLT: Swap = true;
4644 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4645 case ISD::SETLE: Swap = true;
4646 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4647 case ISD::SETULT: Swap = true;
4648 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4649 case ISD::SETULE: Swap = true;
4650 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4651 }
4652
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004653 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004654 if (Opc == ARMISD::VCEQ) {
4655
4656 SDValue AndOp;
4657 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4658 AndOp = Op0;
4659 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4660 AndOp = Op1;
4661
4662 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004663 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004664 AndOp = AndOp.getOperand(0);
4665
4666 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4667 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004668 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4669 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004670 Invert = !Invert;
4671 }
4672 }
4673 }
4674
4675 if (Swap)
4676 std::swap(Op0, Op1);
4677
Owen Andersonc7baee32010-11-08 23:21:22 +00004678 // If one of the operands is a constant vector zero, attempt to fold the
4679 // comparison to a specialized compare-against-zero form.
4680 SDValue SingleOp;
4681 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4682 SingleOp = Op0;
4683 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4684 if (Opc == ARMISD::VCGE)
4685 Opc = ARMISD::VCLEZ;
4686 else if (Opc == ARMISD::VCGT)
4687 Opc = ARMISD::VCLTZ;
4688 SingleOp = Op1;
4689 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004690
Owen Andersonc7baee32010-11-08 23:21:22 +00004691 SDValue Result;
4692 if (SingleOp.getNode()) {
4693 switch (Opc) {
4694 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004695 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004696 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004697 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004698 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004699 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004700 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004701 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004702 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004703 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004704 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004705 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004706 }
4707 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004708 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004709 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004710
Tim Northover45aa89c2015-02-08 00:50:47 +00004711 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4712
Bob Wilson2e076c42009-06-22 23:27:02 +00004713 if (Invert)
4714 Result = DAG.getNOT(dl, Result, VT);
4715
4716 return Result;
4717}
4718
Bob Wilson5b2b5042010-06-14 22:19:57 +00004719/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4720/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004721/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004722static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4723 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004724 SDLoc dl, EVT &VT, bool is128Bits,
4725 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004726 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004727
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004728 // SplatBitSize is set to the smallest size that splats the vector, so a
4729 // zero vector will always have SplatBitSize == 8. However, NEON modified
4730 // immediate instructions others than VMOV do not support the 8-bit encoding
4731 // of a zero vector, and the default encoding of zero is supposed to be the
4732 // 32-bit version.
4733 if (SplatBits == 0)
4734 SplatBitSize = 32;
4735
Bob Wilson2e076c42009-06-22 23:27:02 +00004736 switch (SplatBitSize) {
4737 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004738 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004739 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004740 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004741 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004742 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004743 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004744 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004745 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004746
4747 case 16:
4748 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004749 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004750 if ((SplatBits & ~0xff) == 0) {
4751 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004752 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004753 Imm = SplatBits;
4754 break;
4755 }
4756 if ((SplatBits & ~0xff00) == 0) {
4757 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004758 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004759 Imm = SplatBits >> 8;
4760 break;
4761 }
4762 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004763
4764 case 32:
4765 // NEON's 32-bit VMOV supports splat values where:
4766 // * only one byte is nonzero, or
4767 // * the least significant byte is 0xff and the second byte is nonzero, or
4768 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004769 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004770 if ((SplatBits & ~0xff) == 0) {
4771 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004772 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004773 Imm = SplatBits;
4774 break;
4775 }
4776 if ((SplatBits & ~0xff00) == 0) {
4777 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004778 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004779 Imm = SplatBits >> 8;
4780 break;
4781 }
4782 if ((SplatBits & ~0xff0000) == 0) {
4783 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004784 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004785 Imm = SplatBits >> 16;
4786 break;
4787 }
4788 if ((SplatBits & ~0xff000000) == 0) {
4789 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004790 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004791 Imm = SplatBits >> 24;
4792 break;
4793 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004794
Owen Andersona4076922010-11-05 21:57:54 +00004795 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4796 if (type == OtherModImm) return SDValue();
4797
Bob Wilson2e076c42009-06-22 23:27:02 +00004798 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004799 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4800 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004801 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004802 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004803 break;
4804 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004805
4806 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004807 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4808 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004809 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004810 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004811 break;
4812 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004813
4814 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4815 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4816 // VMOV.I32. A (very) minor optimization would be to replicate the value
4817 // and fall through here to test for a valid 64-bit splat. But, then the
4818 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004819 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004820
4821 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004822 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004823 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004824 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004825 uint64_t BitMask = 0xff;
4826 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004827 unsigned ImmMask = 1;
4828 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004829 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004830 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004831 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004832 Imm |= ImmMask;
4833 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004834 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004835 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004836 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004837 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004838 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004839
Mehdi Aminiffc14022015-07-08 01:00:38 +00004840 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004841 // swap higher and lower 32 bit word
4842 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4843
Bob Wilson6eae5202010-06-11 21:34:50 +00004844 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004845 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004846 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004847 break;
4848 }
4849
Bob Wilson6eae5202010-06-11 21:34:50 +00004850 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004851 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004852 }
4853
Bob Wilsona3f19012010-07-13 21:16:48 +00004854 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004855 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004856}
4857
Lang Hames591cdaf2012-03-29 21:56:11 +00004858SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4859 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004860 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004861 return SDValue();
4862
Tim Northoverf79c3a52013-08-20 08:57:11 +00004863 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004864 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004865
Oliver Stannard51b1d462014-08-21 12:50:31 +00004866 // Use the default (constant pool) lowering for double constants when we have
4867 // an SP-only FPU
4868 if (IsDouble && Subtarget->isFPOnlySP())
4869 return SDValue();
4870
Lang Hames591cdaf2012-03-29 21:56:11 +00004871 // Try splatting with a VMOV.f32...
4872 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004873 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4874
Lang Hames591cdaf2012-03-29 21:56:11 +00004875 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004876 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4877 // We have code in place to select a valid ConstantFP already, no need to
4878 // do any mangling.
4879 return Op;
4880 }
4881
4882 // It's a float and we are trying to use NEON operations where
4883 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004884 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004885 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004886 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4887 NewVal);
4888 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004889 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004890 }
4891
Tim Northoverf79c3a52013-08-20 08:57:11 +00004892 // The rest of our options are NEON only, make sure that's allowed before
4893 // proceeding..
4894 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4895 return SDValue();
4896
Lang Hames591cdaf2012-03-29 21:56:11 +00004897 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004898 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4899
4900 // It wouldn't really be worth bothering for doubles except for one very
4901 // important value, which does happen to match: 0.0. So make sure we don't do
4902 // anything stupid.
4903 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4904 return SDValue();
4905
4906 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004907 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4908 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004909 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004910 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004911 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4912 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004913 if (IsDouble)
4914 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4915
4916 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004917 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4918 VecConstant);
4919 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004920 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004921 }
4922
4923 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004924 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004925 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004926 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004927 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004928 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004929
4930 if (IsDouble)
4931 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4932
4933 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004934 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4935 VecConstant);
4936 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004937 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004938 }
4939
4940 return SDValue();
4941}
4942
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004943// check if an VEXT instruction can handle the shuffle mask when the
4944// vector sources of the shuffle are the same.
4945static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4946 unsigned NumElts = VT.getVectorNumElements();
4947
4948 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4949 if (M[0] < 0)
4950 return false;
4951
4952 Imm = M[0];
4953
4954 // If this is a VEXT shuffle, the immediate value is the index of the first
4955 // element. The other shuffle indices must be the successive elements after
4956 // the first one.
4957 unsigned ExpectedElt = Imm;
4958 for (unsigned i = 1; i < NumElts; ++i) {
4959 // Increment the expected index. If it wraps around, just follow it
4960 // back to index zero and keep going.
4961 ++ExpectedElt;
4962 if (ExpectedElt == NumElts)
4963 ExpectedElt = 0;
4964
4965 if (M[i] < 0) continue; // ignore UNDEF indices
4966 if (ExpectedElt != static_cast<unsigned>(M[i]))
4967 return false;
4968 }
4969
4970 return true;
4971}
4972
Lang Hames591cdaf2012-03-29 21:56:11 +00004973
Benjamin Kramer339ced42012-01-15 13:16:05 +00004974static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004975 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004976 unsigned NumElts = VT.getVectorNumElements();
4977 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004978
4979 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4980 if (M[0] < 0)
4981 return false;
4982
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004983 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004984
4985 // If this is a VEXT shuffle, the immediate value is the index of the first
4986 // element. The other shuffle indices must be the successive elements after
4987 // the first one.
4988 unsigned ExpectedElt = Imm;
4989 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004990 // Increment the expected index. If it wraps around, it may still be
4991 // a VEXT but the source vectors must be swapped.
4992 ExpectedElt += 1;
4993 if (ExpectedElt == NumElts * 2) {
4994 ExpectedElt = 0;
4995 ReverseVEXT = true;
4996 }
4997
Bob Wilson411dfad2010-08-17 05:54:34 +00004998 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004999 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005000 return false;
5001 }
5002
5003 // Adjust the index value if the source operands will be swapped.
5004 if (ReverseVEXT)
5005 Imm -= NumElts;
5006
Bob Wilson32cd8552009-08-19 17:03:43 +00005007 return true;
5008}
5009
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005010/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5011/// instruction with the specified blocksize. (The order of the elements
5012/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005013static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005014 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5015 "Only possible block sizes for VREV are: 16, 32, 64");
5016
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005017 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005018 if (EltSz == 64)
5019 return false;
5020
5021 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005022 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005023 // If the first shuffle index is UNDEF, be optimistic.
5024 if (M[0] < 0)
5025 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005026
5027 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5028 return false;
5029
5030 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005031 if (M[i] < 0) continue; // ignore UNDEF indices
5032 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005033 return false;
5034 }
5035
5036 return true;
5037}
5038
Benjamin Kramer339ced42012-01-15 13:16:05 +00005039static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005040 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5041 // range, then 0 is placed into the resulting vector. So pretty much any mask
5042 // of 8 elements can work here.
5043 return VT == MVT::v8i8 && M.size() == 8;
5044}
5045
Benjamin Kramer339ced42012-01-15 13:16:05 +00005046static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005047 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5048 if (EltSz == 64)
5049 return false;
5050
Bob Wilsona7062312009-08-21 20:54:19 +00005051 unsigned NumElts = VT.getVectorNumElements();
5052 WhichResult = (M[0] == 0 ? 0 : 1);
5053 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005054 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
5055 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00005056 return false;
5057 }
5058 return true;
5059}
5060
Bob Wilson0bbd3072009-12-03 06:40:55 +00005061/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5062/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5063/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005064static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005065 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5066 if (EltSz == 64)
5067 return false;
5068
5069 unsigned NumElts = VT.getVectorNumElements();
5070 WhichResult = (M[0] == 0 ? 0 : 1);
5071 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005072 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
5073 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005074 return false;
5075 }
5076 return true;
5077}
5078
Benjamin Kramer339ced42012-01-15 13:16:05 +00005079static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005080 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5081 if (EltSz == 64)
5082 return false;
5083
Bob Wilsona7062312009-08-21 20:54:19 +00005084 unsigned NumElts = VT.getVectorNumElements();
5085 WhichResult = (M[0] == 0 ? 0 : 1);
5086 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005087 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00005088 if ((unsigned) M[i] != 2 * i + WhichResult)
5089 return false;
5090 }
5091
5092 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005093 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005094 return false;
5095
5096 return true;
5097}
5098
Bob Wilson0bbd3072009-12-03 06:40:55 +00005099/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5100/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5101/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005102static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005103 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5104 if (EltSz == 64)
5105 return false;
5106
5107 unsigned Half = VT.getVectorNumElements() / 2;
5108 WhichResult = (M[0] == 0 ? 0 : 1);
5109 for (unsigned j = 0; j != 2; ++j) {
5110 unsigned Idx = WhichResult;
5111 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005112 int MIdx = M[i + j * Half];
5113 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00005114 return false;
5115 Idx += 2;
5116 }
5117 }
5118
5119 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5120 if (VT.is64BitVector() && EltSz == 32)
5121 return false;
5122
5123 return true;
5124}
5125
Benjamin Kramer339ced42012-01-15 13:16:05 +00005126static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005127 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5128 if (EltSz == 64)
5129 return false;
5130
Bob Wilsona7062312009-08-21 20:54:19 +00005131 unsigned NumElts = VT.getVectorNumElements();
5132 WhichResult = (M[0] == 0 ? 0 : 1);
5133 unsigned Idx = WhichResult * NumElts / 2;
5134 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005135 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5136 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00005137 return false;
5138 Idx += 1;
5139 }
5140
5141 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005142 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005143 return false;
5144
5145 return true;
5146}
5147
Bob Wilson0bbd3072009-12-03 06:40:55 +00005148/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5149/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5150/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005151static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005152 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5153 if (EltSz == 64)
5154 return false;
5155
5156 unsigned NumElts = VT.getVectorNumElements();
5157 WhichResult = (M[0] == 0 ? 0 : 1);
5158 unsigned Idx = WhichResult * NumElts / 2;
5159 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005160 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5161 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005162 return false;
5163 Idx += 1;
5164 }
5165
5166 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5167 if (VT.is64BitVector() && EltSz == 32)
5168 return false;
5169
5170 return true;
5171}
5172
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005173/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5174/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5175static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5176 unsigned &WhichResult,
5177 bool &isV_UNDEF) {
5178 isV_UNDEF = false;
5179 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5180 return ARMISD::VTRN;
5181 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5182 return ARMISD::VUZP;
5183 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5184 return ARMISD::VZIP;
5185
5186 isV_UNDEF = true;
5187 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5188 return ARMISD::VTRN;
5189 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5190 return ARMISD::VUZP;
5191 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5192 return ARMISD::VZIP;
5193
5194 return 0;
5195}
5196
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005197/// \return true if this is a reverse operation on an vector.
5198static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5199 unsigned NumElts = VT.getVectorNumElements();
5200 // Make sure the mask has the right size.
5201 if (NumElts != M.size())
5202 return false;
5203
5204 // Look for <15, ..., 3, -1, 1, 0>.
5205 for (unsigned i = 0; i != NumElts; ++i)
5206 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5207 return false;
5208
5209 return true;
5210}
5211
Dale Johannesen2bff5052010-07-29 20:10:08 +00005212// If N is an integer constant that can be moved into a register in one
5213// instruction, return an SDValue of such a constant (will become a MOV
5214// instruction). Otherwise return null.
5215static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005216 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005217 uint64_t Val;
5218 if (!isa<ConstantSDNode>(N))
5219 return SDValue();
5220 Val = cast<ConstantSDNode>(N)->getZExtValue();
5221
5222 if (ST->isThumb1Only()) {
5223 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005224 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005225 } else {
5226 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005227 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005228 }
5229 return SDValue();
5230}
5231
Bob Wilson2e076c42009-06-22 23:27:02 +00005232// If this is a case we can't handle, return null and let the default
5233// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005234SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5235 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005236 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005237 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005238 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005239
5240 APInt SplatBits, SplatUndef;
5241 unsigned SplatBitSize;
5242 bool HasAnyUndefs;
5243 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005244 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005245 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005246 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005247 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005248 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005249 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005250 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005251 if (Val.getNode()) {
5252 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005253 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005254 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005255
5256 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005257 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005258 Val = isNEONModifiedImm(NegatedImm,
5259 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005260 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005261 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005262 if (Val.getNode()) {
5263 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005264 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005265 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005266
5267 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005268 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005269 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005270 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005271 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005272 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5273 }
5274 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005275 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005276 }
5277
Bob Wilson91fdf682010-05-22 00:23:12 +00005278 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005279 //
5280 // As an optimisation, even if more than one value is used it may be more
5281 // profitable to splat with one value then change some lanes.
5282 //
5283 // Heuristically we decide to do this if the vector has a "dominant" value,
5284 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005285 unsigned NumElts = VT.getVectorNumElements();
5286 bool isOnlyLowElement = true;
5287 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005288 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005289 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005290
5291 // Map of the number of times a particular SDValue appears in the
5292 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005293 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005294 SDValue Value;
5295 for (unsigned i = 0; i < NumElts; ++i) {
5296 SDValue V = Op.getOperand(i);
5297 if (V.getOpcode() == ISD::UNDEF)
5298 continue;
5299 if (i > 0)
5300 isOnlyLowElement = false;
5301 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5302 isConstant = false;
5303
James Molloy49bdbce2012-09-06 09:55:02 +00005304 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005305 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005306
James Molloy49bdbce2012-09-06 09:55:02 +00005307 // Is this value dominant? (takes up more than half of the lanes)
5308 if (++Count > (NumElts / 2)) {
5309 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005310 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005311 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005312 }
James Molloy49bdbce2012-09-06 09:55:02 +00005313 if (ValueCounts.size() != 1)
5314 usesOnlyOneValue = false;
5315 if (!Value.getNode() && ValueCounts.size() > 0)
5316 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005317
James Molloy49bdbce2012-09-06 09:55:02 +00005318 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005319 return DAG.getUNDEF(VT);
5320
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005321 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5322 // Keep going if we are hitting this case.
5323 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005324 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5325
Dale Johannesen2bff5052010-07-29 20:10:08 +00005326 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5327
Dale Johannesen710a2d92010-10-19 20:00:17 +00005328 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5329 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005330 if (hasDominantValue && EltSize <= 32) {
5331 if (!isConstant) {
5332 SDValue N;
5333
5334 // If we are VDUPing a value that comes directly from a vector, that will
5335 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005336 // just use VDUPLANE. We can only do this if the lane being extracted
5337 // is at a constant index, as the VDUP from lane instructions only have
5338 // constant-index forms.
5339 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5340 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005341 // We need to create a new undef vector to use for the VDUPLANE if the
5342 // size of the vector from which we get the value is different than the
5343 // size of the vector that we need to create. We will insert the element
5344 // such that the register coalescer will remove unnecessary copies.
5345 if (VT != Value->getOperand(0).getValueType()) {
5346 ConstantSDNode *constIndex;
5347 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5348 assert(constIndex && "The index is not a constant!");
5349 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5350 VT.getVectorNumElements();
5351 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5352 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005353 Value, DAG.getConstant(index, dl, MVT::i32)),
5354 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005355 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005356 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005357 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005358 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005359 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5360
5361 if (!usesOnlyOneValue) {
5362 // The dominant value was splatted as 'N', but we now have to insert
5363 // all differing elements.
5364 for (unsigned I = 0; I < NumElts; ++I) {
5365 if (Op.getOperand(I) == Value)
5366 continue;
5367 SmallVector<SDValue, 3> Ops;
5368 Ops.push_back(N);
5369 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005370 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005371 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005372 }
5373 }
5374 return N;
5375 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005376 if (VT.getVectorElementType().isFloatingPoint()) {
5377 SmallVector<SDValue, 8> Ops;
5378 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005379 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005380 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005381 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005382 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005383 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5384 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005385 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005386 }
James Molloy49bdbce2012-09-06 09:55:02 +00005387 if (usesOnlyOneValue) {
5388 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5389 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005390 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005391 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005392 }
5393
5394 // If all elements are constants and the case above didn't get hit, fall back
5395 // to the default expansion, which will generate a load from the constant
5396 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005397 if (isConstant)
5398 return SDValue();
5399
Bob Wilson6f2b8962011-01-07 21:37:30 +00005400 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5401 if (NumElts >= 4) {
5402 SDValue shuffle = ReconstructShuffle(Op, DAG);
5403 if (shuffle != SDValue())
5404 return shuffle;
5405 }
5406
Bob Wilson91fdf682010-05-22 00:23:12 +00005407 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005408 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5409 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005410 if (EltSize >= 32) {
5411 // Do the expansion with floating-point types, since that is what the VFP
5412 // registers are defined to use, and since i64 is not legal.
5413 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5414 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005415 SmallVector<SDValue, 8> Ops;
5416 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005417 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005418 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005419 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005420 }
5421
Jim Grosbach24e102a2013-07-08 18:18:52 +00005422 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5423 // know the default expansion would otherwise fall back on something even
5424 // worse. For a vector with one or two non-undef values, that's
5425 // scalar_to_vector for the elements followed by a shuffle (provided the
5426 // shuffle is valid for the target) and materialization element by element
5427 // on the stack followed by a load for everything else.
5428 if (!isConstant && !usesOnlyOneValue) {
5429 SDValue Vec = DAG.getUNDEF(VT);
5430 for (unsigned i = 0 ; i < NumElts; ++i) {
5431 SDValue V = Op.getOperand(i);
5432 if (V.getOpcode() == ISD::UNDEF)
5433 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005434 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005435 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5436 }
5437 return Vec;
5438 }
5439
Bob Wilson2e076c42009-06-22 23:27:02 +00005440 return SDValue();
5441}
5442
Bob Wilson6f2b8962011-01-07 21:37:30 +00005443// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005444// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005445SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5446 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005447 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005448 EVT VT = Op.getValueType();
5449 unsigned NumElts = VT.getVectorNumElements();
5450
5451 SmallVector<SDValue, 2> SourceVecs;
5452 SmallVector<unsigned, 2> MinElts;
5453 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005454
Bob Wilson6f2b8962011-01-07 21:37:30 +00005455 for (unsigned i = 0; i < NumElts; ++i) {
5456 SDValue V = Op.getOperand(i);
5457 if (V.getOpcode() == ISD::UNDEF)
5458 continue;
5459 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5460 // A shuffle can only come from building a vector from various
5461 // elements of other vectors.
5462 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005463 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5464 VT.getVectorElementType()) {
5465 // This code doesn't know how to handle shuffles where the vector
5466 // element types do not match (this happens because type legalization
5467 // promotes the return type of EXTRACT_VECTOR_ELT).
5468 // FIXME: It might be appropriate to extend this code to handle
5469 // mismatched types.
5470 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005471 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005472
Bob Wilson6f2b8962011-01-07 21:37:30 +00005473 // Record this extraction against the appropriate vector if possible...
5474 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005475 // If the element number isn't a constant, we can't effectively
5476 // analyze what's going on.
5477 if (!isa<ConstantSDNode>(V.getOperand(1)))
5478 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005479 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5480 bool FoundSource = false;
5481 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5482 if (SourceVecs[j] == SourceVec) {
5483 if (MinElts[j] > EltNo)
5484 MinElts[j] = EltNo;
5485 if (MaxElts[j] < EltNo)
5486 MaxElts[j] = EltNo;
5487 FoundSource = true;
5488 break;
5489 }
5490 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005491
Bob Wilson6f2b8962011-01-07 21:37:30 +00005492 // Or record a new source if not...
5493 if (!FoundSource) {
5494 SourceVecs.push_back(SourceVec);
5495 MinElts.push_back(EltNo);
5496 MaxElts.push_back(EltNo);
5497 }
5498 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005499
Bob Wilson6f2b8962011-01-07 21:37:30 +00005500 // Currently only do something sane when at most two source vectors
5501 // involved.
5502 if (SourceVecs.size() > 2)
5503 return SDValue();
5504
5505 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5506 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005507
Bob Wilson6f2b8962011-01-07 21:37:30 +00005508 // This loop extracts the usage patterns of the source vectors
5509 // and prepares appropriate SDValues for a shuffle if possible.
5510 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5511 if (SourceVecs[i].getValueType() == VT) {
5512 // No VEXT necessary
5513 ShuffleSrcs[i] = SourceVecs[i];
5514 VEXTOffsets[i] = 0;
5515 continue;
5516 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5517 // It probably isn't worth padding out a smaller vector just to
5518 // break it down again in a shuffle.
5519 return SDValue();
5520 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005521
Bob Wilson6f2b8962011-01-07 21:37:30 +00005522 // Since only 64-bit and 128-bit vectors are legal on ARM and
5523 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005524 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5525 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005526
Bob Wilson6f2b8962011-01-07 21:37:30 +00005527 if (MaxElts[i] - MinElts[i] >= NumElts) {
5528 // Span too large for a VEXT to cope
5529 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005530 }
5531
Bob Wilson6f2b8962011-01-07 21:37:30 +00005532 if (MinElts[i] >= NumElts) {
5533 // The extraction can just take the second half
5534 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005535 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5536 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005537 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005538 } else if (MaxElts[i] < NumElts) {
5539 // The extraction can just take the first half
5540 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005541 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5542 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005543 DAG.getIntPtrConstant(0, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005544 } else {
5545 // An actual VEXT is needed
5546 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005547 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5548 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005549 DAG.getIntPtrConstant(0, dl));
Eric Christopher2af95512011-01-14 23:50:53 +00005550 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5551 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005552 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005553 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005554 DAG.getConstant(VEXTOffsets[i], dl,
5555 MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005556 }
5557 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005558
Bob Wilson6f2b8962011-01-07 21:37:30 +00005559 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005560
Bob Wilson6f2b8962011-01-07 21:37:30 +00005561 for (unsigned i = 0; i < NumElts; ++i) {
5562 SDValue Entry = Op.getOperand(i);
5563 if (Entry.getOpcode() == ISD::UNDEF) {
5564 Mask.push_back(-1);
5565 continue;
5566 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005567
Bob Wilson6f2b8962011-01-07 21:37:30 +00005568 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005569 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5570 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005571 if (ExtractVec == SourceVecs[0]) {
5572 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5573 } else {
5574 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5575 }
5576 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005577
Bob Wilson6f2b8962011-01-07 21:37:30 +00005578 // Final check before we try to produce nonsense...
5579 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005580 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5581 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005582
Bob Wilson6f2b8962011-01-07 21:37:30 +00005583 return SDValue();
5584}
5585
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005586/// isShuffleMaskLegal - Targets can use this to indicate that they only
5587/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5588/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5589/// are assumed to be legal.
5590bool
5591ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5592 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005593 if (VT.getVectorNumElements() == 4 &&
5594 (VT.is128BitVector() || VT.is64BitVector())) {
5595 unsigned PFIndexes[4];
5596 for (unsigned i = 0; i != 4; ++i) {
5597 if (M[i] < 0)
5598 PFIndexes[i] = 8;
5599 else
5600 PFIndexes[i] = M[i];
5601 }
5602
5603 // Compute the index in the perfect shuffle table.
5604 unsigned PFTableIndex =
5605 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5606 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5607 unsigned Cost = (PFEntry >> 30);
5608
5609 if (Cost <= 4)
5610 return true;
5611 }
5612
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005613 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005614 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005615
Bob Wilson846bd792010-06-07 23:53:38 +00005616 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5617 return (EltSize >= 32 ||
5618 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005619 isVREVMask(M, VT, 64) ||
5620 isVREVMask(M, VT, 32) ||
5621 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005622 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005623 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005624 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005625 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005626}
5627
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005628/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5629/// the specified operations to build the shuffle.
5630static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5631 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005632 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005633 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5634 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5635 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5636
5637 enum {
5638 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5639 OP_VREV,
5640 OP_VDUP0,
5641 OP_VDUP1,
5642 OP_VDUP2,
5643 OP_VDUP3,
5644 OP_VEXT1,
5645 OP_VEXT2,
5646 OP_VEXT3,
5647 OP_VUZPL, // VUZP, left result
5648 OP_VUZPR, // VUZP, right result
5649 OP_VZIPL, // VZIP, left result
5650 OP_VZIPR, // VZIP, right result
5651 OP_VTRNL, // VTRN, left result
5652 OP_VTRNR // VTRN, right result
5653 };
5654
5655 if (OpNum == OP_COPY) {
5656 if (LHSID == (1*9+2)*9+3) return LHS;
5657 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5658 return RHS;
5659 }
5660
5661 SDValue OpLHS, OpRHS;
5662 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5663 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5664 EVT VT = OpLHS.getValueType();
5665
5666 switch (OpNum) {
5667 default: llvm_unreachable("Unknown shuffle opcode!");
5668 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005669 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005670 if (VT.getVectorElementType() == MVT::i32 ||
5671 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005672 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5673 // vrev <4 x i16> -> VREV32
5674 if (VT.getVectorElementType() == MVT::i16)
5675 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5676 // vrev <4 x i8> -> VREV16
5677 assert(VT.getVectorElementType() == MVT::i8);
5678 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005679 case OP_VDUP0:
5680 case OP_VDUP1:
5681 case OP_VDUP2:
5682 case OP_VDUP3:
5683 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005684 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005685 case OP_VEXT1:
5686 case OP_VEXT2:
5687 case OP_VEXT3:
5688 return DAG.getNode(ARMISD::VEXT, dl, VT,
5689 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005690 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005691 case OP_VUZPL:
5692 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005693 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005694 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5695 case OP_VZIPL:
5696 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005697 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005698 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5699 case OP_VTRNL:
5700 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005701 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5702 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005703 }
5704}
5705
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005706static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005707 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005708 SelectionDAG &DAG) {
5709 // Check to see if we can use the VTBL instruction.
5710 SDValue V1 = Op.getOperand(0);
5711 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005712 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005713
5714 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005715 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005716 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005717 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005718
5719 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5720 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005721 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005722
Owen Anderson77aa2662011-04-05 21:48:57 +00005723 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005724 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005725}
5726
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005727static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5728 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005729 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005730 SDValue OpLHS = Op.getOperand(0);
5731 EVT VT = OpLHS.getValueType();
5732
5733 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5734 "Expect an v8i16/v16i8 type");
5735 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5736 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5737 // extract the first 8 bytes into the top double word and the last 8 bytes
5738 // into the bottom double word. The v8i16 case is similar.
5739 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5740 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005741 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005742}
5743
Bob Wilson2e076c42009-06-22 23:27:02 +00005744static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005745 SDValue V1 = Op.getOperand(0);
5746 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005747 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005748 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005749 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005750
Bob Wilsonc6800b52009-08-13 02:13:04 +00005751 // Convert shuffles that are directly supported on NEON to target-specific
5752 // DAG nodes, instead of keeping them as shuffles and matching them again
5753 // during code selection. This is more efficient and avoids the possibility
5754 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005755 // FIXME: floating-point vectors should be canonicalized to integer vectors
5756 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005757 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005758
Bob Wilson846bd792010-06-07 23:53:38 +00005759 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5760 if (EltSize <= 32) {
5761 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5762 int Lane = SVN->getSplatIndex();
5763 // If this is undef splat, generate it via "just" vdup, if possible.
5764 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005765
Dan Gohman198b7ff2011-11-03 21:49:52 +00005766 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005767 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5768 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5769 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005770 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5771 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5772 // reaches it).
5773 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5774 !isa<ConstantSDNode>(V1.getOperand(0))) {
5775 bool IsScalarToVector = true;
5776 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5777 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5778 IsScalarToVector = false;
5779 break;
5780 }
5781 if (IsScalarToVector)
5782 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5783 }
Bob Wilson846bd792010-06-07 23:53:38 +00005784 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005785 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005786 }
Bob Wilson846bd792010-06-07 23:53:38 +00005787
5788 bool ReverseVEXT;
5789 unsigned Imm;
5790 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5791 if (ReverseVEXT)
5792 std::swap(V1, V2);
5793 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005794 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005795 }
5796
5797 if (isVREVMask(ShuffleMask, VT, 64))
5798 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5799 if (isVREVMask(ShuffleMask, VT, 32))
5800 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5801 if (isVREVMask(ShuffleMask, VT, 16))
5802 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5803
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005804 if (V2->getOpcode() == ISD::UNDEF &&
5805 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5806 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005807 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005808 }
5809
Bob Wilson846bd792010-06-07 23:53:38 +00005810 // Check for Neon shuffles that modify both input vectors in place.
5811 // If both results are used, i.e., if there are two shuffles with the same
5812 // source operands and with masks corresponding to both results of one of
5813 // these operations, DAG memoization will ensure that a single node is
5814 // used for both shuffles.
5815 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005816 bool isV_UNDEF;
5817 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5818 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5819 if (isV_UNDEF)
5820 V2 = V1;
5821 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5822 .getValue(WhichResult);
5823 }
Bob Wilson846bd792010-06-07 23:53:38 +00005824
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005825 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5826 // shuffles that produce a result larger than their operands with:
5827 // shuffle(concat(v1, undef), concat(v2, undef))
5828 // ->
5829 // shuffle(concat(v1, v2), undef)
5830 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5831 //
5832 // This is useful in the general case, but there are special cases where
5833 // native shuffles produce larger results: the two-result ops.
5834 //
5835 // Look through the concat when lowering them:
5836 // shuffle(concat(v1, v2), undef)
5837 // ->
5838 // concat(VZIP(v1, v2):0, :1)
5839 //
5840 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5841 V2->getOpcode() == ISD::UNDEF) {
5842 SDValue SubV1 = V1->getOperand(0);
5843 SDValue SubV2 = V1->getOperand(1);
5844 EVT SubVT = SubV1.getValueType();
5845
5846 // We expect these to have been canonicalized to -1.
5847 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5848 return i < (int)VT.getVectorNumElements();
5849 }) && "Unexpected shuffle index into UNDEF operand!");
5850
5851 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5852 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5853 if (isV_UNDEF)
5854 SubV2 = SubV1;
5855 assert((WhichResult == 0) &&
5856 "In-place shuffle of concat can only have one result!");
5857 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
5858 SubV1, SubV2);
5859 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
5860 Res.getValue(1));
5861 }
5862 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00005863 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005864
Bob Wilsona7062312009-08-21 20:54:19 +00005865 // If the shuffle is not directly supported and it has 4 elements, use
5866 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005867 unsigned NumElts = VT.getVectorNumElements();
5868 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005869 unsigned PFIndexes[4];
5870 for (unsigned i = 0; i != 4; ++i) {
5871 if (ShuffleMask[i] < 0)
5872 PFIndexes[i] = 8;
5873 else
5874 PFIndexes[i] = ShuffleMask[i];
5875 }
5876
5877 // Compute the index in the perfect shuffle table.
5878 unsigned PFTableIndex =
5879 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005880 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5881 unsigned Cost = (PFEntry >> 30);
5882
5883 if (Cost <= 4)
5884 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5885 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005886
Bob Wilsond8a9a042010-06-04 00:04:02 +00005887 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005888 if (EltSize >= 32) {
5889 // Do the expansion with floating-point types, since that is what the VFP
5890 // registers are defined to use, and since i64 is not legal.
5891 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5892 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005893 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5894 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005895 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005896 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005897 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005898 Ops.push_back(DAG.getUNDEF(EltVT));
5899 else
5900 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5901 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5902 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005903 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005904 }
Craig Topper48d114b2014-04-26 18:35:24 +00005905 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005906 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005907 }
5908
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005909 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5910 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5911
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005912 if (VT == MVT::v8i8) {
5913 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5914 if (NewOp.getNode())
5915 return NewOp;
5916 }
5917
Bob Wilson6f34e272009-08-14 05:16:33 +00005918 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005919}
5920
Eli Friedmana5e244c2011-10-24 23:08:52 +00005921static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5922 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5923 SDValue Lane = Op.getOperand(2);
5924 if (!isa<ConstantSDNode>(Lane))
5925 return SDValue();
5926
5927 return Op;
5928}
5929
Bob Wilson2e076c42009-06-22 23:27:02 +00005930static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005931 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005932 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005933 if (!isa<ConstantSDNode>(Lane))
5934 return SDValue();
5935
5936 SDValue Vec = Op.getOperand(0);
5937 if (Op.getValueType() == MVT::i32 &&
5938 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005939 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005940 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5941 }
5942
5943 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005944}
5945
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005946static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5947 // The only time a CONCAT_VECTORS operation can have legal types is when
5948 // two 64-bit vectors are concatenated to a 128-bit vector.
5949 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5950 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005951 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005952 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005953 SDValue Op0 = Op.getOperand(0);
5954 SDValue Op1 = Op.getOperand(1);
5955 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005956 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005957 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005958 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005959 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005960 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005961 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005962 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00005963 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005964}
5965
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005966/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5967/// element has been zero/sign-extended, depending on the isSigned parameter,
5968/// from an integer type half its size.
5969static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5970 bool isSigned) {
5971 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5972 EVT VT = N->getValueType(0);
5973 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5974 SDNode *BVN = N->getOperand(0).getNode();
5975 if (BVN->getValueType(0) != MVT::v4i32 ||
5976 BVN->getOpcode() != ISD::BUILD_VECTOR)
5977 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00005978 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005979 unsigned HiElt = 1 - LoElt;
5980 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5981 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5982 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5983 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5984 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5985 return false;
5986 if (isSigned) {
5987 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5988 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5989 return true;
5990 } else {
5991 if (Hi0->isNullValue() && Hi1->isNullValue())
5992 return true;
5993 }
5994 return false;
5995 }
5996
5997 if (N->getOpcode() != ISD::BUILD_VECTOR)
5998 return false;
5999
6000 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6001 SDNode *Elt = N->getOperand(i).getNode();
6002 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
6003 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
6004 unsigned HalfSize = EltSize / 2;
6005 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006006 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006007 return false;
6008 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006009 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006010 return false;
6011 }
6012 continue;
6013 }
6014 return false;
6015 }
6016
6017 return true;
6018}
6019
6020/// isSignExtended - Check if a node is a vector value that is sign-extended
6021/// or a constant BUILD_VECTOR with sign-extended elements.
6022static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6023 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6024 return true;
6025 if (isExtendedBUILD_VECTOR(N, DAG, true))
6026 return true;
6027 return false;
6028}
6029
6030/// isZeroExtended - Check if a node is a vector value that is zero-extended
6031/// or a constant BUILD_VECTOR with zero-extended elements.
6032static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6033 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6034 return true;
6035 if (isExtendedBUILD_VECTOR(N, DAG, false))
6036 return true;
6037 return false;
6038}
6039
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006040static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6041 if (OrigVT.getSizeInBits() >= 64)
6042 return OrigVT;
6043
6044 assert(OrigVT.isSimple() && "Expecting a simple value type");
6045
6046 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6047 switch (OrigSimpleTy) {
6048 default: llvm_unreachable("Unexpected Vector Type");
6049 case MVT::v2i8:
6050 case MVT::v2i16:
6051 return MVT::v2i32;
6052 case MVT::v4i8:
6053 return MVT::v4i16;
6054 }
6055}
6056
Sebastian Popa204f722012-11-30 19:08:04 +00006057/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6058/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6059/// We insert the required extension here to get the vector to fill a D register.
6060static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6061 const EVT &OrigTy,
6062 const EVT &ExtTy,
6063 unsigned ExtOpcode) {
6064 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6065 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6066 // 64-bits we need to insert a new extension so that it will be 64-bits.
6067 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6068 if (OrigTy.getSizeInBits() >= 64)
6069 return N;
6070
6071 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006072 EVT NewVT = getExtensionTo64Bits(OrigTy);
6073
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006075}
6076
6077/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6078/// does not do any sign/zero extension. If the original vector is less
6079/// than 64 bits, an appropriate extension will be added after the load to
6080/// reach a total size of 64 bits. We have to add the extension separately
6081/// because ARM does not have a sign/zero extending load for vectors.
6082static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006083 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6084
6085 // The load already has the right type.
6086 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006087 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006088 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6089 LD->isNonTemporal(), LD->isInvariant(),
6090 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006091
6092 // We need to create a zextload/sextload. We cannot just create a load
6093 // followed by a zext/zext node because LowerMUL is also run during normal
6094 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006095 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006096 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006097 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006098 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006099}
6100
6101/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6102/// extending load, or BUILD_VECTOR with extended elements, return the
6103/// unextended value. The unextended vector should be 64 bits so that it can
6104/// be used as an operand to a VMULL instruction. If the original vector size
6105/// before extension is less than 64 bits we add a an extension to resize
6106/// the vector to 64 bits.
6107static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006108 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006109 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6110 N->getOperand(0)->getValueType(0),
6111 N->getValueType(0),
6112 N->getOpcode());
6113
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006114 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006115 return SkipLoadExtensionForVMULL(LD, DAG);
6116
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006117 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6118 // have been legalized as a BITCAST from v4i32.
6119 if (N->getOpcode() == ISD::BITCAST) {
6120 SDNode *BVN = N->getOperand(0).getNode();
6121 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6122 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006123 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006124 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006125 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6126 }
6127 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6128 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6129 EVT VT = N->getValueType(0);
6130 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6131 unsigned NumElts = VT.getVectorNumElements();
6132 MVT TruncVT = MVT::getIntegerVT(EltSize);
6133 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006134 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006135 for (unsigned i = 0; i != NumElts; ++i) {
6136 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6137 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006138 // Element types smaller than 32 bits are not legal, so use i32 elements.
6139 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006140 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006141 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006142 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006143 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006144}
6145
Evan Chenge2086e72011-03-29 01:56:09 +00006146static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6147 unsigned Opcode = N->getOpcode();
6148 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6149 SDNode *N0 = N->getOperand(0).getNode();
6150 SDNode *N1 = N->getOperand(1).getNode();
6151 return N0->hasOneUse() && N1->hasOneUse() &&
6152 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6153 }
6154 return false;
6155}
6156
6157static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6158 unsigned Opcode = N->getOpcode();
6159 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6160 SDNode *N0 = N->getOperand(0).getNode();
6161 SDNode *N1 = N->getOperand(1).getNode();
6162 return N0->hasOneUse() && N1->hasOneUse() &&
6163 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6164 }
6165 return false;
6166}
6167
Bob Wilson38ab35a2010-09-01 23:50:19 +00006168static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6169 // Multiplications are only custom-lowered for 128-bit vectors so that
6170 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6171 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006172 assert(VT.is128BitVector() && VT.isInteger() &&
6173 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006174 SDNode *N0 = Op.getOperand(0).getNode();
6175 SDNode *N1 = Op.getOperand(1).getNode();
6176 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006177 bool isMLA = false;
6178 bool isN0SExt = isSignExtended(N0, DAG);
6179 bool isN1SExt = isSignExtended(N1, DAG);
6180 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006181 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006182 else {
6183 bool isN0ZExt = isZeroExtended(N0, DAG);
6184 bool isN1ZExt = isZeroExtended(N1, DAG);
6185 if (isN0ZExt && isN1ZExt)
6186 NewOpc = ARMISD::VMULLu;
6187 else if (isN1SExt || isN1ZExt) {
6188 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6189 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6190 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6191 NewOpc = ARMISD::VMULLs;
6192 isMLA = true;
6193 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6194 NewOpc = ARMISD::VMULLu;
6195 isMLA = true;
6196 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6197 std::swap(N0, N1);
6198 NewOpc = ARMISD::VMULLu;
6199 isMLA = true;
6200 }
6201 }
6202
6203 if (!NewOpc) {
6204 if (VT == MVT::v2i64)
6205 // Fall through to expand this. It is not legal.
6206 return SDValue();
6207 else
6208 // Other vector multiplications are legal.
6209 return Op;
6210 }
6211 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006212
6213 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006214 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006215 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006216 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006217 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006218 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006219 assert(Op0.getValueType().is64BitVector() &&
6220 Op1.getValueType().is64BitVector() &&
6221 "unexpected types for extended operands to VMULL");
6222 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6223 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006224
Evan Chenge2086e72011-03-29 01:56:09 +00006225 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6226 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6227 // vmull q0, d4, d6
6228 // vmlal q0, d5, d6
6229 // is faster than
6230 // vaddl q0, d4, d5
6231 // vmovl q1, d6
6232 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006233 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6234 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006235 EVT Op1VT = Op1.getValueType();
6236 return DAG.getNode(N0->getOpcode(), DL, VT,
6237 DAG.getNode(NewOpc, DL, VT,
6238 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6239 DAG.getNode(NewOpc, DL, VT,
6240 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006241}
6242
Owen Anderson77aa2662011-04-05 21:48:57 +00006243static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006244LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006245 // Convert to float
6246 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6247 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6248 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6249 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6250 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6251 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6252 // Get reciprocal estimate.
6253 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006254 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006255 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6256 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006257 // Because char has a smaller range than uchar, we can actually get away
6258 // without any newton steps. This requires that we use a weird bias
6259 // of 0xb000, however (again, this has been exhaustively tested).
6260 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6261 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6262 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006263 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006264 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6265 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6266 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6267 // Convert back to short.
6268 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6269 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6270 return X;
6271}
6272
Owen Anderson77aa2662011-04-05 21:48:57 +00006273static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006274LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006275 SDValue N2;
6276 // Convert to float.
6277 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6278 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6279 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6280 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6281 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6282 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006283
Nate Begemanfa62d502011-02-11 20:53:29 +00006284 // Use reciprocal estimate and one refinement step.
6285 // float4 recip = vrecpeq_f32(yf);
6286 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006287 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006288 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6289 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006290 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006291 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006292 N1, N2);
6293 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6294 // Because short has a smaller range than ushort, we can actually get away
6295 // with only a single newton step. This requires that we use a weird bias
6296 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006297 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006298 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6299 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006300 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006301 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6302 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6303 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6304 // Convert back to integer and return.
6305 // return vmovn_s32(vcvt_s32_f32(result));
6306 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6307 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6308 return N0;
6309}
6310
6311static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6312 EVT VT = Op.getValueType();
6313 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6314 "unexpected type for custom-lowering ISD::SDIV");
6315
Andrew Trickef9de2a2013-05-25 02:42:55 +00006316 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006317 SDValue N0 = Op.getOperand(0);
6318 SDValue N1 = Op.getOperand(1);
6319 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006320
Nate Begemanfa62d502011-02-11 20:53:29 +00006321 if (VT == MVT::v8i8) {
6322 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6323 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006324
Nate Begemanfa62d502011-02-11 20:53:29 +00006325 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006326 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006327 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006328 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006329 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006330 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006331 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006332 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006333
6334 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6335 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6336
6337 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6338 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006339
Nate Begemanfa62d502011-02-11 20:53:29 +00006340 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6341 return N0;
6342 }
6343 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6344}
6345
6346static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6347 EVT VT = Op.getValueType();
6348 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6349 "unexpected type for custom-lowering ISD::UDIV");
6350
Andrew Trickef9de2a2013-05-25 02:42:55 +00006351 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006352 SDValue N0 = Op.getOperand(0);
6353 SDValue N1 = Op.getOperand(1);
6354 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006355
Nate Begemanfa62d502011-02-11 20:53:29 +00006356 if (VT == MVT::v8i8) {
6357 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6358 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006359
Nate Begemanfa62d502011-02-11 20:53:29 +00006360 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006361 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006362 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006363 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006364 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006365 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006366 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006367 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006368
Nate Begemanfa62d502011-02-11 20:53:29 +00006369 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6370 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006371
Nate Begemanfa62d502011-02-11 20:53:29 +00006372 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6373 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006374
6375 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006376 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6377 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006378 N0);
6379 return N0;
6380 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006381
Nate Begemanfa62d502011-02-11 20:53:29 +00006382 // v4i16 sdiv ... Convert to float.
6383 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6384 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6385 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6386 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6387 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006388 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006389
6390 // Use reciprocal estimate and two refinement steps.
6391 // float4 recip = vrecpeq_f32(yf);
6392 // recip *= vrecpsq_f32(yf, recip);
6393 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006394 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006395 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6396 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006397 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006398 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006399 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006400 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006401 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006402 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006403 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006404 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6405 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6406 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6407 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006408 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006409 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6410 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006411 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006412 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6413 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6414 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6415 // Convert back to integer and return.
6416 // return vmovn_u32(vcvt_s32_f32(result));
6417 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6418 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6419 return N0;
6420}
6421
Evan Chenge8916542011-08-30 01:34:54 +00006422static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6423 EVT VT = Op.getNode()->getValueType(0);
6424 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6425
6426 unsigned Opc;
6427 bool ExtraOp = false;
6428 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006429 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006430 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6431 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6432 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6433 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6434 }
6435
6436 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006437 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006438 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006439 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006440 Op.getOperand(1), Op.getOperand(2));
6441}
6442
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006443SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6444 assert(Subtarget->isTargetDarwin());
6445
6446 // For iOS, we want to call an alternative entry point: __sincos_stret,
6447 // return values are passed via sret.
6448 SDLoc dl(Op);
6449 SDValue Arg = Op.getOperand(0);
6450 EVT ArgVT = Arg.getValueType();
6451 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006452 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006453
6454 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006455
6456 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006457 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006458
6459 // Create stack object for sret.
Mehdi Amini44ede332015-07-09 02:09:04 +00006460 auto &DL = DAG.getDataLayout();
6461 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6462 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006463 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00006464 SDValue SRet = DAG.getFrameIndex(FrameIdx, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006465
6466 ArgListTy Args;
6467 ArgListEntry Entry;
6468
6469 Entry.Node = SRet;
6470 Entry.Ty = RetTy->getPointerTo();
6471 Entry.isSExt = false;
6472 Entry.isZExt = false;
6473 Entry.isSRet = true;
6474 Args.push_back(Entry);
6475
6476 Entry.Node = Arg;
6477 Entry.Ty = ArgTy;
6478 Entry.isSExt = false;
6479 Entry.isZExt = false;
6480 Args.push_back(Entry);
6481
6482 const char *LibcallName = (ArgVT == MVT::f64)
6483 ? "__sincos_stret" : "__sincosf_stret";
Mehdi Amini44ede332015-07-09 02:09:04 +00006484 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006485
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006486 TargetLowering::CallLoweringInfo CLI(DAG);
6487 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6488 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006489 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006490 .setDiscardResult();
6491
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006492 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6493
6494 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6495 MachinePointerInfo(), false, false, false, 0);
6496
6497 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006498 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006499 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006500 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6501 MachinePointerInfo(), false, false, false, 0);
6502
6503 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6504 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6505 LoadSin.getValue(0), LoadCos.getValue(0));
6506}
6507
Eli Friedman10f9ce22011-09-15 22:26:18 +00006508static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006509 // Monotonic load/store is legal for all targets
6510 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6511 return Op;
6512
Alp Tokercb402912014-01-24 17:20:08 +00006513 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006514 // dmb or equivalent available.
6515 return SDValue();
6516}
6517
Tim Northoverbc933082013-05-23 19:11:20 +00006518static void ReplaceREADCYCLECOUNTER(SDNode *N,
6519 SmallVectorImpl<SDValue> &Results,
6520 SelectionDAG &DAG,
6521 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006522 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006523 SDValue Cycles32, OutChain;
6524
6525 if (Subtarget->hasPerfMon()) {
6526 // Under Power Management extensions, the cycle-count is:
6527 // mrc p15, #0, <Rt>, c9, c13, #0
6528 SDValue Ops[] = { N->getOperand(0), // Chain
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006529 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6530 DAG.getConstant(15, DL, MVT::i32),
6531 DAG.getConstant(0, DL, MVT::i32),
6532 DAG.getConstant(9, DL, MVT::i32),
6533 DAG.getConstant(13, DL, MVT::i32),
6534 DAG.getConstant(0, DL, MVT::i32)
Tim Northoverbc933082013-05-23 19:11:20 +00006535 };
6536
6537 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006538 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006539 OutChain = Cycles32.getValue(1);
6540 } else {
6541 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6542 // there are older ARM CPUs that have implementation-specific ways of
6543 // obtaining this information (FIXME!).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006544 Cycles32 = DAG.getConstant(0, DL, MVT::i32);
Tim Northoverbc933082013-05-23 19:11:20 +00006545 OutChain = DAG.getEntryNode();
6546 }
6547
6548
6549 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006550 Cycles32, DAG.getConstant(0, DL, MVT::i32));
Tim Northoverbc933082013-05-23 19:11:20 +00006551 Results.push_back(Cycles64);
6552 Results.push_back(OutChain);
6553}
6554
Dan Gohman21cea8a2010-04-17 15:26:15 +00006555SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006556 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006557 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006558 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006559 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006560 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006561 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006562 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6563 default: llvm_unreachable("unknown object format");
6564 case Triple::COFF:
6565 return LowerGlobalAddressWindows(Op, DAG);
6566 case Triple::ELF:
6567 return LowerGlobalAddressELF(Op, DAG);
6568 case Triple::MachO:
6569 return LowerGlobalAddressDarwin(Op, DAG);
6570 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006571 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006572 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006573 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6574 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006575 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006576 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006577 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006578 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006579 case ISD::SINT_TO_FP:
6580 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6581 case ISD::FP_TO_SINT:
6582 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006583 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006584 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006585 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006586 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006587 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006588 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00006589 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006590 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6591 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006592 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006593 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006594 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006595 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006596 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006597 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006598 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006599 case ISD::CTTZ:
6600 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006601 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006602 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006603 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006604 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006605 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006606 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006607 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006608 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006609 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006610 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006611 case ISD::SDIV: return LowerSDIV(Op, DAG);
6612 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006613 case ISD::ADDC:
6614 case ISD::ADDE:
6615 case ISD::SUBC:
6616 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006617 case ISD::SADDO:
6618 case ISD::UADDO:
6619 case ISD::SSUBO:
6620 case ISD::USUBO:
6621 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006622 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006623 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006624 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006625 case ISD::SDIVREM:
6626 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006627 case ISD::DYNAMIC_STACKALLOC:
6628 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6629 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6630 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006631 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6632 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006633 }
Evan Cheng10043e22007-01-19 07:51:42 +00006634}
6635
Duncan Sands6ed40142008-12-01 11:39:25 +00006636/// ReplaceNodeResults - Replace the results of node with an illegal result
6637/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006638void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6639 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006640 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006641 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006642 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006643 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006644 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006645 case ISD::READ_REGISTER:
6646 ExpandREAD_REGISTER(N, Results, DAG);
6647 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006648 case ISD::BITCAST:
6649 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006650 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006651 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006652 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006653 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006654 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006655 case ISD::READCYCLECOUNTER:
6656 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6657 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006658 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006659 if (Res.getNode())
6660 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006661}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006662
Evan Cheng10043e22007-01-19 07:51:42 +00006663//===----------------------------------------------------------------------===//
6664// ARM Scheduler Hooks
6665//===----------------------------------------------------------------------===//
6666
Bill Wendling030b58e2011-10-06 22:18:16 +00006667/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6668/// registers the function context.
6669void ARMTargetLowering::
6670SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6671 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006672 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006673 DebugLoc dl = MI->getDebugLoc();
6674 MachineFunction *MF = MBB->getParent();
6675 MachineRegisterInfo *MRI = &MF->getRegInfo();
6676 MachineConstantPool *MCP = MF->getConstantPool();
6677 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6678 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006679
Bill Wendling374ee192011-10-03 21:25:38 +00006680 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006681 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006682
Bill Wendling374ee192011-10-03 21:25:38 +00006683 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006684 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006685 ARMConstantPoolValue *CPV =
6686 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6687 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6688
Craig Topper61e88f42014-11-21 05:58:21 +00006689 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6690 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006691
Bill Wendling030b58e2011-10-06 22:18:16 +00006692 // Grab constant pool and fixed stack memory operands.
6693 MachineMemOperand *CPMMO =
6694 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6695 MachineMemOperand::MOLoad, 4, 4);
6696
6697 MachineMemOperand *FIMMOSt =
6698 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6699 MachineMemOperand::MOStore, 4, 4);
6700
6701 // Load the address of the dispatch MBB into the jump buffer.
6702 if (isThumb2) {
6703 // Incoming value: jbuf
6704 // ldr.n r5, LCPI1_1
6705 // orr r5, r5, #1
6706 // add r5, pc
6707 // str r5, [$jbuf, #+4] ; &jbuf[1]
6708 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6709 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6710 .addConstantPoolIndex(CPI)
6711 .addMemOperand(CPMMO));
6712 // Set the low bit because of thumb mode.
6713 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6714 AddDefaultCC(
6715 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6716 .addReg(NewVReg1, RegState::Kill)
6717 .addImm(0x01)));
6718 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6719 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6720 .addReg(NewVReg2, RegState::Kill)
6721 .addImm(PCLabelId);
6722 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6723 .addReg(NewVReg3, RegState::Kill)
6724 .addFrameIndex(FI)
6725 .addImm(36) // &jbuf[1] :: pc
6726 .addMemOperand(FIMMOSt));
6727 } else if (isThumb) {
6728 // Incoming value: jbuf
6729 // ldr.n r1, LCPI1_4
6730 // add r1, pc
6731 // mov r2, #1
6732 // orrs r1, r2
6733 // add r2, $jbuf, #+4 ; &jbuf[1]
6734 // str r1, [r2]
6735 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6736 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6737 .addConstantPoolIndex(CPI)
6738 .addMemOperand(CPMMO));
6739 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6740 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6741 .addReg(NewVReg1, RegState::Kill)
6742 .addImm(PCLabelId);
6743 // Set the low bit because of thumb mode.
6744 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6745 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6746 .addReg(ARM::CPSR, RegState::Define)
6747 .addImm(1));
6748 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6749 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6750 .addReg(ARM::CPSR, RegState::Define)
6751 .addReg(NewVReg2, RegState::Kill)
6752 .addReg(NewVReg3, RegState::Kill));
6753 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006754 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6755 .addFrameIndex(FI)
6756 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006757 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6758 .addReg(NewVReg4, RegState::Kill)
6759 .addReg(NewVReg5, RegState::Kill)
6760 .addImm(0)
6761 .addMemOperand(FIMMOSt));
6762 } else {
6763 // Incoming value: jbuf
6764 // ldr r1, LCPI1_1
6765 // add r1, pc, r1
6766 // str r1, [$jbuf, #+4] ; &jbuf[1]
6767 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6768 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6769 .addConstantPoolIndex(CPI)
6770 .addImm(0)
6771 .addMemOperand(CPMMO));
6772 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6773 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6774 .addReg(NewVReg1, RegState::Kill)
6775 .addImm(PCLabelId));
6776 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6777 .addReg(NewVReg2, RegState::Kill)
6778 .addFrameIndex(FI)
6779 .addImm(36) // &jbuf[1] :: pc
6780 .addMemOperand(FIMMOSt));
6781 }
6782}
6783
Matthias Brauneec4efc2015-04-28 00:37:05 +00006784void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6785 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006786 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006787 DebugLoc dl = MI->getDebugLoc();
6788 MachineFunction *MF = MBB->getParent();
6789 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006790 MachineFrameInfo *MFI = MF->getFrameInfo();
6791 int FI = MFI->getFunctionContextIndex();
6792
Craig Topper61e88f42014-11-21 05:58:21 +00006793 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6794 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006795
Bill Wendling362c1b02011-10-06 21:29:56 +00006796 // Get a mapping of the call site numbers to all of the landing pads they're
6797 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006798 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6799 unsigned MaxCSNum = 0;
6800 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006801 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6802 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006803 if (!BB->isLandingPad()) continue;
6804
6805 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6806 // pad.
6807 for (MachineBasicBlock::iterator
6808 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6809 if (!II->isEHLabel()) continue;
6810
6811 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006812 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006813
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006814 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6815 for (SmallVectorImpl<unsigned>::iterator
6816 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6817 CSI != CSE; ++CSI) {
6818 CallSiteNumToLPad[*CSI].push_back(BB);
6819 MaxCSNum = std::max(MaxCSNum, *CSI);
6820 }
Bill Wendling202803e2011-10-05 00:02:33 +00006821 break;
6822 }
6823 }
6824
6825 // Get an ordered list of the machine basic blocks for the jump table.
6826 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006827 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006828 LPadList.reserve(CallSiteNumToLPad.size());
6829 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6830 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6831 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006832 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006833 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006834 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6835 }
Bill Wendling202803e2011-10-05 00:02:33 +00006836 }
6837
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006838 assert(!LPadList.empty() &&
6839 "No landing pad destinations for the dispatch jump table!");
6840
Bill Wendling362c1b02011-10-06 21:29:56 +00006841 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006842 MachineJumpTableInfo *JTI =
6843 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6844 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00006845 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006846
Bill Wendling362c1b02011-10-06 21:29:56 +00006847 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006848
6849 // Shove the dispatch's address into the return slot in the function context.
6850 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6851 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006852
Bill Wendling324be982011-10-05 00:39:32 +00006853 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006854 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006855 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006856 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006857 else
6858 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6859
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006860 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006861 DispatchBB->addSuccessor(TrapBB);
6862
6863 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6864 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006865
Bill Wendling510fbcd2011-10-17 21:32:56 +00006866 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006867 MF->insert(MF->end(), DispatchBB);
6868 MF->insert(MF->end(), DispContBB);
6869 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006870
Bill Wendling030b58e2011-10-06 22:18:16 +00006871 // Insert code into the entry block that creates and registers the function
6872 // context.
6873 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6874
Bill Wendling030b58e2011-10-06 22:18:16 +00006875 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006876 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006877 MachineMemOperand::MOLoad |
6878 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006879
Chad Rosier1ec8e402012-11-06 23:05:24 +00006880 MachineInstrBuilder MIB;
6881 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6882
6883 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6884 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6885
6886 // Add a register mask with no preserved registers. This results in all
6887 // registers being marked as clobbered.
6888 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006889
Bill Wendling85833f72011-10-18 22:49:07 +00006890 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006891 if (Subtarget->isThumb2()) {
6892 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6893 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6894 .addFrameIndex(FI)
6895 .addImm(4)
6896 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006897
Bill Wendling85833f72011-10-18 22:49:07 +00006898 if (NumLPads < 256) {
6899 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6900 .addReg(NewVReg1)
6901 .addImm(LPadList.size()));
6902 } else {
6903 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6904 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006905 .addImm(NumLPads & 0xFFFF));
6906
6907 unsigned VReg2 = VReg1;
6908 if ((NumLPads & 0xFFFF0000) != 0) {
6909 VReg2 = MRI->createVirtualRegister(TRC);
6910 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6911 .addReg(VReg1)
6912 .addImm(NumLPads >> 16));
6913 }
6914
Bill Wendling85833f72011-10-18 22:49:07 +00006915 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6916 .addReg(NewVReg1)
6917 .addReg(VReg2));
6918 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006919
Bill Wendling5626c662011-10-06 22:53:00 +00006920 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6921 .addMBB(TrapBB)
6922 .addImm(ARMCC::HI)
6923 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006924
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006925 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6926 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00006927 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00006928
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006929 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006930 AddDefaultCC(
6931 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006932 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6933 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006934 .addReg(NewVReg1)
6935 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6936
6937 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006938 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006939 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00006940 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00006941 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006942 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6943 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6944 .addFrameIndex(FI)
6945 .addImm(1)
6946 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006947
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006948 if (NumLPads < 256) {
6949 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6950 .addReg(NewVReg1)
6951 .addImm(NumLPads));
6952 } else {
6953 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006954 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6955 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6956
6957 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00006958 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006959 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00006960 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006961 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006962
6963 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6964 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6965 .addReg(VReg1, RegState::Define)
6966 .addConstantPoolIndex(Idx));
6967 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6968 .addReg(NewVReg1)
6969 .addReg(VReg1));
6970 }
6971
Bill Wendlingb3d46782011-10-06 23:37:36 +00006972 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6973 .addMBB(TrapBB)
6974 .addImm(ARMCC::HI)
6975 .addReg(ARM::CPSR);
6976
6977 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6978 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6979 .addReg(ARM::CPSR, RegState::Define)
6980 .addReg(NewVReg1)
6981 .addImm(2));
6982
6983 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006984 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00006985 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00006986
6987 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6988 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6989 .addReg(ARM::CPSR, RegState::Define)
6990 .addReg(NewVReg2, RegState::Kill)
6991 .addReg(NewVReg3));
6992
6993 MachineMemOperand *JTMMOLd =
6994 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6995 MachineMemOperand::MOLoad, 4, 4);
6996
6997 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6998 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6999 .addReg(NewVReg4, RegState::Kill)
7000 .addImm(0)
7001 .addMemOperand(JTMMOLd));
7002
Chad Rosier96603432013-03-01 18:30:38 +00007003 unsigned NewVReg6 = NewVReg5;
7004 if (RelocM == Reloc::PIC_) {
7005 NewVReg6 = MRI->createVirtualRegister(TRC);
7006 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7007 .addReg(ARM::CPSR, RegState::Define)
7008 .addReg(NewVReg5, RegState::Kill)
7009 .addReg(NewVReg3));
7010 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007011
7012 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7013 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007014 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007015 } else {
7016 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7017 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7018 .addFrameIndex(FI)
7019 .addImm(4)
7020 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007021
Bill Wendling4969dcd2011-10-18 22:52:20 +00007022 if (NumLPads < 256) {
7023 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7024 .addReg(NewVReg1)
7025 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007026 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007027 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7028 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007029 .addImm(NumLPads & 0xFFFF));
7030
7031 unsigned VReg2 = VReg1;
7032 if ((NumLPads & 0xFFFF0000) != 0) {
7033 VReg2 = MRI->createVirtualRegister(TRC);
7034 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7035 .addReg(VReg1)
7036 .addImm(NumLPads >> 16));
7037 }
7038
Bill Wendling4969dcd2011-10-18 22:52:20 +00007039 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7040 .addReg(NewVReg1)
7041 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007042 } else {
7043 MachineConstantPool *ConstantPool = MF->getConstantPool();
7044 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7045 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7046
7047 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007048 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007049 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007050 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007051 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7052
7053 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7054 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7055 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007056 .addConstantPoolIndex(Idx)
7057 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007058 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7059 .addReg(NewVReg1)
7060 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007061 }
7062
Bill Wendling5626c662011-10-06 22:53:00 +00007063 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7064 .addMBB(TrapBB)
7065 .addImm(ARMCC::HI)
7066 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007067
Bill Wendling973c8172011-10-18 22:11:18 +00007068 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007069 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007070 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007071 .addReg(NewVReg1)
7072 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007073 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7074 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007075 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007076
7077 MachineMemOperand *JTMMOLd =
7078 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
7079 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007080 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007081 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007082 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7083 .addReg(NewVReg3, RegState::Kill)
7084 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007085 .addImm(0)
7086 .addMemOperand(JTMMOLd));
7087
Chad Rosier96603432013-03-01 18:30:38 +00007088 if (RelocM == Reloc::PIC_) {
7089 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7090 .addReg(NewVReg5, RegState::Kill)
7091 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007092 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007093 } else {
7094 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7095 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007096 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007097 }
Bill Wendling5626c662011-10-06 22:53:00 +00007098 }
Bill Wendling202803e2011-10-05 00:02:33 +00007099
Bill Wendling324be982011-10-05 00:39:32 +00007100 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007101 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007102 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007103 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7104 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007105 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007106 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007107 }
7108
Bill Wendling26d27802011-10-17 05:25:09 +00007109 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007110 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007111 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007112 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007113
7114 // Remove the landing pad successor from the invoke block and replace it
7115 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007116 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7117 BB->succ_end());
7118 while (!Successors.empty()) {
7119 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00007120 if (SMBB->isLandingPad()) {
7121 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007122 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007123 }
7124 }
7125
7126 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007127
7128 // Find the invoke call and mark all of the callee-saved registers as
7129 // 'implicit defined' so that they're spilled. This prevents code from
7130 // moving instructions to before the EH block, where they will never be
7131 // executed.
7132 for (MachineBasicBlock::reverse_iterator
7133 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007134 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007135
7136 DenseMap<unsigned, bool> DefRegs;
7137 for (MachineInstr::mop_iterator
7138 OI = II->operands_begin(), OE = II->operands_end();
7139 OI != OE; ++OI) {
7140 if (!OI->isReg()) continue;
7141 DefRegs[OI->getReg()] = true;
7142 }
7143
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007144 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007145
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007146 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007147 unsigned Reg = SavedRegs[i];
7148 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007149 !ARM::tGPRRegClass.contains(Reg) &&
7150 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007151 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007152 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007153 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007154 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007155 continue;
7156 if (!DefRegs[Reg])
7157 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007158 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007159
7160 break;
7161 }
Bill Wendling883ec972011-10-07 23:18:02 +00007162 }
Bill Wendling324be982011-10-05 00:39:32 +00007163
Bill Wendling617075f2011-10-18 18:30:49 +00007164 // Mark all former landing pads as non-landing pads. The dispatch is the only
7165 // landing pad now.
7166 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7167 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7168 (*I)->setIsLandingPad(false);
7169
Bill Wendling324be982011-10-05 00:39:32 +00007170 // The instruction is gone now.
7171 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007172}
7173
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007174static
7175MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7176 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7177 E = MBB->succ_end(); I != E; ++I)
7178 if (*I != Succ)
7179 return *I;
7180 llvm_unreachable("Expecting a BB with two successors!");
7181}
7182
Manman Renb504f492013-10-29 22:27:32 +00007183/// Return the load opcode for a given load size. If load size >= 8,
7184/// neon opcode will be returned.
7185static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7186 if (LdSize >= 8)
7187 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7188 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7189 if (IsThumb1)
7190 return LdSize == 4 ? ARM::tLDRi
7191 : LdSize == 2 ? ARM::tLDRHi
7192 : LdSize == 1 ? ARM::tLDRBi : 0;
7193 if (IsThumb2)
7194 return LdSize == 4 ? ARM::t2LDR_POST
7195 : LdSize == 2 ? ARM::t2LDRH_POST
7196 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7197 return LdSize == 4 ? ARM::LDR_POST_IMM
7198 : LdSize == 2 ? ARM::LDRH_POST
7199 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7200}
7201
7202/// Return the store opcode for a given store size. If store size >= 8,
7203/// neon opcode will be returned.
7204static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7205 if (StSize >= 8)
7206 return StSize == 16 ? ARM::VST1q32wb_fixed
7207 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7208 if (IsThumb1)
7209 return StSize == 4 ? ARM::tSTRi
7210 : StSize == 2 ? ARM::tSTRHi
7211 : StSize == 1 ? ARM::tSTRBi : 0;
7212 if (IsThumb2)
7213 return StSize == 4 ? ARM::t2STR_POST
7214 : StSize == 2 ? ARM::t2STRH_POST
7215 : StSize == 1 ? ARM::t2STRB_POST : 0;
7216 return StSize == 4 ? ARM::STR_POST_IMM
7217 : StSize == 2 ? ARM::STRH_POST
7218 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7219}
7220
7221/// Emit a post-increment load operation with given size. The instructions
7222/// will be added to BB at Pos.
7223static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7224 const TargetInstrInfo *TII, DebugLoc dl,
7225 unsigned LdSize, unsigned Data, unsigned AddrIn,
7226 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7227 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7228 assert(LdOpc != 0 && "Should have a load opcode");
7229 if (LdSize >= 8) {
7230 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7231 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7232 .addImm(0));
7233 } else if (IsThumb1) {
7234 // load + update AddrIn
7235 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7236 .addReg(AddrIn).addImm(0));
7237 MachineInstrBuilder MIB =
7238 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7239 MIB = AddDefaultT1CC(MIB);
7240 MIB.addReg(AddrIn).addImm(LdSize);
7241 AddDefaultPred(MIB);
7242 } else if (IsThumb2) {
7243 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7244 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7245 .addImm(LdSize));
7246 } else { // arm
7247 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7248 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7249 .addReg(0).addImm(LdSize));
7250 }
7251}
7252
7253/// Emit a post-increment store operation with given size. The instructions
7254/// will be added to BB at Pos.
7255static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7256 const TargetInstrInfo *TII, DebugLoc dl,
7257 unsigned StSize, unsigned Data, unsigned AddrIn,
7258 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7259 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7260 assert(StOpc != 0 && "Should have a store opcode");
7261 if (StSize >= 8) {
7262 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7263 .addReg(AddrIn).addImm(0).addReg(Data));
7264 } else if (IsThumb1) {
7265 // store + update AddrIn
7266 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7267 .addReg(AddrIn).addImm(0));
7268 MachineInstrBuilder MIB =
7269 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7270 MIB = AddDefaultT1CC(MIB);
7271 MIB.addReg(AddrIn).addImm(StSize);
7272 AddDefaultPred(MIB);
7273 } else if (IsThumb2) {
7274 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7275 .addReg(Data).addReg(AddrIn).addImm(StSize));
7276 } else { // arm
7277 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7278 .addReg(Data).addReg(AddrIn).addReg(0)
7279 .addImm(StSize));
7280 }
7281}
7282
David Peixottoc32e24a2013-10-17 19:49:22 +00007283MachineBasicBlock *
7284ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7285 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007286 // This pseudo instruction has 3 operands: dst, src, size
7287 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7288 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007289 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007290 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7291 MachineFunction::iterator It = BB;
7292 ++It;
7293
7294 unsigned dest = MI->getOperand(0).getReg();
7295 unsigned src = MI->getOperand(1).getReg();
7296 unsigned SizeVal = MI->getOperand(2).getImm();
7297 unsigned Align = MI->getOperand(3).getImm();
7298 DebugLoc dl = MI->getDebugLoc();
7299
Manman Rene8735522012-06-01 19:33:18 +00007300 MachineFunction *MF = BB->getParent();
7301 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007302 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007303 const TargetRegisterClass *TRC = nullptr;
7304 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007305
7306 bool IsThumb1 = Subtarget->isThumb1Only();
7307 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007308
7309 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007310 UnitSize = 1;
7311 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007312 UnitSize = 2;
7313 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007314 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007315 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007316 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007317 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007318 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007319 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007320 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007321 }
7322 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007323 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007324 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007325 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007326
David Peixottob0653e532013-10-24 16:39:36 +00007327 // Select the correct opcode and register class for unit size load/store
7328 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007329 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007330 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007331 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7332 : UnitSize == 8 ? &ARM::DPRRegClass
7333 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007334
Manman Rene8735522012-06-01 19:33:18 +00007335 unsigned BytesLeft = SizeVal % UnitSize;
7336 unsigned LoopSize = SizeVal - BytesLeft;
7337
7338 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7339 // Use LDR and STR to copy.
7340 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7341 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7342 unsigned srcIn = src;
7343 unsigned destIn = dest;
7344 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007345 unsigned srcOut = MRI.createVirtualRegister(TRC);
7346 unsigned destOut = MRI.createVirtualRegister(TRC);
7347 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007348 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7349 IsThumb1, IsThumb2);
7350 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7351 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007352 srcIn = srcOut;
7353 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007354 }
7355
7356 // Handle the leftover bytes with LDRB and STRB.
7357 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7358 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007359 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007360 unsigned srcOut = MRI.createVirtualRegister(TRC);
7361 unsigned destOut = MRI.createVirtualRegister(TRC);
7362 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007363 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7364 IsThumb1, IsThumb2);
7365 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7366 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007367 srcIn = srcOut;
7368 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007369 }
7370 MI->eraseFromParent(); // The instruction is gone now.
7371 return BB;
7372 }
7373
7374 // Expand the pseudo op to a loop.
7375 // thisMBB:
7376 // ...
7377 // movw varEnd, # --> with thumb2
7378 // movt varEnd, #
7379 // ldrcp varEnd, idx --> without thumb2
7380 // fallthrough --> loopMBB
7381 // loopMBB:
7382 // PHI varPhi, varEnd, varLoop
7383 // PHI srcPhi, src, srcLoop
7384 // PHI destPhi, dst, destLoop
7385 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7386 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7387 // subs varLoop, varPhi, #UnitSize
7388 // bne loopMBB
7389 // fallthrough --> exitMBB
7390 // exitMBB:
7391 // epilogue to handle left-over bytes
7392 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7393 // [destOut] = STRB_POST(scratch, destLoop, 1)
7394 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7395 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7396 MF->insert(It, loopMBB);
7397 MF->insert(It, exitMBB);
7398
7399 // Transfer the remainder of BB and its successor edges to exitMBB.
7400 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007401 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007402 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7403
7404 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007405 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007406 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007407 unsigned Vtmp = varEnd;
7408 if ((LoopSize & 0xFFFF0000) != 0)
7409 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007410 AddDefaultPred(BuildMI(BB, dl,
7411 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7412 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007413
7414 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007415 AddDefaultPred(BuildMI(BB, dl,
7416 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7417 varEnd)
7418 .addReg(Vtmp)
7419 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007420 } else {
7421 MachineConstantPool *ConstantPool = MF->getConstantPool();
7422 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7423 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7424
7425 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007426 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007427 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007428 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007429 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7430
7431 if (IsThumb1)
7432 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7433 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7434 else
7435 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7436 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7437 }
Manman Rene8735522012-06-01 19:33:18 +00007438 BB->addSuccessor(loopMBB);
7439
7440 // Generate the loop body:
7441 // varPhi = PHI(varLoop, varEnd)
7442 // srcPhi = PHI(srcLoop, src)
7443 // destPhi = PHI(destLoop, dst)
7444 MachineBasicBlock *entryBB = BB;
7445 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007446 unsigned varLoop = MRI.createVirtualRegister(TRC);
7447 unsigned varPhi = MRI.createVirtualRegister(TRC);
7448 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7449 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7450 unsigned destLoop = MRI.createVirtualRegister(TRC);
7451 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007452
7453 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7454 .addReg(varLoop).addMBB(loopMBB)
7455 .addReg(varEnd).addMBB(entryBB);
7456 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7457 .addReg(srcLoop).addMBB(loopMBB)
7458 .addReg(src).addMBB(entryBB);
7459 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7460 .addReg(destLoop).addMBB(loopMBB)
7461 .addReg(dest).addMBB(entryBB);
7462
7463 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7464 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007465 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007466 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7467 IsThumb1, IsThumb2);
7468 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7469 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007470
7471 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007472 if (IsThumb1) {
7473 MachineInstrBuilder MIB =
7474 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7475 MIB = AddDefaultT1CC(MIB);
7476 MIB.addReg(varPhi).addImm(UnitSize);
7477 AddDefaultPred(MIB);
7478 } else {
7479 MachineInstrBuilder MIB =
7480 BuildMI(*BB, BB->end(), dl,
7481 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7482 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7483 MIB->getOperand(5).setReg(ARM::CPSR);
7484 MIB->getOperand(5).setIsDef(true);
7485 }
7486 BuildMI(*BB, BB->end(), dl,
7487 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7488 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007489
7490 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7491 BB->addSuccessor(loopMBB);
7492 BB->addSuccessor(exitMBB);
7493
7494 // Add epilogue to handle BytesLeft.
7495 BB = exitMBB;
7496 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007497
7498 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7499 // [destOut] = STRB_POST(scratch, destLoop, 1)
7500 unsigned srcIn = srcLoop;
7501 unsigned destIn = destLoop;
7502 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007503 unsigned srcOut = MRI.createVirtualRegister(TRC);
7504 unsigned destOut = MRI.createVirtualRegister(TRC);
7505 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007506 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7507 IsThumb1, IsThumb2);
7508 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7509 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007510 srcIn = srcOut;
7511 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007512 }
7513
7514 MI->eraseFromParent(); // The instruction is gone now.
7515 return BB;
7516}
7517
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007518MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007519ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7520 MachineBasicBlock *MBB) const {
7521 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007522 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007523 DebugLoc DL = MI->getDebugLoc();
7524
7525 assert(Subtarget->isTargetWindows() &&
7526 "__chkstk is only supported on Windows");
7527 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7528
7529 // __chkstk takes the number of words to allocate on the stack in R4, and
7530 // returns the stack adjustment in number of bytes in R4. This will not
7531 // clober any other registers (other than the obvious lr).
7532 //
7533 // Although, technically, IP should be considered a register which may be
7534 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7535 // thumb-2 environment, so there is no interworking required. As a result, we
7536 // do not expect a veneer to be emitted by the linker, clobbering IP.
7537 //
Alp Toker1d099d92014-06-19 19:41:26 +00007538 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007539 // required, again, ensuring that IP is not clobbered.
7540 //
7541 // Finally, although some linkers may theoretically provide a trampoline for
7542 // out of range calls (which is quite common due to a 32M range limitation of
7543 // branches for Thumb), we can generate the long-call version via
7544 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7545 // IP.
7546
7547 switch (TM.getCodeModel()) {
7548 case CodeModel::Small:
7549 case CodeModel::Medium:
7550 case CodeModel::Default:
7551 case CodeModel::Kernel:
7552 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7553 .addImm((unsigned)ARMCC::AL).addReg(0)
7554 .addExternalSymbol("__chkstk")
7555 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7556 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7557 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7558 break;
7559 case CodeModel::Large:
7560 case CodeModel::JITDefault: {
7561 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7562 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7563
7564 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7565 .addExternalSymbol("__chkstk");
7566 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7567 .addImm((unsigned)ARMCC::AL).addReg(0)
7568 .addReg(Reg, RegState::Kill)
7569 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7570 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7571 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7572 break;
7573 }
7574 }
7575
7576 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7577 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007578 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007579
7580 MI->eraseFromParent();
7581 return MBB;
7582}
7583
7584MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007585ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007586 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007587 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007588 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007589 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007590 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007591 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007592 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007593 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007594 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007595 // The Thumb2 pre-indexed stores have the same MI operands, they just
7596 // define them differently in the .td files from the isel patterns, so
7597 // they need pseudos.
7598 case ARM::t2STR_preidx:
7599 MI->setDesc(TII->get(ARM::t2STR_PRE));
7600 return BB;
7601 case ARM::t2STRB_preidx:
7602 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7603 return BB;
7604 case ARM::t2STRH_preidx:
7605 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7606 return BB;
7607
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007608 case ARM::STRi_preidx:
7609 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007610 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007611 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7612 // Decode the offset.
7613 unsigned Offset = MI->getOperand(4).getImm();
7614 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7615 Offset = ARM_AM::getAM2Offset(Offset);
7616 if (isSub)
7617 Offset = -Offset;
7618
Jim Grosbachf402f692011-08-12 21:02:34 +00007619 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007620 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007621 .addOperand(MI->getOperand(0)) // Rn_wb
7622 .addOperand(MI->getOperand(1)) // Rt
7623 .addOperand(MI->getOperand(2)) // Rn
7624 .addImm(Offset) // offset (skip GPR==zero_reg)
7625 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007626 .addOperand(MI->getOperand(6))
7627 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007628 MI->eraseFromParent();
7629 return BB;
7630 }
7631 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007632 case ARM::STRBr_preidx:
7633 case ARM::STRH_preidx: {
7634 unsigned NewOpc;
7635 switch (MI->getOpcode()) {
7636 default: llvm_unreachable("unexpected opcode!");
7637 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7638 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7639 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7640 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007641 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7642 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7643 MIB.addOperand(MI->getOperand(i));
7644 MI->eraseFromParent();
7645 return BB;
7646 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007647
Evan Chengbb2af352009-08-12 05:17:19 +00007648 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007649 // To "insert" a SELECT_CC instruction, we actually have to insert the
7650 // diamond control-flow pattern. The incoming instruction knows the
7651 // destination vreg to set, the condition code register to branch on, the
7652 // true/false values to select between, and a branch opcode to use.
7653 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007654 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007655 ++It;
7656
7657 // thisMBB:
7658 // ...
7659 // TrueVal = ...
7660 // cmpTY ccX, r1, r2
7661 // bCC copy1MBB
7662 // fallthrough --> copy0MBB
7663 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007664 MachineFunction *F = BB->getParent();
7665 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7666 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007667 F->insert(It, copy0MBB);
7668 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007669
7670 // Transfer the remainder of BB and its successor edges to sinkMBB.
7671 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007672 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007673 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7674
Dan Gohmanf4f04102010-07-06 15:49:48 +00007675 BB->addSuccessor(copy0MBB);
7676 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007677
Dan Gohman34396292010-07-06 20:24:04 +00007678 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7679 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7680
Evan Cheng10043e22007-01-19 07:51:42 +00007681 // copy0MBB:
7682 // %FalseValue = ...
7683 // # fallthrough to sinkMBB
7684 BB = copy0MBB;
7685
7686 // Update machine-CFG edges
7687 BB->addSuccessor(sinkMBB);
7688
7689 // sinkMBB:
7690 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7691 // ...
7692 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007693 BuildMI(*BB, BB->begin(), dl,
7694 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007695 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7696 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7697
Dan Gohman34396292010-07-06 20:24:04 +00007698 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007699 return BB;
7700 }
Evan Chengb972e562009-08-07 00:34:42 +00007701
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007702 case ARM::BCCi64:
7703 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007704 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007705 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007706
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007707 // Compare both parts that make up the double comparison separately for
7708 // equality.
7709 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7710
7711 unsigned LHS1 = MI->getOperand(1).getReg();
7712 unsigned LHS2 = MI->getOperand(2).getReg();
7713 if (RHSisZero) {
7714 AddDefaultPred(BuildMI(BB, dl,
7715 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7716 .addReg(LHS1).addImm(0));
7717 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7718 .addReg(LHS2).addImm(0)
7719 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7720 } else {
7721 unsigned RHS1 = MI->getOperand(3).getReg();
7722 unsigned RHS2 = MI->getOperand(4).getReg();
7723 AddDefaultPred(BuildMI(BB, dl,
7724 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7725 .addReg(LHS1).addReg(RHS1));
7726 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7727 .addReg(LHS2).addReg(RHS2)
7728 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7729 }
7730
7731 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7732 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7733 if (MI->getOperand(0).getImm() == ARMCC::NE)
7734 std::swap(destMBB, exitMBB);
7735
7736 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7737 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007738 if (isThumb2)
7739 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7740 else
7741 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007742
7743 MI->eraseFromParent(); // The pseudo instruction is gone now.
7744 return BB;
7745 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007746
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007747 case ARM::Int_eh_sjlj_setjmp:
7748 case ARM::Int_eh_sjlj_setjmp_nofp:
7749 case ARM::tInt_eh_sjlj_setjmp:
7750 case ARM::t2Int_eh_sjlj_setjmp:
7751 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00007752 return BB;
7753
7754 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007755 EmitSjLjDispatchBlock(MI, BB);
7756 return BB;
7757
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007758 case ARM::ABS:
7759 case ARM::t2ABS: {
7760 // To insert an ABS instruction, we have to insert the
7761 // diamond control-flow pattern. The incoming instruction knows the
7762 // source vreg to test against 0, the destination vreg to set,
7763 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007764 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007765 // It transforms
7766 // V1 = ABS V0
7767 // into
7768 // V2 = MOVS V0
7769 // BCC (branch to SinkBB if V0 >= 0)
7770 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007771 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007772 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7773 MachineFunction::iterator BBI = BB;
7774 ++BBI;
7775 MachineFunction *Fn = BB->getParent();
7776 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7777 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7778 Fn->insert(BBI, RSBBB);
7779 Fn->insert(BBI, SinkBB);
7780
7781 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7782 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00007783 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007784 bool isThumb2 = Subtarget->isThumb2();
7785 MachineRegisterInfo &MRI = Fn->getRegInfo();
7786 // In Thumb mode S must not be specified if source register is the SP or
7787 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007788 unsigned NewRsbDstReg =
7789 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007790
7791 // Transfer the remainder of BB and its successor edges to sinkMBB.
7792 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007793 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007794 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7795
7796 BB->addSuccessor(RSBBB);
7797 BB->addSuccessor(SinkBB);
7798
7799 // fall through to SinkMBB
7800 RSBBB->addSuccessor(SinkBB);
7801
Manman Rene0763c72012-06-15 21:32:12 +00007802 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007803 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007804 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7805 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007806
7807 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007808 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007809 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7810 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7811
7812 // insert rsbri in RSBBB
7813 // Note: BCC and rsbri will be converted into predicated rsbmi
7814 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007815 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007816 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00007817 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007818 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7819
Andrew Trick3f07c422011-10-18 18:40:53 +00007820 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007821 // reuse ABSDstReg to not change uses of ABS instruction
7822 BuildMI(*SinkBB, SinkBB->begin(), dl,
7823 TII->get(ARM::PHI), ABSDstReg)
7824 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007825 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007826
7827 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007828 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007829
7830 // return last added BB
7831 return SinkBB;
7832 }
Manman Rene8735522012-06-01 19:33:18 +00007833 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007834 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007835 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007836 case ARM::WIN__CHKSTK:
7837 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007838 }
7839}
7840
Evan Chenge6fba772011-08-30 19:09:48 +00007841void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7842 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007843 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007844 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7845 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7846 // operand is still set to noreg. If needed, set the optional operand's
7847 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007848 //
Andrew Trick88b24502011-10-18 19:18:52 +00007849 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007850
Andrew Trick924123a2011-09-21 02:20:46 +00007851 // Rename pseudo opcodes.
7852 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7853 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007854 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007855 MCID = &TII->get(NewOpc);
7856
7857 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7858 "converted opcode should be the same except for cc_out");
7859
7860 MI->setDesc(*MCID);
7861
7862 // Add the optional cc_out operand
7863 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007864 }
Andrew Trick88b24502011-10-18 19:18:52 +00007865 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007866
7867 // Any ARM instruction that sets the 's' bit should specify an optional
7868 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007869 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007870 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007871 return;
7872 }
Andrew Trick924123a2011-09-21 02:20:46 +00007873 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7874 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007875 bool definesCPSR = false;
7876 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007877 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007878 i != e; ++i) {
7879 const MachineOperand &MO = MI->getOperand(i);
7880 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7881 definesCPSR = true;
7882 if (MO.isDead())
7883 deadCPSR = true;
7884 MI->RemoveOperand(i);
7885 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007886 }
7887 }
Andrew Trick8586e622011-09-20 03:17:40 +00007888 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007889 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007890 return;
7891 }
7892 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007893 if (deadCPSR) {
7894 assert(!MI->getOperand(ccOutIdx).getReg() &&
7895 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007896 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007897 }
Andrew Trick8586e622011-09-20 03:17:40 +00007898
Andrew Trick924123a2011-09-21 02:20:46 +00007899 // If this instruction was defined with an optional CPSR def and its dag node
7900 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007901 MachineOperand &MO = MI->getOperand(ccOutIdx);
7902 MO.setReg(ARM::CPSR);
7903 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007904}
7905
Evan Cheng10043e22007-01-19 07:51:42 +00007906//===----------------------------------------------------------------------===//
7907// ARM Optimization Hooks
7908//===----------------------------------------------------------------------===//
7909
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007910// Helper function that checks if N is a null or all ones constant.
7911static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7912 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7913 if (!C)
7914 return false;
7915 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7916}
7917
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007918// Return true if N is conditionally 0 or all ones.
7919// Detects these expressions where cc is an i1 value:
7920//
7921// (select cc 0, y) [AllOnes=0]
7922// (select cc y, 0) [AllOnes=0]
7923// (zext cc) [AllOnes=0]
7924// (sext cc) [AllOnes=0/1]
7925// (select cc -1, y) [AllOnes=1]
7926// (select cc y, -1) [AllOnes=1]
7927//
7928// Invert is set when N is the null/all ones constant when CC is false.
7929// OtherOp is set to the alternative value of N.
7930static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7931 SDValue &CC, bool &Invert,
7932 SDValue &OtherOp,
7933 SelectionDAG &DAG) {
7934 switch (N->getOpcode()) {
7935 default: return false;
7936 case ISD::SELECT: {
7937 CC = N->getOperand(0);
7938 SDValue N1 = N->getOperand(1);
7939 SDValue N2 = N->getOperand(2);
7940 if (isZeroOrAllOnes(N1, AllOnes)) {
7941 Invert = false;
7942 OtherOp = N2;
7943 return true;
7944 }
7945 if (isZeroOrAllOnes(N2, AllOnes)) {
7946 Invert = true;
7947 OtherOp = N1;
7948 return true;
7949 }
7950 return false;
7951 }
7952 case ISD::ZERO_EXTEND:
7953 // (zext cc) can never be the all ones value.
7954 if (AllOnes)
7955 return false;
7956 // Fall through.
7957 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007958 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007959 EVT VT = N->getValueType(0);
7960 CC = N->getOperand(0);
7961 if (CC.getValueType() != MVT::i1)
7962 return false;
7963 Invert = !AllOnes;
7964 if (AllOnes)
7965 // When looking for an AllOnes constant, N is an sext, and the 'other'
7966 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007967 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007968 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7969 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007970 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007971 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007972 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
7973 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007974 return true;
7975 }
7976 }
7977}
7978
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007979// Combine a constant select operand into its use:
7980//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007981// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7982// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7983// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7984// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7985// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007986//
7987// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007988// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007989//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007990// Also recognize sext/zext from i1:
7991//
7992// (add (zext cc), x) -> (select cc (add x, 1), x)
7993// (add (sext cc), x) -> (select cc (add x, -1), x)
7994//
7995// These transformations eventually create predicated instructions.
7996//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007997// @param N The node to transform.
7998// @param Slct The N operand that is a select.
7999// @param OtherOp The other N operand (x above).
8000// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008001// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008002// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00008003static
8004SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008005 TargetLowering::DAGCombinerInfo &DCI,
8006 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00008007 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00008008 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008009 SDValue NonConstantVal;
8010 SDValue CCOp;
8011 bool SwapSelectOps;
8012 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
8013 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008014 return SDValue();
8015
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008016 // Slct is now know to be the desired identity constant when CC is true.
8017 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008018 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008019 OtherOp, NonConstantVal);
8020 // Unless SwapSelectOps says CC should be false.
8021 if (SwapSelectOps)
8022 std::swap(TrueVal, FalseVal);
8023
Andrew Trickef9de2a2013-05-25 02:42:55 +00008024 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008025 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008026}
8027
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008028// Attempt combineSelectAndUse on each operand of a commutative operator N.
8029static
8030SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8031 TargetLowering::DAGCombinerInfo &DCI) {
8032 SDValue N0 = N->getOperand(0);
8033 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008034 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008035 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8036 if (Result.getNode())
8037 return Result;
8038 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008039 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008040 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8041 if (Result.getNode())
8042 return Result;
8043 }
8044 return SDValue();
8045}
8046
Eric Christopher1b8b94192011-06-29 21:10:36 +00008047// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008048// (only after legalization).
8049static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8050 TargetLowering::DAGCombinerInfo &DCI,
8051 const ARMSubtarget *Subtarget) {
8052
8053 // Only perform optimization if after legalize, and if NEON is available. We
8054 // also expected both operands to be BUILD_VECTORs.
8055 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8056 || N0.getOpcode() != ISD::BUILD_VECTOR
8057 || N1.getOpcode() != ISD::BUILD_VECTOR)
8058 return SDValue();
8059
8060 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8061 EVT VT = N->getValueType(0);
8062 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8063 return SDValue();
8064
8065 // Check that the vector operands are of the right form.
8066 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8067 // operands, where N is the size of the formed vector.
8068 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8069 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008070
8071 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008072 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008073 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008074 SDValue Vec = N0->getOperand(0)->getOperand(0);
8075 SDNode *V = Vec.getNode();
8076 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008077
Eric Christopher1b8b94192011-06-29 21:10:36 +00008078 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008079 // check to see if each of their operands are an EXTRACT_VECTOR with
8080 // the same vector and appropriate index.
8081 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8082 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8083 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008084
Tanya Lattnere9e67052011-06-14 23:48:48 +00008085 SDValue ExtVec0 = N0->getOperand(i);
8086 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008087
Tanya Lattnere9e67052011-06-14 23:48:48 +00008088 // First operand is the vector, verify its the same.
8089 if (V != ExtVec0->getOperand(0).getNode() ||
8090 V != ExtVec1->getOperand(0).getNode())
8091 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008092
Tanya Lattnere9e67052011-06-14 23:48:48 +00008093 // Second is the constant, verify its correct.
8094 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8095 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008096
Tanya Lattnere9e67052011-06-14 23:48:48 +00008097 // For the constant, we want to see all the even or all the odd.
8098 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8099 || C1->getZExtValue() != nextIndex+1)
8100 return SDValue();
8101
8102 // Increment index.
8103 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008104 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008105 return SDValue();
8106 }
8107
8108 // Create VPADDL node.
8109 SelectionDAG &DAG = DCI.DAG;
8110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008111
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008112 SDLoc dl(N);
8113
Tanya Lattnere9e67052011-06-14 23:48:48 +00008114 // Build operand list.
8115 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008116 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008117 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008118
8119 // Input is the vector.
8120 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008121
Tanya Lattnere9e67052011-06-14 23:48:48 +00008122 // Get widened type and narrowed type.
8123 MVT widenType;
8124 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008125
Silviu Barangaa3106e62014-04-03 10:44:27 +00008126 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8127 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008128 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8129 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8130 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8131 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008132 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008133 }
8134
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008135 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008136 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008137 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008138}
8139
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008140static SDValue findMUL_LOHI(SDValue V) {
8141 if (V->getOpcode() == ISD::UMUL_LOHI ||
8142 V->getOpcode() == ISD::SMUL_LOHI)
8143 return V;
8144 return SDValue();
8145}
8146
8147static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8148 TargetLowering::DAGCombinerInfo &DCI,
8149 const ARMSubtarget *Subtarget) {
8150
8151 if (Subtarget->isThumb1Only()) return SDValue();
8152
8153 // Only perform the checks after legalize when the pattern is available.
8154 if (DCI.isBeforeLegalize()) return SDValue();
8155
8156 // Look for multiply add opportunities.
8157 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8158 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8159 // a glue link from the first add to the second add.
8160 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8161 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008162 // UMUL_LOHI
8163 // / :lo \ :hi
8164 // / \ [no multiline comment]
8165 // loAdd -> ADDE |
8166 // \ :glue /
8167 // \ /
8168 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008169 //
8170 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8171 SDValue AddcOp0 = AddcNode->getOperand(0);
8172 SDValue AddcOp1 = AddcNode->getOperand(1);
8173
8174 // Check if the two operands are from the same mul_lohi node.
8175 if (AddcOp0.getNode() == AddcOp1.getNode())
8176 return SDValue();
8177
8178 assert(AddcNode->getNumValues() == 2 &&
8179 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008180 "Expect ADDC with two result values. First: i32");
8181
8182 // Check that we have a glued ADDC node.
8183 if (AddcNode->getValueType(1) != MVT::Glue)
8184 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008185
8186 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8187 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8188 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8189 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8190 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8191 return SDValue();
8192
8193 // Look for the glued ADDE.
8194 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008195 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008196 return SDValue();
8197
8198 // Make sure it is really an ADDE.
8199 if (AddeNode->getOpcode() != ISD::ADDE)
8200 return SDValue();
8201
8202 assert(AddeNode->getNumOperands() == 3 &&
8203 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8204 "ADDE node has the wrong inputs");
8205
8206 // Check for the triangle shape.
8207 SDValue AddeOp0 = AddeNode->getOperand(0);
8208 SDValue AddeOp1 = AddeNode->getOperand(1);
8209
8210 // Make sure that the ADDE operands are not coming from the same node.
8211 if (AddeOp0.getNode() == AddeOp1.getNode())
8212 return SDValue();
8213
8214 // Find the MUL_LOHI node walking up ADDE's operands.
8215 bool IsLeftOperandMUL = false;
8216 SDValue MULOp = findMUL_LOHI(AddeOp0);
8217 if (MULOp == SDValue())
8218 MULOp = findMUL_LOHI(AddeOp1);
8219 else
8220 IsLeftOperandMUL = true;
8221 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008222 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008223
8224 // Figure out the right opcode.
8225 unsigned Opc = MULOp->getOpcode();
8226 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8227
8228 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008229 SDValue* HiAdd = nullptr;
8230 SDValue* LoMul = nullptr;
8231 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008232
Jyoti Allurf1d70502015-01-23 09:10:03 +00008233 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8234 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8235 return SDValue();
8236
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008237 if (IsLeftOperandMUL)
8238 HiAdd = &AddeOp1;
8239 else
8240 HiAdd = &AddeOp0;
8241
8242
Jyoti Allurf1d70502015-01-23 09:10:03 +00008243 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8244 // whose low result is fed to the ADDC we are checking.
8245
8246 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008247 LoMul = &AddcOp0;
8248 LowAdd = &AddcOp1;
8249 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008250 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008251 LoMul = &AddcOp1;
8252 LowAdd = &AddcOp0;
8253 }
8254
Craig Topper062a2ba2014-04-25 05:30:21 +00008255 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008256 return SDValue();
8257
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008258 // Create the merged node.
8259 SelectionDAG &DAG = DCI.DAG;
8260
8261 // Build operand list.
8262 SmallVector<SDValue, 8> Ops;
8263 Ops.push_back(LoMul->getOperand(0));
8264 Ops.push_back(LoMul->getOperand(1));
8265 Ops.push_back(*LowAdd);
8266 Ops.push_back(*HiAdd);
8267
Andrew Trickef9de2a2013-05-25 02:42:55 +00008268 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008269 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008270
8271 // Replace the ADDs' nodes uses by the MLA node's values.
8272 SDValue HiMLALResult(MLALNode.getNode(), 1);
8273 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8274
8275 SDValue LoMLALResult(MLALNode.getNode(), 0);
8276 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8277
8278 // Return original node to notify the driver to stop replacing.
8279 SDValue resNode(AddcNode, 0);
8280 return resNode;
8281}
8282
8283/// PerformADDCCombine - Target-specific dag combine transform from
8284/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8285static SDValue PerformADDCCombine(SDNode *N,
8286 TargetLowering::DAGCombinerInfo &DCI,
8287 const ARMSubtarget *Subtarget) {
8288
8289 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8290
8291}
8292
Bob Wilson728eb292010-07-29 20:34:14 +00008293/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8294/// operands N0 and N1. This is a helper for PerformADDCombine that is
8295/// called with the default operands, and if that fails, with commuted
8296/// operands.
8297static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008298 TargetLowering::DAGCombinerInfo &DCI,
8299 const ARMSubtarget *Subtarget){
8300
8301 // Attempt to create vpaddl for this add.
8302 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8303 if (Result.getNode())
8304 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008305
Chris Lattner4147f082009-03-12 06:52:53 +00008306 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008307 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008308 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8309 if (Result.getNode()) return Result;
8310 }
Chris Lattner4147f082009-03-12 06:52:53 +00008311 return SDValue();
8312}
8313
Bob Wilson728eb292010-07-29 20:34:14 +00008314/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8315///
8316static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008317 TargetLowering::DAGCombinerInfo &DCI,
8318 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008319 SDValue N0 = N->getOperand(0);
8320 SDValue N1 = N->getOperand(1);
8321
8322 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008323 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008324 if (Result.getNode())
8325 return Result;
8326
8327 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008328 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008329}
8330
Chris Lattner4147f082009-03-12 06:52:53 +00008331/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008332///
Chris Lattner4147f082009-03-12 06:52:53 +00008333static SDValue PerformSUBCombine(SDNode *N,
8334 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008335 SDValue N0 = N->getOperand(0);
8336 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008337
Chris Lattner4147f082009-03-12 06:52:53 +00008338 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008339 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008340 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8341 if (Result.getNode()) return Result;
8342 }
Bob Wilson7117a912009-03-20 22:42:55 +00008343
Chris Lattner4147f082009-03-12 06:52:53 +00008344 return SDValue();
8345}
8346
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008347/// PerformVMULCombine
8348/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8349/// special multiplier accumulator forwarding.
8350/// vmul d3, d0, d2
8351/// vmla d3, d1, d2
8352/// is faster than
8353/// vadd d3, d0, d1
8354/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008355// However, for (A + B) * (A + B),
8356// vadd d2, d0, d1
8357// vmul d3, d0, d2
8358// vmla d3, d1, d2
8359// is slower than
8360// vadd d2, d0, d1
8361// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008362static SDValue PerformVMULCombine(SDNode *N,
8363 TargetLowering::DAGCombinerInfo &DCI,
8364 const ARMSubtarget *Subtarget) {
8365 if (!Subtarget->hasVMLxForwarding())
8366 return SDValue();
8367
8368 SelectionDAG &DAG = DCI.DAG;
8369 SDValue N0 = N->getOperand(0);
8370 SDValue N1 = N->getOperand(1);
8371 unsigned Opcode = N0.getOpcode();
8372 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8373 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008374 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008375 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8376 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8377 return SDValue();
8378 std::swap(N0, N1);
8379 }
8380
Weiming Zhao2052f482013-09-25 23:12:06 +00008381 if (N0 == N1)
8382 return SDValue();
8383
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008384 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008385 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008386 SDValue N00 = N0->getOperand(0);
8387 SDValue N01 = N0->getOperand(1);
8388 return DAG.getNode(Opcode, DL, VT,
8389 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8390 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8391}
8392
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008393static SDValue PerformMULCombine(SDNode *N,
8394 TargetLowering::DAGCombinerInfo &DCI,
8395 const ARMSubtarget *Subtarget) {
8396 SelectionDAG &DAG = DCI.DAG;
8397
8398 if (Subtarget->isThumb1Only())
8399 return SDValue();
8400
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008401 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8402 return SDValue();
8403
8404 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008405 if (VT.is64BitVector() || VT.is128BitVector())
8406 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008407 if (VT != MVT::i32)
8408 return SDValue();
8409
8410 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8411 if (!C)
8412 return SDValue();
8413
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008414 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008415 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008416
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008417 ShiftAmt = ShiftAmt & (32 - 1);
8418 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008419 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008420
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008421 SDValue Res;
8422 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008423
8424 if (MulAmt >= 0) {
8425 if (isPowerOf2_32(MulAmt - 1)) {
8426 // (mul x, 2^N + 1) => (add (shl x, N), x)
8427 Res = DAG.getNode(ISD::ADD, DL, VT,
8428 V,
8429 DAG.getNode(ISD::SHL, DL, VT,
8430 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008431 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008432 MVT::i32)));
8433 } else if (isPowerOf2_32(MulAmt + 1)) {
8434 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8435 Res = DAG.getNode(ISD::SUB, DL, VT,
8436 DAG.getNode(ISD::SHL, DL, VT,
8437 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008438 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008439 MVT::i32)),
8440 V);
8441 } else
8442 return SDValue();
8443 } else {
8444 uint64_t MulAmtAbs = -MulAmt;
8445 if (isPowerOf2_32(MulAmtAbs + 1)) {
8446 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8447 Res = DAG.getNode(ISD::SUB, DL, VT,
8448 V,
8449 DAG.getNode(ISD::SHL, DL, VT,
8450 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008451 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008452 MVT::i32)));
8453 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8454 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8455 Res = DAG.getNode(ISD::ADD, DL, VT,
8456 V,
8457 DAG.getNode(ISD::SHL, DL, VT,
8458 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008459 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008460 MVT::i32)));
8461 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008462 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008463
8464 } else
8465 return SDValue();
8466 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008467
8468 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008469 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008470 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008471
8472 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008473 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008474 return SDValue();
8475}
8476
Owen Anderson30c48922010-11-05 19:27:46 +00008477static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008478 TargetLowering::DAGCombinerInfo &DCI,
8479 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008480
Owen Anderson30c48922010-11-05 19:27:46 +00008481 // Attempt to use immediate-form VBIC
8482 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008483 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008484 EVT VT = N->getValueType(0);
8485 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008486
Tanya Lattner266792a2011-04-07 15:24:20 +00008487 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8488 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008489
Owen Anderson30c48922010-11-05 19:27:46 +00008490 APInt SplatBits, SplatUndef;
8491 unsigned SplatBitSize;
8492 bool HasAnyUndefs;
8493 if (BVN &&
8494 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8495 if (SplatBitSize <= 64) {
8496 EVT VbicVT;
8497 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8498 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008499 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008500 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008501 if (Val.getNode()) {
8502 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008503 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008504 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008505 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008506 }
8507 }
8508 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008509
Evan Chenge87681c2012-02-23 01:19:06 +00008510 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008511 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8512 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8513 if (Result.getNode())
8514 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008515 }
8516
Owen Anderson30c48922010-11-05 19:27:46 +00008517 return SDValue();
8518}
8519
Jim Grosbach11013ed2010-07-16 23:05:05 +00008520/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8521static SDValue PerformORCombine(SDNode *N,
8522 TargetLowering::DAGCombinerInfo &DCI,
8523 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008524 // Attempt to use immediate-form VORR
8525 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008526 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008527 EVT VT = N->getValueType(0);
8528 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008529
Tanya Lattner266792a2011-04-07 15:24:20 +00008530 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8531 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008532
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008533 APInt SplatBits, SplatUndef;
8534 unsigned SplatBitSize;
8535 bool HasAnyUndefs;
8536 if (BVN && Subtarget->hasNEON() &&
8537 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8538 if (SplatBitSize <= 64) {
8539 EVT VorrVT;
8540 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8541 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008542 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008543 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008544 if (Val.getNode()) {
8545 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008546 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008547 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008548 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008549 }
8550 }
8551 }
8552
Evan Chenge87681c2012-02-23 01:19:06 +00008553 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008554 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8555 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8556 if (Result.getNode())
8557 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008558 }
8559
Nadav Rotem3a94c542012-08-13 18:52:44 +00008560 // The code below optimizes (or (and X, Y), Z).
8561 // The AND operand needs to have a single user to make these optimizations
8562 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008563 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008564 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008565 return SDValue();
8566 SDValue N1 = N->getOperand(1);
8567
8568 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8569 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8570 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8571 APInt SplatUndef;
8572 unsigned SplatBitSize;
8573 bool HasAnyUndefs;
8574
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008575 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008576 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008577 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8578 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008579 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008580 HasAnyUndefs) && !HasAnyUndefs) {
8581 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8582 HasAnyUndefs) && !HasAnyUndefs) {
8583 // Ensure that the bit width of the constants are the same and that
8584 // the splat arguments are logical inverses as per the pattern we
8585 // are trying to simplify.
8586 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8587 SplatBits0 == ~SplatBits1) {
8588 // Canonicalize the vector type to make instruction selection
8589 // simpler.
8590 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8591 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8592 N0->getOperand(1),
8593 N0->getOperand(0),
8594 N1->getOperand(0));
8595 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8596 }
8597 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008598 }
8599 }
8600
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008601 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8602 // reasonable.
8603
Jim Grosbach11013ed2010-07-16 23:05:05 +00008604 // BFI is only available on V6T2+
8605 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8606 return SDValue();
8607
Andrew Trickef9de2a2013-05-25 02:42:55 +00008608 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008609 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008610 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008611 //
8612 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008613 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008614 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008615 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008616 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008617 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008618
Jim Grosbach11013ed2010-07-16 23:05:05 +00008619 if (VT != MVT::i32)
8620 return SDValue();
8621
Evan Cheng2e51bb42010-12-13 20:32:54 +00008622 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008623
Jim Grosbach11013ed2010-07-16 23:05:05 +00008624 // The value and the mask need to be constants so we can verify this is
8625 // actually a bitfield set. If the mask is 0xffff, we can do better
8626 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008627 SDValue MaskOp = N0.getOperand(1);
8628 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8629 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008630 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008631 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008632 if (Mask == 0xffff)
8633 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008634 SDValue Res;
8635 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008636 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8637 if (N1C) {
8638 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008639 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008640 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008641
Evan Cheng34345752010-12-11 04:11:38 +00008642 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008643 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008644
Evan Cheng2e51bb42010-12-13 20:32:54 +00008645 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008646 DAG.getConstant(Val, DL, MVT::i32),
8647 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008648
8649 // Do not add new nodes to DAG combiner worklist.
8650 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008651 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008652 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008653 } else if (N1.getOpcode() == ISD::AND) {
8654 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008655 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8656 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008657 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008658 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008659
Eric Christopherd5530962011-03-26 01:21:03 +00008660 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8661 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008662 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008663 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008664 // The pack halfword instruction works better for masks that fit it,
8665 // so use that when it's available.
8666 if (Subtarget->hasT2ExtractPack() &&
8667 (Mask == 0xffff || Mask == 0xffff0000))
8668 return SDValue();
8669 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008670 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008671 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008672 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008673 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008674 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008675 // Do not add new nodes to DAG combiner worklist.
8676 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008677 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008678 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008679 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008680 // The pack halfword instruction works better for masks that fit it,
8681 // so use that when it's available.
8682 if (Subtarget->hasT2ExtractPack() &&
8683 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8684 return SDValue();
8685 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008686 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008687 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008688 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008689 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008690 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008691 // Do not add new nodes to DAG combiner worklist.
8692 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008693 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008694 }
8695 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008696
Evan Cheng2e51bb42010-12-13 20:32:54 +00008697 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8698 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8699 ARM::isBitFieldInvertedMask(~Mask)) {
8700 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8701 // where lsb(mask) == #shamt and masked bits of B are known zero.
8702 SDValue ShAmt = N00.getOperand(1);
8703 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008704 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008705 if (ShAmtC != LSB)
8706 return SDValue();
8707
8708 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008709 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008710
8711 // Do not add new nodes to DAG combiner worklist.
8712 DCI.CombineTo(N, Res, false);
8713 }
8714
Jim Grosbach11013ed2010-07-16 23:05:05 +00008715 return SDValue();
8716}
8717
Evan Chenge87681c2012-02-23 01:19:06 +00008718static SDValue PerformXORCombine(SDNode *N,
8719 TargetLowering::DAGCombinerInfo &DCI,
8720 const ARMSubtarget *Subtarget) {
8721 EVT VT = N->getValueType(0);
8722 SelectionDAG &DAG = DCI.DAG;
8723
8724 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8725 return SDValue();
8726
8727 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008728 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8729 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8730 if (Result.getNode())
8731 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008732 }
8733
8734 return SDValue();
8735}
8736
Evan Cheng6d02d902011-06-15 01:12:31 +00008737/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8738/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008739static SDValue PerformBFICombine(SDNode *N,
8740 TargetLowering::DAGCombinerInfo &DCI) {
8741 SDValue N1 = N->getOperand(1);
8742 if (N1.getOpcode() == ISD::AND) {
8743 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8744 if (!N11C)
8745 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008746 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008747 unsigned LSB = countTrailingZeros(~InvMask);
8748 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008749 assert(Width <
8750 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008751 "undefined behavior");
8752 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008753 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008754 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008755 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008756 N->getOperand(0), N1.getOperand(0),
8757 N->getOperand(2));
8758 }
8759 return SDValue();
8760}
8761
Bob Wilson22806742010-09-22 22:09:21 +00008762/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8763/// ARMISD::VMOVRRD.
8764static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008765 TargetLowering::DAGCombinerInfo &DCI,
8766 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008767 // vmovrrd(vmovdrr x, y) -> x,y
8768 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008769 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008770 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008771
8772 // vmovrrd(load f64) -> (load i32), (load i32)
8773 SDNode *InNode = InDouble.getNode();
8774 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8775 InNode->getValueType(0) == MVT::f64 &&
8776 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8777 !cast<LoadSDNode>(InNode)->isVolatile()) {
8778 // TODO: Should this be done for non-FrameIndex operands?
8779 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8780
8781 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008782 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008783 SDValue BasePtr = LD->getBasePtr();
8784 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8785 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008786 LD->isNonTemporal(), LD->isInvariant(),
8787 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008788
8789 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008790 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008791 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8792 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008793 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008794 std::min(4U, LD->getAlignment() / 2));
8795
8796 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00008797 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00008798 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008799 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008800 return Result;
8801 }
8802
Bob Wilson22806742010-09-22 22:09:21 +00008803 return SDValue();
8804}
8805
8806/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8807/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8808static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8809 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8810 SDValue Op0 = N->getOperand(0);
8811 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008812 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008813 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008814 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008815 Op1 = Op1.getOperand(0);
8816 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8817 Op0.getNode() == Op1.getNode() &&
8818 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008819 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008820 N->getValueType(0), Op0.getOperand(0));
8821 return SDValue();
8822}
8823
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008824/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8825/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8826/// i64 vector to have f64 elements, since the value can then be loaded
8827/// directly into a VFP register.
8828static bool hasNormalLoadOperand(SDNode *N) {
8829 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8830 for (unsigned i = 0; i < NumElts; ++i) {
8831 SDNode *Elt = N->getOperand(i).getNode();
8832 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8833 return true;
8834 }
8835 return false;
8836}
8837
Bob Wilsoncb6db982010-09-17 22:59:05 +00008838/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8839/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008840static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008841 TargetLowering::DAGCombinerInfo &DCI,
8842 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008843 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8844 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8845 // into a pair of GPRs, which is fine when the value is used as a scalar,
8846 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008847 SelectionDAG &DAG = DCI.DAG;
8848 if (N->getNumOperands() == 2) {
8849 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8850 if (RV.getNode())
8851 return RV;
8852 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008853
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008854 // Load i64 elements as f64 values so that type legalization does not split
8855 // them up into i32 values.
8856 EVT VT = N->getValueType(0);
8857 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8858 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008859 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008860 SmallVector<SDValue, 8> Ops;
8861 unsigned NumElts = VT.getVectorNumElements();
8862 for (unsigned i = 0; i < NumElts; ++i) {
8863 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8864 Ops.push_back(V);
8865 // Make the DAGCombiner fold the bitcast.
8866 DCI.AddToWorklist(V.getNode());
8867 }
8868 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008869 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008870 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8871}
8872
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008873/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8874static SDValue
8875PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8876 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8877 // At that time, we may have inserted bitcasts from integer to float.
8878 // If these bitcasts have survived DAGCombine, change the lowering of this
8879 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8880 // force to use floating point types.
8881
8882 // Make sure we can change the type of the vector.
8883 // This is possible iff:
8884 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8885 // 1.1. Vector is used only once.
8886 // 1.2. Use is a bit convert to an integer type.
8887 // 2. The size of its operands are 32-bits (64-bits are not legal).
8888 EVT VT = N->getValueType(0);
8889 EVT EltVT = VT.getVectorElementType();
8890
8891 // Check 1.1. and 2.
8892 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8893 return SDValue();
8894
8895 // By construction, the input type must be float.
8896 assert(EltVT == MVT::f32 && "Unexpected type!");
8897
8898 // Check 1.2.
8899 SDNode *Use = *N->use_begin();
8900 if (Use->getOpcode() != ISD::BITCAST ||
8901 Use->getValueType(0).isFloatingPoint())
8902 return SDValue();
8903
8904 // Check profitability.
8905 // Model is, if more than half of the relevant operands are bitcast from
8906 // i32, turn the build_vector into a sequence of insert_vector_elt.
8907 // Relevant operands are everything that is not statically
8908 // (i.e., at compile time) bitcasted.
8909 unsigned NumOfBitCastedElts = 0;
8910 unsigned NumElts = VT.getVectorNumElements();
8911 unsigned NumOfRelevantElts = NumElts;
8912 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8913 SDValue Elt = N->getOperand(Idx);
8914 if (Elt->getOpcode() == ISD::BITCAST) {
8915 // Assume only bit cast to i32 will go away.
8916 if (Elt->getOperand(0).getValueType() == MVT::i32)
8917 ++NumOfBitCastedElts;
8918 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8919 // Constants are statically casted, thus do not count them as
8920 // relevant operands.
8921 --NumOfRelevantElts;
8922 }
8923
8924 // Check if more than half of the elements require a non-free bitcast.
8925 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8926 return SDValue();
8927
8928 SelectionDAG &DAG = DCI.DAG;
8929 // Create the new vector type.
8930 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8931 // Check if the type is legal.
8932 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8933 if (!TLI.isTypeLegal(VecVT))
8934 return SDValue();
8935
8936 // Combine:
8937 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8938 // => BITCAST INSERT_VECTOR_ELT
8939 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8940 // (BITCAST EN), N.
8941 SDValue Vec = DAG.getUNDEF(VecVT);
8942 SDLoc dl(N);
8943 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8944 SDValue V = N->getOperand(Idx);
8945 if (V.getOpcode() == ISD::UNDEF)
8946 continue;
8947 if (V.getOpcode() == ISD::BITCAST &&
8948 V->getOperand(0).getValueType() == MVT::i32)
8949 // Fold obvious case.
8950 V = V.getOperand(0);
8951 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008952 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008953 // Make the DAGCombiner fold the bitcasts.
8954 DCI.AddToWorklist(V.getNode());
8955 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008956 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008957 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8958 }
8959 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8960 // Make the DAGCombiner fold the bitcasts.
8961 DCI.AddToWorklist(Vec.getNode());
8962 return Vec;
8963}
8964
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008965/// PerformInsertEltCombine - Target-specific dag combine xforms for
8966/// ISD::INSERT_VECTOR_ELT.
8967static SDValue PerformInsertEltCombine(SDNode *N,
8968 TargetLowering::DAGCombinerInfo &DCI) {
8969 // Bitcast an i64 load inserted into a vector to f64.
8970 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8971 EVT VT = N->getValueType(0);
8972 SDNode *Elt = N->getOperand(1).getNode();
8973 if (VT.getVectorElementType() != MVT::i64 ||
8974 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8975 return SDValue();
8976
8977 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008978 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008979 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8980 VT.getVectorNumElements());
8981 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8982 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8983 // Make the DAGCombiner fold the bitcasts.
8984 DCI.AddToWorklist(Vec.getNode());
8985 DCI.AddToWorklist(V.getNode());
8986 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8987 Vec, V, N->getOperand(2));
8988 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008989}
8990
Bob Wilsonc7334a12010-10-27 20:38:28 +00008991/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8992/// ISD::VECTOR_SHUFFLE.
8993static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8994 // The LLVM shufflevector instruction does not require the shuffle mask
8995 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8996 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8997 // operands do not match the mask length, they are extended by concatenating
8998 // them with undef vectors. That is probably the right thing for other
8999 // targets, but for NEON it is better to concatenate two double-register
9000 // size vector operands into a single quad-register size vector. Do that
9001 // transformation here:
9002 // shuffle(concat(v1, undef), concat(v2, undef)) ->
9003 // shuffle(concat(v1, v2), undef)
9004 SDValue Op0 = N->getOperand(0);
9005 SDValue Op1 = N->getOperand(1);
9006 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
9007 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
9008 Op0.getNumOperands() != 2 ||
9009 Op1.getNumOperands() != 2)
9010 return SDValue();
9011 SDValue Concat0Op1 = Op0.getOperand(1);
9012 SDValue Concat1Op1 = Op1.getOperand(1);
9013 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
9014 Concat1Op1.getOpcode() != ISD::UNDEF)
9015 return SDValue();
9016 // Skip the transformation if any of the types are illegal.
9017 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9018 EVT VT = N->getValueType(0);
9019 if (!TLI.isTypeLegal(VT) ||
9020 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9021 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9022 return SDValue();
9023
Andrew Trickef9de2a2013-05-25 02:42:55 +00009024 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009025 Op0.getOperand(0), Op1.getOperand(0));
9026 // Translate the shuffle mask.
9027 SmallVector<int, 16> NewMask;
9028 unsigned NumElts = VT.getVectorNumElements();
9029 unsigned HalfElts = NumElts/2;
9030 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9031 for (unsigned n = 0; n < NumElts; ++n) {
9032 int MaskElt = SVN->getMaskElt(n);
9033 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009034 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009035 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009036 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009037 NewElt = HalfElts + MaskElt - NumElts;
9038 NewMask.push_back(NewElt);
9039 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009040 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009041 DAG.getUNDEF(VT), NewMask.data());
9042}
9043
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009044/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9045/// NEON load/store intrinsics, and generic vector load/stores, to merge
9046/// base address updates.
9047/// For generic load/stores, the memory type is assumed to be a vector.
9048/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009049static SDValue CombineBaseUpdate(SDNode *N,
9050 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009051 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009052 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9053 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009054 const bool isStore = N->getOpcode() == ISD::STORE;
9055 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009056 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009057 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009058 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009059
9060 // Search for a use of the address operand that is an increment.
9061 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9062 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9063 SDNode *User = *UI;
9064 if (User->getOpcode() != ISD::ADD ||
9065 UI.getUse().getResNo() != Addr.getResNo())
9066 continue;
9067
9068 // Check that the add is independent of the load/store. Otherwise, folding
9069 // it would create a cycle.
9070 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9071 continue;
9072
9073 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009074 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009075 bool isLaneOp = false;
9076 unsigned NewOpc = 0;
9077 unsigned NumVecs = 0;
9078 if (isIntrinsic) {
9079 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9080 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009081 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009082 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9083 NumVecs = 1; break;
9084 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9085 NumVecs = 2; break;
9086 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9087 NumVecs = 3; break;
9088 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9089 NumVecs = 4; break;
9090 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9091 NumVecs = 2; isLaneOp = true; break;
9092 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9093 NumVecs = 3; isLaneOp = true; break;
9094 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9095 NumVecs = 4; isLaneOp = true; break;
9096 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009097 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009098 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009099 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009100 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009101 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009102 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009103 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009104 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009105 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009106 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009107 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009108 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009109 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009110 }
9111 } else {
9112 isLaneOp = true;
9113 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009114 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009115 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9116 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9117 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009118 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9119 NumVecs = 1; isLaneOp = false; break;
9120 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9121 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009122 }
9123 }
9124
9125 // Find the size of memory referenced by the load/store.
9126 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009127 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009128 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009129 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009130 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009131 } else {
9132 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9133 VecTy = N->getOperand(1).getValueType();
9134 }
9135
Bob Wilson06fce872011-02-07 17:43:21 +00009136 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9137 if (isLaneOp)
9138 NumBytes /= VecTy.getVectorNumElements();
9139
9140 // If the increment is a constant, it must match the memory ref size.
9141 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9142 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9143 uint64_t IncVal = CInc->getZExtValue();
9144 if (IncVal != NumBytes)
9145 continue;
9146 } else if (NumBytes >= 3 * 16) {
9147 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9148 // separate instructions that make it harder to use a non-constant update.
9149 continue;
9150 }
9151
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009152 // OK, we found an ADD we can fold into the base update.
9153 // Now, create a _UPD node, taking care of not breaking alignment.
9154
9155 EVT AlignedVecTy = VecTy;
9156 unsigned Alignment = MemN->getAlignment();
9157
9158 // If this is a less-than-standard-aligned load/store, change the type to
9159 // match the standard alignment.
9160 // The alignment is overlooked when selecting _UPD variants; and it's
9161 // easier to introduce bitcasts here than fix that.
9162 // There are 3 ways to get to this base-update combine:
9163 // - intrinsics: they are assumed to be properly aligned (to the standard
9164 // alignment of the memory type), so we don't need to do anything.
9165 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9166 // intrinsics, so, likewise, there's nothing to do.
9167 // - generic load/store instructions: the alignment is specified as an
9168 // explicit operand, rather than implicitly as the standard alignment
9169 // of the memory type (like the intrisics). We need to change the
9170 // memory type to match the explicit alignment. That way, we don't
9171 // generate non-standard-aligned ARMISD::VLDx nodes.
9172 if (isa<LSBaseSDNode>(N)) {
9173 if (Alignment == 0)
9174 Alignment = 1;
9175 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9176 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9177 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9178 assert(!isLaneOp && "Unexpected generic load/store lane.");
9179 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9180 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9181 }
9182 // Don't set an explicit alignment on regular load/stores that we want
9183 // to transform to VLD/VST 1_UPD nodes.
9184 // This matches the behavior of regular load/stores, which only get an
9185 // explicit alignment if the MMO alignment is larger than the standard
9186 // alignment of the memory type.
9187 // Intrinsics, however, always get an explicit alignment, set to the
9188 // alignment of the MMO.
9189 Alignment = 1;
9190 }
9191
Bob Wilson06fce872011-02-07 17:43:21 +00009192 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009193 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009194 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009195 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009196 unsigned n;
9197 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009198 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009199 Tys[n++] = MVT::i32;
9200 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009201 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009202
9203 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009204 SmallVector<SDValue, 8> Ops;
9205 Ops.push_back(N->getOperand(0)); // incoming chain
9206 Ops.push_back(N->getOperand(AddrOpIdx));
9207 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009208
9209 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9210 // Try to match the intrinsic's signature
9211 Ops.push_back(StN->getValue());
9212 } else {
9213 // Loads (and of course intrinsics) match the intrinsics' signature,
9214 // so just add all but the alignment operand.
9215 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9216 Ops.push_back(N->getOperand(i));
9217 }
9218
9219 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009220 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009221
9222 // If this is a non-standard-aligned STORE, the penultimate operand is the
9223 // stored value. Bitcast it to the aligned type.
9224 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9225 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009226 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009227 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009228
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009229 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009230 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009231 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009232
9233 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009234 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009235 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009236 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009237
9238 // If this is an non-standard-aligned LOAD, the first result is the loaded
9239 // value. Bitcast it to the expected result type.
9240 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9241 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009242 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009243 }
9244
Bob Wilson06fce872011-02-07 17:43:21 +00009245 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9246 DCI.CombineTo(N, NewResults);
9247 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9248
9249 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009250 }
Bob Wilson06fce872011-02-07 17:43:21 +00009251 return SDValue();
9252}
9253
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009254static SDValue PerformVLDCombine(SDNode *N,
9255 TargetLowering::DAGCombinerInfo &DCI) {
9256 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9257 return SDValue();
9258
9259 return CombineBaseUpdate(N, DCI);
9260}
9261
Bob Wilson2d790df2010-11-28 06:51:26 +00009262/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9263/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9264/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9265/// return true.
9266static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9267 SelectionDAG &DAG = DCI.DAG;
9268 EVT VT = N->getValueType(0);
9269 // vldN-dup instructions only support 64-bit vectors for N > 1.
9270 if (!VT.is64BitVector())
9271 return false;
9272
9273 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9274 SDNode *VLD = N->getOperand(0).getNode();
9275 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9276 return false;
9277 unsigned NumVecs = 0;
9278 unsigned NewOpc = 0;
9279 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9280 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9281 NumVecs = 2;
9282 NewOpc = ARMISD::VLD2DUP;
9283 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9284 NumVecs = 3;
9285 NewOpc = ARMISD::VLD3DUP;
9286 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9287 NumVecs = 4;
9288 NewOpc = ARMISD::VLD4DUP;
9289 } else {
9290 return false;
9291 }
9292
9293 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9294 // numbers match the load.
9295 unsigned VLDLaneNo =
9296 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9297 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9298 UI != UE; ++UI) {
9299 // Ignore uses of the chain result.
9300 if (UI.getUse().getResNo() == NumVecs)
9301 continue;
9302 SDNode *User = *UI;
9303 if (User->getOpcode() != ARMISD::VDUPLANE ||
9304 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9305 return false;
9306 }
9307
9308 // Create the vldN-dup node.
9309 EVT Tys[5];
9310 unsigned n;
9311 for (n = 0; n < NumVecs; ++n)
9312 Tys[n] = VT;
9313 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009314 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009315 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9316 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009317 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009318 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009319 VLDMemInt->getMemOperand());
9320
9321 // Update the uses.
9322 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9323 UI != UE; ++UI) {
9324 unsigned ResNo = UI.getUse().getResNo();
9325 // Ignore uses of the chain result.
9326 if (ResNo == NumVecs)
9327 continue;
9328 SDNode *User = *UI;
9329 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9330 }
9331
9332 // Now the vldN-lane intrinsic is dead except for its chain result.
9333 // Update uses of the chain.
9334 std::vector<SDValue> VLDDupResults;
9335 for (unsigned n = 0; n < NumVecs; ++n)
9336 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9337 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9338 DCI.CombineTo(VLD, VLDDupResults);
9339
9340 return true;
9341}
9342
Bob Wilson103a0dc2010-07-14 01:22:12 +00009343/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9344/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009345static SDValue PerformVDUPLANECombine(SDNode *N,
9346 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009347 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009348
Bob Wilson2d790df2010-11-28 06:51:26 +00009349 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9350 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9351 if (CombineVLDDUP(N, DCI))
9352 return SDValue(N, 0);
9353
9354 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9355 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009356 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009357 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009358 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009359 return SDValue();
9360
9361 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9362 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9363 // The canonical VMOV for a zero vector uses a 32-bit element size.
9364 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9365 unsigned EltBits;
9366 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9367 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009368 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009369 if (EltSize > VT.getVectorElementType().getSizeInBits())
9370 return SDValue();
9371
Andrew Trickef9de2a2013-05-25 02:42:55 +00009372 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009373}
9374
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009375static SDValue PerformLOADCombine(SDNode *N,
9376 TargetLowering::DAGCombinerInfo &DCI) {
9377 EVT VT = N->getValueType(0);
9378
9379 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9380 if (ISD::isNormalLoad(N) && VT.isVector() &&
9381 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9382 return CombineBaseUpdate(N, DCI);
9383
9384 return SDValue();
9385}
9386
Ahmed Bougacha23167462014-12-09 21:26:53 +00009387/// PerformSTORECombine - Target-specific dag combine xforms for
9388/// ISD::STORE.
9389static SDValue PerformSTORECombine(SDNode *N,
9390 TargetLowering::DAGCombinerInfo &DCI) {
9391 StoreSDNode *St = cast<StoreSDNode>(N);
9392 if (St->isVolatile())
9393 return SDValue();
9394
9395 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9396 // pack all of the elements in one place. Next, store to memory in fewer
9397 // chunks.
9398 SDValue StVal = St->getValue();
9399 EVT VT = StVal.getValueType();
9400 if (St->isTruncatingStore() && VT.isVector()) {
9401 SelectionDAG &DAG = DCI.DAG;
9402 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9403 EVT StVT = St->getMemoryVT();
9404 unsigned NumElems = VT.getVectorNumElements();
9405 assert(StVT != VT && "Cannot truncate to the same type");
9406 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9407 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9408
9409 // From, To sizes and ElemCount must be pow of two
9410 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9411
9412 // We are going to use the original vector elt for storing.
9413 // Accumulated smaller vector elements must be a multiple of the store size.
9414 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9415
9416 unsigned SizeRatio = FromEltSz / ToEltSz;
9417 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9418
9419 // Create a type on which we perform the shuffle.
9420 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9421 NumElems*SizeRatio);
9422 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9423
9424 SDLoc DL(St);
9425 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9426 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9427 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009428 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9429 ? (i + 1) * SizeRatio - 1
9430 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009431
9432 // Can't shuffle using an illegal type.
9433 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9434
9435 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9436 DAG.getUNDEF(WideVec.getValueType()),
9437 ShuffleVec.data());
9438 // At this point all of the data is stored at the bottom of the
9439 // register. We now need to save it to mem.
9440
9441 // Find the largest store unit
9442 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009443 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009444 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9445 StoreType = Tp;
9446 }
9447 // Didn't find a legal store type.
9448 if (!TLI.isTypeLegal(StoreType))
9449 return SDValue();
9450
9451 // Bitcast the original vector into a vector of store-size units
9452 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9453 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9454 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9455 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9456 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009457 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9458 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009459 SDValue BasePtr = St->getBasePtr();
9460
9461 // Perform one or more big stores into memory.
9462 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9463 for (unsigned I = 0; I < E; I++) {
9464 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9465 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009466 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009467 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9468 St->getPointerInfo(), St->isVolatile(),
9469 St->isNonTemporal(), St->getAlignment());
9470 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9471 Increment);
9472 Chains.push_back(Ch);
9473 }
9474 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9475 }
9476
9477 if (!ISD::isNormalStore(St))
9478 return SDValue();
9479
9480 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9481 // ARM stores of arguments in the same cache line.
9482 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9483 StVal.getNode()->hasOneUse()) {
9484 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009485 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009486 SDLoc DL(St);
9487 SDValue BasePtr = St->getBasePtr();
9488 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9489 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9490 BasePtr, St->getPointerInfo(), St->isVolatile(),
9491 St->isNonTemporal(), St->getAlignment());
9492
9493 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009494 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009495 return DAG.getStore(NewST1.getValue(0), DL,
9496 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9497 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9498 St->isNonTemporal(),
9499 std::min(4U, St->getAlignment() / 2));
9500 }
9501
9502 if (StVal.getValueType() == MVT::i64 &&
9503 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9504
9505 // Bitcast an i64 store extracted from a vector to f64.
9506 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9507 SelectionDAG &DAG = DCI.DAG;
9508 SDLoc dl(StVal);
9509 SDValue IntVec = StVal.getOperand(0);
9510 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9511 IntVec.getValueType().getVectorNumElements());
9512 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9513 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9514 Vec, StVal.getOperand(1));
9515 dl = SDLoc(N);
9516 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9517 // Make the DAGCombiner fold the bitcasts.
9518 DCI.AddToWorklist(Vec.getNode());
9519 DCI.AddToWorklist(ExtElt.getNode());
9520 DCI.AddToWorklist(V.getNode());
9521 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9522 St->getPointerInfo(), St->isVolatile(),
9523 St->isNonTemporal(), St->getAlignment(),
9524 St->getAAInfo());
9525 }
9526
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009527 // If this is a legal vector store, try to combine it into a VST1_UPD.
9528 if (ISD::isNormalStore(N) && VT.isVector() &&
9529 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9530 return CombineBaseUpdate(N, DCI);
9531
Ahmed Bougacha23167462014-12-09 21:26:53 +00009532 return SDValue();
9533}
9534
Eric Christopher1b8b94192011-06-29 21:10:36 +00009535// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009536// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9537static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9538{
Chad Rosier6b610b32011-06-28 17:26:57 +00009539 integerPart cN;
9540 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009541 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9542 I != E; I++) {
9543 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9544 if (!C)
9545 return false;
9546
Eric Christopher1b8b94192011-06-29 21:10:36 +00009547 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009548 APFloat APF = C->getValueAPF();
9549 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9550 != APFloat::opOK || !isExact)
9551 return false;
9552
9553 c0 = (I == 0) ? cN : c0;
9554 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9555 return false;
9556 }
9557 C = c0;
9558 return true;
9559}
9560
9561/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9562/// can replace combinations of VMUL and VCVT (floating-point to integer)
9563/// when the VMUL has a constant operand that is a power of 2.
9564///
9565/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9566/// vmul.f32 d16, d17, d16
9567/// vcvt.s32.f32 d16, d16
9568/// becomes:
9569/// vcvt.s32.f32 d16, d16, #3
9570static SDValue PerformVCVTCombine(SDNode *N,
9571 TargetLowering::DAGCombinerInfo &DCI,
9572 const ARMSubtarget *Subtarget) {
9573 SelectionDAG &DAG = DCI.DAG;
9574 SDValue Op = N->getOperand(0);
9575
9576 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9577 Op.getOpcode() != ISD::FMUL)
9578 return SDValue();
9579
9580 uint64_t C;
9581 SDValue N0 = Op->getOperand(0);
9582 SDValue ConstVec = Op->getOperand(1);
9583 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9584
Eric Christopher1b8b94192011-06-29 21:10:36 +00009585 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009586 !isConstVecPow2(ConstVec, isSigned, C))
9587 return SDValue();
9588
Tim Northover7cbc2152013-06-28 15:29:25 +00009589 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9590 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009591 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9592 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9593 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009594 // These instructions only exist converting from f32 to i32. We can handle
9595 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009596 // be lossy. We also can't handle more then 4 lanes, since these intructions
9597 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009598 return SDValue();
9599 }
9600
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009601 SDLoc dl(N);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009602 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9603 Intrinsic::arm_neon_vcvtfp2fxu;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009604 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Tim Northover7cbc2152013-06-28 15:29:25 +00009605 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009606 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9607 N0,
9608 DAG.getConstant(Log2_64(C), dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009609
9610 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009611 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009612
9613 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009614}
9615
9616/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9617/// can replace combinations of VCVT (integer to floating-point) and VDIV
9618/// when the VDIV has a constant operand that is a power of 2.
9619///
9620/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9621/// vcvt.f32.s32 d16, d16
9622/// vdiv.f32 d16, d17, d16
9623/// becomes:
9624/// vcvt.f32.s32 d16, d16, #3
9625static SDValue PerformVDIVCombine(SDNode *N,
9626 TargetLowering::DAGCombinerInfo &DCI,
9627 const ARMSubtarget *Subtarget) {
9628 SelectionDAG &DAG = DCI.DAG;
9629 SDValue Op = N->getOperand(0);
9630 unsigned OpOpcode = Op.getNode()->getOpcode();
9631
9632 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9633 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9634 return SDValue();
9635
9636 uint64_t C;
9637 SDValue ConstVec = N->getOperand(1);
9638 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9639
9640 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9641 !isConstVecPow2(ConstVec, isSigned, C))
9642 return SDValue();
9643
Tim Northover7cbc2152013-06-28 15:29:25 +00009644 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9645 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9646 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9647 // These instructions only exist converting from i32 to f32. We can handle
9648 // smaller integers by generating an extra extend, but larger ones would
9649 // be lossy.
9650 return SDValue();
9651 }
9652
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009653 SDLoc dl(N);
Tim Northover7cbc2152013-06-28 15:29:25 +00009654 SDValue ConvInput = Op.getOperand(0);
9655 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9656 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9657 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009658 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009659 ConvInput);
9660
Eric Christopher1b8b94192011-06-29 21:10:36 +00009661 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009662 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009663 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009664 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009665 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9666 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009667}
9668
9669/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009670/// operand of a vector shift operation, where all the elements of the
9671/// build_vector must have the same constant integer value.
9672static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9673 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009674 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009675 Op = Op.getOperand(0);
9676 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9677 APInt SplatBits, SplatUndef;
9678 unsigned SplatBitSize;
9679 bool HasAnyUndefs;
9680 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9681 HasAnyUndefs, ElementBits) ||
9682 SplatBitSize > ElementBits)
9683 return false;
9684 Cnt = SplatBits.getSExtValue();
9685 return true;
9686}
9687
9688/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9689/// operand of a vector shift left operation. That value must be in the range:
9690/// 0 <= Value < ElementBits for a left shift; or
9691/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009692static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009693 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009694 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009695 if (! getVShiftImm(Op, ElementBits, Cnt))
9696 return false;
9697 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9698}
9699
9700/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9701/// operand of a vector shift right operation. For a shift opcode, the value
9702/// is positive, but for an intrinsic the value count must be negative. The
9703/// absolute value must be in the range:
9704/// 1 <= |Value| <= ElementBits for a right shift; or
9705/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009706static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009707 int64_t &Cnt) {
9708 assert(VT.isVector() && "vector shift count is not a vector type");
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009709 int64_t ElementBits = VT.getVectorElementType().getSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +00009710 if (! getVShiftImm(Op, ElementBits, Cnt))
9711 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009712 if (!isIntrinsic)
9713 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9714 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009715 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +00009716 return true;
9717 }
9718 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +00009719}
9720
9721/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9722static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9723 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9724 switch (IntNo) {
9725 default:
9726 // Don't do anything for most intrinsics.
9727 break;
9728
James Molloya6702e22015-07-17 17:10:55 +00009729 case Intrinsic::arm_neon_vabds:
9730 if (!N->getValueType(0).isInteger())
9731 return SDValue();
9732 return DAG.getNode(ISD::SABSDIFF, SDLoc(N), N->getValueType(0),
9733 N->getOperand(1), N->getOperand(2));
9734 case Intrinsic::arm_neon_vabdu:
9735 return DAG.getNode(ISD::UABSDIFF, SDLoc(N), N->getValueType(0),
9736 N->getOperand(1), N->getOperand(2));
9737
Bob Wilson2e076c42009-06-22 23:27:02 +00009738 // Vector shifts: check for immediate versions and lower them.
9739 // Note: This is done during DAG combining instead of DAG legalizing because
9740 // the build_vectors for 64-bit vector element shift counts are generally
9741 // not legal, and it is hard to see their values after they get legalized to
9742 // loads from a constant pool.
9743 case Intrinsic::arm_neon_vshifts:
9744 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009745 case Intrinsic::arm_neon_vrshifts:
9746 case Intrinsic::arm_neon_vrshiftu:
9747 case Intrinsic::arm_neon_vrshiftn:
9748 case Intrinsic::arm_neon_vqshifts:
9749 case Intrinsic::arm_neon_vqshiftu:
9750 case Intrinsic::arm_neon_vqshiftsu:
9751 case Intrinsic::arm_neon_vqshiftns:
9752 case Intrinsic::arm_neon_vqshiftnu:
9753 case Intrinsic::arm_neon_vqshiftnsu:
9754 case Intrinsic::arm_neon_vqrshiftns:
9755 case Intrinsic::arm_neon_vqrshiftnu:
9756 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009757 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009758 int64_t Cnt;
9759 unsigned VShiftOpc = 0;
9760
9761 switch (IntNo) {
9762 case Intrinsic::arm_neon_vshifts:
9763 case Intrinsic::arm_neon_vshiftu:
9764 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9765 VShiftOpc = ARMISD::VSHL;
9766 break;
9767 }
9768 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9769 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9770 ARMISD::VSHRs : ARMISD::VSHRu);
9771 break;
9772 }
9773 return SDValue();
9774
Bob Wilson2e076c42009-06-22 23:27:02 +00009775 case Intrinsic::arm_neon_vrshifts:
9776 case Intrinsic::arm_neon_vrshiftu:
9777 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9778 break;
9779 return SDValue();
9780
9781 case Intrinsic::arm_neon_vqshifts:
9782 case Intrinsic::arm_neon_vqshiftu:
9783 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9784 break;
9785 return SDValue();
9786
9787 case Intrinsic::arm_neon_vqshiftsu:
9788 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9789 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009790 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009791
Bob Wilson2e076c42009-06-22 23:27:02 +00009792 case Intrinsic::arm_neon_vrshiftn:
9793 case Intrinsic::arm_neon_vqshiftns:
9794 case Intrinsic::arm_neon_vqshiftnu:
9795 case Intrinsic::arm_neon_vqshiftnsu:
9796 case Intrinsic::arm_neon_vqrshiftns:
9797 case Intrinsic::arm_neon_vqrshiftnu:
9798 case Intrinsic::arm_neon_vqrshiftnsu:
9799 // Narrowing shifts require an immediate right shift.
9800 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9801 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009802 llvm_unreachable("invalid shift count for narrowing vector shift "
9803 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009804
9805 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009806 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009807 }
9808
9809 switch (IntNo) {
9810 case Intrinsic::arm_neon_vshifts:
9811 case Intrinsic::arm_neon_vshiftu:
9812 // Opcode already set above.
9813 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009814 case Intrinsic::arm_neon_vrshifts:
9815 VShiftOpc = ARMISD::VRSHRs; break;
9816 case Intrinsic::arm_neon_vrshiftu:
9817 VShiftOpc = ARMISD::VRSHRu; break;
9818 case Intrinsic::arm_neon_vrshiftn:
9819 VShiftOpc = ARMISD::VRSHRN; break;
9820 case Intrinsic::arm_neon_vqshifts:
9821 VShiftOpc = ARMISD::VQSHLs; break;
9822 case Intrinsic::arm_neon_vqshiftu:
9823 VShiftOpc = ARMISD::VQSHLu; break;
9824 case Intrinsic::arm_neon_vqshiftsu:
9825 VShiftOpc = ARMISD::VQSHLsu; break;
9826 case Intrinsic::arm_neon_vqshiftns:
9827 VShiftOpc = ARMISD::VQSHRNs; break;
9828 case Intrinsic::arm_neon_vqshiftnu:
9829 VShiftOpc = ARMISD::VQSHRNu; break;
9830 case Intrinsic::arm_neon_vqshiftnsu:
9831 VShiftOpc = ARMISD::VQSHRNsu; break;
9832 case Intrinsic::arm_neon_vqrshiftns:
9833 VShiftOpc = ARMISD::VQRSHRNs; break;
9834 case Intrinsic::arm_neon_vqrshiftnu:
9835 VShiftOpc = ARMISD::VQRSHRNu; break;
9836 case Intrinsic::arm_neon_vqrshiftnsu:
9837 VShiftOpc = ARMISD::VQRSHRNsu; break;
9838 }
9839
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009840 SDLoc dl(N);
9841 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
9842 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009843 }
9844
9845 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009846 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009847 int64_t Cnt;
9848 unsigned VShiftOpc = 0;
9849
9850 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9851 VShiftOpc = ARMISD::VSLI;
9852 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9853 VShiftOpc = ARMISD::VSRI;
9854 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009855 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009856 }
9857
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009858 SDLoc dl(N);
9859 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009860 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009861 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009862 }
9863
9864 case Intrinsic::arm_neon_vqrshifts:
9865 case Intrinsic::arm_neon_vqrshiftu:
9866 // No immediate versions of these to check for.
9867 break;
9868 }
9869
9870 return SDValue();
9871}
9872
9873/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9874/// lowers them. As with the vector shift intrinsics, this is done during DAG
9875/// combining instead of DAG legalizing because the build_vectors for 64-bit
9876/// vector element shift counts are generally not legal, and it is hard to see
9877/// their values after they get legalized to loads from a constant pool.
9878static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9879 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009880 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009881 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9882 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9883 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9884 SDValue N1 = N->getOperand(1);
9885 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9886 SDValue N0 = N->getOperand(0);
9887 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9888 DAG.MaskedValueIsZero(N0.getOperand(0),
9889 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009890 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009891 }
9892 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009893
9894 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009895 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9896 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009897 return SDValue();
9898
9899 assert(ST->hasNEON() && "unexpected vector shift");
9900 int64_t Cnt;
9901
9902 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009903 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009904
9905 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009906 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
9907 SDLoc dl(N);
9908 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
9909 DAG.getConstant(Cnt, dl, MVT::i32));
9910 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009911 break;
9912
9913 case ISD::SRA:
9914 case ISD::SRL:
9915 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9916 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9917 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009918 SDLoc dl(N);
9919 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
9920 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009921 }
9922 }
9923 return SDValue();
9924}
9925
9926/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9927/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9928static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9929 const ARMSubtarget *ST) {
9930 SDValue N0 = N->getOperand(0);
9931
9932 // Check for sign- and zero-extensions of vector extract operations of 8-
9933 // and 16-bit vector elements. NEON supports these directly. They are
9934 // handled during DAG combining because type legalization will promote them
9935 // to 32-bit types and it is messy to recognize the operations after that.
9936 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9937 SDValue Vec = N0.getOperand(0);
9938 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009939 EVT VT = N->getValueType(0);
9940 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009941 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9942
Owen Anderson9f944592009-08-11 20:47:22 +00009943 if (VT == MVT::i32 &&
9944 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009945 TLI.isTypeLegal(Vec.getValueType()) &&
9946 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009947
9948 unsigned Opc = 0;
9949 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009950 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009951 case ISD::SIGN_EXTEND:
9952 Opc = ARMISD::VGETLANEs;
9953 break;
9954 case ISD::ZERO_EXTEND:
9955 case ISD::ANY_EXTEND:
9956 Opc = ARMISD::VGETLANEu;
9957 break;
9958 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009959 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009960 }
9961 }
9962
9963 return SDValue();
9964}
9965
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009966/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9967/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9968static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9969 const ARMSubtarget *ST) {
9970 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009971 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009972 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9973 // a NaN; only do the transformation when it matches that behavior.
9974
9975 // For now only do this when using NEON for FP operations; if using VFP, it
9976 // is not obvious that the benefit outweighs the cost of switching to the
9977 // NEON pipeline.
9978 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9979 N->getValueType(0) != MVT::f32)
9980 return SDValue();
9981
9982 SDValue CondLHS = N->getOperand(0);
9983 SDValue CondRHS = N->getOperand(1);
9984 SDValue LHS = N->getOperand(2);
9985 SDValue RHS = N->getOperand(3);
9986 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9987
9988 unsigned Opcode = 0;
9989 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009990 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009991 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009992 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009993 IsReversed = true ; // x CC y ? y : x
9994 } else {
9995 return SDValue();
9996 }
9997
Bob Wilsonba8ac742010-02-24 22:15:53 +00009998 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009999 switch (CC) {
10000 default: break;
10001 case ISD::SETOLT:
10002 case ISD::SETOLE:
10003 case ISD::SETLT:
10004 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010005 case ISD::SETULT:
10006 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +000010007 // If LHS is NaN, an ordered comparison will be false and the result will
10008 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
10009 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
10010 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
10011 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
10012 break;
10013 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
10014 // will return -0, so vmin can only be used for unsafe math or if one of
10015 // the operands is known to be nonzero.
10016 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +000010017 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +000010018 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10019 break;
10020 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010021 break;
10022
10023 case ISD::SETOGT:
10024 case ISD::SETOGE:
10025 case ISD::SETGT:
10026 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010027 case ISD::SETUGT:
10028 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +000010029 // If LHS is NaN, an ordered comparison will be false and the result will
10030 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
10031 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
10032 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
10033 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
10034 break;
10035 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
10036 // will return +0, so vmax can only be used for unsafe math or if one of
10037 // the operands is known to be nonzero.
10038 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +000010039 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +000010040 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10041 break;
10042 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010043 break;
10044 }
10045
10046 if (!Opcode)
10047 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010048 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010049}
10050
Evan Chengf863e3f2011-07-13 00:42:17 +000010051/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10052SDValue
10053ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10054 SDValue Cmp = N->getOperand(4);
10055 if (Cmp.getOpcode() != ARMISD::CMPZ)
10056 // Only looking at EQ and NE cases.
10057 return SDValue();
10058
10059 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010060 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010061 SDValue LHS = Cmp.getOperand(0);
10062 SDValue RHS = Cmp.getOperand(1);
10063 SDValue FalseVal = N->getOperand(0);
10064 SDValue TrueVal = N->getOperand(1);
10065 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010066 ARMCC::CondCodes CC =
10067 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010068
10069 // Simplify
10070 // mov r1, r0
10071 // cmp r1, x
10072 // mov r0, y
10073 // moveq r0, x
10074 // to
10075 // cmp r0, x
10076 // movne r0, y
10077 //
10078 // mov r1, r0
10079 // cmp r1, x
10080 // mov r0, x
10081 // movne r0, y
10082 // to
10083 // cmp r0, x
10084 // movne r0, y
10085 /// FIXME: Turn this into a target neutral optimization?
10086 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010087 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010088 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10089 N->getOperand(3), Cmp);
10090 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10091 SDValue ARMcc;
10092 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10093 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10094 N->getOperand(3), NewCmp);
10095 }
10096
10097 if (Res.getNode()) {
10098 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010099 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010100 // Capture demanded bits information that would be otherwise lost.
10101 if (KnownZero == 0xfffffffe)
10102 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10103 DAG.getValueType(MVT::i1));
10104 else if (KnownZero == 0xffffff00)
10105 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10106 DAG.getValueType(MVT::i8));
10107 else if (KnownZero == 0xffff0000)
10108 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10109 DAG.getValueType(MVT::i16));
10110 }
10111
10112 return Res;
10113}
10114
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010115SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010116 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010117 switch (N->getOpcode()) {
10118 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010119 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010120 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010121 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010122 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010123 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010124 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10125 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010126 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010127 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010128 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010129 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010130 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010131 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010132 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010133 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010134 case ISD::FP_TO_SINT:
10135 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
10136 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010137 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010138 case ISD::SHL:
10139 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010140 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010141 case ISD::SIGN_EXTEND:
10142 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010143 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
10144 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010145 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010146 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010147 case ARMISD::VLD2DUP:
10148 case ARMISD::VLD3DUP:
10149 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010150 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010151 case ARMISD::BUILD_VECTOR:
10152 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010153 case ISD::INTRINSIC_VOID:
10154 case ISD::INTRINSIC_W_CHAIN:
10155 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10156 case Intrinsic::arm_neon_vld1:
10157 case Intrinsic::arm_neon_vld2:
10158 case Intrinsic::arm_neon_vld3:
10159 case Intrinsic::arm_neon_vld4:
10160 case Intrinsic::arm_neon_vld2lane:
10161 case Intrinsic::arm_neon_vld3lane:
10162 case Intrinsic::arm_neon_vld4lane:
10163 case Intrinsic::arm_neon_vst1:
10164 case Intrinsic::arm_neon_vst2:
10165 case Intrinsic::arm_neon_vst3:
10166 case Intrinsic::arm_neon_vst4:
10167 case Intrinsic::arm_neon_vst2lane:
10168 case Intrinsic::arm_neon_vst3lane:
10169 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010170 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010171 default: break;
10172 }
10173 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010174 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010175 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010176}
10177
Evan Chengd42641c2011-02-02 01:06:55 +000010178bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10179 EVT VT) const {
10180 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10181}
10182
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010183bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10184 unsigned,
10185 unsigned,
10186 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010187 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010188 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010189
10190 switch (VT.getSimpleVT().SimpleTy) {
10191 default:
10192 return false;
10193 case MVT::i8:
10194 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010195 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010196 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010197 if (AllowsUnaligned) {
10198 if (Fast)
10199 *Fast = Subtarget->hasV7Ops();
10200 return true;
10201 }
10202 return false;
10203 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010204 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010205 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010206 // For any little-endian targets with neon, we can support unaligned ld/st
10207 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010208 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010209 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010210 if (Fast)
10211 *Fast = true;
10212 return true;
10213 }
10214 return false;
10215 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010216 }
10217}
10218
Lang Hames9929c422011-11-02 22:52:45 +000010219static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10220 unsigned AlignCheck) {
10221 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10222 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10223}
10224
10225EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10226 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010227 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010228 bool MemcpyStrSrc,
10229 MachineFunction &MF) const {
10230 const Function *F = MF.getFunction();
10231
10232 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010233 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10234 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010235 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010236 if (Size >= 16 &&
10237 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010238 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010239 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010240 } else if (Size >= 8 &&
10241 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010242 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10243 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010244 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010245 }
10246 }
10247
Lang Hamesb85fcd02011-11-08 18:56:23 +000010248 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010249 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010250 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010251 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010252 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010253
Lang Hames9929c422011-11-02 22:52:45 +000010254 // Let the target-independent logic figure it out.
10255 return MVT::Other;
10256}
10257
Evan Cheng9ec512d2012-12-06 19:13:27 +000010258bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10259 if (Val.getOpcode() != ISD::LOAD)
10260 return false;
10261
10262 EVT VT1 = Val.getValueType();
10263 if (!VT1.isSimple() || !VT1.isInteger() ||
10264 !VT2.isSimple() || !VT2.isInteger())
10265 return false;
10266
10267 switch (VT1.getSimpleVT().SimpleTy) {
10268 default: break;
10269 case MVT::i1:
10270 case MVT::i8:
10271 case MVT::i16:
10272 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10273 return true;
10274 }
10275
10276 return false;
10277}
10278
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010279bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10280 EVT VT = ExtVal.getValueType();
10281
10282 if (!isTypeLegal(VT))
10283 return false;
10284
10285 // Don't create a loadext if we can fold the extension into a wide/long
10286 // instruction.
10287 // If there's more than one user instruction, the loadext is desirable no
10288 // matter what. There can be two uses by the same instruction.
10289 if (ExtVal->use_empty() ||
10290 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10291 return true;
10292
10293 SDNode *U = *ExtVal->use_begin();
10294 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10295 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10296 return false;
10297
10298 return true;
10299}
10300
Tim Northovercc2e9032013-08-06 13:58:03 +000010301bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10302 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10303 return false;
10304
10305 if (!isTypeLegal(EVT::getEVT(Ty1)))
10306 return false;
10307
10308 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10309
10310 // Assuming the caller doesn't have a zeroext or signext return parameter,
10311 // truncation all the way down to i1 is valid.
10312 return true;
10313}
10314
10315
Evan Chengdc49a8d2009-08-14 20:09:37 +000010316static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10317 if (V < 0)
10318 return false;
10319
10320 unsigned Scale = 1;
10321 switch (VT.getSimpleVT().SimpleTy) {
10322 default: return false;
10323 case MVT::i1:
10324 case MVT::i8:
10325 // Scale == 1;
10326 break;
10327 case MVT::i16:
10328 // Scale == 2;
10329 Scale = 2;
10330 break;
10331 case MVT::i32:
10332 // Scale == 4;
10333 Scale = 4;
10334 break;
10335 }
10336
10337 if ((V & (Scale - 1)) != 0)
10338 return false;
10339 V /= Scale;
10340 return V == (V & ((1LL << 5) - 1));
10341}
10342
10343static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10344 const ARMSubtarget *Subtarget) {
10345 bool isNeg = false;
10346 if (V < 0) {
10347 isNeg = true;
10348 V = - V;
10349 }
10350
10351 switch (VT.getSimpleVT().SimpleTy) {
10352 default: return false;
10353 case MVT::i1:
10354 case MVT::i8:
10355 case MVT::i16:
10356 case MVT::i32:
10357 // + imm12 or - imm8
10358 if (isNeg)
10359 return V == (V & ((1LL << 8) - 1));
10360 return V == (V & ((1LL << 12) - 1));
10361 case MVT::f32:
10362 case MVT::f64:
10363 // Same as ARM mode. FIXME: NEON?
10364 if (!Subtarget->hasVFP2())
10365 return false;
10366 if ((V & 3) != 0)
10367 return false;
10368 V >>= 2;
10369 return V == (V & ((1LL << 8) - 1));
10370 }
10371}
10372
Evan Cheng2150b922007-03-12 23:30:29 +000010373/// isLegalAddressImmediate - Return true if the integer value can be used
10374/// as the offset of the target addressing mode for load / store of the
10375/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010376static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010377 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010378 if (V == 0)
10379 return true;
10380
Evan Chengce5dfb62009-03-09 19:15:00 +000010381 if (!VT.isSimple())
10382 return false;
10383
Evan Chengdc49a8d2009-08-14 20:09:37 +000010384 if (Subtarget->isThumb1Only())
10385 return isLegalT1AddressImmediate(V, VT);
10386 else if (Subtarget->isThumb2())
10387 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010388
Evan Chengdc49a8d2009-08-14 20:09:37 +000010389 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010390 if (V < 0)
10391 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010392 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010393 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010394 case MVT::i1:
10395 case MVT::i8:
10396 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010397 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010398 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010399 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010400 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010401 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010402 case MVT::f32:
10403 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010404 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010405 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010406 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010407 return false;
10408 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010409 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010410 }
Evan Cheng10043e22007-01-19 07:51:42 +000010411}
10412
Evan Chengdc49a8d2009-08-14 20:09:37 +000010413bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10414 EVT VT) const {
10415 int Scale = AM.Scale;
10416 if (Scale < 0)
10417 return false;
10418
10419 switch (VT.getSimpleVT().SimpleTy) {
10420 default: return false;
10421 case MVT::i1:
10422 case MVT::i8:
10423 case MVT::i16:
10424 case MVT::i32:
10425 if (Scale == 1)
10426 return true;
10427 // r + r << imm
10428 Scale = Scale & ~1;
10429 return Scale == 2 || Scale == 4 || Scale == 8;
10430 case MVT::i64:
10431 // r + r
10432 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10433 return true;
10434 return false;
10435 case MVT::isVoid:
10436 // Note, we allow "void" uses (basically, uses that aren't loads or
10437 // stores), because arm allows folding a scale into many arithmetic
10438 // operations. This should be made more precise and revisited later.
10439
10440 // Allow r << imm, but the imm has to be a multiple of two.
10441 if (Scale & 1) return false;
10442 return isPowerOf2_32(Scale);
10443 }
10444}
10445
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010446/// isLegalAddressingMode - Return true if the addressing mode represented
10447/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010448bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10449 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010450 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010451 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010452 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010453 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010454
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010455 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010456 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010457 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010458
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010459 switch (AM.Scale) {
10460 case 0: // no scale reg, must be "r+i" or "r", or "i".
10461 break;
10462 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010463 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010464 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010465 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010466 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010467 // ARM doesn't support any R+R*scale+imm addr modes.
10468 if (AM.BaseOffs)
10469 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010470
Bob Wilson866c1742009-04-08 17:55:28 +000010471 if (!VT.isSimple())
10472 return false;
10473
Evan Chengdc49a8d2009-08-14 20:09:37 +000010474 if (Subtarget->isThumb2())
10475 return isLegalT2ScaledAddressingMode(AM, VT);
10476
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010477 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010478 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010479 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010480 case MVT::i1:
10481 case MVT::i8:
10482 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010483 if (Scale < 0) Scale = -Scale;
10484 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010485 return true;
10486 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010487 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010488 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010489 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010490 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010491 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010492 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010493 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010494
Owen Anderson9f944592009-08-11 20:47:22 +000010495 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010496 // Note, we allow "void" uses (basically, uses that aren't loads or
10497 // stores), because arm allows folding a scale into many arithmetic
10498 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010499
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010500 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010501 if (Scale & 1) return false;
10502 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010503 }
Evan Cheng2150b922007-03-12 23:30:29 +000010504 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010505 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010506}
10507
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010508/// isLegalICmpImmediate - Return true if the specified immediate is legal
10509/// icmp immediate, that is the target has icmp instructions which can compare
10510/// a register against the immediate without having to materialize the
10511/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010512bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010513 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010514 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010515 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010516 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010517 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010518 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010519 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010520}
10521
Andrew Tricka22cdb72012-07-18 18:34:27 +000010522/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10523/// *or sub* immediate, that is the target has add or sub instructions which can
10524/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010525/// immediate into a register.
10526bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010527 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010528 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010529 if (!Subtarget->isThumb())
10530 return ARM_AM::getSOImmVal(AbsImm) != -1;
10531 if (Subtarget->isThumb2())
10532 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10533 // Thumb1 only has 8-bit unsigned immediate.
10534 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010535}
10536
Owen Anderson53aa7a92009-08-10 22:56:29 +000010537static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010538 bool isSEXTLoad, SDValue &Base,
10539 SDValue &Offset, bool &isInc,
10540 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010541 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10542 return false;
10543
Owen Anderson9f944592009-08-11 20:47:22 +000010544 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010545 // AddressingMode 3
10546 Base = Ptr->getOperand(0);
10547 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010548 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010549 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010550 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010551 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010552 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010553 return true;
10554 }
10555 }
10556 isInc = (Ptr->getOpcode() == ISD::ADD);
10557 Offset = Ptr->getOperand(1);
10558 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010559 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010560 // AddressingMode 2
10561 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010562 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010563 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010564 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010565 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010566 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010567 Base = Ptr->getOperand(0);
10568 return true;
10569 }
10570 }
10571
10572 if (Ptr->getOpcode() == ISD::ADD) {
10573 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010574 ARM_AM::ShiftOpc ShOpcVal=
10575 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010576 if (ShOpcVal != ARM_AM::no_shift) {
10577 Base = Ptr->getOperand(1);
10578 Offset = Ptr->getOperand(0);
10579 } else {
10580 Base = Ptr->getOperand(0);
10581 Offset = Ptr->getOperand(1);
10582 }
10583 return true;
10584 }
10585
10586 isInc = (Ptr->getOpcode() == ISD::ADD);
10587 Base = Ptr->getOperand(0);
10588 Offset = Ptr->getOperand(1);
10589 return true;
10590 }
10591
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010592 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010593 return false;
10594}
10595
Owen Anderson53aa7a92009-08-10 22:56:29 +000010596static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010597 bool isSEXTLoad, SDValue &Base,
10598 SDValue &Offset, bool &isInc,
10599 SelectionDAG &DAG) {
10600 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10601 return false;
10602
10603 Base = Ptr->getOperand(0);
10604 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10605 int RHSC = (int)RHS->getZExtValue();
10606 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10607 assert(Ptr->getOpcode() == ISD::ADD);
10608 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010609 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010610 return true;
10611 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10612 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010613 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010614 return true;
10615 }
10616 }
10617
10618 return false;
10619}
10620
Evan Cheng10043e22007-01-19 07:51:42 +000010621/// getPreIndexedAddressParts - returns true by value, base pointer and
10622/// offset pointer and addressing mode by reference if the node's address
10623/// can be legally represented as pre-indexed load / store address.
10624bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010625ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10626 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010627 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010628 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010629 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010630 return false;
10631
Owen Anderson53aa7a92009-08-10 22:56:29 +000010632 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010633 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010634 bool isSEXTLoad = false;
10635 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10636 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010637 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010638 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10639 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10640 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010641 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010642 } else
10643 return false;
10644
10645 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010646 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010647 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010648 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10649 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010650 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010651 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010652 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010653 if (!isLegal)
10654 return false;
10655
10656 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10657 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010658}
10659
10660/// getPostIndexedAddressParts - returns true by value, base pointer and
10661/// offset pointer and addressing mode by reference if this node can be
10662/// combined with a load / store to form a post-indexed load / store.
10663bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010664 SDValue &Base,
10665 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010666 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010667 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010668 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010669 return false;
10670
Owen Anderson53aa7a92009-08-10 22:56:29 +000010671 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010672 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010673 bool isSEXTLoad = false;
10674 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010675 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010676 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010677 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10678 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010679 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010680 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010681 } else
10682 return false;
10683
10684 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010685 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010686 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010687 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010688 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010689 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010690 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10691 isInc, DAG);
10692 if (!isLegal)
10693 return false;
10694
Evan Chengf19384d2010-05-18 21:31:17 +000010695 if (Ptr != Base) {
10696 // Swap base ptr and offset to catch more post-index load / store when
10697 // it's legal. In Thumb2 mode, offset must be an immediate.
10698 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10699 !Subtarget->isThumb2())
10700 std::swap(Base, Offset);
10701
10702 // Post-indexed load / store update the base pointer.
10703 if (Ptr != Base)
10704 return false;
10705 }
10706
Evan Cheng84c6cda2009-07-02 07:28:31 +000010707 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10708 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010709}
10710
Jay Foada0653a32014-05-14 21:14:37 +000010711void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10712 APInt &KnownZero,
10713 APInt &KnownOne,
10714 const SelectionDAG &DAG,
10715 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010716 unsigned BitWidth = KnownOne.getBitWidth();
10717 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010718 switch (Op.getOpcode()) {
10719 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010720 case ARMISD::ADDC:
10721 case ARMISD::ADDE:
10722 case ARMISD::SUBC:
10723 case ARMISD::SUBE:
10724 // These nodes' second result is a boolean
10725 if (Op.getResNo() == 0)
10726 break;
10727 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10728 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010729 case ARMISD::CMOV: {
10730 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010731 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010732 if (KnownZero == 0 && KnownOne == 0) return;
10733
Dan Gohmanf990faf2008-02-13 00:35:47 +000010734 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010735 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010736 KnownZero &= KnownZeroRHS;
10737 KnownOne &= KnownOneRHS;
10738 return;
10739 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010740 case ISD::INTRINSIC_W_CHAIN: {
10741 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10742 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10743 switch (IntID) {
10744 default: return;
10745 case Intrinsic::arm_ldaex:
10746 case Intrinsic::arm_ldrex: {
10747 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10748 unsigned MemBits = VT.getScalarType().getSizeInBits();
10749 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10750 return;
10751 }
10752 }
10753 }
Evan Cheng10043e22007-01-19 07:51:42 +000010754 }
10755}
10756
10757//===----------------------------------------------------------------------===//
10758// ARM Inline Assembly Support
10759//===----------------------------------------------------------------------===//
10760
Evan Cheng078b0b02011-01-08 01:24:27 +000010761bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10762 // Looking for "rev" which is V6+.
10763 if (!Subtarget->hasV6Ops())
10764 return false;
10765
10766 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10767 std::string AsmStr = IA->getAsmString();
10768 SmallVector<StringRef, 4> AsmPieces;
10769 SplitString(AsmStr, AsmPieces, ";\n");
10770
10771 switch (AsmPieces.size()) {
10772 default: return false;
10773 case 1:
10774 AsmStr = AsmPieces[0];
10775 AsmPieces.clear();
10776 SplitString(AsmStr, AsmPieces, " \t,");
10777
10778 // rev $0, $1
10779 if (AsmPieces.size() == 3 &&
10780 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10781 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010782 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010783 if (Ty && Ty->getBitWidth() == 32)
10784 return IntrinsicLowering::LowerToByteSwap(CI);
10785 }
10786 break;
10787 }
10788
10789 return false;
10790}
10791
Evan Cheng10043e22007-01-19 07:51:42 +000010792/// getConstraintType - Given a constraint letter, return the type of
10793/// constraint it is for this target.
10794ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010795ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000010796 if (Constraint.size() == 1) {
10797 switch (Constraint[0]) {
10798 default: break;
10799 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010800 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010801 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010802 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010803 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010804 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010805 // An address with a single base register. Due to the way we
10806 // currently handle addresses it is the same as an 'r' memory constraint.
10807 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010808 }
Eric Christophere256cd02011-06-21 22:10:57 +000010809 } else if (Constraint.size() == 2) {
10810 switch (Constraint[0]) {
10811 default: break;
10812 // All 'U+' constraints are addresses.
10813 case 'U': return C_Memory;
10814 }
Evan Cheng10043e22007-01-19 07:51:42 +000010815 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010816 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010817}
10818
John Thompsone8360b72010-10-29 17:29:13 +000010819/// Examine constraint type and operand type and determine a weight value.
10820/// This object must already have been set up with the operand type
10821/// and the current alternative constraint selected.
10822TargetLowering::ConstraintWeight
10823ARMTargetLowering::getSingleConstraintMatchWeight(
10824 AsmOperandInfo &info, const char *constraint) const {
10825 ConstraintWeight weight = CW_Invalid;
10826 Value *CallOperandVal = info.CallOperandVal;
10827 // If we don't have a value, we can't do a match,
10828 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010829 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010830 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010831 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010832 // Look at the constraint type.
10833 switch (*constraint) {
10834 default:
10835 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10836 break;
10837 case 'l':
10838 if (type->isIntegerTy()) {
10839 if (Subtarget->isThumb())
10840 weight = CW_SpecificReg;
10841 else
10842 weight = CW_Register;
10843 }
10844 break;
10845 case 'w':
10846 if (type->isFloatingPointTy())
10847 weight = CW_Register;
10848 break;
10849 }
10850 return weight;
10851}
10852
Eric Christophercf2007c2011-06-30 23:50:52 +000010853typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010854RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
10855 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010856 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010857 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010858 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010859 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010860 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010861 return RCPair(0U, &ARM::tGPRRegClass);
10862 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010863 case 'h': // High regs or no regs.
10864 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010865 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010866 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010867 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010868 if (Subtarget->isThumb1Only())
10869 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010870 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010871 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010872 if (VT == MVT::Other)
10873 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010874 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010875 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010876 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010877 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010878 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010879 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010880 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010881 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010882 if (VT == MVT::Other)
10883 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010884 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010885 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010886 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010887 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010888 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010889 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010890 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010891 case 't':
10892 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010893 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010894 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010895 }
10896 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010897 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010898 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010899
Eric Christopher11e4df72015-02-26 22:38:43 +000010900 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010901}
10902
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010903/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10904/// vector. If it is invalid, don't add anything to Ops.
10905void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010906 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010907 std::vector<SDValue>&Ops,
10908 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010909 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010910
Eric Christopherde9399b2011-06-02 23:16:42 +000010911 // Currently only support length 1 constraints.
10912 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010913
Eric Christopherde9399b2011-06-02 23:16:42 +000010914 char ConstraintLetter = Constraint[0];
10915 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010916 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010917 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010918 case 'I': case 'J': case 'K': case 'L':
10919 case 'M': case 'N': case 'O':
10920 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10921 if (!C)
10922 return;
10923
10924 int64_t CVal64 = C->getSExtValue();
10925 int CVal = (int) CVal64;
10926 // None of these constraints allow values larger than 32 bits. Check
10927 // that the value fits in an int.
10928 if (CVal != CVal64)
10929 return;
10930
Eric Christopherde9399b2011-06-02 23:16:42 +000010931 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010932 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010933 // Constant suitable for movw, must be between 0 and
10934 // 65535.
10935 if (Subtarget->hasV6T2Ops())
10936 if (CVal >= 0 && CVal <= 65535)
10937 break;
10938 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010939 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010940 if (Subtarget->isThumb1Only()) {
10941 // This must be a constant between 0 and 255, for ADD
10942 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010943 if (CVal >= 0 && CVal <= 255)
10944 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010945 } else if (Subtarget->isThumb2()) {
10946 // A constant that can be used as an immediate value in a
10947 // data-processing instruction.
10948 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10949 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010950 } else {
10951 // A constant that can be used as an immediate value in a
10952 // data-processing instruction.
10953 if (ARM_AM::getSOImmVal(CVal) != -1)
10954 break;
10955 }
10956 return;
10957
10958 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010959 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010960 // This must be a constant between -255 and -1, for negated ADD
10961 // immediates. This can be used in GCC with an "n" modifier that
10962 // prints the negated value, for use with SUB instructions. It is
10963 // not useful otherwise but is implemented for compatibility.
10964 if (CVal >= -255 && CVal <= -1)
10965 break;
10966 } else {
10967 // This must be a constant between -4095 and 4095. It is not clear
10968 // what this constraint is intended for. Implemented for
10969 // compatibility with GCC.
10970 if (CVal >= -4095 && CVal <= 4095)
10971 break;
10972 }
10973 return;
10974
10975 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010976 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010977 // A 32-bit value where only one byte has a nonzero value. Exclude
10978 // zero to match GCC. This constraint is used by GCC internally for
10979 // constants that can be loaded with a move/shift combination.
10980 // It is not useful otherwise but is implemented for compatibility.
10981 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10982 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010983 } else if (Subtarget->isThumb2()) {
10984 // A constant whose bitwise inverse can be used as an immediate
10985 // value in a data-processing instruction. This can be used in GCC
10986 // with a "B" modifier that prints the inverted value, for use with
10987 // BIC and MVN instructions. It is not useful otherwise but is
10988 // implemented for compatibility.
10989 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10990 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010991 } else {
10992 // A constant whose bitwise inverse can be used as an immediate
10993 // value in a data-processing instruction. This can be used in GCC
10994 // with a "B" modifier that prints the inverted value, for use with
10995 // BIC and MVN instructions. It is not useful otherwise but is
10996 // implemented for compatibility.
10997 if (ARM_AM::getSOImmVal(~CVal) != -1)
10998 break;
10999 }
11000 return;
11001
11002 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000011003 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011004 // This must be a constant between -7 and 7,
11005 // for 3-operand ADD/SUB immediate instructions.
11006 if (CVal >= -7 && CVal < 7)
11007 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000011008 } else if (Subtarget->isThumb2()) {
11009 // A constant whose negation can be used as an immediate value in a
11010 // data-processing instruction. This can be used in GCC with an "n"
11011 // modifier that prints the negated value, for use with SUB
11012 // instructions. It is not useful otherwise but is implemented for
11013 // compatibility.
11014 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
11015 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011016 } else {
11017 // A constant whose negation can be used as an immediate value in a
11018 // data-processing instruction. This can be used in GCC with an "n"
11019 // modifier that prints the negated value, for use with SUB
11020 // instructions. It is not useful otherwise but is implemented for
11021 // compatibility.
11022 if (ARM_AM::getSOImmVal(-CVal) != -1)
11023 break;
11024 }
11025 return;
11026
11027 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000011028 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011029 // This must be a multiple of 4 between 0 and 1020, for
11030 // ADD sp + immediate.
11031 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11032 break;
11033 } else {
11034 // A power of two or a constant between 0 and 32. This is used in
11035 // GCC for the shift amount on shifted register operands, but it is
11036 // useful in general for any shift amounts.
11037 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11038 break;
11039 }
11040 return;
11041
11042 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011043 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011044 // This must be a constant between 0 and 31, for shift amounts.
11045 if (CVal >= 0 && CVal <= 31)
11046 break;
11047 }
11048 return;
11049
11050 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011051 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011052 // This must be a multiple of 4 between -508 and 508, for
11053 // ADD/SUB sp = sp + immediate.
11054 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11055 break;
11056 }
11057 return;
11058 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011059 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011060 break;
11061 }
11062
11063 if (Result.getNode()) {
11064 Ops.push_back(Result);
11065 return;
11066 }
Dale Johannesence97d552010-06-25 21:55:36 +000011067 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011068}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011069
Renato Golin87610692013-07-16 09:32:17 +000011070SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
11071 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
11072 unsigned Opcode = Op->getOpcode();
11073 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011074 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011075 bool isSigned = (Opcode == ISD::SDIVREM);
11076 EVT VT = Op->getValueType(0);
11077 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11078
11079 RTLIB::Libcall LC;
11080 switch (VT.getSimpleVT().SimpleTy) {
11081 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011082 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11083 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11084 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11085 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000011086 }
11087
11088 SDValue InChain = DAG.getEntryNode();
11089
11090 TargetLowering::ArgListTy Args;
11091 TargetLowering::ArgListEntry Entry;
11092 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
11093 EVT ArgVT = Op->getOperand(i).getValueType();
11094 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
11095 Entry.Node = Op->getOperand(i);
11096 Entry.Ty = ArgTy;
11097 Entry.isSExt = isSigned;
11098 Entry.isZExt = !isSigned;
11099 Args.push_back(Entry);
11100 }
11101
11102 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011103 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011104
Reid Kleckner343c3952014-11-20 23:51:47 +000011105 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011106
11107 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011108 TargetLowering::CallLoweringInfo CLI(DAG);
11109 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011110 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011111 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011112
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011113 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011114 return CallInfo.first;
11115}
11116
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011117SDValue
11118ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11119 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11120 SDLoc DL(Op);
11121
11122 // Get the inputs.
11123 SDValue Chain = Op.getOperand(0);
11124 SDValue Size = Op.getOperand(1);
11125
11126 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011127 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011128
11129 SDValue Flag;
11130 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11131 Flag = Chain.getValue(1);
11132
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011133 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011134 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11135
11136 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11137 Chain = NewSP.getValue(1);
11138
11139 SDValue Ops[2] = { NewSP, Chain };
11140 return DAG.getMergeValues(Ops, DL);
11141}
11142
Oliver Stannard51b1d462014-08-21 12:50:31 +000011143SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11144 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11145 "Unexpected type for custom-lowering FP_EXTEND");
11146
11147 RTLIB::Libcall LC;
11148 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11149
11150 SDValue SrcVal = Op.getOperand(0);
11151 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11152 /*isSigned*/ false, SDLoc(Op)).first;
11153}
11154
11155SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11156 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11157 Subtarget->isFPOnlySP() &&
11158 "Unexpected type for custom-lowering FP_ROUND");
11159
11160 RTLIB::Libcall LC;
11161 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11162
11163 SDValue SrcVal = Op.getOperand(0);
11164 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11165 /*isSigned*/ false, SDLoc(Op)).first;
11166}
11167
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011168bool
11169ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11170 // The ARM target isn't yet aware of offsets.
11171 return false;
11172}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011173
Jim Grosbach11013ed2010-07-16 23:05:05 +000011174bool ARM::isBitFieldInvertedMask(unsigned v) {
11175 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011176 return false;
11177
Jim Grosbach11013ed2010-07-16 23:05:05 +000011178 // there can be 1's on either or both "outsides", all the "inside"
11179 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011180 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011181}
11182
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011183/// isFPImmLegal - Returns true if the target can instruction select the
11184/// specified FP immediate natively. If false, the legalizer will
11185/// materialize the FP immediate as a load from a constant pool.
11186bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11187 if (!Subtarget->hasVFP3())
11188 return false;
11189 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011190 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011191 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011192 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011193 return false;
11194}
Bob Wilson5549d492010-09-21 17:56:22 +000011195
Wesley Peck527da1b2010-11-23 03:31:01 +000011196/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011197/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11198/// specified in the intrinsic calls.
11199bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11200 const CallInst &I,
11201 unsigned Intrinsic) const {
11202 switch (Intrinsic) {
11203 case Intrinsic::arm_neon_vld1:
11204 case Intrinsic::arm_neon_vld2:
11205 case Intrinsic::arm_neon_vld3:
11206 case Intrinsic::arm_neon_vld4:
11207 case Intrinsic::arm_neon_vld2lane:
11208 case Intrinsic::arm_neon_vld3lane:
11209 case Intrinsic::arm_neon_vld4lane: {
11210 Info.opc = ISD::INTRINSIC_W_CHAIN;
11211 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011212 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11213 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011214 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11215 Info.ptrVal = I.getArgOperand(0);
11216 Info.offset = 0;
11217 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11218 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11219 Info.vol = false; // volatile loads with NEON intrinsics not supported
11220 Info.readMem = true;
11221 Info.writeMem = false;
11222 return true;
11223 }
11224 case Intrinsic::arm_neon_vst1:
11225 case Intrinsic::arm_neon_vst2:
11226 case Intrinsic::arm_neon_vst3:
11227 case Intrinsic::arm_neon_vst4:
11228 case Intrinsic::arm_neon_vst2lane:
11229 case Intrinsic::arm_neon_vst3lane:
11230 case Intrinsic::arm_neon_vst4lane: {
11231 Info.opc = ISD::INTRINSIC_VOID;
11232 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011233 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011234 unsigned NumElts = 0;
11235 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011236 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011237 if (!ArgTy->isVectorTy())
11238 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011239 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011240 }
11241 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11242 Info.ptrVal = I.getArgOperand(0);
11243 Info.offset = 0;
11244 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11245 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11246 Info.vol = false; // volatile stores with NEON intrinsics not supported
11247 Info.readMem = false;
11248 Info.writeMem = true;
11249 return true;
11250 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011251 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011252 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011253 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011254 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11255 Info.opc = ISD::INTRINSIC_W_CHAIN;
11256 Info.memVT = MVT::getVT(PtrTy->getElementType());
11257 Info.ptrVal = I.getArgOperand(0);
11258 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011259 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011260 Info.vol = true;
11261 Info.readMem = true;
11262 Info.writeMem = false;
11263 return true;
11264 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011265 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011266 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011267 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011268 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11269 Info.opc = ISD::INTRINSIC_W_CHAIN;
11270 Info.memVT = MVT::getVT(PtrTy->getElementType());
11271 Info.ptrVal = I.getArgOperand(1);
11272 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011273 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011274 Info.vol = true;
11275 Info.readMem = false;
11276 Info.writeMem = true;
11277 return true;
11278 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011279 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011280 case Intrinsic::arm_strexd: {
11281 Info.opc = ISD::INTRINSIC_W_CHAIN;
11282 Info.memVT = MVT::i64;
11283 Info.ptrVal = I.getArgOperand(2);
11284 Info.offset = 0;
11285 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011286 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011287 Info.readMem = false;
11288 Info.writeMem = true;
11289 return true;
11290 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011291 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011292 case Intrinsic::arm_ldrexd: {
11293 Info.opc = ISD::INTRINSIC_W_CHAIN;
11294 Info.memVT = MVT::i64;
11295 Info.ptrVal = I.getArgOperand(0);
11296 Info.offset = 0;
11297 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011298 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011299 Info.readMem = true;
11300 Info.writeMem = false;
11301 return true;
11302 }
Bob Wilson5549d492010-09-21 17:56:22 +000011303 default:
11304 break;
11305 }
11306
11307 return false;
11308}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011309
11310/// \brief Returns true if it is beneficial to convert a load of a constant
11311/// to just the constant itself.
11312bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11313 Type *Ty) const {
11314 assert(Ty->isIntegerTy());
11315
11316 unsigned Bits = Ty->getPrimitiveSizeInBits();
11317 if (Bits == 0 || Bits > 32)
11318 return false;
11319 return true;
11320}
Tim Northover037f26f22014-04-17 18:22:47 +000011321
Robin Morisset25c8e312014-09-17 00:06:58 +000011322bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11323
Robin Morisset5349e8e2014-09-18 18:56:04 +000011324Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11325 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011326 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011327
11328 // First, if the target has no DMB, see what fallback we can use.
11329 if (!Subtarget->hasDataBarrier()) {
11330 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11331 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11332 // here.
11333 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11334 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11335 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11336 Builder.getInt32(0), Builder.getInt32(7),
11337 Builder.getInt32(10), Builder.getInt32(5)};
11338 return Builder.CreateCall(MCR, args);
11339 } else {
11340 // Instead of using barriers, atomic accesses on these subtargets use
11341 // libcalls.
11342 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11343 }
11344 } else {
11345 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11346 // Only a full system barrier exists in the M-class architectures.
11347 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11348 Constant *CDomain = Builder.getInt32(Domain);
11349 return Builder.CreateCall(DMB, CDomain);
11350 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011351}
11352
11353// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011354Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011355 AtomicOrdering Ord, bool IsStore,
11356 bool IsLoad) const {
11357 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011358 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011359
11360 switch (Ord) {
11361 case NotAtomic:
11362 case Unordered:
11363 llvm_unreachable("Invalid fence: unordered/non-atomic");
11364 case Monotonic:
11365 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011366 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011367 case SequentiallyConsistent:
11368 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011369 return nullptr; // Nothing to do
11370 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011371 case Release:
11372 case AcquireRelease:
11373 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011374 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011375 // FIXME: add a comment with a link to documentation justifying this.
11376 else
Robin Morissetdedef332014-09-23 20:31:14 +000011377 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011378 }
Robin Morissetdedef332014-09-23 20:31:14 +000011379 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011380}
11381
Robin Morissetdedef332014-09-23 20:31:14 +000011382Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011383 AtomicOrdering Ord, bool IsStore,
11384 bool IsLoad) const {
11385 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011386 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011387
11388 switch (Ord) {
11389 case NotAtomic:
11390 case Unordered:
11391 llvm_unreachable("Invalid fence: unordered/not-atomic");
11392 case Monotonic:
11393 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011394 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011395 case Acquire:
11396 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011397 case SequentiallyConsistent:
11398 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011399 }
Robin Morissetdedef332014-09-23 20:31:14 +000011400 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011401}
11402
Robin Morisseted3d48f2014-09-03 21:29:59 +000011403// Loads and stores less than 64-bits are already atomic; ones above that
11404// are doomed anyway, so defer to the default libcall and blame the OS when
11405// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11406// anything for those.
11407bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11408 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11409 return (Size == 64) && !Subtarget->isMClass();
11410}
Tim Northover037f26f22014-04-17 18:22:47 +000011411
Robin Morisseted3d48f2014-09-03 21:29:59 +000011412// Loads and stores less than 64-bits are already atomic; ones above that
11413// are doomed anyway, so defer to the default libcall and blame the OS when
11414// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11415// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011416// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11417// guarantee, see DDI0406C ARM architecture reference manual,
11418// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011419bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11420 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11421 return (Size == 64) && !Subtarget->isMClass();
11422}
11423
11424// For the real atomic operations, we have ldrex/strex up to 32 bits,
11425// and up to 64 bits on the non-M profiles
JF Bastienf14889e2015-03-04 15:47:57 +000011426TargetLoweringBase::AtomicRMWExpansionKind
11427ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011428 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011429 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
11430 ? AtomicRMWExpansionKind::LLSC
11431 : AtomicRMWExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011432}
11433
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011434// This has so far only been implemented for MachO.
11435bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011436 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011437}
11438
Quentin Colombetc32615d2014-10-31 17:52:53 +000011439bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11440 unsigned &Cost) const {
11441 // If we do not have NEON, vector types are not natively supported.
11442 if (!Subtarget->hasNEON())
11443 return false;
11444
11445 // Floating point values and vector values map to the same register file.
11446 // Therefore, althought we could do a store extract of a vector type, this is
11447 // better to leave at float as we have more freedom in the addressing mode for
11448 // those.
11449 if (VectorTy->isFPOrFPVectorTy())
11450 return false;
11451
11452 // If the index is unknown at compile time, this is very expensive to lower
11453 // and it is not possible to combine the store with the extract.
11454 if (!isa<ConstantInt>(Idx))
11455 return false;
11456
11457 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11458 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11459 // We can do a store + vector extract on any vector that fits perfectly in a D
11460 // or Q register.
11461 if (BitWidth == 64 || BitWidth == 128) {
11462 Cost = 0;
11463 return true;
11464 }
11465 return false;
11466}
11467
Tim Northover037f26f22014-04-17 18:22:47 +000011468Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11469 AtomicOrdering Ord) const {
11470 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11471 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011472 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011473
11474 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11475 // intrinsic must return {i32, i32} and we have to recombine them into a
11476 // single i64 here.
11477 if (ValTy->getPrimitiveSizeInBits() == 64) {
11478 Intrinsic::ID Int =
11479 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11480 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11481
11482 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11483 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11484
11485 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11486 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011487 if (!Subtarget->isLittle())
11488 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011489 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11490 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11491 return Builder.CreateOr(
11492 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11493 }
11494
11495 Type *Tys[] = { Addr->getType() };
11496 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11497 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11498
11499 return Builder.CreateTruncOrBitCast(
11500 Builder.CreateCall(Ldrex, Addr),
11501 cast<PointerType>(Addr->getType())->getElementType());
11502}
11503
11504Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11505 Value *Addr,
11506 AtomicOrdering Ord) const {
11507 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011508 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011509
11510 // Since the intrinsics must have legal type, the i64 intrinsics take two
11511 // parameters: "i32, i32". We must marshal Val into the appropriate form
11512 // before the call.
11513 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11514 Intrinsic::ID Int =
11515 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11516 Function *Strex = Intrinsic::getDeclaration(M, Int);
11517 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11518
11519 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11520 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011521 if (!Subtarget->isLittle())
11522 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011523 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011524 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011525 }
11526
11527 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11528 Type *Tys[] = { Addr->getType() };
11529 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11530
David Blaikieff6409d2015-05-18 22:13:54 +000011531 return Builder.CreateCall(
11532 Strex, {Builder.CreateZExtOrBitCast(
11533 Val, Strex->getFunctionType()->getParamType(0)),
11534 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011535}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011536
Hao Liu2cd34bb2015-06-26 02:45:36 +000011537/// \brief Lower an interleaved load into a vldN intrinsic.
11538///
11539/// E.g. Lower an interleaved load (Factor = 2):
11540/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
11541/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
11542/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
11543///
11544/// Into:
11545/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
11546/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
11547/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
11548bool ARMTargetLowering::lowerInterleavedLoad(
11549 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
11550 ArrayRef<unsigned> Indices, unsigned Factor) const {
11551 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11552 "Invalid interleave factor");
11553 assert(!Shuffles.empty() && "Empty shufflevector input");
11554 assert(Shuffles.size() == Indices.size() &&
11555 "Unmatched number of shufflevectors and indices");
11556
11557 VectorType *VecTy = Shuffles[0]->getType();
11558 Type *EltTy = VecTy->getVectorElementType();
11559
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011560 const DataLayout &DL = LI->getModule()->getDataLayout();
11561 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
11562 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011563
11564 // Skip illegal vector types and vector types of i64/f64 element (vldN doesn't
11565 // support i64/f64 element).
11566 if ((VecSize != 64 && VecSize != 128) || EltIs64Bits)
11567 return false;
11568
11569 // A pointer vector can not be the return type of the ldN intrinsics. Need to
11570 // load integer vectors first and then convert to pointer vectors.
11571 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011572 VecTy =
11573 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000011574
11575 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
11576 Intrinsic::arm_neon_vld3,
11577 Intrinsic::arm_neon_vld4};
11578
11579 Function *VldnFunc =
11580 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], VecTy);
11581
11582 IRBuilder<> Builder(LI);
11583 SmallVector<Value *, 2> Ops;
11584
11585 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
11586 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
11587 Ops.push_back(Builder.getInt32(LI->getAlignment()));
11588
11589 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
11590
11591 // Replace uses of each shufflevector with the corresponding vector loaded
11592 // by ldN.
11593 for (unsigned i = 0; i < Shuffles.size(); i++) {
11594 ShuffleVectorInst *SV = Shuffles[i];
11595 unsigned Index = Indices[i];
11596
11597 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
11598
11599 // Convert the integer vector to pointer vector if the element is pointer.
11600 if (EltTy->isPointerTy())
11601 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
11602
11603 SV->replaceAllUsesWith(SubVec);
11604 }
11605
11606 return true;
11607}
11608
11609/// \brief Get a mask consisting of sequential integers starting from \p Start.
11610///
11611/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
11612static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
11613 unsigned NumElts) {
11614 SmallVector<Constant *, 16> Mask;
11615 for (unsigned i = 0; i < NumElts; i++)
11616 Mask.push_back(Builder.getInt32(Start + i));
11617
11618 return ConstantVector::get(Mask);
11619}
11620
11621/// \brief Lower an interleaved store into a vstN intrinsic.
11622///
11623/// E.g. Lower an interleaved store (Factor = 3):
11624/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
11625/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
11626/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
11627///
11628/// Into:
11629/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
11630/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
11631/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
11632/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
11633///
11634/// Note that the new shufflevectors will be removed and we'll only generate one
11635/// vst3 instruction in CodeGen.
11636bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
11637 ShuffleVectorInst *SVI,
11638 unsigned Factor) const {
11639 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11640 "Invalid interleave factor");
11641
11642 VectorType *VecTy = SVI->getType();
11643 assert(VecTy->getVectorNumElements() % Factor == 0 &&
11644 "Invalid interleaved store");
11645
11646 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
11647 Type *EltTy = VecTy->getVectorElementType();
11648 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
11649
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011650 const DataLayout &DL = SI->getModule()->getDataLayout();
11651 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
11652 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011653
11654 // Skip illegal sub vector types and vector types of i64/f64 element (vstN
11655 // doesn't support i64/f64 element).
11656 if ((SubVecSize != 64 && SubVecSize != 128) || EltIs64Bits)
11657 return false;
11658
11659 Value *Op0 = SVI->getOperand(0);
11660 Value *Op1 = SVI->getOperand(1);
11661 IRBuilder<> Builder(SI);
11662
11663 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
11664 // vectors to integer vectors.
11665 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011666 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011667
11668 // Convert to the corresponding integer vector.
11669 Type *IntVecTy =
11670 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
11671 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
11672 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
11673
11674 SubVecTy = VectorType::get(IntTy, NumSubElts);
11675 }
11676
11677 static Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
11678 Intrinsic::arm_neon_vst3,
11679 Intrinsic::arm_neon_vst4};
11680 Function *VstNFunc = Intrinsic::getDeclaration(
11681 SI->getModule(), StoreInts[Factor - 2], SubVecTy);
11682
11683 SmallVector<Value *, 6> Ops;
11684
11685 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
11686 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
11687
11688 // Split the shufflevector operands into sub vectors for the new vstN call.
11689 for (unsigned i = 0; i < Factor; i++)
11690 Ops.push_back(Builder.CreateShuffleVector(
11691 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
11692
11693 Ops.push_back(Builder.getInt32(SI->getAlignment()));
11694 Builder.CreateCall(VstNFunc, Ops);
11695 return true;
11696}
11697
Oliver Stannardc24f2172014-05-09 14:01:47 +000011698enum HABaseType {
11699 HA_UNKNOWN = 0,
11700 HA_FLOAT,
11701 HA_DOUBLE,
11702 HA_VECT64,
11703 HA_VECT128
11704};
11705
11706static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11707 uint64_t &Members) {
11708 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11709 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11710 uint64_t SubMembers = 0;
11711 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11712 return false;
11713 Members += SubMembers;
11714 }
11715 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11716 uint64_t SubMembers = 0;
11717 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11718 return false;
11719 Members += SubMembers * AT->getNumElements();
11720 } else if (Ty->isFloatTy()) {
11721 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11722 return false;
11723 Members = 1;
11724 Base = HA_FLOAT;
11725 } else if (Ty->isDoubleTy()) {
11726 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11727 return false;
11728 Members = 1;
11729 Base = HA_DOUBLE;
11730 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11731 Members = 1;
11732 switch (Base) {
11733 case HA_FLOAT:
11734 case HA_DOUBLE:
11735 return false;
11736 case HA_VECT64:
11737 return VT->getBitWidth() == 64;
11738 case HA_VECT128:
11739 return VT->getBitWidth() == 128;
11740 case HA_UNKNOWN:
11741 switch (VT->getBitWidth()) {
11742 case 64:
11743 Base = HA_VECT64;
11744 return true;
11745 case 128:
11746 Base = HA_VECT128;
11747 return true;
11748 default:
11749 return false;
11750 }
11751 }
11752 }
11753
11754 return (Members > 0 && Members <= 4);
11755}
11756
Tim Northovere95c5b32015-02-24 17:22:34 +000011757/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11758/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11759/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011760bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11761 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011762 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11763 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011764 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011765
11766 HABaseType Base = HA_UNKNOWN;
11767 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011768 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11769 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11770
11771 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11772 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011773}