blob: e335784f6d876cf742e6da081b1bd049cce8da3e [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);
Bob Wilson2e076c42009-06-22 23:27:02 +0000145}
146
Craig Topper4fa625f2012-08-12 03:16:37 +0000147void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000148 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000149 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000153 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Eric Christopher1889fdc2015-01-29 00:19:39 +0000157ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
158 const ARMSubtarget &STI)
159 : TargetLowering(TM), Subtarget(&STI) {
160 RegInfo = Subtarget->getRegisterInfo();
161 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000162
Duncan Sandsf2641e12011-09-06 19:07:46 +0000163 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
164
Tim Northoverd6a729b2014-01-06 14:28:05 +0000165 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000166 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000167 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000168 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000169 // Single-precision floating-point arithmetic.
170 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
171 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
172 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
173 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000174
Evan Chengc9f22fd12007-04-27 08:15:43 +0000175 // Double-precision floating-point arithmetic.
176 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
177 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
178 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
179 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000180
Evan Chengc9f22fd12007-04-27 08:15:43 +0000181 // Single-precision comparisons.
182 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
183 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
184 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
185 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
186 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
187 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
188 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
189 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000190
Evan Chengc9f22fd12007-04-27 08:15:43 +0000191 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
192 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
193 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000199
Evan Chengc9f22fd12007-04-27 08:15:43 +0000200 // Double-precision comparisons.
201 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
202 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
203 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
204 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
205 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
206 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
207 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
208 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000209
Evan Chengc9f22fd12007-04-27 08:15:43 +0000210 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
211 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
212 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Evan Chengc9f22fd12007-04-27 08:15:43 +0000219 // Floating-point to integer conversions.
220 // i64 conversions are done via library routines even when generating VFP
221 // instructions, so use the same ones.
222 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
223 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
224 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
225 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000226
Evan Chengc9f22fd12007-04-27 08:15:43 +0000227 // Conversions between floating types.
228 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
229 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
230
231 // Integer to floating-point conversions.
232 // i64 conversions are done via library routines even when generating VFP
233 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000234 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
235 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000236 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
237 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
238 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
239 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
240 }
Evan Cheng10043e22007-01-19 07:51:42 +0000241 }
242
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000243 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000244 setLibcallName(RTLIB::SHL_I128, nullptr);
245 setLibcallName(RTLIB::SRL_I128, nullptr);
246 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000247
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000248 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
249 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000250 static const struct {
251 const RTLIB::Libcall Op;
252 const char * const Name;
253 const CallingConv::ID CC;
254 const ISD::CondCode Cond;
255 } LibraryCalls[] = {
256 // Double-precision floating-point arithmetic helper functions
257 // RTABI chapter 4.1.2, Table 2
258 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
259 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
260 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
261 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000262
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000263 // Double-precision floating-point comparison helper functions
264 // RTABI chapter 4.1.2, Table 3
265 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
266 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
267 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
268 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
269 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
270 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
272 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000273
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000274 // Single-precision floating-point arithmetic helper functions
275 // RTABI chapter 4.1.2, Table 4
276 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
277 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
278 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
279 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000280
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000281 // Single-precision floating-point comparison helper functions
282 // RTABI chapter 4.1.2, Table 5
283 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
284 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
285 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
286 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
287 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
288 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
290 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000291
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000292 // Floating-point to integer conversions.
293 // RTABI chapter 4.1.2, Table 6
294 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
295 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
296 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
297 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
298 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000302
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000303 // Conversions between floating types.
304 // RTABI chapter 4.1.2, Table 7
305 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000306 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000307 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000308
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000309 // Integer to floating-point conversions.
310 // RTABI chapter 4.1.2, Table 8
311 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
312 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
313 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
314 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000319
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000320 // Long long helper functions
321 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000322 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000326
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000327 // Integer division functions
328 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000329 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
330 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000337
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000338 // Memory operations
339 // RTABI chapter 4.3.4
340 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 };
344
345 for (const auto &LC : LibraryCalls) {
346 setLibcallName(LC.Op, LC.Name);
347 setLibcallCallingConv(LC.Op, LC.CC);
348 if (LC.Cond != ISD::SETCC_INVALID)
349 setCmpLibcallCC(LC.Op, LC.Cond);
350 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000351 }
352
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000353 if (Subtarget->isTargetWindows()) {
354 static const struct {
355 const RTLIB::Libcall Op;
356 const char * const Name;
357 const CallingConv::ID CC;
358 } LibraryCalls[] = {
359 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
360 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
361 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
362 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
363 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
367 };
368
369 for (const auto &LC : LibraryCalls) {
370 setLibcallName(LC.Op, LC.Name);
371 setLibcallCallingConv(LC.Op, LC.CC);
372 }
373 }
374
Bob Wilsonbc158992011-10-07 16:59:21 +0000375 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000376 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000377 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
378 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
379 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
380 }
381
Oliver Stannard11790b22014-08-11 09:12:32 +0000382 // The half <-> float conversion functions are always soft-float, but are
383 // needed for some targets which use a hard-float calling convention by
384 // default.
385 if (Subtarget->isAAPCS_ABI()) {
386 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
389 } else {
390 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
391 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
392 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
393 }
394
David Goodwin22c2fba2009-07-08 23:10:31 +0000395 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000396 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000397 else
Craig Topperc7242e02012-04-20 07:30:17 +0000398 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000399 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000400 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000401 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000402 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000403 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000404
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000405 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000406 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000407 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000408 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
409 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
410 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
411 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000412
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000413 setOperationAction(ISD::MULHS, VT, Expand);
414 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
415 setOperationAction(ISD::MULHU, VT, Expand);
416 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000417
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000418 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000419 }
420
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000421 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000422 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000423
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000424 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
425 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
426
Bob Wilson2e076c42009-06-22 23:27:02 +0000427 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000428 addDRTypeForNEON(MVT::v2f32);
429 addDRTypeForNEON(MVT::v8i8);
430 addDRTypeForNEON(MVT::v4i16);
431 addDRTypeForNEON(MVT::v2i32);
432 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000433
Owen Anderson9f944592009-08-11 20:47:22 +0000434 addQRTypeForNEON(MVT::v4f32);
435 addQRTypeForNEON(MVT::v2f64);
436 addQRTypeForNEON(MVT::v16i8);
437 addQRTypeForNEON(MVT::v8i16);
438 addQRTypeForNEON(MVT::v4i32);
439 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000440
Bob Wilson194a2512009-09-15 23:55:57 +0000441 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
442 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000443 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
444 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000445 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
446 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
447 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000448 // FIXME: Code duplication: FDIV and FREM are expanded always, see
449 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000450 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
451 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000452 // FIXME: Create unittest.
453 // In another words, find a way when "copysign" appears in DAG with vector
454 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000456 // FIXME: Code duplication: SETCC has custom operation action, see
457 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000458 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000459 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000460 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
461 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
463 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
464 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
465 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
466 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
467 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
468 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
470 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
471 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000472 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000473 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
474 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
475 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
476 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
477 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000478 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000479
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000480 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
481 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
482 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
483 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
484 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
485 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
486 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
487 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
488 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
489 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000490 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
491 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
492 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
493 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000494 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000495
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000496 // Mark v2f32 intrinsics.
497 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
498 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
499 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
500 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
501 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
502 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
503 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
504 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
505 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
506 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
507 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
508 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
509 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
510 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
511 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
512
Bob Wilson6cc46572009-09-16 00:32:15 +0000513 // Neon does not support some operations on v1i64 and v2i64 types.
514 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000515 // Custom handling for some quad-vector types to detect VMULL.
516 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
517 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
518 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000519 // Custom handling for some vector types to avoid expensive expansions
520 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
521 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
522 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
523 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000524 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
525 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000526 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000527 // a destination type that is wider than the source, and nor does
528 // it have a FP_TO_[SU]INT instruction with a narrower destination than
529 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000530 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
531 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000532 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
533 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000534
Eli Friedmane6385e62012-11-15 22:44:27 +0000535 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000536 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000537
Evan Chengb4eae132012-12-04 22:41:50 +0000538 // NEON does not have single instruction CTPOP for vectors with element
539 // types wider than 8-bits. However, custom lowering can leverage the
540 // v8i8/v16i8 vcnt instruction.
541 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
542 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
543 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
544 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
545
Logan Chien0a43abc2015-07-13 15:37:30 +0000546 // NEON does not have single instruction CTTZ for vectors.
547 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
548 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
549 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
550 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
551
552 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
553 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
554 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
555 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
556
557 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
558 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
559 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
560 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
561
562 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
563 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
564 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
565 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
566
Jim Grosbach5f215872013-02-27 21:31:12 +0000567 // NEON only has FMA instructions as of VFP4.
568 if (!Subtarget->hasVFP4()) {
569 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
570 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
571 }
572
Bob Wilson06fce872011-02-07 17:43:21 +0000573 setTargetDAGCombine(ISD::INTRINSIC_VOID);
574 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000575 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
576 setTargetDAGCombine(ISD::SHL);
577 setTargetDAGCombine(ISD::SRL);
578 setTargetDAGCombine(ISD::SRA);
579 setTargetDAGCombine(ISD::SIGN_EXTEND);
580 setTargetDAGCombine(ISD::ZERO_EXTEND);
581 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000582 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000583 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000584 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000585 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
586 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000587 setTargetDAGCombine(ISD::FP_TO_SINT);
588 setTargetDAGCombine(ISD::FP_TO_UINT);
589 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000590 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000591
James Molloy547d4c02012-02-20 09:24:05 +0000592 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000593 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
594 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000595 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000596 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
597 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
598 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000599 }
James Molloy547d4c02012-02-20 09:24:05 +0000600 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000601 }
602
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000603 // ARM and Thumb2 support UMLAL/SMLAL.
604 if (!Subtarget->isThumb1Only())
605 setTargetDAGCombine(ISD::ADDC);
606
Oliver Stannard51b1d462014-08-21 12:50:31 +0000607 if (Subtarget->isFPOnlySP()) {
608 // When targetting a floating-point unit with only single-precision
609 // operations, f64 is legal for the few double-precision instructions which
610 // are present However, no double-precision operations other than moves,
611 // loads and stores are provided by the hardware.
612 setOperationAction(ISD::FADD, MVT::f64, Expand);
613 setOperationAction(ISD::FSUB, MVT::f64, Expand);
614 setOperationAction(ISD::FMUL, MVT::f64, Expand);
615 setOperationAction(ISD::FMA, MVT::f64, Expand);
616 setOperationAction(ISD::FDIV, MVT::f64, Expand);
617 setOperationAction(ISD::FREM, MVT::f64, Expand);
618 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
619 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
620 setOperationAction(ISD::FNEG, MVT::f64, Expand);
621 setOperationAction(ISD::FABS, MVT::f64, Expand);
622 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
623 setOperationAction(ISD::FSIN, MVT::f64, Expand);
624 setOperationAction(ISD::FCOS, MVT::f64, Expand);
625 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
626 setOperationAction(ISD::FPOW, MVT::f64, Expand);
627 setOperationAction(ISD::FLOG, MVT::f64, Expand);
628 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
629 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
630 setOperationAction(ISD::FEXP, MVT::f64, Expand);
631 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
632 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
633 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
634 setOperationAction(ISD::FRINT, MVT::f64, Expand);
635 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
636 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000637 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
638 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
639 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
640 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
641 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
642 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000643 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
644 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
645 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000646
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000647 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000648
Tim Northover4e80b582014-07-18 13:01:19 +0000649 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000650 for (MVT VT : MVT::fp_valuetypes()) {
651 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
652 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
653 }
Tim Northover4e80b582014-07-18 13:01:19 +0000654
655 // ... or truncating stores
656 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
657 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
658 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000659
Duncan Sands95d46ef2008-01-23 20:39:46 +0000660 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000661 for (MVT VT : MVT::integer_valuetypes())
662 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000663
Evan Cheng10043e22007-01-19 07:51:42 +0000664 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000665 if (!Subtarget->isThumb1Only()) {
666 for (unsigned im = (unsigned)ISD::PRE_INC;
667 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000668 setIndexedLoadAction(im, MVT::i1, Legal);
669 setIndexedLoadAction(im, MVT::i8, Legal);
670 setIndexedLoadAction(im, MVT::i16, Legal);
671 setIndexedLoadAction(im, MVT::i32, Legal);
672 setIndexedStoreAction(im, MVT::i1, Legal);
673 setIndexedStoreAction(im, MVT::i8, Legal);
674 setIndexedStoreAction(im, MVT::i16, Legal);
675 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000676 }
Evan Cheng10043e22007-01-19 07:51:42 +0000677 }
678
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000679 setOperationAction(ISD::SADDO, MVT::i32, Custom);
680 setOperationAction(ISD::UADDO, MVT::i32, Custom);
681 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
682 setOperationAction(ISD::USUBO, MVT::i32, Custom);
683
Evan Cheng10043e22007-01-19 07:51:42 +0000684 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000685 setOperationAction(ISD::MUL, MVT::i64, Expand);
686 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000687 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000688 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
689 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000690 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000691 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
692 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000693 setOperationAction(ISD::MULHS, MVT::i32, Expand);
694
Jim Grosbach5d994042009-10-31 19:38:01 +0000695 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000696 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000697 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000698 setOperationAction(ISD::SRL, MVT::i64, Custom);
699 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000700
Evan Chenge8916542011-08-30 01:34:54 +0000701 if (!Subtarget->isThumb1Only()) {
702 // FIXME: We should do this for Thumb1 as well.
703 setOperationAction(ISD::ADDC, MVT::i32, Custom);
704 setOperationAction(ISD::ADDE, MVT::i32, Custom);
705 setOperationAction(ISD::SUBC, MVT::i32, Custom);
706 setOperationAction(ISD::SUBE, MVT::i32, Custom);
707 }
708
Evan Cheng10043e22007-01-19 07:51:42 +0000709 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000710 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000711 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000712 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000713 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000714 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000715
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000716 // These just redirect to CTTZ and CTLZ on ARM.
717 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
718 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
719
Tim Northoverbc933082013-05-23 19:11:20 +0000720 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
721
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000722 // Only ARMv6 has BSWAP.
723 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000724 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000725
Bob Wilsone8a549c2012-09-29 21:43:49 +0000726 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
727 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
728 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000729 setOperationAction(ISD::SDIV, MVT::i32, Expand);
730 setOperationAction(ISD::UDIV, MVT::i32, Expand);
731 }
Renato Golin87610692013-07-16 09:32:17 +0000732
733 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000734 setOperationAction(ISD::SREM, MVT::i32, Expand);
735 setOperationAction(ISD::UREM, MVT::i32, Expand);
736 // Register based DivRem for AEABI (RTABI 4.2)
737 if (Subtarget->isTargetAEABI()) {
738 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
739 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
740 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
741 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
742 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
743 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
744 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
745 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
746
747 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
748 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
749 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
750 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
751 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
752 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
753 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
754 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
755
756 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
757 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
758 } else {
Renato Golin87610692013-07-16 09:32:17 +0000759 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
760 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
761 }
Bob Wilson7117a912009-03-20 22:42:55 +0000762
Owen Anderson9f944592009-08-11 20:47:22 +0000763 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
764 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
765 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
766 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000767 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000768
Evan Cheng74d92c12011-04-08 21:37:21 +0000769 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000770
Evan Cheng10043e22007-01-19 07:51:42 +0000771 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000772 setOperationAction(ISD::VASTART, MVT::Other, Custom);
773 setOperationAction(ISD::VAARG, MVT::Other, Expand);
774 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
775 setOperationAction(ISD::VAEND, MVT::Other, Expand);
776 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
777 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000778
Tim Northoverd6a729b2014-01-06 14:28:05 +0000779 if (!Subtarget->isTargetMachO()) {
780 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000781 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000782 setExceptionPointerRegister(ARM::R0);
783 setExceptionSelectorRegister(ARM::R1);
784 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000785
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000786 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
787 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
788 else
789 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
790
Evan Cheng6e809de2010-08-11 06:22:01 +0000791 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000792 // the default expansion. If we are targeting a single threaded system,
793 // then set them all for expand so we can lower them later into their
794 // non-atomic form.
795 if (TM.Options.ThreadModel == ThreadModel::Single)
796 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
797 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000798 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
799 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000800 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000801
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000802 // On v8, we have particularly efficient implementations of atomic fences
803 // if they can be combined with nearby atomic loads and stores.
804 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000805 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000806 setInsertFencesForAtomic(true);
807 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000808 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000809 // If there's anything we can use as a barrier, go through custom lowering
810 // for ATOMIC_FENCE.
811 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
812 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
813
Jim Grosbach6860bb72010-06-18 22:35:32 +0000814 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000815 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000816 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000817 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000818 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000819 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000820 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000821 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000822 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000823 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000824 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000825 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000826 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000827 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
828 // Unordered/Monotonic case.
829 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
830 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000831 }
Evan Cheng10043e22007-01-19 07:51:42 +0000832
Evan Cheng21acf9f2010-11-04 05:19:35 +0000833 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000834
Eli Friedman8cfa7712010-06-26 04:36:50 +0000835 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
836 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000837 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
838 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000839 }
Owen Anderson9f944592009-08-11 20:47:22 +0000840 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000841
Eric Christopher824f42f2015-05-12 01:26:05 +0000842 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000843 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000844 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000845 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000846 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000847 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
848 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000849
850 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000851 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000852 if (Subtarget->isTargetDarwin()) {
853 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
854 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000855 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000856 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000857
Owen Anderson9f944592009-08-11 20:47:22 +0000858 setOperationAction(ISD::SETCC, MVT::i32, Expand);
859 setOperationAction(ISD::SETCC, MVT::f32, Expand);
860 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000861 setOperationAction(ISD::SELECT, MVT::i32, Custom);
862 setOperationAction(ISD::SELECT, MVT::f32, Custom);
863 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000864 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
865 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
866 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000867
Owen Anderson9f944592009-08-11 20:47:22 +0000868 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
869 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
870 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
871 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
872 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000873
Dan Gohman482732a2007-10-11 23:21:31 +0000874 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000875 setOperationAction(ISD::FSIN, MVT::f64, Expand);
876 setOperationAction(ISD::FSIN, MVT::f32, Expand);
877 setOperationAction(ISD::FCOS, MVT::f32, Expand);
878 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000879 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
880 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000881 setOperationAction(ISD::FREM, MVT::f64, Expand);
882 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000883 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000884 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000885 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
886 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000887 }
Owen Anderson9f944592009-08-11 20:47:22 +0000888 setOperationAction(ISD::FPOW, MVT::f64, Expand);
889 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000890
Evan Chengd0007f32012-04-10 21:40:28 +0000891 if (!Subtarget->hasVFP4()) {
892 setOperationAction(ISD::FMA, MVT::f64, Expand);
893 setOperationAction(ISD::FMA, MVT::f32, Expand);
894 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000895
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000896 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000897 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000898 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
899 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000900 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
901 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
902 }
903
904 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000905 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000906 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
907 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000908 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000909 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000910
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000911 // Combine sin / cos into one node or libcall if possible.
912 if (Subtarget->hasSinCos()) {
913 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
914 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000915 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000916 // For iOS, we don't want to the normal expansion of a libcall to
917 // sincos. We want to issue a libcall to __sincos_stret.
918 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
919 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
920 }
921 }
Evan Cheng10043e22007-01-19 07:51:42 +0000922
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000923 // FP-ARMv8 implements a lot of rounding-like FP operations.
924 if (Subtarget->hasFPARMv8()) {
925 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
926 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
927 setOperationAction(ISD::FROUND, MVT::f32, Legal);
928 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
929 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
930 setOperationAction(ISD::FRINT, MVT::f32, Legal);
931 if (!Subtarget->isFPOnlySP()) {
932 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
933 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
934 setOperationAction(ISD::FROUND, MVT::f64, Legal);
935 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
936 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
937 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000938 }
939 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000940 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000941 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000942 setTargetDAGCombine(ISD::ADD);
943 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000944 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000945 setTargetDAGCombine(ISD::AND);
946 setTargetDAGCombine(ISD::OR);
947 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000948
Evan Chengf258a152012-02-23 02:58:19 +0000949 if (Subtarget->hasV6Ops())
950 setTargetDAGCombine(ISD::SRL);
951
Evan Cheng10043e22007-01-19 07:51:42 +0000952 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000953
Eric Christopher824f42f2015-05-12 01:26:05 +0000954 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000955 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000956 setSchedulingPreference(Sched::RegPressure);
957 else
958 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000959
Evan Cheng3ae2b792011-01-06 06:52:41 +0000960 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000961 MaxStoresPerMemset = 8;
962 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
963 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
964 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
965 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
966 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000967
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000968 // On ARM arguments smaller than 4 bytes are extended, so all arguments
969 // are at least 4 bytes aligned.
970 setMinStackArgumentAlignment(4);
971
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000972 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000973 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000974
Eli Friedman2518f832011-05-06 20:34:06 +0000975 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000976}
977
Eric Christopher824f42f2015-05-12 01:26:05 +0000978bool ARMTargetLowering::useSoftFloat() const {
979 return Subtarget->useSoftFloat();
980}
981
Andrew Trick43f25632011-01-19 02:35:27 +0000982// FIXME: It might make sense to define the representative register class as the
983// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
984// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
985// SPR's representative would be DPR_VFP2. This should work well if register
986// pressure tracking were modified such that a register use would increment the
987// pressure of the register class's representative and all of it's super
988// classes' representatives transitively. We have not implemented this because
989// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000990// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000991// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000992std::pair<const TargetRegisterClass *, uint8_t>
993ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
994 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000995 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000996 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000997 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000998 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000999 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001000 // Use DPR as representative register class for all floating point
1001 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1002 // the cost is 1 for both f32 and f64.
1003 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001004 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001005 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001006 // When NEON is used for SP, only half of the register file is available
1007 // because operations that define both SP and DP results will be constrained
1008 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1009 // coalescing by double-counting the SP regs. See the FIXME above.
1010 if (Subtarget->useNEONForSinglePrecisionFP())
1011 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001012 break;
1013 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1014 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001015 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001016 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001017 break;
1018 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001019 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001020 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001021 break;
1022 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001023 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001024 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001025 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001026 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001027 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001028}
1029
Evan Cheng10043e22007-01-19 07:51:42 +00001030const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001031 switch ((ARMISD::NodeType)Opcode) {
1032 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001033 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001034 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001035 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001036 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001037 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001038 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001039 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1040 case ARMISD::tCALL: return "ARMISD::tCALL";
1041 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1042 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001043 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001044 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001045 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001046 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1047 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001048 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001049 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001050 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1051 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001052 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001053 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001054
Evan Cheng10043e22007-01-19 07:51:42 +00001055 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001056
Jim Grosbach8546ec92010-01-18 19:58:49 +00001057 case ARMISD::RBIT: return "ARMISD::RBIT";
1058
Evan Cheng10043e22007-01-19 07:51:42 +00001059 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1060 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1061 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001062
Evan Chenge8916542011-08-30 01:34:54 +00001063 case ARMISD::ADDC: return "ARMISD::ADDC";
1064 case ARMISD::ADDE: return "ARMISD::ADDE";
1065 case ARMISD::SUBC: return "ARMISD::SUBC";
1066 case ARMISD::SUBE: return "ARMISD::SUBE";
1067
Bob Wilson22806742010-09-22 22:09:21 +00001068 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1069 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001070
Evan Chengec6d7c92009-10-28 06:55:03 +00001071 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1072 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1073
Dale Johannesend679ff72010-06-03 21:09:53 +00001074 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001075
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001076 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001077
Evan Chengb972e562009-08-07 00:34:42 +00001078 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1079
Bob Wilson7ed59712010-10-30 00:54:37 +00001080 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001081
Evan Cheng8740ee32010-11-03 06:34:55 +00001082 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1083
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001084 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1085
Bob Wilson2e076c42009-06-22 23:27:02 +00001086 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001087 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001088 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001089 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1090 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001091 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1092 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001093 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1094 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001095 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1096 case ARMISD::VTST: return "ARMISD::VTST";
1097
1098 case ARMISD::VSHL: return "ARMISD::VSHL";
1099 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1100 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001101 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1102 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1103 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1104 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1105 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1106 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1107 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1108 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1109 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1110 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1111 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1112 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001113 case ARMISD::VSLI: return "ARMISD::VSLI";
1114 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001115 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1116 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001117 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001118 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001119 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001120 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001121 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001122 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001123 case ARMISD::VREV64: return "ARMISD::VREV64";
1124 case ARMISD::VREV32: return "ARMISD::VREV32";
1125 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001126 case ARMISD::VZIP: return "ARMISD::VZIP";
1127 case ARMISD::VUZP: return "ARMISD::VUZP";
1128 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001129 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1130 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001131 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1132 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001133 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1134 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001135 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001136 case ARMISD::FMAX: return "ARMISD::FMAX";
1137 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001138 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1139 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001140 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001141 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1142 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001143 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001144 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1145 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1146 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001147 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1148 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1149 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1150 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1151 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1152 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1153 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1154 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1155 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1156 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1157 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1158 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1159 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1160 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1161 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1162 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1163 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001164 }
Matthias Braund04893f2015-05-07 21:33:59 +00001165 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001166}
1167
Mehdi Amini44ede332015-07-09 02:09:04 +00001168EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1169 EVT VT) const {
1170 if (!VT.isVector())
1171 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001172 return VT.changeVectorElementTypeToInteger();
1173}
1174
Evan Cheng4cad68e2010-05-15 02:18:07 +00001175/// getRegClassFor - Return the register class that should be used for the
1176/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001177const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001178 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1179 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1180 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001181 if (Subtarget->hasNEON()) {
1182 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001183 return &ARM::QQPRRegClass;
1184 if (VT == MVT::v8i64)
1185 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001186 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001187 return TargetLowering::getRegClassFor(VT);
1188}
1189
John Brawn0dbcd652015-03-18 12:01:59 +00001190// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1191// source/dest is aligned and the copy size is large enough. We therefore want
1192// to align such objects passed to memory intrinsics.
1193bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1194 unsigned &PrefAlign) const {
1195 if (!isa<MemIntrinsic>(CI))
1196 return false;
1197 MinSize = 8;
1198 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1199 // cycle faster than 4-byte aligned LDM.
1200 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1201 return true;
1202}
1203
Eric Christopher84bdfd82010-07-21 22:26:11 +00001204// Create a fast isel object.
1205FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001206ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1207 const TargetLibraryInfo *libInfo) const {
1208 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001209}
1210
Evan Cheng4401f882010-05-20 23:26:43 +00001211Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001212 unsigned NumVals = N->getNumValues();
1213 if (!NumVals)
1214 return Sched::RegPressure;
1215
1216 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001217 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001218 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001219 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001220 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001221 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001222 }
Evan Chengbf914992010-05-28 23:25:23 +00001223
1224 if (!N->isMachineOpcode())
1225 return Sched::RegPressure;
1226
1227 // Load are scheduled for latency even if there instruction itinerary
1228 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001229 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001230 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001231
Evan Cheng6cc775f2011-06-28 19:10:37 +00001232 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001233 return Sched::RegPressure;
1234 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001235 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001236 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001237
Evan Cheng4401f882010-05-20 23:26:43 +00001238 return Sched::RegPressure;
1239}
1240
Evan Cheng10043e22007-01-19 07:51:42 +00001241//===----------------------------------------------------------------------===//
1242// Lowering Code
1243//===----------------------------------------------------------------------===//
1244
Evan Cheng10043e22007-01-19 07:51:42 +00001245/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1246static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1247 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001248 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001249 case ISD::SETNE: return ARMCC::NE;
1250 case ISD::SETEQ: return ARMCC::EQ;
1251 case ISD::SETGT: return ARMCC::GT;
1252 case ISD::SETGE: return ARMCC::GE;
1253 case ISD::SETLT: return ARMCC::LT;
1254 case ISD::SETLE: return ARMCC::LE;
1255 case ISD::SETUGT: return ARMCC::HI;
1256 case ISD::SETUGE: return ARMCC::HS;
1257 case ISD::SETULT: return ARMCC::LO;
1258 case ISD::SETULE: return ARMCC::LS;
1259 }
1260}
1261
Bob Wilsona2e83332009-09-09 23:14:54 +00001262/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1263static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001264 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001265 CondCode2 = ARMCC::AL;
1266 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001267 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001268 case ISD::SETEQ:
1269 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1270 case ISD::SETGT:
1271 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1272 case ISD::SETGE:
1273 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1274 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001275 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001276 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1277 case ISD::SETO: CondCode = ARMCC::VC; break;
1278 case ISD::SETUO: CondCode = ARMCC::VS; break;
1279 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1280 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1281 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1282 case ISD::SETLT:
1283 case ISD::SETULT: CondCode = ARMCC::LT; break;
1284 case ISD::SETLE:
1285 case ISD::SETULE: CondCode = ARMCC::LE; break;
1286 case ISD::SETNE:
1287 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1288 }
Evan Cheng10043e22007-01-19 07:51:42 +00001289}
1290
Bob Wilsona4c22902009-04-17 19:07:39 +00001291//===----------------------------------------------------------------------===//
1292// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001293//===----------------------------------------------------------------------===//
1294
1295#include "ARMGenCallingConv.inc"
1296
Oliver Stannardc24f2172014-05-09 14:01:47 +00001297/// getEffectiveCallingConv - Get the effective calling convention, taking into
1298/// account presence of floating point hardware and calling convention
1299/// limitations, such as support for variadic functions.
1300CallingConv::ID
1301ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1302 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001303 switch (CC) {
1304 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001305 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001306 case CallingConv::ARM_AAPCS:
1307 case CallingConv::ARM_APCS:
1308 case CallingConv::GHC:
1309 return CC;
1310 case CallingConv::ARM_AAPCS_VFP:
1311 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1312 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001313 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001314 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001315 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001316 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1317 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001318 return CallingConv::ARM_AAPCS_VFP;
1319 else
1320 return CallingConv::ARM_AAPCS;
1321 case CallingConv::Fast:
1322 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001323 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001324 return CallingConv::Fast;
1325 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001326 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001327 return CallingConv::ARM_AAPCS_VFP;
1328 else
1329 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001330 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001331}
1332
1333/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1334/// CallingConvention.
1335CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1336 bool Return,
1337 bool isVarArg) const {
1338 switch (getEffectiveCallingConv(CC, isVarArg)) {
1339 default:
1340 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001341 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001342 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001343 case CallingConv::ARM_AAPCS:
1344 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1345 case CallingConv::ARM_AAPCS_VFP:
1346 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1347 case CallingConv::Fast:
1348 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001349 case CallingConv::GHC:
1350 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001351 }
1352}
1353
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001354/// LowerCallResult - Lower the result values of a call into the
1355/// appropriate copies out of appropriate physical registers.
1356SDValue
1357ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001358 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001359 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001360 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001361 SmallVectorImpl<SDValue> &InVals,
1362 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001363
Bob Wilsona4c22902009-04-17 19:07:39 +00001364 // Assign locations to each value returned by this call.
1365 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001366 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1367 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001368 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001369 CCAssignFnForNode(CallConv, /* Return*/ true,
1370 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001371
1372 // Copy all of the result registers out of their specified physreg.
1373 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1374 CCValAssign VA = RVLocs[i];
1375
Stephen Linb8bd2322013-04-20 05:14:40 +00001376 // Pass 'this' value directly from the argument to return value, to avoid
1377 // reg unit interference
1378 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001379 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1380 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001381 InVals.push_back(ThisVal);
1382 continue;
1383 }
1384
Bob Wilson0041bd32009-04-25 00:33:20 +00001385 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001386 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001387 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001388 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001389 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001390 Chain = Lo.getValue(1);
1391 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001392 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001393 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001394 InFlag);
1395 Chain = Hi.getValue(1);
1396 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001397 if (!Subtarget->isLittle())
1398 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001399 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001400
Owen Anderson9f944592009-08-11 20:47:22 +00001401 if (VA.getLocVT() == MVT::v2f64) {
1402 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1403 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001404 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001405
1406 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001407 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001408 Chain = Lo.getValue(1);
1409 InFlag = Lo.getValue(2);
1410 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001411 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001412 Chain = Hi.getValue(1);
1413 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001414 if (!Subtarget->isLittle())
1415 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001416 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001417 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001418 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001419 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001420 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001421 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1422 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001423 Chain = Val.getValue(1);
1424 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001425 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001426
1427 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001428 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001429 case CCValAssign::Full: break;
1430 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001431 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001432 break;
1433 }
1434
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001435 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001436 }
1437
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001438 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001439}
1440
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001441/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001442SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001443ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1444 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001445 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001446 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001447 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001448 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001449 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001450 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1451 StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001452 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001453 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001454 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001455}
1456
Andrew Trickef9de2a2013-05-25 02:42:55 +00001457void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001458 SDValue Chain, SDValue &Arg,
1459 RegsToPassVector &RegsToPass,
1460 CCValAssign &VA, CCValAssign &NextVA,
1461 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001462 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001463 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001464
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001465 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001466 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001467 unsigned id = Subtarget->isLittle() ? 0 : 1;
1468 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001469
1470 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001471 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001472 else {
1473 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001474 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001475 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1476 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001477
Christian Pirkerb5728192014-05-08 14:06:24 +00001478 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001479 dl, DAG, NextVA,
1480 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001481 }
1482}
1483
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001484/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001485/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1486/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001487SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001488ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001489 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001490 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001491 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001492 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1493 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1494 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001495 SDValue Chain = CLI.Chain;
1496 SDValue Callee = CLI.Callee;
1497 bool &isTailCall = CLI.IsTailCall;
1498 CallingConv::ID CallConv = CLI.CallConv;
1499 bool doesNotRet = CLI.DoesNotReturn;
1500 bool isVarArg = CLI.IsVarArg;
1501
Dale Johannesend679ff72010-06-03 21:09:53 +00001502 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001503 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1504 bool isThisReturn = false;
1505 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001506 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001507
Bob Wilson8decdc42011-10-07 17:17:49 +00001508 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001509 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001510 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001511
Dale Johannesend679ff72010-06-03 21:09:53 +00001512 if (isTailCall) {
1513 // Check if it's really possible to do a tail call.
1514 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001515 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001516 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001517 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1518 report_fatal_error("failed to perform tail call elimination on a call "
1519 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001520 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1521 // detected sibcalls.
1522 if (isTailCall) {
1523 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001524 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001525 }
1526 }
Evan Cheng10043e22007-01-19 07:51:42 +00001527
Bob Wilsona4c22902009-04-17 19:07:39 +00001528 // Analyze operands of the call, assigning locations to each operand.
1529 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001530 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1531 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001532 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001533 CCAssignFnForNode(CallConv, /* Return*/ false,
1534 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001535
Bob Wilsona4c22902009-04-17 19:07:39 +00001536 // Get a count of how many bytes are to be pushed on the stack.
1537 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001538
Dale Johannesend679ff72010-06-03 21:09:53 +00001539 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001540 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001541 NumBytes = 0;
1542
Evan Cheng10043e22007-01-19 07:51:42 +00001543 // Adjust the stack pointer for the new arguments...
1544 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001545 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001546 Chain = DAG.getCALLSEQ_START(Chain,
1547 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001548
Mehdi Amini44ede332015-07-09 02:09:04 +00001549 SDValue StackPtr =
1550 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001551
Bob Wilson2e076c42009-06-22 23:27:02 +00001552 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001553 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001554
Bob Wilsona4c22902009-04-17 19:07:39 +00001555 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001556 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001557 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1558 i != e;
1559 ++i, ++realArgIdx) {
1560 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001561 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001562 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001563 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001564
Bob Wilsona4c22902009-04-17 19:07:39 +00001565 // Promote the value if needed.
1566 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001567 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001568 case CCValAssign::Full: break;
1569 case CCValAssign::SExt:
1570 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1571 break;
1572 case CCValAssign::ZExt:
1573 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1574 break;
1575 case CCValAssign::AExt:
1576 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1577 break;
1578 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001579 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001580 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001581 }
1582
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001583 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001584 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001585 if (VA.getLocVT() == MVT::v2f64) {
1586 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001587 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001588 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001589 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001590
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001591 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001592 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1593
1594 VA = ArgLocs[++i]; // skip ahead to next loc
1595 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001596 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001597 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1598 } else {
1599 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001600
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001601 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1602 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001603 }
1604 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001605 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001606 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001607 }
1608 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001609 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1610 assert(VA.getLocVT() == MVT::i32 &&
1611 "unexpected calling convention register assignment");
1612 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001613 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001614 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001615 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001616 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001617 } else if (isByVal) {
1618 assert(VA.isMemLoc());
1619 unsigned offset = 0;
1620
1621 // True if this byval aggregate will be split between registers
1622 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001623 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001624 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001625
1626 if (CurByValIdx < ByValArgsCount) {
1627
1628 unsigned RegBegin, RegEnd;
1629 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1630
Mehdi Amini44ede332015-07-09 02:09:04 +00001631 EVT PtrVT =
1632 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001633 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001634 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001635 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001636 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1637 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1638 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001639 false, false, false,
1640 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001641 MemOpChains.push_back(Load.getValue(1));
1642 RegsToPass.push_back(std::make_pair(j, Load));
1643 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001644
1645 // If parameter size outsides register area, "offset" value
1646 // helps us to calculate stack slot for remained part properly.
1647 offset = RegEnd - RegBegin;
1648
1649 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001650 }
1651
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001652 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001653 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001654 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001655 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001656 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001657 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001658 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001659 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001660 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001661 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1662 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001663
Manman Ren9f911162012-06-01 02:44:42 +00001664 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001665 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001666 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001667 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001668 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001669 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001670 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001671
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001672 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1673 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001674 }
Evan Cheng10043e22007-01-19 07:51:42 +00001675 }
1676
1677 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001678 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001679
1680 // Build a sequence of copy-to-reg nodes chained together with token chain
1681 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001682 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001683 // Tail call byval lowering might overwrite argument registers so in case of
1684 // tail call optimization the copies to registers are lowered later.
1685 if (!isTailCall)
1686 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1687 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1688 RegsToPass[i].second, InFlag);
1689 InFlag = Chain.getValue(1);
1690 }
Evan Cheng10043e22007-01-19 07:51:42 +00001691
Dale Johannesend679ff72010-06-03 21:09:53 +00001692 // For tail calls lower the arguments to the 'real' stack slot.
1693 if (isTailCall) {
1694 // Force all the incoming stack arguments to be loaded from the stack
1695 // before any new outgoing arguments are stored to the stack, because the
1696 // outgoing stack slots may alias the incoming argument stack slots, and
1697 // the alias isn't otherwise explicit. This is slightly more conservative
1698 // than necessary, because it means that each store effectively depends
1699 // on every argument instead of just those arguments it would clobber.
1700
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001701 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001702 InFlag = SDValue();
1703 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1704 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1705 RegsToPass[i].second, InFlag);
1706 InFlag = Chain.getValue(1);
1707 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001708 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001709 }
1710
Bill Wendling24c79f22008-09-16 21:48:12 +00001711 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1712 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1713 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001714 bool isDirect = false;
1715 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001716 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001717 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00001718 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00001719
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00001720 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001721 assert((Subtarget->isTargetWindows() ||
1722 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1723 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001724 // Handle a global address or an external symbol. If it's not one of
1725 // those, the target's already in a register, so we don't need to do
1726 // anything extra.
1727 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001728 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001729 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001730 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001731 ARMConstantPoolValue *CPV =
1732 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1733
Jim Grosbach32bb3622010-04-14 22:28:31 +00001734 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001735 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001736 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001737 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1738 MachinePointerInfo::getConstantPool(), false, false,
1739 false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001740 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1741 const char *Sym = S->getSymbol();
1742
1743 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001744 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001745 ARMConstantPoolValue *CPV =
1746 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1747 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001748 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00001749 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001750 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001751 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1752 MachinePointerInfo::getConstantPool(), false, false,
1753 false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001754 }
1755 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001756 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001757 isDirect = true;
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001758 bool isDef = GV->isStrongDefinitionForLinker();
1759 bool isStub = (!isDef && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001760 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001761 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001762 // ARM call to a local ARM function is predicable.
Peter Collingbourne6a9d1772015-07-05 20:52:35 +00001763 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001764 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001765 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001766 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Mehdi Amini44ede332015-07-09 02:09:04 +00001767 Callee = DAG.getNode(
1768 ARMISD::WrapperPIC, dl, PtrVt,
1769 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
1770 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), Callee,
Tim Northoverd4d294d2014-08-06 11:13:06 +00001771 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001772 } else if (Subtarget->isTargetCOFF()) {
1773 assert(Subtarget->isTargetWindows() &&
1774 "Windows is the only supported COFF target");
Reid Klecknerc35e7f52015-06-11 01:31:48 +00001775 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1776 ? ARMII::MO_DLLIMPORT
1777 : ARMII::MO_NO_FLAG;
Mehdi Amini44ede332015-07-09 02:09:04 +00001778 Callee =
1779 DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0, TargetFlags);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001780 if (GV->hasDLLImportStorageClass())
Mehdi Amini44ede332015-07-09 02:09:04 +00001781 Callee =
1782 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
1783 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
1784 MachinePointerInfo::getGOT(), false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001785 } else {
1786 // On ELF targets for PIC code, direct calls should go through the PLT
1787 unsigned OpFlags = 0;
1788 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001789 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001790 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001791 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001792 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001793 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001794 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001795 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001796 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001797 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001798 // tBX takes a register source operand.
1799 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001800 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001801 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001802 ARMConstantPoolValue *CPV =
1803 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1804 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00001805 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001806 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Mehdi Amini44ede332015-07-09 02:09:04 +00001807 Callee = DAG.getLoad(PtrVt, dl, DAG.getEntryNode(), CPAddr,
1808 MachinePointerInfo::getConstantPool(), false, false,
1809 false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001810 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001811 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001812 } else {
1813 unsigned OpFlags = 0;
1814 // On ELF targets for PIC code, direct calls should go through the PLT
1815 if (Subtarget->isTargetELF() &&
1816 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1817 OpFlags = ARMII::MO_PLT;
Mehdi Amini44ede332015-07-09 02:09:04 +00001818 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, OpFlags);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001819 }
Evan Cheng10043e22007-01-19 07:51:42 +00001820 }
1821
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001822 // FIXME: handle tail calls differently.
1823 unsigned CallOpc;
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001824 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001825 if (Subtarget->isThumb()) {
1826 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001827 CallOpc = ARMISD::CALL_NOLINK;
1828 else
1829 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1830 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001831 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001832 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001833 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001834 // Emit regular call when code size is the priority
1835 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001836 // "mov lr, pc; b _foo" to avoid confusing the RSP
1837 CallOpc = ARMISD::CALL_NOLINK;
1838 else
1839 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001840 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001841
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001842 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001843 Ops.push_back(Chain);
1844 Ops.push_back(Callee);
1845
1846 // Add argument registers to the end of the list so that they are known live
1847 // into the call.
1848 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1849 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1850 RegsToPass[i].second.getValueType()));
1851
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001852 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001853 if (!isTailCall) {
1854 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001855 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001856 if (isThisReturn) {
1857 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001858 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001859 if (!Mask) {
1860 // Set isThisReturn to false if the calling convention is not one that
1861 // allows 'returned' to be modeled in this way, so LowerCallResult does
1862 // not try to pass 'this' straight through
1863 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001864 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001865 }
1866 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001867 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001868
Matthias Braunc22630e2013-10-04 16:52:54 +00001869 assert(Mask && "Missing call preserved mask for calling convention");
1870 Ops.push_back(DAG.getRegisterMask(Mask));
1871 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001872
Gabor Greiff304a7a2008-08-28 21:40:38 +00001873 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001874 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001875
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001876 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001877 if (isTailCall) {
1878 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001879 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001880 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001881
Duncan Sands739a0542008-07-02 17:40:58 +00001882 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001883 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001884 InFlag = Chain.getValue(1);
1885
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001886 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1887 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001888 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001889 InFlag = Chain.getValue(1);
1890
Bob Wilsona4c22902009-04-17 19:07:39 +00001891 // Handle result values, copying them out of physregs into vregs that we
1892 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001893 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001894 InVals, isThisReturn,
1895 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001896}
1897
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001898/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001899/// on the stack. Remember the next parameter register to allocate,
1900/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001901/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001902void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1903 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001904 assert((State->getCallOrPrologue() == Prologue ||
1905 State->getCallOrPrologue() == Call) &&
1906 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001907
Tim Northover8cda34f2015-03-11 18:54:22 +00001908 // Byval (as with any stack) slots are always at least 4 byte aligned.
1909 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001910
Tim Northover8cda34f2015-03-11 18:54:22 +00001911 unsigned Reg = State->AllocateReg(GPRArgRegs);
1912 if (!Reg)
1913 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001914
Tim Northover8cda34f2015-03-11 18:54:22 +00001915 unsigned AlignInRegs = Align / 4;
1916 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1917 for (unsigned i = 0; i < Waste; ++i)
1918 Reg = State->AllocateReg(GPRArgRegs);
1919
1920 if (!Reg)
1921 return;
1922
1923 unsigned Excess = 4 * (ARM::R4 - Reg);
1924
1925 // Special case when NSAA != SP and parameter size greater than size of
1926 // all remained GPR regs. In that case we can't split parameter, we must
1927 // send it to stack. We also must set NCRN to R4, so waste all
1928 // remained registers.
1929 const unsigned NSAAOffset = State->getNextStackOffset();
1930 if (NSAAOffset != 0 && Size > Excess) {
1931 while (State->AllocateReg(GPRArgRegs))
1932 ;
1933 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001934 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001935
1936 // First register for byval parameter is the first register that wasn't
1937 // allocated before this method call, so it would be "reg".
1938 // If parameter is small enough to be saved in range [reg, r4), then
1939 // the end (first after last) register would be reg + param-size-in-regs,
1940 // else parameter would be splitted between registers and stack,
1941 // end register would be r4 in this case.
1942 unsigned ByValRegBegin = Reg;
1943 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1944 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1945 // Note, first register is allocated in the beginning of function already,
1946 // allocate remained amount of registers we need.
1947 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1948 State->AllocateReg(GPRArgRegs);
1949 // A byval parameter that is split between registers and memory needs its
1950 // size truncated here.
1951 // In the case where the entire structure fits in registers, we set the
1952 // size in memory to zero.
1953 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001954}
1955
Dale Johannesend679ff72010-06-03 21:09:53 +00001956/// MatchingStackOffset - Return true if the given stack call argument is
1957/// already available in the same position (relatively) of the caller's
1958/// incoming argument stack.
1959static
1960bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1961 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001962 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001963 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1964 int FI = INT_MAX;
1965 if (Arg.getOpcode() == ISD::CopyFromReg) {
1966 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001967 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001968 return false;
1969 MachineInstr *Def = MRI->getVRegDef(VR);
1970 if (!Def)
1971 return false;
1972 if (!Flags.isByVal()) {
1973 if (!TII->isLoadFromStackSlot(Def, FI))
1974 return false;
1975 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001976 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001977 }
1978 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1979 if (Flags.isByVal())
1980 // ByVal argument is passed in as a pointer but it's now being
1981 // dereferenced. e.g.
1982 // define @foo(%struct.X* %A) {
1983 // tail call @bar(%struct.X* byval %A)
1984 // }
1985 return false;
1986 SDValue Ptr = Ld->getBasePtr();
1987 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1988 if (!FINode)
1989 return false;
1990 FI = FINode->getIndex();
1991 } else
1992 return false;
1993
1994 assert(FI != INT_MAX);
1995 if (!MFI->isFixedObjectIndex(FI))
1996 return false;
1997 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1998}
1999
2000/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2001/// for tail call optimization. Targets which want to do tail call
2002/// optimization should implement this function.
2003bool
2004ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2005 CallingConv::ID CalleeCC,
2006 bool isVarArg,
2007 bool isCalleeStructRet,
2008 bool isCallerStructRet,
2009 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002010 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002011 const SmallVectorImpl<ISD::InputArg> &Ins,
2012 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00002013 const Function *CallerF = DAG.getMachineFunction().getFunction();
2014 CallingConv::ID CallerCC = CallerF->getCallingConv();
2015 bool CCMatch = CallerCC == CalleeCC;
2016
2017 // Look for obvious safe cases to perform tail call optimization that do not
2018 // require ABI changes. This is what gcc calls sibcall.
2019
Jim Grosbache3864cc2010-06-16 23:45:49 +00002020 // Do not sibcall optimize vararg calls unless the call site is not passing
2021 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002022 if (isVarArg && !Outs.empty())
2023 return false;
2024
Tim Northoverd8407452013-10-01 14:33:28 +00002025 // Exception-handling functions need a special set of instructions to indicate
2026 // a return to the hardware. Tail-calling another function would probably
2027 // break this.
2028 if (CallerF->hasFnAttribute("interrupt"))
2029 return false;
2030
Dale Johannesend679ff72010-06-03 21:09:53 +00002031 // Also avoid sibcall optimization if either caller or callee uses struct
2032 // return semantics.
2033 if (isCalleeStructRet || isCallerStructRet)
2034 return false;
2035
Eric Christopherae326492015-03-12 22:48:50 +00002036 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002037 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2038 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2039 // support in the assembler and linker to be used. This would need to be
2040 // fixed to fully support tail calls in Thumb1.
2041 //
Dale Johannesene2289282010-07-08 01:18:23 +00002042 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2043 // LR. This means if we need to reload LR, it takes an extra instructions,
2044 // which outweighs the value of the tail call; but here we don't know yet
2045 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002046 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002047 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002048
2049 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2050 // but we need to make sure there are enough registers; the only valid
2051 // registers are the 4 used for parameters. We don't currently do this
2052 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002053 if (Subtarget->isThumb1Only())
2054 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002055
Oliver Stannard12993dd2014-08-18 12:42:15 +00002056 // Externally-defined functions with weak linkage should not be
2057 // tail-called on ARM when the OS does not support dynamic
2058 // pre-emption of symbols, as the AAELF spec requires normal calls
2059 // to undefined weak functions to be replaced with a NOP or jump to the
2060 // next instruction. The behaviour of branch instructions in this
2061 // situation (as used for tail calls) is implementation-defined, so we
2062 // cannot rely on the linker replacing the tail call with a return.
2063 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2064 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002065 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002066 if (GV->hasExternalWeakLinkage() &&
2067 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002068 return false;
2069 }
2070
Dale Johannesend679ff72010-06-03 21:09:53 +00002071 // If the calling conventions do not match, then we'd better make sure the
2072 // results are returned in the same way as what the caller expects.
2073 if (!CCMatch) {
2074 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002075 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2076 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002077 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2078
2079 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002080 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2081 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002082 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2083
2084 if (RVLocs1.size() != RVLocs2.size())
2085 return false;
2086 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2087 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2088 return false;
2089 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2090 return false;
2091 if (RVLocs1[i].isRegLoc()) {
2092 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2093 return false;
2094 } else {
2095 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2096 return false;
2097 }
2098 }
2099 }
2100
Manman Ren7e48b252012-10-12 23:39:43 +00002101 // If Caller's vararg or byval argument has been split between registers and
2102 // stack, do not perform tail call, since part of the argument is in caller's
2103 // local frame.
2104 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2105 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002106 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002107 return false;
2108
Dale Johannesend679ff72010-06-03 21:09:53 +00002109 // If the callee takes no arguments then go on to check the results of the
2110 // call.
2111 if (!Outs.empty()) {
2112 // Check if stack adjustment is needed. For now, do not do this if any
2113 // argument is passed on the stack.
2114 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002115 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2116 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002117 CCInfo.AnalyzeCallOperands(Outs,
2118 CCAssignFnForNode(CalleeCC, false, isVarArg));
2119 if (CCInfo.getNextStackOffset()) {
2120 MachineFunction &MF = DAG.getMachineFunction();
2121
2122 // Check if the arguments are already laid out in the right way as
2123 // the caller's fixed stack objects.
2124 MachineFrameInfo *MFI = MF.getFrameInfo();
2125 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002126 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002127 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2128 i != e;
2129 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002130 CCValAssign &VA = ArgLocs[i];
2131 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002132 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002133 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002134 if (VA.getLocInfo() == CCValAssign::Indirect)
2135 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002136 if (VA.needsCustom()) {
2137 // f64 and vector types are split into multiple registers or
2138 // register/stack-slot combinations. The types will not match
2139 // the registers; give up on memory f64 refs until we figure
2140 // out what to do about this.
2141 if (!VA.isRegLoc())
2142 return false;
2143 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002144 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002145 if (RegVT == MVT::v2f64) {
2146 if (!ArgLocs[++i].isRegLoc())
2147 return false;
2148 if (!ArgLocs[++i].isRegLoc())
2149 return false;
2150 }
2151 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002152 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2153 MFI, MRI, TII))
2154 return false;
2155 }
2156 }
2157 }
2158 }
2159
2160 return true;
2161}
2162
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002163bool
2164ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2165 MachineFunction &MF, bool isVarArg,
2166 const SmallVectorImpl<ISD::OutputArg> &Outs,
2167 LLVMContext &Context) const {
2168 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002169 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002170 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2171 isVarArg));
2172}
2173
Tim Northoverd8407452013-10-01 14:33:28 +00002174static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2175 SDLoc DL, SelectionDAG &DAG) {
2176 const MachineFunction &MF = DAG.getMachineFunction();
2177 const Function *F = MF.getFunction();
2178
2179 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2180
2181 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2182 // version of the "preferred return address". These offsets affect the return
2183 // instruction if this is a return from PL1 without hypervisor extensions.
2184 // IRQ/FIQ: +4 "subs pc, lr, #4"
2185 // SWI: 0 "subs pc, lr, #0"
2186 // ABORT: +4 "subs pc, lr, #4"
2187 // UNDEF: +4/+2 "subs pc, lr, #0"
2188 // UNDEF varies depending on where the exception came from ARM or Thumb
2189 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2190
2191 int64_t LROffset;
2192 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2193 IntKind == "ABORT")
2194 LROffset = 4;
2195 else if (IntKind == "SWI" || IntKind == "UNDEF")
2196 LROffset = 0;
2197 else
2198 report_fatal_error("Unsupported interrupt attribute. If present, value "
2199 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2200
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002201 RetOps.insert(RetOps.begin() + 1,
2202 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002203
Craig Topper48d114b2014-04-26 18:35:24 +00002204 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002205}
2206
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002207SDValue
2208ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002209 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002211 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002212 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002213
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002214 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002215 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002216
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002217 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002218 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2219 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002220
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002221 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002222 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2223 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002224
Bob Wilsona4c22902009-04-17 19:07:39 +00002225 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002226 SmallVector<SDValue, 4> RetOps;
2227 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002228 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002229
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002230 MachineFunction &MF = DAG.getMachineFunction();
2231 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2232 AFI->setReturnRegsCount(RVLocs.size());
2233
Bob Wilsona4c22902009-04-17 19:07:39 +00002234 // Copy the result values into the output registers.
2235 for (unsigned i = 0, realRVLocIdx = 0;
2236 i != RVLocs.size();
2237 ++i, ++realRVLocIdx) {
2238 CCValAssign &VA = RVLocs[i];
2239 assert(VA.isRegLoc() && "Can only return in registers!");
2240
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002241 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002242
2243 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002244 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002245 case CCValAssign::Full: break;
2246 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002247 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002248 break;
2249 }
2250
Bob Wilsona4c22902009-04-17 19:07:39 +00002251 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002252 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002253 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002254 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002255 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002256 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002257 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002258
Christian Pirkerb5728192014-05-08 14:06:24 +00002259 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2260 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2261 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002262 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002263 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002264 VA = RVLocs[++i]; // skip ahead to next loc
2265 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002266 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
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
2272 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002273 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002274 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002275 }
2276 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2277 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002278 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002279 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002280 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2281 fmrrd.getValue(isLittleEndian ? 0 : 1),
2282 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002283 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002284 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002285 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002286 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2287 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002288 Flag);
2289 } else
2290 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2291
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002292 // Guarantee that all emitted copies are
2293 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002294 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002295 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002296 }
2297
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002298 // Update chain and glue.
2299 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002300 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002301 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002302
Tim Northoverd8407452013-10-01 14:33:28 +00002303 // CPUs which aren't M-class use a special sequence to return from
2304 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2305 // though we use "subs pc, lr, #N").
2306 //
2307 // M-class CPUs actually use a normal return sequence with a special
2308 // (hardware-provided) value in LR, so the normal code path works.
2309 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2310 !Subtarget->isMClass()) {
2311 if (Subtarget->isThumb1Only())
2312 report_fatal_error("interrupt attribute is not supported in Thumb1");
2313 return LowerInterruptReturn(RetOps, dl, DAG);
2314 }
2315
Craig Topper48d114b2014-04-26 18:35:24 +00002316 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002317}
2318
Evan Chengf8bad082012-04-10 01:51:00 +00002319bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002320 if (N->getNumValues() != 1)
2321 return false;
2322 if (!N->hasNUsesOfValue(1, 0))
2323 return false;
2324
Evan Chengf8bad082012-04-10 01:51:00 +00002325 SDValue TCChain = Chain;
2326 SDNode *Copy = *N->use_begin();
2327 if (Copy->getOpcode() == ISD::CopyToReg) {
2328 // If the copy has a glue operand, we conservatively assume it isn't safe to
2329 // perform a tail call.
2330 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2331 return false;
2332 TCChain = Copy->getOperand(0);
2333 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2334 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002335 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002336 SmallPtrSet<SDNode*, 2> Copies;
2337 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002338 UI != UE; ++UI) {
2339 if (UI->getOpcode() != ISD::CopyToReg)
2340 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002341 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002342 }
Evan Chengf8bad082012-04-10 01:51:00 +00002343 if (Copies.size() > 2)
2344 return false;
2345
2346 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2347 UI != UE; ++UI) {
2348 SDValue UseChain = UI->getOperand(0);
2349 if (Copies.count(UseChain.getNode()))
2350 // Second CopyToReg
2351 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002352 else {
2353 // We are at the top of this chain.
2354 // If the copy has a glue operand, we conservatively assume it
2355 // isn't safe to perform a tail call.
2356 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2357 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002358 // First CopyToReg
2359 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002360 }
Evan Chengf8bad082012-04-10 01:51:00 +00002361 }
2362 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002363 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002364 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002365 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002366 Copy = *Copy->use_begin();
2367 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002368 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002369 // If the copy has a glue operand, we conservatively assume it isn't safe to
2370 // perform a tail call.
2371 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2372 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002373 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002374 } else {
2375 return false;
2376 }
2377
Evan Cheng419ea282010-12-01 22:59:46 +00002378 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002379 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2380 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002381 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2382 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002383 return false;
2384 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002385 }
2386
Evan Chengf8bad082012-04-10 01:51:00 +00002387 if (!HasRet)
2388 return false;
2389
2390 Chain = TCChain;
2391 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002392}
2393
Evan Cheng0663f232011-03-21 01:19:09 +00002394bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002395 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002396 return false;
2397
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002398 auto Attr =
2399 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2400 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002401 return false;
2402
2403 return !Subtarget->isThumb1Only();
2404}
2405
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002406// Trying to write a 64 bit value so need to split into two 32 bit values first,
2407// and pass the lower and high parts through.
2408static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2409 SDLoc DL(Op);
2410 SDValue WriteValue = Op->getOperand(2);
2411
2412 // This function is only supposed to be called for i64 type argument.
2413 assert(WriteValue.getValueType() == MVT::i64
2414 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2415
2416 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2417 DAG.getConstant(0, DL, MVT::i32));
2418 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2419 DAG.getConstant(1, DL, MVT::i32));
2420 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2421 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2422}
2423
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002424// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2425// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2426// one of the above mentioned nodes. It has to be wrapped because otherwise
2427// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2428// be used to form addressing mode. These wrapped nodes will be selected
2429// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002430static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002431 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002432 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002433 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002434 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002435 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002436 if (CP->isMachineConstantPoolEntry())
2437 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2438 CP->getAlignment());
2439 else
2440 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2441 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002442 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002443}
2444
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002445unsigned ARMTargetLowering::getJumpTableEncoding() const {
2446 return MachineJumpTableInfo::EK_Inline;
2447}
2448
Dan Gohman21cea8a2010-04-17 15:26:15 +00002449SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2450 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002451 MachineFunction &MF = DAG.getMachineFunction();
2452 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2453 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002454 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002455 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002456 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002457 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2458 SDValue CPAddr;
2459 if (RelocM == Reloc::Static) {
2460 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2461 } else {
2462 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002463 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002464 ARMConstantPoolValue *CPV =
2465 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2466 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002467 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2468 }
2469 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2470 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002471 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002472 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002473 if (RelocM == Reloc::Static)
2474 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002475 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002476 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002477}
2478
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002479// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002480SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002481ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002482 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002483 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002484 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002485 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002486 MachineFunction &MF = DAG.getMachineFunction();
2487 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002488 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002489 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002490 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2491 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002492 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002493 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002494 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002495 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002496 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002497 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002498
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002499 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002500 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002501
2502 // call __tls_get_addr.
2503 ArgListTy Args;
2504 ArgListEntry Entry;
2505 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002506 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002507 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002508
Dale Johannesen555a3752009-01-30 23:10:59 +00002509 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002510 TargetLowering::CallLoweringInfo CLI(DAG);
2511 CLI.setDebugLoc(dl).setChain(Chain)
2512 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002513 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2514 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002515
Justin Holewinskiaa583972012-05-25 16:35:28 +00002516 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002517 return CallResult.first;
2518}
2519
2520// Lower ISD::GlobalTLSAddress using the "initial exec" or
2521// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002522SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002523ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002524 SelectionDAG &DAG,
2525 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002526 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002527 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002528 SDValue Offset;
2529 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002530 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002531 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002532 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002533
Hans Wennborgaea41202012-05-04 09:40:39 +00002534 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002535 MachineFunction &MF = DAG.getMachineFunction();
2536 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002537 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002538 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002539 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2540 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002541 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2542 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2543 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002544 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002545 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002546 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002547 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002548 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002549 Chain = Offset.getValue(1);
2550
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002551 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002552 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002553
Evan Chengcdbb70c2009-10-31 03:39:36 +00002554 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002555 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002556 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002557 } else {
2558 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002559 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002560 ARMConstantPoolValue *CPV =
2561 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002562 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002563 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002564 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002565 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002566 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002567 }
2568
2569 // The address of the thread local variable is the add of the thread
2570 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002571 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002572}
2573
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002574SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002575ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002576 // TODO: implement the "local dynamic" model
2577 assert(Subtarget->isTargetELF() &&
2578 "TLS not implemented for non-ELF targets");
2579 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002580
2581 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2582
2583 switch (model) {
2584 case TLSModel::GeneralDynamic:
2585 case TLSModel::LocalDynamic:
2586 return LowerToTLSGeneralDynamicModel(GA, DAG);
2587 case TLSModel::InitialExec:
2588 case TLSModel::LocalExec:
2589 return LowerToTLSExecModels(GA, DAG, model);
2590 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002591 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002592}
2593
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002594SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002595 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002596 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002597 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002598 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002599 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002600 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002601 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002602 ARMConstantPoolConstant::Create(GV,
2603 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002604 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002605 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002606 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002607 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002608 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002609 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002610 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002611 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002612 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002613 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002614 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002615 MachinePointerInfo::getGOT(),
2616 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002617 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002618 }
2619
2620 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002621 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002622 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002623 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002624 // FIXME: Once remat is capable of dealing with instructions with register
2625 // operands, expand this into two nodes.
2626 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2627 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002628 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002629 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2630 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2631 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2632 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002633 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002634 }
2635}
2636
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002637SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002638 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002639 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002640 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002641 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002642 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002643
Eric Christopherc1058df2014-07-04 01:55:26 +00002644 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002645 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002646
Tim Northover72360d22013-12-02 10:35:41 +00002647 // FIXME: Once remat is capable of dealing with instructions with register
2648 // operands, expand this into multiple nodes
2649 unsigned Wrapper =
2650 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002651
Tim Northover72360d22013-12-02 10:35:41 +00002652 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2653 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002654
Evan Cheng1b389522009-09-03 07:04:02 +00002655 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002656 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2657 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002658 return Result;
2659}
2660
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002661SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2662 SelectionDAG &DAG) const {
2663 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002664 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2665 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002666
2667 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002668 const ARMII::TOF TargetFlags =
2669 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00002670 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002671 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002672 SDLoc DL(Op);
2673
2674 ++NumMovwMovt;
2675
2676 // FIXME: Once remat is capable of dealing with instructions with register
2677 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002678 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2679 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00002680 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002681 if (GV->hasDLLImportStorageClass())
2682 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2683 MachinePointerInfo::getGOT(), false, false, false, 0);
2684 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002685}
2686
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002687SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002688 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002689 assert(Subtarget->isTargetELF() &&
2690 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002691 MachineFunction &MF = DAG.getMachineFunction();
2692 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002693 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002694 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002695 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002696 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002697 ARMConstantPoolValue *CPV =
2698 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2699 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002700 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002701 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002702 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002703 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002704 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002705 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002706 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002707}
2708
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002709SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002710ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002711 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002712 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002713 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2714 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002715 Op.getOperand(1), Val);
2716}
2717
2718SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002719ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002720 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002721 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002722 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002723}
2724
2725SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002726ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002727 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002728 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002729 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002730 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002731 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002732 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002733 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002734 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002735 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002736 }
Bob Wilson17f88782009-08-04 00:25:01 +00002737 case Intrinsic::arm_thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00002738 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00002739 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2740 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002741 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002742 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002743 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002744 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00002745 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00002746 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2747 SDValue CPAddr;
2748 unsigned PCAdj = (RelocM != Reloc::PIC_)
2749 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002750 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002751 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2752 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002753 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002754 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002755 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002756 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002757 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002758 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002759
2760 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002761 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002762 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2763 }
2764 return Result;
2765 }
Evan Cheng18381b42011-03-29 23:06:19 +00002766 case Intrinsic::arm_neon_vmulls:
2767 case Intrinsic::arm_neon_vmullu: {
2768 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2769 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002770 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002771 Op.getOperand(1), Op.getOperand(2));
2772 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002773 }
2774}
2775
Eli Friedman30a49e92011-08-03 21:06:02 +00002776static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2777 const ARMSubtarget *Subtarget) {
2778 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002779 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002780 if (!Subtarget->hasDataBarrier()) {
2781 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2782 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2783 // here.
2784 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002785 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002786 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002787 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002788 }
2789
Tim Northover36b24172013-07-03 09:20:36 +00002790 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2791 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002792 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002793 if (Subtarget->isMClass()) {
2794 // Only a full system barrier exists in the M-class architectures.
2795 Domain = ARM_MB::SY;
2796 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002797 // Swift happens to implement ISHST barriers in a way that's compatible with
2798 // Release semantics but weaker than ISH so we'd be fools not to use
2799 // it. Beware: other processors probably don't!
2800 Domain = ARM_MB::ISHST;
2801 }
2802
Joey Gouly926d3f52013-09-05 15:35:24 +00002803 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002804 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2805 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002806}
2807
Evan Cheng8740ee32010-11-03 06:34:55 +00002808static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2809 const ARMSubtarget *Subtarget) {
2810 // ARM pre v5TE and Thumb1 does not have preload instructions.
2811 if (!(Subtarget->isThumb2() ||
2812 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2813 // Just preserve the chain.
2814 return Op.getOperand(0);
2815
Andrew Trickef9de2a2013-05-25 02:42:55 +00002816 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002817 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2818 if (!isRead &&
2819 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2820 // ARMv7 with MP extension has PLDW.
2821 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002822
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002823 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2824 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002825 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002826 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002827 isData = ~isData & 1;
2828 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002829
2830 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002831 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2832 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002833}
2834
Dan Gohman31ae5862010-04-17 14:41:14 +00002835static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2836 MachineFunction &MF = DAG.getMachineFunction();
2837 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2838
Evan Cheng10043e22007-01-19 07:51:42 +00002839 // vastart just stores the address of the VarArgsFrameIndex slot into the
2840 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002841 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002842 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00002843 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002844 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002845 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2846 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002847}
2848
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002849SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002850ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2851 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002852 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002853 MachineFunction &MF = DAG.getMachineFunction();
2854 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2855
Craig Topper760b1342012-02-22 05:59:10 +00002856 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002857 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002858 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002859 else
Craig Topperc7242e02012-04-20 07:30:17 +00002860 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002861
2862 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002863 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002864 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002865
2866 SDValue ArgValue2;
2867 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002868 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002869 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002870
2871 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00002872 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Evan Chengcdbb70c2009-10-31 03:39:36 +00002873 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002874 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002875 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002876 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002877 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002878 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002879 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002880 if (!Subtarget->isLittle())
2881 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002882 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002883}
2884
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002885// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002886// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002887// byval). Either way, we allocate stack slots adjacent to the data
2888// provided by our caller, and store the unallocated registers there.
2889// If this is a variadic function, the va_list pointer will begin with
2890// these values; otherwise, this reassembles a (byval) structure that
2891// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002892// Return: The frame index registers were stored into.
2893int
2894ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002895 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002896 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002897 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002898 int ArgOffset,
2899 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002900 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002901 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002902 // Setup first unallocated register as first byval register;
2903 // eat all remained registers
2904 // (these two actions are performed by HandleByVal method).
2905 // Then, here, we initialize stack frame with
2906 // "store-reg" instructions.
2907 // Case #2. Var-args function, that doesn't contain byval parameters.
2908 // The same: eat all remained unallocated registers,
2909 // initialize stack frame.
2910
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002911 MachineFunction &MF = DAG.getMachineFunction();
2912 MachineFrameInfo *MFI = MF.getFrameInfo();
2913 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002914 unsigned RBegin, REnd;
2915 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2916 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002917 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002918 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00002919 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00002920 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002921 }
2922
Tim Northover8cda34f2015-03-11 18:54:22 +00002923 if (REnd != RBegin)
2924 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002925
Mehdi Amini44ede332015-07-09 02:09:04 +00002926 auto PtrVT = getPointerTy(DAG.getDataLayout());
Tim Northover8cda34f2015-03-11 18:54:22 +00002927 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00002928 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002929
Tim Northover8cda34f2015-03-11 18:54:22 +00002930 SmallVector<SDValue, 4> MemOps;
2931 const TargetRegisterClass *RC =
2932 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002933
Tim Northover8cda34f2015-03-11 18:54:22 +00002934 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
2935 unsigned VReg = MF.addLiveIn(Reg, RC);
2936 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2937 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002938 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00002939 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
2940 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00002941 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00002942 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002943
2944 if (!MemOps.empty())
2945 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
2946 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002947}
2948
2949// Setup stack frame, the va_list pointer will start from.
2950void
2951ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002952 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002953 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002954 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002955 bool ForceMutable) const {
2956 MachineFunction &MF = DAG.getMachineFunction();
2957 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2958
2959 // Try to store any remaining integer argument regs
2960 // to their spots on the stack so that they may be loaded by deferencing
2961 // the result of va_next.
2962 // If there is no regs to be stored, just point address after last
2963 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00002964 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2965 CCInfo.getInRegsParamsCount(),
2966 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002967 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002968}
2969
Bob Wilson2e076c42009-06-22 23:27:02 +00002970SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002971ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002972 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002973 const SmallVectorImpl<ISD::InputArg>
2974 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002975 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002976 SmallVectorImpl<SDValue> &InVals)
2977 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002978 MachineFunction &MF = DAG.getMachineFunction();
2979 MachineFrameInfo *MFI = MF.getFrameInfo();
2980
Bob Wilsona4c22902009-04-17 19:07:39 +00002981 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2982
2983 // Assign locations to all of the incoming arguments.
2984 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002985 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2986 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002987 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002988 CCAssignFnForNode(CallConv, /* Return*/ false,
2989 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002990
Bob Wilsona4c22902009-04-17 19:07:39 +00002991 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002992 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002993 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2994 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002995
2996 // Initially ArgRegsSaveSize is zero.
2997 // Then we increase this value each time we meet byval parameter.
2998 // We also increase this value in case of varargs function.
2999 AFI->setArgRegsSaveSize(0);
3000
Oliver Stannardd55e1152014-03-05 15:25:27 +00003001 // Calculate the amount of stack space that we need to allocate to store
3002 // byval and variadic arguments that are passed in registers.
3003 // We need to know this before we allocate the first byval or variadic
3004 // argument, as they will be allocated a stack slot below the CFA (Canonical
3005 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003006 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003007 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003008 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3009 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003010
Tim Northover8cda34f2015-03-11 18:54:22 +00003011 CCValAssign &VA = ArgLocs[i];
3012 unsigned Index = VA.getValNo();
3013 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3014 if (!Flags.isByVal())
3015 continue;
3016
3017 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3018 unsigned RBegin, REnd;
3019 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3020 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3021
3022 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003023 }
3024 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003025
3026 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003027 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003028 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3029 if (RegIdx != array_lengthof(GPRArgRegs))
3030 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003031 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003032
3033 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3034 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003035 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003036
Bob Wilsona4c22902009-04-17 19:07:39 +00003037 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3038 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003039 if (Ins[VA.getValNo()].isOrigArg()) {
3040 std::advance(CurOrigArg,
3041 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3042 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3043 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003044 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003045 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003046 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003047
Bob Wilsona4c22902009-04-17 19:07:39 +00003048 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003049 // f64 and vector types are split up into multiple registers or
3050 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003051 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003052 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003053 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003054 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003055 SDValue ArgValue2;
3056 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003057 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003058 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Bob Wilson699bdf72010-04-13 22:03:22 +00003059 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003060 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003061 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003062 } else {
3063 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3064 Chain, DAG, dl);
3065 }
Owen Anderson9f944592009-08-11 20:47:22 +00003066 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3067 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003068 ArgValue, ArgValue1,
3069 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003070 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003071 ArgValue, ArgValue2,
3072 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003073 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003074 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003075
Bob Wilson2e076c42009-06-22 23:27:02 +00003076 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003077 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003078
Owen Anderson9f944592009-08-11 20:47:22 +00003079 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003080 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003081 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003082 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003083 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003084 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003085 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003086 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3087 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003088 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003089 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003090
3091 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003092 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003093 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003094 }
3095
3096 // If this is an 8 or 16-bit value, it is really passed promoted
3097 // to 32 bits. Insert an assert[sz]ext to capture this, then
3098 // truncate to the right size.
3099 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003100 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003101 case CCValAssign::Full: break;
3102 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003103 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003104 break;
3105 case CCValAssign::SExt:
3106 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3107 DAG.getValueType(VA.getValVT()));
3108 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3109 break;
3110 case CCValAssign::ZExt:
3111 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3112 DAG.getValueType(VA.getValVT()));
3113 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3114 break;
3115 }
3116
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003117 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003118
3119 } else { // VA.isRegLoc()
3120
3121 // sanity check
3122 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003123 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003124
Andrew Trick05938a52015-02-16 18:10:47 +00003125 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003126
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003127 // Some Ins[] entries become multiple ArgLoc[] entries.
3128 // Process them only once.
3129 if (index != lastInsIndex)
3130 {
3131 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003132 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003133 // This can be changed with more analysis.
3134 // In case of tail call optimization mark all arguments mutable.
3135 // Since they could be overwritten by lowering of arguments in case of
3136 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003137 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003138 assert(Ins[index].isOrigArg() &&
3139 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003140 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003141
Tim Northover8cda34f2015-03-11 18:54:22 +00003142 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3143 CurByValIndex, VA.getLocMemOffset(),
3144 Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003145 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003146 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003147 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003148 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003149 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003150 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003151
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003152 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003153 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003154 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3155 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003156 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003157 }
3158 lastInsIndex = index;
3159 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003160 }
3161 }
3162
3163 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003164 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003165 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003166 CCInfo.getNextStackOffset(),
3167 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003168
Oliver Stannardb14c6252014-04-02 16:10:33 +00003169 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3170
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003171 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003172}
3173
3174/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003175static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003176 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003177 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003178 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003179 // Maybe this has already been legalized into the constant pool?
3180 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003181 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003182 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003183 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003184 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003185 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003186 } else if (Op->getOpcode() == ISD::BITCAST &&
3187 Op->getValueType(0) == MVT::f64) {
3188 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3189 // created by LowerConstantFP().
3190 SDValue BitcastOp = Op->getOperand(0);
3191 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3192 SDValue MoveOp = BitcastOp->getOperand(0);
3193 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3194 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3195 return true;
3196 }
3197 }
Evan Cheng10043e22007-01-19 07:51:42 +00003198 }
3199 return false;
3200}
3201
Evan Cheng10043e22007-01-19 07:51:42 +00003202/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3203/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003204SDValue
3205ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003206 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003207 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003208 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003209 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003210 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003211 // Constant does not fit, try adjusting it by one?
3212 switch (CC) {
3213 default: break;
3214 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003215 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003216 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003217 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003218 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003219 }
3220 break;
3221 case ISD::SETULT:
3222 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003223 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003224 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003225 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003226 }
3227 break;
3228 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003229 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003230 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003231 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003232 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003233 }
3234 break;
3235 case ISD::SETULE:
3236 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003237 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003238 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003239 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003240 }
3241 break;
3242 }
3243 }
3244 }
3245
3246 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003247 ARMISD::NodeType CompareType;
3248 switch (CondCode) {
3249 default:
3250 CompareType = ARMISD::CMP;
3251 break;
3252 case ARMCC::EQ:
3253 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003254 // Uses only Z Flag
3255 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003256 break;
3257 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003258 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003259 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003260}
3261
3262/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003263SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003264ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003265 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003266 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003267 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003268 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003269 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003270 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003271 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3272 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003273}
3274
Bob Wilson45acbd02011-03-08 01:17:20 +00003275/// duplicateCmp - Glue values can have only one use, so this function
3276/// duplicates a comparison node.
3277SDValue
3278ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3279 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003280 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003281 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3282 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3283
3284 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3285 Cmp = Cmp.getOperand(0);
3286 Opc = Cmp.getOpcode();
3287 if (Opc == ARMISD::CMPFP)
3288 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3289 else {
3290 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3291 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3292 }
3293 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3294}
3295
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003296std::pair<SDValue, SDValue>
3297ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3298 SDValue &ARMcc) const {
3299 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3300
3301 SDValue Value, OverflowCmp;
3302 SDValue LHS = Op.getOperand(0);
3303 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003304 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003305
3306 // FIXME: We are currently always generating CMPs because we don't support
3307 // generating CMN through the backend. This is not as good as the natural
3308 // CMP case because it causes a register dependency and cannot be folded
3309 // later.
3310
3311 switch (Op.getOpcode()) {
3312 default:
3313 llvm_unreachable("Unknown overflow instruction!");
3314 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003315 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3316 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3317 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003318 break;
3319 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003320 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3321 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3322 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003323 break;
3324 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003325 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3326 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3327 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003328 break;
3329 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003330 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3331 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3332 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003333 break;
3334 } // switch (...)
3335
3336 return std::make_pair(Value, OverflowCmp);
3337}
3338
3339
3340SDValue
3341ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3342 // Let legalize expand this if it isn't a legal type yet.
3343 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3344 return SDValue();
3345
3346 SDValue Value, OverflowCmp;
3347 SDValue ARMcc;
3348 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3349 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003350 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003351 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003352 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3353 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003354 EVT VT = Op.getValueType();
3355
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003356 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003357 ARMcc, CCR, OverflowCmp);
3358
3359 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003360 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003361}
3362
3363
Bill Wendling6a981312010-08-11 08:43:16 +00003364SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3365 SDValue Cond = Op.getOperand(0);
3366 SDValue SelectTrue = Op.getOperand(1);
3367 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003368 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003369 unsigned Opc = Cond.getOpcode();
3370
3371 if (Cond.getResNo() == 1 &&
3372 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3373 Opc == ISD::USUBO)) {
3374 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3375 return SDValue();
3376
3377 SDValue Value, OverflowCmp;
3378 SDValue ARMcc;
3379 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3380 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3381 EVT VT = Op.getValueType();
3382
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003383 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003384 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003385 }
Bill Wendling6a981312010-08-11 08:43:16 +00003386
3387 // Convert:
3388 //
3389 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3390 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3391 //
3392 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3393 const ConstantSDNode *CMOVTrue =
3394 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3395 const ConstantSDNode *CMOVFalse =
3396 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3397
3398 if (CMOVTrue && CMOVFalse) {
3399 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3400 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3401
3402 SDValue True;
3403 SDValue False;
3404 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3405 True = SelectTrue;
3406 False = SelectFalse;
3407 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3408 True = SelectFalse;
3409 False = SelectTrue;
3410 }
3411
3412 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003413 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003414 SDValue ARMcc = Cond.getOperand(2);
3415 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003416 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003417 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003418 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003419 }
3420 }
3421 }
3422
Dan Gohmand4a77c42012-02-24 00:09:36 +00003423 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3424 // undefined bits before doing a full-word comparison with zero.
3425 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003426 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003427
Bill Wendling6a981312010-08-11 08:43:16 +00003428 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003429 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003430 SelectTrue, SelectFalse, ISD::SETNE);
3431}
3432
Joey Gouly881eab52013-08-22 15:29:11 +00003433static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3434 bool &swpCmpOps, bool &swpVselOps) {
3435 // Start by selecting the GE condition code for opcodes that return true for
3436 // 'equality'
3437 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3438 CC == ISD::SETULE)
3439 CondCode = ARMCC::GE;
3440
3441 // and GT for opcodes that return false for 'equality'.
3442 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3443 CC == ISD::SETULT)
3444 CondCode = ARMCC::GT;
3445
3446 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3447 // to swap the compare operands.
3448 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3449 CC == ISD::SETULT)
3450 swpCmpOps = true;
3451
3452 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3453 // If we have an unordered opcode, we need to swap the operands to the VSEL
3454 // instruction (effectively negating the condition).
3455 //
3456 // This also has the effect of swapping which one of 'less' or 'greater'
3457 // returns true, so we also swap the compare operands. It also switches
3458 // whether we return true for 'equality', so we compensate by picking the
3459 // opposite condition code to our original choice.
3460 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3461 CC == ISD::SETUGT) {
3462 swpCmpOps = !swpCmpOps;
3463 swpVselOps = !swpVselOps;
3464 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3465 }
3466
3467 // 'ordered' is 'anything but unordered', so use the VS condition code and
3468 // swap the VSEL operands.
3469 if (CC == ISD::SETO) {
3470 CondCode = ARMCC::VS;
3471 swpVselOps = true;
3472 }
3473
3474 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3475 // code and swap the VSEL operands.
3476 if (CC == ISD::SETUNE) {
3477 CondCode = ARMCC::EQ;
3478 swpVselOps = true;
3479 }
3480}
3481
Oliver Stannard51b1d462014-08-21 12:50:31 +00003482SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3483 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3484 SDValue Cmp, SelectionDAG &DAG) const {
3485 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3486 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3487 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3488 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3489 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3490
3491 SDValue TrueLow = TrueVal.getValue(0);
3492 SDValue TrueHigh = TrueVal.getValue(1);
3493 SDValue FalseLow = FalseVal.getValue(0);
3494 SDValue FalseHigh = FalseVal.getValue(1);
3495
3496 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3497 ARMcc, CCR, Cmp);
3498 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3499 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3500
3501 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3502 } else {
3503 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3504 Cmp);
3505 }
3506}
3507
Dan Gohman21cea8a2010-04-17 15:26:15 +00003508SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003509 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003510 SDValue LHS = Op.getOperand(0);
3511 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003512 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003513 SDValue TrueVal = Op.getOperand(2);
3514 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003515 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003516
Oliver Stannard51b1d462014-08-21 12:50:31 +00003517 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3518 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3519 dl);
3520
3521 // If softenSetCCOperands only returned one value, we should compare it to
3522 // zero.
3523 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003524 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003525 CC = ISD::SETNE;
3526 }
3527 }
3528
Owen Anderson9f944592009-08-11 20:47:22 +00003529 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003530 // Try to generate VSEL on ARMv8.
3531 // The VSEL instruction can't use all the usual ARM condition
3532 // codes: it only has two bits to select the condition code, so it's
3533 // constrained to use only GE, GT, VS and EQ.
3534 //
3535 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3536 // swap the operands of the previous compare instruction (effectively
3537 // inverting the compare condition, swapping 'less' and 'greater') and
3538 // sometimes need to swap the operands to the VSEL (which inverts the
3539 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003540 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3541 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003542 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3543 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3544 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003545 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003546 std::swap(TrueVal, FalseVal);
3547 }
3548 }
3549
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003550 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003551 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003552 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003553 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003554 }
3555
3556 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003557 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003558
Scott Douglass7ad77922015-04-08 17:18:28 +00003559 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003560 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3561 TrueVal.getValueType() == MVT::f64)) {
Scott Douglass7ad77922015-04-08 17:18:28 +00003562 // We can use VMAXNM/VMINNM for a compare followed by a select with the
Joey Goulye3dd6842013-08-23 12:01:13 +00003563 // same operands, as follows:
Scott Douglass7ad77922015-04-08 17:18:28 +00003564 // c = fcmp [?gt, ?ge, ?lt, ?le] a, b
Joey Goulye3dd6842013-08-23 12:01:13 +00003565 // select c, a, b
Scott Douglass7ad77922015-04-08 17:18:28 +00003566 // In NoNaNsFPMath the CC will have been changed from, e.g., 'ogt' to 'gt'.
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003567 bool swapSides = false;
3568 if (!getTargetMachine().Options.NoNaNsFPMath) {
3569 // transformability may depend on which way around we compare
3570 switch (CC) {
3571 default:
3572 break;
3573 case ISD::SETOGT:
3574 case ISD::SETOGE:
3575 case ISD::SETOLT:
3576 case ISD::SETOLE:
3577 // the non-NaN should be RHS
3578 swapSides = DAG.isKnownNeverNaN(LHS) && !DAG.isKnownNeverNaN(RHS);
3579 break;
3580 case ISD::SETUGT:
3581 case ISD::SETUGE:
3582 case ISD::SETULT:
3583 case ISD::SETULE:
3584 // the non-NaN should be LHS
3585 swapSides = DAG.isKnownNeverNaN(RHS) && !DAG.isKnownNeverNaN(LHS);
3586 break;
3587 }
3588 }
3589 swapSides = swapSides || (LHS == FalseVal && RHS == TrueVal);
3590 if (swapSides) {
3591 CC = ISD::getSetCCSwappedOperands(CC);
3592 std::swap(LHS, RHS);
3593 }
3594 if (LHS == TrueVal && RHS == FalseVal) {
3595 bool canTransform = true;
3596 // FIXME: FastMathFlags::noSignedZeros() doesn't appear reachable from here
3597 if (!getTargetMachine().Options.UnsafeFPMath &&
3598 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
3599 const ConstantFPSDNode *Zero;
3600 switch (CC) {
3601 default:
3602 break;
3603 case ISD::SETOGT:
3604 case ISD::SETUGT:
3605 case ISD::SETGT:
3606 // RHS must not be -0
3607 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3608 !Zero->isNegative();
3609 break;
3610 case ISD::SETOGE:
3611 case ISD::SETUGE:
3612 case ISD::SETGE:
3613 // LHS must not be -0
3614 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3615 !Zero->isNegative();
3616 break;
3617 case ISD::SETOLT:
3618 case ISD::SETULT:
3619 case ISD::SETLT:
3620 // RHS must not be +0
3621 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3622 Zero->isNegative();
3623 break;
3624 case ISD::SETOLE:
3625 case ISD::SETULE:
3626 case ISD::SETLE:
3627 // LHS must not be +0
3628 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3629 Zero->isNegative();
3630 break;
3631 }
3632 }
3633 if (canTransform) {
3634 // Note: If one of the elements in a pair is a number and the other
3635 // element is NaN, the corresponding result element is the number.
3636 // This is consistent with the IEEE 754-2008 standard.
3637 // Therefore, a > b ? a : b <=> vmax(a,b), if b is constant and a is NaN
3638 switch (CC) {
3639 default:
3640 break;
3641 case ISD::SETOGT:
3642 case ISD::SETOGE:
3643 if (!DAG.isKnownNeverNaN(RHS))
3644 break;
3645 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3646 case ISD::SETUGT:
3647 case ISD::SETUGE:
3648 if (!DAG.isKnownNeverNaN(LHS))
3649 break;
3650 case ISD::SETGT:
3651 case ISD::SETGE:
3652 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3653 case ISD::SETOLT:
3654 case ISD::SETOLE:
3655 if (!DAG.isKnownNeverNaN(RHS))
3656 break;
3657 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3658 case ISD::SETULT:
3659 case ISD::SETULE:
3660 if (!DAG.isKnownNeverNaN(LHS))
3661 break;
3662 case ISD::SETLT:
3663 case ISD::SETLE:
3664 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3665 }
Oliver Stannard79efe412014-10-27 09:23:02 +00003666 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003667 }
3668
Joey Gouly881eab52013-08-22 15:29:11 +00003669 bool swpCmpOps = false;
3670 bool swpVselOps = false;
3671 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3672
3673 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3674 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3675 if (swpCmpOps)
3676 std::swap(LHS, RHS);
3677 if (swpVselOps)
3678 std::swap(TrueVal, FalseVal);
3679 }
3680 }
3681
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003682 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003683 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003684 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003685 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003686 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003687 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003688 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003689 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003690 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003691 }
3692 return Result;
3693}
3694
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003695/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3696/// to morph to an integer compare sequence.
3697static bool canChangeToInt(SDValue Op, bool &SeenZero,
3698 const ARMSubtarget *Subtarget) {
3699 SDNode *N = Op.getNode();
3700 if (!N->hasOneUse())
3701 // Otherwise it requires moving the value from fp to integer registers.
3702 return false;
3703 if (!N->getNumValues())
3704 return false;
3705 EVT VT = Op.getValueType();
3706 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3707 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3708 // vmrs are very slow, e.g. cortex-a8.
3709 return false;
3710
3711 if (isFloatingPointZero(Op)) {
3712 SeenZero = true;
3713 return true;
3714 }
3715 return ISD::isNormalLoad(N);
3716}
3717
3718static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3719 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003720 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003721
3722 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003723 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003724 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003725 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003726 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003727
3728 llvm_unreachable("Unknown VFP cmp argument!");
3729}
3730
3731static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3732 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003733 SDLoc dl(Op);
3734
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003735 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003736 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3737 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003738 return;
3739 }
3740
3741 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3742 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003743 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003744 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003745 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003746 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003747 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003748
3749 EVT PtrType = Ptr.getValueType();
3750 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003751 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3752 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3753 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003754 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003755 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003756 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003757 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003758 return;
3759 }
3760
3761 llvm_unreachable("Unknown VFP cmp argument!");
3762}
3763
3764/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3765/// f32 and even f64 comparisons to integer ones.
3766SDValue
3767ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3768 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003769 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003770 SDValue LHS = Op.getOperand(2);
3771 SDValue RHS = Op.getOperand(3);
3772 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003773 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003774
Evan Chengd12af5d2012-03-01 23:27:13 +00003775 bool LHSSeenZero = false;
3776 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3777 bool RHSSeenZero = false;
3778 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3779 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003780 // If unsafe fp math optimization is enabled and there are no other uses of
3781 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003782 // to an integer comparison.
3783 if (CC == ISD::SETOEQ)
3784 CC = ISD::SETEQ;
3785 else if (CC == ISD::SETUNE)
3786 CC = ISD::SETNE;
3787
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003788 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003789 SDValue ARMcc;
3790 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003791 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3792 bitcastf32Toi32(LHS, DAG), Mask);
3793 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3794 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003795 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3796 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3797 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3798 Chain, Dest, ARMcc, CCR, Cmp);
3799 }
3800
3801 SDValue LHS1, LHS2;
3802 SDValue RHS1, RHS2;
3803 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3804 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003805 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3806 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003807 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003808 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003809 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003810 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003811 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003812 }
3813
3814 return SDValue();
3815}
3816
3817SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3818 SDValue Chain = Op.getOperand(0);
3819 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3820 SDValue LHS = Op.getOperand(2);
3821 SDValue RHS = Op.getOperand(3);
3822 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003823 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003824
Oliver Stannard51b1d462014-08-21 12:50:31 +00003825 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3826 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3827 dl);
3828
3829 // If softenSetCCOperands only returned one value, we should compare it to
3830 // zero.
3831 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003832 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003833 CC = ISD::SETNE;
3834 }
3835 }
3836
Owen Anderson9f944592009-08-11 20:47:22 +00003837 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003838 SDValue ARMcc;
3839 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003840 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003841 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003842 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003843 }
3844
Owen Anderson9f944592009-08-11 20:47:22 +00003845 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003846
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003847 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003848 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3849 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3850 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3851 if (Result.getNode())
3852 return Result;
3853 }
3854
Evan Cheng10043e22007-01-19 07:51:42 +00003855 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003856 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003857
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003858 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003859 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003860 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003861 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003862 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003863 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003864 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003865 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003866 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003867 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003868 }
3869 return Res;
3870}
3871
Dan Gohman21cea8a2010-04-17 15:26:15 +00003872SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003873 SDValue Chain = Op.getOperand(0);
3874 SDValue Table = Op.getOperand(1);
3875 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003876 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003877
Mehdi Amini44ede332015-07-09 02:09:04 +00003878 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00003879 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003880 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003881 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003882 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003883 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003884 if (Subtarget->isThumb2()) {
3885 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3886 // which does another jump to the destination. This also makes it easier
3887 // to translate it to TBB / TBH later.
3888 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003889 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003890 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003891 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003892 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003893 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003894 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003895 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003896 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003897 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003898 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003899 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003900 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003901 MachinePointerInfo::getJumpTable(),
3902 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003903 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003904 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003905 }
Evan Cheng10043e22007-01-19 07:51:42 +00003906}
3907
Eli Friedman2d4055b2011-11-09 23:36:02 +00003908static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003909 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003910 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003911
James Molloy547d4c02012-02-20 09:24:05 +00003912 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3913 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3914 return Op;
3915 return DAG.UnrollVectorOp(Op.getNode());
3916 }
3917
3918 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3919 "Invalid type for custom lowering!");
3920 if (VT != MVT::v4i16)
3921 return DAG.UnrollVectorOp(Op.getNode());
3922
3923 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3924 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003925}
3926
Oliver Stannard51b1d462014-08-21 12:50:31 +00003927SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003928 EVT VT = Op.getValueType();
3929 if (VT.isVector())
3930 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003931 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3932 RTLIB::Libcall LC;
3933 if (Op.getOpcode() == ISD::FP_TO_SINT)
3934 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3935 Op.getValueType());
3936 else
3937 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3938 Op.getValueType());
3939 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3940 /*isSigned*/ false, SDLoc(Op)).first;
3941 }
3942
James Molloyfa041152015-03-23 16:15:16 +00003943 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003944}
3945
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003946static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3947 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003948 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003949
Eli Friedman2d4055b2011-11-09 23:36:02 +00003950 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3951 if (VT.getVectorElementType() == MVT::f32)
3952 return Op;
3953 return DAG.UnrollVectorOp(Op.getNode());
3954 }
3955
Duncan Sandsa41634e2011-08-12 14:54:45 +00003956 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3957 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003958 if (VT != MVT::v4f32)
3959 return DAG.UnrollVectorOp(Op.getNode());
3960
3961 unsigned CastOpc;
3962 unsigned Opc;
3963 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003964 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003965 case ISD::SINT_TO_FP:
3966 CastOpc = ISD::SIGN_EXTEND;
3967 Opc = ISD::SINT_TO_FP;
3968 break;
3969 case ISD::UINT_TO_FP:
3970 CastOpc = ISD::ZERO_EXTEND;
3971 Opc = ISD::UINT_TO_FP;
3972 break;
3973 }
3974
3975 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3976 return DAG.getNode(Opc, dl, VT, Op);
3977}
3978
Oliver Stannard51b1d462014-08-21 12:50:31 +00003979SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003980 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003981 if (VT.isVector())
3982 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003983 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3984 RTLIB::Libcall LC;
3985 if (Op.getOpcode() == ISD::SINT_TO_FP)
3986 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3987 Op.getValueType());
3988 else
3989 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3990 Op.getValueType());
3991 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3992 /*isSigned*/ false, SDLoc(Op)).first;
3993 }
3994
James Molloyfa041152015-03-23 16:15:16 +00003995 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003996}
3997
Evan Cheng25f93642010-07-08 02:08:50 +00003998SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003999 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004000 SDValue Tmp0 = Op.getOperand(0);
4001 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004002 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004003 EVT VT = Op.getValueType();
4004 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004005 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4006 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4007 bool UseNEON = !InGPR && Subtarget->hasNEON();
4008
4009 if (UseNEON) {
4010 // Use VBSL to copy the sign bit.
4011 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4012 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004013 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004014 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4015 if (VT == MVT::f64)
4016 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4017 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004018 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004019 else /*if (VT == MVT::f32)*/
4020 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4021 if (SrcVT == MVT::f32) {
4022 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4023 if (VT == MVT::f64)
4024 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4025 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004026 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004027 } else if (VT == MVT::f32)
4028 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4029 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004030 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004031 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4032 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4033
4034 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004035 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004036 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4037 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4038 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004039
Evan Chengd6b641e2011-02-23 02:24:55 +00004040 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4041 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4042 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004043 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004044 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4045 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004046 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004047 } else {
4048 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4049 }
4050
4051 return Res;
4052 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004053
4054 // Bitcast operand 1 to i32.
4055 if (SrcVT == MVT::f64)
4056 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004057 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004058 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4059
Evan Chengd6b641e2011-02-23 02:24:55 +00004060 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004061 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4062 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004063 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4064 if (VT == MVT::f32) {
4065 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4066 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4067 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4068 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004069 }
4070
Evan Chengd6b641e2011-02-23 02:24:55 +00004071 // f64: Or the high part with signbit and then combine two parts.
4072 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004073 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004074 SDValue Lo = Tmp0.getValue(0);
4075 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4076 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4077 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004078}
4079
Evan Cheng168ced92010-05-22 01:47:14 +00004080SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4081 MachineFunction &MF = DAG.getMachineFunction();
4082 MachineFrameInfo *MFI = MF.getFrameInfo();
4083 MFI->setReturnAddressIsTaken(true);
4084
Bill Wendling908bf812014-01-06 00:43:20 +00004085 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004086 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004087
Evan Cheng168ced92010-05-22 01:47:14 +00004088 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004089 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004090 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4091 if (Depth) {
4092 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004093 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004094 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4095 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004096 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004097 }
4098
4099 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004100 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004101 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4102}
4103
Dan Gohman21cea8a2010-04-17 15:26:15 +00004104SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004105 const ARMBaseRegisterInfo &ARI =
4106 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4107 MachineFunction &MF = DAG.getMachineFunction();
4108 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004109 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004110
Owen Anderson53aa7a92009-08-10 22:56:29 +00004111 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004112 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004113 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004114 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004115 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4116 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004117 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4118 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004119 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004120 return FrameAddr;
4121}
4122
Renato Golinc7aea402014-05-06 16:51:25 +00004123// FIXME? Maybe this could be a TableGen attribute on some registers and
4124// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004125unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4126 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004127 unsigned Reg = StringSwitch<unsigned>(RegName)
4128 .Case("sp", ARM::SP)
4129 .Default(0);
4130 if (Reg)
4131 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004132 report_fatal_error(Twine("Invalid register name \""
4133 + StringRef(RegName) + "\"."));
4134}
4135
4136// Result is 64 bit value so split into two 32 bit values and return as a
4137// pair of values.
4138static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4139 SelectionDAG &DAG) {
4140 SDLoc DL(N);
4141
4142 // This function is only supposed to be called for i64 type destination.
4143 assert(N->getValueType(0) == MVT::i64
4144 && "ExpandREAD_REGISTER called for non-i64 type result.");
4145
4146 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4147 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4148 N->getOperand(0),
4149 N->getOperand(1));
4150
4151 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4152 Read.getValue(1)));
4153 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004154}
4155
Wesley Peck527da1b2010-11-23 03:31:01 +00004156/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004157/// expand a bit convert where either the source or destination type is i64 to
4158/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4159/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4160/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004161static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004162 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004163 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004164 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004165
Bob Wilson59b70ea2010-04-17 05:30:19 +00004166 // This function is only supposed to be called for i64 types, either as the
4167 // source or destination of the bit convert.
4168 EVT SrcVT = Op.getValueType();
4169 EVT DstVT = N->getValueType(0);
4170 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004171 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004172
Bob Wilson59b70ea2010-04-17 05:30:19 +00004173 // Turn i64->f64 into VMOVDRR.
4174 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004175 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004176 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004177 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004178 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004179 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004180 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004181 }
Bob Wilson7117a912009-03-20 22:42:55 +00004182
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004183 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004184 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004185 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004186 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004187 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004188 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4189 DAG.getVTList(MVT::i32, MVT::i32),
4190 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4191 else
4192 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4193 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004194 // Merge the pieces into a single i64 value.
4195 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4196 }
Bob Wilson7117a912009-03-20 22:42:55 +00004197
Bob Wilson59b70ea2010-04-17 05:30:19 +00004198 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004199}
4200
Bob Wilson2e076c42009-06-22 23:27:02 +00004201/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004202/// Zero vectors are used to represent vector negation and in those cases
4203/// will be implemented with the NEON VNEG instruction. However, VNEG does
4204/// not support i64 elements, so sometimes the zero vectors will need to be
4205/// explicitly constructed. Regardless, use a canonical VMOV to create the
4206/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004207static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004208 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004209 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004210 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004211 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4212 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004213 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004214}
4215
Jim Grosbach624fcb22009-10-31 21:00:56 +00004216/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4217/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004218SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4219 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004220 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4221 EVT VT = Op.getValueType();
4222 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004223 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004224 SDValue ShOpLo = Op.getOperand(0);
4225 SDValue ShOpHi = Op.getOperand(1);
4226 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004227 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004228 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004229
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004230 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4231
Jim Grosbach624fcb22009-10-31 21:00:56 +00004232 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004233 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004234 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4235 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004236 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004237 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4238 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004239 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004240
4241 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004242 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4243 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004244 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004245 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004246 CCR, Cmp);
4247
4248 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004249 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004250}
4251
Jim Grosbach5d994042009-10-31 19:38:01 +00004252/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4253/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004254SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4255 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004256 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4257 EVT VT = Op.getValueType();
4258 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004259 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004260 SDValue ShOpLo = Op.getOperand(0);
4261 SDValue ShOpHi = Op.getOperand(1);
4262 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004263 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004264
4265 assert(Op.getOpcode() == ISD::SHL_PARTS);
4266 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004267 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004268 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4269 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004270 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004271 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4272 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4273
4274 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4275 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004276 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4277 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004278 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004279 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004280 CCR, Cmp);
4281
4282 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004283 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004284}
4285
Jim Grosbach535d3b42010-09-08 03:54:02 +00004286SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004287 SelectionDAG &DAG) const {
4288 // The rounding mode is in bits 23:22 of the FPSCR.
4289 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4290 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4291 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004292 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004293 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004294 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004295 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004296 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004297 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004298 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004299 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004300 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004301 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004302}
4303
Jim Grosbach8546ec92010-01-18 19:58:49 +00004304static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4305 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004306 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004307 EVT VT = N->getValueType(0);
4308 if (VT.isVector()) {
4309 assert(ST->hasNEON());
4310
4311 // Compute the least significant set bit: LSB = X & -X
4312 SDValue X = N->getOperand(0);
4313 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4314 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4315
4316 EVT ElemTy = VT.getVectorElementType();
4317
4318 if (ElemTy == MVT::i8) {
4319 // Compute with: cttz(x) = ctpop(lsb - 1)
4320 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4321 DAG.getTargetConstant(1, dl, ElemTy));
4322 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4323 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4324 }
4325
4326 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4327 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4328 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4329 unsigned NumBits = ElemTy.getSizeInBits();
4330 SDValue WidthMinus1 =
4331 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4332 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4333 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4334 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4335 }
4336
4337 // Compute with: cttz(x) = ctpop(lsb - 1)
4338
4339 // Since we can only compute the number of bits in a byte with vcnt.8, we
4340 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
4341 // and i64.
4342
4343 // Compute LSB - 1.
4344 SDValue Bits;
4345 if (ElemTy == MVT::i64) {
4346 // Load constant 0xffff'ffff'ffff'ffff to register.
4347 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4348 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
4349 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
4350 } else {
4351 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4352 DAG.getTargetConstant(1, dl, ElemTy));
4353 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4354 }
4355
4356 // Count #bits with vcnt.8.
4357 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4358 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
4359 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
4360
4361 // Gather the #bits with vpaddl (pairwise add.)
4362 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4363 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
4364 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4365 Cnt8);
4366 if (ElemTy == MVT::i16)
4367 return Cnt16;
4368
4369 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
4370 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
4371 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4372 Cnt16);
4373 if (ElemTy == MVT::i32)
4374 return Cnt32;
4375
4376 assert(ElemTy == MVT::i64);
4377 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4378 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
4379 Cnt32);
4380 return Cnt64;
4381 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00004382
4383 if (!ST->hasV6T2Ops())
4384 return SDValue();
4385
4386 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4387 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4388}
4389
Evan Chengb4eae132012-12-04 22:41:50 +00004390/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4391/// for each 16-bit element from operand, repeated. The basic idea is to
4392/// leverage vcnt to get the 8-bit counts, gather and add the results.
4393///
4394/// Trace for v4i16:
4395/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4396/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4397/// 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 +00004398/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004399/// [b0 b1 b2 b3 b4 b5 b6 b7]
4400/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4401/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4402/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4403static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4404 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004405 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004406
4407 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4408 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4409 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4410 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4411 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4412 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4413}
4414
4415/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4416/// bit-count for each 16-bit element from the operand. We need slightly
4417/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4418/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004419///
Evan Chengb4eae132012-12-04 22:41:50 +00004420/// Trace for v4i16:
4421/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4422/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4423/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4424/// v4i16:Extracted = [k0 k1 k2 k3 ]
4425static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4426 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004427 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004428
4429 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4430 if (VT.is64BitVector()) {
4431 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4432 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004433 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004434 } else {
4435 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004436 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004437 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4438 }
4439}
4440
4441/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4442/// bit-count for each 32-bit element from the operand. The idea here is
4443/// to split the vector into 16-bit elements, leverage the 16-bit count
4444/// routine, and then combine the results.
4445///
4446/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4447/// input = [v0 v1 ] (vi: 32-bit elements)
4448/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4449/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004450/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004451/// [k0 k1 k2 k3 ]
4452/// N1 =+[k1 k0 k3 k2 ]
4453/// [k0 k2 k1 k3 ]
4454/// N2 =+[k1 k3 k0 k2 ]
4455/// [k0 k2 k1 k3 ]
4456/// Extended =+[k1 k3 k0 k2 ]
4457/// [k0 k2 ]
4458/// Extracted=+[k1 k3 ]
4459///
4460static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4461 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004462 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004463
4464 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4465
4466 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4467 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4468 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4469 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4470 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4471
4472 if (VT.is64BitVector()) {
4473 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4474 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004475 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004476 } else {
4477 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004478 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004479 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4480 }
4481}
4482
4483static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4484 const ARMSubtarget *ST) {
4485 EVT VT = N->getValueType(0);
4486
4487 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004488 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4489 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004490 "Unexpected type for custom ctpop lowering");
4491
4492 if (VT.getVectorElementType() == MVT::i32)
4493 return lowerCTPOP32BitElements(N, DAG);
4494 else
4495 return lowerCTPOP16BitElements(N, DAG);
4496}
4497
Bob Wilson2e076c42009-06-22 23:27:02 +00004498static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4499 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004500 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004501 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004502
Bob Wilson7d471332010-11-18 21:16:28 +00004503 if (!VT.isVector())
4504 return SDValue();
4505
Bob Wilson2e076c42009-06-22 23:27:02 +00004506 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004507 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004508
Bob Wilson7d471332010-11-18 21:16:28 +00004509 // Left shifts translate directly to the vshiftu intrinsic.
4510 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004511 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004512 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4513 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004514 N->getOperand(0), N->getOperand(1));
4515
4516 assert((N->getOpcode() == ISD::SRA ||
4517 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4518
4519 // NEON uses the same intrinsics for both left and right shifts. For
4520 // right shifts, the shift amounts are negative, so negate the vector of
4521 // shift amounts.
4522 EVT ShiftVT = N->getOperand(1).getValueType();
4523 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4524 getZeroVector(ShiftVT, DAG, dl),
4525 N->getOperand(1));
4526 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4527 Intrinsic::arm_neon_vshifts :
4528 Intrinsic::arm_neon_vshiftu);
4529 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004530 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004531 N->getOperand(0), NegatedCount);
4532}
4533
4534static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4535 const ARMSubtarget *ST) {
4536 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004537 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004538
Eli Friedman682d8c12009-08-22 03:13:10 +00004539 // We can get here for a node like i32 = ISD::SHL i32, i64
4540 if (VT != MVT::i64)
4541 return SDValue();
4542
4543 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004544 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004545
Chris Lattnerf81d5882007-11-24 07:07:01 +00004546 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4547 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004548 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004549 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004550
Chris Lattnerf81d5882007-11-24 07:07:01 +00004551 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004552 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004553
Chris Lattnerf81d5882007-11-24 07:07:01 +00004554 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004555 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004556 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004557 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004558 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004559
Chris Lattnerf81d5882007-11-24 07:07:01 +00004560 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4561 // captures the result into a carry flag.
4562 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004563 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004564
Chris Lattnerf81d5882007-11-24 07:07:01 +00004565 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004566 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004567
Chris Lattnerf81d5882007-11-24 07:07:01 +00004568 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004569 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004570}
4571
Bob Wilson2e076c42009-06-22 23:27:02 +00004572static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4573 SDValue TmpOp0, TmpOp1;
4574 bool Invert = false;
4575 bool Swap = false;
4576 unsigned Opc = 0;
4577
4578 SDValue Op0 = Op.getOperand(0);
4579 SDValue Op1 = Op.getOperand(1);
4580 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004581 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004582 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004583 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004584 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004585
Oliver Stannard51b1d462014-08-21 12:50:31 +00004586 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004587 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004588 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004589 case ISD::SETUNE:
4590 case ISD::SETNE: Invert = true; // Fallthrough
4591 case ISD::SETOEQ:
4592 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4593 case ISD::SETOLT:
4594 case ISD::SETLT: Swap = true; // Fallthrough
4595 case ISD::SETOGT:
4596 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4597 case ISD::SETOLE:
4598 case ISD::SETLE: Swap = true; // Fallthrough
4599 case ISD::SETOGE:
4600 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4601 case ISD::SETUGE: Swap = true; // Fallthrough
4602 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4603 case ISD::SETUGT: Swap = true; // Fallthrough
4604 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4605 case ISD::SETUEQ: Invert = true; // Fallthrough
4606 case ISD::SETONE:
4607 // Expand this to (OLT | OGT).
4608 TmpOp0 = Op0;
4609 TmpOp1 = Op1;
4610 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004611 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4612 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004613 break;
4614 case ISD::SETUO: Invert = true; // Fallthrough
4615 case ISD::SETO:
4616 // Expand this to (OLT | OGE).
4617 TmpOp0 = Op0;
4618 TmpOp1 = Op1;
4619 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004620 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4621 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004622 break;
4623 }
4624 } else {
4625 // Integer comparisons.
4626 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004627 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004628 case ISD::SETNE: Invert = true;
4629 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4630 case ISD::SETLT: Swap = true;
4631 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4632 case ISD::SETLE: Swap = true;
4633 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4634 case ISD::SETULT: Swap = true;
4635 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4636 case ISD::SETULE: Swap = true;
4637 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4638 }
4639
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004640 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004641 if (Opc == ARMISD::VCEQ) {
4642
4643 SDValue AndOp;
4644 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4645 AndOp = Op0;
4646 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4647 AndOp = Op1;
4648
4649 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004650 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004651 AndOp = AndOp.getOperand(0);
4652
4653 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4654 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004655 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4656 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004657 Invert = !Invert;
4658 }
4659 }
4660 }
4661
4662 if (Swap)
4663 std::swap(Op0, Op1);
4664
Owen Andersonc7baee32010-11-08 23:21:22 +00004665 // If one of the operands is a constant vector zero, attempt to fold the
4666 // comparison to a specialized compare-against-zero form.
4667 SDValue SingleOp;
4668 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4669 SingleOp = Op0;
4670 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4671 if (Opc == ARMISD::VCGE)
4672 Opc = ARMISD::VCLEZ;
4673 else if (Opc == ARMISD::VCGT)
4674 Opc = ARMISD::VCLTZ;
4675 SingleOp = Op1;
4676 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004677
Owen Andersonc7baee32010-11-08 23:21:22 +00004678 SDValue Result;
4679 if (SingleOp.getNode()) {
4680 switch (Opc) {
4681 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004682 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004683 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004684 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004685 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004686 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004687 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004688 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004689 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004690 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004691 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004692 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004693 }
4694 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004695 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004696 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004697
Tim Northover45aa89c2015-02-08 00:50:47 +00004698 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4699
Bob Wilson2e076c42009-06-22 23:27:02 +00004700 if (Invert)
4701 Result = DAG.getNOT(dl, Result, VT);
4702
4703 return Result;
4704}
4705
Bob Wilson5b2b5042010-06-14 22:19:57 +00004706/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4707/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004708/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004709static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4710 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004711 SDLoc dl, EVT &VT, bool is128Bits,
4712 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004713 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004714
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004715 // SplatBitSize is set to the smallest size that splats the vector, so a
4716 // zero vector will always have SplatBitSize == 8. However, NEON modified
4717 // immediate instructions others than VMOV do not support the 8-bit encoding
4718 // of a zero vector, and the default encoding of zero is supposed to be the
4719 // 32-bit version.
4720 if (SplatBits == 0)
4721 SplatBitSize = 32;
4722
Bob Wilson2e076c42009-06-22 23:27:02 +00004723 switch (SplatBitSize) {
4724 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004725 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004726 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004727 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004728 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004729 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004730 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004731 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004732 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004733
4734 case 16:
4735 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004736 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004737 if ((SplatBits & ~0xff) == 0) {
4738 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004739 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004740 Imm = SplatBits;
4741 break;
4742 }
4743 if ((SplatBits & ~0xff00) == 0) {
4744 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004745 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004746 Imm = SplatBits >> 8;
4747 break;
4748 }
4749 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004750
4751 case 32:
4752 // NEON's 32-bit VMOV supports splat values where:
4753 // * only one byte is nonzero, or
4754 // * the least significant byte is 0xff and the second byte is nonzero, or
4755 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004756 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004757 if ((SplatBits & ~0xff) == 0) {
4758 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004759 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004760 Imm = SplatBits;
4761 break;
4762 }
4763 if ((SplatBits & ~0xff00) == 0) {
4764 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004765 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004766 Imm = SplatBits >> 8;
4767 break;
4768 }
4769 if ((SplatBits & ~0xff0000) == 0) {
4770 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004771 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004772 Imm = SplatBits >> 16;
4773 break;
4774 }
4775 if ((SplatBits & ~0xff000000) == 0) {
4776 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004777 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004778 Imm = SplatBits >> 24;
4779 break;
4780 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004781
Owen Andersona4076922010-11-05 21:57:54 +00004782 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4783 if (type == OtherModImm) return SDValue();
4784
Bob Wilson2e076c42009-06-22 23:27:02 +00004785 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004786 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4787 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004788 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004789 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004790 break;
4791 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004792
4793 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004794 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4795 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004796 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004797 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004798 break;
4799 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004800
4801 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4802 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4803 // VMOV.I32. A (very) minor optimization would be to replicate the value
4804 // and fall through here to test for a valid 64-bit splat. But, then the
4805 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004806 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004807
4808 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004809 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004810 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004811 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004812 uint64_t BitMask = 0xff;
4813 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004814 unsigned ImmMask = 1;
4815 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004816 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004817 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004818 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004819 Imm |= ImmMask;
4820 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004821 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004822 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004823 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004824 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004825 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004826
Mehdi Aminiffc14022015-07-08 01:00:38 +00004827 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00004828 // swap higher and lower 32 bit word
4829 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4830
Bob Wilson6eae5202010-06-11 21:34:50 +00004831 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004832 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004833 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004834 break;
4835 }
4836
Bob Wilson6eae5202010-06-11 21:34:50 +00004837 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004838 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004839 }
4840
Bob Wilsona3f19012010-07-13 21:16:48 +00004841 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004842 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004843}
4844
Lang Hames591cdaf2012-03-29 21:56:11 +00004845SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4846 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004847 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004848 return SDValue();
4849
Tim Northoverf79c3a52013-08-20 08:57:11 +00004850 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004851 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004852
Oliver Stannard51b1d462014-08-21 12:50:31 +00004853 // Use the default (constant pool) lowering for double constants when we have
4854 // an SP-only FPU
4855 if (IsDouble && Subtarget->isFPOnlySP())
4856 return SDValue();
4857
Lang Hames591cdaf2012-03-29 21:56:11 +00004858 // Try splatting with a VMOV.f32...
4859 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004860 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4861
Lang Hames591cdaf2012-03-29 21:56:11 +00004862 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004863 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4864 // We have code in place to select a valid ConstantFP already, no need to
4865 // do any mangling.
4866 return Op;
4867 }
4868
4869 // It's a float and we are trying to use NEON operations where
4870 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004871 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004872 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004873 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4874 NewVal);
4875 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004876 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004877 }
4878
Tim Northoverf79c3a52013-08-20 08:57:11 +00004879 // The rest of our options are NEON only, make sure that's allowed before
4880 // proceeding..
4881 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4882 return SDValue();
4883
Lang Hames591cdaf2012-03-29 21:56:11 +00004884 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004885 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4886
4887 // It wouldn't really be worth bothering for doubles except for one very
4888 // important value, which does happen to match: 0.0. So make sure we don't do
4889 // anything stupid.
4890 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4891 return SDValue();
4892
4893 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004894 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4895 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004896 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004897 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004898 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4899 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004900 if (IsDouble)
4901 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4902
4903 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004904 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4905 VecConstant);
4906 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004907 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004908 }
4909
4910 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004911 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004912 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004913 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004914 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004915 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004916
4917 if (IsDouble)
4918 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4919
4920 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004921 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4922 VecConstant);
4923 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004924 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004925 }
4926
4927 return SDValue();
4928}
4929
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004930// check if an VEXT instruction can handle the shuffle mask when the
4931// vector sources of the shuffle are the same.
4932static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4933 unsigned NumElts = VT.getVectorNumElements();
4934
4935 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4936 if (M[0] < 0)
4937 return false;
4938
4939 Imm = M[0];
4940
4941 // If this is a VEXT shuffle, the immediate value is the index of the first
4942 // element. The other shuffle indices must be the successive elements after
4943 // the first one.
4944 unsigned ExpectedElt = Imm;
4945 for (unsigned i = 1; i < NumElts; ++i) {
4946 // Increment the expected index. If it wraps around, just follow it
4947 // back to index zero and keep going.
4948 ++ExpectedElt;
4949 if (ExpectedElt == NumElts)
4950 ExpectedElt = 0;
4951
4952 if (M[i] < 0) continue; // ignore UNDEF indices
4953 if (ExpectedElt != static_cast<unsigned>(M[i]))
4954 return false;
4955 }
4956
4957 return true;
4958}
4959
Lang Hames591cdaf2012-03-29 21:56:11 +00004960
Benjamin Kramer339ced42012-01-15 13:16:05 +00004961static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004962 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004963 unsigned NumElts = VT.getVectorNumElements();
4964 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004965
4966 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4967 if (M[0] < 0)
4968 return false;
4969
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004970 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004971
4972 // If this is a VEXT shuffle, the immediate value is the index of the first
4973 // element. The other shuffle indices must be the successive elements after
4974 // the first one.
4975 unsigned ExpectedElt = Imm;
4976 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004977 // Increment the expected index. If it wraps around, it may still be
4978 // a VEXT but the source vectors must be swapped.
4979 ExpectedElt += 1;
4980 if (ExpectedElt == NumElts * 2) {
4981 ExpectedElt = 0;
4982 ReverseVEXT = true;
4983 }
4984
Bob Wilson411dfad2010-08-17 05:54:34 +00004985 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004986 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004987 return false;
4988 }
4989
4990 // Adjust the index value if the source operands will be swapped.
4991 if (ReverseVEXT)
4992 Imm -= NumElts;
4993
Bob Wilson32cd8552009-08-19 17:03:43 +00004994 return true;
4995}
4996
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004997/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4998/// instruction with the specified blocksize. (The order of the elements
4999/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005000static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005001 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5002 "Only possible block sizes for VREV are: 16, 32, 64");
5003
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005004 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005005 if (EltSz == 64)
5006 return false;
5007
5008 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005009 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005010 // If the first shuffle index is UNDEF, be optimistic.
5011 if (M[0] < 0)
5012 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005013
5014 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5015 return false;
5016
5017 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005018 if (M[i] < 0) continue; // ignore UNDEF indices
5019 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005020 return false;
5021 }
5022
5023 return true;
5024}
5025
Benjamin Kramer339ced42012-01-15 13:16:05 +00005026static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005027 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5028 // range, then 0 is placed into the resulting vector. So pretty much any mask
5029 // of 8 elements can work here.
5030 return VT == MVT::v8i8 && M.size() == 8;
5031}
5032
Benjamin Kramer339ced42012-01-15 13:16:05 +00005033static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005034 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5035 if (EltSz == 64)
5036 return false;
5037
Bob Wilsona7062312009-08-21 20:54:19 +00005038 unsigned NumElts = VT.getVectorNumElements();
5039 WhichResult = (M[0] == 0 ? 0 : 1);
5040 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005041 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
5042 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00005043 return false;
5044 }
5045 return true;
5046}
5047
Bob Wilson0bbd3072009-12-03 06:40:55 +00005048/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5049/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5050/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005051static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005052 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5053 if (EltSz == 64)
5054 return false;
5055
5056 unsigned NumElts = VT.getVectorNumElements();
5057 WhichResult = (M[0] == 0 ? 0 : 1);
5058 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005059 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
5060 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005061 return false;
5062 }
5063 return true;
5064}
5065
Benjamin Kramer339ced42012-01-15 13:16:05 +00005066static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005067 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5068 if (EltSz == 64)
5069 return false;
5070
Bob Wilsona7062312009-08-21 20:54:19 +00005071 unsigned NumElts = VT.getVectorNumElements();
5072 WhichResult = (M[0] == 0 ? 0 : 1);
5073 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005074 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00005075 if ((unsigned) M[i] != 2 * i + WhichResult)
5076 return false;
5077 }
5078
5079 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005080 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005081 return false;
5082
5083 return true;
5084}
5085
Bob Wilson0bbd3072009-12-03 06:40:55 +00005086/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5087/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5088/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005089static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005090 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5091 if (EltSz == 64)
5092 return false;
5093
5094 unsigned Half = VT.getVectorNumElements() / 2;
5095 WhichResult = (M[0] == 0 ? 0 : 1);
5096 for (unsigned j = 0; j != 2; ++j) {
5097 unsigned Idx = WhichResult;
5098 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005099 int MIdx = M[i + j * Half];
5100 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00005101 return false;
5102 Idx += 2;
5103 }
5104 }
5105
5106 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5107 if (VT.is64BitVector() && EltSz == 32)
5108 return false;
5109
5110 return true;
5111}
5112
Benjamin Kramer339ced42012-01-15 13:16:05 +00005113static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005114 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5115 if (EltSz == 64)
5116 return false;
5117
Bob Wilsona7062312009-08-21 20:54:19 +00005118 unsigned NumElts = VT.getVectorNumElements();
5119 WhichResult = (M[0] == 0 ? 0 : 1);
5120 unsigned Idx = WhichResult * NumElts / 2;
5121 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005122 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5123 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00005124 return false;
5125 Idx += 1;
5126 }
5127
5128 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005129 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005130 return false;
5131
5132 return true;
5133}
5134
Bob Wilson0bbd3072009-12-03 06:40:55 +00005135/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5136/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5137/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005138static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005139 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5140 if (EltSz == 64)
5141 return false;
5142
5143 unsigned NumElts = VT.getVectorNumElements();
5144 WhichResult = (M[0] == 0 ? 0 : 1);
5145 unsigned Idx = WhichResult * NumElts / 2;
5146 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005147 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5148 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005149 return false;
5150 Idx += 1;
5151 }
5152
5153 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5154 if (VT.is64BitVector() && EltSz == 32)
5155 return false;
5156
5157 return true;
5158}
5159
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005160/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5161/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5162static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5163 unsigned &WhichResult,
5164 bool &isV_UNDEF) {
5165 isV_UNDEF = false;
5166 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5167 return ARMISD::VTRN;
5168 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5169 return ARMISD::VUZP;
5170 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5171 return ARMISD::VZIP;
5172
5173 isV_UNDEF = true;
5174 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5175 return ARMISD::VTRN;
5176 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5177 return ARMISD::VUZP;
5178 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5179 return ARMISD::VZIP;
5180
5181 return 0;
5182}
5183
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005184/// \return true if this is a reverse operation on an vector.
5185static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5186 unsigned NumElts = VT.getVectorNumElements();
5187 // Make sure the mask has the right size.
5188 if (NumElts != M.size())
5189 return false;
5190
5191 // Look for <15, ..., 3, -1, 1, 0>.
5192 for (unsigned i = 0; i != NumElts; ++i)
5193 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5194 return false;
5195
5196 return true;
5197}
5198
Dale Johannesen2bff5052010-07-29 20:10:08 +00005199// If N is an integer constant that can be moved into a register in one
5200// instruction, return an SDValue of such a constant (will become a MOV
5201// instruction). Otherwise return null.
5202static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005203 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005204 uint64_t Val;
5205 if (!isa<ConstantSDNode>(N))
5206 return SDValue();
5207 Val = cast<ConstantSDNode>(N)->getZExtValue();
5208
5209 if (ST->isThumb1Only()) {
5210 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005211 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005212 } else {
5213 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005214 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005215 }
5216 return SDValue();
5217}
5218
Bob Wilson2e076c42009-06-22 23:27:02 +00005219// If this is a case we can't handle, return null and let the default
5220// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005221SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5222 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005223 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005224 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005225 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005226
5227 APInt SplatBits, SplatUndef;
5228 unsigned SplatBitSize;
5229 bool HasAnyUndefs;
5230 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005231 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005232 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005233 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005234 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005235 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005236 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005237 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005238 if (Val.getNode()) {
5239 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005240 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005241 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005242
5243 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005244 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005245 Val = isNEONModifiedImm(NegatedImm,
5246 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005247 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005248 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005249 if (Val.getNode()) {
5250 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005251 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005252 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005253
5254 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005255 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005256 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005257 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005258 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005259 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5260 }
5261 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005262 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005263 }
5264
Bob Wilson91fdf682010-05-22 00:23:12 +00005265 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005266 //
5267 // As an optimisation, even if more than one value is used it may be more
5268 // profitable to splat with one value then change some lanes.
5269 //
5270 // Heuristically we decide to do this if the vector has a "dominant" value,
5271 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005272 unsigned NumElts = VT.getVectorNumElements();
5273 bool isOnlyLowElement = true;
5274 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005275 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005276 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005277
5278 // Map of the number of times a particular SDValue appears in the
5279 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005280 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005281 SDValue Value;
5282 for (unsigned i = 0; i < NumElts; ++i) {
5283 SDValue V = Op.getOperand(i);
5284 if (V.getOpcode() == ISD::UNDEF)
5285 continue;
5286 if (i > 0)
5287 isOnlyLowElement = false;
5288 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5289 isConstant = false;
5290
James Molloy49bdbce2012-09-06 09:55:02 +00005291 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005292 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005293
James Molloy49bdbce2012-09-06 09:55:02 +00005294 // Is this value dominant? (takes up more than half of the lanes)
5295 if (++Count > (NumElts / 2)) {
5296 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005297 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005298 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005299 }
James Molloy49bdbce2012-09-06 09:55:02 +00005300 if (ValueCounts.size() != 1)
5301 usesOnlyOneValue = false;
5302 if (!Value.getNode() && ValueCounts.size() > 0)
5303 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005304
James Molloy49bdbce2012-09-06 09:55:02 +00005305 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005306 return DAG.getUNDEF(VT);
5307
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005308 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5309 // Keep going if we are hitting this case.
5310 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005311 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5312
Dale Johannesen2bff5052010-07-29 20:10:08 +00005313 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5314
Dale Johannesen710a2d92010-10-19 20:00:17 +00005315 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5316 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005317 if (hasDominantValue && EltSize <= 32) {
5318 if (!isConstant) {
5319 SDValue N;
5320
5321 // If we are VDUPing a value that comes directly from a vector, that will
5322 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005323 // just use VDUPLANE. We can only do this if the lane being extracted
5324 // is at a constant index, as the VDUP from lane instructions only have
5325 // constant-index forms.
5326 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5327 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005328 // We need to create a new undef vector to use for the VDUPLANE if the
5329 // size of the vector from which we get the value is different than the
5330 // size of the vector that we need to create. We will insert the element
5331 // such that the register coalescer will remove unnecessary copies.
5332 if (VT != Value->getOperand(0).getValueType()) {
5333 ConstantSDNode *constIndex;
5334 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5335 assert(constIndex && "The index is not a constant!");
5336 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5337 VT.getVectorNumElements();
5338 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5339 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005340 Value, DAG.getConstant(index, dl, MVT::i32)),
5341 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005342 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005343 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005344 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005345 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005346 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5347
5348 if (!usesOnlyOneValue) {
5349 // The dominant value was splatted as 'N', but we now have to insert
5350 // all differing elements.
5351 for (unsigned I = 0; I < NumElts; ++I) {
5352 if (Op.getOperand(I) == Value)
5353 continue;
5354 SmallVector<SDValue, 3> Ops;
5355 Ops.push_back(N);
5356 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005357 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005358 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005359 }
5360 }
5361 return N;
5362 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005363 if (VT.getVectorElementType().isFloatingPoint()) {
5364 SmallVector<SDValue, 8> Ops;
5365 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005366 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005367 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005368 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005369 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005370 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5371 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005372 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005373 }
James Molloy49bdbce2012-09-06 09:55:02 +00005374 if (usesOnlyOneValue) {
5375 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5376 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005377 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005378 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005379 }
5380
5381 // If all elements are constants and the case above didn't get hit, fall back
5382 // to the default expansion, which will generate a load from the constant
5383 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005384 if (isConstant)
5385 return SDValue();
5386
Bob Wilson6f2b8962011-01-07 21:37:30 +00005387 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5388 if (NumElts >= 4) {
5389 SDValue shuffle = ReconstructShuffle(Op, DAG);
5390 if (shuffle != SDValue())
5391 return shuffle;
5392 }
5393
Bob Wilson91fdf682010-05-22 00:23:12 +00005394 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005395 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5396 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005397 if (EltSize >= 32) {
5398 // Do the expansion with floating-point types, since that is what the VFP
5399 // registers are defined to use, and since i64 is not legal.
5400 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5401 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005402 SmallVector<SDValue, 8> Ops;
5403 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005404 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005405 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005406 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005407 }
5408
Jim Grosbach24e102a2013-07-08 18:18:52 +00005409 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5410 // know the default expansion would otherwise fall back on something even
5411 // worse. For a vector with one or two non-undef values, that's
5412 // scalar_to_vector for the elements followed by a shuffle (provided the
5413 // shuffle is valid for the target) and materialization element by element
5414 // on the stack followed by a load for everything else.
5415 if (!isConstant && !usesOnlyOneValue) {
5416 SDValue Vec = DAG.getUNDEF(VT);
5417 for (unsigned i = 0 ; i < NumElts; ++i) {
5418 SDValue V = Op.getOperand(i);
5419 if (V.getOpcode() == ISD::UNDEF)
5420 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005421 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005422 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5423 }
5424 return Vec;
5425 }
5426
Bob Wilson2e076c42009-06-22 23:27:02 +00005427 return SDValue();
5428}
5429
Bob Wilson6f2b8962011-01-07 21:37:30 +00005430// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005431// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005432SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5433 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005434 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005435 EVT VT = Op.getValueType();
5436 unsigned NumElts = VT.getVectorNumElements();
5437
5438 SmallVector<SDValue, 2> SourceVecs;
5439 SmallVector<unsigned, 2> MinElts;
5440 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005441
Bob Wilson6f2b8962011-01-07 21:37:30 +00005442 for (unsigned i = 0; i < NumElts; ++i) {
5443 SDValue V = Op.getOperand(i);
5444 if (V.getOpcode() == ISD::UNDEF)
5445 continue;
5446 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5447 // A shuffle can only come from building a vector from various
5448 // elements of other vectors.
5449 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005450 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5451 VT.getVectorElementType()) {
5452 // This code doesn't know how to handle shuffles where the vector
5453 // element types do not match (this happens because type legalization
5454 // promotes the return type of EXTRACT_VECTOR_ELT).
5455 // FIXME: It might be appropriate to extend this code to handle
5456 // mismatched types.
5457 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005458 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005459
Bob Wilson6f2b8962011-01-07 21:37:30 +00005460 // Record this extraction against the appropriate vector if possible...
5461 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005462 // If the element number isn't a constant, we can't effectively
5463 // analyze what's going on.
5464 if (!isa<ConstantSDNode>(V.getOperand(1)))
5465 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005466 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5467 bool FoundSource = false;
5468 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5469 if (SourceVecs[j] == SourceVec) {
5470 if (MinElts[j] > EltNo)
5471 MinElts[j] = EltNo;
5472 if (MaxElts[j] < EltNo)
5473 MaxElts[j] = EltNo;
5474 FoundSource = true;
5475 break;
5476 }
5477 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005478
Bob Wilson6f2b8962011-01-07 21:37:30 +00005479 // Or record a new source if not...
5480 if (!FoundSource) {
5481 SourceVecs.push_back(SourceVec);
5482 MinElts.push_back(EltNo);
5483 MaxElts.push_back(EltNo);
5484 }
5485 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005486
Bob Wilson6f2b8962011-01-07 21:37:30 +00005487 // Currently only do something sane when at most two source vectors
5488 // involved.
5489 if (SourceVecs.size() > 2)
5490 return SDValue();
5491
5492 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5493 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005494
Bob Wilson6f2b8962011-01-07 21:37:30 +00005495 // This loop extracts the usage patterns of the source vectors
5496 // and prepares appropriate SDValues for a shuffle if possible.
5497 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5498 if (SourceVecs[i].getValueType() == VT) {
5499 // No VEXT necessary
5500 ShuffleSrcs[i] = SourceVecs[i];
5501 VEXTOffsets[i] = 0;
5502 continue;
5503 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5504 // It probably isn't worth padding out a smaller vector just to
5505 // break it down again in a shuffle.
5506 return SDValue();
5507 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005508
Bob Wilson6f2b8962011-01-07 21:37:30 +00005509 // Since only 64-bit and 128-bit vectors are legal on ARM and
5510 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005511 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5512 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005513
Bob Wilson6f2b8962011-01-07 21:37:30 +00005514 if (MaxElts[i] - MinElts[i] >= NumElts) {
5515 // Span too large for a VEXT to cope
5516 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005517 }
5518
Bob Wilson6f2b8962011-01-07 21:37:30 +00005519 if (MinElts[i] >= NumElts) {
5520 // The extraction can just take the second half
5521 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005522 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5523 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005524 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005525 } else if (MaxElts[i] < NumElts) {
5526 // The extraction can just take the first half
5527 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005528 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5529 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005530 DAG.getIntPtrConstant(0, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005531 } else {
5532 // An actual VEXT is needed
5533 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005534 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5535 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005536 DAG.getIntPtrConstant(0, dl));
Eric Christopher2af95512011-01-14 23:50:53 +00005537 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5538 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005539 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005540 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005541 DAG.getConstant(VEXTOffsets[i], dl,
5542 MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005543 }
5544 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005545
Bob Wilson6f2b8962011-01-07 21:37:30 +00005546 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005547
Bob Wilson6f2b8962011-01-07 21:37:30 +00005548 for (unsigned i = 0; i < NumElts; ++i) {
5549 SDValue Entry = Op.getOperand(i);
5550 if (Entry.getOpcode() == ISD::UNDEF) {
5551 Mask.push_back(-1);
5552 continue;
5553 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005554
Bob Wilson6f2b8962011-01-07 21:37:30 +00005555 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005556 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5557 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005558 if (ExtractVec == SourceVecs[0]) {
5559 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5560 } else {
5561 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5562 }
5563 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005564
Bob Wilson6f2b8962011-01-07 21:37:30 +00005565 // Final check before we try to produce nonsense...
5566 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005567 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5568 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005569
Bob Wilson6f2b8962011-01-07 21:37:30 +00005570 return SDValue();
5571}
5572
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005573/// isShuffleMaskLegal - Targets can use this to indicate that they only
5574/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5575/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5576/// are assumed to be legal.
5577bool
5578ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5579 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005580 if (VT.getVectorNumElements() == 4 &&
5581 (VT.is128BitVector() || VT.is64BitVector())) {
5582 unsigned PFIndexes[4];
5583 for (unsigned i = 0; i != 4; ++i) {
5584 if (M[i] < 0)
5585 PFIndexes[i] = 8;
5586 else
5587 PFIndexes[i] = M[i];
5588 }
5589
5590 // Compute the index in the perfect shuffle table.
5591 unsigned PFTableIndex =
5592 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5593 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5594 unsigned Cost = (PFEntry >> 30);
5595
5596 if (Cost <= 4)
5597 return true;
5598 }
5599
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005600 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00005601 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005602
Bob Wilson846bd792010-06-07 23:53:38 +00005603 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5604 return (EltSize >= 32 ||
5605 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005606 isVREVMask(M, VT, 64) ||
5607 isVREVMask(M, VT, 32) ||
5608 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005609 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005610 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005611 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005612 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005613}
5614
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005615/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5616/// the specified operations to build the shuffle.
5617static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5618 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005619 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005620 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5621 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5622 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5623
5624 enum {
5625 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5626 OP_VREV,
5627 OP_VDUP0,
5628 OP_VDUP1,
5629 OP_VDUP2,
5630 OP_VDUP3,
5631 OP_VEXT1,
5632 OP_VEXT2,
5633 OP_VEXT3,
5634 OP_VUZPL, // VUZP, left result
5635 OP_VUZPR, // VUZP, right result
5636 OP_VZIPL, // VZIP, left result
5637 OP_VZIPR, // VZIP, right result
5638 OP_VTRNL, // VTRN, left result
5639 OP_VTRNR // VTRN, right result
5640 };
5641
5642 if (OpNum == OP_COPY) {
5643 if (LHSID == (1*9+2)*9+3) return LHS;
5644 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5645 return RHS;
5646 }
5647
5648 SDValue OpLHS, OpRHS;
5649 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5650 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5651 EVT VT = OpLHS.getValueType();
5652
5653 switch (OpNum) {
5654 default: llvm_unreachable("Unknown shuffle opcode!");
5655 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005656 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005657 if (VT.getVectorElementType() == MVT::i32 ||
5658 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005659 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5660 // vrev <4 x i16> -> VREV32
5661 if (VT.getVectorElementType() == MVT::i16)
5662 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5663 // vrev <4 x i8> -> VREV16
5664 assert(VT.getVectorElementType() == MVT::i8);
5665 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005666 case OP_VDUP0:
5667 case OP_VDUP1:
5668 case OP_VDUP2:
5669 case OP_VDUP3:
5670 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005671 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005672 case OP_VEXT1:
5673 case OP_VEXT2:
5674 case OP_VEXT3:
5675 return DAG.getNode(ARMISD::VEXT, dl, VT,
5676 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005677 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005678 case OP_VUZPL:
5679 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005680 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005681 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5682 case OP_VZIPL:
5683 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005684 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005685 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5686 case OP_VTRNL:
5687 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005688 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5689 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005690 }
5691}
5692
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005693static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005694 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005695 SelectionDAG &DAG) {
5696 // Check to see if we can use the VTBL instruction.
5697 SDValue V1 = Op.getOperand(0);
5698 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005699 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005700
5701 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005702 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005703 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005704 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005705
5706 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5707 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005708 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005709
Owen Anderson77aa2662011-04-05 21:48:57 +00005710 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005711 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005712}
5713
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005714static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5715 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005716 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005717 SDValue OpLHS = Op.getOperand(0);
5718 EVT VT = OpLHS.getValueType();
5719
5720 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5721 "Expect an v8i16/v16i8 type");
5722 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5723 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5724 // extract the first 8 bytes into the top double word and the last 8 bytes
5725 // into the bottom double word. The v8i16 case is similar.
5726 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5727 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005728 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005729}
5730
Bob Wilson2e076c42009-06-22 23:27:02 +00005731static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005732 SDValue V1 = Op.getOperand(0);
5733 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005734 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005735 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005736 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005737
Bob Wilsonc6800b52009-08-13 02:13:04 +00005738 // Convert shuffles that are directly supported on NEON to target-specific
5739 // DAG nodes, instead of keeping them as shuffles and matching them again
5740 // during code selection. This is more efficient and avoids the possibility
5741 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005742 // FIXME: floating-point vectors should be canonicalized to integer vectors
5743 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005744 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005745
Bob Wilson846bd792010-06-07 23:53:38 +00005746 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5747 if (EltSize <= 32) {
5748 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5749 int Lane = SVN->getSplatIndex();
5750 // If this is undef splat, generate it via "just" vdup, if possible.
5751 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005752
Dan Gohman198b7ff2011-11-03 21:49:52 +00005753 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005754 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5755 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5756 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005757 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5758 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5759 // reaches it).
5760 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5761 !isa<ConstantSDNode>(V1.getOperand(0))) {
5762 bool IsScalarToVector = true;
5763 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5764 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5765 IsScalarToVector = false;
5766 break;
5767 }
5768 if (IsScalarToVector)
5769 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5770 }
Bob Wilson846bd792010-06-07 23:53:38 +00005771 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005772 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005773 }
Bob Wilson846bd792010-06-07 23:53:38 +00005774
5775 bool ReverseVEXT;
5776 unsigned Imm;
5777 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5778 if (ReverseVEXT)
5779 std::swap(V1, V2);
5780 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005781 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005782 }
5783
5784 if (isVREVMask(ShuffleMask, VT, 64))
5785 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5786 if (isVREVMask(ShuffleMask, VT, 32))
5787 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5788 if (isVREVMask(ShuffleMask, VT, 16))
5789 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5790
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005791 if (V2->getOpcode() == ISD::UNDEF &&
5792 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5793 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005794 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005795 }
5796
Bob Wilson846bd792010-06-07 23:53:38 +00005797 // Check for Neon shuffles that modify both input vectors in place.
5798 // If both results are used, i.e., if there are two shuffles with the same
5799 // source operands and with masks corresponding to both results of one of
5800 // these operations, DAG memoization will ensure that a single node is
5801 // used for both shuffles.
5802 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005803 bool isV_UNDEF;
5804 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5805 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
5806 if (isV_UNDEF)
5807 V2 = V1;
5808 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
5809 .getValue(WhichResult);
5810 }
Bob Wilson846bd792010-06-07 23:53:38 +00005811
Ahmed Bougacha9a909422015-06-19 02:32:35 +00005812 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
5813 // shuffles that produce a result larger than their operands with:
5814 // shuffle(concat(v1, undef), concat(v2, undef))
5815 // ->
5816 // shuffle(concat(v1, v2), undef)
5817 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
5818 //
5819 // This is useful in the general case, but there are special cases where
5820 // native shuffles produce larger results: the two-result ops.
5821 //
5822 // Look through the concat when lowering them:
5823 // shuffle(concat(v1, v2), undef)
5824 // ->
5825 // concat(VZIP(v1, v2):0, :1)
5826 //
5827 if (V1->getOpcode() == ISD::CONCAT_VECTORS &&
5828 V2->getOpcode() == ISD::UNDEF) {
5829 SDValue SubV1 = V1->getOperand(0);
5830 SDValue SubV2 = V1->getOperand(1);
5831 EVT SubVT = SubV1.getValueType();
5832
5833 // We expect these to have been canonicalized to -1.
5834 assert(std::all_of(ShuffleMask.begin(), ShuffleMask.end(), [&](int i) {
5835 return i < (int)VT.getVectorNumElements();
5836 }) && "Unexpected shuffle index into UNDEF operand!");
5837
5838 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
5839 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
5840 if (isV_UNDEF)
5841 SubV2 = SubV1;
5842 assert((WhichResult == 0) &&
5843 "In-place shuffle of concat can only have one result!");
5844 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
5845 SubV1, SubV2);
5846 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
5847 Res.getValue(1));
5848 }
5849 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00005850 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005851
Bob Wilsona7062312009-08-21 20:54:19 +00005852 // If the shuffle is not directly supported and it has 4 elements, use
5853 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005854 unsigned NumElts = VT.getVectorNumElements();
5855 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005856 unsigned PFIndexes[4];
5857 for (unsigned i = 0; i != 4; ++i) {
5858 if (ShuffleMask[i] < 0)
5859 PFIndexes[i] = 8;
5860 else
5861 PFIndexes[i] = ShuffleMask[i];
5862 }
5863
5864 // Compute the index in the perfect shuffle table.
5865 unsigned PFTableIndex =
5866 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005867 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5868 unsigned Cost = (PFEntry >> 30);
5869
5870 if (Cost <= 4)
5871 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5872 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005873
Bob Wilsond8a9a042010-06-04 00:04:02 +00005874 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005875 if (EltSize >= 32) {
5876 // Do the expansion with floating-point types, since that is what the VFP
5877 // registers are defined to use, and since i64 is not legal.
5878 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5879 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005880 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5881 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005882 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005883 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005884 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005885 Ops.push_back(DAG.getUNDEF(EltVT));
5886 else
5887 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5888 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5889 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005890 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005891 }
Craig Topper48d114b2014-04-26 18:35:24 +00005892 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005893 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005894 }
5895
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005896 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5897 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5898
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005899 if (VT == MVT::v8i8) {
5900 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5901 if (NewOp.getNode())
5902 return NewOp;
5903 }
5904
Bob Wilson6f34e272009-08-14 05:16:33 +00005905 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005906}
5907
Eli Friedmana5e244c2011-10-24 23:08:52 +00005908static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5909 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5910 SDValue Lane = Op.getOperand(2);
5911 if (!isa<ConstantSDNode>(Lane))
5912 return SDValue();
5913
5914 return Op;
5915}
5916
Bob Wilson2e076c42009-06-22 23:27:02 +00005917static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005918 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005919 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005920 if (!isa<ConstantSDNode>(Lane))
5921 return SDValue();
5922
5923 SDValue Vec = Op.getOperand(0);
5924 if (Op.getValueType() == MVT::i32 &&
5925 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005926 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005927 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5928 }
5929
5930 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005931}
5932
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005933static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5934 // The only time a CONCAT_VECTORS operation can have legal types is when
5935 // two 64-bit vectors are concatenated to a 128-bit vector.
5936 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5937 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005938 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005939 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005940 SDValue Op0 = Op.getOperand(0);
5941 SDValue Op1 = Op.getOperand(1);
5942 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005943 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005944 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005945 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005946 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005947 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005948 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005949 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00005950 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005951}
5952
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005953/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5954/// element has been zero/sign-extended, depending on the isSigned parameter,
5955/// from an integer type half its size.
5956static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5957 bool isSigned) {
5958 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5959 EVT VT = N->getValueType(0);
5960 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5961 SDNode *BVN = N->getOperand(0).getNode();
5962 if (BVN->getValueType(0) != MVT::v4i32 ||
5963 BVN->getOpcode() != ISD::BUILD_VECTOR)
5964 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00005965 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005966 unsigned HiElt = 1 - LoElt;
5967 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5968 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5969 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5970 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5971 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5972 return false;
5973 if (isSigned) {
5974 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5975 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5976 return true;
5977 } else {
5978 if (Hi0->isNullValue() && Hi1->isNullValue())
5979 return true;
5980 }
5981 return false;
5982 }
5983
5984 if (N->getOpcode() != ISD::BUILD_VECTOR)
5985 return false;
5986
5987 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5988 SDNode *Elt = N->getOperand(i).getNode();
5989 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5990 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5991 unsigned HalfSize = EltSize / 2;
5992 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005993 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005994 return false;
5995 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005996 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005997 return false;
5998 }
5999 continue;
6000 }
6001 return false;
6002 }
6003
6004 return true;
6005}
6006
6007/// isSignExtended - Check if a node is a vector value that is sign-extended
6008/// or a constant BUILD_VECTOR with sign-extended elements.
6009static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6010 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6011 return true;
6012 if (isExtendedBUILD_VECTOR(N, DAG, true))
6013 return true;
6014 return false;
6015}
6016
6017/// isZeroExtended - Check if a node is a vector value that is zero-extended
6018/// or a constant BUILD_VECTOR with zero-extended elements.
6019static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6020 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6021 return true;
6022 if (isExtendedBUILD_VECTOR(N, DAG, false))
6023 return true;
6024 return false;
6025}
6026
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006027static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6028 if (OrigVT.getSizeInBits() >= 64)
6029 return OrigVT;
6030
6031 assert(OrigVT.isSimple() && "Expecting a simple value type");
6032
6033 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6034 switch (OrigSimpleTy) {
6035 default: llvm_unreachable("Unexpected Vector Type");
6036 case MVT::v2i8:
6037 case MVT::v2i16:
6038 return MVT::v2i32;
6039 case MVT::v4i8:
6040 return MVT::v4i16;
6041 }
6042}
6043
Sebastian Popa204f722012-11-30 19:08:04 +00006044/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6045/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6046/// We insert the required extension here to get the vector to fill a D register.
6047static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6048 const EVT &OrigTy,
6049 const EVT &ExtTy,
6050 unsigned ExtOpcode) {
6051 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6052 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6053 // 64-bits we need to insert a new extension so that it will be 64-bits.
6054 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6055 if (OrigTy.getSizeInBits() >= 64)
6056 return N;
6057
6058 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006059 EVT NewVT = getExtensionTo64Bits(OrigTy);
6060
Andrew Trickef9de2a2013-05-25 02:42:55 +00006061 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006062}
6063
6064/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6065/// does not do any sign/zero extension. If the original vector is less
6066/// than 64 bits, an appropriate extension will be added after the load to
6067/// reach a total size of 64 bits. We have to add the extension separately
6068/// because ARM does not have a sign/zero extending load for vectors.
6069static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006070 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6071
6072 // The load already has the right type.
6073 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006074 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00006075 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
6076 LD->isNonTemporal(), LD->isInvariant(),
6077 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006078
6079 // We need to create a zextload/sextload. We cannot just create a load
6080 // followed by a zext/zext node because LowerMUL is also run during normal
6081 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006082 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006083 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00006084 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006085 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00006086}
6087
6088/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6089/// extending load, or BUILD_VECTOR with extended elements, return the
6090/// unextended value. The unextended vector should be 64 bits so that it can
6091/// be used as an operand to a VMULL instruction. If the original vector size
6092/// before extension is less than 64 bits we add a an extension to resize
6093/// the vector to 64 bits.
6094static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006095 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006096 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6097 N->getOperand(0)->getValueType(0),
6098 N->getValueType(0),
6099 N->getOpcode());
6100
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006101 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006102 return SkipLoadExtensionForVMULL(LD, DAG);
6103
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006104 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6105 // have been legalized as a BITCAST from v4i32.
6106 if (N->getOpcode() == ISD::BITCAST) {
6107 SDNode *BVN = N->getOperand(0).getNode();
6108 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6109 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006110 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006111 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006112 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
6113 }
6114 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6115 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6116 EVT VT = N->getValueType(0);
6117 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
6118 unsigned NumElts = VT.getVectorNumElements();
6119 MVT TruncVT = MVT::getIntegerVT(EltSize);
6120 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006121 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006122 for (unsigned i = 0; i != NumElts; ++i) {
6123 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6124 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006125 // Element types smaller than 32 bits are not legal, so use i32 elements.
6126 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006127 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006128 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006129 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00006130 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006131}
6132
Evan Chenge2086e72011-03-29 01:56:09 +00006133static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6134 unsigned Opcode = N->getOpcode();
6135 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6136 SDNode *N0 = N->getOperand(0).getNode();
6137 SDNode *N1 = N->getOperand(1).getNode();
6138 return N0->hasOneUse() && N1->hasOneUse() &&
6139 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6140 }
6141 return false;
6142}
6143
6144static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6145 unsigned Opcode = N->getOpcode();
6146 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6147 SDNode *N0 = N->getOperand(0).getNode();
6148 SDNode *N1 = N->getOperand(1).getNode();
6149 return N0->hasOneUse() && N1->hasOneUse() &&
6150 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6151 }
6152 return false;
6153}
6154
Bob Wilson38ab35a2010-09-01 23:50:19 +00006155static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6156 // Multiplications are only custom-lowered for 128-bit vectors so that
6157 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6158 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006159 assert(VT.is128BitVector() && VT.isInteger() &&
6160 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006161 SDNode *N0 = Op.getOperand(0).getNode();
6162 SDNode *N1 = Op.getOperand(1).getNode();
6163 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006164 bool isMLA = false;
6165 bool isN0SExt = isSignExtended(N0, DAG);
6166 bool isN1SExt = isSignExtended(N1, DAG);
6167 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006168 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006169 else {
6170 bool isN0ZExt = isZeroExtended(N0, DAG);
6171 bool isN1ZExt = isZeroExtended(N1, DAG);
6172 if (isN0ZExt && isN1ZExt)
6173 NewOpc = ARMISD::VMULLu;
6174 else if (isN1SExt || isN1ZExt) {
6175 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6176 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6177 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6178 NewOpc = ARMISD::VMULLs;
6179 isMLA = true;
6180 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6181 NewOpc = ARMISD::VMULLu;
6182 isMLA = true;
6183 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6184 std::swap(N0, N1);
6185 NewOpc = ARMISD::VMULLu;
6186 isMLA = true;
6187 }
6188 }
6189
6190 if (!NewOpc) {
6191 if (VT == MVT::v2i64)
6192 // Fall through to expand this. It is not legal.
6193 return SDValue();
6194 else
6195 // Other vector multiplications are legal.
6196 return Op;
6197 }
6198 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006199
6200 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006201 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006202 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006203 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006204 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006205 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006206 assert(Op0.getValueType().is64BitVector() &&
6207 Op1.getValueType().is64BitVector() &&
6208 "unexpected types for extended operands to VMULL");
6209 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6210 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006211
Evan Chenge2086e72011-03-29 01:56:09 +00006212 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6213 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6214 // vmull q0, d4, d6
6215 // vmlal q0, d5, d6
6216 // is faster than
6217 // vaddl q0, d4, d5
6218 // vmovl q1, d6
6219 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006220 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6221 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006222 EVT Op1VT = Op1.getValueType();
6223 return DAG.getNode(N0->getOpcode(), DL, VT,
6224 DAG.getNode(NewOpc, DL, VT,
6225 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6226 DAG.getNode(NewOpc, DL, VT,
6227 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006228}
6229
Owen Anderson77aa2662011-04-05 21:48:57 +00006230static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006231LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006232 // Convert to float
6233 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6234 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6235 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6236 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6237 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6238 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6239 // Get reciprocal estimate.
6240 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006241 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006242 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6243 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006244 // Because char has a smaller range than uchar, we can actually get away
6245 // without any newton steps. This requires that we use a weird bias
6246 // of 0xb000, however (again, this has been exhaustively tested).
6247 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6248 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6249 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006250 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006251 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6252 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6253 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6254 // Convert back to short.
6255 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6256 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6257 return X;
6258}
6259
Owen Anderson77aa2662011-04-05 21:48:57 +00006260static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006261LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006262 SDValue N2;
6263 // Convert to float.
6264 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6265 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6266 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6267 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6268 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6269 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006270
Nate Begemanfa62d502011-02-11 20:53:29 +00006271 // Use reciprocal estimate and one refinement step.
6272 // float4 recip = vrecpeq_f32(yf);
6273 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006274 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006275 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6276 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006277 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006278 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006279 N1, N2);
6280 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6281 // Because short has a smaller range than ushort, we can actually get away
6282 // with only a single newton step. This requires that we use a weird bias
6283 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006284 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006285 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6286 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006287 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006288 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6289 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6290 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6291 // Convert back to integer and return.
6292 // return vmovn_s32(vcvt_s32_f32(result));
6293 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6294 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6295 return N0;
6296}
6297
6298static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6299 EVT VT = Op.getValueType();
6300 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6301 "unexpected type for custom-lowering ISD::SDIV");
6302
Andrew Trickef9de2a2013-05-25 02:42:55 +00006303 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006304 SDValue N0 = Op.getOperand(0);
6305 SDValue N1 = Op.getOperand(1);
6306 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006307
Nate Begemanfa62d502011-02-11 20:53:29 +00006308 if (VT == MVT::v8i8) {
6309 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6310 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006311
Nate Begemanfa62d502011-02-11 20:53:29 +00006312 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006313 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006314 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006315 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006316 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006317 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006318 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006319 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006320
6321 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6322 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6323
6324 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6325 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006326
Nate Begemanfa62d502011-02-11 20:53:29 +00006327 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6328 return N0;
6329 }
6330 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6331}
6332
6333static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6334 EVT VT = Op.getValueType();
6335 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6336 "unexpected type for custom-lowering ISD::UDIV");
6337
Andrew Trickef9de2a2013-05-25 02:42:55 +00006338 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006339 SDValue N0 = Op.getOperand(0);
6340 SDValue N1 = Op.getOperand(1);
6341 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006342
Nate Begemanfa62d502011-02-11 20:53:29 +00006343 if (VT == MVT::v8i8) {
6344 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6345 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006346
Nate Begemanfa62d502011-02-11 20:53:29 +00006347 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006348 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006349 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006350 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006351 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006352 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006353 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006354 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006355
Nate Begemanfa62d502011-02-11 20:53:29 +00006356 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6357 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006358
Nate Begemanfa62d502011-02-11 20:53:29 +00006359 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6360 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006361
6362 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006363 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6364 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006365 N0);
6366 return N0;
6367 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006368
Nate Begemanfa62d502011-02-11 20:53:29 +00006369 // v4i16 sdiv ... Convert to float.
6370 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6371 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6372 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6373 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6374 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006375 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006376
6377 // Use reciprocal estimate and two refinement steps.
6378 // float4 recip = vrecpeq_f32(yf);
6379 // recip *= vrecpsq_f32(yf, recip);
6380 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006381 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006382 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6383 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006384 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006385 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006386 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006387 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006388 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006389 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006390 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006391 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6392 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6393 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6394 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006395 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006396 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6397 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006398 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006399 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6400 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6401 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6402 // Convert back to integer and return.
6403 // return vmovn_u32(vcvt_s32_f32(result));
6404 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6405 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6406 return N0;
6407}
6408
Evan Chenge8916542011-08-30 01:34:54 +00006409static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6410 EVT VT = Op.getNode()->getValueType(0);
6411 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6412
6413 unsigned Opc;
6414 bool ExtraOp = false;
6415 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006416 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006417 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6418 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6419 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6420 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6421 }
6422
6423 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006424 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006425 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006426 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006427 Op.getOperand(1), Op.getOperand(2));
6428}
6429
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006430SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6431 assert(Subtarget->isTargetDarwin());
6432
6433 // For iOS, we want to call an alternative entry point: __sincos_stret,
6434 // return values are passed via sret.
6435 SDLoc dl(Op);
6436 SDValue Arg = Op.getOperand(0);
6437 EVT ArgVT = Arg.getValueType();
6438 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00006439 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006440
6441 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006442
6443 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006444 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006445
6446 // Create stack object for sret.
Mehdi Amini44ede332015-07-09 02:09:04 +00006447 auto &DL = DAG.getDataLayout();
6448 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
6449 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006450 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00006451 SDValue SRet = DAG.getFrameIndex(FrameIdx, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006452
6453 ArgListTy Args;
6454 ArgListEntry Entry;
6455
6456 Entry.Node = SRet;
6457 Entry.Ty = RetTy->getPointerTo();
6458 Entry.isSExt = false;
6459 Entry.isZExt = false;
6460 Entry.isSRet = true;
6461 Args.push_back(Entry);
6462
6463 Entry.Node = Arg;
6464 Entry.Ty = ArgTy;
6465 Entry.isSExt = false;
6466 Entry.isZExt = false;
6467 Args.push_back(Entry);
6468
6469 const char *LibcallName = (ArgVT == MVT::f64)
6470 ? "__sincos_stret" : "__sincosf_stret";
Mehdi Amini44ede332015-07-09 02:09:04 +00006471 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006472
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006473 TargetLowering::CallLoweringInfo CLI(DAG);
6474 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6475 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006476 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006477 .setDiscardResult();
6478
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006479 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6480
6481 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6482 MachinePointerInfo(), false, false, false, 0);
6483
6484 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00006485 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006486 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006487 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6488 MachinePointerInfo(), false, false, false, 0);
6489
6490 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6491 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6492 LoadSin.getValue(0), LoadCos.getValue(0));
6493}
6494
Eli Friedman10f9ce22011-09-15 22:26:18 +00006495static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006496 // Monotonic load/store is legal for all targets
6497 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6498 return Op;
6499
Alp Tokercb402912014-01-24 17:20:08 +00006500 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006501 // dmb or equivalent available.
6502 return SDValue();
6503}
6504
Tim Northoverbc933082013-05-23 19:11:20 +00006505static void ReplaceREADCYCLECOUNTER(SDNode *N,
6506 SmallVectorImpl<SDValue> &Results,
6507 SelectionDAG &DAG,
6508 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006509 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006510 SDValue Cycles32, OutChain;
6511
6512 if (Subtarget->hasPerfMon()) {
6513 // Under Power Management extensions, the cycle-count is:
6514 // mrc p15, #0, <Rt>, c9, c13, #0
6515 SDValue Ops[] = { N->getOperand(0), // Chain
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006516 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6517 DAG.getConstant(15, DL, MVT::i32),
6518 DAG.getConstant(0, DL, MVT::i32),
6519 DAG.getConstant(9, DL, MVT::i32),
6520 DAG.getConstant(13, DL, MVT::i32),
6521 DAG.getConstant(0, DL, MVT::i32)
Tim Northoverbc933082013-05-23 19:11:20 +00006522 };
6523
6524 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006525 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006526 OutChain = Cycles32.getValue(1);
6527 } else {
6528 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6529 // there are older ARM CPUs that have implementation-specific ways of
6530 // obtaining this information (FIXME!).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006531 Cycles32 = DAG.getConstant(0, DL, MVT::i32);
Tim Northoverbc933082013-05-23 19:11:20 +00006532 OutChain = DAG.getEntryNode();
6533 }
6534
6535
6536 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006537 Cycles32, DAG.getConstant(0, DL, MVT::i32));
Tim Northoverbc933082013-05-23 19:11:20 +00006538 Results.push_back(Cycles64);
6539 Results.push_back(OutChain);
6540}
6541
Dan Gohman21cea8a2010-04-17 15:26:15 +00006542SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006543 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006544 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006545 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006546 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006547 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006548 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006549 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6550 default: llvm_unreachable("unknown object format");
6551 case Triple::COFF:
6552 return LowerGlobalAddressWindows(Op, DAG);
6553 case Triple::ELF:
6554 return LowerGlobalAddressELF(Op, DAG);
6555 case Triple::MachO:
6556 return LowerGlobalAddressDarwin(Op, DAG);
6557 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006558 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006559 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006560 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6561 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006562 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006563 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006564 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006565 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006566 case ISD::SINT_TO_FP:
6567 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6568 case ISD::FP_TO_SINT:
6569 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006570 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006571 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006572 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006573 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006574 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006575 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006576 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6577 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006578 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006579 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006580 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006581 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006582 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006583 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006584 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00006585 case ISD::CTTZ:
6586 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006587 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006588 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006589 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006590 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006591 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006592 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006593 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006594 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006595 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006596 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006597 case ISD::SDIV: return LowerSDIV(Op, DAG);
6598 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006599 case ISD::ADDC:
6600 case ISD::ADDE:
6601 case ISD::SUBC:
6602 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006603 case ISD::SADDO:
6604 case ISD::UADDO:
6605 case ISD::SSUBO:
6606 case ISD::USUBO:
6607 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006608 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006609 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006610 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006611 case ISD::SDIVREM:
6612 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006613 case ISD::DYNAMIC_STACKALLOC:
6614 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6615 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6616 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006617 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6618 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006619 }
Evan Cheng10043e22007-01-19 07:51:42 +00006620}
6621
Duncan Sands6ed40142008-12-01 11:39:25 +00006622/// ReplaceNodeResults - Replace the results of node with an illegal result
6623/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006624void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6625 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006626 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006627 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006628 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006629 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006630 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00006631 case ISD::READ_REGISTER:
6632 ExpandREAD_REGISTER(N, Results, DAG);
6633 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00006634 case ISD::BITCAST:
6635 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006636 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006637 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006638 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006639 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006640 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006641 case ISD::READCYCLECOUNTER:
6642 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6643 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006644 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006645 if (Res.getNode())
6646 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006647}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006648
Evan Cheng10043e22007-01-19 07:51:42 +00006649//===----------------------------------------------------------------------===//
6650// ARM Scheduler Hooks
6651//===----------------------------------------------------------------------===//
6652
Bill Wendling030b58e2011-10-06 22:18:16 +00006653/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6654/// registers the function context.
6655void ARMTargetLowering::
6656SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6657 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006658 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006659 DebugLoc dl = MI->getDebugLoc();
6660 MachineFunction *MF = MBB->getParent();
6661 MachineRegisterInfo *MRI = &MF->getRegInfo();
6662 MachineConstantPool *MCP = MF->getConstantPool();
6663 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6664 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006665
Bill Wendling374ee192011-10-03 21:25:38 +00006666 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006667 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006668
Bill Wendling374ee192011-10-03 21:25:38 +00006669 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006670 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006671 ARMConstantPoolValue *CPV =
6672 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6673 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6674
Craig Topper61e88f42014-11-21 05:58:21 +00006675 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6676 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006677
Bill Wendling030b58e2011-10-06 22:18:16 +00006678 // Grab constant pool and fixed stack memory operands.
6679 MachineMemOperand *CPMMO =
6680 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6681 MachineMemOperand::MOLoad, 4, 4);
6682
6683 MachineMemOperand *FIMMOSt =
6684 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6685 MachineMemOperand::MOStore, 4, 4);
6686
6687 // Load the address of the dispatch MBB into the jump buffer.
6688 if (isThumb2) {
6689 // Incoming value: jbuf
6690 // ldr.n r5, LCPI1_1
6691 // orr r5, r5, #1
6692 // add r5, pc
6693 // str r5, [$jbuf, #+4] ; &jbuf[1]
6694 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6695 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6696 .addConstantPoolIndex(CPI)
6697 .addMemOperand(CPMMO));
6698 // Set the low bit because of thumb mode.
6699 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6700 AddDefaultCC(
6701 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6702 .addReg(NewVReg1, RegState::Kill)
6703 .addImm(0x01)));
6704 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6705 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6706 .addReg(NewVReg2, RegState::Kill)
6707 .addImm(PCLabelId);
6708 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6709 .addReg(NewVReg3, RegState::Kill)
6710 .addFrameIndex(FI)
6711 .addImm(36) // &jbuf[1] :: pc
6712 .addMemOperand(FIMMOSt));
6713 } else if (isThumb) {
6714 // Incoming value: jbuf
6715 // ldr.n r1, LCPI1_4
6716 // add r1, pc
6717 // mov r2, #1
6718 // orrs r1, r2
6719 // add r2, $jbuf, #+4 ; &jbuf[1]
6720 // str r1, [r2]
6721 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6722 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6723 .addConstantPoolIndex(CPI)
6724 .addMemOperand(CPMMO));
6725 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6726 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6727 .addReg(NewVReg1, RegState::Kill)
6728 .addImm(PCLabelId);
6729 // Set the low bit because of thumb mode.
6730 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6731 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6732 .addReg(ARM::CPSR, RegState::Define)
6733 .addImm(1));
6734 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6735 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6736 .addReg(ARM::CPSR, RegState::Define)
6737 .addReg(NewVReg2, RegState::Kill)
6738 .addReg(NewVReg3, RegState::Kill));
6739 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006740 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6741 .addFrameIndex(FI)
6742 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006743 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6744 .addReg(NewVReg4, RegState::Kill)
6745 .addReg(NewVReg5, RegState::Kill)
6746 .addImm(0)
6747 .addMemOperand(FIMMOSt));
6748 } else {
6749 // Incoming value: jbuf
6750 // ldr r1, LCPI1_1
6751 // add r1, pc, r1
6752 // str r1, [$jbuf, #+4] ; &jbuf[1]
6753 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6754 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6755 .addConstantPoolIndex(CPI)
6756 .addImm(0)
6757 .addMemOperand(CPMMO));
6758 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6759 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6760 .addReg(NewVReg1, RegState::Kill)
6761 .addImm(PCLabelId));
6762 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6763 .addReg(NewVReg2, RegState::Kill)
6764 .addFrameIndex(FI)
6765 .addImm(36) // &jbuf[1] :: pc
6766 .addMemOperand(FIMMOSt));
6767 }
6768}
6769
Matthias Brauneec4efc2015-04-28 00:37:05 +00006770void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6771 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006772 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006773 DebugLoc dl = MI->getDebugLoc();
6774 MachineFunction *MF = MBB->getParent();
6775 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006776 MachineFrameInfo *MFI = MF->getFrameInfo();
6777 int FI = MFI->getFunctionContextIndex();
6778
Craig Topper61e88f42014-11-21 05:58:21 +00006779 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6780 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006781
Bill Wendling362c1b02011-10-06 21:29:56 +00006782 // Get a mapping of the call site numbers to all of the landing pads they're
6783 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006784 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6785 unsigned MaxCSNum = 0;
6786 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006787 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6788 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006789 if (!BB->isLandingPad()) continue;
6790
6791 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6792 // pad.
6793 for (MachineBasicBlock::iterator
6794 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6795 if (!II->isEHLabel()) continue;
6796
6797 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006798 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006799
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006800 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6801 for (SmallVectorImpl<unsigned>::iterator
6802 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6803 CSI != CSE; ++CSI) {
6804 CallSiteNumToLPad[*CSI].push_back(BB);
6805 MaxCSNum = std::max(MaxCSNum, *CSI);
6806 }
Bill Wendling202803e2011-10-05 00:02:33 +00006807 break;
6808 }
6809 }
6810
6811 // Get an ordered list of the machine basic blocks for the jump table.
6812 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006813 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006814 LPadList.reserve(CallSiteNumToLPad.size());
6815 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6816 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6817 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006818 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006819 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006820 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6821 }
Bill Wendling202803e2011-10-05 00:02:33 +00006822 }
6823
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006824 assert(!LPadList.empty() &&
6825 "No landing pad destinations for the dispatch jump table!");
6826
Bill Wendling362c1b02011-10-06 21:29:56 +00006827 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006828 MachineJumpTableInfo *JTI =
6829 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6830 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00006831 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006832
Bill Wendling362c1b02011-10-06 21:29:56 +00006833 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006834
6835 // Shove the dispatch's address into the return slot in the function context.
6836 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6837 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006838
Bill Wendling324be982011-10-05 00:39:32 +00006839 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006840 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006841 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006842 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006843 else
6844 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6845
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006846 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006847 DispatchBB->addSuccessor(TrapBB);
6848
6849 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6850 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006851
Bill Wendling510fbcd2011-10-17 21:32:56 +00006852 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006853 MF->insert(MF->end(), DispatchBB);
6854 MF->insert(MF->end(), DispContBB);
6855 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006856
Bill Wendling030b58e2011-10-06 22:18:16 +00006857 // Insert code into the entry block that creates and registers the function
6858 // context.
6859 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6860
Bill Wendling030b58e2011-10-06 22:18:16 +00006861 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006862 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006863 MachineMemOperand::MOLoad |
6864 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006865
Chad Rosier1ec8e402012-11-06 23:05:24 +00006866 MachineInstrBuilder MIB;
6867 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6868
6869 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6870 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6871
6872 // Add a register mask with no preserved registers. This results in all
6873 // registers being marked as clobbered.
6874 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006875
Bill Wendling85833f72011-10-18 22:49:07 +00006876 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006877 if (Subtarget->isThumb2()) {
6878 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6879 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6880 .addFrameIndex(FI)
6881 .addImm(4)
6882 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006883
Bill Wendling85833f72011-10-18 22:49:07 +00006884 if (NumLPads < 256) {
6885 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6886 .addReg(NewVReg1)
6887 .addImm(LPadList.size()));
6888 } else {
6889 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6890 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006891 .addImm(NumLPads & 0xFFFF));
6892
6893 unsigned VReg2 = VReg1;
6894 if ((NumLPads & 0xFFFF0000) != 0) {
6895 VReg2 = MRI->createVirtualRegister(TRC);
6896 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6897 .addReg(VReg1)
6898 .addImm(NumLPads >> 16));
6899 }
6900
Bill Wendling85833f72011-10-18 22:49:07 +00006901 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6902 .addReg(NewVReg1)
6903 .addReg(VReg2));
6904 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006905
Bill Wendling5626c662011-10-06 22:53:00 +00006906 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6907 .addMBB(TrapBB)
6908 .addImm(ARMCC::HI)
6909 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006910
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006911 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6912 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00006913 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00006914
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006915 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006916 AddDefaultCC(
6917 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006918 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6919 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006920 .addReg(NewVReg1)
6921 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6922
6923 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006924 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006925 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00006926 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00006927 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006928 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6929 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6930 .addFrameIndex(FI)
6931 .addImm(1)
6932 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006933
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006934 if (NumLPads < 256) {
6935 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6936 .addReg(NewVReg1)
6937 .addImm(NumLPads));
6938 } else {
6939 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006940 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6941 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6942
6943 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00006944 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006945 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00006946 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006947 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006948
6949 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6950 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6951 .addReg(VReg1, RegState::Define)
6952 .addConstantPoolIndex(Idx));
6953 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6954 .addReg(NewVReg1)
6955 .addReg(VReg1));
6956 }
6957
Bill Wendlingb3d46782011-10-06 23:37:36 +00006958 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6959 .addMBB(TrapBB)
6960 .addImm(ARMCC::HI)
6961 .addReg(ARM::CPSR);
6962
6963 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6964 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6965 .addReg(ARM::CPSR, RegState::Define)
6966 .addReg(NewVReg1)
6967 .addImm(2));
6968
6969 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006970 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00006971 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00006972
6973 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6974 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6975 .addReg(ARM::CPSR, RegState::Define)
6976 .addReg(NewVReg2, RegState::Kill)
6977 .addReg(NewVReg3));
6978
6979 MachineMemOperand *JTMMOLd =
6980 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6981 MachineMemOperand::MOLoad, 4, 4);
6982
6983 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6984 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6985 .addReg(NewVReg4, RegState::Kill)
6986 .addImm(0)
6987 .addMemOperand(JTMMOLd));
6988
Chad Rosier96603432013-03-01 18:30:38 +00006989 unsigned NewVReg6 = NewVReg5;
6990 if (RelocM == Reloc::PIC_) {
6991 NewVReg6 = MRI->createVirtualRegister(TRC);
6992 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6993 .addReg(ARM::CPSR, RegState::Define)
6994 .addReg(NewVReg5, RegState::Kill)
6995 .addReg(NewVReg3));
6996 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006997
6998 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6999 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007000 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007001 } else {
7002 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7003 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
7004 .addFrameIndex(FI)
7005 .addImm(4)
7006 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00007007
Bill Wendling4969dcd2011-10-18 22:52:20 +00007008 if (NumLPads < 256) {
7009 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
7010 .addReg(NewVReg1)
7011 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00007012 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00007013 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7014 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007015 .addImm(NumLPads & 0xFFFF));
7016
7017 unsigned VReg2 = VReg1;
7018 if ((NumLPads & 0xFFFF0000) != 0) {
7019 VReg2 = MRI->createVirtualRegister(TRC);
7020 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
7021 .addReg(VReg1)
7022 .addImm(NumLPads >> 16));
7023 }
7024
Bill Wendling4969dcd2011-10-18 22:52:20 +00007025 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7026 .addReg(NewVReg1)
7027 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00007028 } else {
7029 MachineConstantPool *ConstantPool = MF->getConstantPool();
7030 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7031 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7032
7033 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007034 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007035 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007036 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007037 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7038
7039 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7040 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
7041 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00007042 .addConstantPoolIndex(Idx)
7043 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00007044 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
7045 .addReg(NewVReg1)
7046 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00007047 }
7048
Bill Wendling5626c662011-10-06 22:53:00 +00007049 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
7050 .addMBB(TrapBB)
7051 .addImm(ARMCC::HI)
7052 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00007053
Bill Wendling973c8172011-10-18 22:11:18 +00007054 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007055 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00007056 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00007057 .addReg(NewVReg1)
7058 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00007059 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7060 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007061 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00007062
7063 MachineMemOperand *JTMMOLd =
7064 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
7065 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00007066 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007067 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00007068 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
7069 .addReg(NewVReg3, RegState::Kill)
7070 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00007071 .addImm(0)
7072 .addMemOperand(JTMMOLd));
7073
Chad Rosier96603432013-03-01 18:30:38 +00007074 if (RelocM == Reloc::PIC_) {
7075 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
7076 .addReg(NewVReg5, RegState::Kill)
7077 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00007078 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007079 } else {
7080 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
7081 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007082 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00007083 }
Bill Wendling5626c662011-10-06 22:53:00 +00007084 }
Bill Wendling202803e2011-10-05 00:02:33 +00007085
Bill Wendling324be982011-10-05 00:39:32 +00007086 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00007087 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00007088 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007089 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
7090 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00007091 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00007092 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007093 }
7094
Bill Wendling26d27802011-10-17 05:25:09 +00007095 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00007096 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00007097 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00007098 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007099
7100 // Remove the landing pad successor from the invoke block and replace it
7101 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00007102 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
7103 BB->succ_end());
7104 while (!Successors.empty()) {
7105 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00007106 if (SMBB->isLandingPad()) {
7107 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00007108 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00007109 }
7110 }
7111
7112 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007113
7114 // Find the invoke call and mark all of the callee-saved registers as
7115 // 'implicit defined' so that they're spilled. This prevents code from
7116 // moving instructions to before the EH block, where they will never be
7117 // executed.
7118 for (MachineBasicBlock::reverse_iterator
7119 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007120 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007121
7122 DenseMap<unsigned, bool> DefRegs;
7123 for (MachineInstr::mop_iterator
7124 OI = II->operands_begin(), OE = II->operands_end();
7125 OI != OE; ++OI) {
7126 if (!OI->isReg()) continue;
7127 DefRegs[OI->getReg()] = true;
7128 }
7129
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00007130 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007131
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007132 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00007133 unsigned Reg = SavedRegs[i];
7134 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00007135 !ARM::tGPRRegClass.contains(Reg) &&
7136 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007137 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007138 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007139 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00007140 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00007141 continue;
7142 if (!DefRegs[Reg])
7143 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007144 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007145
7146 break;
7147 }
Bill Wendling883ec972011-10-07 23:18:02 +00007148 }
Bill Wendling324be982011-10-05 00:39:32 +00007149
Bill Wendling617075f2011-10-18 18:30:49 +00007150 // Mark all former landing pads as non-landing pads. The dispatch is the only
7151 // landing pad now.
7152 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7153 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7154 (*I)->setIsLandingPad(false);
7155
Bill Wendling324be982011-10-05 00:39:32 +00007156 // The instruction is gone now.
7157 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00007158}
7159
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007160static
7161MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7162 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7163 E = MBB->succ_end(); I != E; ++I)
7164 if (*I != Succ)
7165 return *I;
7166 llvm_unreachable("Expecting a BB with two successors!");
7167}
7168
Manman Renb504f492013-10-29 22:27:32 +00007169/// Return the load opcode for a given load size. If load size >= 8,
7170/// neon opcode will be returned.
7171static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7172 if (LdSize >= 8)
7173 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7174 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7175 if (IsThumb1)
7176 return LdSize == 4 ? ARM::tLDRi
7177 : LdSize == 2 ? ARM::tLDRHi
7178 : LdSize == 1 ? ARM::tLDRBi : 0;
7179 if (IsThumb2)
7180 return LdSize == 4 ? ARM::t2LDR_POST
7181 : LdSize == 2 ? ARM::t2LDRH_POST
7182 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7183 return LdSize == 4 ? ARM::LDR_POST_IMM
7184 : LdSize == 2 ? ARM::LDRH_POST
7185 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7186}
7187
7188/// Return the store opcode for a given store size. If store size >= 8,
7189/// neon opcode will be returned.
7190static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7191 if (StSize >= 8)
7192 return StSize == 16 ? ARM::VST1q32wb_fixed
7193 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7194 if (IsThumb1)
7195 return StSize == 4 ? ARM::tSTRi
7196 : StSize == 2 ? ARM::tSTRHi
7197 : StSize == 1 ? ARM::tSTRBi : 0;
7198 if (IsThumb2)
7199 return StSize == 4 ? ARM::t2STR_POST
7200 : StSize == 2 ? ARM::t2STRH_POST
7201 : StSize == 1 ? ARM::t2STRB_POST : 0;
7202 return StSize == 4 ? ARM::STR_POST_IMM
7203 : StSize == 2 ? ARM::STRH_POST
7204 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7205}
7206
7207/// Emit a post-increment load operation with given size. The instructions
7208/// will be added to BB at Pos.
7209static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7210 const TargetInstrInfo *TII, DebugLoc dl,
7211 unsigned LdSize, unsigned Data, unsigned AddrIn,
7212 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7213 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7214 assert(LdOpc != 0 && "Should have a load opcode");
7215 if (LdSize >= 8) {
7216 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7217 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7218 .addImm(0));
7219 } else if (IsThumb1) {
7220 // load + update AddrIn
7221 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7222 .addReg(AddrIn).addImm(0));
7223 MachineInstrBuilder MIB =
7224 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7225 MIB = AddDefaultT1CC(MIB);
7226 MIB.addReg(AddrIn).addImm(LdSize);
7227 AddDefaultPred(MIB);
7228 } else if (IsThumb2) {
7229 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7230 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7231 .addImm(LdSize));
7232 } else { // arm
7233 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7234 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7235 .addReg(0).addImm(LdSize));
7236 }
7237}
7238
7239/// Emit a post-increment store operation with given size. The instructions
7240/// will be added to BB at Pos.
7241static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7242 const TargetInstrInfo *TII, DebugLoc dl,
7243 unsigned StSize, unsigned Data, unsigned AddrIn,
7244 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7245 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7246 assert(StOpc != 0 && "Should have a store opcode");
7247 if (StSize >= 8) {
7248 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7249 .addReg(AddrIn).addImm(0).addReg(Data));
7250 } else if (IsThumb1) {
7251 // store + update AddrIn
7252 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7253 .addReg(AddrIn).addImm(0));
7254 MachineInstrBuilder MIB =
7255 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7256 MIB = AddDefaultT1CC(MIB);
7257 MIB.addReg(AddrIn).addImm(StSize);
7258 AddDefaultPred(MIB);
7259 } else if (IsThumb2) {
7260 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7261 .addReg(Data).addReg(AddrIn).addImm(StSize));
7262 } else { // arm
7263 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7264 .addReg(Data).addReg(AddrIn).addReg(0)
7265 .addImm(StSize));
7266 }
7267}
7268
David Peixottoc32e24a2013-10-17 19:49:22 +00007269MachineBasicBlock *
7270ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7271 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007272 // This pseudo instruction has 3 operands: dst, src, size
7273 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7274 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007275 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007276 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7277 MachineFunction::iterator It = BB;
7278 ++It;
7279
7280 unsigned dest = MI->getOperand(0).getReg();
7281 unsigned src = MI->getOperand(1).getReg();
7282 unsigned SizeVal = MI->getOperand(2).getImm();
7283 unsigned Align = MI->getOperand(3).getImm();
7284 DebugLoc dl = MI->getDebugLoc();
7285
Manman Rene8735522012-06-01 19:33:18 +00007286 MachineFunction *MF = BB->getParent();
7287 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007288 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007289 const TargetRegisterClass *TRC = nullptr;
7290 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007291
7292 bool IsThumb1 = Subtarget->isThumb1Only();
7293 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007294
7295 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007296 UnitSize = 1;
7297 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007298 UnitSize = 2;
7299 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007300 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007301 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007302 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007303 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007304 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007305 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007306 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007307 }
7308 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007309 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007310 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007311 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007312
David Peixottob0653e532013-10-24 16:39:36 +00007313 // Select the correct opcode and register class for unit size load/store
7314 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007315 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007316 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007317 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7318 : UnitSize == 8 ? &ARM::DPRRegClass
7319 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007320
Manman Rene8735522012-06-01 19:33:18 +00007321 unsigned BytesLeft = SizeVal % UnitSize;
7322 unsigned LoopSize = SizeVal - BytesLeft;
7323
7324 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7325 // Use LDR and STR to copy.
7326 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7327 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7328 unsigned srcIn = src;
7329 unsigned destIn = dest;
7330 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007331 unsigned srcOut = MRI.createVirtualRegister(TRC);
7332 unsigned destOut = MRI.createVirtualRegister(TRC);
7333 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007334 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7335 IsThumb1, IsThumb2);
7336 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7337 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007338 srcIn = srcOut;
7339 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007340 }
7341
7342 // Handle the leftover bytes with LDRB and STRB.
7343 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7344 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007345 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007346 unsigned srcOut = MRI.createVirtualRegister(TRC);
7347 unsigned destOut = MRI.createVirtualRegister(TRC);
7348 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007349 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7350 IsThumb1, IsThumb2);
7351 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7352 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007353 srcIn = srcOut;
7354 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007355 }
7356 MI->eraseFromParent(); // The instruction is gone now.
7357 return BB;
7358 }
7359
7360 // Expand the pseudo op to a loop.
7361 // thisMBB:
7362 // ...
7363 // movw varEnd, # --> with thumb2
7364 // movt varEnd, #
7365 // ldrcp varEnd, idx --> without thumb2
7366 // fallthrough --> loopMBB
7367 // loopMBB:
7368 // PHI varPhi, varEnd, varLoop
7369 // PHI srcPhi, src, srcLoop
7370 // PHI destPhi, dst, destLoop
7371 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7372 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7373 // subs varLoop, varPhi, #UnitSize
7374 // bne loopMBB
7375 // fallthrough --> exitMBB
7376 // exitMBB:
7377 // epilogue to handle left-over bytes
7378 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7379 // [destOut] = STRB_POST(scratch, destLoop, 1)
7380 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7381 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7382 MF->insert(It, loopMBB);
7383 MF->insert(It, exitMBB);
7384
7385 // Transfer the remainder of BB and its successor edges to exitMBB.
7386 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007387 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007388 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7389
7390 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007391 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007392 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007393 unsigned Vtmp = varEnd;
7394 if ((LoopSize & 0xFFFF0000) != 0)
7395 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007396 AddDefaultPred(BuildMI(BB, dl,
7397 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7398 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007399
7400 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007401 AddDefaultPred(BuildMI(BB, dl,
7402 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7403 varEnd)
7404 .addReg(Vtmp)
7405 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007406 } else {
7407 MachineConstantPool *ConstantPool = MF->getConstantPool();
7408 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7409 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7410
7411 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007412 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00007413 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007414 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00007415 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7416
7417 if (IsThumb1)
7418 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7419 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7420 else
7421 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7422 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7423 }
Manman Rene8735522012-06-01 19:33:18 +00007424 BB->addSuccessor(loopMBB);
7425
7426 // Generate the loop body:
7427 // varPhi = PHI(varLoop, varEnd)
7428 // srcPhi = PHI(srcLoop, src)
7429 // destPhi = PHI(destLoop, dst)
7430 MachineBasicBlock *entryBB = BB;
7431 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007432 unsigned varLoop = MRI.createVirtualRegister(TRC);
7433 unsigned varPhi = MRI.createVirtualRegister(TRC);
7434 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7435 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7436 unsigned destLoop = MRI.createVirtualRegister(TRC);
7437 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007438
7439 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7440 .addReg(varLoop).addMBB(loopMBB)
7441 .addReg(varEnd).addMBB(entryBB);
7442 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7443 .addReg(srcLoop).addMBB(loopMBB)
7444 .addReg(src).addMBB(entryBB);
7445 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7446 .addReg(destLoop).addMBB(loopMBB)
7447 .addReg(dest).addMBB(entryBB);
7448
7449 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7450 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007451 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007452 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7453 IsThumb1, IsThumb2);
7454 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7455 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007456
7457 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007458 if (IsThumb1) {
7459 MachineInstrBuilder MIB =
7460 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7461 MIB = AddDefaultT1CC(MIB);
7462 MIB.addReg(varPhi).addImm(UnitSize);
7463 AddDefaultPred(MIB);
7464 } else {
7465 MachineInstrBuilder MIB =
7466 BuildMI(*BB, BB->end(), dl,
7467 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7468 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7469 MIB->getOperand(5).setReg(ARM::CPSR);
7470 MIB->getOperand(5).setIsDef(true);
7471 }
7472 BuildMI(*BB, BB->end(), dl,
7473 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7474 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007475
7476 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7477 BB->addSuccessor(loopMBB);
7478 BB->addSuccessor(exitMBB);
7479
7480 // Add epilogue to handle BytesLeft.
7481 BB = exitMBB;
7482 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007483
7484 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7485 // [destOut] = STRB_POST(scratch, destLoop, 1)
7486 unsigned srcIn = srcLoop;
7487 unsigned destIn = destLoop;
7488 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007489 unsigned srcOut = MRI.createVirtualRegister(TRC);
7490 unsigned destOut = MRI.createVirtualRegister(TRC);
7491 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007492 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7493 IsThumb1, IsThumb2);
7494 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7495 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007496 srcIn = srcOut;
7497 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007498 }
7499
7500 MI->eraseFromParent(); // The instruction is gone now.
7501 return BB;
7502}
7503
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007504MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007505ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7506 MachineBasicBlock *MBB) const {
7507 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007508 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007509 DebugLoc DL = MI->getDebugLoc();
7510
7511 assert(Subtarget->isTargetWindows() &&
7512 "__chkstk is only supported on Windows");
7513 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7514
7515 // __chkstk takes the number of words to allocate on the stack in R4, and
7516 // returns the stack adjustment in number of bytes in R4. This will not
7517 // clober any other registers (other than the obvious lr).
7518 //
7519 // Although, technically, IP should be considered a register which may be
7520 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7521 // thumb-2 environment, so there is no interworking required. As a result, we
7522 // do not expect a veneer to be emitted by the linker, clobbering IP.
7523 //
Alp Toker1d099d92014-06-19 19:41:26 +00007524 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007525 // required, again, ensuring that IP is not clobbered.
7526 //
7527 // Finally, although some linkers may theoretically provide a trampoline for
7528 // out of range calls (which is quite common due to a 32M range limitation of
7529 // branches for Thumb), we can generate the long-call version via
7530 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7531 // IP.
7532
7533 switch (TM.getCodeModel()) {
7534 case CodeModel::Small:
7535 case CodeModel::Medium:
7536 case CodeModel::Default:
7537 case CodeModel::Kernel:
7538 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7539 .addImm((unsigned)ARMCC::AL).addReg(0)
7540 .addExternalSymbol("__chkstk")
7541 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7542 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7543 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7544 break;
7545 case CodeModel::Large:
7546 case CodeModel::JITDefault: {
7547 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7548 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7549
7550 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7551 .addExternalSymbol("__chkstk");
7552 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7553 .addImm((unsigned)ARMCC::AL).addReg(0)
7554 .addReg(Reg, RegState::Kill)
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 }
7560 }
7561
7562 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7563 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007564 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007565
7566 MI->eraseFromParent();
7567 return MBB;
7568}
7569
7570MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007571ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007572 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007573 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007574 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007575 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007576 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007577 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007578 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007579 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007580 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007581 // The Thumb2 pre-indexed stores have the same MI operands, they just
7582 // define them differently in the .td files from the isel patterns, so
7583 // they need pseudos.
7584 case ARM::t2STR_preidx:
7585 MI->setDesc(TII->get(ARM::t2STR_PRE));
7586 return BB;
7587 case ARM::t2STRB_preidx:
7588 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7589 return BB;
7590 case ARM::t2STRH_preidx:
7591 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7592 return BB;
7593
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007594 case ARM::STRi_preidx:
7595 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007596 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007597 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7598 // Decode the offset.
7599 unsigned Offset = MI->getOperand(4).getImm();
7600 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7601 Offset = ARM_AM::getAM2Offset(Offset);
7602 if (isSub)
7603 Offset = -Offset;
7604
Jim Grosbachf402f692011-08-12 21:02:34 +00007605 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007606 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007607 .addOperand(MI->getOperand(0)) // Rn_wb
7608 .addOperand(MI->getOperand(1)) // Rt
7609 .addOperand(MI->getOperand(2)) // Rn
7610 .addImm(Offset) // offset (skip GPR==zero_reg)
7611 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007612 .addOperand(MI->getOperand(6))
7613 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007614 MI->eraseFromParent();
7615 return BB;
7616 }
7617 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007618 case ARM::STRBr_preidx:
7619 case ARM::STRH_preidx: {
7620 unsigned NewOpc;
7621 switch (MI->getOpcode()) {
7622 default: llvm_unreachable("unexpected opcode!");
7623 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7624 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7625 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7626 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007627 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7628 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7629 MIB.addOperand(MI->getOperand(i));
7630 MI->eraseFromParent();
7631 return BB;
7632 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007633
Evan Chengbb2af352009-08-12 05:17:19 +00007634 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007635 // To "insert" a SELECT_CC instruction, we actually have to insert the
7636 // diamond control-flow pattern. The incoming instruction knows the
7637 // destination vreg to set, the condition code register to branch on, the
7638 // true/false values to select between, and a branch opcode to use.
7639 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007640 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007641 ++It;
7642
7643 // thisMBB:
7644 // ...
7645 // TrueVal = ...
7646 // cmpTY ccX, r1, r2
7647 // bCC copy1MBB
7648 // fallthrough --> copy0MBB
7649 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007650 MachineFunction *F = BB->getParent();
7651 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7652 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007653 F->insert(It, copy0MBB);
7654 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007655
7656 // Transfer the remainder of BB and its successor edges to sinkMBB.
7657 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007658 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007659 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7660
Dan Gohmanf4f04102010-07-06 15:49:48 +00007661 BB->addSuccessor(copy0MBB);
7662 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007663
Dan Gohman34396292010-07-06 20:24:04 +00007664 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7665 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7666
Evan Cheng10043e22007-01-19 07:51:42 +00007667 // copy0MBB:
7668 // %FalseValue = ...
7669 // # fallthrough to sinkMBB
7670 BB = copy0MBB;
7671
7672 // Update machine-CFG edges
7673 BB->addSuccessor(sinkMBB);
7674
7675 // sinkMBB:
7676 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7677 // ...
7678 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007679 BuildMI(*BB, BB->begin(), dl,
7680 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007681 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7682 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7683
Dan Gohman34396292010-07-06 20:24:04 +00007684 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007685 return BB;
7686 }
Evan Chengb972e562009-08-07 00:34:42 +00007687
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007688 case ARM::BCCi64:
7689 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007690 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007691 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007692
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007693 // Compare both parts that make up the double comparison separately for
7694 // equality.
7695 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7696
7697 unsigned LHS1 = MI->getOperand(1).getReg();
7698 unsigned LHS2 = MI->getOperand(2).getReg();
7699 if (RHSisZero) {
7700 AddDefaultPred(BuildMI(BB, dl,
7701 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7702 .addReg(LHS1).addImm(0));
7703 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7704 .addReg(LHS2).addImm(0)
7705 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7706 } else {
7707 unsigned RHS1 = MI->getOperand(3).getReg();
7708 unsigned RHS2 = MI->getOperand(4).getReg();
7709 AddDefaultPred(BuildMI(BB, dl,
7710 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7711 .addReg(LHS1).addReg(RHS1));
7712 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7713 .addReg(LHS2).addReg(RHS2)
7714 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7715 }
7716
7717 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7718 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7719 if (MI->getOperand(0).getImm() == ARMCC::NE)
7720 std::swap(destMBB, exitMBB);
7721
7722 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7723 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007724 if (isThumb2)
7725 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7726 else
7727 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007728
7729 MI->eraseFromParent(); // The pseudo instruction is gone now.
7730 return BB;
7731 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007732
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007733 case ARM::Int_eh_sjlj_setjmp:
7734 case ARM::Int_eh_sjlj_setjmp_nofp:
7735 case ARM::tInt_eh_sjlj_setjmp:
7736 case ARM::t2Int_eh_sjlj_setjmp:
7737 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7738 EmitSjLjDispatchBlock(MI, BB);
7739 return BB;
7740
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007741 case ARM::ABS:
7742 case ARM::t2ABS: {
7743 // To insert an ABS instruction, we have to insert the
7744 // diamond control-flow pattern. The incoming instruction knows the
7745 // source vreg to test against 0, the destination vreg to set,
7746 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007747 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007748 // It transforms
7749 // V1 = ABS V0
7750 // into
7751 // V2 = MOVS V0
7752 // BCC (branch to SinkBB if V0 >= 0)
7753 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007754 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007755 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7756 MachineFunction::iterator BBI = BB;
7757 ++BBI;
7758 MachineFunction *Fn = BB->getParent();
7759 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7760 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7761 Fn->insert(BBI, RSBBB);
7762 Fn->insert(BBI, SinkBB);
7763
7764 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7765 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00007766 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007767 bool isThumb2 = Subtarget->isThumb2();
7768 MachineRegisterInfo &MRI = Fn->getRegInfo();
7769 // In Thumb mode S must not be specified if source register is the SP or
7770 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007771 unsigned NewRsbDstReg =
7772 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007773
7774 // Transfer the remainder of BB and its successor edges to sinkMBB.
7775 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007776 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007777 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7778
7779 BB->addSuccessor(RSBBB);
7780 BB->addSuccessor(SinkBB);
7781
7782 // fall through to SinkMBB
7783 RSBBB->addSuccessor(SinkBB);
7784
Manman Rene0763c72012-06-15 21:32:12 +00007785 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007786 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007787 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7788 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007789
7790 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007791 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007792 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7793 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7794
7795 // insert rsbri in RSBBB
7796 // Note: BCC and rsbri will be converted into predicated rsbmi
7797 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007798 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007799 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00007800 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007801 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7802
Andrew Trick3f07c422011-10-18 18:40:53 +00007803 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007804 // reuse ABSDstReg to not change uses of ABS instruction
7805 BuildMI(*SinkBB, SinkBB->begin(), dl,
7806 TII->get(ARM::PHI), ABSDstReg)
7807 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007808 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007809
7810 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007811 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007812
7813 // return last added BB
7814 return SinkBB;
7815 }
Manman Rene8735522012-06-01 19:33:18 +00007816 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007817 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007818 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007819 case ARM::WIN__CHKSTK:
7820 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007821 }
7822}
7823
Evan Chenge6fba772011-08-30 19:09:48 +00007824void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7825 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007826 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007827 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7828 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7829 // operand is still set to noreg. If needed, set the optional operand's
7830 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007831 //
Andrew Trick88b24502011-10-18 19:18:52 +00007832 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007833
Andrew Trick924123a2011-09-21 02:20:46 +00007834 // Rename pseudo opcodes.
7835 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7836 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007837 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007838 MCID = &TII->get(NewOpc);
7839
7840 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7841 "converted opcode should be the same except for cc_out");
7842
7843 MI->setDesc(*MCID);
7844
7845 // Add the optional cc_out operand
7846 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007847 }
Andrew Trick88b24502011-10-18 19:18:52 +00007848 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007849
7850 // Any ARM instruction that sets the 's' bit should specify an optional
7851 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007852 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007853 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007854 return;
7855 }
Andrew Trick924123a2011-09-21 02:20:46 +00007856 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7857 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007858 bool definesCPSR = false;
7859 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007860 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007861 i != e; ++i) {
7862 const MachineOperand &MO = MI->getOperand(i);
7863 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7864 definesCPSR = true;
7865 if (MO.isDead())
7866 deadCPSR = true;
7867 MI->RemoveOperand(i);
7868 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007869 }
7870 }
Andrew Trick8586e622011-09-20 03:17:40 +00007871 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007872 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007873 return;
7874 }
7875 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007876 if (deadCPSR) {
7877 assert(!MI->getOperand(ccOutIdx).getReg() &&
7878 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007879 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007880 }
Andrew Trick8586e622011-09-20 03:17:40 +00007881
Andrew Trick924123a2011-09-21 02:20:46 +00007882 // If this instruction was defined with an optional CPSR def and its dag node
7883 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007884 MachineOperand &MO = MI->getOperand(ccOutIdx);
7885 MO.setReg(ARM::CPSR);
7886 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007887}
7888
Evan Cheng10043e22007-01-19 07:51:42 +00007889//===----------------------------------------------------------------------===//
7890// ARM Optimization Hooks
7891//===----------------------------------------------------------------------===//
7892
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007893// Helper function that checks if N is a null or all ones constant.
7894static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7895 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7896 if (!C)
7897 return false;
7898 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7899}
7900
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007901// Return true if N is conditionally 0 or all ones.
7902// Detects these expressions where cc is an i1 value:
7903//
7904// (select cc 0, y) [AllOnes=0]
7905// (select cc y, 0) [AllOnes=0]
7906// (zext cc) [AllOnes=0]
7907// (sext cc) [AllOnes=0/1]
7908// (select cc -1, y) [AllOnes=1]
7909// (select cc y, -1) [AllOnes=1]
7910//
7911// Invert is set when N is the null/all ones constant when CC is false.
7912// OtherOp is set to the alternative value of N.
7913static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7914 SDValue &CC, bool &Invert,
7915 SDValue &OtherOp,
7916 SelectionDAG &DAG) {
7917 switch (N->getOpcode()) {
7918 default: return false;
7919 case ISD::SELECT: {
7920 CC = N->getOperand(0);
7921 SDValue N1 = N->getOperand(1);
7922 SDValue N2 = N->getOperand(2);
7923 if (isZeroOrAllOnes(N1, AllOnes)) {
7924 Invert = false;
7925 OtherOp = N2;
7926 return true;
7927 }
7928 if (isZeroOrAllOnes(N2, AllOnes)) {
7929 Invert = true;
7930 OtherOp = N1;
7931 return true;
7932 }
7933 return false;
7934 }
7935 case ISD::ZERO_EXTEND:
7936 // (zext cc) can never be the all ones value.
7937 if (AllOnes)
7938 return false;
7939 // Fall through.
7940 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007941 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007942 EVT VT = N->getValueType(0);
7943 CC = N->getOperand(0);
7944 if (CC.getValueType() != MVT::i1)
7945 return false;
7946 Invert = !AllOnes;
7947 if (AllOnes)
7948 // When looking for an AllOnes constant, N is an sext, and the 'other'
7949 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007950 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007951 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7952 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007953 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007954 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007955 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
7956 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007957 return true;
7958 }
7959 }
7960}
7961
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007962// Combine a constant select operand into its use:
7963//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007964// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7965// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7966// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7967// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7968// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007969//
7970// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007971// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007972//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007973// Also recognize sext/zext from i1:
7974//
7975// (add (zext cc), x) -> (select cc (add x, 1), x)
7976// (add (sext cc), x) -> (select cc (add x, -1), x)
7977//
7978// These transformations eventually create predicated instructions.
7979//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007980// @param N The node to transform.
7981// @param Slct The N operand that is a select.
7982// @param OtherOp The other N operand (x above).
7983// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007984// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007985// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007986static
7987SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007988 TargetLowering::DAGCombinerInfo &DCI,
7989 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007990 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007991 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007992 SDValue NonConstantVal;
7993 SDValue CCOp;
7994 bool SwapSelectOps;
7995 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7996 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007997 return SDValue();
7998
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007999 // Slct is now know to be the desired identity constant when CC is true.
8000 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008001 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008002 OtherOp, NonConstantVal);
8003 // Unless SwapSelectOps says CC should be false.
8004 if (SwapSelectOps)
8005 std::swap(TrueVal, FalseVal);
8006
Andrew Trickef9de2a2013-05-25 02:42:55 +00008007 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008008 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00008009}
8010
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008011// Attempt combineSelectAndUse on each operand of a commutative operator N.
8012static
8013SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
8014 TargetLowering::DAGCombinerInfo &DCI) {
8015 SDValue N0 = N->getOperand(0);
8016 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008017 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008018 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
8019 if (Result.getNode())
8020 return Result;
8021 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008022 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008023 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
8024 if (Result.getNode())
8025 return Result;
8026 }
8027 return SDValue();
8028}
8029
Eric Christopher1b8b94192011-06-29 21:10:36 +00008030// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00008031// (only after legalization).
8032static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
8033 TargetLowering::DAGCombinerInfo &DCI,
8034 const ARMSubtarget *Subtarget) {
8035
8036 // Only perform optimization if after legalize, and if NEON is available. We
8037 // also expected both operands to be BUILD_VECTORs.
8038 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
8039 || N0.getOpcode() != ISD::BUILD_VECTOR
8040 || N1.getOpcode() != ISD::BUILD_VECTOR)
8041 return SDValue();
8042
8043 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
8044 EVT VT = N->getValueType(0);
8045 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
8046 return SDValue();
8047
8048 // Check that the vector operands are of the right form.
8049 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
8050 // operands, where N is the size of the formed vector.
8051 // Each EXTRACT_VECTOR should have the same input vector and odd or even
8052 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008053
8054 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00008055 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00008056 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00008057 SDValue Vec = N0->getOperand(0)->getOperand(0);
8058 SDNode *V = Vec.getNode();
8059 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00008060
Eric Christopher1b8b94192011-06-29 21:10:36 +00008061 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008062 // check to see if each of their operands are an EXTRACT_VECTOR with
8063 // the same vector and appropriate index.
8064 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
8065 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
8066 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00008067
Tanya Lattnere9e67052011-06-14 23:48:48 +00008068 SDValue ExtVec0 = N0->getOperand(i);
8069 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008070
Tanya Lattnere9e67052011-06-14 23:48:48 +00008071 // First operand is the vector, verify its the same.
8072 if (V != ExtVec0->getOperand(0).getNode() ||
8073 V != ExtVec1->getOperand(0).getNode())
8074 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00008075
Tanya Lattnere9e67052011-06-14 23:48:48 +00008076 // Second is the constant, verify its correct.
8077 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
8078 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00008079
Tanya Lattnere9e67052011-06-14 23:48:48 +00008080 // For the constant, we want to see all the even or all the odd.
8081 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
8082 || C1->getZExtValue() != nextIndex+1)
8083 return SDValue();
8084
8085 // Increment index.
8086 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008087 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00008088 return SDValue();
8089 }
8090
8091 // Create VPADDL node.
8092 SelectionDAG &DAG = DCI.DAG;
8093 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00008094
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008095 SDLoc dl(N);
8096
Tanya Lattnere9e67052011-06-14 23:48:48 +00008097 // Build operand list.
8098 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008099 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00008100 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00008101
8102 // Input is the vector.
8103 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00008104
Tanya Lattnere9e67052011-06-14 23:48:48 +00008105 // Get widened type and narrowed type.
8106 MVT widenType;
8107 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00008108
Silviu Barangaa3106e62014-04-03 10:44:27 +00008109 EVT inputLaneType = Vec.getValueType().getVectorElementType();
8110 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00008111 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
8112 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
8113 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
8114 default:
Craig Toppere55c5562012-02-07 02:50:20 +00008115 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00008116 }
8117
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008118 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00008119 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008120 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00008121}
8122
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008123static SDValue findMUL_LOHI(SDValue V) {
8124 if (V->getOpcode() == ISD::UMUL_LOHI ||
8125 V->getOpcode() == ISD::SMUL_LOHI)
8126 return V;
8127 return SDValue();
8128}
8129
8130static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
8131 TargetLowering::DAGCombinerInfo &DCI,
8132 const ARMSubtarget *Subtarget) {
8133
8134 if (Subtarget->isThumb1Only()) return SDValue();
8135
8136 // Only perform the checks after legalize when the pattern is available.
8137 if (DCI.isBeforeLegalize()) return SDValue();
8138
8139 // Look for multiply add opportunities.
8140 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8141 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8142 // a glue link from the first add to the second add.
8143 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8144 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008145 // UMUL_LOHI
8146 // / :lo \ :hi
8147 // / \ [no multiline comment]
8148 // loAdd -> ADDE |
8149 // \ :glue /
8150 // \ /
8151 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008152 //
8153 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8154 SDValue AddcOp0 = AddcNode->getOperand(0);
8155 SDValue AddcOp1 = AddcNode->getOperand(1);
8156
8157 // Check if the two operands are from the same mul_lohi node.
8158 if (AddcOp0.getNode() == AddcOp1.getNode())
8159 return SDValue();
8160
8161 assert(AddcNode->getNumValues() == 2 &&
8162 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008163 "Expect ADDC with two result values. First: i32");
8164
8165 // Check that we have a glued ADDC node.
8166 if (AddcNode->getValueType(1) != MVT::Glue)
8167 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008168
8169 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8170 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8171 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8172 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8173 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8174 return SDValue();
8175
8176 // Look for the glued ADDE.
8177 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008178 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008179 return SDValue();
8180
8181 // Make sure it is really an ADDE.
8182 if (AddeNode->getOpcode() != ISD::ADDE)
8183 return SDValue();
8184
8185 assert(AddeNode->getNumOperands() == 3 &&
8186 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8187 "ADDE node has the wrong inputs");
8188
8189 // Check for the triangle shape.
8190 SDValue AddeOp0 = AddeNode->getOperand(0);
8191 SDValue AddeOp1 = AddeNode->getOperand(1);
8192
8193 // Make sure that the ADDE operands are not coming from the same node.
8194 if (AddeOp0.getNode() == AddeOp1.getNode())
8195 return SDValue();
8196
8197 // Find the MUL_LOHI node walking up ADDE's operands.
8198 bool IsLeftOperandMUL = false;
8199 SDValue MULOp = findMUL_LOHI(AddeOp0);
8200 if (MULOp == SDValue())
8201 MULOp = findMUL_LOHI(AddeOp1);
8202 else
8203 IsLeftOperandMUL = true;
8204 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008205 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008206
8207 // Figure out the right opcode.
8208 unsigned Opc = MULOp->getOpcode();
8209 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8210
8211 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008212 SDValue* HiAdd = nullptr;
8213 SDValue* LoMul = nullptr;
8214 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008215
Jyoti Allurf1d70502015-01-23 09:10:03 +00008216 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8217 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8218 return SDValue();
8219
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008220 if (IsLeftOperandMUL)
8221 HiAdd = &AddeOp1;
8222 else
8223 HiAdd = &AddeOp0;
8224
8225
Jyoti Allurf1d70502015-01-23 09:10:03 +00008226 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8227 // whose low result is fed to the ADDC we are checking.
8228
8229 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008230 LoMul = &AddcOp0;
8231 LowAdd = &AddcOp1;
8232 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008233 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008234 LoMul = &AddcOp1;
8235 LowAdd = &AddcOp0;
8236 }
8237
Craig Topper062a2ba2014-04-25 05:30:21 +00008238 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008239 return SDValue();
8240
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008241 // Create the merged node.
8242 SelectionDAG &DAG = DCI.DAG;
8243
8244 // Build operand list.
8245 SmallVector<SDValue, 8> Ops;
8246 Ops.push_back(LoMul->getOperand(0));
8247 Ops.push_back(LoMul->getOperand(1));
8248 Ops.push_back(*LowAdd);
8249 Ops.push_back(*HiAdd);
8250
Andrew Trickef9de2a2013-05-25 02:42:55 +00008251 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008252 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008253
8254 // Replace the ADDs' nodes uses by the MLA node's values.
8255 SDValue HiMLALResult(MLALNode.getNode(), 1);
8256 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8257
8258 SDValue LoMLALResult(MLALNode.getNode(), 0);
8259 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8260
8261 // Return original node to notify the driver to stop replacing.
8262 SDValue resNode(AddcNode, 0);
8263 return resNode;
8264}
8265
8266/// PerformADDCCombine - Target-specific dag combine transform from
8267/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8268static SDValue PerformADDCCombine(SDNode *N,
8269 TargetLowering::DAGCombinerInfo &DCI,
8270 const ARMSubtarget *Subtarget) {
8271
8272 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8273
8274}
8275
Bob Wilson728eb292010-07-29 20:34:14 +00008276/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8277/// operands N0 and N1. This is a helper for PerformADDCombine that is
8278/// called with the default operands, and if that fails, with commuted
8279/// operands.
8280static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008281 TargetLowering::DAGCombinerInfo &DCI,
8282 const ARMSubtarget *Subtarget){
8283
8284 // Attempt to create vpaddl for this add.
8285 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8286 if (Result.getNode())
8287 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008288
Chris Lattner4147f082009-03-12 06:52:53 +00008289 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008290 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008291 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8292 if (Result.getNode()) return Result;
8293 }
Chris Lattner4147f082009-03-12 06:52:53 +00008294 return SDValue();
8295}
8296
Bob Wilson728eb292010-07-29 20:34:14 +00008297/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8298///
8299static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008300 TargetLowering::DAGCombinerInfo &DCI,
8301 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008302 SDValue N0 = N->getOperand(0);
8303 SDValue N1 = N->getOperand(1);
8304
8305 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008306 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008307 if (Result.getNode())
8308 return Result;
8309
8310 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008311 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008312}
8313
Chris Lattner4147f082009-03-12 06:52:53 +00008314/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008315///
Chris Lattner4147f082009-03-12 06:52:53 +00008316static SDValue PerformSUBCombine(SDNode *N,
8317 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008318 SDValue N0 = N->getOperand(0);
8319 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008320
Chris Lattner4147f082009-03-12 06:52:53 +00008321 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008322 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008323 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8324 if (Result.getNode()) return Result;
8325 }
Bob Wilson7117a912009-03-20 22:42:55 +00008326
Chris Lattner4147f082009-03-12 06:52:53 +00008327 return SDValue();
8328}
8329
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008330/// PerformVMULCombine
8331/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8332/// special multiplier accumulator forwarding.
8333/// vmul d3, d0, d2
8334/// vmla d3, d1, d2
8335/// is faster than
8336/// vadd d3, d0, d1
8337/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008338// However, for (A + B) * (A + B),
8339// vadd d2, d0, d1
8340// vmul d3, d0, d2
8341// vmla d3, d1, d2
8342// is slower than
8343// vadd d2, d0, d1
8344// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008345static SDValue PerformVMULCombine(SDNode *N,
8346 TargetLowering::DAGCombinerInfo &DCI,
8347 const ARMSubtarget *Subtarget) {
8348 if (!Subtarget->hasVMLxForwarding())
8349 return SDValue();
8350
8351 SelectionDAG &DAG = DCI.DAG;
8352 SDValue N0 = N->getOperand(0);
8353 SDValue N1 = N->getOperand(1);
8354 unsigned Opcode = N0.getOpcode();
8355 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8356 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008357 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008358 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8359 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8360 return SDValue();
8361 std::swap(N0, N1);
8362 }
8363
Weiming Zhao2052f482013-09-25 23:12:06 +00008364 if (N0 == N1)
8365 return SDValue();
8366
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008367 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008368 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008369 SDValue N00 = N0->getOperand(0);
8370 SDValue N01 = N0->getOperand(1);
8371 return DAG.getNode(Opcode, DL, VT,
8372 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8373 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8374}
8375
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008376static SDValue PerformMULCombine(SDNode *N,
8377 TargetLowering::DAGCombinerInfo &DCI,
8378 const ARMSubtarget *Subtarget) {
8379 SelectionDAG &DAG = DCI.DAG;
8380
8381 if (Subtarget->isThumb1Only())
8382 return SDValue();
8383
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008384 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8385 return SDValue();
8386
8387 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008388 if (VT.is64BitVector() || VT.is128BitVector())
8389 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008390 if (VT != MVT::i32)
8391 return SDValue();
8392
8393 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8394 if (!C)
8395 return SDValue();
8396
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008397 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008398 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008399
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008400 ShiftAmt = ShiftAmt & (32 - 1);
8401 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008402 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008403
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008404 SDValue Res;
8405 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008406
8407 if (MulAmt >= 0) {
8408 if (isPowerOf2_32(MulAmt - 1)) {
8409 // (mul x, 2^N + 1) => (add (shl x, N), x)
8410 Res = DAG.getNode(ISD::ADD, DL, VT,
8411 V,
8412 DAG.getNode(ISD::SHL, DL, VT,
8413 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008414 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008415 MVT::i32)));
8416 } else if (isPowerOf2_32(MulAmt + 1)) {
8417 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8418 Res = DAG.getNode(ISD::SUB, DL, VT,
8419 DAG.getNode(ISD::SHL, DL, VT,
8420 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008421 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008422 MVT::i32)),
8423 V);
8424 } else
8425 return SDValue();
8426 } else {
8427 uint64_t MulAmtAbs = -MulAmt;
8428 if (isPowerOf2_32(MulAmtAbs + 1)) {
8429 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8430 Res = DAG.getNode(ISD::SUB, DL, VT,
8431 V,
8432 DAG.getNode(ISD::SHL, DL, VT,
8433 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008434 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008435 MVT::i32)));
8436 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8437 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8438 Res = DAG.getNode(ISD::ADD, DL, VT,
8439 V,
8440 DAG.getNode(ISD::SHL, DL, VT,
8441 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008442 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008443 MVT::i32)));
8444 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008445 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008446
8447 } else
8448 return SDValue();
8449 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008450
8451 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008452 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008453 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008454
8455 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008456 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008457 return SDValue();
8458}
8459
Owen Anderson30c48922010-11-05 19:27:46 +00008460static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008461 TargetLowering::DAGCombinerInfo &DCI,
8462 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008463
Owen Anderson30c48922010-11-05 19:27:46 +00008464 // Attempt to use immediate-form VBIC
8465 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008466 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008467 EVT VT = N->getValueType(0);
8468 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008469
Tanya Lattner266792a2011-04-07 15:24:20 +00008470 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8471 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008472
Owen Anderson30c48922010-11-05 19:27:46 +00008473 APInt SplatBits, SplatUndef;
8474 unsigned SplatBitSize;
8475 bool HasAnyUndefs;
8476 if (BVN &&
8477 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8478 if (SplatBitSize <= 64) {
8479 EVT VbicVT;
8480 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8481 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008482 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008483 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008484 if (Val.getNode()) {
8485 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008486 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008487 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008488 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008489 }
8490 }
8491 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008492
Evan Chenge87681c2012-02-23 01:19:06 +00008493 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008494 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8495 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8496 if (Result.getNode())
8497 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008498 }
8499
Owen Anderson30c48922010-11-05 19:27:46 +00008500 return SDValue();
8501}
8502
Jim Grosbach11013ed2010-07-16 23:05:05 +00008503/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8504static SDValue PerformORCombine(SDNode *N,
8505 TargetLowering::DAGCombinerInfo &DCI,
8506 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008507 // Attempt to use immediate-form VORR
8508 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008509 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008510 EVT VT = N->getValueType(0);
8511 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008512
Tanya Lattner266792a2011-04-07 15:24:20 +00008513 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8514 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008515
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008516 APInt SplatBits, SplatUndef;
8517 unsigned SplatBitSize;
8518 bool HasAnyUndefs;
8519 if (BVN && Subtarget->hasNEON() &&
8520 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8521 if (SplatBitSize <= 64) {
8522 EVT VorrVT;
8523 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8524 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008525 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008526 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008527 if (Val.getNode()) {
8528 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008529 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008530 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008531 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008532 }
8533 }
8534 }
8535
Evan Chenge87681c2012-02-23 01:19:06 +00008536 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008537 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8538 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8539 if (Result.getNode())
8540 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008541 }
8542
Nadav Rotem3a94c542012-08-13 18:52:44 +00008543 // The code below optimizes (or (and X, Y), Z).
8544 // The AND operand needs to have a single user to make these optimizations
8545 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008546 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008547 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008548 return SDValue();
8549 SDValue N1 = N->getOperand(1);
8550
8551 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8552 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8553 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8554 APInt SplatUndef;
8555 unsigned SplatBitSize;
8556 bool HasAnyUndefs;
8557
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008558 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008559 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008560 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8561 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008562 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008563 HasAnyUndefs) && !HasAnyUndefs) {
8564 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8565 HasAnyUndefs) && !HasAnyUndefs) {
8566 // Ensure that the bit width of the constants are the same and that
8567 // the splat arguments are logical inverses as per the pattern we
8568 // are trying to simplify.
8569 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8570 SplatBits0 == ~SplatBits1) {
8571 // Canonicalize the vector type to make instruction selection
8572 // simpler.
8573 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8574 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8575 N0->getOperand(1),
8576 N0->getOperand(0),
8577 N1->getOperand(0));
8578 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8579 }
8580 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008581 }
8582 }
8583
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008584 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8585 // reasonable.
8586
Jim Grosbach11013ed2010-07-16 23:05:05 +00008587 // BFI is only available on V6T2+
8588 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8589 return SDValue();
8590
Andrew Trickef9de2a2013-05-25 02:42:55 +00008591 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008592 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008593 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008594 //
8595 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008596 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008597 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008598 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008599 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008600 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008601
Jim Grosbach11013ed2010-07-16 23:05:05 +00008602 if (VT != MVT::i32)
8603 return SDValue();
8604
Evan Cheng2e51bb42010-12-13 20:32:54 +00008605 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008606
Jim Grosbach11013ed2010-07-16 23:05:05 +00008607 // The value and the mask need to be constants so we can verify this is
8608 // actually a bitfield set. If the mask is 0xffff, we can do better
8609 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008610 SDValue MaskOp = N0.getOperand(1);
8611 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8612 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008613 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008614 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008615 if (Mask == 0xffff)
8616 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008617 SDValue Res;
8618 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008619 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8620 if (N1C) {
8621 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008622 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008623 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008624
Evan Cheng34345752010-12-11 04:11:38 +00008625 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008626 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008627
Evan Cheng2e51bb42010-12-13 20:32:54 +00008628 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008629 DAG.getConstant(Val, DL, MVT::i32),
8630 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008631
8632 // Do not add new nodes to DAG combiner worklist.
8633 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008634 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008635 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008636 } else if (N1.getOpcode() == ISD::AND) {
8637 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008638 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8639 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008640 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008641 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008642
Eric Christopherd5530962011-03-26 01:21:03 +00008643 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8644 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008645 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008646 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008647 // The pack halfword instruction works better for masks that fit it,
8648 // so use that when it's available.
8649 if (Subtarget->hasT2ExtractPack() &&
8650 (Mask == 0xffff || Mask == 0xffff0000))
8651 return SDValue();
8652 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008653 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008654 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008655 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008656 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008657 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008658 // Do not add new nodes to DAG combiner worklist.
8659 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008660 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008661 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008662 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008663 // The pack halfword instruction works better for masks that fit it,
8664 // so use that when it's available.
8665 if (Subtarget->hasT2ExtractPack() &&
8666 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8667 return SDValue();
8668 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008669 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008670 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008671 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008672 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008673 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008674 // Do not add new nodes to DAG combiner worklist.
8675 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008676 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008677 }
8678 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008679
Evan Cheng2e51bb42010-12-13 20:32:54 +00008680 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8681 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8682 ARM::isBitFieldInvertedMask(~Mask)) {
8683 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8684 // where lsb(mask) == #shamt and masked bits of B are known zero.
8685 SDValue ShAmt = N00.getOperand(1);
8686 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008687 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008688 if (ShAmtC != LSB)
8689 return SDValue();
8690
8691 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008692 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008693
8694 // Do not add new nodes to DAG combiner worklist.
8695 DCI.CombineTo(N, Res, false);
8696 }
8697
Jim Grosbach11013ed2010-07-16 23:05:05 +00008698 return SDValue();
8699}
8700
Evan Chenge87681c2012-02-23 01:19:06 +00008701static SDValue PerformXORCombine(SDNode *N,
8702 TargetLowering::DAGCombinerInfo &DCI,
8703 const ARMSubtarget *Subtarget) {
8704 EVT VT = N->getValueType(0);
8705 SelectionDAG &DAG = DCI.DAG;
8706
8707 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8708 return SDValue();
8709
8710 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008711 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8712 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8713 if (Result.getNode())
8714 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008715 }
8716
8717 return SDValue();
8718}
8719
Evan Cheng6d02d902011-06-15 01:12:31 +00008720/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8721/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008722static SDValue PerformBFICombine(SDNode *N,
8723 TargetLowering::DAGCombinerInfo &DCI) {
8724 SDValue N1 = N->getOperand(1);
8725 if (N1.getOpcode() == ISD::AND) {
8726 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8727 if (!N11C)
8728 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008729 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008730 unsigned LSB = countTrailingZeros(~InvMask);
8731 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008732 assert(Width <
8733 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008734 "undefined behavior");
8735 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008736 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008737 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008738 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008739 N->getOperand(0), N1.getOperand(0),
8740 N->getOperand(2));
8741 }
8742 return SDValue();
8743}
8744
Bob Wilson22806742010-09-22 22:09:21 +00008745/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8746/// ARMISD::VMOVRRD.
8747static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008748 TargetLowering::DAGCombinerInfo &DCI,
8749 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008750 // vmovrrd(vmovdrr x, y) -> x,y
8751 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008752 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008753 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008754
8755 // vmovrrd(load f64) -> (load i32), (load i32)
8756 SDNode *InNode = InDouble.getNode();
8757 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8758 InNode->getValueType(0) == MVT::f64 &&
8759 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8760 !cast<LoadSDNode>(InNode)->isVolatile()) {
8761 // TODO: Should this be done for non-FrameIndex operands?
8762 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8763
8764 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008765 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008766 SDValue BasePtr = LD->getBasePtr();
8767 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8768 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008769 LD->isNonTemporal(), LD->isInvariant(),
8770 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008771
8772 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008773 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008774 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8775 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008776 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008777 std::min(4U, LD->getAlignment() / 2));
8778
8779 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +00008780 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +00008781 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008782 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008783 return Result;
8784 }
8785
Bob Wilson22806742010-09-22 22:09:21 +00008786 return SDValue();
8787}
8788
8789/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8790/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8791static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8792 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8793 SDValue Op0 = N->getOperand(0);
8794 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008795 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008796 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008797 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008798 Op1 = Op1.getOperand(0);
8799 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8800 Op0.getNode() == Op1.getNode() &&
8801 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008802 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008803 N->getValueType(0), Op0.getOperand(0));
8804 return SDValue();
8805}
8806
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008807/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8808/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8809/// i64 vector to have f64 elements, since the value can then be loaded
8810/// directly into a VFP register.
8811static bool hasNormalLoadOperand(SDNode *N) {
8812 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8813 for (unsigned i = 0; i < NumElts; ++i) {
8814 SDNode *Elt = N->getOperand(i).getNode();
8815 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8816 return true;
8817 }
8818 return false;
8819}
8820
Bob Wilsoncb6db982010-09-17 22:59:05 +00008821/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8822/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008823static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008824 TargetLowering::DAGCombinerInfo &DCI,
8825 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008826 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8827 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8828 // into a pair of GPRs, which is fine when the value is used as a scalar,
8829 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008830 SelectionDAG &DAG = DCI.DAG;
8831 if (N->getNumOperands() == 2) {
8832 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8833 if (RV.getNode())
8834 return RV;
8835 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008836
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008837 // Load i64 elements as f64 values so that type legalization does not split
8838 // them up into i32 values.
8839 EVT VT = N->getValueType(0);
8840 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8841 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008842 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008843 SmallVector<SDValue, 8> Ops;
8844 unsigned NumElts = VT.getVectorNumElements();
8845 for (unsigned i = 0; i < NumElts; ++i) {
8846 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8847 Ops.push_back(V);
8848 // Make the DAGCombiner fold the bitcast.
8849 DCI.AddToWorklist(V.getNode());
8850 }
8851 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008852 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008853 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8854}
8855
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008856/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8857static SDValue
8858PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8859 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8860 // At that time, we may have inserted bitcasts from integer to float.
8861 // If these bitcasts have survived DAGCombine, change the lowering of this
8862 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8863 // force to use floating point types.
8864
8865 // Make sure we can change the type of the vector.
8866 // This is possible iff:
8867 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8868 // 1.1. Vector is used only once.
8869 // 1.2. Use is a bit convert to an integer type.
8870 // 2. The size of its operands are 32-bits (64-bits are not legal).
8871 EVT VT = N->getValueType(0);
8872 EVT EltVT = VT.getVectorElementType();
8873
8874 // Check 1.1. and 2.
8875 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8876 return SDValue();
8877
8878 // By construction, the input type must be float.
8879 assert(EltVT == MVT::f32 && "Unexpected type!");
8880
8881 // Check 1.2.
8882 SDNode *Use = *N->use_begin();
8883 if (Use->getOpcode() != ISD::BITCAST ||
8884 Use->getValueType(0).isFloatingPoint())
8885 return SDValue();
8886
8887 // Check profitability.
8888 // Model is, if more than half of the relevant operands are bitcast from
8889 // i32, turn the build_vector into a sequence of insert_vector_elt.
8890 // Relevant operands are everything that is not statically
8891 // (i.e., at compile time) bitcasted.
8892 unsigned NumOfBitCastedElts = 0;
8893 unsigned NumElts = VT.getVectorNumElements();
8894 unsigned NumOfRelevantElts = NumElts;
8895 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8896 SDValue Elt = N->getOperand(Idx);
8897 if (Elt->getOpcode() == ISD::BITCAST) {
8898 // Assume only bit cast to i32 will go away.
8899 if (Elt->getOperand(0).getValueType() == MVT::i32)
8900 ++NumOfBitCastedElts;
8901 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8902 // Constants are statically casted, thus do not count them as
8903 // relevant operands.
8904 --NumOfRelevantElts;
8905 }
8906
8907 // Check if more than half of the elements require a non-free bitcast.
8908 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8909 return SDValue();
8910
8911 SelectionDAG &DAG = DCI.DAG;
8912 // Create the new vector type.
8913 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8914 // Check if the type is legal.
8915 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8916 if (!TLI.isTypeLegal(VecVT))
8917 return SDValue();
8918
8919 // Combine:
8920 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8921 // => BITCAST INSERT_VECTOR_ELT
8922 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8923 // (BITCAST EN), N.
8924 SDValue Vec = DAG.getUNDEF(VecVT);
8925 SDLoc dl(N);
8926 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8927 SDValue V = N->getOperand(Idx);
8928 if (V.getOpcode() == ISD::UNDEF)
8929 continue;
8930 if (V.getOpcode() == ISD::BITCAST &&
8931 V->getOperand(0).getValueType() == MVT::i32)
8932 // Fold obvious case.
8933 V = V.getOperand(0);
8934 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008935 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008936 // Make the DAGCombiner fold the bitcasts.
8937 DCI.AddToWorklist(V.getNode());
8938 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008939 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008940 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8941 }
8942 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8943 // Make the DAGCombiner fold the bitcasts.
8944 DCI.AddToWorklist(Vec.getNode());
8945 return Vec;
8946}
8947
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008948/// PerformInsertEltCombine - Target-specific dag combine xforms for
8949/// ISD::INSERT_VECTOR_ELT.
8950static SDValue PerformInsertEltCombine(SDNode *N,
8951 TargetLowering::DAGCombinerInfo &DCI) {
8952 // Bitcast an i64 load inserted into a vector to f64.
8953 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8954 EVT VT = N->getValueType(0);
8955 SDNode *Elt = N->getOperand(1).getNode();
8956 if (VT.getVectorElementType() != MVT::i64 ||
8957 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8958 return SDValue();
8959
8960 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008961 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008962 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8963 VT.getVectorNumElements());
8964 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8965 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8966 // Make the DAGCombiner fold the bitcasts.
8967 DCI.AddToWorklist(Vec.getNode());
8968 DCI.AddToWorklist(V.getNode());
8969 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8970 Vec, V, N->getOperand(2));
8971 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008972}
8973
Bob Wilsonc7334a12010-10-27 20:38:28 +00008974/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8975/// ISD::VECTOR_SHUFFLE.
8976static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8977 // The LLVM shufflevector instruction does not require the shuffle mask
8978 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8979 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8980 // operands do not match the mask length, they are extended by concatenating
8981 // them with undef vectors. That is probably the right thing for other
8982 // targets, but for NEON it is better to concatenate two double-register
8983 // size vector operands into a single quad-register size vector. Do that
8984 // transformation here:
8985 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8986 // shuffle(concat(v1, v2), undef)
8987 SDValue Op0 = N->getOperand(0);
8988 SDValue Op1 = N->getOperand(1);
8989 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8990 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8991 Op0.getNumOperands() != 2 ||
8992 Op1.getNumOperands() != 2)
8993 return SDValue();
8994 SDValue Concat0Op1 = Op0.getOperand(1);
8995 SDValue Concat1Op1 = Op1.getOperand(1);
8996 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8997 Concat1Op1.getOpcode() != ISD::UNDEF)
8998 return SDValue();
8999 // Skip the transformation if any of the types are illegal.
9000 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9001 EVT VT = N->getValueType(0);
9002 if (!TLI.isTypeLegal(VT) ||
9003 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
9004 !TLI.isTypeLegal(Concat1Op1.getValueType()))
9005 return SDValue();
9006
Andrew Trickef9de2a2013-05-25 02:42:55 +00009007 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009008 Op0.getOperand(0), Op1.getOperand(0));
9009 // Translate the shuffle mask.
9010 SmallVector<int, 16> NewMask;
9011 unsigned NumElts = VT.getVectorNumElements();
9012 unsigned HalfElts = NumElts/2;
9013 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9014 for (unsigned n = 0; n < NumElts; ++n) {
9015 int MaskElt = SVN->getMaskElt(n);
9016 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00009017 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00009018 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00009019 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00009020 NewElt = HalfElts + MaskElt - NumElts;
9021 NewMask.push_back(NewElt);
9022 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009023 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00009024 DAG.getUNDEF(VT), NewMask.data());
9025}
9026
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009027/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
9028/// NEON load/store intrinsics, and generic vector load/stores, to merge
9029/// base address updates.
9030/// For generic load/stores, the memory type is assumed to be a vector.
9031/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00009032static SDValue CombineBaseUpdate(SDNode *N,
9033 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00009034 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009035 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
9036 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009037 const bool isStore = N->getOpcode() == ISD::STORE;
9038 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00009039 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009040 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009041 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00009042
9043 // Search for a use of the address operand that is an increment.
9044 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
9045 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
9046 SDNode *User = *UI;
9047 if (User->getOpcode() != ISD::ADD ||
9048 UI.getUse().getResNo() != Addr.getResNo())
9049 continue;
9050
9051 // Check that the add is independent of the load/store. Otherwise, folding
9052 // it would create a cycle.
9053 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
9054 continue;
9055
9056 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009057 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00009058 bool isLaneOp = false;
9059 unsigned NewOpc = 0;
9060 unsigned NumVecs = 0;
9061 if (isIntrinsic) {
9062 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9063 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00009064 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009065 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
9066 NumVecs = 1; break;
9067 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
9068 NumVecs = 2; break;
9069 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
9070 NumVecs = 3; break;
9071 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
9072 NumVecs = 4; break;
9073 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
9074 NumVecs = 2; isLaneOp = true; break;
9075 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
9076 NumVecs = 3; isLaneOp = true; break;
9077 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
9078 NumVecs = 4; isLaneOp = true; break;
9079 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009080 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009081 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009082 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009083 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009084 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009085 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009086 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009087 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009088 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009089 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009090 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009091 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009092 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009093 }
9094 } else {
9095 isLaneOp = true;
9096 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00009097 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00009098 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
9099 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
9100 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009101 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
9102 NumVecs = 1; isLaneOp = false; break;
9103 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
9104 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00009105 }
9106 }
9107
9108 // Find the size of memory referenced by the load/store.
9109 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009110 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00009111 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009112 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00009113 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009114 } else {
9115 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
9116 VecTy = N->getOperand(1).getValueType();
9117 }
9118
Bob Wilson06fce872011-02-07 17:43:21 +00009119 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
9120 if (isLaneOp)
9121 NumBytes /= VecTy.getVectorNumElements();
9122
9123 // If the increment is a constant, it must match the memory ref size.
9124 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
9125 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
9126 uint64_t IncVal = CInc->getZExtValue();
9127 if (IncVal != NumBytes)
9128 continue;
9129 } else if (NumBytes >= 3 * 16) {
9130 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
9131 // separate instructions that make it harder to use a non-constant update.
9132 continue;
9133 }
9134
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009135 // OK, we found an ADD we can fold into the base update.
9136 // Now, create a _UPD node, taking care of not breaking alignment.
9137
9138 EVT AlignedVecTy = VecTy;
9139 unsigned Alignment = MemN->getAlignment();
9140
9141 // If this is a less-than-standard-aligned load/store, change the type to
9142 // match the standard alignment.
9143 // The alignment is overlooked when selecting _UPD variants; and it's
9144 // easier to introduce bitcasts here than fix that.
9145 // There are 3 ways to get to this base-update combine:
9146 // - intrinsics: they are assumed to be properly aligned (to the standard
9147 // alignment of the memory type), so we don't need to do anything.
9148 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9149 // intrinsics, so, likewise, there's nothing to do.
9150 // - generic load/store instructions: the alignment is specified as an
9151 // explicit operand, rather than implicitly as the standard alignment
9152 // of the memory type (like the intrisics). We need to change the
9153 // memory type to match the explicit alignment. That way, we don't
9154 // generate non-standard-aligned ARMISD::VLDx nodes.
9155 if (isa<LSBaseSDNode>(N)) {
9156 if (Alignment == 0)
9157 Alignment = 1;
9158 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9159 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9160 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9161 assert(!isLaneOp && "Unexpected generic load/store lane.");
9162 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9163 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9164 }
9165 // Don't set an explicit alignment on regular load/stores that we want
9166 // to transform to VLD/VST 1_UPD nodes.
9167 // This matches the behavior of regular load/stores, which only get an
9168 // explicit alignment if the MMO alignment is larger than the standard
9169 // alignment of the memory type.
9170 // Intrinsics, however, always get an explicit alignment, set to the
9171 // alignment of the MMO.
9172 Alignment = 1;
9173 }
9174
Bob Wilson06fce872011-02-07 17:43:21 +00009175 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009176 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009177 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009178 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009179 unsigned n;
9180 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009181 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009182 Tys[n++] = MVT::i32;
9183 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009184 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009185
9186 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009187 SmallVector<SDValue, 8> Ops;
9188 Ops.push_back(N->getOperand(0)); // incoming chain
9189 Ops.push_back(N->getOperand(AddrOpIdx));
9190 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009191
9192 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9193 // Try to match the intrinsic's signature
9194 Ops.push_back(StN->getValue());
9195 } else {
9196 // Loads (and of course intrinsics) match the intrinsics' signature,
9197 // so just add all but the alignment operand.
9198 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9199 Ops.push_back(N->getOperand(i));
9200 }
9201
9202 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009203 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009204
9205 // If this is a non-standard-aligned STORE, the penultimate operand is the
9206 // stored value. Bitcast it to the aligned type.
9207 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9208 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009209 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009210 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009211
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009212 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009213 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009214 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009215
9216 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009217 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009218 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009219 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009220
9221 // If this is an non-standard-aligned LOAD, the first result is the loaded
9222 // value. Bitcast it to the expected result type.
9223 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9224 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009225 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009226 }
9227
Bob Wilson06fce872011-02-07 17:43:21 +00009228 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9229 DCI.CombineTo(N, NewResults);
9230 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9231
9232 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009233 }
Bob Wilson06fce872011-02-07 17:43:21 +00009234 return SDValue();
9235}
9236
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009237static SDValue PerformVLDCombine(SDNode *N,
9238 TargetLowering::DAGCombinerInfo &DCI) {
9239 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9240 return SDValue();
9241
9242 return CombineBaseUpdate(N, DCI);
9243}
9244
Bob Wilson2d790df2010-11-28 06:51:26 +00009245/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9246/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9247/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9248/// return true.
9249static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9250 SelectionDAG &DAG = DCI.DAG;
9251 EVT VT = N->getValueType(0);
9252 // vldN-dup instructions only support 64-bit vectors for N > 1.
9253 if (!VT.is64BitVector())
9254 return false;
9255
9256 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9257 SDNode *VLD = N->getOperand(0).getNode();
9258 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9259 return false;
9260 unsigned NumVecs = 0;
9261 unsigned NewOpc = 0;
9262 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9263 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9264 NumVecs = 2;
9265 NewOpc = ARMISD::VLD2DUP;
9266 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9267 NumVecs = 3;
9268 NewOpc = ARMISD::VLD3DUP;
9269 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9270 NumVecs = 4;
9271 NewOpc = ARMISD::VLD4DUP;
9272 } else {
9273 return false;
9274 }
9275
9276 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9277 // numbers match the load.
9278 unsigned VLDLaneNo =
9279 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9280 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9281 UI != UE; ++UI) {
9282 // Ignore uses of the chain result.
9283 if (UI.getUse().getResNo() == NumVecs)
9284 continue;
9285 SDNode *User = *UI;
9286 if (User->getOpcode() != ARMISD::VDUPLANE ||
9287 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9288 return false;
9289 }
9290
9291 // Create the vldN-dup node.
9292 EVT Tys[5];
9293 unsigned n;
9294 for (n = 0; n < NumVecs; ++n)
9295 Tys[n] = VT;
9296 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009297 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009298 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9299 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009300 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009301 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009302 VLDMemInt->getMemOperand());
9303
9304 // Update the uses.
9305 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9306 UI != UE; ++UI) {
9307 unsigned ResNo = UI.getUse().getResNo();
9308 // Ignore uses of the chain result.
9309 if (ResNo == NumVecs)
9310 continue;
9311 SDNode *User = *UI;
9312 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9313 }
9314
9315 // Now the vldN-lane intrinsic is dead except for its chain result.
9316 // Update uses of the chain.
9317 std::vector<SDValue> VLDDupResults;
9318 for (unsigned n = 0; n < NumVecs; ++n)
9319 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9320 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9321 DCI.CombineTo(VLD, VLDDupResults);
9322
9323 return true;
9324}
9325
Bob Wilson103a0dc2010-07-14 01:22:12 +00009326/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9327/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009328static SDValue PerformVDUPLANECombine(SDNode *N,
9329 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009330 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009331
Bob Wilson2d790df2010-11-28 06:51:26 +00009332 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9333 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9334 if (CombineVLDDUP(N, DCI))
9335 return SDValue(N, 0);
9336
9337 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9338 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009339 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009340 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009341 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009342 return SDValue();
9343
9344 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9345 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9346 // The canonical VMOV for a zero vector uses a 32-bit element size.
9347 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9348 unsigned EltBits;
9349 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9350 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009351 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009352 if (EltSize > VT.getVectorElementType().getSizeInBits())
9353 return SDValue();
9354
Andrew Trickef9de2a2013-05-25 02:42:55 +00009355 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009356}
9357
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009358static SDValue PerformLOADCombine(SDNode *N,
9359 TargetLowering::DAGCombinerInfo &DCI) {
9360 EVT VT = N->getValueType(0);
9361
9362 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9363 if (ISD::isNormalLoad(N) && VT.isVector() &&
9364 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9365 return CombineBaseUpdate(N, DCI);
9366
9367 return SDValue();
9368}
9369
Ahmed Bougacha23167462014-12-09 21:26:53 +00009370/// PerformSTORECombine - Target-specific dag combine xforms for
9371/// ISD::STORE.
9372static SDValue PerformSTORECombine(SDNode *N,
9373 TargetLowering::DAGCombinerInfo &DCI) {
9374 StoreSDNode *St = cast<StoreSDNode>(N);
9375 if (St->isVolatile())
9376 return SDValue();
9377
9378 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9379 // pack all of the elements in one place. Next, store to memory in fewer
9380 // chunks.
9381 SDValue StVal = St->getValue();
9382 EVT VT = StVal.getValueType();
9383 if (St->isTruncatingStore() && VT.isVector()) {
9384 SelectionDAG &DAG = DCI.DAG;
9385 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9386 EVT StVT = St->getMemoryVT();
9387 unsigned NumElems = VT.getVectorNumElements();
9388 assert(StVT != VT && "Cannot truncate to the same type");
9389 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9390 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9391
9392 // From, To sizes and ElemCount must be pow of two
9393 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9394
9395 // We are going to use the original vector elt for storing.
9396 // Accumulated smaller vector elements must be a multiple of the store size.
9397 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9398
9399 unsigned SizeRatio = FromEltSz / ToEltSz;
9400 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9401
9402 // Create a type on which we perform the shuffle.
9403 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9404 NumElems*SizeRatio);
9405 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9406
9407 SDLoc DL(St);
9408 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9409 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9410 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +00009411 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
9412 ? (i + 1) * SizeRatio - 1
9413 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +00009414
9415 // Can't shuffle using an illegal type.
9416 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9417
9418 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9419 DAG.getUNDEF(WideVec.getValueType()),
9420 ShuffleVec.data());
9421 // At this point all of the data is stored at the bottom of the
9422 // register. We now need to save it to mem.
9423
9424 // Find the largest store unit
9425 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009426 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009427 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9428 StoreType = Tp;
9429 }
9430 // Didn't find a legal store type.
9431 if (!TLI.isTypeLegal(StoreType))
9432 return SDValue();
9433
9434 // Bitcast the original vector into a vector of store-size units
9435 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9436 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9437 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9438 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9439 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +00009440 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
9441 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009442 SDValue BasePtr = St->getBasePtr();
9443
9444 // Perform one or more big stores into memory.
9445 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9446 for (unsigned I = 0; I < E; I++) {
9447 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9448 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009449 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009450 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9451 St->getPointerInfo(), St->isVolatile(),
9452 St->isNonTemporal(), St->getAlignment());
9453 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9454 Increment);
9455 Chains.push_back(Ch);
9456 }
9457 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9458 }
9459
9460 if (!ISD::isNormalStore(St))
9461 return SDValue();
9462
9463 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9464 // ARM stores of arguments in the same cache line.
9465 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9466 StVal.getNode()->hasOneUse()) {
9467 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +00009468 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +00009469 SDLoc DL(St);
9470 SDValue BasePtr = St->getBasePtr();
9471 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9472 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9473 BasePtr, St->getPointerInfo(), St->isVolatile(),
9474 St->isNonTemporal(), St->getAlignment());
9475
9476 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009477 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009478 return DAG.getStore(NewST1.getValue(0), DL,
9479 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9480 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9481 St->isNonTemporal(),
9482 std::min(4U, St->getAlignment() / 2));
9483 }
9484
9485 if (StVal.getValueType() == MVT::i64 &&
9486 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9487
9488 // Bitcast an i64 store extracted from a vector to f64.
9489 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9490 SelectionDAG &DAG = DCI.DAG;
9491 SDLoc dl(StVal);
9492 SDValue IntVec = StVal.getOperand(0);
9493 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9494 IntVec.getValueType().getVectorNumElements());
9495 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9496 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9497 Vec, StVal.getOperand(1));
9498 dl = SDLoc(N);
9499 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9500 // Make the DAGCombiner fold the bitcasts.
9501 DCI.AddToWorklist(Vec.getNode());
9502 DCI.AddToWorklist(ExtElt.getNode());
9503 DCI.AddToWorklist(V.getNode());
9504 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9505 St->getPointerInfo(), St->isVolatile(),
9506 St->isNonTemporal(), St->getAlignment(),
9507 St->getAAInfo());
9508 }
9509
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009510 // If this is a legal vector store, try to combine it into a VST1_UPD.
9511 if (ISD::isNormalStore(N) && VT.isVector() &&
9512 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9513 return CombineBaseUpdate(N, DCI);
9514
Ahmed Bougacha23167462014-12-09 21:26:53 +00009515 return SDValue();
9516}
9517
Eric Christopher1b8b94192011-06-29 21:10:36 +00009518// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009519// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9520static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9521{
Chad Rosier6b610b32011-06-28 17:26:57 +00009522 integerPart cN;
9523 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009524 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9525 I != E; I++) {
9526 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9527 if (!C)
9528 return false;
9529
Eric Christopher1b8b94192011-06-29 21:10:36 +00009530 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009531 APFloat APF = C->getValueAPF();
9532 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9533 != APFloat::opOK || !isExact)
9534 return false;
9535
9536 c0 = (I == 0) ? cN : c0;
9537 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9538 return false;
9539 }
9540 C = c0;
9541 return true;
9542}
9543
9544/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9545/// can replace combinations of VMUL and VCVT (floating-point to integer)
9546/// when the VMUL has a constant operand that is a power of 2.
9547///
9548/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9549/// vmul.f32 d16, d17, d16
9550/// vcvt.s32.f32 d16, d16
9551/// becomes:
9552/// vcvt.s32.f32 d16, d16, #3
9553static SDValue PerformVCVTCombine(SDNode *N,
9554 TargetLowering::DAGCombinerInfo &DCI,
9555 const ARMSubtarget *Subtarget) {
9556 SelectionDAG &DAG = DCI.DAG;
9557 SDValue Op = N->getOperand(0);
9558
9559 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9560 Op.getOpcode() != ISD::FMUL)
9561 return SDValue();
9562
9563 uint64_t C;
9564 SDValue N0 = Op->getOperand(0);
9565 SDValue ConstVec = Op->getOperand(1);
9566 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9567
Eric Christopher1b8b94192011-06-29 21:10:36 +00009568 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009569 !isConstVecPow2(ConstVec, isSigned, C))
9570 return SDValue();
9571
Tim Northover7cbc2152013-06-28 15:29:25 +00009572 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9573 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009574 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9575 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9576 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009577 // These instructions only exist converting from f32 to i32. We can handle
9578 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009579 // be lossy. We also can't handle more then 4 lanes, since these intructions
9580 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009581 return SDValue();
9582 }
9583
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009584 SDLoc dl(N);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009585 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9586 Intrinsic::arm_neon_vcvtfp2fxu;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009587 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Tim Northover7cbc2152013-06-28 15:29:25 +00009588 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009589 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9590 N0,
9591 DAG.getConstant(Log2_64(C), dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009592
9593 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009594 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009595
9596 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009597}
9598
9599/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9600/// can replace combinations of VCVT (integer to floating-point) and VDIV
9601/// when the VDIV has a constant operand that is a power of 2.
9602///
9603/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9604/// vcvt.f32.s32 d16, d16
9605/// vdiv.f32 d16, d17, d16
9606/// becomes:
9607/// vcvt.f32.s32 d16, d16, #3
9608static SDValue PerformVDIVCombine(SDNode *N,
9609 TargetLowering::DAGCombinerInfo &DCI,
9610 const ARMSubtarget *Subtarget) {
9611 SelectionDAG &DAG = DCI.DAG;
9612 SDValue Op = N->getOperand(0);
9613 unsigned OpOpcode = Op.getNode()->getOpcode();
9614
9615 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9616 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9617 return SDValue();
9618
9619 uint64_t C;
9620 SDValue ConstVec = N->getOperand(1);
9621 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9622
9623 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9624 !isConstVecPow2(ConstVec, isSigned, C))
9625 return SDValue();
9626
Tim Northover7cbc2152013-06-28 15:29:25 +00009627 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9628 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9629 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9630 // These instructions only exist converting from i32 to f32. We can handle
9631 // smaller integers by generating an extra extend, but larger ones would
9632 // be lossy.
9633 return SDValue();
9634 }
9635
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009636 SDLoc dl(N);
Tim Northover7cbc2152013-06-28 15:29:25 +00009637 SDValue ConvInput = Op.getOperand(0);
9638 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9639 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9640 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009641 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009642 ConvInput);
9643
Eric Christopher1b8b94192011-06-29 21:10:36 +00009644 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009645 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009646 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009647 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009648 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9649 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009650}
9651
9652/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009653/// operand of a vector shift operation, where all the elements of the
9654/// build_vector must have the same constant integer value.
9655static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9656 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009657 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009658 Op = Op.getOperand(0);
9659 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9660 APInt SplatBits, SplatUndef;
9661 unsigned SplatBitSize;
9662 bool HasAnyUndefs;
9663 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9664 HasAnyUndefs, ElementBits) ||
9665 SplatBitSize > ElementBits)
9666 return false;
9667 Cnt = SplatBits.getSExtValue();
9668 return true;
9669}
9670
9671/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9672/// operand of a vector shift left operation. That value must be in the range:
9673/// 0 <= Value < ElementBits for a left shift; or
9674/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009675static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009676 assert(VT.isVector() && "vector shift count is not a vector type");
9677 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9678 if (! getVShiftImm(Op, ElementBits, Cnt))
9679 return false;
9680 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9681}
9682
9683/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9684/// operand of a vector shift right operation. For a shift opcode, the value
9685/// is positive, but for an intrinsic the value count must be negative. The
9686/// absolute value must be in the range:
9687/// 1 <= |Value| <= ElementBits for a right shift; or
9688/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009689static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009690 int64_t &Cnt) {
9691 assert(VT.isVector() && "vector shift count is not a vector type");
9692 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9693 if (! getVShiftImm(Op, ElementBits, Cnt))
9694 return false;
9695 if (isIntrinsic)
9696 Cnt = -Cnt;
9697 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9698}
9699
9700/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9701static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9702 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9703 switch (IntNo) {
9704 default:
9705 // Don't do anything for most intrinsics.
9706 break;
9707
9708 // Vector shifts: check for immediate versions and lower them.
9709 // Note: This is done during DAG combining instead of DAG legalizing because
9710 // the build_vectors for 64-bit vector element shift counts are generally
9711 // not legal, and it is hard to see their values after they get legalized to
9712 // loads from a constant pool.
9713 case Intrinsic::arm_neon_vshifts:
9714 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009715 case Intrinsic::arm_neon_vrshifts:
9716 case Intrinsic::arm_neon_vrshiftu:
9717 case Intrinsic::arm_neon_vrshiftn:
9718 case Intrinsic::arm_neon_vqshifts:
9719 case Intrinsic::arm_neon_vqshiftu:
9720 case Intrinsic::arm_neon_vqshiftsu:
9721 case Intrinsic::arm_neon_vqshiftns:
9722 case Intrinsic::arm_neon_vqshiftnu:
9723 case Intrinsic::arm_neon_vqshiftnsu:
9724 case Intrinsic::arm_neon_vqrshiftns:
9725 case Intrinsic::arm_neon_vqrshiftnu:
9726 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009727 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009728 int64_t Cnt;
9729 unsigned VShiftOpc = 0;
9730
9731 switch (IntNo) {
9732 case Intrinsic::arm_neon_vshifts:
9733 case Intrinsic::arm_neon_vshiftu:
9734 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9735 VShiftOpc = ARMISD::VSHL;
9736 break;
9737 }
9738 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9739 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9740 ARMISD::VSHRs : ARMISD::VSHRu);
9741 break;
9742 }
9743 return SDValue();
9744
Bob Wilson2e076c42009-06-22 23:27:02 +00009745 case Intrinsic::arm_neon_vrshifts:
9746 case Intrinsic::arm_neon_vrshiftu:
9747 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9748 break;
9749 return SDValue();
9750
9751 case Intrinsic::arm_neon_vqshifts:
9752 case Intrinsic::arm_neon_vqshiftu:
9753 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9754 break;
9755 return SDValue();
9756
9757 case Intrinsic::arm_neon_vqshiftsu:
9758 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9759 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009760 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009761
Bob Wilson2e076c42009-06-22 23:27:02 +00009762 case Intrinsic::arm_neon_vrshiftn:
9763 case Intrinsic::arm_neon_vqshiftns:
9764 case Intrinsic::arm_neon_vqshiftnu:
9765 case Intrinsic::arm_neon_vqshiftnsu:
9766 case Intrinsic::arm_neon_vqrshiftns:
9767 case Intrinsic::arm_neon_vqrshiftnu:
9768 case Intrinsic::arm_neon_vqrshiftnsu:
9769 // Narrowing shifts require an immediate right shift.
9770 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9771 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009772 llvm_unreachable("invalid shift count for narrowing vector shift "
9773 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009774
9775 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009776 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009777 }
9778
9779 switch (IntNo) {
9780 case Intrinsic::arm_neon_vshifts:
9781 case Intrinsic::arm_neon_vshiftu:
9782 // Opcode already set above.
9783 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009784 case Intrinsic::arm_neon_vrshifts:
9785 VShiftOpc = ARMISD::VRSHRs; break;
9786 case Intrinsic::arm_neon_vrshiftu:
9787 VShiftOpc = ARMISD::VRSHRu; break;
9788 case Intrinsic::arm_neon_vrshiftn:
9789 VShiftOpc = ARMISD::VRSHRN; break;
9790 case Intrinsic::arm_neon_vqshifts:
9791 VShiftOpc = ARMISD::VQSHLs; break;
9792 case Intrinsic::arm_neon_vqshiftu:
9793 VShiftOpc = ARMISD::VQSHLu; break;
9794 case Intrinsic::arm_neon_vqshiftsu:
9795 VShiftOpc = ARMISD::VQSHLsu; break;
9796 case Intrinsic::arm_neon_vqshiftns:
9797 VShiftOpc = ARMISD::VQSHRNs; break;
9798 case Intrinsic::arm_neon_vqshiftnu:
9799 VShiftOpc = ARMISD::VQSHRNu; break;
9800 case Intrinsic::arm_neon_vqshiftnsu:
9801 VShiftOpc = ARMISD::VQSHRNsu; break;
9802 case Intrinsic::arm_neon_vqrshiftns:
9803 VShiftOpc = ARMISD::VQRSHRNs; break;
9804 case Intrinsic::arm_neon_vqrshiftnu:
9805 VShiftOpc = ARMISD::VQRSHRNu; break;
9806 case Intrinsic::arm_neon_vqrshiftnsu:
9807 VShiftOpc = ARMISD::VQRSHRNsu; break;
9808 }
9809
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009810 SDLoc dl(N);
9811 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
9812 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009813 }
9814
9815 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009816 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009817 int64_t Cnt;
9818 unsigned VShiftOpc = 0;
9819
9820 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9821 VShiftOpc = ARMISD::VSLI;
9822 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9823 VShiftOpc = ARMISD::VSRI;
9824 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009825 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009826 }
9827
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009828 SDLoc dl(N);
9829 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009830 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009831 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009832 }
9833
9834 case Intrinsic::arm_neon_vqrshifts:
9835 case Intrinsic::arm_neon_vqrshiftu:
9836 // No immediate versions of these to check for.
9837 break;
9838 }
9839
9840 return SDValue();
9841}
9842
9843/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9844/// lowers them. As with the vector shift intrinsics, this is done during DAG
9845/// combining instead of DAG legalizing because the build_vectors for 64-bit
9846/// vector element shift counts are generally not legal, and it is hard to see
9847/// their values after they get legalized to loads from a constant pool.
9848static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9849 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009850 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009851 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9852 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9853 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9854 SDValue N1 = N->getOperand(1);
9855 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9856 SDValue N0 = N->getOperand(0);
9857 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9858 DAG.MaskedValueIsZero(N0.getOperand(0),
9859 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009860 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009861 }
9862 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009863
9864 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009865 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9866 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009867 return SDValue();
9868
9869 assert(ST->hasNEON() && "unexpected vector shift");
9870 int64_t Cnt;
9871
9872 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009873 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009874
9875 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009876 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
9877 SDLoc dl(N);
9878 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
9879 DAG.getConstant(Cnt, dl, MVT::i32));
9880 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009881 break;
9882
9883 case ISD::SRA:
9884 case ISD::SRL:
9885 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9886 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9887 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009888 SDLoc dl(N);
9889 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
9890 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009891 }
9892 }
9893 return SDValue();
9894}
9895
9896/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9897/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9898static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9899 const ARMSubtarget *ST) {
9900 SDValue N0 = N->getOperand(0);
9901
9902 // Check for sign- and zero-extensions of vector extract operations of 8-
9903 // and 16-bit vector elements. NEON supports these directly. They are
9904 // handled during DAG combining because type legalization will promote them
9905 // to 32-bit types and it is messy to recognize the operations after that.
9906 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9907 SDValue Vec = N0.getOperand(0);
9908 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009909 EVT VT = N->getValueType(0);
9910 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009911 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9912
Owen Anderson9f944592009-08-11 20:47:22 +00009913 if (VT == MVT::i32 &&
9914 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009915 TLI.isTypeLegal(Vec.getValueType()) &&
9916 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009917
9918 unsigned Opc = 0;
9919 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009920 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009921 case ISD::SIGN_EXTEND:
9922 Opc = ARMISD::VGETLANEs;
9923 break;
9924 case ISD::ZERO_EXTEND:
9925 case ISD::ANY_EXTEND:
9926 Opc = ARMISD::VGETLANEu;
9927 break;
9928 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009929 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009930 }
9931 }
9932
9933 return SDValue();
9934}
9935
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009936/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9937/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9938static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9939 const ARMSubtarget *ST) {
9940 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009941 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009942 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9943 // a NaN; only do the transformation when it matches that behavior.
9944
9945 // For now only do this when using NEON for FP operations; if using VFP, it
9946 // is not obvious that the benefit outweighs the cost of switching to the
9947 // NEON pipeline.
9948 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9949 N->getValueType(0) != MVT::f32)
9950 return SDValue();
9951
9952 SDValue CondLHS = N->getOperand(0);
9953 SDValue CondRHS = N->getOperand(1);
9954 SDValue LHS = N->getOperand(2);
9955 SDValue RHS = N->getOperand(3);
9956 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9957
9958 unsigned Opcode = 0;
9959 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009960 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009961 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009962 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009963 IsReversed = true ; // x CC y ? y : x
9964 } else {
9965 return SDValue();
9966 }
9967
Bob Wilsonba8ac742010-02-24 22:15:53 +00009968 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009969 switch (CC) {
9970 default: break;
9971 case ISD::SETOLT:
9972 case ISD::SETOLE:
9973 case ISD::SETLT:
9974 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009975 case ISD::SETULT:
9976 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009977 // If LHS is NaN, an ordered comparison will be false and the result will
9978 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9979 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9980 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9981 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9982 break;
9983 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9984 // will return -0, so vmin can only be used for unsafe math or if one of
9985 // the operands is known to be nonzero.
9986 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009987 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009988 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9989 break;
9990 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009991 break;
9992
9993 case ISD::SETOGT:
9994 case ISD::SETOGE:
9995 case ISD::SETGT:
9996 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009997 case ISD::SETUGT:
9998 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009999 // If LHS is NaN, an ordered comparison will be false and the result will
10000 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
10001 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
10002 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
10003 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
10004 break;
10005 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
10006 // will return +0, so vmax can only be used for unsafe math or if one of
10007 // the operands is known to be nonzero.
10008 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +000010009 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +000010010 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10011 break;
10012 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010013 break;
10014 }
10015
10016 if (!Opcode)
10017 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010018 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010019}
10020
Evan Chengf863e3f2011-07-13 00:42:17 +000010021/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
10022SDValue
10023ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
10024 SDValue Cmp = N->getOperand(4);
10025 if (Cmp.getOpcode() != ARMISD::CMPZ)
10026 // Only looking at EQ and NE cases.
10027 return SDValue();
10028
10029 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010030 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000010031 SDValue LHS = Cmp.getOperand(0);
10032 SDValue RHS = Cmp.getOperand(1);
10033 SDValue FalseVal = N->getOperand(0);
10034 SDValue TrueVal = N->getOperand(1);
10035 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000010036 ARMCC::CondCodes CC =
10037 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000010038
10039 // Simplify
10040 // mov r1, r0
10041 // cmp r1, x
10042 // mov r0, y
10043 // moveq r0, x
10044 // to
10045 // cmp r0, x
10046 // movne r0, y
10047 //
10048 // mov r1, r0
10049 // cmp r1, x
10050 // mov r0, x
10051 // movne r0, y
10052 // to
10053 // cmp r0, x
10054 // movne r0, y
10055 /// FIXME: Turn this into a target neutral optimization?
10056 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000010057 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000010058 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
10059 N->getOperand(3), Cmp);
10060 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
10061 SDValue ARMcc;
10062 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
10063 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
10064 N->getOperand(3), NewCmp);
10065 }
10066
10067 if (Res.getNode()) {
10068 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000010069 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000010070 // Capture demanded bits information that would be otherwise lost.
10071 if (KnownZero == 0xfffffffe)
10072 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10073 DAG.getValueType(MVT::i1));
10074 else if (KnownZero == 0xffffff00)
10075 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10076 DAG.getValueType(MVT::i8));
10077 else if (KnownZero == 0xffff0000)
10078 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
10079 DAG.getValueType(MVT::i16));
10080 }
10081
10082 return Res;
10083}
10084
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010085SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000010086 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010087 switch (N->getOpcode()) {
10088 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000010089 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000010090 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010091 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000010092 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010093 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000010094 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
10095 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000010096 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010097 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000010098 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010099 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010100 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010101 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010102 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000010103 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000010104 case ISD::FP_TO_SINT:
10105 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
10106 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010107 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000010108 case ISD::SHL:
10109 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010110 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000010111 case ISD::SIGN_EXTEND:
10112 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000010113 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
10114 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000010115 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010116 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010117 case ARMISD::VLD2DUP:
10118 case ARMISD::VLD3DUP:
10119 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010120 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010121 case ARMISD::BUILD_VECTOR:
10122 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010123 case ISD::INTRINSIC_VOID:
10124 case ISD::INTRINSIC_W_CHAIN:
10125 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
10126 case Intrinsic::arm_neon_vld1:
10127 case Intrinsic::arm_neon_vld2:
10128 case Intrinsic::arm_neon_vld3:
10129 case Intrinsic::arm_neon_vld4:
10130 case Intrinsic::arm_neon_vld2lane:
10131 case Intrinsic::arm_neon_vld3lane:
10132 case Intrinsic::arm_neon_vld4lane:
10133 case Intrinsic::arm_neon_vst1:
10134 case Intrinsic::arm_neon_vst2:
10135 case Intrinsic::arm_neon_vst3:
10136 case Intrinsic::arm_neon_vst4:
10137 case Intrinsic::arm_neon_vst2lane:
10138 case Intrinsic::arm_neon_vst3lane:
10139 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010140 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000010141 default: break;
10142 }
10143 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010144 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010145 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010146}
10147
Evan Chengd42641c2011-02-02 01:06:55 +000010148bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10149 EVT VT) const {
10150 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10151}
10152
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010153bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10154 unsigned,
10155 unsigned,
10156 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010157 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010158 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010159
10160 switch (VT.getSimpleVT().SimpleTy) {
10161 default:
10162 return false;
10163 case MVT::i8:
10164 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010165 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010166 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010167 if (AllowsUnaligned) {
10168 if (Fast)
10169 *Fast = Subtarget->hasV7Ops();
10170 return true;
10171 }
10172 return false;
10173 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010174 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010175 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010176 // For any little-endian targets with neon, we can support unaligned ld/st
10177 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010178 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000010179 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010180 if (Fast)
10181 *Fast = true;
10182 return true;
10183 }
10184 return false;
10185 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010186 }
10187}
10188
Lang Hames9929c422011-11-02 22:52:45 +000010189static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10190 unsigned AlignCheck) {
10191 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10192 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10193}
10194
10195EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10196 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010197 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010198 bool MemcpyStrSrc,
10199 MachineFunction &MF) const {
10200 const Function *F = MF.getFunction();
10201
10202 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010203 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10204 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010205 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010206 if (Size >= 16 &&
10207 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010208 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010209 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010210 } else if (Size >= 8 &&
10211 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010212 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10213 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010214 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010215 }
10216 }
10217
Lang Hamesb85fcd02011-11-08 18:56:23 +000010218 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010219 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010220 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010221 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010222 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010223
Lang Hames9929c422011-11-02 22:52:45 +000010224 // Let the target-independent logic figure it out.
10225 return MVT::Other;
10226}
10227
Evan Cheng9ec512d2012-12-06 19:13:27 +000010228bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10229 if (Val.getOpcode() != ISD::LOAD)
10230 return false;
10231
10232 EVT VT1 = Val.getValueType();
10233 if (!VT1.isSimple() || !VT1.isInteger() ||
10234 !VT2.isSimple() || !VT2.isInteger())
10235 return false;
10236
10237 switch (VT1.getSimpleVT().SimpleTy) {
10238 default: break;
10239 case MVT::i1:
10240 case MVT::i8:
10241 case MVT::i16:
10242 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10243 return true;
10244 }
10245
10246 return false;
10247}
10248
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010249bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10250 EVT VT = ExtVal.getValueType();
10251
10252 if (!isTypeLegal(VT))
10253 return false;
10254
10255 // Don't create a loadext if we can fold the extension into a wide/long
10256 // instruction.
10257 // If there's more than one user instruction, the loadext is desirable no
10258 // matter what. There can be two uses by the same instruction.
10259 if (ExtVal->use_empty() ||
10260 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10261 return true;
10262
10263 SDNode *U = *ExtVal->use_begin();
10264 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10265 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10266 return false;
10267
10268 return true;
10269}
10270
Tim Northovercc2e9032013-08-06 13:58:03 +000010271bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10272 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10273 return false;
10274
10275 if (!isTypeLegal(EVT::getEVT(Ty1)))
10276 return false;
10277
10278 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10279
10280 // Assuming the caller doesn't have a zeroext or signext return parameter,
10281 // truncation all the way down to i1 is valid.
10282 return true;
10283}
10284
10285
Evan Chengdc49a8d2009-08-14 20:09:37 +000010286static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10287 if (V < 0)
10288 return false;
10289
10290 unsigned Scale = 1;
10291 switch (VT.getSimpleVT().SimpleTy) {
10292 default: return false;
10293 case MVT::i1:
10294 case MVT::i8:
10295 // Scale == 1;
10296 break;
10297 case MVT::i16:
10298 // Scale == 2;
10299 Scale = 2;
10300 break;
10301 case MVT::i32:
10302 // Scale == 4;
10303 Scale = 4;
10304 break;
10305 }
10306
10307 if ((V & (Scale - 1)) != 0)
10308 return false;
10309 V /= Scale;
10310 return V == (V & ((1LL << 5) - 1));
10311}
10312
10313static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10314 const ARMSubtarget *Subtarget) {
10315 bool isNeg = false;
10316 if (V < 0) {
10317 isNeg = true;
10318 V = - V;
10319 }
10320
10321 switch (VT.getSimpleVT().SimpleTy) {
10322 default: return false;
10323 case MVT::i1:
10324 case MVT::i8:
10325 case MVT::i16:
10326 case MVT::i32:
10327 // + imm12 or - imm8
10328 if (isNeg)
10329 return V == (V & ((1LL << 8) - 1));
10330 return V == (V & ((1LL << 12) - 1));
10331 case MVT::f32:
10332 case MVT::f64:
10333 // Same as ARM mode. FIXME: NEON?
10334 if (!Subtarget->hasVFP2())
10335 return false;
10336 if ((V & 3) != 0)
10337 return false;
10338 V >>= 2;
10339 return V == (V & ((1LL << 8) - 1));
10340 }
10341}
10342
Evan Cheng2150b922007-03-12 23:30:29 +000010343/// isLegalAddressImmediate - Return true if the integer value can be used
10344/// as the offset of the target addressing mode for load / store of the
10345/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010346static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010347 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010348 if (V == 0)
10349 return true;
10350
Evan Chengce5dfb62009-03-09 19:15:00 +000010351 if (!VT.isSimple())
10352 return false;
10353
Evan Chengdc49a8d2009-08-14 20:09:37 +000010354 if (Subtarget->isThumb1Only())
10355 return isLegalT1AddressImmediate(V, VT);
10356 else if (Subtarget->isThumb2())
10357 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010358
Evan Chengdc49a8d2009-08-14 20:09:37 +000010359 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010360 if (V < 0)
10361 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010362 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010363 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010364 case MVT::i1:
10365 case MVT::i8:
10366 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010367 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010368 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010369 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010370 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010371 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010372 case MVT::f32:
10373 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010374 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010375 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010376 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010377 return false;
10378 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010379 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010380 }
Evan Cheng10043e22007-01-19 07:51:42 +000010381}
10382
Evan Chengdc49a8d2009-08-14 20:09:37 +000010383bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10384 EVT VT) const {
10385 int Scale = AM.Scale;
10386 if (Scale < 0)
10387 return false;
10388
10389 switch (VT.getSimpleVT().SimpleTy) {
10390 default: return false;
10391 case MVT::i1:
10392 case MVT::i8:
10393 case MVT::i16:
10394 case MVT::i32:
10395 if (Scale == 1)
10396 return true;
10397 // r + r << imm
10398 Scale = Scale & ~1;
10399 return Scale == 2 || Scale == 4 || Scale == 8;
10400 case MVT::i64:
10401 // r + r
10402 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10403 return true;
10404 return false;
10405 case MVT::isVoid:
10406 // Note, we allow "void" uses (basically, uses that aren't loads or
10407 // stores), because arm allows folding a scale into many arithmetic
10408 // operations. This should be made more precise and revisited later.
10409
10410 // Allow r << imm, but the imm has to be a multiple of two.
10411 if (Scale & 1) return false;
10412 return isPowerOf2_32(Scale);
10413 }
10414}
10415
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010416/// isLegalAddressingMode - Return true if the addressing mode represented
10417/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010418bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10419 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010420 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000010421 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010422 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010423 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010424
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010425 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010426 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010427 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010428
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010429 switch (AM.Scale) {
10430 case 0: // no scale reg, must be "r+i" or "r", or "i".
10431 break;
10432 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010433 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010434 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010435 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010436 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010437 // ARM doesn't support any R+R*scale+imm addr modes.
10438 if (AM.BaseOffs)
10439 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010440
Bob Wilson866c1742009-04-08 17:55:28 +000010441 if (!VT.isSimple())
10442 return false;
10443
Evan Chengdc49a8d2009-08-14 20:09:37 +000010444 if (Subtarget->isThumb2())
10445 return isLegalT2ScaledAddressingMode(AM, VT);
10446
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010447 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010448 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010449 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010450 case MVT::i1:
10451 case MVT::i8:
10452 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010453 if (Scale < 0) Scale = -Scale;
10454 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010455 return true;
10456 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010457 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010458 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010459 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010460 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010461 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010462 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010463 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010464
Owen Anderson9f944592009-08-11 20:47:22 +000010465 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010466 // Note, we allow "void" uses (basically, uses that aren't loads or
10467 // stores), because arm allows folding a scale into many arithmetic
10468 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010469
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010470 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010471 if (Scale & 1) return false;
10472 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010473 }
Evan Cheng2150b922007-03-12 23:30:29 +000010474 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010475 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010476}
10477
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010478/// isLegalICmpImmediate - Return true if the specified immediate is legal
10479/// icmp immediate, that is the target has icmp instructions which can compare
10480/// a register against the immediate without having to materialize the
10481/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010482bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010483 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010484 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010485 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010486 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010487 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010488 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010489 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010490}
10491
Andrew Tricka22cdb72012-07-18 18:34:27 +000010492/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10493/// *or sub* immediate, that is the target has add or sub instructions which can
10494/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010495/// immediate into a register.
10496bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010497 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010498 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010499 if (!Subtarget->isThumb())
10500 return ARM_AM::getSOImmVal(AbsImm) != -1;
10501 if (Subtarget->isThumb2())
10502 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10503 // Thumb1 only has 8-bit unsigned immediate.
10504 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010505}
10506
Owen Anderson53aa7a92009-08-10 22:56:29 +000010507static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010508 bool isSEXTLoad, SDValue &Base,
10509 SDValue &Offset, bool &isInc,
10510 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010511 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10512 return false;
10513
Owen Anderson9f944592009-08-11 20:47:22 +000010514 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010515 // AddressingMode 3
10516 Base = Ptr->getOperand(0);
10517 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010518 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010519 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010520 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010521 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010522 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010523 return true;
10524 }
10525 }
10526 isInc = (Ptr->getOpcode() == ISD::ADD);
10527 Offset = Ptr->getOperand(1);
10528 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010529 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010530 // AddressingMode 2
10531 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010532 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010533 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010534 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010535 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010536 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010537 Base = Ptr->getOperand(0);
10538 return true;
10539 }
10540 }
10541
10542 if (Ptr->getOpcode() == ISD::ADD) {
10543 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010544 ARM_AM::ShiftOpc ShOpcVal=
10545 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010546 if (ShOpcVal != ARM_AM::no_shift) {
10547 Base = Ptr->getOperand(1);
10548 Offset = Ptr->getOperand(0);
10549 } else {
10550 Base = Ptr->getOperand(0);
10551 Offset = Ptr->getOperand(1);
10552 }
10553 return true;
10554 }
10555
10556 isInc = (Ptr->getOpcode() == ISD::ADD);
10557 Base = Ptr->getOperand(0);
10558 Offset = Ptr->getOperand(1);
10559 return true;
10560 }
10561
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010562 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010563 return false;
10564}
10565
Owen Anderson53aa7a92009-08-10 22:56:29 +000010566static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010567 bool isSEXTLoad, SDValue &Base,
10568 SDValue &Offset, bool &isInc,
10569 SelectionDAG &DAG) {
10570 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10571 return false;
10572
10573 Base = Ptr->getOperand(0);
10574 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10575 int RHSC = (int)RHS->getZExtValue();
10576 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10577 assert(Ptr->getOpcode() == ISD::ADD);
10578 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010579 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010580 return true;
10581 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10582 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010583 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010584 return true;
10585 }
10586 }
10587
10588 return false;
10589}
10590
Evan Cheng10043e22007-01-19 07:51:42 +000010591/// getPreIndexedAddressParts - returns true by value, base pointer and
10592/// offset pointer and addressing mode by reference if the node's address
10593/// can be legally represented as pre-indexed load / store address.
10594bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010595ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10596 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010597 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010598 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010599 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010600 return false;
10601
Owen Anderson53aa7a92009-08-10 22:56:29 +000010602 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010603 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010604 bool isSEXTLoad = false;
10605 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10606 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010607 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010608 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10609 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10610 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010611 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010612 } else
10613 return false;
10614
10615 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010616 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010617 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010618 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10619 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010620 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010621 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010622 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010623 if (!isLegal)
10624 return false;
10625
10626 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10627 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010628}
10629
10630/// getPostIndexedAddressParts - returns true by value, base pointer and
10631/// offset pointer and addressing mode by reference if this node can be
10632/// combined with a load / store to form a post-indexed load / store.
10633bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010634 SDValue &Base,
10635 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010636 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010637 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010638 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010639 return false;
10640
Owen Anderson53aa7a92009-08-10 22:56:29 +000010641 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010642 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010643 bool isSEXTLoad = false;
10644 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010645 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010646 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010647 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10648 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010649 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010650 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010651 } else
10652 return false;
10653
10654 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010655 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010656 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010657 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010658 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010659 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010660 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10661 isInc, DAG);
10662 if (!isLegal)
10663 return false;
10664
Evan Chengf19384d2010-05-18 21:31:17 +000010665 if (Ptr != Base) {
10666 // Swap base ptr and offset to catch more post-index load / store when
10667 // it's legal. In Thumb2 mode, offset must be an immediate.
10668 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10669 !Subtarget->isThumb2())
10670 std::swap(Base, Offset);
10671
10672 // Post-indexed load / store update the base pointer.
10673 if (Ptr != Base)
10674 return false;
10675 }
10676
Evan Cheng84c6cda2009-07-02 07:28:31 +000010677 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10678 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010679}
10680
Jay Foada0653a32014-05-14 21:14:37 +000010681void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10682 APInt &KnownZero,
10683 APInt &KnownOne,
10684 const SelectionDAG &DAG,
10685 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010686 unsigned BitWidth = KnownOne.getBitWidth();
10687 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010688 switch (Op.getOpcode()) {
10689 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010690 case ARMISD::ADDC:
10691 case ARMISD::ADDE:
10692 case ARMISD::SUBC:
10693 case ARMISD::SUBE:
10694 // These nodes' second result is a boolean
10695 if (Op.getResNo() == 0)
10696 break;
10697 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10698 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010699 case ARMISD::CMOV: {
10700 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010701 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010702 if (KnownZero == 0 && KnownOne == 0) return;
10703
Dan Gohmanf990faf2008-02-13 00:35:47 +000010704 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010705 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010706 KnownZero &= KnownZeroRHS;
10707 KnownOne &= KnownOneRHS;
10708 return;
10709 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010710 case ISD::INTRINSIC_W_CHAIN: {
10711 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10712 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10713 switch (IntID) {
10714 default: return;
10715 case Intrinsic::arm_ldaex:
10716 case Intrinsic::arm_ldrex: {
10717 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10718 unsigned MemBits = VT.getScalarType().getSizeInBits();
10719 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10720 return;
10721 }
10722 }
10723 }
Evan Cheng10043e22007-01-19 07:51:42 +000010724 }
10725}
10726
10727//===----------------------------------------------------------------------===//
10728// ARM Inline Assembly Support
10729//===----------------------------------------------------------------------===//
10730
Evan Cheng078b0b02011-01-08 01:24:27 +000010731bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10732 // Looking for "rev" which is V6+.
10733 if (!Subtarget->hasV6Ops())
10734 return false;
10735
10736 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10737 std::string AsmStr = IA->getAsmString();
10738 SmallVector<StringRef, 4> AsmPieces;
10739 SplitString(AsmStr, AsmPieces, ";\n");
10740
10741 switch (AsmPieces.size()) {
10742 default: return false;
10743 case 1:
10744 AsmStr = AsmPieces[0];
10745 AsmPieces.clear();
10746 SplitString(AsmStr, AsmPieces, " \t,");
10747
10748 // rev $0, $1
10749 if (AsmPieces.size() == 3 &&
10750 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10751 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010752 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010753 if (Ty && Ty->getBitWidth() == 32)
10754 return IntrinsicLowering::LowerToByteSwap(CI);
10755 }
10756 break;
10757 }
10758
10759 return false;
10760}
10761
Evan Cheng10043e22007-01-19 07:51:42 +000010762/// getConstraintType - Given a constraint letter, return the type of
10763/// constraint it is for this target.
10764ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010765ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000010766 if (Constraint.size() == 1) {
10767 switch (Constraint[0]) {
10768 default: break;
10769 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010770 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010771 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010772 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010773 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010774 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010775 // An address with a single base register. Due to the way we
10776 // currently handle addresses it is the same as an 'r' memory constraint.
10777 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010778 }
Eric Christophere256cd02011-06-21 22:10:57 +000010779 } else if (Constraint.size() == 2) {
10780 switch (Constraint[0]) {
10781 default: break;
10782 // All 'U+' constraints are addresses.
10783 case 'U': return C_Memory;
10784 }
Evan Cheng10043e22007-01-19 07:51:42 +000010785 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010786 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010787}
10788
John Thompsone8360b72010-10-29 17:29:13 +000010789/// Examine constraint type and operand type and determine a weight value.
10790/// This object must already have been set up with the operand type
10791/// and the current alternative constraint selected.
10792TargetLowering::ConstraintWeight
10793ARMTargetLowering::getSingleConstraintMatchWeight(
10794 AsmOperandInfo &info, const char *constraint) const {
10795 ConstraintWeight weight = CW_Invalid;
10796 Value *CallOperandVal = info.CallOperandVal;
10797 // If we don't have a value, we can't do a match,
10798 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010799 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010800 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010801 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010802 // Look at the constraint type.
10803 switch (*constraint) {
10804 default:
10805 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10806 break;
10807 case 'l':
10808 if (type->isIntegerTy()) {
10809 if (Subtarget->isThumb())
10810 weight = CW_SpecificReg;
10811 else
10812 weight = CW_Register;
10813 }
10814 break;
10815 case 'w':
10816 if (type->isFloatingPointTy())
10817 weight = CW_Register;
10818 break;
10819 }
10820 return weight;
10821}
10822
Eric Christophercf2007c2011-06-30 23:50:52 +000010823typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000010824RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
10825 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010826 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010827 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010828 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010829 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010830 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010831 return RCPair(0U, &ARM::tGPRRegClass);
10832 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010833 case 'h': // High regs or no regs.
10834 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010835 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010836 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010837 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010838 if (Subtarget->isThumb1Only())
10839 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010840 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010841 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010842 if (VT == MVT::Other)
10843 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010844 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010845 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010846 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010847 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010848 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010849 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010850 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010851 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010852 if (VT == MVT::Other)
10853 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010854 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010855 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010856 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010857 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010858 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010859 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010860 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010861 case 't':
10862 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010863 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010864 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010865 }
10866 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010867 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010868 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010869
Eric Christopher11e4df72015-02-26 22:38:43 +000010870 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010871}
10872
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010873/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10874/// vector. If it is invalid, don't add anything to Ops.
10875void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010876 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010877 std::vector<SDValue>&Ops,
10878 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010879 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010880
Eric Christopherde9399b2011-06-02 23:16:42 +000010881 // Currently only support length 1 constraints.
10882 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010883
Eric Christopherde9399b2011-06-02 23:16:42 +000010884 char ConstraintLetter = Constraint[0];
10885 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010886 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010887 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010888 case 'I': case 'J': case 'K': case 'L':
10889 case 'M': case 'N': case 'O':
10890 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10891 if (!C)
10892 return;
10893
10894 int64_t CVal64 = C->getSExtValue();
10895 int CVal = (int) CVal64;
10896 // None of these constraints allow values larger than 32 bits. Check
10897 // that the value fits in an int.
10898 if (CVal != CVal64)
10899 return;
10900
Eric Christopherde9399b2011-06-02 23:16:42 +000010901 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010902 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010903 // Constant suitable for movw, must be between 0 and
10904 // 65535.
10905 if (Subtarget->hasV6T2Ops())
10906 if (CVal >= 0 && CVal <= 65535)
10907 break;
10908 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010909 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010910 if (Subtarget->isThumb1Only()) {
10911 // This must be a constant between 0 and 255, for ADD
10912 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010913 if (CVal >= 0 && CVal <= 255)
10914 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010915 } else if (Subtarget->isThumb2()) {
10916 // A constant that can be used as an immediate value in a
10917 // data-processing instruction.
10918 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10919 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010920 } else {
10921 // A constant that can be used as an immediate value in a
10922 // data-processing instruction.
10923 if (ARM_AM::getSOImmVal(CVal) != -1)
10924 break;
10925 }
10926 return;
10927
10928 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010929 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010930 // This must be a constant between -255 and -1, for negated ADD
10931 // immediates. This can be used in GCC with an "n" modifier that
10932 // prints the negated value, for use with SUB instructions. It is
10933 // not useful otherwise but is implemented for compatibility.
10934 if (CVal >= -255 && CVal <= -1)
10935 break;
10936 } else {
10937 // This must be a constant between -4095 and 4095. It is not clear
10938 // what this constraint is intended for. Implemented for
10939 // compatibility with GCC.
10940 if (CVal >= -4095 && CVal <= 4095)
10941 break;
10942 }
10943 return;
10944
10945 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010946 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010947 // A 32-bit value where only one byte has a nonzero value. Exclude
10948 // zero to match GCC. This constraint is used by GCC internally for
10949 // constants that can be loaded with a move/shift combination.
10950 // It is not useful otherwise but is implemented for compatibility.
10951 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10952 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010953 } else if (Subtarget->isThumb2()) {
10954 // A constant whose bitwise inverse can be used as an immediate
10955 // value in a data-processing instruction. This can be used in GCC
10956 // with a "B" modifier that prints the inverted value, for use with
10957 // BIC and MVN instructions. It is not useful otherwise but is
10958 // implemented for compatibility.
10959 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10960 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010961 } else {
10962 // A constant whose bitwise inverse can be used as an immediate
10963 // value in a data-processing instruction. This can be used in GCC
10964 // with a "B" modifier that prints the inverted value, for use with
10965 // BIC and MVN instructions. It is not useful otherwise but is
10966 // implemented for compatibility.
10967 if (ARM_AM::getSOImmVal(~CVal) != -1)
10968 break;
10969 }
10970 return;
10971
10972 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010973 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010974 // This must be a constant between -7 and 7,
10975 // for 3-operand ADD/SUB immediate instructions.
10976 if (CVal >= -7 && CVal < 7)
10977 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010978 } else if (Subtarget->isThumb2()) {
10979 // A constant whose negation can be used as an immediate value in a
10980 // data-processing instruction. This can be used in GCC with an "n"
10981 // modifier that prints the negated value, for use with SUB
10982 // instructions. It is not useful otherwise but is implemented for
10983 // compatibility.
10984 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10985 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010986 } else {
10987 // A constant whose negation can be used as an immediate value in a
10988 // data-processing instruction. This can be used in GCC with an "n"
10989 // modifier that prints the negated value, for use with SUB
10990 // instructions. It is not useful otherwise but is implemented for
10991 // compatibility.
10992 if (ARM_AM::getSOImmVal(-CVal) != -1)
10993 break;
10994 }
10995 return;
10996
10997 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010998 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010999 // This must be a multiple of 4 between 0 and 1020, for
11000 // ADD sp + immediate.
11001 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
11002 break;
11003 } else {
11004 // A power of two or a constant between 0 and 32. This is used in
11005 // GCC for the shift amount on shifted register operands, but it is
11006 // useful in general for any shift amounts.
11007 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
11008 break;
11009 }
11010 return;
11011
11012 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000011013 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011014 // This must be a constant between 0 and 31, for shift amounts.
11015 if (CVal >= 0 && CVal <= 31)
11016 break;
11017 }
11018 return;
11019
11020 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000011021 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011022 // This must be a multiple of 4 between -508 and 508, for
11023 // ADD/SUB sp = sp + immediate.
11024 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
11025 break;
11026 }
11027 return;
11028 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011029 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011030 break;
11031 }
11032
11033 if (Result.getNode()) {
11034 Ops.push_back(Result);
11035 return;
11036 }
Dale Johannesence97d552010-06-25 21:55:36 +000011037 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000011038}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011039
Renato Golin87610692013-07-16 09:32:17 +000011040SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
11041 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
11042 unsigned Opcode = Op->getOpcode();
11043 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011044 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000011045 bool isSigned = (Opcode == ISD::SDIVREM);
11046 EVT VT = Op->getValueType(0);
11047 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
11048
11049 RTLIB::Libcall LC;
11050 switch (VT.getSimpleVT().SimpleTy) {
11051 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000011052 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
11053 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
11054 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
11055 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000011056 }
11057
11058 SDValue InChain = DAG.getEntryNode();
11059
11060 TargetLowering::ArgListTy Args;
11061 TargetLowering::ArgListEntry Entry;
11062 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
11063 EVT ArgVT = Op->getOperand(i).getValueType();
11064 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
11065 Entry.Node = Op->getOperand(i);
11066 Entry.Ty = ArgTy;
11067 Entry.isSExt = isSigned;
11068 Entry.isZExt = !isSigned;
11069 Args.push_back(Entry);
11070 }
11071
11072 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000011073 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000011074
Reid Kleckner343c3952014-11-20 23:51:47 +000011075 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000011076
11077 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011078 TargetLowering::CallLoweringInfo CLI(DAG);
11079 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000011080 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011081 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000011082
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000011083 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000011084 return CallInfo.first;
11085}
11086
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011087SDValue
11088ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
11089 assert(Subtarget->isTargetWindows() && "unsupported target platform");
11090 SDLoc DL(Op);
11091
11092 // Get the inputs.
11093 SDValue Chain = Op.getOperand(0);
11094 SDValue Size = Op.getOperand(1);
11095
11096 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011097 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011098
11099 SDValue Flag;
11100 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
11101 Flag = Chain.getValue(1);
11102
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000011103 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000011104 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
11105
11106 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
11107 Chain = NewSP.getValue(1);
11108
11109 SDValue Ops[2] = { NewSP, Chain };
11110 return DAG.getMergeValues(Ops, DL);
11111}
11112
Oliver Stannard51b1d462014-08-21 12:50:31 +000011113SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
11114 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
11115 "Unexpected type for custom-lowering FP_EXTEND");
11116
11117 RTLIB::Libcall LC;
11118 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
11119
11120 SDValue SrcVal = Op.getOperand(0);
11121 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11122 /*isSigned*/ false, SDLoc(Op)).first;
11123}
11124
11125SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
11126 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
11127 Subtarget->isFPOnlySP() &&
11128 "Unexpected type for custom-lowering FP_ROUND");
11129
11130 RTLIB::Libcall LC;
11131 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
11132
11133 SDValue SrcVal = Op.getOperand(0);
11134 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
11135 /*isSigned*/ false, SDLoc(Op)).first;
11136}
11137
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000011138bool
11139ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11140 // The ARM target isn't yet aware of offsets.
11141 return false;
11142}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011143
Jim Grosbach11013ed2010-07-16 23:05:05 +000011144bool ARM::isBitFieldInvertedMask(unsigned v) {
11145 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011146 return false;
11147
Jim Grosbach11013ed2010-07-16 23:05:05 +000011148 // there can be 1's on either or both "outsides", all the "inside"
11149 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011150 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011151}
11152
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011153/// isFPImmLegal - Returns true if the target can instruction select the
11154/// specified FP immediate natively. If false, the legalizer will
11155/// materialize the FP immediate as a load from a constant pool.
11156bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11157 if (!Subtarget->hasVFP3())
11158 return false;
11159 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011160 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011161 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011162 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011163 return false;
11164}
Bob Wilson5549d492010-09-21 17:56:22 +000011165
Wesley Peck527da1b2010-11-23 03:31:01 +000011166/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011167/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11168/// specified in the intrinsic calls.
11169bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11170 const CallInst &I,
11171 unsigned Intrinsic) const {
11172 switch (Intrinsic) {
11173 case Intrinsic::arm_neon_vld1:
11174 case Intrinsic::arm_neon_vld2:
11175 case Intrinsic::arm_neon_vld3:
11176 case Intrinsic::arm_neon_vld4:
11177 case Intrinsic::arm_neon_vld2lane:
11178 case Intrinsic::arm_neon_vld3lane:
11179 case Intrinsic::arm_neon_vld4lane: {
11180 Info.opc = ISD::INTRINSIC_W_CHAIN;
11181 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011182 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
11183 uint64_t NumElts = DL.getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011184 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11185 Info.ptrVal = I.getArgOperand(0);
11186 Info.offset = 0;
11187 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11188 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11189 Info.vol = false; // volatile loads with NEON intrinsics not supported
11190 Info.readMem = true;
11191 Info.writeMem = false;
11192 return true;
11193 }
11194 case Intrinsic::arm_neon_vst1:
11195 case Intrinsic::arm_neon_vst2:
11196 case Intrinsic::arm_neon_vst3:
11197 case Intrinsic::arm_neon_vst4:
11198 case Intrinsic::arm_neon_vst2lane:
11199 case Intrinsic::arm_neon_vst3lane:
11200 case Intrinsic::arm_neon_vst4lane: {
11201 Info.opc = ISD::INTRINSIC_VOID;
11202 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011203 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000011204 unsigned NumElts = 0;
11205 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011206 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011207 if (!ArgTy->isVectorTy())
11208 break;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011209 NumElts += DL.getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011210 }
11211 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11212 Info.ptrVal = I.getArgOperand(0);
11213 Info.offset = 0;
11214 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11215 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11216 Info.vol = false; // volatile stores with NEON intrinsics not supported
11217 Info.readMem = false;
11218 Info.writeMem = true;
11219 return true;
11220 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011221 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011222 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011223 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011224 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11225 Info.opc = ISD::INTRINSIC_W_CHAIN;
11226 Info.memVT = MVT::getVT(PtrTy->getElementType());
11227 Info.ptrVal = I.getArgOperand(0);
11228 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011229 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011230 Info.vol = true;
11231 Info.readMem = true;
11232 Info.writeMem = false;
11233 return true;
11234 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011235 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011236 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011237 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000011238 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11239 Info.opc = ISD::INTRINSIC_W_CHAIN;
11240 Info.memVT = MVT::getVT(PtrTy->getElementType());
11241 Info.ptrVal = I.getArgOperand(1);
11242 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011243 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000011244 Info.vol = true;
11245 Info.readMem = false;
11246 Info.writeMem = true;
11247 return true;
11248 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011249 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011250 case Intrinsic::arm_strexd: {
11251 Info.opc = ISD::INTRINSIC_W_CHAIN;
11252 Info.memVT = MVT::i64;
11253 Info.ptrVal = I.getArgOperand(2);
11254 Info.offset = 0;
11255 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011256 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011257 Info.readMem = false;
11258 Info.writeMem = true;
11259 return true;
11260 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011261 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011262 case Intrinsic::arm_ldrexd: {
11263 Info.opc = ISD::INTRINSIC_W_CHAIN;
11264 Info.memVT = MVT::i64;
11265 Info.ptrVal = I.getArgOperand(0);
11266 Info.offset = 0;
11267 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011268 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011269 Info.readMem = true;
11270 Info.writeMem = false;
11271 return true;
11272 }
Bob Wilson5549d492010-09-21 17:56:22 +000011273 default:
11274 break;
11275 }
11276
11277 return false;
11278}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011279
11280/// \brief Returns true if it is beneficial to convert a load of a constant
11281/// to just the constant itself.
11282bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11283 Type *Ty) const {
11284 assert(Ty->isIntegerTy());
11285
11286 unsigned Bits = Ty->getPrimitiveSizeInBits();
11287 if (Bits == 0 || Bits > 32)
11288 return false;
11289 return true;
11290}
Tim Northover037f26f22014-04-17 18:22:47 +000011291
Robin Morisset25c8e312014-09-17 00:06:58 +000011292bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11293
Robin Morisset5349e8e2014-09-18 18:56:04 +000011294Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11295 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011296 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011297
11298 // First, if the target has no DMB, see what fallback we can use.
11299 if (!Subtarget->hasDataBarrier()) {
11300 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11301 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11302 // here.
11303 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11304 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11305 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11306 Builder.getInt32(0), Builder.getInt32(7),
11307 Builder.getInt32(10), Builder.getInt32(5)};
11308 return Builder.CreateCall(MCR, args);
11309 } else {
11310 // Instead of using barriers, atomic accesses on these subtargets use
11311 // libcalls.
11312 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11313 }
11314 } else {
11315 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11316 // Only a full system barrier exists in the M-class architectures.
11317 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11318 Constant *CDomain = Builder.getInt32(Domain);
11319 return Builder.CreateCall(DMB, CDomain);
11320 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011321}
11322
11323// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011324Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011325 AtomicOrdering Ord, bool IsStore,
11326 bool IsLoad) const {
11327 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011328 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011329
11330 switch (Ord) {
11331 case NotAtomic:
11332 case Unordered:
11333 llvm_unreachable("Invalid fence: unordered/non-atomic");
11334 case Monotonic:
11335 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011336 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011337 case SequentiallyConsistent:
11338 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011339 return nullptr; // Nothing to do
11340 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011341 case Release:
11342 case AcquireRelease:
11343 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011344 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011345 // FIXME: add a comment with a link to documentation justifying this.
11346 else
Robin Morissetdedef332014-09-23 20:31:14 +000011347 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011348 }
Robin Morissetdedef332014-09-23 20:31:14 +000011349 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011350}
11351
Robin Morissetdedef332014-09-23 20:31:14 +000011352Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011353 AtomicOrdering Ord, bool IsStore,
11354 bool IsLoad) const {
11355 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011356 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011357
11358 switch (Ord) {
11359 case NotAtomic:
11360 case Unordered:
11361 llvm_unreachable("Invalid fence: unordered/not-atomic");
11362 case Monotonic:
11363 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011364 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011365 case Acquire:
11366 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011367 case SequentiallyConsistent:
11368 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011369 }
Robin Morissetdedef332014-09-23 20:31:14 +000011370 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011371}
11372
Robin Morisseted3d48f2014-09-03 21:29:59 +000011373// Loads and stores less than 64-bits are already atomic; ones above that
11374// are doomed anyway, so defer to the default libcall and blame the OS when
11375// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11376// anything for those.
11377bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11378 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11379 return (Size == 64) && !Subtarget->isMClass();
11380}
Tim Northover037f26f22014-04-17 18:22:47 +000011381
Robin Morisseted3d48f2014-09-03 21:29:59 +000011382// Loads and stores less than 64-bits are already atomic; ones above that
11383// are doomed anyway, so defer to the default libcall and blame the OS when
11384// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11385// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011386// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11387// guarantee, see DDI0406C ARM architecture reference manual,
11388// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011389bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11390 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11391 return (Size == 64) && !Subtarget->isMClass();
11392}
11393
11394// For the real atomic operations, we have ldrex/strex up to 32 bits,
11395// and up to 64 bits on the non-M profiles
JF Bastienf14889e2015-03-04 15:47:57 +000011396TargetLoweringBase::AtomicRMWExpansionKind
11397ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011398 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011399 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
11400 ? AtomicRMWExpansionKind::LLSC
11401 : AtomicRMWExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011402}
11403
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011404// This has so far only been implemented for MachO.
11405bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011406 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011407}
11408
Quentin Colombetc32615d2014-10-31 17:52:53 +000011409bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11410 unsigned &Cost) const {
11411 // If we do not have NEON, vector types are not natively supported.
11412 if (!Subtarget->hasNEON())
11413 return false;
11414
11415 // Floating point values and vector values map to the same register file.
11416 // Therefore, althought we could do a store extract of a vector type, this is
11417 // better to leave at float as we have more freedom in the addressing mode for
11418 // those.
11419 if (VectorTy->isFPOrFPVectorTy())
11420 return false;
11421
11422 // If the index is unknown at compile time, this is very expensive to lower
11423 // and it is not possible to combine the store with the extract.
11424 if (!isa<ConstantInt>(Idx))
11425 return false;
11426
11427 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11428 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11429 // We can do a store + vector extract on any vector that fits perfectly in a D
11430 // or Q register.
11431 if (BitWidth == 64 || BitWidth == 128) {
11432 Cost = 0;
11433 return true;
11434 }
11435 return false;
11436}
11437
Tim Northover037f26f22014-04-17 18:22:47 +000011438Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11439 AtomicOrdering Ord) const {
11440 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11441 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011442 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011443
11444 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11445 // intrinsic must return {i32, i32} and we have to recombine them into a
11446 // single i64 here.
11447 if (ValTy->getPrimitiveSizeInBits() == 64) {
11448 Intrinsic::ID Int =
11449 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11450 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11451
11452 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11453 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11454
11455 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11456 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011457 if (!Subtarget->isLittle())
11458 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011459 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11460 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11461 return Builder.CreateOr(
11462 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11463 }
11464
11465 Type *Tys[] = { Addr->getType() };
11466 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11467 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11468
11469 return Builder.CreateTruncOrBitCast(
11470 Builder.CreateCall(Ldrex, Addr),
11471 cast<PointerType>(Addr->getType())->getElementType());
11472}
11473
11474Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11475 Value *Addr,
11476 AtomicOrdering Ord) const {
11477 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011478 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011479
11480 // Since the intrinsics must have legal type, the i64 intrinsics take two
11481 // parameters: "i32, i32". We must marshal Val into the appropriate form
11482 // before the call.
11483 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11484 Intrinsic::ID Int =
11485 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11486 Function *Strex = Intrinsic::getDeclaration(M, Int);
11487 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11488
11489 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11490 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011491 if (!Subtarget->isLittle())
11492 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011493 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011494 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011495 }
11496
11497 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11498 Type *Tys[] = { Addr->getType() };
11499 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11500
David Blaikieff6409d2015-05-18 22:13:54 +000011501 return Builder.CreateCall(
11502 Strex, {Builder.CreateZExtOrBitCast(
11503 Val, Strex->getFunctionType()->getParamType(0)),
11504 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011505}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011506
Hao Liu2cd34bb2015-06-26 02:45:36 +000011507/// \brief Lower an interleaved load into a vldN intrinsic.
11508///
11509/// E.g. Lower an interleaved load (Factor = 2):
11510/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
11511/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
11512/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
11513///
11514/// Into:
11515/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
11516/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
11517/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
11518bool ARMTargetLowering::lowerInterleavedLoad(
11519 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
11520 ArrayRef<unsigned> Indices, unsigned Factor) const {
11521 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11522 "Invalid interleave factor");
11523 assert(!Shuffles.empty() && "Empty shufflevector input");
11524 assert(Shuffles.size() == Indices.size() &&
11525 "Unmatched number of shufflevectors and indices");
11526
11527 VectorType *VecTy = Shuffles[0]->getType();
11528 Type *EltTy = VecTy->getVectorElementType();
11529
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011530 const DataLayout &DL = LI->getModule()->getDataLayout();
11531 unsigned VecSize = DL.getTypeAllocSizeInBits(VecTy);
11532 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011533
11534 // Skip illegal vector types and vector types of i64/f64 element (vldN doesn't
11535 // support i64/f64 element).
11536 if ((VecSize != 64 && VecSize != 128) || EltIs64Bits)
11537 return false;
11538
11539 // A pointer vector can not be the return type of the ldN intrinsics. Need to
11540 // load integer vectors first and then convert to pointer vectors.
11541 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011542 VecTy =
11543 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000011544
11545 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
11546 Intrinsic::arm_neon_vld3,
11547 Intrinsic::arm_neon_vld4};
11548
11549 Function *VldnFunc =
11550 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], VecTy);
11551
11552 IRBuilder<> Builder(LI);
11553 SmallVector<Value *, 2> Ops;
11554
11555 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
11556 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
11557 Ops.push_back(Builder.getInt32(LI->getAlignment()));
11558
11559 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
11560
11561 // Replace uses of each shufflevector with the corresponding vector loaded
11562 // by ldN.
11563 for (unsigned i = 0; i < Shuffles.size(); i++) {
11564 ShuffleVectorInst *SV = Shuffles[i];
11565 unsigned Index = Indices[i];
11566
11567 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
11568
11569 // Convert the integer vector to pointer vector if the element is pointer.
11570 if (EltTy->isPointerTy())
11571 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
11572
11573 SV->replaceAllUsesWith(SubVec);
11574 }
11575
11576 return true;
11577}
11578
11579/// \brief Get a mask consisting of sequential integers starting from \p Start.
11580///
11581/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
11582static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
11583 unsigned NumElts) {
11584 SmallVector<Constant *, 16> Mask;
11585 for (unsigned i = 0; i < NumElts; i++)
11586 Mask.push_back(Builder.getInt32(Start + i));
11587
11588 return ConstantVector::get(Mask);
11589}
11590
11591/// \brief Lower an interleaved store into a vstN intrinsic.
11592///
11593/// E.g. Lower an interleaved store (Factor = 3):
11594/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
11595/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
11596/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
11597///
11598/// Into:
11599/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
11600/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
11601/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
11602/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
11603///
11604/// Note that the new shufflevectors will be removed and we'll only generate one
11605/// vst3 instruction in CodeGen.
11606bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
11607 ShuffleVectorInst *SVI,
11608 unsigned Factor) const {
11609 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
11610 "Invalid interleave factor");
11611
11612 VectorType *VecTy = SVI->getType();
11613 assert(VecTy->getVectorNumElements() % Factor == 0 &&
11614 "Invalid interleaved store");
11615
11616 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
11617 Type *EltTy = VecTy->getVectorElementType();
11618 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
11619
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011620 const DataLayout &DL = SI->getModule()->getDataLayout();
11621 unsigned SubVecSize = DL.getTypeAllocSizeInBits(SubVecTy);
11622 bool EltIs64Bits = DL.getTypeAllocSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000011623
11624 // Skip illegal sub vector types and vector types of i64/f64 element (vstN
11625 // doesn't support i64/f64 element).
11626 if ((SubVecSize != 64 && SubVecSize != 128) || EltIs64Bits)
11627 return false;
11628
11629 Value *Op0 = SVI->getOperand(0);
11630 Value *Op1 = SVI->getOperand(1);
11631 IRBuilder<> Builder(SI);
11632
11633 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
11634 // vectors to integer vectors.
11635 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000011636 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000011637
11638 // Convert to the corresponding integer vector.
11639 Type *IntVecTy =
11640 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
11641 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
11642 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
11643
11644 SubVecTy = VectorType::get(IntTy, NumSubElts);
11645 }
11646
11647 static Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
11648 Intrinsic::arm_neon_vst3,
11649 Intrinsic::arm_neon_vst4};
11650 Function *VstNFunc = Intrinsic::getDeclaration(
11651 SI->getModule(), StoreInts[Factor - 2], SubVecTy);
11652
11653 SmallVector<Value *, 6> Ops;
11654
11655 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
11656 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
11657
11658 // Split the shufflevector operands into sub vectors for the new vstN call.
11659 for (unsigned i = 0; i < Factor; i++)
11660 Ops.push_back(Builder.CreateShuffleVector(
11661 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
11662
11663 Ops.push_back(Builder.getInt32(SI->getAlignment()));
11664 Builder.CreateCall(VstNFunc, Ops);
11665 return true;
11666}
11667
Oliver Stannardc24f2172014-05-09 14:01:47 +000011668enum HABaseType {
11669 HA_UNKNOWN = 0,
11670 HA_FLOAT,
11671 HA_DOUBLE,
11672 HA_VECT64,
11673 HA_VECT128
11674};
11675
11676static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11677 uint64_t &Members) {
11678 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11679 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11680 uint64_t SubMembers = 0;
11681 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11682 return false;
11683 Members += SubMembers;
11684 }
11685 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11686 uint64_t SubMembers = 0;
11687 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11688 return false;
11689 Members += SubMembers * AT->getNumElements();
11690 } else if (Ty->isFloatTy()) {
11691 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11692 return false;
11693 Members = 1;
11694 Base = HA_FLOAT;
11695 } else if (Ty->isDoubleTy()) {
11696 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11697 return false;
11698 Members = 1;
11699 Base = HA_DOUBLE;
11700 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11701 Members = 1;
11702 switch (Base) {
11703 case HA_FLOAT:
11704 case HA_DOUBLE:
11705 return false;
11706 case HA_VECT64:
11707 return VT->getBitWidth() == 64;
11708 case HA_VECT128:
11709 return VT->getBitWidth() == 128;
11710 case HA_UNKNOWN:
11711 switch (VT->getBitWidth()) {
11712 case 64:
11713 Base = HA_VECT64;
11714 return true;
11715 case 128:
11716 Base = HA_VECT128;
11717 return true;
11718 default:
11719 return false;
11720 }
11721 }
11722 }
11723
11724 return (Members > 0 && Members <= 4);
11725}
11726
Tim Northovere95c5b32015-02-24 17:22:34 +000011727/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11728/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11729/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011730bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11731 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011732 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11733 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011734 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011735
11736 HABaseType Base = HA_UNKNOWN;
11737 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011738 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11739 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11740
11741 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11742 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011743}