blob: de4b7f7dfe57309ea39f6591ac60a52dbb24c935 [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"
James Molloy9abb2fa2016-09-26 07:26:24 +000040#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000042#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Instructions.h"
John Brawn0dbcd652015-03-18 12:01:59 +000045#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000046#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000047#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000048#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000049#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000050#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000051#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000052#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000053#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000055#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000056using namespace llvm;
57
Chandler Carruth84e68b22014-04-22 02:41:26 +000058#define DEBUG_TYPE "arm-isel"
59
Dale Johannesend679ff72010-06-03 21:09:53 +000060STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000061STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000062STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
James Molloy9abb2fa2016-09-26 07:26:24 +000063STATISTIC(NumConstpoolPromoted,
64 "Number of constants with their storage promoted into constant pools");
Dale Johannesend679ff72010-06-03 21:09:53 +000065
Evan Chengf128bdc2010-06-16 07:35:02 +000066static cl::opt<bool>
67ARMInterworking("arm-interworking", cl::Hidden,
68 cl::desc("Enable / disable ARM interworking (for debugging only)"),
69 cl::init(true));
70
James Molloy9abb2fa2016-09-26 07:26:24 +000071static cl::opt<bool> EnableConstpoolPromotion(
72 "arm-promote-constant", cl::Hidden,
73 cl::desc("Enable / disable promotion of unnamed_addr constants into "
74 "constant pools"),
75 cl::init(true));
76static cl::opt<unsigned> ConstpoolPromotionMaxSize(
77 "arm-promote-constant-max-size", cl::Hidden,
78 cl::desc("Maximum size of constant to promote into a constant pool"),
79 cl::init(64));
80static cl::opt<unsigned> ConstpoolPromotionMaxTotal(
81 "arm-promote-constant-max-total", cl::Hidden,
82 cl::desc("Maximum size of ALL constants to promote into a constant pool"),
83 cl::init(128));
84
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000085namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000086 class ARMCCState : public CCState {
87 public:
88 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000089 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
90 ParmContext PC)
91 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000092 assert(((PC == Call) || (PC == Prologue)) &&
93 "ARMCCState users must specify whether their context is call"
94 "or prologue generation.");
95 CallOrPrologue = PC;
96 }
97 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000098}
Cameron Zwarich89019782011-06-10 20:59:24 +000099
Saleem Abdulrasool92e33a32016-09-09 20:11:31 +0000100void ARMTargetLowering::InitLibcallCallingConvs() {
101 // The builtins on ARM always use AAPCS, irrespective of wheter C is AAPCS or
102 // AAPCS_VFP.
103 for (const auto LC : {
104 RTLIB::SHL_I16,
105 RTLIB::SHL_I32,
106 RTLIB::SHL_I64,
107 RTLIB::SHL_I128,
108 RTLIB::SRL_I16,
109 RTLIB::SRL_I32,
110 RTLIB::SRL_I64,
111 RTLIB::SRL_I128,
112 RTLIB::SRA_I16,
113 RTLIB::SRA_I32,
114 RTLIB::SRA_I64,
115 RTLIB::SRA_I128,
116 RTLIB::MUL_I8,
117 RTLIB::MUL_I16,
118 RTLIB::MUL_I32,
119 RTLIB::MUL_I64,
120 RTLIB::MUL_I128,
121 RTLIB::MULO_I32,
122 RTLIB::MULO_I64,
123 RTLIB::MULO_I128,
124 RTLIB::SDIV_I8,
125 RTLIB::SDIV_I16,
126 RTLIB::SDIV_I32,
127 RTLIB::SDIV_I64,
128 RTLIB::SDIV_I128,
129 RTLIB::UDIV_I8,
130 RTLIB::UDIV_I16,
131 RTLIB::UDIV_I32,
132 RTLIB::UDIV_I64,
133 RTLIB::UDIV_I128,
134 RTLIB::SREM_I8,
135 RTLIB::SREM_I16,
136 RTLIB::SREM_I32,
137 RTLIB::SREM_I64,
138 RTLIB::SREM_I128,
139 RTLIB::UREM_I8,
140 RTLIB::UREM_I16,
141 RTLIB::UREM_I32,
142 RTLIB::UREM_I64,
143 RTLIB::UREM_I128,
144 RTLIB::SDIVREM_I8,
145 RTLIB::SDIVREM_I16,
146 RTLIB::SDIVREM_I32,
147 RTLIB::SDIVREM_I64,
148 RTLIB::SDIVREM_I128,
149 RTLIB::UDIVREM_I8,
150 RTLIB::UDIVREM_I16,
151 RTLIB::UDIVREM_I32,
152 RTLIB::UDIVREM_I64,
153 RTLIB::UDIVREM_I128,
154 RTLIB::NEG_I32,
155 RTLIB::NEG_I64,
156 RTLIB::ADD_F32,
157 RTLIB::ADD_F64,
158 RTLIB::ADD_F80,
159 RTLIB::ADD_F128,
160 RTLIB::SUB_F32,
161 RTLIB::SUB_F64,
162 RTLIB::SUB_F80,
163 RTLIB::SUB_F128,
164 RTLIB::MUL_F32,
165 RTLIB::MUL_F64,
166 RTLIB::MUL_F80,
167 RTLIB::MUL_F128,
168 RTLIB::DIV_F32,
169 RTLIB::DIV_F64,
170 RTLIB::DIV_F80,
171 RTLIB::DIV_F128,
172 RTLIB::POWI_F32,
173 RTLIB::POWI_F64,
174 RTLIB::POWI_F80,
175 RTLIB::POWI_F128,
176 RTLIB::FPEXT_F64_F128,
177 RTLIB::FPEXT_F32_F128,
178 RTLIB::FPEXT_F32_F64,
179 RTLIB::FPEXT_F16_F32,
180 RTLIB::FPROUND_F32_F16,
181 RTLIB::FPROUND_F64_F16,
182 RTLIB::FPROUND_F80_F16,
183 RTLIB::FPROUND_F128_F16,
184 RTLIB::FPROUND_F64_F32,
185 RTLIB::FPROUND_F80_F32,
186 RTLIB::FPROUND_F128_F32,
187 RTLIB::FPROUND_F80_F64,
188 RTLIB::FPROUND_F128_F64,
189 RTLIB::FPTOSINT_F32_I32,
190 RTLIB::FPTOSINT_F32_I64,
191 RTLIB::FPTOSINT_F32_I128,
192 RTLIB::FPTOSINT_F64_I32,
193 RTLIB::FPTOSINT_F64_I64,
194 RTLIB::FPTOSINT_F64_I128,
195 RTLIB::FPTOSINT_F80_I32,
196 RTLIB::FPTOSINT_F80_I64,
197 RTLIB::FPTOSINT_F80_I128,
198 RTLIB::FPTOSINT_F128_I32,
199 RTLIB::FPTOSINT_F128_I64,
200 RTLIB::FPTOSINT_F128_I128,
201 RTLIB::FPTOUINT_F32_I32,
202 RTLIB::FPTOUINT_F32_I64,
203 RTLIB::FPTOUINT_F32_I128,
204 RTLIB::FPTOUINT_F64_I32,
205 RTLIB::FPTOUINT_F64_I64,
206 RTLIB::FPTOUINT_F64_I128,
207 RTLIB::FPTOUINT_F80_I32,
208 RTLIB::FPTOUINT_F80_I64,
209 RTLIB::FPTOUINT_F80_I128,
210 RTLIB::FPTOUINT_F128_I32,
211 RTLIB::FPTOUINT_F128_I64,
212 RTLIB::FPTOUINT_F128_I128,
213 RTLIB::SINTTOFP_I32_F32,
214 RTLIB::SINTTOFP_I32_F64,
215 RTLIB::SINTTOFP_I32_F80,
216 RTLIB::SINTTOFP_I32_F128,
217 RTLIB::SINTTOFP_I64_F32,
218 RTLIB::SINTTOFP_I64_F64,
219 RTLIB::SINTTOFP_I64_F80,
220 RTLIB::SINTTOFP_I64_F128,
221 RTLIB::SINTTOFP_I128_F32,
222 RTLIB::SINTTOFP_I128_F64,
223 RTLIB::SINTTOFP_I128_F80,
224 RTLIB::SINTTOFP_I128_F128,
225 RTLIB::UINTTOFP_I32_F32,
226 RTLIB::UINTTOFP_I32_F64,
227 RTLIB::UINTTOFP_I32_F80,
228 RTLIB::UINTTOFP_I32_F128,
229 RTLIB::UINTTOFP_I64_F32,
230 RTLIB::UINTTOFP_I64_F64,
231 RTLIB::UINTTOFP_I64_F80,
232 RTLIB::UINTTOFP_I64_F128,
233 RTLIB::UINTTOFP_I128_F32,
234 RTLIB::UINTTOFP_I128_F64,
235 RTLIB::UINTTOFP_I128_F80,
236 RTLIB::UINTTOFP_I128_F128,
237 RTLIB::OEQ_F32,
238 RTLIB::OEQ_F64,
239 RTLIB::OEQ_F128,
240 RTLIB::UNE_F32,
241 RTLIB::UNE_F64,
242 RTLIB::UNE_F128,
243 RTLIB::OGE_F32,
244 RTLIB::OGE_F64,
245 RTLIB::OGE_F128,
246 RTLIB::OLT_F32,
247 RTLIB::OLT_F64,
248 RTLIB::OLT_F128,
249 RTLIB::OLE_F32,
250 RTLIB::OLE_F64,
251 RTLIB::OLE_F128,
252 RTLIB::OGT_F32,
253 RTLIB::OGT_F64,
254 RTLIB::OGT_F128,
255 RTLIB::UO_F32,
256 RTLIB::UO_F64,
257 RTLIB::UO_F128,
258 RTLIB::O_F32,
259 RTLIB::O_F64,
260 RTLIB::O_F128,
261 })
262 setLibcallCallingConv(LC, CallingConv::ARM_AAPCS);
263}
264
Stuart Hastings45fe3c32011-04-20 16:47:52 +0000265// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +0000266static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +0000267 ARM::R0, ARM::R1, ARM::R2, ARM::R3
268};
269
Craig Topper4fa625f2012-08-12 03:16:37 +0000270void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
271 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +0000272 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000273 setOperationAction(ISD::LOAD, VT, Promote);
274 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000275
Craig Topper4fa625f2012-08-12 03:16:37 +0000276 setOperationAction(ISD::STORE, VT, Promote);
277 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000278 }
279
Craig Topper4fa625f2012-08-12 03:16:37 +0000280 MVT ElemTy = VT.getVectorElementType();
Eli Friedmanc0a717b2016-10-18 21:03:40 +0000281 if (ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000282 setOperationAction(ISD::SETCC, VT, Custom);
283 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
284 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000285 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000286 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
287 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
288 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
289 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000290 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000291 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
292 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
293 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
294 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000295 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000296 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
297 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
298 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
299 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
300 setOperationAction(ISD::SELECT, VT, Expand);
301 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000302 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000303 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000304 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000305 setOperationAction(ISD::SHL, VT, Custom);
306 setOperationAction(ISD::SRA, VT, Custom);
307 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000308 }
309
310 // Promote all bit-wise operations.
311 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000312 setOperationAction(ISD::AND, VT, Promote);
313 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
314 setOperationAction(ISD::OR, VT, Promote);
315 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
316 setOperationAction(ISD::XOR, VT, Promote);
317 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000318 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000319
320 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000321 setOperationAction(ISD::SDIV, VT, Expand);
322 setOperationAction(ISD::UDIV, VT, Expand);
323 setOperationAction(ISD::FDIV, VT, Expand);
324 setOperationAction(ISD::SREM, VT, Expand);
325 setOperationAction(ISD::UREM, VT, Expand);
326 setOperationAction(ISD::FREM, VT, Expand);
James Molloya6702e22015-07-17 17:10:55 +0000327
Silviu Barangaad1b19f2015-08-19 14:11:27 +0000328 if (!VT.isFloatingPoint() &&
329 VT != MVT::v2i64 && VT != MVT::v1i64)
330 for (unsigned Opcode : {ISD::SMIN, ISD::SMAX, ISD::UMIN, ISD::UMAX})
331 setOperationAction(Opcode, VT, Legal);
Bob Wilson2e076c42009-06-22 23:27:02 +0000332}
333
Craig Topper4fa625f2012-08-12 03:16:37 +0000334void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000335 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000336 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000337}
338
Craig Topper4fa625f2012-08-12 03:16:37 +0000339void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000340 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000341 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000342}
343
Eric Christopher1889fdc2015-01-29 00:19:39 +0000344ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
345 const ARMSubtarget &STI)
346 : TargetLowering(TM), Subtarget(&STI) {
347 RegInfo = Subtarget->getRegisterInfo();
348 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000349
Duncan Sandsf2641e12011-09-06 19:07:46 +0000350 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
351
Saleem Abdulrasool92e33a32016-09-09 20:11:31 +0000352 InitLibcallCallingConvs();
353
Tim Northoverd6a729b2014-01-06 14:28:05 +0000354 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000355 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000356 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000357 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000358 static const struct {
359 const RTLIB::Libcall Op;
360 const char * const Name;
361 const ISD::CondCode Cond;
362 } LibraryCalls[] = {
363 // Single-precision floating-point arithmetic.
364 { RTLIB::ADD_F32, "__addsf3vfp", ISD::SETCC_INVALID },
365 { RTLIB::SUB_F32, "__subsf3vfp", ISD::SETCC_INVALID },
366 { RTLIB::MUL_F32, "__mulsf3vfp", ISD::SETCC_INVALID },
367 { RTLIB::DIV_F32, "__divsf3vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000368
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000369 // Double-precision floating-point arithmetic.
370 { RTLIB::ADD_F64, "__adddf3vfp", ISD::SETCC_INVALID },
371 { RTLIB::SUB_F64, "__subdf3vfp", ISD::SETCC_INVALID },
372 { RTLIB::MUL_F64, "__muldf3vfp", ISD::SETCC_INVALID },
373 { RTLIB::DIV_F64, "__divdf3vfp", ISD::SETCC_INVALID },
Evan Cheng143576d2007-01-31 09:30:58 +0000374
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000375 // Single-precision comparisons.
376 { RTLIB::OEQ_F32, "__eqsf2vfp", ISD::SETNE },
377 { RTLIB::UNE_F32, "__nesf2vfp", ISD::SETNE },
378 { RTLIB::OLT_F32, "__ltsf2vfp", ISD::SETNE },
379 { RTLIB::OLE_F32, "__lesf2vfp", ISD::SETNE },
380 { RTLIB::OGE_F32, "__gesf2vfp", ISD::SETNE },
381 { RTLIB::OGT_F32, "__gtsf2vfp", ISD::SETNE },
382 { RTLIB::UO_F32, "__unordsf2vfp", ISD::SETNE },
383 { RTLIB::O_F32, "__unordsf2vfp", ISD::SETEQ },
Evan Cheng10043e22007-01-19 07:51:42 +0000384
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000385 // Double-precision comparisons.
386 { RTLIB::OEQ_F64, "__eqdf2vfp", ISD::SETNE },
387 { RTLIB::UNE_F64, "__nedf2vfp", ISD::SETNE },
388 { RTLIB::OLT_F64, "__ltdf2vfp", ISD::SETNE },
389 { RTLIB::OLE_F64, "__ledf2vfp", ISD::SETNE },
390 { RTLIB::OGE_F64, "__gedf2vfp", ISD::SETNE },
391 { RTLIB::OGT_F64, "__gtdf2vfp", ISD::SETNE },
392 { RTLIB::UO_F64, "__unorddf2vfp", ISD::SETNE },
393 { RTLIB::O_F64, "__unorddf2vfp", ISD::SETEQ },
Evan Cheng143576d2007-01-31 09:30:58 +0000394
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000395 // Floating-point to integer conversions.
396 // i64 conversions are done via library routines even when generating VFP
397 // instructions, so use the same ones.
398 { RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp", ISD::SETCC_INVALID },
399 { RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp", ISD::SETCC_INVALID },
400 { RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp", ISD::SETCC_INVALID },
401 { RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000402
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000403 // Conversions between floating types.
404 { RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp", ISD::SETCC_INVALID },
405 { RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp", ISD::SETCC_INVALID },
Evan Cheng10043e22007-01-19 07:51:42 +0000406
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000407 // Integer to floating-point conversions.
408 // i64 conversions are done via library routines even when generating VFP
409 // instructions, so use the same ones.
410 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
411 // e.g., __floatunsidf vs. __floatunssidfvfp.
412 { RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp", ISD::SETCC_INVALID },
413 { RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp", ISD::SETCC_INVALID },
414 { RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp", ISD::SETCC_INVALID },
415 { RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp", ISD::SETCC_INVALID },
416 };
Evan Cheng10043e22007-01-19 07:51:42 +0000417
Saleem Abdulrasool67697a72015-08-04 03:57:52 +0000418 for (const auto &LC : LibraryCalls) {
419 setLibcallName(LC.Op, LC.Name);
420 if (LC.Cond != ISD::SETCC_INVALID)
421 setCmpLibcallCC(LC.Op, LC.Cond);
422 }
Evan Chengc9f22fd12007-04-27 08:15:43 +0000423 }
Tim Northover8b403662015-10-28 22:51:16 +0000424
425 // Set the correct calling convention for ARMv7k WatchOS. It's just
426 // AAPCS_VFP for functions as simple as libcalls.
Tim Northover042a6c12016-01-27 19:32:29 +0000427 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +0000428 for (int i = 0; i < RTLIB::UNKNOWN_LIBCALL; ++i)
429 setLibcallCallingConv((RTLIB::Libcall)i, CallingConv::ARM_AAPCS_VFP);
430 }
Evan Cheng10043e22007-01-19 07:51:42 +0000431 }
432
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000433 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000434 setLibcallName(RTLIB::SHL_I128, nullptr);
435 setLibcallName(RTLIB::SRL_I128, nullptr);
436 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000437
Renato Golin6d435f12015-11-09 12:40:30 +0000438 // RTLIB
439 if (Subtarget->isAAPCS_ABI() &&
440 (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
Rafael Espindolaa895a0c2016-06-24 21:14:33 +0000441 Subtarget->isTargetMuslAEABI() || Subtarget->isTargetAndroid())) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000442 static const struct {
443 const RTLIB::Libcall Op;
444 const char * const Name;
445 const CallingConv::ID CC;
446 const ISD::CondCode Cond;
447 } LibraryCalls[] = {
448 // Double-precision floating-point arithmetic helper functions
449 // RTABI chapter 4.1.2, Table 2
450 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
451 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
452 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
453 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000454
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000455 // Double-precision floating-point comparison helper functions
456 // RTABI chapter 4.1.2, Table 3
457 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
458 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
459 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
460 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
461 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
462 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
463 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
464 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000465
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000466 // Single-precision floating-point arithmetic helper functions
467 // RTABI chapter 4.1.2, Table 4
468 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
469 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
470 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
471 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000472
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000473 // Single-precision floating-point comparison helper functions
474 // RTABI chapter 4.1.2, Table 5
475 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
476 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
477 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
478 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
479 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
480 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
481 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
482 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000483
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000484 // Floating-point to integer conversions.
485 // RTABI chapter 4.1.2, Table 6
486 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
487 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
488 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
489 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
490 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
491 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
492 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
493 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000494
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000495 // Conversions between floating types.
496 // RTABI chapter 4.1.2, Table 7
497 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000498 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000499 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000500
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000501 // Integer to floating-point conversions.
502 // RTABI chapter 4.1.2, Table 8
503 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
504 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
505 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
506 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
507 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
508 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
509 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
510 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000511
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000512 // Long long helper functions
513 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000514 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
515 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
516 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
517 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000518
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000519 // Integer division functions
520 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000521 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
522 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
523 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
524 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
525 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
526 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
527 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
528 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000529 };
530
531 for (const auto &LC : LibraryCalls) {
532 setLibcallName(LC.Op, LC.Name);
533 setLibcallCallingConv(LC.Op, LC.CC);
534 if (LC.Cond != ISD::SETCC_INVALID)
535 setCmpLibcallCC(LC.Op, LC.Cond);
536 }
Renato Golin6d435f12015-11-09 12:40:30 +0000537
538 // EABI dependent RTLIB
539 if (TM.Options.EABIVersion == EABI::EABI4 ||
540 TM.Options.EABIVersion == EABI::EABI5) {
541 static const struct {
542 const RTLIB::Libcall Op;
543 const char *const Name;
544 const CallingConv::ID CC;
545 const ISD::CondCode Cond;
546 } MemOpsLibraryCalls[] = {
547 // Memory operations
548 // RTABI chapter 4.3.4
549 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
550 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
551 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
552 };
553
554 for (const auto &LC : MemOpsLibraryCalls) {
555 setLibcallName(LC.Op, LC.Name);
556 setLibcallCallingConv(LC.Op, LC.CC);
557 if (LC.Cond != ISD::SETCC_INVALID)
558 setCmpLibcallCC(LC.Op, LC.Cond);
559 }
560 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000561 }
562
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000563 if (Subtarget->isTargetWindows()) {
564 static const struct {
565 const RTLIB::Libcall Op;
566 const char * const Name;
567 const CallingConv::ID CC;
568 } LibraryCalls[] = {
569 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
570 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
571 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
572 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
573 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
574 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
575 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
576 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
577 };
578
579 for (const auto &LC : LibraryCalls) {
580 setLibcallName(LC.Op, LC.Name);
581 setLibcallCallingConv(LC.Op, LC.CC);
582 }
583 }
584
Bob Wilsonbc158992011-10-07 16:59:21 +0000585 // Use divmod compiler-rt calls for iOS 5.0 and later.
Tim Northover8b403662015-10-28 22:51:16 +0000586 if (Subtarget->isTargetWatchOS() ||
587 (Subtarget->isTargetIOS() &&
588 !Subtarget->getTargetTriple().isOSVersionLT(5, 0))) {
Bob Wilsonbc158992011-10-07 16:59:21 +0000589 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
590 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
591 }
592
Ahmed Bougachaf0b46ee2016-06-24 00:08:01 +0000593 // The half <-> float conversion functions are always soft-float on
594 // non-watchos platforms, but are needed for some targets which use a
595 // hard-float calling convention by default.
596 if (!Subtarget->isTargetWatchABI()) {
597 if (Subtarget->isAAPCS_ABI()) {
598 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
599 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
600 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
601 } else {
602 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
603 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
604 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
605 }
Oliver Stannard11790b22014-08-11 09:12:32 +0000606 }
607
Oliver Stannardd3d114b2015-10-07 16:58:49 +0000608 // In EABI, these functions have an __aeabi_ prefix, but in GNUEABI they have
609 // a __gnu_ prefix (which is the default).
610 if (Subtarget->isTargetAEABI()) {
611 setLibcallName(RTLIB::FPROUND_F32_F16, "__aeabi_f2h");
612 setLibcallName(RTLIB::FPROUND_F64_F16, "__aeabi_d2h");
613 setLibcallName(RTLIB::FPEXT_F16_F32, "__aeabi_h2f");
614 }
615
David Goodwin22c2fba2009-07-08 23:10:31 +0000616 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000617 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000618 else
Craig Topperc7242e02012-04-20 07:30:17 +0000619 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000620 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000621 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000622 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000623 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000624 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000625
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000626 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000627 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000628 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000629 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
630 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
631 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
632 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000633
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000634 setOperationAction(ISD::MULHS, VT, Expand);
635 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
636 setOperationAction(ISD::MULHU, VT, Expand);
637 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000638
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000639 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000640 }
641
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000642 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000643 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000644
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000645 setOperationAction(ISD::READ_REGISTER, MVT::i64, Custom);
646 setOperationAction(ISD::WRITE_REGISTER, MVT::i64, Custom);
647
Bob Wilson2e076c42009-06-22 23:27:02 +0000648 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000649 addDRTypeForNEON(MVT::v2f32);
650 addDRTypeForNEON(MVT::v8i8);
651 addDRTypeForNEON(MVT::v4i16);
652 addDRTypeForNEON(MVT::v2i32);
653 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000654
Owen Anderson9f944592009-08-11 20:47:22 +0000655 addQRTypeForNEON(MVT::v4f32);
656 addQRTypeForNEON(MVT::v2f64);
657 addQRTypeForNEON(MVT::v16i8);
658 addQRTypeForNEON(MVT::v8i16);
659 addQRTypeForNEON(MVT::v4i32);
660 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000661
Bob Wilson194a2512009-09-15 23:55:57 +0000662 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
663 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000664 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
665 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000666 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
667 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
668 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000669 // FIXME: Code duplication: FDIV and FREM are expanded always, see
670 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000671 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
672 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000673 // FIXME: Create unittest.
674 // In another words, find a way when "copysign" appears in DAG with vector
675 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000676 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000677 // FIXME: Code duplication: SETCC has custom operation action, see
678 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000679 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000680 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000681 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
682 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
683 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
684 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
685 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
686 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
687 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
688 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
689 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
690 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
691 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
692 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000693 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000694 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
695 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
696 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
697 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
698 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000699 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000700
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000701 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
702 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
703 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
704 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
705 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
706 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
707 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
708 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
709 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
710 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000711 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
712 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
713 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
714 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000715 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000716
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000717 // Mark v2f32 intrinsics.
718 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
719 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
720 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
721 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
722 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
723 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
724 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
725 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
726 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
727 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
728 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
729 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
730 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
731 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
732 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
733
Bob Wilson6cc46572009-09-16 00:32:15 +0000734 // Neon does not support some operations on v1i64 and v2i64 types.
735 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000736 // Custom handling for some quad-vector types to detect VMULL.
737 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
738 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
739 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000740 // Custom handling for some vector types to avoid expensive expansions
741 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
742 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
743 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
744 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000745 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000746 // a destination type that is wider than the source, and nor does
747 // it have a FP_TO_[SU]INT instruction with a narrower destination than
748 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000749 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
750 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000751 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
752 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000753
Eli Friedmane6385e62012-11-15 22:44:27 +0000754 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000755 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000756
Evan Chengb4eae132012-12-04 22:41:50 +0000757 // NEON does not have single instruction CTPOP for vectors with element
758 // types wider than 8-bits. However, custom lowering can leverage the
759 // v8i8/v16i8 vcnt instruction.
760 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
761 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
762 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
763 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
Benjamin Kramer569efd22016-03-31 19:42:04 +0000764 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand);
765 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand);
Evan Chengb4eae132012-12-04 22:41:50 +0000766
Craig Topperedb4a6b2016-04-26 05:04:33 +0000767 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand);
768 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand);
769
Logan Chien0a43abc2015-07-13 15:37:30 +0000770 // NEON does not have single instruction CTTZ for vectors.
771 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
772 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
773 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
774 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
775
776 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
777 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
778 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
779 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
780
781 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
782 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
783 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
784 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
785
786 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
787 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
788 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
789 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
790
Jim Grosbach5f215872013-02-27 21:31:12 +0000791 // NEON only has FMA instructions as of VFP4.
792 if (!Subtarget->hasVFP4()) {
793 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
794 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
795 }
796
Bob Wilson06fce872011-02-07 17:43:21 +0000797 setTargetDAGCombine(ISD::INTRINSIC_VOID);
798 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000799 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
800 setTargetDAGCombine(ISD::SHL);
801 setTargetDAGCombine(ISD::SRL);
802 setTargetDAGCombine(ISD::SRA);
803 setTargetDAGCombine(ISD::SIGN_EXTEND);
804 setTargetDAGCombine(ISD::ZERO_EXTEND);
805 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000806 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000807 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000808 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
809 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000810 setTargetDAGCombine(ISD::FP_TO_SINT);
811 setTargetDAGCombine(ISD::FP_TO_UINT);
812 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000813 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000814
James Molloy547d4c02012-02-20 09:24:05 +0000815 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000816 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
817 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000818 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000819 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
820 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
821 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000822 }
James Molloy547d4c02012-02-20 09:24:05 +0000823 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000824 }
825
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000826 // ARM and Thumb2 support UMLAL/SMLAL.
827 if (!Subtarget->isThumb1Only())
828 setTargetDAGCombine(ISD::ADDC);
829
Oliver Stannard51b1d462014-08-21 12:50:31 +0000830 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000831 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000832 // operations, f64 is legal for the few double-precision instructions which
833 // are present However, no double-precision operations other than moves,
834 // loads and stores are provided by the hardware.
835 setOperationAction(ISD::FADD, MVT::f64, Expand);
836 setOperationAction(ISD::FSUB, MVT::f64, Expand);
837 setOperationAction(ISD::FMUL, MVT::f64, Expand);
838 setOperationAction(ISD::FMA, MVT::f64, Expand);
839 setOperationAction(ISD::FDIV, MVT::f64, Expand);
840 setOperationAction(ISD::FREM, MVT::f64, Expand);
841 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
842 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
843 setOperationAction(ISD::FNEG, MVT::f64, Expand);
844 setOperationAction(ISD::FABS, MVT::f64, Expand);
845 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
846 setOperationAction(ISD::FSIN, MVT::f64, Expand);
847 setOperationAction(ISD::FCOS, MVT::f64, Expand);
848 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
849 setOperationAction(ISD::FPOW, MVT::f64, Expand);
850 setOperationAction(ISD::FLOG, MVT::f64, Expand);
851 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
852 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
853 setOperationAction(ISD::FEXP, MVT::f64, Expand);
854 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
855 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
856 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
857 setOperationAction(ISD::FRINT, MVT::f64, Expand);
858 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
859 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000860 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
861 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
862 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
863 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
864 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
865 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000866 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
867 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
868 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000869
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000870 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000871
Tim Northover4e80b582014-07-18 13:01:19 +0000872 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000873 for (MVT VT : MVT::fp_valuetypes()) {
874 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
875 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
876 }
Tim Northover4e80b582014-07-18 13:01:19 +0000877
878 // ... or truncating stores
879 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
880 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
881 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000882
Duncan Sands95d46ef2008-01-23 20:39:46 +0000883 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000884 for (MVT VT : MVT::integer_valuetypes())
885 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000886
Evan Cheng10043e22007-01-19 07:51:42 +0000887 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000888 if (!Subtarget->isThumb1Only()) {
889 for (unsigned im = (unsigned)ISD::PRE_INC;
890 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000891 setIndexedLoadAction(im, MVT::i1, Legal);
892 setIndexedLoadAction(im, MVT::i8, Legal);
893 setIndexedLoadAction(im, MVT::i16, Legal);
894 setIndexedLoadAction(im, MVT::i32, Legal);
895 setIndexedStoreAction(im, MVT::i1, Legal);
896 setIndexedStoreAction(im, MVT::i8, Legal);
897 setIndexedStoreAction(im, MVT::i16, Legal);
898 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000899 }
James Molloyb3326df2016-07-15 08:03:56 +0000900 } else {
901 // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}.
902 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
903 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
Evan Cheng10043e22007-01-19 07:51:42 +0000904 }
905
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000906 setOperationAction(ISD::SADDO, MVT::i32, Custom);
907 setOperationAction(ISD::UADDO, MVT::i32, Custom);
908 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
909 setOperationAction(ISD::USUBO, MVT::i32, Custom);
910
Evan Cheng10043e22007-01-19 07:51:42 +0000911 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000912 setOperationAction(ISD::MUL, MVT::i64, Expand);
913 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000914 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000915 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
916 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000917 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000918 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000919 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000920 setOperationAction(ISD::MULHS, MVT::i32, Expand);
921
Jim Grosbach5d994042009-10-31 19:38:01 +0000922 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000923 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000924 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000925 setOperationAction(ISD::SRL, MVT::i64, Custom);
926 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000927
Evan Chenge8916542011-08-30 01:34:54 +0000928 if (!Subtarget->isThumb1Only()) {
929 // FIXME: We should do this for Thumb1 as well.
930 setOperationAction(ISD::ADDC, MVT::i32, Custom);
931 setOperationAction(ISD::ADDE, MVT::i32, Custom);
932 setOperationAction(ISD::SUBC, MVT::i32, Custom);
933 setOperationAction(ISD::SUBE, MVT::i32, Custom);
934 }
935
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000936 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000937 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
938
Evan Cheng10043e22007-01-19 07:51:42 +0000939 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000940 setOperationAction(ISD::ROTL, MVT::i32, Expand);
941 for (MVT VT : MVT::vector_valuetypes()) {
942 setOperationAction(ISD::ROTL, VT, Expand);
943 setOperationAction(ISD::ROTR, VT, Expand);
944 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000945 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000946 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000947 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000948 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000949
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000950 // @llvm.readcyclecounter requires the Performance Monitors extension.
951 // Default to the 0 expansion on unsupported platforms.
952 // FIXME: Technically there are older ARM CPUs that have
953 // implementation-specific ways of obtaining this information.
954 if (Subtarget->hasPerfMon())
955 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000956
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000957 // Only ARMv6 has BSWAP.
958 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000959 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000960
Bradley Smith519563e2016-01-15 10:25:35 +0000961 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide()
962 : Subtarget->hasDivideInARMMode();
963 if (!hasDivide) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000964 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000965 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
966 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000967 }
Renato Golin87610692013-07-16 09:32:17 +0000968
Saleem Abdulrasool071a0992016-03-17 14:10:49 +0000969 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
970 setOperationAction(ISD::SDIV, MVT::i32, Custom);
971 setOperationAction(ISD::UDIV, MVT::i32, Custom);
972
973 setOperationAction(ISD::SDIV, MVT::i64, Custom);
974 setOperationAction(ISD::UDIV, MVT::i64, Custom);
975 }
976
Chad Rosierad7c9102014-08-23 18:29:43 +0000977 setOperationAction(ISD::SREM, MVT::i32, Expand);
978 setOperationAction(ISD::UREM, MVT::i32, Expand);
979 // Register based DivRem for AEABI (RTABI 4.2)
Renato Golin6027dd38e2016-02-03 16:10:54 +0000980 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
Martin Storsjo04864f42016-10-07 13:28:53 +0000981 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
982 Subtarget->isTargetWindows()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000983 setOperationAction(ISD::SREM, MVT::i64, Custom);
984 setOperationAction(ISD::UREM, MVT::i64, Custom);
Diana Picus774d1572016-07-18 06:48:25 +0000985 HasStandaloneRem = false;
Scott Douglassbdef6042015-08-24 09:17:18 +0000986
Martin Storsjo04864f42016-10-07 13:28:53 +0000987 for (const auto &LC :
988 {RTLIB::SDIVREM_I8, RTLIB::SDIVREM_I16, RTLIB::SDIVREM_I32})
989 setLibcallName(LC, Subtarget->isTargetWindows() ? "__rt_sdiv"
990 : "__aeabi_idivmod");
991 setLibcallName(RTLIB::SDIVREM_I64, Subtarget->isTargetWindows()
992 ? "__rt_sdiv64"
993 : "__aeabi_ldivmod");
994 for (const auto &LC :
995 {RTLIB::UDIVREM_I8, RTLIB::UDIVREM_I16, RTLIB::UDIVREM_I32})
996 setLibcallName(LC, Subtarget->isTargetWindows() ? "__rt_udiv"
997 : "__aeabi_uidivmod");
998 setLibcallName(RTLIB::UDIVREM_I64, Subtarget->isTargetWindows()
999 ? "__rt_udiv64"
1000 : "__aeabi_uldivmod");
Chad Rosierad7c9102014-08-23 18:29:43 +00001001
1002 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
1003 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
1004 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
1005 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
1006 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
1007 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
1008 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
1009 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
1010
1011 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
1012 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Renato Golin175c6d62016-03-04 19:19:36 +00001013 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
1014 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Chad Rosierad7c9102014-08-23 18:29:43 +00001015 } else {
Renato Golin87610692013-07-16 09:32:17 +00001016 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1017 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1018 }
Bob Wilson7117a912009-03-20 22:42:55 +00001019
Saleem Abdulrasool804e12e2016-11-06 19:46:54 +00001020 if (Subtarget->isTargetWindows() && Subtarget->getTargetTriple().isOSMSVCRT())
1021 for (auto &VT : {MVT::f32, MVT::f64})
1022 setOperationAction(ISD::FPOWI, VT, Custom);
1023
Owen Anderson9f944592009-08-11 20:47:22 +00001024 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1025 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +00001026 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +00001027 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +00001028
Evan Cheng74d92c12011-04-08 21:37:21 +00001029 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +00001030
Evan Cheng10043e22007-01-19 07:51:42 +00001031 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001032 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1033 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1034 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
1035 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1036 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1037 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +00001038
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001039 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
1040 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1041 else
1042 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
1043
Evan Cheng6e809de2010-08-11 06:22:01 +00001044 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
James Y Knighte6a46462016-04-01 19:33:19 +00001045 // the default expansion.
James Y Knightf44fc522016-03-16 22:12:04 +00001046 InsertFencesForAtomic = false;
James Y Knighte6a46462016-04-01 19:33:19 +00001047 if (Subtarget->hasAnyDataBarrier() &&
1048 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) {
Tim Northoverc882eb02014-04-03 11:44:58 +00001049 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
1050 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +00001051 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverb629c772016-04-18 21:48:55 +00001052 if (!Subtarget->isThumb() || !Subtarget->isMClass())
1053 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +00001054
Amara Emersonb4ad2f32013-09-26 12:22:36 +00001055 // On v8, we have particularly efficient implementations of atomic fences
1056 // if they can be combined with nearby atomic loads and stores.
Tim Northoverb629c772016-04-18 21:48:55 +00001057 if (!Subtarget->hasV8Ops() || getTargetMachine().getOptLevel() == 0) {
Robin Morissetd18cda62014-08-15 22:17:28 +00001058 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
James Y Knightf44fc522016-03-16 22:12:04 +00001059 InsertFencesForAtomic = true;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00001060 }
Jim Grosbach6860bb72010-06-18 22:35:32 +00001061 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +00001062 // If there's anything we can use as a barrier, go through custom lowering
1063 // for ATOMIC_FENCE.
Weiming Zhao962eaae2016-11-03 21:49:08 +00001064 // If target has DMB in thumb, Fences can be inserted.
1065 if (Subtarget->hasDataBarrier())
1066 InsertFencesForAtomic = true;
1067
Tim Northoverc7ea8042013-10-25 09:30:24 +00001068 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
1069 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
1070
Jim Grosbach6860bb72010-06-18 22:35:32 +00001071 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +00001072 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +00001073 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001074 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001075 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001076 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001077 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001078 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001079 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001080 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001081 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001082 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001083 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +00001084 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
1085 // Unordered/Monotonic case.
Weiming Zhao962eaae2016-11-03 21:49:08 +00001086 if (!InsertFencesForAtomic) {
1087 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1088 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1089 }
Jim Grosbach6860bb72010-06-18 22:35:32 +00001090 }
Evan Cheng10043e22007-01-19 07:51:42 +00001091
Evan Cheng21acf9f2010-11-04 05:19:35 +00001092 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +00001093
Eli Friedman8cfa7712010-06-26 04:36:50 +00001094 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
1095 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001096 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1097 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +00001098 }
Owen Anderson9f944592009-08-11 20:47:22 +00001099 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +00001100
Eric Christopher824f42f2015-05-12 01:26:05 +00001101 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001102 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +00001103 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001104 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +00001105 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +00001106 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
1107 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00001108
1109 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +00001110 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +00001111 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1112 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1113 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +00001114 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +00001115 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00001116
Owen Anderson9f944592009-08-11 20:47:22 +00001117 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1118 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1119 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +00001120 setOperationAction(ISD::SELECT, MVT::i32, Custom);
1121 setOperationAction(ISD::SELECT, MVT::f32, Custom);
1122 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +00001123 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1124 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1125 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +00001126
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00001127 // Thumb-1 cannot currently select ARMISD::SUBE.
1128 if (!Subtarget->isThumb1Only())
1129 setOperationAction(ISD::SETCCE, MVT::i32, Custom);
1130
Owen Anderson9f944592009-08-11 20:47:22 +00001131 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1132 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1133 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1134 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
1135 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +00001136
Dan Gohman482732a2007-10-11 23:21:31 +00001137 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +00001138 setOperationAction(ISD::FSIN, MVT::f64, Expand);
1139 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1140 setOperationAction(ISD::FCOS, MVT::f32, Expand);
1141 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001142 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1143 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001144 setOperationAction(ISD::FREM, MVT::f64, Expand);
1145 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +00001146 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001147 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001148 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
1149 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +00001150 }
Owen Anderson9f944592009-08-11 20:47:22 +00001151 setOperationAction(ISD::FPOW, MVT::f64, Expand);
1152 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +00001153
Evan Chengd0007f32012-04-10 21:40:28 +00001154 if (!Subtarget->hasVFP4()) {
1155 setOperationAction(ISD::FMA, MVT::f64, Expand);
1156 setOperationAction(ISD::FMA, MVT::f32, Expand);
1157 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001158
Anton Korobeynikovd7fece32010-03-14 18:42:31 +00001159 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +00001160 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001161 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
1162 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +00001163 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
1164 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
1165 }
1166
1167 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +00001168 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +00001169 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
1170 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +00001171 }
Evan Cheng86e476b2008-04-01 01:50:16 +00001172 }
Jim Grosbach1a597112014-04-03 23:43:18 +00001173
Bob Wilsone7dde0c2013-11-03 06:14:38 +00001174 // Combine sin / cos into one node or libcall if possible.
1175 if (Subtarget->hasSinCos()) {
1176 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1177 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover042a6c12016-01-27 19:32:29 +00001178 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +00001179 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
1180 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
1181 }
1182 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +00001183 // For iOS, we don't want to the normal expansion of a libcall to
1184 // sincos. We want to issue a libcall to __sincos_stret.
1185 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1186 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1187 }
1188 }
Evan Cheng10043e22007-01-19 07:51:42 +00001189
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001190 // FP-ARMv8 implements a lot of rounding-like FP operations.
1191 if (Subtarget->hasFPARMv8()) {
1192 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
1193 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
1194 setOperationAction(ISD::FROUND, MVT::f32, Legal);
1195 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
1196 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
1197 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001198 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1199 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +00001200 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
1201 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
1202 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
1203 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
1204
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001205 if (!Subtarget->isFPOnlySP()) {
1206 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1207 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1208 setOperationAction(ISD::FROUND, MVT::f64, Legal);
1209 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1210 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1211 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001212 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1213 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001214 }
1215 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001216
James Molloy974838f2015-08-17 19:37:12 +00001217 if (Subtarget->hasNEON()) {
1218 // vmin and vmax aren't available in a scalar form, so we use
1219 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001220 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1221 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001222 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1223 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1224 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1225 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1226 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001227
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001228 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001229 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001230 setTargetDAGCombine(ISD::ADD);
1231 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001232 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001233 setTargetDAGCombine(ISD::AND);
1234 setTargetDAGCombine(ISD::OR);
1235 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001236
Evan Chengf258a152012-02-23 02:58:19 +00001237 if (Subtarget->hasV6Ops())
1238 setTargetDAGCombine(ISD::SRL);
1239
Evan Cheng10043e22007-01-19 07:51:42 +00001240 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001241
Eric Christopher824f42f2015-05-12 01:26:05 +00001242 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001243 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001244 setSchedulingPreference(Sched::RegPressure);
1245 else
1246 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001247
Evan Cheng3ae2b792011-01-06 06:52:41 +00001248 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001249 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001250 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001251 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001252 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001253 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001254 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001255
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001256 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1257 // are at least 4 bytes aligned.
1258 setMinStackArgumentAlignment(4);
1259
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001260 // Prefer likely predicted branches to selects on out-of-order cores.
Junmo Park453f4aa2016-02-23 09:56:58 +00001261 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001262
Eli Friedman2518f832011-05-06 20:34:06 +00001263 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001264}
1265
Eric Christopher824f42f2015-05-12 01:26:05 +00001266bool ARMTargetLowering::useSoftFloat() const {
1267 return Subtarget->useSoftFloat();
1268}
1269
Andrew Trick43f25632011-01-19 02:35:27 +00001270// FIXME: It might make sense to define the representative register class as the
1271// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1272// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1273// SPR's representative would be DPR_VFP2. This should work well if register
1274// pressure tracking were modified such that a register use would increment the
1275// pressure of the register class's representative and all of it's super
1276// classes' representatives transitively. We have not implemented this because
1277// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001278// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001279// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001280std::pair<const TargetRegisterClass *, uint8_t>
1281ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1282 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001283 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001284 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001285 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001286 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001287 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001288 // Use DPR as representative register class for all floating point
1289 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1290 // the cost is 1 for both f32 and f64.
1291 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001292 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001293 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001294 // When NEON is used for SP, only half of the register file is available
1295 // because operations that define both SP and DP results will be constrained
1296 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1297 // coalescing by double-counting the SP regs. See the FIXME above.
1298 if (Subtarget->useNEONForSinglePrecisionFP())
1299 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001300 break;
1301 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1302 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001303 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001304 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001305 break;
1306 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001307 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001308 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001309 break;
1310 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001311 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001312 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001313 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001314 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001315 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001316}
1317
Evan Cheng10043e22007-01-19 07:51:42 +00001318const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001319 switch ((ARMISD::NodeType)Opcode) {
1320 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001321 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001322 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001323 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001324 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001325 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001326 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001327 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
Evan Cheng10043e22007-01-19 07:51:42 +00001328 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1329 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001330 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001331 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001332 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001333 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1334 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001335 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001336 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001337 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1338 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001339 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001340 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001341
Evan Cheng10043e22007-01-19 07:51:42 +00001342 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001343
Pablo Barrio7a643462016-06-23 16:53:49 +00001344 case ARMISD::SSAT: return "ARMISD::SSAT";
1345
Evan Cheng10043e22007-01-19 07:51:42 +00001346 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1347 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1348 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001349
Evan Chenge8916542011-08-30 01:34:54 +00001350 case ARMISD::ADDC: return "ARMISD::ADDC";
1351 case ARMISD::ADDE: return "ARMISD::ADDE";
1352 case ARMISD::SUBC: return "ARMISD::SUBC";
1353 case ARMISD::SUBE: return "ARMISD::SUBE";
1354
Bob Wilson22806742010-09-22 22:09:21 +00001355 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1356 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001357
Evan Chengec6d7c92009-10-28 06:55:03 +00001358 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001359 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1360 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001361
Dale Johannesend679ff72010-06-03 21:09:53 +00001362 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001363
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001364 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001365
Evan Chengb972e562009-08-07 00:34:42 +00001366 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1367
Bob Wilson7ed59712010-10-30 00:54:37 +00001368 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001369
Evan Cheng8740ee32010-11-03 06:34:55 +00001370 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1371
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001372 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001373 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001374
Bob Wilson2e076c42009-06-22 23:27:02 +00001375 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001376 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001377 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001378 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1379 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001380 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1381 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001382 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1383 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001384 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1385 case ARMISD::VTST: return "ARMISD::VTST";
1386
1387 case ARMISD::VSHL: return "ARMISD::VSHL";
1388 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1389 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001390 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1391 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1392 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1393 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1394 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1395 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1396 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1397 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1398 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1399 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1400 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1401 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001402 case ARMISD::VSLI: return "ARMISD::VSLI";
1403 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001404 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1405 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001406 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001407 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001408 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001409 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001410 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001411 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001412 case ARMISD::VREV64: return "ARMISD::VREV64";
1413 case ARMISD::VREV32: return "ARMISD::VREV32";
1414 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001415 case ARMISD::VZIP: return "ARMISD::VZIP";
1416 case ARMISD::VUZP: return "ARMISD::VUZP";
1417 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001418 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1419 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001420 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1421 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Sam Parkerd616cf02016-06-20 16:47:09 +00001422 case ARMISD::UMAAL: return "ARMISD::UMAAL";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001423 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1424 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001425 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001426 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001427 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1428 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001429 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001430 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001431 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1432 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1433 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001434 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1435 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1436 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1437 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1438 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1439 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1440 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1441 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1442 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1443 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1444 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1445 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1446 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1447 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1448 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1449 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1450 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001451 }
Matthias Braund04893f2015-05-07 21:33:59 +00001452 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001453}
1454
Mehdi Amini44ede332015-07-09 02:09:04 +00001455EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1456 EVT VT) const {
1457 if (!VT.isVector())
1458 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001459 return VT.changeVectorElementTypeToInteger();
1460}
1461
Evan Cheng4cad68e2010-05-15 02:18:07 +00001462/// getRegClassFor - Return the register class that should be used for the
1463/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001464const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001465 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1466 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1467 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001468 if (Subtarget->hasNEON()) {
1469 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001470 return &ARM::QQPRRegClass;
1471 if (VT == MVT::v8i64)
1472 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001473 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001474 return TargetLowering::getRegClassFor(VT);
1475}
1476
John Brawn0dbcd652015-03-18 12:01:59 +00001477// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1478// source/dest is aligned and the copy size is large enough. We therefore want
1479// to align such objects passed to memory intrinsics.
1480bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1481 unsigned &PrefAlign) const {
1482 if (!isa<MemIntrinsic>(CI))
1483 return false;
1484 MinSize = 8;
1485 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1486 // cycle faster than 4-byte aligned LDM.
1487 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1488 return true;
1489}
1490
Eric Christopher84bdfd82010-07-21 22:26:11 +00001491// Create a fast isel object.
1492FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001493ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1494 const TargetLibraryInfo *libInfo) const {
1495 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001496}
1497
Evan Cheng4401f882010-05-20 23:26:43 +00001498Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001499 unsigned NumVals = N->getNumValues();
1500 if (!NumVals)
1501 return Sched::RegPressure;
1502
1503 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001504 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001505 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001506 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001507 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001508 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001509 }
Evan Chengbf914992010-05-28 23:25:23 +00001510
1511 if (!N->isMachineOpcode())
1512 return Sched::RegPressure;
1513
1514 // Load are scheduled for latency even if there instruction itinerary
1515 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001516 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001517 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001518
Evan Cheng6cc775f2011-06-28 19:10:37 +00001519 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001520 return Sched::RegPressure;
1521 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001522 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001523 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001524
Evan Cheng4401f882010-05-20 23:26:43 +00001525 return Sched::RegPressure;
1526}
1527
Evan Cheng10043e22007-01-19 07:51:42 +00001528//===----------------------------------------------------------------------===//
1529// Lowering Code
1530//===----------------------------------------------------------------------===//
1531
Evan Cheng10043e22007-01-19 07:51:42 +00001532/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1533static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1534 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001535 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001536 case ISD::SETNE: return ARMCC::NE;
1537 case ISD::SETEQ: return ARMCC::EQ;
1538 case ISD::SETGT: return ARMCC::GT;
1539 case ISD::SETGE: return ARMCC::GE;
1540 case ISD::SETLT: return ARMCC::LT;
1541 case ISD::SETLE: return ARMCC::LE;
1542 case ISD::SETUGT: return ARMCC::HI;
1543 case ISD::SETUGE: return ARMCC::HS;
1544 case ISD::SETULT: return ARMCC::LO;
1545 case ISD::SETULE: return ARMCC::LS;
1546 }
1547}
1548
Bob Wilsona2e83332009-09-09 23:14:54 +00001549/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1550static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001551 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001552 CondCode2 = ARMCC::AL;
1553 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001554 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001555 case ISD::SETEQ:
1556 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1557 case ISD::SETGT:
1558 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1559 case ISD::SETGE:
1560 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1561 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001562 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001563 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1564 case ISD::SETO: CondCode = ARMCC::VC; break;
1565 case ISD::SETUO: CondCode = ARMCC::VS; break;
1566 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1567 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1568 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1569 case ISD::SETLT:
1570 case ISD::SETULT: CondCode = ARMCC::LT; break;
1571 case ISD::SETLE:
1572 case ISD::SETULE: CondCode = ARMCC::LE; break;
1573 case ISD::SETNE:
1574 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1575 }
Evan Cheng10043e22007-01-19 07:51:42 +00001576}
1577
Bob Wilsona4c22902009-04-17 19:07:39 +00001578//===----------------------------------------------------------------------===//
1579// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001580//===----------------------------------------------------------------------===//
1581
1582#include "ARMGenCallingConv.inc"
1583
Oliver Stannardc24f2172014-05-09 14:01:47 +00001584/// getEffectiveCallingConv - Get the effective calling convention, taking into
1585/// account presence of floating point hardware and calling convention
1586/// limitations, such as support for variadic functions.
1587CallingConv::ID
1588ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1589 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001590 switch (CC) {
1591 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001592 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001593 case CallingConv::ARM_AAPCS:
1594 case CallingConv::ARM_APCS:
1595 case CallingConv::GHC:
1596 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001597 case CallingConv::PreserveMost:
1598 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001599 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001600 case CallingConv::Swift:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001601 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1602 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001603 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001604 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001605 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001606 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1607 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001608 return CallingConv::ARM_AAPCS_VFP;
1609 else
1610 return CallingConv::ARM_AAPCS;
1611 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001612 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001613 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001614 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001615 return CallingConv::Fast;
1616 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001617 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001618 return CallingConv::ARM_AAPCS_VFP;
1619 else
1620 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001621 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001622}
1623
1624/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1625/// CallingConvention.
1626CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1627 bool Return,
1628 bool isVarArg) const {
1629 switch (getEffectiveCallingConv(CC, isVarArg)) {
1630 default:
1631 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001632 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001633 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001634 case CallingConv::ARM_AAPCS:
1635 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1636 case CallingConv::ARM_AAPCS_VFP:
1637 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1638 case CallingConv::Fast:
1639 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001640 case CallingConv::GHC:
1641 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001642 case CallingConv::PreserveMost:
1643 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001644 }
1645}
1646
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001647/// LowerCallResult - Lower the result values of a call into the
1648/// appropriate copies out of appropriate physical registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001649SDValue ARMTargetLowering::LowerCallResult(
1650 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1651 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1652 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
1653 SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001654
Bob Wilsona4c22902009-04-17 19:07:39 +00001655 // Assign locations to each value returned by this call.
1656 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001657 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1658 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001659 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001660 CCAssignFnForNode(CallConv, /* Return*/ true,
1661 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001662
1663 // Copy all of the result registers out of their specified physreg.
1664 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1665 CCValAssign VA = RVLocs[i];
1666
Stephen Linb8bd2322013-04-20 05:14:40 +00001667 // Pass 'this' value directly from the argument to return value, to avoid
1668 // reg unit interference
David Majnemer5d261272016-07-20 04:13:01 +00001669 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001670 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1671 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001672 InVals.push_back(ThisVal);
1673 continue;
1674 }
1675
Bob Wilson0041bd32009-04-25 00:33:20 +00001676 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001677 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001678 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001679 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001680 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001681 Chain = Lo.getValue(1);
1682 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001683 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001684 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001685 InFlag);
1686 Chain = Hi.getValue(1);
1687 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001688 if (!Subtarget->isLittle())
1689 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001690 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001691
Owen Anderson9f944592009-08-11 20:47:22 +00001692 if (VA.getLocVT() == MVT::v2f64) {
1693 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1694 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001695 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001696
1697 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001698 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001699 Chain = Lo.getValue(1);
1700 InFlag = Lo.getValue(2);
1701 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001702 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001703 Chain = Hi.getValue(1);
1704 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001705 if (!Subtarget->isLittle())
1706 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001707 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001708 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001709 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001710 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001711 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001712 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1713 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001714 Chain = Val.getValue(1);
1715 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001716 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001717
1718 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001719 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001720 case CCValAssign::Full: break;
1721 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001722 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001723 break;
1724 }
1725
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001726 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001727 }
1728
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001729 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001730}
1731
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001732/// LowerMemOpCallTo - Store the argument to the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001733SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
1734 SDValue Arg, const SDLoc &dl,
1735 SelectionDAG &DAG,
1736 const CCValAssign &VA,
1737 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001738 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001739 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001740 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1741 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001742 return DAG.getStore(
1743 Chain, dl, Arg, PtrOff,
Justin Lebar9c375812016-07-15 18:27:10 +00001744 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset));
Evan Cheng10043e22007-01-19 07:51:42 +00001745}
1746
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001747void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001748 SDValue Chain, SDValue &Arg,
1749 RegsToPassVector &RegsToPass,
1750 CCValAssign &VA, CCValAssign &NextVA,
1751 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001752 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001753 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001754
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001755 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001756 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001757 unsigned id = Subtarget->isLittle() ? 0 : 1;
1758 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001759
1760 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001761 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001762 else {
1763 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001764 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001765 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1766 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001767
Christian Pirkerb5728192014-05-08 14:06:24 +00001768 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001769 dl, DAG, NextVA,
1770 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001771 }
1772}
1773
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001774/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001775/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1776/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001777SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001778ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001779 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001780 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001781 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001782 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1783 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1784 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001785 SDValue Chain = CLI.Chain;
1786 SDValue Callee = CLI.Callee;
1787 bool &isTailCall = CLI.IsTailCall;
1788 CallingConv::ID CallConv = CLI.CallConv;
1789 bool doesNotRet = CLI.DoesNotReturn;
1790 bool isVarArg = CLI.IsVarArg;
1791
Dale Johannesend679ff72010-06-03 21:09:53 +00001792 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001793 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1794 bool isThisReturn = false;
1795 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001796 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001797
Bob Wilson8decdc42011-10-07 17:17:49 +00001798 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001799 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001800 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001801
Dale Johannesend679ff72010-06-03 21:09:53 +00001802 if (isTailCall) {
1803 // Check if it's really possible to do a tail call.
1804 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001805 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001806 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001807 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1808 report_fatal_error("failed to perform tail call elimination on a call "
1809 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001810 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1811 // detected sibcalls.
1812 if (isTailCall) {
1813 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001814 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001815 }
1816 }
Evan Cheng10043e22007-01-19 07:51:42 +00001817
Bob Wilsona4c22902009-04-17 19:07:39 +00001818 // Analyze operands of the call, assigning locations to each operand.
1819 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001820 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1821 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001822 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001823 CCAssignFnForNode(CallConv, /* Return*/ false,
1824 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001825
Bob Wilsona4c22902009-04-17 19:07:39 +00001826 // Get a count of how many bytes are to be pushed on the stack.
1827 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001828
Dale Johannesend679ff72010-06-03 21:09:53 +00001829 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001830 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001831 NumBytes = 0;
1832
Evan Cheng10043e22007-01-19 07:51:42 +00001833 // Adjust the stack pointer for the new arguments...
1834 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001835 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001836 Chain = DAG.getCALLSEQ_START(Chain,
1837 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001838
Mehdi Amini44ede332015-07-09 02:09:04 +00001839 SDValue StackPtr =
1840 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001841
Bob Wilson2e076c42009-06-22 23:27:02 +00001842 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001843 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001844
Bob Wilsona4c22902009-04-17 19:07:39 +00001845 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001846 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001847 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1848 i != e;
1849 ++i, ++realArgIdx) {
1850 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001851 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001852 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001853 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001854
Bob Wilsona4c22902009-04-17 19:07:39 +00001855 // Promote the value if needed.
1856 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001857 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001858 case CCValAssign::Full: break;
1859 case CCValAssign::SExt:
1860 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1861 break;
1862 case CCValAssign::ZExt:
1863 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1864 break;
1865 case CCValAssign::AExt:
1866 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1867 break;
1868 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001869 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001870 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001871 }
1872
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001873 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001874 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001875 if (VA.getLocVT() == MVT::v2f64) {
1876 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001877 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001878 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001879 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001880
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001881 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001882 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1883
1884 VA = ArgLocs[++i]; // skip ahead to next loc
1885 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001886 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001887 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1888 } else {
1889 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001890
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001891 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1892 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001893 }
1894 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001895 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001896 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001897 }
1898 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001899 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1900 assert(VA.getLocVT() == MVT::i32 &&
1901 "unexpected calling convention register assignment");
1902 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001903 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001904 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001905 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001906 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001907 } else if (isByVal) {
1908 assert(VA.isMemLoc());
1909 unsigned offset = 0;
1910
1911 // True if this byval aggregate will be split between registers
1912 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001913 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001914 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001915
1916 if (CurByValIdx < ByValArgsCount) {
1917
1918 unsigned RegBegin, RegEnd;
1919 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1920
Mehdi Amini44ede332015-07-09 02:09:04 +00001921 EVT PtrVT =
1922 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001923 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001924 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001925 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001926 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1927 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1928 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001929 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001930 MemOpChains.push_back(Load.getValue(1));
1931 RegsToPass.push_back(std::make_pair(j, Load));
1932 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001933
1934 // If parameter size outsides register area, "offset" value
1935 // helps us to calculate stack slot for remained part properly.
1936 offset = RegEnd - RegBegin;
1937
1938 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001939 }
1940
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001941 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001942 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001943 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001944 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001945 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001946 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001947 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001948 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001949 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001950 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1951 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001952
Manman Ren9f911162012-06-01 02:44:42 +00001953 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001954 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001955 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001956 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001957 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001958 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001959 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001960
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001961 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1962 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001963 }
Evan Cheng10043e22007-01-19 07:51:42 +00001964 }
1965
1966 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001967 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001968
1969 // Build a sequence of copy-to-reg nodes chained together with token chain
1970 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001971 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001972 // Tail call byval lowering might overwrite argument registers so in case of
1973 // tail call optimization the copies to registers are lowered later.
1974 if (!isTailCall)
1975 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1976 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1977 RegsToPass[i].second, InFlag);
1978 InFlag = Chain.getValue(1);
1979 }
Evan Cheng10043e22007-01-19 07:51:42 +00001980
Dale Johannesend679ff72010-06-03 21:09:53 +00001981 // For tail calls lower the arguments to the 'real' stack slot.
1982 if (isTailCall) {
1983 // Force all the incoming stack arguments to be loaded from the stack
1984 // before any new outgoing arguments are stored to the stack, because the
1985 // outgoing stack slots may alias the incoming argument stack slots, and
1986 // the alias isn't otherwise explicit. This is slightly more conservative
1987 // than necessary, because it means that each store effectively depends
1988 // on every argument instead of just those arguments it would clobber.
1989
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001990 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001991 InFlag = SDValue();
1992 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1993 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1994 RegsToPass[i].second, InFlag);
1995 InFlag = Chain.getValue(1);
1996 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001997 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001998 }
1999
Bill Wendling24c79f22008-09-16 21:48:12 +00002000 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2001 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2002 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00002003 bool isDirect = false;
Rafael Espindola3888bdb2016-06-16 15:22:01 +00002004
2005 const TargetMachine &TM = getTargetMachine();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00002006 const Module *Mod = MF.getFunction()->getParent();
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00002007 const GlobalValue *GV = nullptr;
2008 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2009 GV = G->getGlobal();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00002010 bool isStub =
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002011 !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO();
Rafael Espindola3888bdb2016-06-16 15:22:01 +00002012
Rafael Espindola9ba9c5b2016-06-16 15:44:06 +00002013 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00002014 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00002015 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002016 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00002017
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002018 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool4d950ef2016-07-05 18:30:52 +00002019 assert((!isPositionIndependent() || Subtarget->isTargetWindows()) &&
Rafael Espindola0f898332016-06-20 16:43:17 +00002020 "long-calls codegen is not position independent!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00002021 // Handle a global address or an external symbol. If it's not one of
2022 // those, the target's already in a register, so we don't need to do
2023 // anything extra.
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00002024 if (isa<GlobalAddressSDNode>(Callee)) {
Jim Grosbach32bb3622010-04-14 22:28:31 +00002025 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00002026 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002027 ARMConstantPoolValue *CPV =
2028 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
2029
Jim Grosbach32bb3622010-04-14 22:28:31 +00002030 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00002031 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00002032 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002033 Callee = DAG.getLoad(
2034 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002035 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach32bb3622010-04-14 22:28:31 +00002036 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
2037 const char *Sym = S->getSymbol();
2038
2039 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00002040 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00002041 ARMConstantPoolValue *CPV =
2042 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
2043 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00002044 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00002045 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00002046 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002047 Callee = DAG.getLoad(
2048 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002049 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach32bb3622010-04-14 22:28:31 +00002050 }
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00002051 } else if (isa<GlobalAddressSDNode>(Callee)) {
James Molloy2af08fa2016-07-15 07:57:35 +00002052 // If we're optimizing for minimum size and the function is called three or
2053 // more times in this block, we can improve codesize by calling indirectly
James Molloya454a112016-07-15 07:55:21 +00002054 // as BLXr has a 16-bit encoding.
2055 auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
2056 auto *BB = CLI.CS->getParent();
2057 bool PreferIndirect =
2058 Subtarget->isThumb() && MF.getFunction()->optForMinSize() &&
David Majnemer0da5afe2016-08-12 04:32:29 +00002059 count_if(GV->users(), [&BB](const User *U) {
James Molloya454a112016-07-15 07:55:21 +00002060 return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB;
2061 }) > 2;
Rafael Espindola41410cc2016-06-01 21:57:11 +00002062
James Molloya454a112016-07-15 07:55:21 +00002063 if (!PreferIndirect) {
2064 isDirect = true;
2065 bool isDef = GV->isStrongDefinitionForLinker();
2066
2067 // ARM call to a local ARM function is predicable.
2068 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
2069 // tBX takes a register source operand.
2070 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
2071 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
2072 Callee = DAG.getNode(
2073 ARMISD::WrapperPIC, dl, PtrVt,
2074 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
Justin Lebaradbf09e2016-09-11 01:38:58 +00002075 Callee = DAG.getLoad(
2076 PtrVt, dl, DAG.getEntryNode(), Callee,
2077 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2078 /* Alignment = */ 0, MachineMemOperand::MODereferenceable |
2079 MachineMemOperand::MOInvariant);
James Molloya454a112016-07-15 07:55:21 +00002080 } else if (Subtarget->isTargetCOFF()) {
2081 assert(Subtarget->isTargetWindows() &&
2082 "Windows is the only supported COFF target");
2083 unsigned TargetFlags = GV->hasDLLImportStorageClass()
2084 ? ARMII::MO_DLLIMPORT
2085 : ARMII::MO_NO_FLAG;
2086 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0,
2087 TargetFlags);
2088 if (GV->hasDLLImportStorageClass())
2089 Callee =
2090 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
2091 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Justin Lebar9c375812016-07-15 18:27:10 +00002092 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
James Molloya454a112016-07-15 07:55:21 +00002093 } else {
2094 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0);
2095 }
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00002096 }
Bill Wendling24c79f22008-09-16 21:48:12 +00002097 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00002098 isDirect = true;
Evan Cheng83f35172007-01-30 20:37:08 +00002099 // tBX takes a register source operand.
2100 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00002101 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00002102 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00002103 ARMConstantPoolValue *CPV =
2104 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
2105 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00002106 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002107 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002108 Callee = DAG.getLoad(
2109 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002110 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002111 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00002112 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00002113 } else {
Rafael Espindolaafade352016-06-16 16:09:53 +00002114 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00002115 }
Evan Cheng10043e22007-01-19 07:51:42 +00002116 }
2117
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002118 // FIXME: handle tail calls differently.
2119 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00002120 if (Subtarget->isThumb()) {
2121 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002122 CallOpc = ARMISD::CALL_NOLINK;
2123 else
Tim Northoverb5ece522016-05-10 19:17:47 +00002124 CallOpc = ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002125 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00002126 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00002127 CallOpc = ARMISD::CALL_NOLINK;
Sjoerd Meijerd906bf12016-06-03 14:03:27 +00002128 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00002129 // Emit regular call when code size is the priority
2130 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00002131 // "mov lr, pc; b _foo" to avoid confusing the RSP
2132 CallOpc = ARMISD::CALL_NOLINK;
2133 else
2134 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002135 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002136
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002137 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00002138 Ops.push_back(Chain);
2139 Ops.push_back(Callee);
2140
2141 // Add argument registers to the end of the list so that they are known live
2142 // into the call.
2143 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2144 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2145 RegsToPass[i].second.getValueType()));
2146
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002147 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00002148 if (!isTailCall) {
2149 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00002150 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00002151 if (isThisReturn) {
2152 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00002153 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00002154 if (!Mask) {
2155 // Set isThisReturn to false if the calling convention is not one that
2156 // allows 'returned' to be modeled in this way, so LowerCallResult does
2157 // not try to pass 'this' straight through
2158 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00002159 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00002160 }
2161 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00002162 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00002163
Matthias Braunc22630e2013-10-04 16:52:54 +00002164 assert(Mask && "Missing call preserved mask for calling convention");
2165 Ops.push_back(DAG.getRegisterMask(Mask));
2166 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002167
Gabor Greiff304a7a2008-08-28 21:40:38 +00002168 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00002169 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00002170
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002171 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00002172 if (isTailCall) {
Matthias Braun941a7052016-07-28 18:40:00 +00002173 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00002174 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00002175 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002176
Duncan Sands739a0542008-07-02 17:40:58 +00002177 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00002178 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00002179 InFlag = Chain.getValue(1);
2180
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002181 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
2182 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002183 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00002184 InFlag = Chain.getValue(1);
2185
Bob Wilsona4c22902009-04-17 19:07:39 +00002186 // Handle result values, copying them out of physregs into vregs that we
2187 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00002188 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00002189 InVals, isThisReturn,
2190 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00002191}
2192
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002193/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002194/// on the stack. Remember the next parameter register to allocate,
2195/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002196/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00002197void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
2198 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002199 assert((State->getCallOrPrologue() == Prologue ||
2200 State->getCallOrPrologue() == Call) &&
2201 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002202
Tim Northover8cda34f2015-03-11 18:54:22 +00002203 // Byval (as with any stack) slots are always at least 4 byte aligned.
2204 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002205
Tim Northover8cda34f2015-03-11 18:54:22 +00002206 unsigned Reg = State->AllocateReg(GPRArgRegs);
2207 if (!Reg)
2208 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002209
Tim Northover8cda34f2015-03-11 18:54:22 +00002210 unsigned AlignInRegs = Align / 4;
2211 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2212 for (unsigned i = 0; i < Waste; ++i)
2213 Reg = State->AllocateReg(GPRArgRegs);
2214
2215 if (!Reg)
2216 return;
2217
2218 unsigned Excess = 4 * (ARM::R4 - Reg);
2219
2220 // Special case when NSAA != SP and parameter size greater than size of
2221 // all remained GPR regs. In that case we can't split parameter, we must
2222 // send it to stack. We also must set NCRN to R4, so waste all
2223 // remained registers.
2224 const unsigned NSAAOffset = State->getNextStackOffset();
2225 if (NSAAOffset != 0 && Size > Excess) {
2226 while (State->AllocateReg(GPRArgRegs))
2227 ;
2228 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002229 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002230
2231 // First register for byval parameter is the first register that wasn't
2232 // allocated before this method call, so it would be "reg".
2233 // If parameter is small enough to be saved in range [reg, r4), then
2234 // the end (first after last) register would be reg + param-size-in-regs,
2235 // else parameter would be splitted between registers and stack,
2236 // end register would be r4 in this case.
2237 unsigned ByValRegBegin = Reg;
2238 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2239 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2240 // Note, first register is allocated in the beginning of function already,
2241 // allocate remained amount of registers we need.
2242 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2243 State->AllocateReg(GPRArgRegs);
2244 // A byval parameter that is split between registers and memory needs its
2245 // size truncated here.
2246 // In the case where the entire structure fits in registers, we set the
2247 // size in memory to zero.
2248 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002249}
2250
Dale Johannesend679ff72010-06-03 21:09:53 +00002251/// MatchingStackOffset - Return true if the given stack call argument is
2252/// already available in the same position (relatively) of the caller's
2253/// incoming argument stack.
2254static
2255bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
Matthias Braun941a7052016-07-28 18:40:00 +00002256 MachineFrameInfo &MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002257 const TargetInstrInfo *TII) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002258 unsigned Bytes = Arg.getValueSizeInBits() / 8;
Dale Johannesend679ff72010-06-03 21:09:53 +00002259 int FI = INT_MAX;
2260 if (Arg.getOpcode() == ISD::CopyFromReg) {
2261 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002262 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002263 return false;
2264 MachineInstr *Def = MRI->getVRegDef(VR);
2265 if (!Def)
2266 return false;
2267 if (!Flags.isByVal()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002268 if (!TII->isLoadFromStackSlot(*Def, FI))
Dale Johannesend679ff72010-06-03 21:09:53 +00002269 return false;
2270 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002271 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002272 }
2273 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2274 if (Flags.isByVal())
2275 // ByVal argument is passed in as a pointer but it's now being
2276 // dereferenced. e.g.
2277 // define @foo(%struct.X* %A) {
2278 // tail call @bar(%struct.X* byval %A)
2279 // }
2280 return false;
2281 SDValue Ptr = Ld->getBasePtr();
2282 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2283 if (!FINode)
2284 return false;
2285 FI = FINode->getIndex();
2286 } else
2287 return false;
2288
2289 assert(FI != INT_MAX);
Matthias Braun941a7052016-07-28 18:40:00 +00002290 if (!MFI.isFixedObjectIndex(FI))
Dale Johannesend679ff72010-06-03 21:09:53 +00002291 return false;
Matthias Braun941a7052016-07-28 18:40:00 +00002292 return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI);
Dale Johannesend679ff72010-06-03 21:09:53 +00002293}
2294
2295/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2296/// for tail call optimization. Targets which want to do tail call
2297/// optimization should implement this function.
2298bool
2299ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2300 CallingConv::ID CalleeCC,
2301 bool isVarArg,
2302 bool isCalleeStructRet,
2303 bool isCallerStructRet,
2304 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002305 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002306 const SmallVectorImpl<ISD::InputArg> &Ins,
2307 SelectionDAG& DAG) const {
Matthias Braun8d414362016-03-30 22:46:04 +00002308 MachineFunction &MF = DAG.getMachineFunction();
2309 const Function *CallerF = MF.getFunction();
Dale Johannesend679ff72010-06-03 21:09:53 +00002310 CallingConv::ID CallerCC = CallerF->getCallingConv();
Manman Ren4865d892016-03-18 23:41:51 +00002311
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002312 assert(Subtarget->supportsTailCall());
2313
Dale Johannesend679ff72010-06-03 21:09:53 +00002314 // Look for obvious safe cases to perform tail call optimization that do not
2315 // require ABI changes. This is what gcc calls sibcall.
2316
Tim Northoverd8407452013-10-01 14:33:28 +00002317 // Exception-handling functions need a special set of instructions to indicate
2318 // a return to the hardware. Tail-calling another function would probably
2319 // break this.
2320 if (CallerF->hasFnAttribute("interrupt"))
2321 return false;
2322
Dale Johannesend679ff72010-06-03 21:09:53 +00002323 // Also avoid sibcall optimization if either caller or callee uses struct
2324 // return semantics.
2325 if (isCalleeStructRet || isCallerStructRet)
2326 return false;
2327
Oliver Stannard12993dd2014-08-18 12:42:15 +00002328 // Externally-defined functions with weak linkage should not be
2329 // tail-called on ARM when the OS does not support dynamic
2330 // pre-emption of symbols, as the AAELF spec requires normal calls
2331 // to undefined weak functions to be replaced with a NOP or jump to the
2332 // next instruction. The behaviour of branch instructions in this
2333 // situation (as used for tail calls) is implementation-defined, so we
2334 // cannot rely on the linker replacing the tail call with a return.
2335 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2336 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002337 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002338 if (GV->hasExternalWeakLinkage() &&
2339 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002340 return false;
2341 }
2342
Matthias Braun8d414362016-03-30 22:46:04 +00002343 // Check that the call results are passed in the same way.
2344 LLVMContext &C = *DAG.getContext();
2345 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2346 CCAssignFnForNode(CalleeCC, true, isVarArg),
2347 CCAssignFnForNode(CallerCC, true, isVarArg)))
2348 return false;
Matthias Braun870c34f2016-04-04 18:56:13 +00002349 // The callee has to preserve all registers the caller needs to preserve.
Matthias Braun707e02c2016-04-13 21:43:25 +00002350 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2351 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
Matthias Braun870c34f2016-04-04 18:56:13 +00002352 if (CalleeCC != CallerCC) {
Matthias Braun707e02c2016-04-13 21:43:25 +00002353 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2354 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
Matthias Braun870c34f2016-04-04 18:56:13 +00002355 return false;
2356 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002357
Manman Ren7e48b252012-10-12 23:39:43 +00002358 // If Caller's vararg or byval argument has been split between registers and
2359 // stack, do not perform tail call, since part of the argument is in caller's
2360 // local frame.
Matthias Braun8d414362016-03-30 22:46:04 +00002361 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002362 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002363 return false;
2364
Dale Johannesend679ff72010-06-03 21:09:53 +00002365 // If the callee takes no arguments then go on to check the results of the
2366 // call.
2367 if (!Outs.empty()) {
2368 // Check if stack adjustment is needed. For now, do not do this if any
2369 // argument is passed on the stack.
2370 SmallVector<CCValAssign, 16> ArgLocs;
Matthias Braun8d414362016-03-30 22:46:04 +00002371 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002372 CCInfo.AnalyzeCallOperands(Outs,
2373 CCAssignFnForNode(CalleeCC, false, isVarArg));
2374 if (CCInfo.getNextStackOffset()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002375 // Check if the arguments are already laid out in the right way as
2376 // the caller's fixed stack objects.
Matthias Braun941a7052016-07-28 18:40:00 +00002377 MachineFrameInfo &MFI = MF.getFrameInfo();
Dale Johannesend679ff72010-06-03 21:09:53 +00002378 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002379 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002380 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2381 i != e;
2382 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002383 CCValAssign &VA = ArgLocs[i];
2384 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002385 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002386 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002387 if (VA.getLocInfo() == CCValAssign::Indirect)
2388 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002389 if (VA.needsCustom()) {
2390 // f64 and vector types are split into multiple registers or
2391 // register/stack-slot combinations. The types will not match
2392 // the registers; give up on memory f64 refs until we figure
2393 // out what to do about this.
2394 if (!VA.isRegLoc())
2395 return false;
2396 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002397 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002398 if (RegVT == MVT::v2f64) {
2399 if (!ArgLocs[++i].isRegLoc())
2400 return false;
2401 if (!ArgLocs[++i].isRegLoc())
2402 return false;
2403 }
2404 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002405 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2406 MFI, MRI, TII))
2407 return false;
2408 }
2409 }
2410 }
Matthias Braun707e02c2016-04-13 21:43:25 +00002411
Matthias Braun46b0f032016-04-14 01:10:42 +00002412 const MachineRegisterInfo &MRI = MF.getRegInfo();
2413 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
2414 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002415 }
2416
2417 return true;
2418}
2419
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002420bool
2421ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2422 MachineFunction &MF, bool isVarArg,
2423 const SmallVectorImpl<ISD::OutputArg> &Outs,
2424 LLVMContext &Context) const {
2425 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002426 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002427 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2428 isVarArg));
2429}
2430
Tim Northoverd8407452013-10-01 14:33:28 +00002431static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002432 const SDLoc &DL, SelectionDAG &DAG) {
Tim Northoverd8407452013-10-01 14:33:28 +00002433 const MachineFunction &MF = DAG.getMachineFunction();
2434 const Function *F = MF.getFunction();
2435
2436 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2437
2438 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2439 // version of the "preferred return address". These offsets affect the return
2440 // instruction if this is a return from PL1 without hypervisor extensions.
2441 // IRQ/FIQ: +4 "subs pc, lr, #4"
2442 // SWI: 0 "subs pc, lr, #0"
2443 // ABORT: +4 "subs pc, lr, #4"
2444 // UNDEF: +4/+2 "subs pc, lr, #0"
2445 // UNDEF varies depending on where the exception came from ARM or Thumb
2446 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2447
2448 int64_t LROffset;
2449 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2450 IntKind == "ABORT")
2451 LROffset = 4;
2452 else if (IntKind == "SWI" || IntKind == "UNDEF")
2453 LROffset = 0;
2454 else
2455 report_fatal_error("Unsupported interrupt attribute. If present, value "
2456 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2457
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002458 RetOps.insert(RetOps.begin() + 1,
2459 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002460
Craig Topper48d114b2014-04-26 18:35:24 +00002461 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002462}
2463
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002464SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002465ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2466 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002467 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002468 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002469 const SDLoc &dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002470
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002471 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002472 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002473
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002474 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002475 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2476 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002477
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002478 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002479 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2480 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002481
Bob Wilsona4c22902009-04-17 19:07:39 +00002482 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002483 SmallVector<SDValue, 4> RetOps;
2484 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002485 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002486
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002487 MachineFunction &MF = DAG.getMachineFunction();
2488 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2489 AFI->setReturnRegsCount(RVLocs.size());
2490
Bob Wilsona4c22902009-04-17 19:07:39 +00002491 // Copy the result values into the output registers.
2492 for (unsigned i = 0, realRVLocIdx = 0;
2493 i != RVLocs.size();
2494 ++i, ++realRVLocIdx) {
2495 CCValAssign &VA = RVLocs[i];
2496 assert(VA.isRegLoc() && "Can only return in registers!");
2497
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002498 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002499
2500 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002501 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002502 case CCValAssign::Full: break;
2503 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002504 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002505 break;
2506 }
2507
Bob Wilsona4c22902009-04-17 19:07:39 +00002508 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002509 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002510 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002511 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002512 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002513 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002514 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002515
Christian Pirkerb5728192014-05-08 14:06:24 +00002516 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2517 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2518 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002519 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002520 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002521 VA = RVLocs[++i]; // skip ahead to next loc
2522 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002523 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2524 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002525 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002526 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002527 VA = RVLocs[++i]; // skip ahead to next loc
2528
2529 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002530 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002531 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002532 }
2533 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2534 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002535 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002536 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002537 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2538 fmrrd.getValue(isLittleEndian ? 0 : 1),
2539 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002540 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002541 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002542 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002543 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2544 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002545 Flag);
2546 } else
2547 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2548
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002549 // Guarantee that all emitted copies are
2550 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002551 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002552 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002553 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002554 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2555 const MCPhysReg *I =
2556 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2557 if (I) {
2558 for (; *I; ++I) {
2559 if (ARM::GPRRegClass.contains(*I))
2560 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2561 else if (ARM::DPRRegClass.contains(*I))
2562 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2563 else
2564 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2565 }
2566 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002567
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002568 // Update chain and glue.
2569 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002570 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002571 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002572
Tim Northoverd8407452013-10-01 14:33:28 +00002573 // CPUs which aren't M-class use a special sequence to return from
2574 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2575 // though we use "subs pc, lr, #N").
2576 //
2577 // M-class CPUs actually use a normal return sequence with a special
2578 // (hardware-provided) value in LR, so the normal code path works.
2579 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2580 !Subtarget->isMClass()) {
2581 if (Subtarget->isThumb1Only())
2582 report_fatal_error("interrupt attribute is not supported in Thumb1");
2583 return LowerInterruptReturn(RetOps, dl, DAG);
2584 }
2585
Craig Topper48d114b2014-04-26 18:35:24 +00002586 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002587}
2588
Evan Chengf8bad082012-04-10 01:51:00 +00002589bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002590 if (N->getNumValues() != 1)
2591 return false;
2592 if (!N->hasNUsesOfValue(1, 0))
2593 return false;
2594
Evan Chengf8bad082012-04-10 01:51:00 +00002595 SDValue TCChain = Chain;
2596 SDNode *Copy = *N->use_begin();
2597 if (Copy->getOpcode() == ISD::CopyToReg) {
2598 // If the copy has a glue operand, we conservatively assume it isn't safe to
2599 // perform a tail call.
2600 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2601 return false;
2602 TCChain = Copy->getOperand(0);
2603 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2604 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002605 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002606 SmallPtrSet<SDNode*, 2> Copies;
2607 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002608 UI != UE; ++UI) {
2609 if (UI->getOpcode() != ISD::CopyToReg)
2610 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002611 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002612 }
Evan Chengf8bad082012-04-10 01:51:00 +00002613 if (Copies.size() > 2)
2614 return false;
2615
2616 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2617 UI != UE; ++UI) {
2618 SDValue UseChain = UI->getOperand(0);
2619 if (Copies.count(UseChain.getNode()))
2620 // Second CopyToReg
2621 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002622 else {
2623 // We are at the top of this chain.
2624 // If the copy has a glue operand, we conservatively assume it
2625 // isn't safe to perform a tail call.
2626 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2627 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002628 // First CopyToReg
2629 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002630 }
Evan Chengf8bad082012-04-10 01:51:00 +00002631 }
2632 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002633 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002634 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002635 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002636 Copy = *Copy->use_begin();
2637 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002638 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002639 // If the copy has a glue operand, we conservatively assume it isn't safe to
2640 // perform a tail call.
2641 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2642 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002643 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002644 } else {
2645 return false;
2646 }
2647
Evan Cheng419ea282010-12-01 22:59:46 +00002648 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002649 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2650 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002651 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2652 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002653 return false;
2654 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002655 }
2656
Evan Chengf8bad082012-04-10 01:51:00 +00002657 if (!HasRet)
2658 return false;
2659
2660 Chain = TCChain;
2661 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002662}
2663
Evan Cheng0663f232011-03-21 01:19:09 +00002664bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002665 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002666 return false;
2667
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002668 auto Attr =
2669 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2670 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002671 return false;
2672
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002673 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002674}
2675
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002676// Trying to write a 64 bit value so need to split into two 32 bit values first,
2677// and pass the lower and high parts through.
2678static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2679 SDLoc DL(Op);
2680 SDValue WriteValue = Op->getOperand(2);
2681
2682 // This function is only supposed to be called for i64 type argument.
2683 assert(WriteValue.getValueType() == MVT::i64
2684 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2685
2686 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2687 DAG.getConstant(0, DL, MVT::i32));
2688 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2689 DAG.getConstant(1, DL, MVT::i32));
2690 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2691 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2692}
2693
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002694// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2695// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2696// one of the above mentioned nodes. It has to be wrapped because otherwise
2697// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2698// be used to form addressing mode. These wrapped nodes will be selected
2699// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002700static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002701 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002702 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002703 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002704 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002705 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002706 if (CP->isMachineConstantPoolEntry())
2707 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2708 CP->getAlignment());
2709 else
2710 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2711 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002712 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002713}
2714
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002715unsigned ARMTargetLowering::getJumpTableEncoding() const {
2716 return MachineJumpTableInfo::EK_Inline;
2717}
2718
Dan Gohman21cea8a2010-04-17 15:26:15 +00002719SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2720 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002721 MachineFunction &MF = DAG.getMachineFunction();
2722 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2723 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002724 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002725 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002726 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002727 SDValue CPAddr;
Oliver Stannard8331aae2016-08-08 15:28:31 +00002728 bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI();
Rafael Espindola0f898332016-06-20 16:43:17 +00002729 if (!IsPositionIndependent) {
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002730 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2731 } else {
2732 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002733 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002734 ARMConstantPoolValue *CPV =
2735 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2736 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002737 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2738 }
2739 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Justin Lebar9c375812016-07-15 18:27:10 +00002740 SDValue Result = DAG.getLoad(
2741 PtrVT, DL, DAG.getEntryNode(), CPAddr,
2742 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Rafael Espindola0f898332016-06-20 16:43:17 +00002743 if (!IsPositionIndependent)
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002744 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002745 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002746 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002747}
2748
Tim Northoverbd41cf82016-01-07 09:03:03 +00002749/// \brief Convert a TLS address reference into the correct sequence of loads
2750/// and calls to compute the variable's address for Darwin, and return an
2751/// SDValue containing the final node.
2752
2753/// Darwin only has one TLS scheme which must be capable of dealing with the
2754/// fully general situation, in the worst case. This means:
2755/// + "extern __thread" declaration.
2756/// + Defined in a possibly unknown dynamic library.
2757///
2758/// The general system is that each __thread variable has a [3 x i32] descriptor
2759/// which contains information used by the runtime to calculate the address. The
2760/// only part of this the compiler needs to know about is the first word, which
2761/// contains a function pointer that must be called with the address of the
2762/// entire descriptor in "r0".
2763///
2764/// Since this descriptor may be in a different unit, in general access must
2765/// proceed along the usual ARM rules. A common sequence to produce is:
2766///
2767/// movw rT1, :lower16:_var$non_lazy_ptr
2768/// movt rT1, :upper16:_var$non_lazy_ptr
2769/// ldr r0, [rT1]
2770/// ldr rT2, [r0]
2771/// blx rT2
2772/// [...address now in r0...]
2773SDValue
2774ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2775 SelectionDAG &DAG) const {
2776 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2777 SDLoc DL(Op);
2778
2779 // First step is to get the address of the actua global symbol. This is where
2780 // the TLS descriptor lives.
2781 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2782
2783 // The first entry in the descriptor is a function pointer that we must call
2784 // to obtain the address of the variable.
2785 SDValue Chain = DAG.getEntryNode();
Justin Lebaradbf09e2016-09-11 01:38:58 +00002786 SDValue FuncTLVGet = DAG.getLoad(
2787 MVT::i32, DL, Chain, DescAddr,
2788 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2789 /* Alignment = */ 4,
2790 MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable |
2791 MachineMemOperand::MOInvariant);
Tim Northoverbd41cf82016-01-07 09:03:03 +00002792 Chain = FuncTLVGet.getValue(1);
2793
2794 MachineFunction &F = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002795 MachineFrameInfo &MFI = F.getFrameInfo();
2796 MFI.setAdjustsStack(true);
Tim Northoverbd41cf82016-01-07 09:03:03 +00002797
2798 // TLS calls preserve all registers except those that absolutely must be
2799 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2800 // silly).
2801 auto TRI =
2802 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2803 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2804 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2805
2806 // Finally, we can make the call. This is just a degenerate version of a
2807 // normal AArch64 call node: r0 takes the address of the descriptor, and
2808 // returns the address of the variable in this thread.
2809 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2810 Chain =
2811 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2812 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2813 DAG.getRegisterMask(Mask), Chain.getValue(1));
2814 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2815}
2816
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002817SDValue
2818ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2819 SelectionDAG &DAG) const {
2820 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002821
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002822 SDValue Chain = DAG.getEntryNode();
2823 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2824 SDLoc DL(Op);
2825
2826 // Load the current TEB (thread environment block)
2827 SDValue Ops[] = {Chain,
2828 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2829 DAG.getConstant(15, DL, MVT::i32),
2830 DAG.getConstant(0, DL, MVT::i32),
2831 DAG.getConstant(13, DL, MVT::i32),
2832 DAG.getConstant(0, DL, MVT::i32),
2833 DAG.getConstant(2, DL, MVT::i32)};
2834 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2835 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2836
2837 SDValue TEB = CurrentTEB.getValue(0);
2838 Chain = CurrentTEB.getValue(1);
2839
2840 // Load the ThreadLocalStoragePointer from the TEB
2841 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2842 SDValue TLSArray =
2843 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002844 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002845
2846 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2847 // offset into the TLSArray.
2848
2849 // Load the TLS index from the C runtime
2850 SDValue TLSIndex =
2851 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2852 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
Justin Lebar9c375812016-07-15 18:27:10 +00002853 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002854
2855 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2856 DAG.getConstant(2, DL, MVT::i32));
2857 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2858 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
Justin Lebar9c375812016-07-15 18:27:10 +00002859 MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002860
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002861 // Get the offset of the start of the .tls section (section base)
2862 const auto *GA = cast<GlobalAddressSDNode>(Op);
2863 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL);
Justin Lebar9c375812016-07-15 18:27:10 +00002864 SDValue Offset = DAG.getLoad(
2865 PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32,
2866 DAG.getTargetConstantPool(CPV, PtrVT, 4)),
2867 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002868
2869 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset);
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002870}
2871
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002872// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002873SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002874ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002875 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002876 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002877 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002878 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002879 MachineFunction &MF = DAG.getMachineFunction();
2880 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002881 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002882 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002883 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2884 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002885 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002886 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Justin Lebar9c375812016-07-15 18:27:10 +00002887 Argument = DAG.getLoad(
2888 PtrVT, dl, DAG.getEntryNode(), Argument,
2889 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002890 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002891
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002892 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002893 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002894
2895 // call __tls_get_addr.
2896 ArgListTy Args;
2897 ArgListEntry Entry;
2898 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002899 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002900 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002901
Dale Johannesen555a3752009-01-30 23:10:59 +00002902 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002903 TargetLowering::CallLoweringInfo CLI(DAG);
2904 CLI.setDebugLoc(dl).setChain(Chain)
2905 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002906 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002907
Justin Holewinskiaa583972012-05-25 16:35:28 +00002908 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002909 return CallResult.first;
2910}
2911
2912// Lower ISD::GlobalTLSAddress using the "initial exec" or
2913// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002914SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002915ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002916 SelectionDAG &DAG,
2917 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002918 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002919 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002920 SDValue Offset;
2921 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002922 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002923 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002924 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002925
Hans Wennborgaea41202012-05-04 09:40:39 +00002926 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002927 MachineFunction &MF = DAG.getMachineFunction();
2928 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002929 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002930 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002931 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2932 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002933 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2934 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2935 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002936 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002937 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002938 Offset = DAG.getLoad(
2939 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002940 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002941 Chain = Offset.getValue(1);
2942
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002943 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002944 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002945
Alex Lorenze40c8a22015-08-11 23:09:45 +00002946 Offset = DAG.getLoad(
2947 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002948 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002949 } else {
2950 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002951 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002952 ARMConstantPoolValue *CPV =
2953 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002954 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002955 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002956 Offset = DAG.getLoad(
2957 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002958 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002959 }
2960
2961 // The address of the thread local variable is the add of the thread
2962 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002963 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002964}
2965
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002966SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002967ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002968 if (Subtarget->isTargetDarwin())
2969 return LowerGlobalTLSAddressDarwin(Op, DAG);
2970
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002971 if (Subtarget->isTargetWindows())
2972 return LowerGlobalTLSAddressWindows(Op, DAG);
2973
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002974 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002975 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002976 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002977 if (DAG.getTarget().Options.EmulatedTLS)
2978 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002979
2980 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2981
2982 switch (model) {
2983 case TLSModel::GeneralDynamic:
2984 case TLSModel::LocalDynamic:
2985 return LowerToTLSGeneralDynamicModel(GA, DAG);
2986 case TLSModel::InitialExec:
2987 case TLSModel::LocalExec:
2988 return LowerToTLSExecModels(GA, DAG, model);
2989 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002990 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002991}
2992
James Molloy9abb2fa2016-09-26 07:26:24 +00002993/// Return true if all users of V are within function F, looking through
2994/// ConstantExprs.
2995static bool allUsersAreInFunction(const Value *V, const Function *F) {
2996 SmallVector<const User*,4> Worklist;
2997 for (auto *U : V->users())
2998 Worklist.push_back(U);
2999 while (!Worklist.empty()) {
3000 auto *U = Worklist.pop_back_val();
3001 if (isa<ConstantExpr>(U)) {
3002 for (auto *UU : U->users())
3003 Worklist.push_back(UU);
3004 continue;
3005 }
3006
3007 auto *I = dyn_cast<Instruction>(U);
3008 if (!I || I->getParent()->getParent() != F)
3009 return false;
3010 }
3011 return true;
3012}
3013
3014/// Return true if all users of V are within some (any) function, looking through
3015/// ConstantExprs. In other words, are there any global constant users?
3016static bool allUsersAreInFunctions(const Value *V) {
3017 SmallVector<const User*,4> Worklist;
3018 for (auto *U : V->users())
3019 Worklist.push_back(U);
3020 while (!Worklist.empty()) {
3021 auto *U = Worklist.pop_back_val();
3022 if (isa<ConstantExpr>(U)) {
3023 for (auto *UU : U->users())
3024 Worklist.push_back(UU);
3025 continue;
3026 }
3027
3028 if (!isa<Instruction>(U))
3029 return false;
3030 }
3031 return true;
3032}
3033
3034// Return true if T is an integer, float or an array/vector of either.
3035static bool isSimpleType(Type *T) {
3036 if (T->isIntegerTy() || T->isFloatingPointTy())
3037 return true;
3038 Type *SubT = nullptr;
3039 if (T->isArrayTy())
3040 SubT = T->getArrayElementType();
3041 else if (T->isVectorTy())
3042 SubT = T->getVectorElementType();
3043 else
3044 return false;
3045 return SubT->isIntegerTy() || SubT->isFloatingPointTy();
3046}
3047
3048static SDValue promoteToConstantPool(const GlobalValue *GV, SelectionDAG &DAG,
3049 EVT PtrVT, SDLoc dl) {
3050 // If we're creating a pool entry for a constant global with unnamed address,
3051 // and the global is small enough, we can emit it inline into the constant pool
3052 // to save ourselves an indirection.
3053 //
3054 // This is a win if the constant is only used in one function (so it doesn't
3055 // need to be duplicated) or duplicating the constant wouldn't increase code
3056 // size (implying the constant is no larger than 4 bytes).
3057 const Function *F = DAG.getMachineFunction().getFunction();
3058
3059 // We rely on this decision to inline being idemopotent and unrelated to the
3060 // use-site. We know that if we inline a variable at one use site, we'll
3061 // inline it elsewhere too (and reuse the constant pool entry). Fast-isel
3062 // doesn't know about this optimization, so bail out if it's enabled else
3063 // we could decide to inline here (and thus never emit the GV) but require
3064 // the GV from fast-isel generated code.
3065 if (!EnableConstpoolPromotion ||
3066 DAG.getMachineFunction().getTarget().Options.EnableFastISel)
3067 return SDValue();
3068
3069 auto *GVar = dyn_cast<GlobalVariable>(GV);
3070 if (!GVar || !GVar->hasInitializer() ||
3071 !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() ||
3072 !GVar->hasLocalLinkage())
3073 return SDValue();
3074
3075 // Ensure that we don't try and inline any type that contains pointers. If
3076 // we inline a value that contains relocations, we move the relocations from
3077 // .data to .text which is not ideal.
3078 auto *Init = GVar->getInitializer();
3079 if (!isSimpleType(Init->getType()))
3080 return SDValue();
3081
3082 // The constant islands pass can only really deal with alignment requests
3083 // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote
3084 // any type wanting greater alignment requirements than 4 bytes. We also
3085 // can only promote constants that are multiples of 4 bytes in size or
3086 // are paddable to a multiple of 4. Currently we only try and pad constants
3087 // that are strings for simplicity.
3088 auto *CDAInit = dyn_cast<ConstantDataArray>(Init);
3089 unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType());
James Molloy6215fad2016-10-06 07:56:00 +00003090 unsigned Align = GVar->getAlignment();
James Molloy9abb2fa2016-09-26 07:26:24 +00003091 unsigned RequiredPadding = 4 - (Size % 4);
3092 bool PaddingPossible =
3093 RequiredPadding == 4 || (CDAInit && CDAInit->isString());
3094 if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize)
3095 return SDValue();
3096
3097 unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding);
3098 MachineFunction &MF = DAG.getMachineFunction();
3099 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3100
3101 // We can't bloat the constant pool too much, else the ConstantIslands pass
3102 // may fail to converge. If we haven't promoted this global yet (it may have
3103 // multiple uses), and promoting it would increase the constant pool size (Sz
3104 // > 4), ensure we have space to do so up to MaxTotal.
3105 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4)
3106 if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >=
3107 ConstpoolPromotionMaxTotal)
3108 return SDValue();
3109
3110 // This is only valid if all users are in a single function OR it has users
3111 // in multiple functions but it no larger than a pointer. We also check if
3112 // GVar has constant (non-ConstantExpr) users. If so, it essentially has its
3113 // address taken.
3114 if (!allUsersAreInFunction(GVar, F) &&
3115 !(Size <= 4 && allUsersAreInFunctions(GVar)))
3116 return SDValue();
3117
3118 // We're going to inline this global. Pad it out if needed.
3119 if (RequiredPadding != 4) {
3120 StringRef S = CDAInit->getAsString();
3121
3122 SmallVector<uint8_t,16> V(S.size());
3123 std::copy(S.bytes_begin(), S.bytes_end(), V.begin());
3124 while (RequiredPadding--)
3125 V.push_back(0);
3126 Init = ConstantDataArray::get(*DAG.getContext(), V);
3127 }
3128
3129 auto CPVal = ARMConstantPoolConstant::Create(GVar, Init);
3130 SDValue CPAddr =
3131 DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4);
3132 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) {
3133 AFI->markGlobalAsPromotedToConstantPool(GVar);
3134 AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() +
3135 PaddedSize - 4);
3136 }
3137 ++NumConstpoolPromoted;
3138 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3139}
3140
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003141SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003142 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00003143 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003144 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003145 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola7ad97b22016-05-31 15:31:55 +00003146 const TargetMachine &TM = getTargetMachine();
Oliver Stannard8331aae2016-08-08 15:28:31 +00003147 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
3148 GV = GA->getBaseObject();
3149 bool IsRO =
3150 (isa<GlobalVariable>(GV) && cast<GlobalVariable>(GV)->isConstant()) ||
3151 isa<Function>(GV);
James Molloy9abb2fa2016-09-26 07:26:24 +00003152
3153 if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
3154 if (SDValue V = promoteToConstantPool(GV, DAG, PtrVT, dl))
3155 return V;
3156
Rafael Espindola0f898332016-06-20 16:43:17 +00003157 if (isPositionIndependent()) {
Rafael Espindola3beef8d2016-06-27 23:15:57 +00003158 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Peter Collingbourne97aae402015-10-26 18:23:16 +00003159
3160 MachineFunction &MF = DAG.getMachineFunction();
3161 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3162 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
3163 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3164 SDLoc dl(Op);
3165 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
3166 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
3167 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
3168 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
3169 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00003170 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00003171 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003172 SDValue Result = DAG.getLoad(
3173 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003174 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003175 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00003176 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
3177 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
3178 if (UseGOT_PREL)
Justin Lebar9c375812016-07-15 18:27:10 +00003179 Result =
3180 DAG.getLoad(PtrVT, dl, Chain, Result,
3181 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00003182 return Result;
Oliver Stannard8331aae2016-08-08 15:28:31 +00003183 } else if (Subtarget->isROPI() && IsRO) {
3184 // PC-relative.
3185 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
3186 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
3187 return Result;
3188 } else if (Subtarget->isRWPI() && !IsRO) {
3189 // SB-relative.
3190 ARMConstantPoolValue *CPV =
3191 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL);
3192 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
3193 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3194 SDValue G = DAG.getLoad(
3195 PtrVT, dl, DAG.getEntryNode(), CPAddr,
3196 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3197 SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT);
3198 SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, G);
3199 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00003200 }
3201
3202 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00003203 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00003204 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00003205 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00003206 // FIXME: Once remat is capable of dealing with instructions with register
3207 // operands, expand this into two nodes.
3208 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
3209 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00003210 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00003211 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
3212 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003213 return DAG.getLoad(
3214 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003215 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00003216 }
3217}
3218
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003219SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003220 SelectionDAG &DAG) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00003221 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
3222 "ROPI/RWPI not currently supported for Darwin");
Mehdi Amini44ede332015-07-09 02:09:04 +00003223 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003224 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003225 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengdfce83c2011-01-17 08:03:18 +00003226
Eric Christopherc1058df2014-07-04 01:55:26 +00003227 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00003228 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00003229
Tim Northover72360d22013-12-02 10:35:41 +00003230 // FIXME: Once remat is capable of dealing with instructions with register
3231 // operands, expand this into multiple nodes
3232 unsigned Wrapper =
Rafael Espindola0f898332016-06-20 16:43:17 +00003233 isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00003234
Tim Northover72360d22013-12-02 10:35:41 +00003235 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
3236 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00003237
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00003238 if (Subtarget->isGVIndirectSymbol(GV))
Tim Northover72360d22013-12-02 10:35:41 +00003239 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00003240 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Evan Cheng10043e22007-01-19 07:51:42 +00003241 return Result;
3242}
3243
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003244SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
3245 SelectionDAG &DAG) const {
3246 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00003247 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
3248 "Windows on ARM expects to use movw/movt");
Oliver Stannard8331aae2016-08-08 15:28:31 +00003249 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
3250 "ROPI/RWPI not currently supported for Windows");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003251
3252 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00003253 const ARMII::TOF TargetFlags =
3254 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00003255 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003256 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003257 SDLoc DL(Op);
3258
3259 ++NumMovwMovt;
3260
3261 // FIXME: Once remat is capable of dealing with instructions with register
3262 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003263 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
3264 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00003265 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003266 if (GV->hasDLLImportStorageClass())
3267 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00003268 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003269 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003270}
3271
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003272SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00003273ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003274 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003275 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00003276 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
3277 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00003278 Op.getOperand(1), Val);
3279}
3280
3281SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003282ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003283 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003284 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003285 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003286}
3287
Matthias Braun3cd00c12015-07-16 22:34:16 +00003288SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
3289 SelectionDAG &DAG) const {
3290 SDLoc dl(Op);
3291 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
3292 Op.getOperand(0));
3293}
3294
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003295SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00003296ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00003297 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003298 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003299 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003300 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003301 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00003302 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00003303 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00003304 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00003305 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00003306 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00003307 case Intrinsic::thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00003308 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00003309 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
3310 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00003311 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00003312 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00003313 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00003314 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00003315 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00003316 SDValue CPAddr;
Rafael Espindola0f898332016-06-20 16:43:17 +00003317 bool IsPositionIndependent = isPositionIndependent();
3318 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
Jim Grosbach693e36a2009-08-11 00:09:57 +00003319 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00003320 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
3321 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00003322 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00003323 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003324 SDValue Result = DAG.getLoad(
3325 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003326 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach693e36a2009-08-11 00:09:57 +00003327
Rafael Espindola0f898332016-06-20 16:43:17 +00003328 if (IsPositionIndependent) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003329 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00003330 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
3331 }
3332 return Result;
3333 }
Evan Cheng18381b42011-03-29 23:06:19 +00003334 case Intrinsic::arm_neon_vmulls:
3335 case Intrinsic::arm_neon_vmullu: {
3336 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
3337 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003338 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00003339 Op.getOperand(1), Op.getOperand(2));
3340 }
James Molloyee868b22015-08-11 12:06:25 +00003341 case Intrinsic::arm_neon_vminnm:
3342 case Intrinsic::arm_neon_vmaxnm: {
3343 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
3344 ? ISD::FMINNUM : ISD::FMAXNUM;
3345 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3346 Op.getOperand(1), Op.getOperand(2));
3347 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00003348 case Intrinsic::arm_neon_vminu:
3349 case Intrinsic::arm_neon_vmaxu: {
3350 if (Op.getValueType().isFloatingPoint())
3351 return SDValue();
3352 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
3353 ? ISD::UMIN : ISD::UMAX;
3354 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3355 Op.getOperand(1), Op.getOperand(2));
3356 }
James Molloyd616c642015-08-11 12:06:28 +00003357 case Intrinsic::arm_neon_vmins:
3358 case Intrinsic::arm_neon_vmaxs: {
3359 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00003360 if (!Op.getValueType().isFloatingPoint()) {
3361 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3362 ? ISD::SMIN : ISD::SMAX;
3363 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3364 Op.getOperand(1), Op.getOperand(2));
3365 }
James Molloyd616c642015-08-11 12:06:28 +00003366 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3367 ? ISD::FMINNAN : ISD::FMAXNAN;
3368 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3369 Op.getOperand(1), Op.getOperand(2));
3370 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003371 }
3372}
3373
Eli Friedman30a49e92011-08-03 21:06:02 +00003374static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3375 const ARMSubtarget *Subtarget) {
3376 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003377 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003378 if (!Subtarget->hasDataBarrier()) {
3379 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3380 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3381 // here.
3382 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003383 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003384 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003385 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003386 }
3387
Tim Northover36b24172013-07-03 09:20:36 +00003388 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3389 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003390 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003391 if (Subtarget->isMClass()) {
3392 // Only a full system barrier exists in the M-class architectures.
3393 Domain = ARM_MB::SY;
Diana Picusc5baa432016-06-23 07:47:35 +00003394 } else if (Subtarget->preferISHSTBarriers() &&
3395 Ord == AtomicOrdering::Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003396 // Swift happens to implement ISHST barriers in a way that's compatible with
3397 // Release semantics but weaker than ISH so we'd be fools not to use
3398 // it. Beware: other processors probably don't!
3399 Domain = ARM_MB::ISHST;
3400 }
3401
Joey Gouly926d3f52013-09-05 15:35:24 +00003402 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003403 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3404 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003405}
3406
Evan Cheng8740ee32010-11-03 06:34:55 +00003407static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3408 const ARMSubtarget *Subtarget) {
3409 // ARM pre v5TE and Thumb1 does not have preload instructions.
3410 if (!(Subtarget->isThumb2() ||
3411 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3412 // Just preserve the chain.
3413 return Op.getOperand(0);
3414
Andrew Trickef9de2a2013-05-25 02:42:55 +00003415 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003416 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3417 if (!isRead &&
3418 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3419 // ARMv7 with MP extension has PLDW.
3420 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003421
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003422 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3423 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003424 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003425 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003426 isData = ~isData & 1;
3427 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003428
3429 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003430 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3431 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003432}
3433
Dan Gohman31ae5862010-04-17 14:41:14 +00003434static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3435 MachineFunction &MF = DAG.getMachineFunction();
3436 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3437
Evan Cheng10043e22007-01-19 07:51:42 +00003438 // vastart just stores the address of the VarArgsFrameIndex slot into the
3439 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003440 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003441 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003442 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003443 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003444 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00003445 MachinePointerInfo(SV));
Evan Cheng10043e22007-01-19 07:51:42 +00003446}
3447
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003448SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA,
3449 CCValAssign &NextVA,
3450 SDValue &Root,
3451 SelectionDAG &DAG,
3452 const SDLoc &dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003453 MachineFunction &MF = DAG.getMachineFunction();
3454 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3455
Craig Topper760b1342012-02-22 05:59:10 +00003456 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003457 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003458 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003459 else
Craig Topperc7242e02012-04-20 07:30:17 +00003460 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003461
3462 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003463 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003464 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003465
3466 SDValue ArgValue2;
3467 if (NextVA.isMemLoc()) {
Matthias Braun941a7052016-07-28 18:40:00 +00003468 MachineFrameInfo &MFI = MF.getFrameInfo();
3469 int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003470
3471 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003472 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003473 ArgValue2 = DAG.getLoad(
3474 MVT::i32, dl, Root, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003475 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Bob Wilson2e076c42009-06-22 23:27:02 +00003476 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003477 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003478 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003479 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003480 if (!Subtarget->isLittle())
3481 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003482 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003483}
3484
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003485// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003486// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003487// byval). Either way, we allocate stack slots adjacent to the data
3488// provided by our caller, and store the unallocated registers there.
3489// If this is a variadic function, the va_list pointer will begin with
3490// these values; otherwise, this reassembles a (byval) structure that
3491// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003492// Return: The frame index registers were stored into.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003493int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
3494 const SDLoc &dl, SDValue &Chain,
3495 const Value *OrigArg,
3496 unsigned InRegsParamRecordIdx,
3497 int ArgOffset, unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003498 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003499 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003500 // Setup first unallocated register as first byval register;
3501 // eat all remained registers
3502 // (these two actions are performed by HandleByVal method).
3503 // Then, here, we initialize stack frame with
3504 // "store-reg" instructions.
3505 // Case #2. Var-args function, that doesn't contain byval parameters.
3506 // The same: eat all remained unallocated registers,
3507 // initialize stack frame.
3508
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003509 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003510 MachineFrameInfo &MFI = MF.getFrameInfo();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003511 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003512 unsigned RBegin, REnd;
3513 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3514 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003515 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003516 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003517 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003518 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003519 }
3520
Tim Northover8cda34f2015-03-11 18:54:22 +00003521 if (REnd != RBegin)
3522 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003523
Mehdi Amini44ede332015-07-09 02:09:04 +00003524 auto PtrVT = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00003525 int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003526 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003527
Tim Northover8cda34f2015-03-11 18:54:22 +00003528 SmallVector<SDValue, 4> MemOps;
3529 const TargetRegisterClass *RC =
3530 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003531
Tim Northover8cda34f2015-03-11 18:54:22 +00003532 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3533 unsigned VReg = MF.addLiveIn(Reg, RC);
3534 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +00003535 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3536 MachinePointerInfo(OrigArg, 4 * i));
Tim Northover8cda34f2015-03-11 18:54:22 +00003537 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003538 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003539 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003540
3541 if (!MemOps.empty())
3542 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3543 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003544}
3545
3546// Setup stack frame, the va_list pointer will start from.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003547void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
3548 const SDLoc &dl, SDValue &Chain,
3549 unsigned ArgOffset,
3550 unsigned TotalArgRegsSaveSize,
3551 bool ForceMutable) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003552 MachineFunction &MF = DAG.getMachineFunction();
3553 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3554
3555 // Try to store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003556 // to their spots on the stack so that they may be loaded by dereferencing
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003557 // the result of va_next.
3558 // If there is no regs to be stored, just point address after last
3559 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003560 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3561 CCInfo.getInRegsParamsCount(),
3562 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003563 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003564}
3565
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003566SDValue ARMTargetLowering::LowerFormalArguments(
3567 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3568 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3569 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003570 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003571 MachineFrameInfo &MFI = MF.getFrameInfo();
Bob Wilsona4c22902009-04-17 19:07:39 +00003572
Bob Wilsona4c22902009-04-17 19:07:39 +00003573 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3574
3575 // Assign locations to all of the incoming arguments.
3576 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003577 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3578 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003579 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003580 CCAssignFnForNode(CallConv, /* Return*/ false,
3581 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003582
Bob Wilsona4c22902009-04-17 19:07:39 +00003583 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003584 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003585 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3586 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003587
3588 // Initially ArgRegsSaveSize is zero.
3589 // Then we increase this value each time we meet byval parameter.
3590 // We also increase this value in case of varargs function.
3591 AFI->setArgRegsSaveSize(0);
3592
Oliver Stannardd55e1152014-03-05 15:25:27 +00003593 // Calculate the amount of stack space that we need to allocate to store
3594 // byval and variadic arguments that are passed in registers.
3595 // We need to know this before we allocate the first byval or variadic
3596 // argument, as they will be allocated a stack slot below the CFA (Canonical
3597 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003598 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003599 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003600 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3601 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003602
Tim Northover8cda34f2015-03-11 18:54:22 +00003603 CCValAssign &VA = ArgLocs[i];
3604 unsigned Index = VA.getValNo();
3605 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3606 if (!Flags.isByVal())
3607 continue;
3608
3609 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3610 unsigned RBegin, REnd;
3611 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3612 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3613
3614 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003615 }
3616 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003617
3618 int lastInsIndex = -1;
Matthias Braun941a7052016-07-28 18:40:00 +00003619 if (isVarArg && MFI.hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003620 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3621 if (RegIdx != array_lengthof(GPRArgRegs))
3622 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003623 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003624
3625 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3626 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003627 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003628
Bob Wilsona4c22902009-04-17 19:07:39 +00003629 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3630 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003631 if (Ins[VA.getValNo()].isOrigArg()) {
3632 std::advance(CurOrigArg,
3633 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3634 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3635 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003636 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003637 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003638 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003639
Bob Wilsona4c22902009-04-17 19:07:39 +00003640 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003641 // f64 and vector types are split up into multiple registers or
3642 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003643 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003644 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003645 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003646 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003647 SDValue ArgValue2;
3648 if (VA.isMemLoc()) {
Matthias Braun941a7052016-07-28 18:40:00 +00003649 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003650 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003651 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
3652 MachinePointerInfo::getFixedStack(
3653 DAG.getMachineFunction(), FI));
Bob Wilson699bdf72010-04-13 22:03:22 +00003654 } else {
3655 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3656 Chain, DAG, dl);
3657 }
Owen Anderson9f944592009-08-11 20:47:22 +00003658 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3659 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003660 ArgValue, ArgValue1,
3661 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003662 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003663 ArgValue, ArgValue2,
3664 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003665 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003666 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003667
Bob Wilson2e076c42009-06-22 23:27:02 +00003668 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003669 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003670
Owen Anderson9f944592009-08-11 20:47:22 +00003671 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003672 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003673 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003674 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003675 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003676 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003677 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003678 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3679 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003680 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003681 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003682
3683 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003684 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003685 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003686 }
3687
3688 // If this is an 8 or 16-bit value, it is really passed promoted
3689 // to 32 bits. Insert an assert[sz]ext to capture this, then
3690 // truncate to the right size.
3691 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003692 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003693 case CCValAssign::Full: break;
3694 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003695 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003696 break;
3697 case CCValAssign::SExt:
3698 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3699 DAG.getValueType(VA.getValVT()));
3700 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3701 break;
3702 case CCValAssign::ZExt:
3703 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3704 DAG.getValueType(VA.getValVT()));
3705 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3706 break;
3707 }
3708
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003709 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003710
3711 } else { // VA.isRegLoc()
3712
3713 // sanity check
3714 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003715 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003716
Andrew Trick05938a52015-02-16 18:10:47 +00003717 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003718
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003719 // Some Ins[] entries become multiple ArgLoc[] entries.
3720 // Process them only once.
3721 if (index != lastInsIndex)
3722 {
3723 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003724 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003725 // This can be changed with more analysis.
3726 // In case of tail call optimization mark all arguments mutable.
3727 // Since they could be overwritten by lowering of arguments in case of
3728 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003729 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003730 assert(Ins[index].isOrigArg() &&
3731 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003732 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003733
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003734 int FrameIndex = StoreByValRegs(
3735 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3736 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003737 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003738 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003739 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003740 unsigned FIOffset = VA.getLocMemOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00003741 int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
3742 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003743
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003744 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003745 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003746 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3747 MachinePointerInfo::getFixedStack(
3748 DAG.getMachineFunction(), FI)));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003749 }
3750 lastInsIndex = index;
3751 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003752 }
3753 }
3754
3755 // varargs
Matthias Braun941a7052016-07-28 18:40:00 +00003756 if (isVarArg && MFI.hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003757 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003758 CCInfo.getNextStackOffset(),
3759 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003760
Oliver Stannardb14c6252014-04-02 16:10:33 +00003761 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3762
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003763 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003764}
3765
3766/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003767static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003768 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003769 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003770 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003771 // Maybe this has already been legalized into the constant pool?
3772 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003773 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003774 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003775 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003776 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003777 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003778 } else if (Op->getOpcode() == ISD::BITCAST &&
3779 Op->getValueType(0) == MVT::f64) {
3780 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3781 // created by LowerConstantFP().
3782 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003783 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3784 isNullConstant(BitcastOp->getOperand(0)))
3785 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003786 }
3787 return false;
3788}
3789
Evan Cheng10043e22007-01-19 07:51:42 +00003790/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3791/// the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003792SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3793 SDValue &ARMcc, SelectionDAG &DAG,
3794 const SDLoc &dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003795 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003796 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003797 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003798 // Constant does not fit, try adjusting it by one?
3799 switch (CC) {
3800 default: break;
3801 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003802 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003803 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003804 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003805 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003806 }
3807 break;
3808 case ISD::SETULT:
3809 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003810 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003811 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003812 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003813 }
3814 break;
3815 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003816 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003817 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003818 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003819 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003820 }
3821 break;
3822 case ISD::SETULE:
3823 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003824 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003825 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003826 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003827 }
3828 break;
3829 }
3830 }
3831 }
3832
3833 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003834 ARMISD::NodeType CompareType;
3835 switch (CondCode) {
3836 default:
3837 CompareType = ARMISD::CMP;
3838 break;
3839 case ARMCC::EQ:
3840 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003841 // Uses only Z Flag
3842 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003843 break;
3844 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003845 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003846 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003847}
3848
3849/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003850SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS,
3851 SelectionDAG &DAG, const SDLoc &dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003852 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003853 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003854 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003855 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003856 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003857 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3858 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003859}
3860
Bob Wilson45acbd02011-03-08 01:17:20 +00003861/// duplicateCmp - Glue values can have only one use, so this function
3862/// duplicates a comparison node.
3863SDValue
3864ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3865 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003866 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003867 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3868 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3869
3870 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3871 Cmp = Cmp.getOperand(0);
3872 Opc = Cmp.getOpcode();
3873 if (Opc == ARMISD::CMPFP)
3874 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3875 else {
3876 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3877 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3878 }
3879 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3880}
3881
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003882std::pair<SDValue, SDValue>
3883ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3884 SDValue &ARMcc) const {
3885 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3886
3887 SDValue Value, OverflowCmp;
3888 SDValue LHS = Op.getOperand(0);
3889 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003890 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003891
3892 // FIXME: We are currently always generating CMPs because we don't support
3893 // generating CMN through the backend. This is not as good as the natural
3894 // CMP case because it causes a register dependency and cannot be folded
3895 // later.
3896
3897 switch (Op.getOpcode()) {
3898 default:
3899 llvm_unreachable("Unknown overflow instruction!");
3900 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003901 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3902 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3903 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003904 break;
3905 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003906 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3907 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3908 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003909 break;
3910 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003911 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3912 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3913 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003914 break;
3915 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003916 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3917 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3918 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003919 break;
3920 } // switch (...)
3921
3922 return std::make_pair(Value, OverflowCmp);
3923}
3924
3925
3926SDValue
3927ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3928 // Let legalize expand this if it isn't a legal type yet.
3929 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3930 return SDValue();
3931
3932 SDValue Value, OverflowCmp;
3933 SDValue ARMcc;
3934 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3935 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003936 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003937 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003938 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3939 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003940 EVT VT = Op.getValueType();
3941
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003942 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003943 ARMcc, CCR, OverflowCmp);
3944
3945 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003946 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003947}
3948
3949
Bill Wendling6a981312010-08-11 08:43:16 +00003950SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3951 SDValue Cond = Op.getOperand(0);
3952 SDValue SelectTrue = Op.getOperand(1);
3953 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003954 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003955 unsigned Opc = Cond.getOpcode();
3956
3957 if (Cond.getResNo() == 1 &&
3958 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3959 Opc == ISD::USUBO)) {
3960 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3961 return SDValue();
3962
3963 SDValue Value, OverflowCmp;
3964 SDValue ARMcc;
3965 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3966 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3967 EVT VT = Op.getValueType();
3968
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003969 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003970 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003971 }
Bill Wendling6a981312010-08-11 08:43:16 +00003972
3973 // Convert:
3974 //
3975 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3976 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3977 //
3978 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3979 const ConstantSDNode *CMOVTrue =
3980 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3981 const ConstantSDNode *CMOVFalse =
3982 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3983
3984 if (CMOVTrue && CMOVFalse) {
3985 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3986 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3987
3988 SDValue True;
3989 SDValue False;
3990 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3991 True = SelectTrue;
3992 False = SelectFalse;
3993 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3994 True = SelectFalse;
3995 False = SelectTrue;
3996 }
3997
3998 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003999 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00004000 SDValue ARMcc = Cond.getOperand(2);
4001 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00004002 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00004003 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004004 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00004005 }
4006 }
4007 }
4008
Dan Gohmand4a77c42012-02-24 00:09:36 +00004009 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
4010 // undefined bits before doing a full-word comparison with zero.
4011 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004012 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00004013
Bill Wendling6a981312010-08-11 08:43:16 +00004014 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004015 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00004016 SelectTrue, SelectFalse, ISD::SETNE);
4017}
4018
Joey Gouly881eab52013-08-22 15:29:11 +00004019static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
4020 bool &swpCmpOps, bool &swpVselOps) {
4021 // Start by selecting the GE condition code for opcodes that return true for
4022 // 'equality'
4023 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
4024 CC == ISD::SETULE)
4025 CondCode = ARMCC::GE;
4026
4027 // and GT for opcodes that return false for 'equality'.
4028 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
4029 CC == ISD::SETULT)
4030 CondCode = ARMCC::GT;
4031
4032 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
4033 // to swap the compare operands.
4034 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
4035 CC == ISD::SETULT)
4036 swpCmpOps = true;
4037
4038 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
4039 // If we have an unordered opcode, we need to swap the operands to the VSEL
4040 // instruction (effectively negating the condition).
4041 //
4042 // This also has the effect of swapping which one of 'less' or 'greater'
4043 // returns true, so we also swap the compare operands. It also switches
4044 // whether we return true for 'equality', so we compensate by picking the
4045 // opposite condition code to our original choice.
4046 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
4047 CC == ISD::SETUGT) {
4048 swpCmpOps = !swpCmpOps;
4049 swpVselOps = !swpVselOps;
4050 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
4051 }
4052
4053 // 'ordered' is 'anything but unordered', so use the VS condition code and
4054 // swap the VSEL operands.
4055 if (CC == ISD::SETO) {
4056 CondCode = ARMCC::VS;
4057 swpVselOps = true;
4058 }
4059
4060 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
4061 // code and swap the VSEL operands.
4062 if (CC == ISD::SETUNE) {
4063 CondCode = ARMCC::EQ;
4064 swpVselOps = true;
4065 }
4066}
4067
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004068SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal,
Oliver Stannard51b1d462014-08-21 12:50:31 +00004069 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
4070 SDValue Cmp, SelectionDAG &DAG) const {
4071 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
4072 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
4073 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
4074 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
4075 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
4076
4077 SDValue TrueLow = TrueVal.getValue(0);
4078 SDValue TrueHigh = TrueVal.getValue(1);
4079 SDValue FalseLow = FalseVal.getValue(0);
4080 SDValue FalseHigh = FalseVal.getValue(1);
4081
4082 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
4083 ARMcc, CCR, Cmp);
4084 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
4085 ARMcc, CCR, duplicateCmp(Cmp, DAG));
4086
4087 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
4088 } else {
4089 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
4090 Cmp);
4091 }
4092}
4093
Benjamin Kramer4d098922016-07-10 11:28:51 +00004094static bool isGTorGE(ISD::CondCode CC) {
4095 return CC == ISD::SETGT || CC == ISD::SETGE;
4096}
Pablo Barrio7a643462016-06-23 16:53:49 +00004097
Benjamin Kramer4d098922016-07-10 11:28:51 +00004098static bool isLTorLE(ISD::CondCode CC) {
4099 return CC == ISD::SETLT || CC == ISD::SETLE;
4100}
Pablo Barrio7a643462016-06-23 16:53:49 +00004101
4102// See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating.
4103// All of these conditions (and their <= and >= counterparts) will do:
4104// x < k ? k : x
4105// x > k ? x : k
4106// k < x ? x : k
4107// k > x ? k : x
Benjamin Kramer4d098922016-07-10 11:28:51 +00004108static bool isLowerSaturate(const SDValue LHS, const SDValue RHS,
4109 const SDValue TrueVal, const SDValue FalseVal,
4110 const ISD::CondCode CC, const SDValue K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00004111 return (isGTorGE(CC) &&
4112 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) ||
4113 (isLTorLE(CC) &&
4114 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal)));
4115}
4116
4117// Similar to isLowerSaturate(), but checks for upper-saturating conditions.
Benjamin Kramer4d098922016-07-10 11:28:51 +00004118static bool isUpperSaturate(const SDValue LHS, const SDValue RHS,
4119 const SDValue TrueVal, const SDValue FalseVal,
4120 const ISD::CondCode CC, const SDValue K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00004121 return (isGTorGE(CC) &&
4122 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) ||
4123 (isLTorLE(CC) &&
4124 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal)));
4125}
4126
4127// Check if two chained conditionals could be converted into SSAT.
4128//
4129// SSAT can replace a set of two conditional selectors that bound a number to an
4130// interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples:
4131//
4132// x < -k ? -k : (x > k ? k : x)
4133// x < -k ? -k : (x < k ? x : k)
4134// x > -k ? (x > k ? k : x) : -k
4135// x < k ? (x < -k ? -k : x) : k
4136// etc.
4137//
4138// It returns true if the conversion can be done, false otherwise.
4139// Additionally, the variable is returned in parameter V and the constant in K.
Benjamin Kramer4d098922016-07-10 11:28:51 +00004140static bool isSaturatingConditional(const SDValue &Op, SDValue &V,
4141 uint64_t &K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00004142
4143 SDValue LHS1 = Op.getOperand(0);
4144 SDValue RHS1 = Op.getOperand(1);
4145 SDValue TrueVal1 = Op.getOperand(2);
4146 SDValue FalseVal1 = Op.getOperand(3);
4147 ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4148
4149 const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1;
4150 if (Op2.getOpcode() != ISD::SELECT_CC)
4151 return false;
4152
4153 SDValue LHS2 = Op2.getOperand(0);
4154 SDValue RHS2 = Op2.getOperand(1);
4155 SDValue TrueVal2 = Op2.getOperand(2);
4156 SDValue FalseVal2 = Op2.getOperand(3);
4157 ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get();
4158
4159 // Find out which are the constants and which are the variables
4160 // in each conditional
4161 SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1)
4162 ? &RHS1
4163 : NULL;
4164 SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2)
4165 ? &RHS2
4166 : NULL;
4167 SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2;
4168 SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1;
4169 SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2;
4170 SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2;
4171
4172 // We must detect cases where the original operations worked with 16- or
4173 // 8-bit values. In such case, V2Tmp != V2 because the comparison operations
4174 // must work with sign-extended values but the select operations return
4175 // the original non-extended value.
4176 SDValue V2TmpReg = V2Tmp;
4177 if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG)
4178 V2TmpReg = V2Tmp->getOperand(0);
4179
4180 // Check that the registers and the constants have the correct values
4181 // in both conditionals
4182 if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp ||
4183 V2TmpReg != V2)
4184 return false;
4185
4186 // Figure out which conditional is saturating the lower/upper bound.
4187 const SDValue *LowerCheckOp =
4188 isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
4189 ? &Op
4190 : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) ? &Op2
4191 : NULL;
4192 const SDValue *UpperCheckOp =
4193 isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
4194 ? &Op
4195 : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) ? &Op2
4196 : NULL;
4197
4198 if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp)
4199 return false;
4200
4201 // Check that the constant in the lower-bound check is
4202 // the opposite of the constant in the upper-bound check
4203 // in 1's complement.
4204 int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue();
4205 int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue();
4206 int64_t PosVal = std::max(Val1, Val2);
4207
4208 if (((Val1 > Val2 && UpperCheckOp == &Op) ||
4209 (Val1 < Val2 && UpperCheckOp == &Op2)) &&
4210 Val1 == ~Val2 && isPowerOf2_64(PosVal + 1)) {
4211
4212 V = V2;
4213 K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive
4214 return true;
4215 }
4216
4217 return false;
4218}
4219
Dan Gohman21cea8a2010-04-17 15:26:15 +00004220SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Pablo Barrio7a643462016-06-23 16:53:49 +00004221
Owen Anderson53aa7a92009-08-10 22:56:29 +00004222 EVT VT = Op.getValueType();
Pablo Barrio7a643462016-06-23 16:53:49 +00004223 SDLoc dl(Op);
4224
4225 // Try to convert two saturating conditional selects into a single SSAT
4226 SDValue SatValue;
4227 uint64_t SatConstant;
Bernard Ogden849f7372016-08-02 10:04:03 +00004228 if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) &&
Renato Golin32b165f2016-07-25 22:25:25 +00004229 isSaturatingConditional(Op, SatValue, SatConstant))
Pablo Barrio7a643462016-06-23 16:53:49 +00004230 return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue,
4231 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
4232
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004233 SDValue LHS = Op.getOperand(0);
4234 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00004235 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004236 SDValue TrueVal = Op.getOperand(2);
4237 SDValue FalseVal = Op.getOperand(3);
Evan Cheng10043e22007-01-19 07:51:42 +00004238
Oliver Stannard51b1d462014-08-21 12:50:31 +00004239 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4240 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4241 dl);
4242
4243 // If softenSetCCOperands only returned one value, we should compare it to
4244 // zero.
4245 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004246 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00004247 CC = ISD::SETNE;
4248 }
4249 }
4250
Owen Anderson9f944592009-08-11 20:47:22 +00004251 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00004252 // Try to generate VSEL on ARMv8.
4253 // The VSEL instruction can't use all the usual ARM condition
4254 // codes: it only has two bits to select the condition code, so it's
4255 // constrained to use only GE, GT, VS and EQ.
4256 //
4257 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
4258 // swap the operands of the previous compare instruction (effectively
4259 // inverting the compare condition, swapping 'less' and 'greater') and
4260 // sometimes need to swap the operands to the VSEL (which inverts the
4261 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00004262 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
4263 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00004264 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
4265 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
4266 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00004267 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00004268 std::swap(TrueVal, FalseVal);
4269 }
4270 }
4271
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004272 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00004273 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004274 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004275 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00004276 }
4277
4278 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00004279 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00004280
Scott Douglass7ad77922015-04-08 17:18:28 +00004281 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00004282 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
4283 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00004284 bool swpCmpOps = false;
4285 bool swpVselOps = false;
4286 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
4287
4288 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
4289 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
4290 if (swpCmpOps)
4291 std::swap(LHS, RHS);
4292 if (swpVselOps)
4293 std::swap(TrueVal, FalseVal);
4294 }
4295 }
4296
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004297 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004298 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004299 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004300 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00004301 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004302 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00004303 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004304 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004305 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00004306 }
4307 return Result;
4308}
4309
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004310/// canChangeToInt - Given the fp compare operand, return true if it is suitable
4311/// to morph to an integer compare sequence.
4312static bool canChangeToInt(SDValue Op, bool &SeenZero,
4313 const ARMSubtarget *Subtarget) {
4314 SDNode *N = Op.getNode();
4315 if (!N->hasOneUse())
4316 // Otherwise it requires moving the value from fp to integer registers.
4317 return false;
4318 if (!N->getNumValues())
4319 return false;
4320 EVT VT = Op.getValueType();
4321 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
4322 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
4323 // vmrs are very slow, e.g. cortex-a8.
4324 return false;
4325
4326 if (isFloatingPointZero(Op)) {
4327 SeenZero = true;
4328 return true;
4329 }
4330 return ISD::isNormalLoad(N);
4331}
4332
4333static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
4334 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004335 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004336
4337 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Justin Lebar9c375812016-07-15 18:27:10 +00004338 return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(),
4339 Ld->getPointerInfo(), Ld->getAlignment(),
4340 Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004341
4342 llvm_unreachable("Unknown VFP cmp argument!");
4343}
4344
4345static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
4346 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004347 SDLoc dl(Op);
4348
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004349 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004350 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
4351 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004352 return;
4353 }
4354
4355 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
4356 SDValue Ptr = Ld->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +00004357 RetVal1 =
4358 DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(),
4359 Ld->getAlignment(), Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004360
4361 EVT PtrType = Ptr.getValueType();
4362 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004363 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
4364 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
Justin Lebar9c375812016-07-15 18:27:10 +00004365 RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr,
4366 Ld->getPointerInfo().getWithOffset(4), NewAlign,
4367 Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004368 return;
4369 }
4370
4371 llvm_unreachable("Unknown VFP cmp argument!");
4372}
4373
4374/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
4375/// f32 and even f64 comparisons to integer ones.
4376SDValue
4377ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
4378 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00004379 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004380 SDValue LHS = Op.getOperand(2);
4381 SDValue RHS = Op.getOperand(3);
4382 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004383 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004384
Evan Chengd12af5d2012-03-01 23:27:13 +00004385 bool LHSSeenZero = false;
4386 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
4387 bool RHSSeenZero = false;
4388 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
4389 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00004390 // If unsafe fp math optimization is enabled and there are no other uses of
4391 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004392 // to an integer comparison.
4393 if (CC == ISD::SETOEQ)
4394 CC = ISD::SETEQ;
4395 else if (CC == ISD::SETUNE)
4396 CC = ISD::SETNE;
4397
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004398 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004399 SDValue ARMcc;
4400 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00004401 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4402 bitcastf32Toi32(LHS, DAG), Mask);
4403 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4404 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004405 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
4406 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4407 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
4408 Chain, Dest, ARMcc, CCR, Cmp);
4409 }
4410
4411 SDValue LHS1, LHS2;
4412 SDValue RHS1, RHS2;
4413 expandf64Toi32(LHS, DAG, LHS1, LHS2);
4414 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00004415 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
4416 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004417 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004418 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004419 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004420 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00004421 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004422 }
4423
4424 return SDValue();
4425}
4426
4427SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
4428 SDValue Chain = Op.getOperand(0);
4429 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
4430 SDValue LHS = Op.getOperand(2);
4431 SDValue RHS = Op.getOperand(3);
4432 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004433 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004434
Oliver Stannard51b1d462014-08-21 12:50:31 +00004435 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4436 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4437 dl);
4438
4439 // If softenSetCCOperands only returned one value, we should compare it to
4440 // zero.
4441 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004442 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00004443 CC = ISD::SETNE;
4444 }
4445 }
4446
Owen Anderson9f944592009-08-11 20:47:22 +00004447 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004448 SDValue ARMcc;
4449 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004450 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00004451 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004452 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00004453 }
4454
Owen Anderson9f944592009-08-11 20:47:22 +00004455 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004456
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004457 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004458 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
4459 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004460 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004461 return Result;
4462 }
4463
Evan Cheng10043e22007-01-19 07:51:42 +00004464 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00004465 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00004466
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004467 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004468 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004469 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004470 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004471 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00004472 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00004473 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004474 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004475 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00004476 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00004477 }
4478 return Res;
4479}
4480
Dan Gohman21cea8a2010-04-17 15:26:15 +00004481SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004482 SDValue Chain = Op.getOperand(0);
4483 SDValue Table = Op.getOperand(1);
4484 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004485 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004486
Mehdi Amini44ede332015-07-09 02:09:04 +00004487 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00004488 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004489 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00004490 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004491 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00004492 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004493 if (Subtarget->isThumb2()) {
4494 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4495 // which does another jump to the destination. This also makes it easier
4496 // to translate it to TBB / TBH later.
4497 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004498 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004499 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004500 }
Oliver Stannard8331aae2016-08-08 15:28:31 +00004501 if (isPositionIndependent() || Subtarget->isROPI()) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004502 Addr =
4503 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00004504 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
Evan Chengf3a1fce2009-07-25 00:33:29 +00004505 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004506 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004507 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004508 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004509 Addr =
4510 DAG.getLoad(PTy, dl, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00004511 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
Evan Chengf3a1fce2009-07-25 00:33:29 +00004512 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004513 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004514 }
Evan Cheng10043e22007-01-19 07:51:42 +00004515}
4516
Eli Friedman2d4055b2011-11-09 23:36:02 +00004517static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004518 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004519 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004520
James Molloy547d4c02012-02-20 09:24:05 +00004521 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4522 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4523 return Op;
4524 return DAG.UnrollVectorOp(Op.getNode());
4525 }
4526
4527 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4528 "Invalid type for custom lowering!");
4529 if (VT != MVT::v4i16)
4530 return DAG.UnrollVectorOp(Op.getNode());
4531
4532 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4533 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004534}
4535
Oliver Stannard51b1d462014-08-21 12:50:31 +00004536SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004537 EVT VT = Op.getValueType();
4538 if (VT.isVector())
4539 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004540 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4541 RTLIB::Libcall LC;
4542 if (Op.getOpcode() == ISD::FP_TO_SINT)
4543 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4544 Op.getValueType());
4545 else
4546 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4547 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004548 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004549 /*isSigned*/ false, SDLoc(Op)).first;
4550 }
4551
James Molloyfa041152015-03-23 16:15:16 +00004552 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004553}
4554
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004555static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4556 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004557 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004558
Eli Friedman2d4055b2011-11-09 23:36:02 +00004559 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4560 if (VT.getVectorElementType() == MVT::f32)
4561 return Op;
4562 return DAG.UnrollVectorOp(Op.getNode());
4563 }
4564
Duncan Sandsa41634e2011-08-12 14:54:45 +00004565 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4566 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004567 if (VT != MVT::v4f32)
4568 return DAG.UnrollVectorOp(Op.getNode());
4569
4570 unsigned CastOpc;
4571 unsigned Opc;
4572 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004573 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004574 case ISD::SINT_TO_FP:
4575 CastOpc = ISD::SIGN_EXTEND;
4576 Opc = ISD::SINT_TO_FP;
4577 break;
4578 case ISD::UINT_TO_FP:
4579 CastOpc = ISD::ZERO_EXTEND;
4580 Opc = ISD::UINT_TO_FP;
4581 break;
4582 }
4583
4584 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4585 return DAG.getNode(Opc, dl, VT, Op);
4586}
4587
Oliver Stannard51b1d462014-08-21 12:50:31 +00004588SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004589 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004590 if (VT.isVector())
4591 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004592 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4593 RTLIB::Libcall LC;
4594 if (Op.getOpcode() == ISD::SINT_TO_FP)
4595 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4596 Op.getValueType());
4597 else
4598 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4599 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004600 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004601 /*isSigned*/ false, SDLoc(Op)).first;
4602 }
4603
James Molloyfa041152015-03-23 16:15:16 +00004604 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004605}
4606
Evan Cheng25f93642010-07-08 02:08:50 +00004607SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004608 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004609 SDValue Tmp0 = Op.getOperand(0);
4610 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004611 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004612 EVT VT = Op.getValueType();
4613 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004614 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4615 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4616 bool UseNEON = !InGPR && Subtarget->hasNEON();
4617
4618 if (UseNEON) {
4619 // Use VBSL to copy the sign bit.
4620 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4621 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004622 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004623 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4624 if (VT == MVT::f64)
4625 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4626 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004627 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004628 else /*if (VT == MVT::f32)*/
4629 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4630 if (SrcVT == MVT::f32) {
4631 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4632 if (VT == MVT::f64)
4633 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4634 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004635 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004636 } else if (VT == MVT::f32)
4637 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4638 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004639 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004640 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4641 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4642
4643 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004644 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004645 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4646 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4647 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004648
Evan Chengd6b641e2011-02-23 02:24:55 +00004649 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4650 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4651 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004652 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004653 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4654 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004655 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004656 } else {
4657 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4658 }
4659
4660 return Res;
4661 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004662
4663 // Bitcast operand 1 to i32.
4664 if (SrcVT == MVT::f64)
4665 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004666 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004667 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4668
Evan Chengd6b641e2011-02-23 02:24:55 +00004669 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004670 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4671 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004672 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4673 if (VT == MVT::f32) {
4674 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4675 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4676 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4677 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004678 }
4679
Evan Chengd6b641e2011-02-23 02:24:55 +00004680 // f64: Or the high part with signbit and then combine two parts.
4681 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004682 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004683 SDValue Lo = Tmp0.getValue(0);
4684 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4685 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4686 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004687}
4688
Evan Cheng168ced92010-05-22 01:47:14 +00004689SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4690 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004691 MachineFrameInfo &MFI = MF.getFrameInfo();
4692 MFI.setReturnAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004693
Bill Wendling908bf812014-01-06 00:43:20 +00004694 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004695 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004696
Evan Cheng168ced92010-05-22 01:47:14 +00004697 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004698 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004699 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4700 if (Depth) {
4701 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004702 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004703 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4704 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00004705 MachinePointerInfo());
Evan Cheng168ced92010-05-22 01:47:14 +00004706 }
4707
4708 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004709 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004710 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4711}
4712
Dan Gohman21cea8a2010-04-17 15:26:15 +00004713SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004714 const ARMBaseRegisterInfo &ARI =
4715 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4716 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004717 MachineFrameInfo &MFI = MF.getFrameInfo();
4718 MFI.setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004719
Owen Anderson53aa7a92009-08-10 22:56:29 +00004720 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004721 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004722 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004723 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004724 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4725 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004726 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00004727 MachinePointerInfo());
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004728 return FrameAddr;
4729}
4730
Renato Golinc7aea402014-05-06 16:51:25 +00004731// FIXME? Maybe this could be a TableGen attribute on some registers and
4732// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004733unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4734 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004735 unsigned Reg = StringSwitch<unsigned>(RegName)
4736 .Case("sp", ARM::SP)
4737 .Default(0);
4738 if (Reg)
4739 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004740 report_fatal_error(Twine("Invalid register name \""
4741 + StringRef(RegName) + "\"."));
4742}
4743
4744// Result is 64 bit value so split into two 32 bit values and return as a
4745// pair of values.
4746static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4747 SelectionDAG &DAG) {
4748 SDLoc DL(N);
4749
4750 // This function is only supposed to be called for i64 type destination.
4751 assert(N->getValueType(0) == MVT::i64
4752 && "ExpandREAD_REGISTER called for non-i64 type result.");
4753
4754 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4755 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4756 N->getOperand(0),
4757 N->getOperand(1));
4758
4759 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4760 Read.getValue(1)));
4761 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004762}
4763
Quentin Colombet901f0362015-12-04 01:53:14 +00004764/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4765/// When \p DstVT, the destination type of \p BC, is on the vector
4766/// register bank and the source of bitcast, \p Op, operates on the same bank,
4767/// it might be possible to combine them, such that everything stays on the
4768/// vector register bank.
4769/// \p return The node that would replace \p BT, if the combine
4770/// is possible.
4771static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4772 SelectionDAG &DAG) {
4773 SDValue Op = BC->getOperand(0);
4774 EVT DstVT = BC->getValueType(0);
4775
4776 // The only vector instruction that can produce a scalar (remember,
4777 // since the bitcast was about to be turned into VMOVDRR, the source
4778 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4779 // Moreover, we can do this combine only if there is one use.
4780 // Finally, if the destination type is not a vector, there is not
4781 // much point on forcing everything on the vector bank.
4782 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4783 !Op.hasOneUse())
4784 return SDValue();
4785
4786 // If the index is not constant, we will introduce an additional
4787 // multiply that will stick.
4788 // Give up in that case.
4789 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4790 if (!Index)
4791 return SDValue();
4792 unsigned DstNumElt = DstVT.getVectorNumElements();
4793
4794 // Compute the new index.
4795 const APInt &APIntIndex = Index->getAPIntValue();
4796 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4797 NewIndex *= APIntIndex;
4798 // Check if the new constant index fits into i32.
4799 if (NewIndex.getBitWidth() > 32)
4800 return SDValue();
4801
4802 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4803 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4804 SDLoc dl(Op);
4805 SDValue ExtractSrc = Op.getOperand(0);
4806 EVT VecVT = EVT::getVectorVT(
4807 *DAG.getContext(), DstVT.getScalarType(),
4808 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4809 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4810 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4811 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4812}
4813
Wesley Peck527da1b2010-11-23 03:31:01 +00004814/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004815/// expand a bit convert where either the source or destination type is i64 to
4816/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4817/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4818/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004819static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004820 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004821 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004822 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004823
Bob Wilson59b70ea2010-04-17 05:30:19 +00004824 // This function is only supposed to be called for i64 types, either as the
4825 // source or destination of the bit convert.
4826 EVT SrcVT = Op.getValueType();
4827 EVT DstVT = N->getValueType(0);
4828 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004829 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004830
Bob Wilson59b70ea2010-04-17 05:30:19 +00004831 // Turn i64->f64 into VMOVDRR.
4832 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004833 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4834 // if we can combine the bitcast with its source.
4835 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4836 return Val;
4837
Owen Anderson9f944592009-08-11 20:47:22 +00004838 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004839 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004840 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004841 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004842 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004843 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004844 }
Bob Wilson7117a912009-03-20 22:42:55 +00004845
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004846 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004847 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004848 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004849 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004850 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004851 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4852 DAG.getVTList(MVT::i32, MVT::i32),
4853 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4854 else
4855 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4856 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004857 // Merge the pieces into a single i64 value.
4858 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4859 }
Bob Wilson7117a912009-03-20 22:42:55 +00004860
Bob Wilson59b70ea2010-04-17 05:30:19 +00004861 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004862}
4863
Bob Wilson2e076c42009-06-22 23:27:02 +00004864/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004865/// Zero vectors are used to represent vector negation and in those cases
4866/// will be implemented with the NEON VNEG instruction. However, VNEG does
4867/// not support i64 elements, so sometimes the zero vectors will need to be
4868/// explicitly constructed. Regardless, use a canonical VMOV to create the
4869/// zero vector.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004870static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004871 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004872 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004873 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004874 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4875 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004876 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004877}
4878
Jim Grosbach624fcb22009-10-31 21:00:56 +00004879/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4880/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004881SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4882 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004883 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4884 EVT VT = Op.getValueType();
4885 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004886 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004887 SDValue ShOpLo = Op.getOperand(0);
4888 SDValue ShOpHi = Op.getOperand(1);
4889 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004890 SDValue ARMcc;
Tim Northover397f9d92016-11-16 20:54:28 +00004891 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004892 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004893
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004894 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4895
Jim Grosbach624fcb22009-10-31 21:00:56 +00004896 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004897 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004898 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4899 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004900 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004901 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
Tim Northover397f9d92016-11-16 20:54:28 +00004902 SDValue LoSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4903 SDValue LoBigShift = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
4904 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4905 ISD::SETGE, ARMcc, DAG, dl);
4906 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift, LoBigShift,
4907 ARMcc, CCR, CmpLo);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004908
Tim Northover397f9d92016-11-16 20:54:28 +00004909
4910 SDValue HiSmallShift = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
4911 SDValue HiBigShift = Opc == ISD::SRA
4912 ? DAG.getNode(Opc, dl, VT, ShOpHi,
4913 DAG.getConstant(VTBits - 1, dl, VT))
4914 : DAG.getConstant(0, dl, VT);
4915 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4916 ISD::SETGE, ARMcc, DAG, dl);
4917 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift,
4918 ARMcc, CCR, CmpHi);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004919
4920 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004921 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004922}
4923
Jim Grosbach5d994042009-10-31 19:38:01 +00004924/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4925/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004926SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4927 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004928 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4929 EVT VT = Op.getValueType();
4930 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004931 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004932 SDValue ShOpLo = Op.getOperand(0);
4933 SDValue ShOpHi = Op.getOperand(1);
4934 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004935 SDValue ARMcc;
Tim Northover397f9d92016-11-16 20:54:28 +00004936 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Jim Grosbach5d994042009-10-31 19:38:01 +00004937
4938 assert(Op.getOpcode() == ISD::SHL_PARTS);
4939 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004940 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004941 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
Tim Northover397f9d92016-11-16 20:54:28 +00004942 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4943 SDValue HiSmallShift = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4944
Jim Grosbach5d994042009-10-31 19:38:01 +00004945 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004946 DAG.getConstant(VTBits, dl, MVT::i32));
Tim Northover397f9d92016-11-16 20:54:28 +00004947 SDValue HiBigShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4948 SDValue CmpHi = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4949 ISD::SETGE, ARMcc, DAG, dl);
4950 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, HiSmallShift, HiBigShift,
4951 ARMcc, CCR, CmpHi);
Jim Grosbach5d994042009-10-31 19:38:01 +00004952
Tim Northover397f9d92016-11-16 20:54:28 +00004953 SDValue CmpLo = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004954 ISD::SETGE, ARMcc, DAG, dl);
Tim Northover397f9d92016-11-16 20:54:28 +00004955 SDValue LoSmallShift = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
4956 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, LoSmallShift,
4957 DAG.getConstant(0, dl, VT), ARMcc, CCR, CmpLo);
Jim Grosbach5d994042009-10-31 19:38:01 +00004958
4959 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004960 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004961}
4962
Jim Grosbach535d3b42010-09-08 03:54:02 +00004963SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004964 SelectionDAG &DAG) const {
4965 // The rounding mode is in bits 23:22 of the FPSCR.
4966 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4967 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4968 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004969 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004970 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004971 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004972 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004973 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004974 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004975 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004976 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004977 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004978 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004979}
4980
Jim Grosbach8546ec92010-01-18 19:58:49 +00004981static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4982 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004983 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004984 EVT VT = N->getValueType(0);
4985 if (VT.isVector()) {
4986 assert(ST->hasNEON());
4987
4988 // Compute the least significant set bit: LSB = X & -X
4989 SDValue X = N->getOperand(0);
4990 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4991 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4992
4993 EVT ElemTy = VT.getVectorElementType();
4994
4995 if (ElemTy == MVT::i8) {
4996 // Compute with: cttz(x) = ctpop(lsb - 1)
4997 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4998 DAG.getTargetConstant(1, dl, ElemTy));
4999 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
5000 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
5001 }
5002
5003 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
5004 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
5005 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
5006 unsigned NumBits = ElemTy.getSizeInBits();
5007 SDValue WidthMinus1 =
5008 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5009 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
5010 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
5011 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
5012 }
5013
5014 // Compute with: cttz(x) = ctpop(lsb - 1)
5015
5016 // Since we can only compute the number of bits in a byte with vcnt.8, we
5017 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
5018 // and i64.
5019
5020 // Compute LSB - 1.
5021 SDValue Bits;
5022 if (ElemTy == MVT::i64) {
5023 // Load constant 0xffff'ffff'ffff'ffff to register.
5024 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5025 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
5026 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
5027 } else {
5028 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5029 DAG.getTargetConstant(1, dl, ElemTy));
5030 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
5031 }
5032
5033 // Count #bits with vcnt.8.
5034 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
5035 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
5036 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
5037
5038 // Gather the #bits with vpaddl (pairwise add.)
5039 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
5040 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
5041 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
5042 Cnt8);
5043 if (ElemTy == MVT::i16)
5044 return Cnt16;
5045
5046 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
5047 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
5048 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
5049 Cnt16);
5050 if (ElemTy == MVT::i32)
5051 return Cnt32;
5052
5053 assert(ElemTy == MVT::i64);
5054 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
5055 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
5056 Cnt32);
5057 return Cnt64;
5058 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00005059
5060 if (!ST->hasV6T2Ops())
5061 return SDValue();
5062
James Molloyb5640982015-11-13 16:05:22 +00005063 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00005064 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
5065}
5066
Evan Chengb4eae132012-12-04 22:41:50 +00005067/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
5068/// for each 16-bit element from operand, repeated. The basic idea is to
5069/// leverage vcnt to get the 8-bit counts, gather and add the results.
5070///
5071/// Trace for v4i16:
5072/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
5073/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
5074/// 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 +00005075/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00005076/// [b0 b1 b2 b3 b4 b5 b6 b7]
5077/// +[b1 b0 b3 b2 b5 b4 b7 b6]
5078/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
5079/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
5080static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
5081 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005082 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00005083
5084 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
5085 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
5086 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
5087 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
5088 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
5089 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
5090}
5091
5092/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
5093/// bit-count for each 16-bit element from the operand. We need slightly
5094/// different sequencing for v4i16 and v8i16 to stay within NEON's available
5095/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00005096///
Evan Chengb4eae132012-12-04 22:41:50 +00005097/// Trace for v4i16:
5098/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
5099/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
5100/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
5101/// v4i16:Extracted = [k0 k1 k2 k3 ]
5102static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
5103 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005104 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00005105
5106 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
5107 if (VT.is64BitVector()) {
5108 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
5109 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005110 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005111 } else {
5112 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005113 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005114 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
5115 }
5116}
5117
5118/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
5119/// bit-count for each 32-bit element from the operand. The idea here is
5120/// to split the vector into 16-bit elements, leverage the 16-bit count
5121/// routine, and then combine the results.
5122///
5123/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
5124/// input = [v0 v1 ] (vi: 32-bit elements)
5125/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
5126/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00005127/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00005128/// [k0 k1 k2 k3 ]
5129/// N1 =+[k1 k0 k3 k2 ]
5130/// [k0 k2 k1 k3 ]
5131/// N2 =+[k1 k3 k0 k2 ]
5132/// [k0 k2 k1 k3 ]
5133/// Extended =+[k1 k3 k0 k2 ]
5134/// [k0 k2 ]
5135/// Extracted=+[k1 k3 ]
5136///
5137static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
5138 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005139 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00005140
5141 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
5142
5143 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
5144 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
5145 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
5146 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
5147 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
5148
5149 if (VT.is64BitVector()) {
5150 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
5151 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005152 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005153 } else {
5154 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005155 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005156 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
5157 }
5158}
5159
5160static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
5161 const ARMSubtarget *ST) {
5162 EVT VT = N->getValueType(0);
5163
5164 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00005165 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
5166 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00005167 "Unexpected type for custom ctpop lowering");
5168
5169 if (VT.getVectorElementType() == MVT::i32)
5170 return lowerCTPOP32BitElements(N, DAG);
5171 else
5172 return lowerCTPOP16BitElements(N, DAG);
5173}
5174
Bob Wilson2e076c42009-06-22 23:27:02 +00005175static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
5176 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005177 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005178 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00005179
Bob Wilson7d471332010-11-18 21:16:28 +00005180 if (!VT.isVector())
5181 return SDValue();
5182
Bob Wilson2e076c42009-06-22 23:27:02 +00005183 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00005184 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00005185
Bob Wilson7d471332010-11-18 21:16:28 +00005186 // Left shifts translate directly to the vshiftu intrinsic.
5187 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00005188 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005189 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
5190 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00005191 N->getOperand(0), N->getOperand(1));
5192
5193 assert((N->getOpcode() == ISD::SRA ||
5194 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
5195
5196 // NEON uses the same intrinsics for both left and right shifts. For
5197 // right shifts, the shift amounts are negative, so negate the vector of
5198 // shift amounts.
5199 EVT ShiftVT = N->getOperand(1).getValueType();
5200 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
5201 getZeroVector(ShiftVT, DAG, dl),
5202 N->getOperand(1));
5203 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
5204 Intrinsic::arm_neon_vshifts :
5205 Intrinsic::arm_neon_vshiftu);
5206 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005207 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00005208 N->getOperand(0), NegatedCount);
5209}
5210
5211static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
5212 const ARMSubtarget *ST) {
5213 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005214 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00005215
Eli Friedman682d8c12009-08-22 03:13:10 +00005216 // We can get here for a node like i32 = ISD::SHL i32, i64
5217 if (VT != MVT::i64)
5218 return SDValue();
5219
5220 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00005221 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00005222
Chris Lattnerf81d5882007-11-24 07:07:01 +00005223 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005224 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00005225 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00005226
Chris Lattnerf81d5882007-11-24 07:07:01 +00005227 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00005228 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00005229
Chris Lattnerf81d5882007-11-24 07:07:01 +00005230 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00005231 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005232 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00005233 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005234 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00005235
Chris Lattnerf81d5882007-11-24 07:07:01 +00005236 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
5237 // captures the result into a carry flag.
5238 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00005239 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00005240
Chris Lattnerf81d5882007-11-24 07:07:01 +00005241 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00005242 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00005243
Chris Lattnerf81d5882007-11-24 07:07:01 +00005244 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00005245 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00005246}
5247
Bob Wilson2e076c42009-06-22 23:27:02 +00005248static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5249 SDValue TmpOp0, TmpOp1;
5250 bool Invert = false;
5251 bool Swap = false;
5252 unsigned Opc = 0;
5253
5254 SDValue Op0 = Op.getOperand(0);
5255 SDValue Op1 = Op.getOperand(1);
5256 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00005257 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005258 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005259 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005260 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00005261
Eli Friedmanc0a717b2016-10-18 21:03:40 +00005262 if (Op0.getValueType().getVectorElementType() == MVT::i64 &&
5263 (SetCCOpcode == ISD::SETEQ || SetCCOpcode == ISD::SETNE)) {
5264 // Special-case integer 64-bit equality comparisons. They aren't legal,
5265 // but they can be lowered with a few vector instructions.
5266 unsigned CmpElements = CmpVT.getVectorNumElements() * 2;
5267 EVT SplitVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, CmpElements);
5268 SDValue CastOp0 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op0);
5269 SDValue CastOp1 = DAG.getNode(ISD::BITCAST, dl, SplitVT, Op1);
5270 SDValue Cmp = DAG.getNode(ISD::SETCC, dl, SplitVT, CastOp0, CastOp1,
5271 DAG.getCondCode(ISD::SETEQ));
5272 SDValue Reversed = DAG.getNode(ARMISD::VREV64, dl, SplitVT, Cmp);
5273 SDValue Merged = DAG.getNode(ISD::AND, dl, SplitVT, Cmp, Reversed);
5274 Merged = DAG.getNode(ISD::BITCAST, dl, CmpVT, Merged);
5275 if (SetCCOpcode == ISD::SETNE)
5276 Merged = DAG.getNOT(dl, Merged, CmpVT);
5277 Merged = DAG.getSExtOrTrunc(Merged, dl, VT);
5278 return Merged;
5279 }
5280
James Molloybf170092015-08-20 16:33:44 +00005281 if (CmpVT.getVectorElementType() == MVT::i64)
Eli Friedmanc0a717b2016-10-18 21:03:40 +00005282 // 64-bit comparisons are not legal in general.
James Molloybf170092015-08-20 16:33:44 +00005283 return SDValue();
5284
Oliver Stannard51b1d462014-08-21 12:50:31 +00005285 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005286 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00005287 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00005288 case ISD::SETUNE:
Justin Bognerb03fd122016-08-17 05:10:15 +00005289 case ISD::SETNE: Invert = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005290 case ISD::SETOEQ:
5291 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
5292 case ISD::SETOLT:
Justin Bognerb03fd122016-08-17 05:10:15 +00005293 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005294 case ISD::SETOGT:
5295 case ISD::SETGT: Opc = ARMISD::VCGT; break;
5296 case ISD::SETOLE:
Justin Bognerb03fd122016-08-17 05:10:15 +00005297 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005298 case ISD::SETOGE:
5299 case ISD::SETGE: Opc = ARMISD::VCGE; break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005300 case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005301 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005302 case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005303 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005304 case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005305 case ISD::SETONE:
5306 // Expand this to (OLT | OGT).
5307 TmpOp0 = Op0;
5308 TmpOp1 = Op1;
5309 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00005310 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
5311 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00005312 break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005313 case ISD::SETUO:
5314 Invert = true;
5315 LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005316 case ISD::SETO:
5317 // Expand this to (OLT | OGE).
5318 TmpOp0 = Op0;
5319 TmpOp1 = Op1;
5320 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00005321 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
5322 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00005323 break;
5324 }
5325 } else {
5326 // Integer comparisons.
5327 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00005328 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00005329 case ISD::SETNE: Invert = true;
5330 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
5331 case ISD::SETLT: Swap = true;
5332 case ISD::SETGT: Opc = ARMISD::VCGT; break;
5333 case ISD::SETLE: Swap = true;
5334 case ISD::SETGE: Opc = ARMISD::VCGE; break;
5335 case ISD::SETULT: Swap = true;
5336 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
5337 case ISD::SETULE: Swap = true;
5338 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
5339 }
5340
Nick Lewyckya21d3da2009-07-08 03:04:38 +00005341 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00005342 if (Opc == ARMISD::VCEQ) {
5343
5344 SDValue AndOp;
5345 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
5346 AndOp = Op0;
5347 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
5348 AndOp = Op1;
5349
5350 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00005351 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00005352 AndOp = AndOp.getOperand(0);
5353
5354 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
5355 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00005356 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
5357 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00005358 Invert = !Invert;
5359 }
5360 }
5361 }
5362
5363 if (Swap)
5364 std::swap(Op0, Op1);
5365
Owen Andersonc7baee32010-11-08 23:21:22 +00005366 // If one of the operands is a constant vector zero, attempt to fold the
5367 // comparison to a specialized compare-against-zero form.
5368 SDValue SingleOp;
5369 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
5370 SingleOp = Op0;
5371 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
5372 if (Opc == ARMISD::VCGE)
5373 Opc = ARMISD::VCLEZ;
5374 else if (Opc == ARMISD::VCGT)
5375 Opc = ARMISD::VCLTZ;
5376 SingleOp = Op1;
5377 }
Wesley Peck527da1b2010-11-23 03:31:01 +00005378
Owen Andersonc7baee32010-11-08 23:21:22 +00005379 SDValue Result;
5380 if (SingleOp.getNode()) {
5381 switch (Opc) {
5382 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00005383 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005384 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00005385 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005386 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00005387 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005388 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00005389 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005390 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00005391 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005392 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00005393 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00005394 }
5395 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00005396 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00005397 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005398
Tim Northover45aa89c2015-02-08 00:50:47 +00005399 Result = DAG.getSExtOrTrunc(Result, dl, VT);
5400
Bob Wilson2e076c42009-06-22 23:27:02 +00005401 if (Invert)
5402 Result = DAG.getNOT(dl, Result, VT);
5403
5404 return Result;
5405}
5406
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00005407static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) {
5408 SDValue LHS = Op.getOperand(0);
5409 SDValue RHS = Op.getOperand(1);
5410 SDValue Carry = Op.getOperand(2);
5411 SDValue Cond = Op.getOperand(3);
5412 SDLoc DL(Op);
5413
5414 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only.");
5415
5416 assert(Carry.getOpcode() != ISD::CARRY_FALSE);
5417 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
5418 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
5419
5420 SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
5421 SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
5422 SDValue ARMcc = DAG.getConstant(
5423 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
5424 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
5425 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
5426 Cmp.getValue(1), SDValue());
5427 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
5428 CCR, Chain.getValue(1));
5429}
5430
Bob Wilson5b2b5042010-06-14 22:19:57 +00005431/// isNEONModifiedImm - Check if the specified splat value corresponds to a
5432/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00005433/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00005434static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
5435 unsigned SplatBitSize, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005436 const SDLoc &dl, EVT &VT, bool is128Bits,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005437 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005438 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00005439
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005440 // SplatBitSize is set to the smallest size that splats the vector, so a
5441 // zero vector will always have SplatBitSize == 8. However, NEON modified
5442 // immediate instructions others than VMOV do not support the 8-bit encoding
5443 // of a zero vector, and the default encoding of zero is supposed to be the
5444 // 32-bit version.
5445 if (SplatBits == 0)
5446 SplatBitSize = 32;
5447
Bob Wilson2e076c42009-06-22 23:27:02 +00005448 switch (SplatBitSize) {
5449 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00005450 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00005451 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005452 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00005453 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005454 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00005455 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00005456 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005457 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00005458
5459 case 16:
5460 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00005461 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005462 if ((SplatBits & ~0xff) == 0) {
5463 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005464 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005465 Imm = SplatBits;
5466 break;
5467 }
5468 if ((SplatBits & ~0xff00) == 0) {
5469 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005470 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00005471 Imm = SplatBits >> 8;
5472 break;
5473 }
5474 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005475
5476 case 32:
5477 // NEON's 32-bit VMOV supports splat values where:
5478 // * only one byte is nonzero, or
5479 // * the least significant byte is 0xff and the second byte is nonzero, or
5480 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00005481 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00005482 if ((SplatBits & ~0xff) == 0) {
5483 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005484 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005485 Imm = SplatBits;
5486 break;
5487 }
5488 if ((SplatBits & ~0xff00) == 0) {
5489 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005490 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00005491 Imm = SplatBits >> 8;
5492 break;
5493 }
5494 if ((SplatBits & ~0xff0000) == 0) {
5495 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005496 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00005497 Imm = SplatBits >> 16;
5498 break;
5499 }
5500 if ((SplatBits & ~0xff000000) == 0) {
5501 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005502 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00005503 Imm = SplatBits >> 24;
5504 break;
5505 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005506
Owen Andersona4076922010-11-05 21:57:54 +00005507 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
5508 if (type == OtherModImm) return SDValue();
5509
Bob Wilson2e076c42009-06-22 23:27:02 +00005510 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005511 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
5512 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005513 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00005514 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005515 break;
5516 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005517
5518 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005519 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
5520 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005521 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00005522 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005523 break;
5524 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005525
5526 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
5527 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
5528 // VMOV.I32. A (very) minor optimization would be to replicate the value
5529 // and fall through here to test for a valid 64-bit splat. But, then the
5530 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00005531 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005532
5533 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00005534 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005535 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005536 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00005537 uint64_t BitMask = 0xff;
5538 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005539 unsigned ImmMask = 1;
5540 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005541 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005542 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005543 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005544 Imm |= ImmMask;
5545 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005546 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005547 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005548 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005549 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005550 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005551
Mehdi Aminiffc14022015-07-08 01:00:38 +00005552 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005553 // swap higher and lower 32 bit word
5554 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5555
Bob Wilson6eae5202010-06-11 21:34:50 +00005556 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005557 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005558 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005559 break;
5560 }
5561
Bob Wilson6eae5202010-06-11 21:34:50 +00005562 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005563 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005564 }
5565
Bob Wilsona3f19012010-07-13 21:16:48 +00005566 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005567 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005568}
5569
Lang Hames591cdaf2012-03-29 21:56:11 +00005570SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5571 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005572 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005573 return SDValue();
5574
Tim Northoverf79c3a52013-08-20 08:57:11 +00005575 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005576 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005577
Oliver Stannard51b1d462014-08-21 12:50:31 +00005578 // Use the default (constant pool) lowering for double constants when we have
5579 // an SP-only FPU
5580 if (IsDouble && Subtarget->isFPOnlySP())
5581 return SDValue();
5582
Lang Hames591cdaf2012-03-29 21:56:11 +00005583 // Try splatting with a VMOV.f32...
Benjamin Kramer46e38f32016-06-08 10:01:20 +00005584 const APFloat &FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005585 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5586
Lang Hames591cdaf2012-03-29 21:56:11 +00005587 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005588 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5589 // We have code in place to select a valid ConstantFP already, no need to
5590 // do any mangling.
5591 return Op;
5592 }
5593
5594 // It's a float and we are trying to use NEON operations where
5595 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005596 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005597 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005598 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5599 NewVal);
5600 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005601 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005602 }
5603
Tim Northoverf79c3a52013-08-20 08:57:11 +00005604 // The rest of our options are NEON only, make sure that's allowed before
5605 // proceeding..
5606 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5607 return SDValue();
5608
Lang Hames591cdaf2012-03-29 21:56:11 +00005609 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005610 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5611
5612 // It wouldn't really be worth bothering for doubles except for one very
5613 // important value, which does happen to match: 0.0. So make sure we don't do
5614 // anything stupid.
5615 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5616 return SDValue();
5617
5618 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005619 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5620 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005621 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005622 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005623 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5624 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005625 if (IsDouble)
5626 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5627
5628 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005629 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5630 VecConstant);
5631 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005632 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005633 }
5634
5635 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005636 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005637 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005638 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005639 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005640 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005641
5642 if (IsDouble)
5643 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5644
5645 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005646 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5647 VecConstant);
5648 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005649 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005650 }
5651
5652 return SDValue();
5653}
5654
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005655// check if an VEXT instruction can handle the shuffle mask when the
5656// vector sources of the shuffle are the same.
5657static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5658 unsigned NumElts = VT.getVectorNumElements();
5659
5660 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5661 if (M[0] < 0)
5662 return false;
5663
5664 Imm = M[0];
5665
5666 // If this is a VEXT shuffle, the immediate value is the index of the first
5667 // element. The other shuffle indices must be the successive elements after
5668 // the first one.
5669 unsigned ExpectedElt = Imm;
5670 for (unsigned i = 1; i < NumElts; ++i) {
5671 // Increment the expected index. If it wraps around, just follow it
5672 // back to index zero and keep going.
5673 ++ExpectedElt;
5674 if (ExpectedElt == NumElts)
5675 ExpectedElt = 0;
5676
5677 if (M[i] < 0) continue; // ignore UNDEF indices
5678 if (ExpectedElt != static_cast<unsigned>(M[i]))
5679 return false;
5680 }
5681
5682 return true;
5683}
5684
Lang Hames591cdaf2012-03-29 21:56:11 +00005685
Benjamin Kramer339ced42012-01-15 13:16:05 +00005686static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005687 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005688 unsigned NumElts = VT.getVectorNumElements();
5689 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005690
5691 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5692 if (M[0] < 0)
5693 return false;
5694
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005695 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005696
5697 // If this is a VEXT shuffle, the immediate value is the index of the first
5698 // element. The other shuffle indices must be the successive elements after
5699 // the first one.
5700 unsigned ExpectedElt = Imm;
5701 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005702 // Increment the expected index. If it wraps around, it may still be
5703 // a VEXT but the source vectors must be swapped.
5704 ExpectedElt += 1;
5705 if (ExpectedElt == NumElts * 2) {
5706 ExpectedElt = 0;
5707 ReverseVEXT = true;
5708 }
5709
Bob Wilson411dfad2010-08-17 05:54:34 +00005710 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005711 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005712 return false;
5713 }
5714
5715 // Adjust the index value if the source operands will be swapped.
5716 if (ReverseVEXT)
5717 Imm -= NumElts;
5718
Bob Wilson32cd8552009-08-19 17:03:43 +00005719 return true;
5720}
5721
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005722/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5723/// instruction with the specified blocksize. (The order of the elements
5724/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005725static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005726 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5727 "Only possible block sizes for VREV are: 16, 32, 64");
5728
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005729 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005730 if (EltSz == 64)
5731 return false;
5732
5733 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005734 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005735 // If the first shuffle index is UNDEF, be optimistic.
5736 if (M[0] < 0)
5737 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005738
5739 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5740 return false;
5741
5742 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005743 if (M[i] < 0) continue; // ignore UNDEF indices
5744 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005745 return false;
5746 }
5747
5748 return true;
5749}
5750
Benjamin Kramer339ced42012-01-15 13:16:05 +00005751static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005752 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5753 // range, then 0 is placed into the resulting vector. So pretty much any mask
5754 // of 8 elements can work here.
5755 return VT == MVT::v8i8 && M.size() == 8;
5756}
5757
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005758// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5759// checking that pairs of elements in the shuffle mask represent the same index
5760// in each vector, incrementing the expected index by 2 at each step.
5761// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5762// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5763// v2={e,f,g,h}
5764// WhichResult gives the offset for each element in the mask based on which
5765// of the two results it belongs to.
5766//
5767// The transpose can be represented either as:
5768// result1 = shufflevector v1, v2, result1_shuffle_mask
5769// result2 = shufflevector v1, v2, result2_shuffle_mask
5770// where v1/v2 and the shuffle masks have the same number of elements
5771// (here WhichResult (see below) indicates which result is being checked)
5772//
5773// or as:
5774// results = shufflevector v1, v2, shuffle_mask
5775// where both results are returned in one vector and the shuffle mask has twice
5776// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5777// want to check the low half and high half of the shuffle mask as if it were
5778// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005779static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005780 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005781 if (EltSz == 64)
5782 return false;
5783
Bob Wilsona7062312009-08-21 20:54:19 +00005784 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005785 if (M.size() != NumElts && M.size() != NumElts*2)
5786 return false;
5787
James Molloy8c995a92015-09-10 08:42:28 +00005788 // If the mask is twice as long as the input vector then we need to check the
5789 // upper and lower parts of the mask with a matching value for WhichResult
5790 // FIXME: A mask with only even values will be rejected in case the first
5791 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5792 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005793 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005794 if (M.size() == NumElts * 2)
5795 WhichResult = i / NumElts;
5796 else
5797 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005798 for (unsigned j = 0; j < NumElts; j += 2) {
5799 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5800 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5801 return false;
5802 }
Bob Wilsona7062312009-08-21 20:54:19 +00005803 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005804
5805 if (M.size() == NumElts*2)
5806 WhichResult = 0;
5807
Bob Wilsona7062312009-08-21 20:54:19 +00005808 return true;
5809}
5810
Bob Wilson0bbd3072009-12-03 06:40:55 +00005811/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5812/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5813/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005814static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005815 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson0bbd3072009-12-03 06:40:55 +00005816 if (EltSz == 64)
5817 return false;
5818
5819 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005820 if (M.size() != NumElts && M.size() != NumElts*2)
5821 return false;
5822
5823 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005824 if (M.size() == NumElts * 2)
5825 WhichResult = i / NumElts;
5826 else
5827 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005828 for (unsigned j = 0; j < NumElts; j += 2) {
5829 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5830 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5831 return false;
5832 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005833 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005834
5835 if (M.size() == NumElts*2)
5836 WhichResult = 0;
5837
Bob Wilson0bbd3072009-12-03 06:40:55 +00005838 return true;
5839}
5840
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005841// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5842// that the mask elements are either all even and in steps of size 2 or all odd
5843// and in steps of size 2.
5844// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5845// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5846// v2={e,f,g,h}
5847// Requires similar checks to that of isVTRNMask with
5848// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005849static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005850 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005851 if (EltSz == 64)
5852 return false;
5853
Bob Wilsona7062312009-08-21 20:54:19 +00005854 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005855 if (M.size() != NumElts && M.size() != NumElts*2)
5856 return false;
5857
5858 for (unsigned i = 0; i < M.size(); i += NumElts) {
5859 WhichResult = M[i] == 0 ? 0 : 1;
5860 for (unsigned j = 0; j < NumElts; ++j) {
5861 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5862 return false;
5863 }
Bob Wilsona7062312009-08-21 20:54:19 +00005864 }
5865
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005866 if (M.size() == NumElts*2)
5867 WhichResult = 0;
5868
Bob Wilsona7062312009-08-21 20:54:19 +00005869 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005870 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005871 return false;
5872
5873 return true;
5874}
5875
Bob Wilson0bbd3072009-12-03 06:40:55 +00005876/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5877/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5878/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005879static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005880 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson0bbd3072009-12-03 06:40:55 +00005881 if (EltSz == 64)
5882 return false;
5883
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005884 unsigned NumElts = VT.getVectorNumElements();
5885 if (M.size() != NumElts && M.size() != NumElts*2)
5886 return false;
5887
5888 unsigned Half = NumElts / 2;
5889 for (unsigned i = 0; i < M.size(); i += NumElts) {
5890 WhichResult = M[i] == 0 ? 0 : 1;
5891 for (unsigned j = 0; j < NumElts; j += Half) {
5892 unsigned Idx = WhichResult;
5893 for (unsigned k = 0; k < Half; ++k) {
5894 int MIdx = M[i + j + k];
5895 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5896 return false;
5897 Idx += 2;
5898 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005899 }
5900 }
5901
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005902 if (M.size() == NumElts*2)
5903 WhichResult = 0;
5904
Bob Wilson0bbd3072009-12-03 06:40:55 +00005905 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5906 if (VT.is64BitVector() && EltSz == 32)
5907 return false;
5908
5909 return true;
5910}
5911
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005912// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5913// that pairs of elements of the shufflemask represent the same index in each
5914// vector incrementing sequentially through the vectors.
5915// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5916// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5917// v2={e,f,g,h}
5918// Requires similar checks to that of isVTRNMask with respect the how results
5919// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005920static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005921 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005922 if (EltSz == 64)
5923 return false;
5924
Bob Wilsona7062312009-08-21 20:54:19 +00005925 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005926 if (M.size() != NumElts && M.size() != NumElts*2)
5927 return false;
5928
5929 for (unsigned i = 0; i < M.size(); i += NumElts) {
5930 WhichResult = M[i] == 0 ? 0 : 1;
5931 unsigned Idx = WhichResult * NumElts / 2;
5932 for (unsigned j = 0; j < NumElts; j += 2) {
5933 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5934 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5935 return false;
5936 Idx += 1;
5937 }
Bob Wilsona7062312009-08-21 20:54:19 +00005938 }
5939
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005940 if (M.size() == NumElts*2)
5941 WhichResult = 0;
5942
Bob Wilsona7062312009-08-21 20:54:19 +00005943 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005944 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005945 return false;
5946
5947 return true;
5948}
5949
Bob Wilson0bbd3072009-12-03 06:40:55 +00005950/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5951/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5952/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005953static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005954 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson0bbd3072009-12-03 06:40:55 +00005955 if (EltSz == 64)
5956 return false;
5957
5958 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005959 if (M.size() != NumElts && M.size() != NumElts*2)
5960 return false;
5961
5962 for (unsigned i = 0; i < M.size(); i += NumElts) {
5963 WhichResult = M[i] == 0 ? 0 : 1;
5964 unsigned Idx = WhichResult * NumElts / 2;
5965 for (unsigned j = 0; j < NumElts; j += 2) {
5966 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5967 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5968 return false;
5969 Idx += 1;
5970 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005971 }
5972
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005973 if (M.size() == NumElts*2)
5974 WhichResult = 0;
5975
Bob Wilson0bbd3072009-12-03 06:40:55 +00005976 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5977 if (VT.is64BitVector() && EltSz == 32)
5978 return false;
5979
5980 return true;
5981}
5982
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005983/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5984/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5985static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5986 unsigned &WhichResult,
5987 bool &isV_UNDEF) {
5988 isV_UNDEF = false;
5989 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5990 return ARMISD::VTRN;
5991 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5992 return ARMISD::VUZP;
5993 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5994 return ARMISD::VZIP;
5995
5996 isV_UNDEF = true;
5997 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5998 return ARMISD::VTRN;
5999 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
6000 return ARMISD::VUZP;
6001 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
6002 return ARMISD::VZIP;
6003
6004 return 0;
6005}
6006
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006007/// \return true if this is a reverse operation on an vector.
6008static bool isReverseMask(ArrayRef<int> M, EVT VT) {
6009 unsigned NumElts = VT.getVectorNumElements();
6010 // Make sure the mask has the right size.
6011 if (NumElts != M.size())
6012 return false;
6013
6014 // Look for <15, ..., 3, -1, 1, 0>.
6015 for (unsigned i = 0; i != NumElts; ++i)
6016 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
6017 return false;
6018
6019 return true;
6020}
6021
Dale Johannesen2bff5052010-07-29 20:10:08 +00006022// If N is an integer constant that can be moved into a register in one
6023// instruction, return an SDValue of such a constant (will become a MOV
6024// instruction). Otherwise return null.
6025static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006026 const ARMSubtarget *ST, const SDLoc &dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00006027 uint64_t Val;
6028 if (!isa<ConstantSDNode>(N))
6029 return SDValue();
6030 Val = cast<ConstantSDNode>(N)->getZExtValue();
6031
6032 if (ST->isThumb1Only()) {
6033 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006034 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006035 } else {
6036 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006037 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006038 }
6039 return SDValue();
6040}
6041
Bob Wilson2e076c42009-06-22 23:27:02 +00006042// If this is a case we can't handle, return null and let the default
6043// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00006044SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
6045 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00006046 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006047 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006048 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00006049
6050 APInt SplatBits, SplatUndef;
6051 unsigned SplatBitSize;
6052 bool HasAnyUndefs;
6053 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00006054 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00006055 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00006056 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00006057 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00006058 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006059 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00006060 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00006061 if (Val.getNode()) {
6062 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00006063 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00006064 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00006065
6066 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00006067 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00006068 Val = isNEONModifiedImm(NegatedImm,
6069 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006070 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00006071 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00006072 if (Val.getNode()) {
6073 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00006074 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00006075 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00006076
6077 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00006078 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00006079 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00006080 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006081 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00006082 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
6083 }
6084 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00006085 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00006086 }
6087
Bob Wilson91fdf682010-05-22 00:23:12 +00006088 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00006089 //
6090 // As an optimisation, even if more than one value is used it may be more
6091 // profitable to splat with one value then change some lanes.
6092 //
6093 // Heuristically we decide to do this if the vector has a "dominant" value,
6094 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00006095 unsigned NumElts = VT.getVectorNumElements();
6096 bool isOnlyLowElement = true;
6097 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00006098 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00006099 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00006100
6101 // Map of the number of times a particular SDValue appears in the
6102 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00006103 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00006104 SDValue Value;
6105 for (unsigned i = 0; i < NumElts; ++i) {
6106 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006107 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00006108 continue;
6109 if (i > 0)
6110 isOnlyLowElement = false;
6111 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
6112 isConstant = false;
6113
James Molloy49bdbce2012-09-06 09:55:02 +00006114 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00006115 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00006116
James Molloy49bdbce2012-09-06 09:55:02 +00006117 // Is this value dominant? (takes up more than half of the lanes)
6118 if (++Count > (NumElts / 2)) {
6119 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00006120 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00006121 }
Bob Wilson91fdf682010-05-22 00:23:12 +00006122 }
James Molloy49bdbce2012-09-06 09:55:02 +00006123 if (ValueCounts.size() != 1)
6124 usesOnlyOneValue = false;
6125 if (!Value.getNode() && ValueCounts.size() > 0)
6126 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00006127
James Molloy49bdbce2012-09-06 09:55:02 +00006128 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00006129 return DAG.getUNDEF(VT);
6130
Quentin Colombet0f2fe742013-07-23 22:34:47 +00006131 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
6132 // Keep going if we are hitting this case.
6133 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00006134 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
6135
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006136 unsigned EltSize = VT.getScalarSizeInBits();
Dale Johannesen2bff5052010-07-29 20:10:08 +00006137
Dale Johannesen710a2d92010-10-19 20:00:17 +00006138 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
6139 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00006140 if (hasDominantValue && EltSize <= 32) {
6141 if (!isConstant) {
6142 SDValue N;
6143
6144 // If we are VDUPing a value that comes directly from a vector, that will
6145 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00006146 // just use VDUPLANE. We can only do this if the lane being extracted
6147 // is at a constant index, as the VDUP from lane instructions only have
6148 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00006149 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00006150 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00006151 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00006152 // We need to create a new undef vector to use for the VDUPLANE if the
6153 // size of the vector from which we get the value is different than the
6154 // size of the vector that we need to create. We will insert the element
6155 // such that the register coalescer will remove unnecessary copies.
6156 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00006157 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
6158 VT.getVectorNumElements();
6159 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
6160 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006161 Value, DAG.getConstant(index, dl, MVT::i32)),
6162 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00006163 } else
Silviu Barangab1409702012-10-15 09:41:32 +00006164 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00006165 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00006166 } else
James Molloy49bdbce2012-09-06 09:55:02 +00006167 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
6168
6169 if (!usesOnlyOneValue) {
6170 // The dominant value was splatted as 'N', but we now have to insert
6171 // all differing elements.
6172 for (unsigned I = 0; I < NumElts; ++I) {
6173 if (Op.getOperand(I) == Value)
6174 continue;
6175 SmallVector<SDValue, 3> Ops;
6176 Ops.push_back(N);
6177 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006178 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00006179 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00006180 }
6181 }
6182 return N;
6183 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00006184 if (VT.getVectorElementType().isFloatingPoint()) {
6185 SmallVector<SDValue, 8> Ops;
6186 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00006187 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00006188 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00006189 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006190 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00006191 Val = LowerBUILD_VECTOR(Val, DAG, ST);
6192 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00006193 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006194 }
James Molloy49bdbce2012-09-06 09:55:02 +00006195 if (usesOnlyOneValue) {
6196 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
6197 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00006198 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00006199 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00006200 }
6201
6202 // If all elements are constants and the case above didn't get hit, fall back
6203 // to the default expansion, which will generate a load from the constant
6204 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00006205 if (isConstant)
6206 return SDValue();
6207
Bob Wilson6f2b8962011-01-07 21:37:30 +00006208 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
6209 if (NumElts >= 4) {
6210 SDValue shuffle = ReconstructShuffle(Op, DAG);
6211 if (shuffle != SDValue())
6212 return shuffle;
6213 }
6214
Bob Wilson91fdf682010-05-22 00:23:12 +00006215 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00006216 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
6217 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00006218 if (EltSize >= 32) {
6219 // Do the expansion with floating-point types, since that is what the VFP
6220 // registers are defined to use, and since i64 is not legal.
6221 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6222 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006223 SmallVector<SDValue, 8> Ops;
6224 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00006225 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00006226 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006227 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006228 }
6229
Jim Grosbach24e102a2013-07-08 18:18:52 +00006230 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
6231 // know the default expansion would otherwise fall back on something even
6232 // worse. For a vector with one or two non-undef values, that's
6233 // scalar_to_vector for the elements followed by a shuffle (provided the
6234 // shuffle is valid for the target) and materialization element by element
6235 // on the stack followed by a load for everything else.
6236 if (!isConstant && !usesOnlyOneValue) {
6237 SDValue Vec = DAG.getUNDEF(VT);
6238 for (unsigned i = 0 ; i < NumElts; ++i) {
6239 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006240 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00006241 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006242 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00006243 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
6244 }
6245 return Vec;
6246 }
6247
Bob Wilson2e076c42009-06-22 23:27:02 +00006248 return SDValue();
6249}
6250
Bob Wilson6f2b8962011-01-07 21:37:30 +00006251// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00006252// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00006253SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
6254 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006255 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006256 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006257 EVT VT = Op.getValueType();
6258 unsigned NumElts = VT.getVectorNumElements();
6259
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006260 struct ShuffleSourceInfo {
6261 SDValue Vec;
6262 unsigned MinElt;
6263 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00006264
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006265 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
6266 // be compatible with the shuffle we intend to construct. As a result
6267 // ShuffleVec will be some sliding window into the original Vec.
6268 SDValue ShuffleVec;
6269
6270 // Code should guarantee that element i in Vec starts at element "WindowBase
6271 // + i * WindowScale in ShuffleVec".
6272 int WindowBase;
6273 int WindowScale;
6274
6275 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
6276 ShuffleSourceInfo(SDValue Vec)
6277 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
6278 WindowScale(1) {}
6279 };
6280
6281 // First gather all vectors used as an immediate source for this BUILD_VECTOR
6282 // node.
6283 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006284 for (unsigned i = 0; i < NumElts; ++i) {
6285 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006286 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00006287 continue;
6288 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
6289 // A shuffle can only come from building a vector from various
6290 // elements of other vectors.
6291 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00006292 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
6293 // Furthermore, shuffles require a constant mask, whereas extractelts
6294 // accept variable indices.
6295 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00006296 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006297
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006298 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00006299 SDValue SourceVec = V.getOperand(0);
David Majnemer0d955d02016-08-11 22:21:41 +00006300 auto Source = find(Sources, SourceVec);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006301 if (Source == Sources.end())
6302 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00006303
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006304 // Update the minimum and maximum lane number seen.
6305 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
6306 Source->MinElt = std::min(Source->MinElt, EltNo);
6307 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006308 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006309
Bob Wilson6f2b8962011-01-07 21:37:30 +00006310 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006311 // are involved.
6312 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00006313 return SDValue();
6314
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006315 // Find out the smallest element size among result and two sources, and use
6316 // it as element size to build the shuffle_vector.
6317 EVT SmallestEltTy = VT.getVectorElementType();
6318 for (auto &Source : Sources) {
6319 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
6320 if (SrcEltTy.bitsLT(SmallestEltTy))
6321 SmallestEltTy = SrcEltTy;
6322 }
6323 unsigned ResMultiplier =
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006324 VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits();
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006325 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
6326 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00006327
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006328 // If the source vector is too wide or too narrow, we may nevertheless be able
6329 // to construct a compatible shuffle either by concatenating it with UNDEF or
6330 // extracting a suitable range of elements.
6331 for (auto &Src : Sources) {
6332 EVT SrcVT = Src.ShuffleVec.getValueType();
6333
6334 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00006335 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006336
6337 // This stage of the search produces a source with the same element type as
6338 // the original, but with a total width matching the BUILD_VECTOR output.
6339 EVT EltVT = SrcVT.getVectorElementType();
6340 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
6341 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
6342
6343 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
6344 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
6345 return SDValue();
6346 // We can pad out the smaller vector for free, so if it's part of a
6347 // shuffle...
6348 Src.ShuffleVec =
6349 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
6350 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
6351 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006352 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006353
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006354 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
6355 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006356
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006357 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00006358 // Span too large for a VEXT to cope
6359 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006360 }
6361
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006362 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00006363 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006364 Src.ShuffleVec =
6365 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6366 DAG.getConstant(NumSrcElts, dl, MVT::i32));
6367 Src.WindowBase = -NumSrcElts;
6368 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00006369 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006370 Src.ShuffleVec =
6371 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6372 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00006373 } else {
6374 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006375 SDValue VEXTSrc1 =
6376 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6377 DAG.getConstant(0, dl, MVT::i32));
6378 SDValue VEXTSrc2 =
6379 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6380 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006381
6382 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
6383 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00006384 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006385 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006386 }
6387 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006388
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006389 // Another possible incompatibility occurs from the vector element types. We
6390 // can fix this by bitcasting the source vectors to the same type we intend
6391 // for the shuffle.
6392 for (auto &Src : Sources) {
6393 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
6394 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00006395 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006396 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
6397 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
6398 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
6399 Src.WindowBase *= Src.WindowScale;
6400 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006401
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006402 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00006403 DEBUG(
6404 for (auto Src : Sources)
6405 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
6406 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006407
6408 // The stars all align, our next step is to produce the mask for the shuffle.
6409 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006410 int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits();
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006411 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
6412 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006413 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006414 continue;
6415
David Majnemer0d955d02016-08-11 22:21:41 +00006416 auto Src = find(Sources, Entry.getOperand(0));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006417 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
6418
6419 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
6420 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
6421 // segment.
6422 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
6423 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006424 VT.getScalarSizeInBits());
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006425 int LanesDefined = BitsDefined / BitsPerShuffleLane;
6426
6427 // This source is expected to fill ResMultiplier lanes of the final shuffle,
6428 // starting at the appropriate offset.
6429 int *LaneMask = &Mask[i * ResMultiplier];
6430
6431 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
6432 ExtractBase += NumElts * (Src - Sources.begin());
6433 for (int j = 0; j < LanesDefined; ++j)
6434 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006435 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006436
Bob Wilson6f2b8962011-01-07 21:37:30 +00006437 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006438 if (!isShuffleMaskLegal(Mask, ShuffleVT))
6439 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006440
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006441 // We can't handle more than two sources. This should have already
6442 // been checked before this point.
6443 assert(Sources.size() <= 2 && "Too many sources!");
6444
6445 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
6446 for (unsigned i = 0; i < Sources.size(); ++i)
6447 ShuffleOps[i] = Sources[i].ShuffleVec;
6448
6449 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
Craig Topper2bd8b4b2016-07-01 06:54:47 +00006450 ShuffleOps[1], Mask);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006451 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006452}
6453
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006454/// isShuffleMaskLegal - Targets can use this to indicate that they only
6455/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6456/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6457/// are assumed to be legal.
6458bool
6459ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6460 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006461 if (VT.getVectorNumElements() == 4 &&
6462 (VT.is128BitVector() || VT.is64BitVector())) {
6463 unsigned PFIndexes[4];
6464 for (unsigned i = 0; i != 4; ++i) {
6465 if (M[i] < 0)
6466 PFIndexes[i] = 8;
6467 else
6468 PFIndexes[i] = M[i];
6469 }
6470
6471 // Compute the index in the perfect shuffle table.
6472 unsigned PFTableIndex =
6473 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
6474 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6475 unsigned Cost = (PFEntry >> 30);
6476
6477 if (Cost <= 4)
6478 return true;
6479 }
6480
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006481 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00006482 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006483
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006484 unsigned EltSize = VT.getScalarSizeInBits();
Bob Wilson846bd792010-06-07 23:53:38 +00006485 return (EltSize >= 32 ||
6486 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006487 isVREVMask(M, VT, 64) ||
6488 isVREVMask(M, VT, 32) ||
6489 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00006490 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00006491 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006492 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006493 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006494}
6495
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006496/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6497/// the specified operations to build the shuffle.
6498static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
6499 SDValue RHS, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006500 const SDLoc &dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006501 unsigned OpNum = (PFEntry >> 26) & 0x0F;
6502 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
6503 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
6504
6505 enum {
6506 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
6507 OP_VREV,
6508 OP_VDUP0,
6509 OP_VDUP1,
6510 OP_VDUP2,
6511 OP_VDUP3,
6512 OP_VEXT1,
6513 OP_VEXT2,
6514 OP_VEXT3,
6515 OP_VUZPL, // VUZP, left result
6516 OP_VUZPR, // VUZP, right result
6517 OP_VZIPL, // VZIP, left result
6518 OP_VZIPR, // VZIP, right result
6519 OP_VTRNL, // VTRN, left result
6520 OP_VTRNR // VTRN, right result
6521 };
6522
6523 if (OpNum == OP_COPY) {
6524 if (LHSID == (1*9+2)*9+3) return LHS;
6525 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6526 return RHS;
6527 }
6528
6529 SDValue OpLHS, OpRHS;
6530 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6531 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6532 EVT VT = OpLHS.getValueType();
6533
6534 switch (OpNum) {
6535 default: llvm_unreachable("Unknown shuffle opcode!");
6536 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00006537 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00006538 if (VT.getVectorElementType() == MVT::i32 ||
6539 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00006540 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6541 // vrev <4 x i16> -> VREV32
6542 if (VT.getVectorElementType() == MVT::i16)
6543 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6544 // vrev <4 x i8> -> VREV16
6545 assert(VT.getVectorElementType() == MVT::i8);
6546 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006547 case OP_VDUP0:
6548 case OP_VDUP1:
6549 case OP_VDUP2:
6550 case OP_VDUP3:
6551 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006552 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006553 case OP_VEXT1:
6554 case OP_VEXT2:
6555 case OP_VEXT3:
6556 return DAG.getNode(ARMISD::VEXT, dl, VT,
6557 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006558 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006559 case OP_VUZPL:
6560 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006561 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006562 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6563 case OP_VZIPL:
6564 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006565 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006566 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6567 case OP_VTRNL:
6568 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006569 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6570 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006571 }
6572}
6573
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006574static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006575 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006576 SelectionDAG &DAG) {
6577 // Check to see if we can use the VTBL instruction.
6578 SDValue V1 = Op.getOperand(0);
6579 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006580 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006581
6582 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006583 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006584 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006585 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006586
Sanjay Patel57195842016-03-14 17:28:46 +00006587 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006588 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006589 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006590
Owen Anderson77aa2662011-04-05 21:48:57 +00006591 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006592 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006593}
6594
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006595static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6596 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006597 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006598 SDValue OpLHS = Op.getOperand(0);
6599 EVT VT = OpLHS.getValueType();
6600
6601 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6602 "Expect an v8i16/v16i8 type");
6603 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6604 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6605 // extract the first 8 bytes into the top double word and the last 8 bytes
6606 // into the bottom double word. The v8i16 case is similar.
6607 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6608 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006609 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006610}
6611
Bob Wilson2e076c42009-06-22 23:27:02 +00006612static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006613 SDValue V1 = Op.getOperand(0);
6614 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006615 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006616 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006617 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006618
Bob Wilsonc6800b52009-08-13 02:13:04 +00006619 // Convert shuffles that are directly supported on NEON to target-specific
6620 // DAG nodes, instead of keeping them as shuffles and matching them again
6621 // during code selection. This is more efficient and avoids the possibility
6622 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006623 // FIXME: floating-point vectors should be canonicalized to integer vectors
6624 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006625 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006626
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006627 unsigned EltSize = VT.getScalarSizeInBits();
Bob Wilson846bd792010-06-07 23:53:38 +00006628 if (EltSize <= 32) {
Craig Topperbc56e3b2016-06-30 04:38:51 +00006629 if (SVN->isSplat()) {
Bob Wilson846bd792010-06-07 23:53:38 +00006630 int Lane = SVN->getSplatIndex();
6631 // If this is undef splat, generate it via "just" vdup, if possible.
6632 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006633
Dan Gohman198b7ff2011-11-03 21:49:52 +00006634 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006635 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6636 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6637 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006638 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6639 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6640 // reaches it).
6641 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6642 !isa<ConstantSDNode>(V1.getOperand(0))) {
6643 bool IsScalarToVector = true;
6644 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006645 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006646 IsScalarToVector = false;
6647 break;
6648 }
6649 if (IsScalarToVector)
6650 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6651 }
Bob Wilson846bd792010-06-07 23:53:38 +00006652 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006653 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006654 }
Bob Wilson846bd792010-06-07 23:53:38 +00006655
6656 bool ReverseVEXT;
6657 unsigned Imm;
6658 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6659 if (ReverseVEXT)
6660 std::swap(V1, V2);
6661 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006662 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006663 }
6664
6665 if (isVREVMask(ShuffleMask, VT, 64))
6666 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6667 if (isVREVMask(ShuffleMask, VT, 32))
6668 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6669 if (isVREVMask(ShuffleMask, VT, 16))
6670 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6671
Sanjay Patel57195842016-03-14 17:28:46 +00006672 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006673 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006674 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006675 }
6676
Bob Wilson846bd792010-06-07 23:53:38 +00006677 // Check for Neon shuffles that modify both input vectors in place.
6678 // If both results are used, i.e., if there are two shuffles with the same
6679 // source operands and with masks corresponding to both results of one of
6680 // these operations, DAG memoization will ensure that a single node is
6681 // used for both shuffles.
6682 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006683 bool isV_UNDEF;
6684 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6685 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6686 if (isV_UNDEF)
6687 V2 = V1;
6688 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6689 .getValue(WhichResult);
6690 }
Bob Wilson846bd792010-06-07 23:53:38 +00006691
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006692 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6693 // shuffles that produce a result larger than their operands with:
6694 // shuffle(concat(v1, undef), concat(v2, undef))
6695 // ->
6696 // shuffle(concat(v1, v2), undef)
6697 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6698 //
6699 // This is useful in the general case, but there are special cases where
6700 // native shuffles produce larger results: the two-result ops.
6701 //
6702 // Look through the concat when lowering them:
6703 // shuffle(concat(v1, v2), undef)
6704 // ->
6705 // concat(VZIP(v1, v2):0, :1)
6706 //
Sanjay Patel57195842016-03-14 17:28:46 +00006707 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006708 SDValue SubV1 = V1->getOperand(0);
6709 SDValue SubV2 = V1->getOperand(1);
6710 EVT SubVT = SubV1.getValueType();
6711
6712 // We expect these to have been canonicalized to -1.
David Majnemer0a16c222016-08-11 21:15:00 +00006713 assert(all_of(ShuffleMask, [&](int i) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006714 return i < (int)VT.getVectorNumElements();
6715 }) && "Unexpected shuffle index into UNDEF operand!");
6716
6717 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6718 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6719 if (isV_UNDEF)
6720 SubV2 = SubV1;
6721 assert((WhichResult == 0) &&
6722 "In-place shuffle of concat can only have one result!");
6723 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6724 SubV1, SubV2);
6725 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6726 Res.getValue(1));
6727 }
6728 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006729 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006730
Bob Wilsona7062312009-08-21 20:54:19 +00006731 // If the shuffle is not directly supported and it has 4 elements, use
6732 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006733 unsigned NumElts = VT.getVectorNumElements();
6734 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006735 unsigned PFIndexes[4];
6736 for (unsigned i = 0; i != 4; ++i) {
6737 if (ShuffleMask[i] < 0)
6738 PFIndexes[i] = 8;
6739 else
6740 PFIndexes[i] = ShuffleMask[i];
6741 }
6742
6743 // Compute the index in the perfect shuffle table.
6744 unsigned PFTableIndex =
6745 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006746 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6747 unsigned Cost = (PFEntry >> 30);
6748
6749 if (Cost <= 4)
6750 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6751 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006752
Bob Wilsond8a9a042010-06-04 00:04:02 +00006753 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006754 if (EltSize >= 32) {
6755 // Do the expansion with floating-point types, since that is what the VFP
6756 // registers are defined to use, and since i64 is not legal.
6757 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6758 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006759 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6760 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006761 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006762 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006763 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006764 Ops.push_back(DAG.getUNDEF(EltVT));
6765 else
6766 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6767 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6768 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006769 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006770 }
Craig Topper48d114b2014-04-26 18:35:24 +00006771 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006772 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006773 }
6774
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006775 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6776 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6777
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006778 if (VT == MVT::v8i8)
6779 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006780 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006781
Bob Wilson6f34e272009-08-14 05:16:33 +00006782 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006783}
6784
Eli Friedmana5e244c2011-10-24 23:08:52 +00006785static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6786 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6787 SDValue Lane = Op.getOperand(2);
6788 if (!isa<ConstantSDNode>(Lane))
6789 return SDValue();
6790
6791 return Op;
6792}
6793
Bob Wilson2e076c42009-06-22 23:27:02 +00006794static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006795 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006796 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006797 if (!isa<ConstantSDNode>(Lane))
6798 return SDValue();
6799
6800 SDValue Vec = Op.getOperand(0);
Sanjay Patel284582b2016-09-14 16:54:10 +00006801 if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006802 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006803 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6804 }
6805
6806 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006807}
6808
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006809static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6810 // The only time a CONCAT_VECTORS operation can have legal types is when
6811 // two 64-bit vectors are concatenated to a 128-bit vector.
6812 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6813 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006814 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006815 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006816 SDValue Op0 = Op.getOperand(0);
6817 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006818 if (!Op0.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006819 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006820 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006821 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006822 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006823 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006824 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006825 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006826 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006827}
6828
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006829/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6830/// element has been zero/sign-extended, depending on the isSigned parameter,
6831/// from an integer type half its size.
6832static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6833 bool isSigned) {
6834 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6835 EVT VT = N->getValueType(0);
6836 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6837 SDNode *BVN = N->getOperand(0).getNode();
6838 if (BVN->getValueType(0) != MVT::v4i32 ||
6839 BVN->getOpcode() != ISD::BUILD_VECTOR)
6840 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006841 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006842 unsigned HiElt = 1 - LoElt;
6843 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6844 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6845 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6846 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6847 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6848 return false;
6849 if (isSigned) {
6850 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6851 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6852 return true;
6853 } else {
6854 if (Hi0->isNullValue() && Hi1->isNullValue())
6855 return true;
6856 }
6857 return false;
6858 }
6859
6860 if (N->getOpcode() != ISD::BUILD_VECTOR)
6861 return false;
6862
6863 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6864 SDNode *Elt = N->getOperand(i).getNode();
6865 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006866 unsigned EltSize = VT.getScalarSizeInBits();
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006867 unsigned HalfSize = EltSize / 2;
6868 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006869 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006870 return false;
6871 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006872 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006873 return false;
6874 }
6875 continue;
6876 }
6877 return false;
6878 }
6879
6880 return true;
6881}
6882
6883/// isSignExtended - Check if a node is a vector value that is sign-extended
6884/// or a constant BUILD_VECTOR with sign-extended elements.
6885static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6886 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6887 return true;
6888 if (isExtendedBUILD_VECTOR(N, DAG, true))
6889 return true;
6890 return false;
6891}
6892
6893/// isZeroExtended - Check if a node is a vector value that is zero-extended
6894/// or a constant BUILD_VECTOR with zero-extended elements.
6895static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6896 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6897 return true;
6898 if (isExtendedBUILD_VECTOR(N, DAG, false))
6899 return true;
6900 return false;
6901}
6902
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006903static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6904 if (OrigVT.getSizeInBits() >= 64)
6905 return OrigVT;
6906
6907 assert(OrigVT.isSimple() && "Expecting a simple value type");
6908
6909 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6910 switch (OrigSimpleTy) {
6911 default: llvm_unreachable("Unexpected Vector Type");
6912 case MVT::v2i8:
6913 case MVT::v2i16:
6914 return MVT::v2i32;
6915 case MVT::v4i8:
6916 return MVT::v4i16;
6917 }
6918}
6919
Sebastian Popa204f722012-11-30 19:08:04 +00006920/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6921/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6922/// We insert the required extension here to get the vector to fill a D register.
6923static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6924 const EVT &OrigTy,
6925 const EVT &ExtTy,
6926 unsigned ExtOpcode) {
6927 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6928 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6929 // 64-bits we need to insert a new extension so that it will be 64-bits.
6930 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6931 if (OrigTy.getSizeInBits() >= 64)
6932 return N;
6933
6934 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006935 EVT NewVT = getExtensionTo64Bits(OrigTy);
6936
Andrew Trickef9de2a2013-05-25 02:42:55 +00006937 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006938}
6939
6940/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6941/// does not do any sign/zero extension. If the original vector is less
6942/// than 64 bits, an appropriate extension will be added after the load to
6943/// reach a total size of 64 bits. We have to add the extension separately
6944/// because ARM does not have a sign/zero extending load for vectors.
6945static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006946 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6947
6948 // The load already has the right type.
6949 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006950 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Justin Lebar9c375812016-07-15 18:27:10 +00006951 LD->getBasePtr(), LD->getPointerInfo(),
6952 LD->getAlignment(), LD->getMemOperand()->getFlags());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006953
6954 // We need to create a zextload/sextload. We cannot just create a load
6955 // followed by a zext/zext node because LowerMUL is also run during normal
6956 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006957 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006958 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00006959 LD->getMemoryVT(), LD->getAlignment(),
6960 LD->getMemOperand()->getFlags());
Sebastian Popa204f722012-11-30 19:08:04 +00006961}
6962
6963/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6964/// extending load, or BUILD_VECTOR with extended elements, return the
6965/// unextended value. The unextended vector should be 64 bits so that it can
6966/// be used as an operand to a VMULL instruction. If the original vector size
6967/// before extension is less than 64 bits we add a an extension to resize
6968/// the vector to 64 bits.
6969static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006970 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006971 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6972 N->getOperand(0)->getValueType(0),
6973 N->getValueType(0),
6974 N->getOpcode());
6975
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006976 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006977 return SkipLoadExtensionForVMULL(LD, DAG);
6978
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006979 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6980 // have been legalized as a BITCAST from v4i32.
6981 if (N->getOpcode() == ISD::BITCAST) {
6982 SDNode *BVN = N->getOperand(0).getNode();
6983 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6984 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006985 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006986 return DAG.getBuildVector(
6987 MVT::v2i32, SDLoc(N),
6988 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)});
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006989 }
6990 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6991 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6992 EVT VT = N->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006993 unsigned EltSize = VT.getScalarSizeInBits() / 2;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006994 unsigned NumElts = VT.getVectorNumElements();
6995 MVT TruncVT = MVT::getIntegerVT(EltSize);
6996 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006997 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006998 for (unsigned i = 0; i != NumElts; ++i) {
6999 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
7000 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00007001 // Element types smaller than 32 bits are not legal, so use i32 elements.
7002 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007003 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00007004 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00007005 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007006}
7007
Evan Chenge2086e72011-03-29 01:56:09 +00007008static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
7009 unsigned Opcode = N->getOpcode();
7010 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
7011 SDNode *N0 = N->getOperand(0).getNode();
7012 SDNode *N1 = N->getOperand(1).getNode();
7013 return N0->hasOneUse() && N1->hasOneUse() &&
7014 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
7015 }
7016 return false;
7017}
7018
7019static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
7020 unsigned Opcode = N->getOpcode();
7021 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
7022 SDNode *N0 = N->getOperand(0).getNode();
7023 SDNode *N1 = N->getOperand(1).getNode();
7024 return N0->hasOneUse() && N1->hasOneUse() &&
7025 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
7026 }
7027 return false;
7028}
7029
Bob Wilson38ab35a2010-09-01 23:50:19 +00007030static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
7031 // Multiplications are only custom-lowered for 128-bit vectors so that
7032 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
7033 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00007034 assert(VT.is128BitVector() && VT.isInteger() &&
7035 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00007036 SDNode *N0 = Op.getOperand(0).getNode();
7037 SDNode *N1 = Op.getOperand(1).getNode();
7038 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00007039 bool isMLA = false;
7040 bool isN0SExt = isSignExtended(N0, DAG);
7041 bool isN1SExt = isSignExtended(N1, DAG);
7042 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00007043 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00007044 else {
7045 bool isN0ZExt = isZeroExtended(N0, DAG);
7046 bool isN1ZExt = isZeroExtended(N1, DAG);
7047 if (isN0ZExt && isN1ZExt)
7048 NewOpc = ARMISD::VMULLu;
7049 else if (isN1SExt || isN1ZExt) {
7050 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
7051 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
7052 if (isN1SExt && isAddSubSExt(N0, DAG)) {
7053 NewOpc = ARMISD::VMULLs;
7054 isMLA = true;
7055 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
7056 NewOpc = ARMISD::VMULLu;
7057 isMLA = true;
7058 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
7059 std::swap(N0, N1);
7060 NewOpc = ARMISD::VMULLu;
7061 isMLA = true;
7062 }
7063 }
7064
7065 if (!NewOpc) {
7066 if (VT == MVT::v2i64)
7067 // Fall through to expand this. It is not legal.
7068 return SDValue();
7069 else
7070 // Other vector multiplications are legal.
7071 return Op;
7072 }
7073 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00007074
7075 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007076 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00007077 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00007078 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00007079 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00007080 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00007081 assert(Op0.getValueType().is64BitVector() &&
7082 Op1.getValueType().is64BitVector() &&
7083 "unexpected types for extended operands to VMULL");
7084 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
7085 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00007086
Evan Chenge2086e72011-03-29 01:56:09 +00007087 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
7088 // isel lowering to take advantage of no-stall back to back vmul + vmla.
7089 // vmull q0, d4, d6
7090 // vmlal q0, d5, d6
7091 // is faster than
7092 // vaddl q0, d4, d5
7093 // vmovl q1, d6
7094 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00007095 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
7096 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00007097 EVT Op1VT = Op1.getValueType();
7098 return DAG.getNode(N0->getOpcode(), DL, VT,
7099 DAG.getNode(NewOpc, DL, VT,
7100 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
7101 DAG.getNode(NewOpc, DL, VT,
7102 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00007103}
7104
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007105static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl,
7106 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00007107 // TODO: Should this propagate fast-math-flags?
7108
Nate Begemanfa62d502011-02-11 20:53:29 +00007109 // Convert to float
7110 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
7111 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
7112 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
7113 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
7114 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
7115 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
7116 // Get reciprocal estimate.
7117 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00007118 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007119 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7120 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00007121 // Because char has a smaller range than uchar, we can actually get away
7122 // without any newton steps. This requires that we use a weird bias
7123 // of 0xb000, however (again, this has been exhaustively tested).
7124 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
7125 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
7126 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007127 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00007128 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
7129 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
7130 // Convert back to short.
7131 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
7132 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
7133 return X;
7134}
7135
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007136static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl,
7137 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00007138 // TODO: Should this propagate fast-math-flags?
7139
Nate Begemanfa62d502011-02-11 20:53:29 +00007140 SDValue N2;
7141 // Convert to float.
7142 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
7143 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
7144 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
7145 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
7146 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
7147 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007148
Nate Begemanfa62d502011-02-11 20:53:29 +00007149 // Use reciprocal estimate and one refinement step.
7150 // float4 recip = vrecpeq_f32(yf);
7151 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00007152 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007153 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7154 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007155 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007156 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00007157 N1, N2);
7158 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7159 // Because short has a smaller range than ushort, we can actually get away
7160 // with only a single newton step. This requires that we use a weird bias
7161 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007162 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00007163 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
7164 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007165 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00007166 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
7167 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
7168 // Convert back to integer and return.
7169 // return vmovn_s32(vcvt_s32_f32(result));
7170 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
7171 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
7172 return N0;
7173}
7174
7175static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
7176 EVT VT = Op.getValueType();
7177 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
7178 "unexpected type for custom-lowering ISD::SDIV");
7179
Andrew Trickef9de2a2013-05-25 02:42:55 +00007180 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00007181 SDValue N0 = Op.getOperand(0);
7182 SDValue N1 = Op.getOperand(1);
7183 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00007184
Nate Begemanfa62d502011-02-11 20:53:29 +00007185 if (VT == MVT::v8i8) {
7186 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
7187 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007188
Nate Begemanfa62d502011-02-11 20:53:29 +00007189 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007190 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007191 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007192 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007193 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007194 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007195 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007196 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007197
7198 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
7199 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
7200
7201 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
7202 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00007203
Nate Begemanfa62d502011-02-11 20:53:29 +00007204 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
7205 return N0;
7206 }
7207 return LowerSDIV_v4i16(N0, N1, dl, DAG);
7208}
7209
7210static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00007211 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00007212 EVT VT = Op.getValueType();
7213 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
7214 "unexpected type for custom-lowering ISD::UDIV");
7215
Andrew Trickef9de2a2013-05-25 02:42:55 +00007216 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00007217 SDValue N0 = Op.getOperand(0);
7218 SDValue N1 = Op.getOperand(1);
7219 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00007220
Nate Begemanfa62d502011-02-11 20:53:29 +00007221 if (VT == MVT::v8i8) {
7222 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
7223 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007224
Nate Begemanfa62d502011-02-11 20:53:29 +00007225 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007226 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007227 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007228 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007229 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007230 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007231 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007232 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00007233
Nate Begemanfa62d502011-02-11 20:53:29 +00007234 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
7235 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00007236
Nate Begemanfa62d502011-02-11 20:53:29 +00007237 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
7238 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00007239
7240 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007241 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
7242 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00007243 N0);
7244 return N0;
7245 }
Owen Anderson77aa2662011-04-05 21:48:57 +00007246
Nate Begemanfa62d502011-02-11 20:53:29 +00007247 // v4i16 sdiv ... Convert to float.
7248 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
7249 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
7250 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
7251 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
7252 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007253 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00007254
7255 // Use reciprocal estimate and two refinement steps.
7256 // float4 recip = vrecpeq_f32(yf);
7257 // recip *= vrecpsq_f32(yf, recip);
7258 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00007259 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007260 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7261 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007262 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007263 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007264 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00007265 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00007266 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007267 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007268 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00007269 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7270 // Simply multiplying by the reciprocal estimate can leave us a few ulps
7271 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
7272 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007273 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00007274 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
7275 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007276 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00007277 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
7278 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
7279 // Convert back to integer and return.
7280 // return vmovn_u32(vcvt_s32_f32(result));
7281 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
7282 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
7283 return N0;
7284}
7285
Evan Chenge8916542011-08-30 01:34:54 +00007286static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
7287 EVT VT = Op.getNode()->getValueType(0);
7288 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
7289
7290 unsigned Opc;
7291 bool ExtraOp = false;
7292 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00007293 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00007294 case ISD::ADDC: Opc = ARMISD::ADDC; break;
7295 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
7296 case ISD::SUBC: Opc = ARMISD::SUBC; break;
7297 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
7298 }
7299
7300 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007301 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00007302 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007303 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00007304 Op.getOperand(1), Op.getOperand(2));
7305}
7306
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007307SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
7308 assert(Subtarget->isTargetDarwin());
7309
7310 // For iOS, we want to call an alternative entry point: __sincos_stret,
7311 // return values are passed via sret.
7312 SDLoc dl(Op);
7313 SDValue Arg = Op.getOperand(0);
7314 EVT ArgVT = Arg.getValueType();
7315 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00007316 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007317
Matthias Braun941a7052016-07-28 18:40:00 +00007318 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00007319 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007320
7321 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00007322 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00007323 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007324
7325 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00007326 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
7327 SDValue SRet;
7328 if (ShouldUseSRet) {
7329 // Create stack object for sret.
7330 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
7331 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Matthias Braun941a7052016-07-28 18:40:00 +00007332 int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
Tim Northover8b403662015-10-28 22:51:16 +00007333 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
7334
7335 ArgListEntry Entry;
7336 Entry.Node = SRet;
7337 Entry.Ty = RetTy->getPointerTo();
7338 Entry.isSExt = false;
7339 Entry.isZExt = false;
7340 Entry.isSRet = true;
7341 Args.push_back(Entry);
7342 RetTy = Type::getVoidTy(*DAG.getContext());
7343 }
7344
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007345 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007346 Entry.Node = Arg;
7347 Entry.Ty = ArgTy;
7348 Entry.isSExt = false;
7349 Entry.isZExt = false;
7350 Args.push_back(Entry);
7351
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00007352 const char *LibcallName =
7353 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00007354 RTLIB::Libcall LC =
7355 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
7356 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00007357 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007358
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00007359 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00007360 CLI.setDebugLoc(dl)
7361 .setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00007362 .setCallee(CC, RetTy, Callee, std::move(Args))
Tim Northover8b403662015-10-28 22:51:16 +00007363 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007364 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
7365
Tim Northover8b403662015-10-28 22:51:16 +00007366 if (!ShouldUseSRet)
7367 return CallResult.first;
7368
Justin Lebar9c375812016-07-15 18:27:10 +00007369 SDValue LoadSin =
7370 DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007371
7372 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00007373 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007374 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Justin Lebar9c375812016-07-15 18:27:10 +00007375 SDValue LoadCos =
7376 DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007377
7378 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
7379 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
7380 LoadSin.getValue(0), LoadCos.getValue(0));
7381}
7382
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007383SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00007384 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007385 SDValue &Chain) const {
7386 EVT VT = Op.getValueType();
7387 assert((VT == MVT::i32 || VT == MVT::i64) &&
7388 "unexpected type for custom lowering DIV");
7389 SDLoc dl(Op);
7390
7391 const auto &DL = DAG.getDataLayout();
7392 const auto &TLI = DAG.getTargetLoweringInfo();
7393
7394 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00007395 if (Signed)
7396 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
7397 else
7398 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007399
7400 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
7401
7402 ARMTargetLowering::ArgListTy Args;
7403
7404 for (auto AI : {1, 0}) {
7405 ArgListEntry Arg;
7406 Arg.Node = Op.getOperand(AI);
7407 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
7408 Args.push_back(Arg);
7409 }
7410
7411 CallLoweringInfo CLI(DAG);
7412 CLI.setDebugLoc(dl)
7413 .setChain(Chain)
7414 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00007415 ES, std::move(Args));
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007416
7417 return LowerCallTo(CLI).first;
7418}
7419
Martell Maloned1229242015-11-26 15:34:03 +00007420SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
7421 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00007422 assert(Op.getValueType() == MVT::i32 &&
7423 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007424 SDLoc dl(Op);
7425
7426 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
7427 DAG.getEntryNode(), Op.getOperand(1));
7428
Martell Maloned1229242015-11-26 15:34:03 +00007429 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007430}
7431
Martin Storsjo04864f42016-10-07 13:28:53 +00007432static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) {
7433 SDLoc DL(N);
7434 SDValue Op = N->getOperand(1);
7435 if (N->getValueType(0) == MVT::i32)
7436 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op);
7437 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
7438 DAG.getConstant(0, DL, MVT::i32));
7439 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
7440 DAG.getConstant(1, DL, MVT::i32));
7441 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain,
7442 DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi));
7443}
7444
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007445void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00007446 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007447 SmallVectorImpl<SDValue> &Results) const {
7448 const auto &DL = DAG.getDataLayout();
7449 const auto &TLI = DAG.getTargetLoweringInfo();
7450
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00007451 assert(Op.getValueType() == MVT::i64 &&
7452 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007453 SDLoc dl(Op);
7454
Martin Storsjo04864f42016-10-07 13:28:53 +00007455 SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode());
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007456
Martell Maloned1229242015-11-26 15:34:03 +00007457 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007458
7459 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
7460 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
7461 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
7462 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
7463
7464 Results.push_back(Lower);
7465 Results.push_back(Upper);
7466}
7467
Eli Friedman10f9ce22011-09-15 22:26:18 +00007468static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00007469 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
7470 // Acquire/Release load/store is not legal for targets without a dmb or
7471 // equivalent available.
7472 return SDValue();
Eli Friedmanba912e02011-09-15 22:18:49 +00007473
JF Bastien800f87a2016-04-06 21:19:33 +00007474 // Monotonic load/store is legal for all targets.
7475 return Op;
Eli Friedmanba912e02011-09-15 22:18:49 +00007476}
7477
Tim Northoverbc933082013-05-23 19:11:20 +00007478static void ReplaceREADCYCLECOUNTER(SDNode *N,
7479 SmallVectorImpl<SDValue> &Results,
7480 SelectionDAG &DAG,
7481 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007482 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00007483 // Under Power Management extensions, the cycle-count is:
7484 // mrc p15, #0, <Rt>, c9, c13, #0
7485 SDValue Ops[] = { N->getOperand(0), // Chain
7486 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
7487 DAG.getConstant(15, DL, MVT::i32),
7488 DAG.getConstant(0, DL, MVT::i32),
7489 DAG.getConstant(9, DL, MVT::i32),
7490 DAG.getConstant(13, DL, MVT::i32),
7491 DAG.getConstant(0, DL, MVT::i32)
7492 };
Tim Northoverbc933082013-05-23 19:11:20 +00007493
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00007494 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
7495 DAG.getVTList(MVT::i32, MVT::Other), Ops);
7496 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
7497 DAG.getConstant(0, DL, MVT::i32)));
7498 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00007499}
7500
Tim Northover1ee27c72016-04-19 22:25:02 +00007501static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) {
7502 SDLoc dl(V.getNode());
7503 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32);
7504 SDValue VHi = DAG.getAnyExtOrTrunc(
7505 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)),
7506 dl, MVT::i32);
Tim Northoverb629c772016-04-18 21:48:55 +00007507 SDValue RegClass =
7508 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
7509 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
7510 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
Tim Northover1ee27c72016-04-19 22:25:02 +00007511 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
Tim Northoverb629c772016-04-18 21:48:55 +00007512 return SDValue(
7513 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
7514}
7515
7516static void ReplaceCMP_SWAP_64Results(SDNode *N,
7517 SmallVectorImpl<SDValue> & Results,
7518 SelectionDAG &DAG) {
7519 assert(N->getValueType(0) == MVT::i64 &&
7520 "AtomicCmpSwap on types less than 64 should be legal");
7521 SDValue Ops[] = {N->getOperand(1),
Tim Northover1ee27c72016-04-19 22:25:02 +00007522 createGPRPairNode(DAG, N->getOperand(2)),
7523 createGPRPairNode(DAG, N->getOperand(3)),
Tim Northoverb629c772016-04-18 21:48:55 +00007524 N->getOperand(0)};
7525 SDNode *CmpSwap = DAG.getMachineNode(
7526 ARM::CMP_SWAP_64, SDLoc(N),
7527 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
7528
7529 MachineFunction &MF = DAG.getMachineFunction();
7530 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
7531 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
7532 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
7533
7534 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_0, SDLoc(N), MVT::i32,
7535 SDValue(CmpSwap, 0)));
7536 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_1, SDLoc(N), MVT::i32,
7537 SDValue(CmpSwap, 0)));
7538 Results.push_back(SDValue(CmpSwap, 2));
7539}
7540
Saleem Abdulrasool804e12e2016-11-06 19:46:54 +00007541static SDValue LowerFPOWI(SDValue Op, const ARMSubtarget &Subtarget,
7542 SelectionDAG &DAG) {
7543 const auto &TLI = DAG.getTargetLoweringInfo();
7544
7545 assert(Subtarget.getTargetTriple().isOSMSVCRT() &&
7546 "Custom lowering is MSVCRT specific!");
7547
7548 SDLoc dl(Op);
7549 SDValue Val = Op.getOperand(0);
7550 MVT Ty = Val->getSimpleValueType(0);
7551 SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, dl, Ty, Op.getOperand(1));
7552 SDValue Callee = DAG.getExternalSymbol(Ty == MVT::f32 ? "powf" : "pow",
7553 TLI.getPointerTy(DAG.getDataLayout()));
7554
7555 TargetLowering::ArgListTy Args;
7556 TargetLowering::ArgListEntry Entry;
7557
7558 Entry.Node = Val;
7559 Entry.Ty = Val.getValueType().getTypeForEVT(*DAG.getContext());
7560 Entry.isZExt = true;
7561 Args.push_back(Entry);
7562
7563 Entry.Node = Exponent;
7564 Entry.Ty = Exponent.getValueType().getTypeForEVT(*DAG.getContext());
7565 Entry.isZExt = true;
7566 Args.push_back(Entry);
7567
7568 Type *LCRTy = Val.getValueType().getTypeForEVT(*DAG.getContext());
7569
7570 // In the in-chain to the call is the entry node If we are emitting a
7571 // tailcall, the chain will be mutated if the node has a non-entry input
7572 // chain.
7573 SDValue InChain = DAG.getEntryNode();
7574 SDValue TCChain = InChain;
7575
7576 const auto *F = DAG.getMachineFunction().getFunction();
7577 bool IsTC = TLI.isInTailCallPosition(DAG, Op.getNode(), TCChain) &&
7578 F->getReturnType() == LCRTy;
7579 if (IsTC)
7580 InChain = TCChain;
7581
7582 TargetLowering::CallLoweringInfo CLI(DAG);
7583 CLI.setDebugLoc(dl)
7584 .setChain(InChain)
7585 .setCallee(CallingConv::ARM_AAPCS_VFP, LCRTy, Callee, std::move(Args))
7586 .setTailCall(IsTC);
7587 std::pair<SDValue, SDValue> CI = TLI.LowerCallTo(CLI);
7588
7589 // Return the chain (the DAG root) if it is a tail call
7590 return !CI.second.getNode() ? DAG.getRoot() : CI.first;
7591}
7592
Dan Gohman21cea8a2010-04-17 15:26:15 +00007593SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007594 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007595 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007596 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007597 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00007598 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00007599 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00007600 switch (Subtarget->getTargetTriple().getObjectFormat()) {
7601 default: llvm_unreachable("unknown object format");
7602 case Triple::COFF:
7603 return LowerGlobalAddressWindows(Op, DAG);
7604 case Triple::ELF:
7605 return LowerGlobalAddressELF(Op, DAG);
7606 case Triple::MachO:
7607 return LowerGlobalAddressDarwin(Op, DAG);
7608 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00007609 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00007610 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007611 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
7612 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007613 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00007614 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00007615 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00007616 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00007617 case ISD::SINT_TO_FP:
7618 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
7619 case ISD::FP_TO_SINT:
7620 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007621 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00007622 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00007623 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00007624 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00007625 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00007626 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00007627 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
7628 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00007629 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007630 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00007631 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00007632 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00007633 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
7634 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007635 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00007636 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007637 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007638 case ISD::CTTZ:
7639 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007640 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007641 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00007642 case ISD::SETCCE: return LowerSETCCE(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007643 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007644 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007645 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007646 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007647 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007648 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007649 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007650 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00007651 case ISD::SDIV:
7652 if (Subtarget->isTargetWindows())
7653 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
7654 return LowerSDIV(Op, DAG);
7655 case ISD::UDIV:
7656 if (Subtarget->isTargetWindows())
7657 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
7658 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007659 case ISD::ADDC:
7660 case ISD::ADDE:
7661 case ISD::SUBC:
7662 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007663 case ISD::SADDO:
7664 case ISD::UADDO:
7665 case ISD::SSUBO:
7666 case ISD::USUBO:
7667 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007668 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007669 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007670 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007671 case ISD::SDIVREM:
7672 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007673 case ISD::DYNAMIC_STACKALLOC:
7674 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7675 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7676 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007677 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7678 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasool804e12e2016-11-06 19:46:54 +00007679 case ISD::FPOWI: return LowerFPOWI(Op, *Subtarget, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007680 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007681 }
Evan Cheng10043e22007-01-19 07:51:42 +00007682}
7683
Duncan Sands6ed40142008-12-01 11:39:25 +00007684/// ReplaceNodeResults - Replace the results of node with an illegal result
7685/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007686void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007687 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007688 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007689 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007690 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007691 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007692 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007693 case ISD::READ_REGISTER:
7694 ExpandREAD_REGISTER(N, Results, DAG);
7695 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007696 case ISD::BITCAST:
7697 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007698 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007699 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007700 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007701 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007702 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007703 case ISD::SREM:
7704 case ISD::UREM:
7705 Res = LowerREM(N, DAG);
7706 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007707 case ISD::SDIVREM:
7708 case ISD::UDIVREM:
7709 Res = LowerDivRem(SDValue(N, 0), DAG);
7710 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7711 Results.push_back(Res.getValue(0));
7712 Results.push_back(Res.getValue(1));
7713 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007714 case ISD::READCYCLECOUNTER:
7715 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7716 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007717 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007718 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007719 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007720 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7721 Results);
Tim Northoverb629c772016-04-18 21:48:55 +00007722 case ISD::ATOMIC_CMP_SWAP:
7723 ReplaceCMP_SWAP_64Results(N, Results, DAG);
7724 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00007725 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007726 if (Res.getNode())
7727 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007728}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007729
Evan Cheng10043e22007-01-19 07:51:42 +00007730//===----------------------------------------------------------------------===//
7731// ARM Scheduler Hooks
7732//===----------------------------------------------------------------------===//
7733
Bill Wendling030b58e2011-10-06 22:18:16 +00007734/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7735/// registers the function context.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007736void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI,
7737 MachineBasicBlock *MBB,
7738 MachineBasicBlock *DispatchBB,
7739 int FI) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00007740 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
7741 "ROPI/RWPI not currently supported with SjLj");
Eric Christopher1889fdc2015-01-29 00:19:39 +00007742 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007743 DebugLoc dl = MI.getDebugLoc();
Bill Wendling374ee192011-10-03 21:25:38 +00007744 MachineFunction *MF = MBB->getParent();
7745 MachineRegisterInfo *MRI = &MF->getRegInfo();
7746 MachineConstantPool *MCP = MF->getConstantPool();
7747 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7748 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007749
Bill Wendling374ee192011-10-03 21:25:38 +00007750 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007751 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007752
Bill Wendling374ee192011-10-03 21:25:38 +00007753 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007754 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007755 ARMConstantPoolValue *CPV =
7756 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7757 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7758
Craig Topper61e88f42014-11-21 05:58:21 +00007759 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7760 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007761
Bill Wendling030b58e2011-10-06 22:18:16 +00007762 // Grab constant pool and fixed stack memory operands.
7763 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007764 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7765 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007766
7767 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007768 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7769 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007770
7771 // Load the address of the dispatch MBB into the jump buffer.
7772 if (isThumb2) {
7773 // Incoming value: jbuf
7774 // ldr.n r5, LCPI1_1
7775 // orr r5, r5, #1
7776 // add r5, pc
7777 // str r5, [$jbuf, #+4] ; &jbuf[1]
7778 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7779 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7780 .addConstantPoolIndex(CPI)
7781 .addMemOperand(CPMMO));
7782 // Set the low bit because of thumb mode.
7783 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7784 AddDefaultCC(
7785 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7786 .addReg(NewVReg1, RegState::Kill)
7787 .addImm(0x01)));
7788 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7789 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7790 .addReg(NewVReg2, RegState::Kill)
7791 .addImm(PCLabelId);
7792 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7793 .addReg(NewVReg3, RegState::Kill)
7794 .addFrameIndex(FI)
7795 .addImm(36) // &jbuf[1] :: pc
7796 .addMemOperand(FIMMOSt));
7797 } else if (isThumb) {
7798 // Incoming value: jbuf
7799 // ldr.n r1, LCPI1_4
7800 // add r1, pc
7801 // mov r2, #1
7802 // orrs r1, r2
7803 // add r2, $jbuf, #+4 ; &jbuf[1]
7804 // str r1, [r2]
7805 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7806 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7807 .addConstantPoolIndex(CPI)
7808 .addMemOperand(CPMMO));
7809 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7810 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7811 .addReg(NewVReg1, RegState::Kill)
7812 .addImm(PCLabelId);
7813 // Set the low bit because of thumb mode.
7814 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7815 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7816 .addReg(ARM::CPSR, RegState::Define)
7817 .addImm(1));
7818 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7819 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7820 .addReg(ARM::CPSR, RegState::Define)
7821 .addReg(NewVReg2, RegState::Kill)
7822 .addReg(NewVReg3, RegState::Kill));
7823 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007824 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7825 .addFrameIndex(FI)
7826 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007827 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7828 .addReg(NewVReg4, RegState::Kill)
7829 .addReg(NewVReg5, RegState::Kill)
7830 .addImm(0)
7831 .addMemOperand(FIMMOSt));
7832 } else {
7833 // Incoming value: jbuf
7834 // ldr r1, LCPI1_1
7835 // add r1, pc, r1
7836 // str r1, [$jbuf, #+4] ; &jbuf[1]
7837 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7838 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7839 .addConstantPoolIndex(CPI)
7840 .addImm(0)
7841 .addMemOperand(CPMMO));
7842 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7843 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7844 .addReg(NewVReg1, RegState::Kill)
7845 .addImm(PCLabelId));
7846 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7847 .addReg(NewVReg2, RegState::Kill)
7848 .addFrameIndex(FI)
7849 .addImm(36) // &jbuf[1] :: pc
7850 .addMemOperand(FIMMOSt));
7851 }
7852}
7853
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007854void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI,
Matthias Brauneec4efc2015-04-28 00:37:05 +00007855 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007856 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007857 DebugLoc dl = MI.getDebugLoc();
Bill Wendling030b58e2011-10-06 22:18:16 +00007858 MachineFunction *MF = MBB->getParent();
7859 MachineRegisterInfo *MRI = &MF->getRegInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00007860 MachineFrameInfo &MFI = MF->getFrameInfo();
7861 int FI = MFI.getFunctionContextIndex();
Bill Wendling030b58e2011-10-06 22:18:16 +00007862
Craig Topper61e88f42014-11-21 05:58:21 +00007863 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7864 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007865
Bill Wendling362c1b02011-10-06 21:29:56 +00007866 // Get a mapping of the call site numbers to all of the landing pads they're
7867 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007868 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7869 unsigned MaxCSNum = 0;
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007870 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7871 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007872 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007873
7874 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7875 // pad.
7876 for (MachineBasicBlock::iterator
7877 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7878 if (!II->isEHLabel()) continue;
7879
7880 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Matthias Braund0ee66c2016-12-01 19:32:15 +00007881 if (!MF->hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007882
Matthias Braund0ee66c2016-12-01 19:32:15 +00007883 SmallVectorImpl<unsigned> &CallSiteIdxs = MF->getCallSiteLandingPad(Sym);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007884 for (SmallVectorImpl<unsigned>::iterator
7885 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7886 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007887 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007888 MaxCSNum = std::max(MaxCSNum, *CSI);
7889 }
Bill Wendling202803e2011-10-05 00:02:33 +00007890 break;
7891 }
7892 }
7893
7894 // Get an ordered list of the machine basic blocks for the jump table.
7895 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007896 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007897 LPadList.reserve(CallSiteNumToLPad.size());
7898 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7899 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7900 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007901 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007902 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007903 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7904 }
Bill Wendling202803e2011-10-05 00:02:33 +00007905 }
7906
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007907 assert(!LPadList.empty() &&
7908 "No landing pad destinations for the dispatch jump table!");
7909
Bill Wendling362c1b02011-10-06 21:29:56 +00007910 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007911 MachineJumpTableInfo *JTI =
7912 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7913 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Bill Wendling202803e2011-10-05 00:02:33 +00007914
Bill Wendling362c1b02011-10-06 21:29:56 +00007915 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007916
7917 // Shove the dispatch's address into the return slot in the function context.
7918 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007919 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007920
Bill Wendling324be982011-10-05 00:39:32 +00007921 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007922 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007923 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007924 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007925 else
7926 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7927
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007928 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007929 DispatchBB->addSuccessor(TrapBB);
7930
7931 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7932 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007933
Bill Wendling510fbcd2011-10-17 21:32:56 +00007934 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007935 MF->insert(MF->end(), DispatchBB);
7936 MF->insert(MF->end(), DispContBB);
7937 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007938
Bill Wendling030b58e2011-10-06 22:18:16 +00007939 // Insert code into the entry block that creates and registers the function
7940 // context.
7941 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7942
Alex Lorenze40c8a22015-08-11 23:09:45 +00007943 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7944 MachinePointerInfo::getFixedStack(*MF, FI),
7945 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007946
Chad Rosier1ec8e402012-11-06 23:05:24 +00007947 MachineInstrBuilder MIB;
7948 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7949
7950 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7951 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7952
7953 // Add a register mask with no preserved registers. This results in all
Oliver Stannard50a74392016-10-11 10:06:59 +00007954 // registers being marked as clobbered. This can't work if the dispatch block
7955 // is in a Thumb1 function and is linked with ARM code which uses the FP
7956 // registers, as there is no way to preserve the FP registers in Thumb1 mode.
7957 MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF));
Bob Wilsonf6d17282011-11-16 07:11:57 +00007958
Rafael Espindola0f898332016-06-20 16:43:17 +00007959 bool IsPositionIndependent = isPositionIndependent();
Bill Wendling85833f72011-10-18 22:49:07 +00007960 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007961 if (Subtarget->isThumb2()) {
7962 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7963 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7964 .addFrameIndex(FI)
7965 .addImm(4)
7966 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007967
Bill Wendling85833f72011-10-18 22:49:07 +00007968 if (NumLPads < 256) {
7969 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7970 .addReg(NewVReg1)
7971 .addImm(LPadList.size()));
7972 } else {
7973 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7974 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007975 .addImm(NumLPads & 0xFFFF));
7976
7977 unsigned VReg2 = VReg1;
7978 if ((NumLPads & 0xFFFF0000) != 0) {
7979 VReg2 = MRI->createVirtualRegister(TRC);
7980 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7981 .addReg(VReg1)
7982 .addImm(NumLPads >> 16));
7983 }
7984
Bill Wendling85833f72011-10-18 22:49:07 +00007985 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7986 .addReg(NewVReg1)
7987 .addReg(VReg2));
7988 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007989
Bill Wendling5626c662011-10-06 22:53:00 +00007990 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7991 .addMBB(TrapBB)
7992 .addImm(ARMCC::HI)
7993 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007994
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007995 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7996 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007997 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007998
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007999 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00008000 AddDefaultCC(
8001 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00008002 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
8003 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00008004 .addReg(NewVReg1)
8005 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
8006
8007 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00008008 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00008009 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00008010 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00008011 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00008012 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8013 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
8014 .addFrameIndex(FI)
8015 .addImm(1)
8016 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00008017
Bill Wendling64e6bfc2011-10-18 23:11:05 +00008018 if (NumLPads < 256) {
8019 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
8020 .addReg(NewVReg1)
8021 .addImm(NumLPads));
8022 } else {
8023 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00008024 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
8025 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
8026
8027 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008028 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00008029 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008030 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00008031 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00008032
8033 unsigned VReg1 = MRI->createVirtualRegister(TRC);
8034 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
8035 .addReg(VReg1, RegState::Define)
8036 .addConstantPoolIndex(Idx));
8037 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
8038 .addReg(NewVReg1)
8039 .addReg(VReg1));
8040 }
8041
Bill Wendlingb3d46782011-10-06 23:37:36 +00008042 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
8043 .addMBB(TrapBB)
8044 .addImm(ARMCC::HI)
8045 .addReg(ARM::CPSR);
8046
8047 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
8048 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
8049 .addReg(ARM::CPSR, RegState::Define)
8050 .addReg(NewVReg1)
8051 .addImm(2));
8052
8053 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00008054 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00008055 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00008056
8057 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8058 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
8059 .addReg(ARM::CPSR, RegState::Define)
8060 .addReg(NewVReg2, RegState::Kill)
8061 .addReg(NewVReg3));
8062
Alex Lorenze40c8a22015-08-11 23:09:45 +00008063 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
8064 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00008065
8066 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
8067 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
8068 .addReg(NewVReg4, RegState::Kill)
8069 .addImm(0)
8070 .addMemOperand(JTMMOLd));
8071
Chad Rosier96603432013-03-01 18:30:38 +00008072 unsigned NewVReg6 = NewVReg5;
Rafael Espindola0f898332016-06-20 16:43:17 +00008073 if (IsPositionIndependent) {
Chad Rosier96603432013-03-01 18:30:38 +00008074 NewVReg6 = MRI->createVirtualRegister(TRC);
8075 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
8076 .addReg(ARM::CPSR, RegState::Define)
8077 .addReg(NewVReg5, RegState::Kill)
8078 .addReg(NewVReg3));
8079 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00008080
8081 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
8082 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00008083 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00008084 } else {
8085 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8086 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
8087 .addFrameIndex(FI)
8088 .addImm(4)
8089 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00008090
Bill Wendling4969dcd2011-10-18 22:52:20 +00008091 if (NumLPads < 256) {
8092 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
8093 .addReg(NewVReg1)
8094 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00008095 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00008096 unsigned VReg1 = MRI->createVirtualRegister(TRC);
8097 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00008098 .addImm(NumLPads & 0xFFFF));
8099
8100 unsigned VReg2 = VReg1;
8101 if ((NumLPads & 0xFFFF0000) != 0) {
8102 VReg2 = MRI->createVirtualRegister(TRC);
8103 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
8104 .addReg(VReg1)
8105 .addImm(NumLPads >> 16));
8106 }
8107
Bill Wendling4969dcd2011-10-18 22:52:20 +00008108 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
8109 .addReg(NewVReg1)
8110 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00008111 } else {
8112 MachineConstantPool *ConstantPool = MF->getConstantPool();
8113 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
8114 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
8115
8116 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008117 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00008118 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008119 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00008120 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8121
8122 unsigned VReg1 = MRI->createVirtualRegister(TRC);
8123 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
8124 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00008125 .addConstantPoolIndex(Idx)
8126 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00008127 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
8128 .addReg(NewVReg1)
8129 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00008130 }
8131
Bill Wendling5626c662011-10-06 22:53:00 +00008132 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
8133 .addMBB(TrapBB)
8134 .addImm(ARMCC::HI)
8135 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00008136
Bill Wendling973c8172011-10-18 22:11:18 +00008137 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00008138 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00008139 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00008140 .addReg(NewVReg1)
8141 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00008142 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8143 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00008144 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00008145
Alex Lorenze40c8a22015-08-11 23:09:45 +00008146 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
8147 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00008148 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00008149 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00008150 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
8151 .addReg(NewVReg3, RegState::Kill)
8152 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00008153 .addImm(0)
8154 .addMemOperand(JTMMOLd));
8155
Rafael Espindola0f898332016-06-20 16:43:17 +00008156 if (IsPositionIndependent) {
Chad Rosier96603432013-03-01 18:30:38 +00008157 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
8158 .addReg(NewVReg5, RegState::Kill)
8159 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00008160 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00008161 } else {
8162 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
8163 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00008164 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00008165 }
Bill Wendling5626c662011-10-06 22:53:00 +00008166 }
Bill Wendling202803e2011-10-05 00:02:33 +00008167
Bill Wendling324be982011-10-05 00:39:32 +00008168 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00008169 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00008170 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00008171 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
8172 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00008173 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00008174 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00008175 }
8176
Bill Wendling26d27802011-10-17 05:25:09 +00008177 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00008178 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00008179 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00008180 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008181
8182 // Remove the landing pad successor from the invoke block and replace it
8183 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00008184 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
8185 BB->succ_end());
8186 while (!Successors.empty()) {
8187 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00008188 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00008189 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00008190 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00008191 }
8192 }
8193
Cong Houd97c1002015-12-01 05:29:22 +00008194 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00008195 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008196
8197 // Find the invoke call and mark all of the callee-saved registers as
8198 // 'implicit defined' so that they're spilled. This prevents code from
8199 // moving instructions to before the EH block, where they will never be
8200 // executed.
8201 for (MachineBasicBlock::reverse_iterator
8202 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00008203 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008204
8205 DenseMap<unsigned, bool> DefRegs;
8206 for (MachineInstr::mop_iterator
8207 OI = II->operands_begin(), OE = II->operands_end();
8208 OI != OE; ++OI) {
8209 if (!OI->isReg()) continue;
8210 DefRegs[OI->getReg()] = true;
8211 }
8212
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00008213 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008214
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00008215 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00008216 unsigned Reg = SavedRegs[i];
8217 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00008218 !ARM::tGPRRegClass.contains(Reg) &&
8219 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00008220 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00008221 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00008222 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00008223 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00008224 continue;
8225 if (!DefRegs[Reg])
8226 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00008227 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008228
8229 break;
8230 }
Bill Wendling883ec972011-10-07 23:18:02 +00008231 }
Bill Wendling324be982011-10-05 00:39:32 +00008232
Bill Wendling617075f2011-10-18 18:30:49 +00008233 // Mark all former landing pads as non-landing pads. The dispatch is the only
8234 // landing pad now.
8235 for (SmallVectorImpl<MachineBasicBlock*>::iterator
8236 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00008237 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00008238
Bill Wendling324be982011-10-05 00:39:32 +00008239 // The instruction is gone now.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008240 MI.eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00008241}
8242
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008243static
8244MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
8245 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
8246 E = MBB->succ_end(); I != E; ++I)
8247 if (*I != Succ)
8248 return *I;
8249 llvm_unreachable("Expecting a BB with two successors!");
8250}
8251
Manman Renb504f492013-10-29 22:27:32 +00008252/// Return the load opcode for a given load size. If load size >= 8,
8253/// neon opcode will be returned.
8254static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
8255 if (LdSize >= 8)
8256 return LdSize == 16 ? ARM::VLD1q32wb_fixed
8257 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
8258 if (IsThumb1)
8259 return LdSize == 4 ? ARM::tLDRi
8260 : LdSize == 2 ? ARM::tLDRHi
8261 : LdSize == 1 ? ARM::tLDRBi : 0;
8262 if (IsThumb2)
8263 return LdSize == 4 ? ARM::t2LDR_POST
8264 : LdSize == 2 ? ARM::t2LDRH_POST
8265 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
8266 return LdSize == 4 ? ARM::LDR_POST_IMM
8267 : LdSize == 2 ? ARM::LDRH_POST
8268 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
8269}
8270
8271/// Return the store opcode for a given store size. If store size >= 8,
8272/// neon opcode will be returned.
8273static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
8274 if (StSize >= 8)
8275 return StSize == 16 ? ARM::VST1q32wb_fixed
8276 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
8277 if (IsThumb1)
8278 return StSize == 4 ? ARM::tSTRi
8279 : StSize == 2 ? ARM::tSTRHi
8280 : StSize == 1 ? ARM::tSTRBi : 0;
8281 if (IsThumb2)
8282 return StSize == 4 ? ARM::t2STR_POST
8283 : StSize == 2 ? ARM::t2STRH_POST
8284 : StSize == 1 ? ARM::t2STRB_POST : 0;
8285 return StSize == 4 ? ARM::STR_POST_IMM
8286 : StSize == 2 ? ARM::STRH_POST
8287 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
8288}
8289
8290/// Emit a post-increment load operation with given size. The instructions
8291/// will be added to BB at Pos.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008292static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00008293 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00008294 unsigned LdSize, unsigned Data, unsigned AddrIn,
8295 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
8296 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
8297 assert(LdOpc != 0 && "Should have a load opcode");
8298 if (LdSize >= 8) {
8299 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8300 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
8301 .addImm(0));
8302 } else if (IsThumb1) {
8303 // load + update AddrIn
8304 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8305 .addReg(AddrIn).addImm(0));
8306 MachineInstrBuilder MIB =
8307 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
8308 MIB = AddDefaultT1CC(MIB);
8309 MIB.addReg(AddrIn).addImm(LdSize);
8310 AddDefaultPred(MIB);
8311 } else if (IsThumb2) {
8312 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8313 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
8314 .addImm(LdSize));
8315 } else { // arm
8316 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8317 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
8318 .addReg(0).addImm(LdSize));
8319 }
8320}
8321
8322/// Emit a post-increment store operation with given size. The instructions
8323/// will be added to BB at Pos.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008324static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00008325 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00008326 unsigned StSize, unsigned Data, unsigned AddrIn,
8327 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
8328 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
8329 assert(StOpc != 0 && "Should have a store opcode");
8330 if (StSize >= 8) {
8331 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8332 .addReg(AddrIn).addImm(0).addReg(Data));
8333 } else if (IsThumb1) {
8334 // store + update AddrIn
8335 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
8336 .addReg(AddrIn).addImm(0));
8337 MachineInstrBuilder MIB =
8338 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
8339 MIB = AddDefaultT1CC(MIB);
8340 MIB.addReg(AddrIn).addImm(StSize);
8341 AddDefaultPred(MIB);
8342 } else if (IsThumb2) {
8343 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8344 .addReg(Data).addReg(AddrIn).addImm(StSize));
8345 } else { // arm
8346 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8347 .addReg(Data).addReg(AddrIn).addReg(0)
8348 .addImm(StSize));
8349 }
8350}
8351
David Peixottoc32e24a2013-10-17 19:49:22 +00008352MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008353ARMTargetLowering::EmitStructByval(MachineInstr &MI,
David Peixottoc32e24a2013-10-17 19:49:22 +00008354 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00008355 // This pseudo instruction has 3 operands: dst, src, size
8356 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
8357 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00008358 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00008359 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008360 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00008361
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008362 unsigned dest = MI.getOperand(0).getReg();
8363 unsigned src = MI.getOperand(1).getReg();
8364 unsigned SizeVal = MI.getOperand(2).getImm();
8365 unsigned Align = MI.getOperand(3).getImm();
8366 DebugLoc dl = MI.getDebugLoc();
Manman Rene8735522012-06-01 19:33:18 +00008367
Manman Rene8735522012-06-01 19:33:18 +00008368 MachineFunction *MF = BB->getParent();
8369 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00008370 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00008371 const TargetRegisterClass *TRC = nullptr;
8372 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00008373
8374 bool IsThumb1 = Subtarget->isThumb1Only();
8375 bool IsThumb2 = Subtarget->isThumb2();
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008376 bool IsThumb = Subtarget->isThumb();
Manman Rene8735522012-06-01 19:33:18 +00008377
8378 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00008379 UnitSize = 1;
8380 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00008381 UnitSize = 2;
8382 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00008383 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00008384 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00008385 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00008386 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00008387 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00008388 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00008389 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00008390 }
8391 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00008392 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00008393 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00008394 }
Manman Ren6e1fd462012-06-18 22:23:48 +00008395
David Peixottob0653e532013-10-24 16:39:36 +00008396 // Select the correct opcode and register class for unit size load/store
8397 bool IsNeon = UnitSize >= 8;
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008398 TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00008399 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00008400 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
8401 : UnitSize == 8 ? &ARM::DPRRegClass
8402 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00008403
Manman Rene8735522012-06-01 19:33:18 +00008404 unsigned BytesLeft = SizeVal % UnitSize;
8405 unsigned LoopSize = SizeVal - BytesLeft;
8406
8407 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
8408 // Use LDR and STR to copy.
8409 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
8410 // [destOut] = STR_POST(scratch, destIn, UnitSize)
8411 unsigned srcIn = src;
8412 unsigned destIn = dest;
8413 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00008414 unsigned srcOut = MRI.createVirtualRegister(TRC);
8415 unsigned destOut = MRI.createVirtualRegister(TRC);
8416 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00008417 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
8418 IsThumb1, IsThumb2);
8419 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
8420 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008421 srcIn = srcOut;
8422 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008423 }
8424
8425 // Handle the leftover bytes with LDRB and STRB.
8426 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
8427 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00008428 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00008429 unsigned srcOut = MRI.createVirtualRegister(TRC);
8430 unsigned destOut = MRI.createVirtualRegister(TRC);
8431 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00008432 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
8433 IsThumb1, IsThumb2);
8434 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
8435 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008436 srcIn = srcOut;
8437 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008438 }
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008439 MI.eraseFromParent(); // The instruction is gone now.
Manman Rene8735522012-06-01 19:33:18 +00008440 return BB;
8441 }
8442
8443 // Expand the pseudo op to a loop.
8444 // thisMBB:
8445 // ...
8446 // movw varEnd, # --> with thumb2
8447 // movt varEnd, #
8448 // ldrcp varEnd, idx --> without thumb2
8449 // fallthrough --> loopMBB
8450 // loopMBB:
8451 // PHI varPhi, varEnd, varLoop
8452 // PHI srcPhi, src, srcLoop
8453 // PHI destPhi, dst, destLoop
8454 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
8455 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
8456 // subs varLoop, varPhi, #UnitSize
8457 // bne loopMBB
8458 // fallthrough --> exitMBB
8459 // exitMBB:
8460 // epilogue to handle left-over bytes
8461 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
8462 // [destOut] = STRB_POST(scratch, destLoop, 1)
8463 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
8464 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
8465 MF->insert(It, loopMBB);
8466 MF->insert(It, exitMBB);
8467
8468 // Transfer the remainder of BB and its successor edges to exitMBB.
8469 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008470 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00008471 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
8472
8473 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00008474 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00008475 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00008476 unsigned Vtmp = varEnd;
8477 if ((LoopSize & 0xFFFF0000) != 0)
8478 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00008479 AddDefaultPred(BuildMI(BB, dl,
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008480 TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16),
Derek Schuffb0513892015-03-26 22:11:00 +00008481 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00008482
8483 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00008484 AddDefaultPred(BuildMI(BB, dl,
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008485 TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16),
Derek Schuffb0513892015-03-26 22:11:00 +00008486 varEnd)
8487 .addReg(Vtmp)
8488 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00008489 } else {
8490 MachineConstantPool *ConstantPool = MF->getConstantPool();
8491 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
8492 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
8493
8494 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008495 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00008496 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008497 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00008498 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8499
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008500 if (IsThumb)
David Peixottob0653e532013-10-24 16:39:36 +00008501 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
8502 varEnd, RegState::Define).addConstantPoolIndex(Idx));
8503 else
8504 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
8505 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
8506 }
Manman Rene8735522012-06-01 19:33:18 +00008507 BB->addSuccessor(loopMBB);
8508
8509 // Generate the loop body:
8510 // varPhi = PHI(varLoop, varEnd)
8511 // srcPhi = PHI(srcLoop, src)
8512 // destPhi = PHI(destLoop, dst)
8513 MachineBasicBlock *entryBB = BB;
8514 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00008515 unsigned varLoop = MRI.createVirtualRegister(TRC);
8516 unsigned varPhi = MRI.createVirtualRegister(TRC);
8517 unsigned srcLoop = MRI.createVirtualRegister(TRC);
8518 unsigned srcPhi = MRI.createVirtualRegister(TRC);
8519 unsigned destLoop = MRI.createVirtualRegister(TRC);
8520 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00008521
8522 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
8523 .addReg(varLoop).addMBB(loopMBB)
8524 .addReg(varEnd).addMBB(entryBB);
8525 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
8526 .addReg(srcLoop).addMBB(loopMBB)
8527 .addReg(src).addMBB(entryBB);
8528 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
8529 .addReg(destLoop).addMBB(loopMBB)
8530 .addReg(dest).addMBB(entryBB);
8531
8532 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
8533 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00008534 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00008535 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
8536 IsThumb1, IsThumb2);
8537 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
8538 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00008539
8540 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00008541 if (IsThumb1) {
8542 MachineInstrBuilder MIB =
8543 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
8544 MIB = AddDefaultT1CC(MIB);
8545 MIB.addReg(varPhi).addImm(UnitSize);
8546 AddDefaultPred(MIB);
8547 } else {
8548 MachineInstrBuilder MIB =
8549 BuildMI(*BB, BB->end(), dl,
8550 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
8551 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
8552 MIB->getOperand(5).setReg(ARM::CPSR);
8553 MIB->getOperand(5).setIsDef(true);
8554 }
8555 BuildMI(*BB, BB->end(), dl,
8556 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
8557 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00008558
8559 // loopMBB can loop back to loopMBB or fall through to exitMBB.
8560 BB->addSuccessor(loopMBB);
8561 BB->addSuccessor(exitMBB);
8562
8563 // Add epilogue to handle BytesLeft.
8564 BB = exitMBB;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00008565 auto StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00008566
8567 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
8568 // [destOut] = STRB_POST(scratch, destLoop, 1)
8569 unsigned srcIn = srcLoop;
8570 unsigned destIn = destLoop;
8571 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00008572 unsigned srcOut = MRI.createVirtualRegister(TRC);
8573 unsigned destOut = MRI.createVirtualRegister(TRC);
8574 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00008575 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
8576 IsThumb1, IsThumb2);
8577 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
8578 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008579 srcIn = srcOut;
8580 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008581 }
8582
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008583 MI.eraseFromParent(); // The instruction is gone now.
Manman Rene8735522012-06-01 19:33:18 +00008584 return BB;
8585}
8586
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00008587MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008588ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008589 MachineBasicBlock *MBB) const {
8590 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00008591 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008592 DebugLoc DL = MI.getDebugLoc();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008593
8594 assert(Subtarget->isTargetWindows() &&
8595 "__chkstk is only supported on Windows");
8596 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
8597
8598 // __chkstk takes the number of words to allocate on the stack in R4, and
8599 // returns the stack adjustment in number of bytes in R4. This will not
8600 // clober any other registers (other than the obvious lr).
8601 //
8602 // Although, technically, IP should be considered a register which may be
8603 // clobbered, the call itself will not touch it. Windows on ARM is a pure
8604 // thumb-2 environment, so there is no interworking required. As a result, we
8605 // do not expect a veneer to be emitted by the linker, clobbering IP.
8606 //
Alp Toker1d099d92014-06-19 19:41:26 +00008607 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008608 // required, again, ensuring that IP is not clobbered.
8609 //
8610 // Finally, although some linkers may theoretically provide a trampoline for
8611 // out of range calls (which is quite common due to a 32M range limitation of
8612 // branches for Thumb), we can generate the long-call version via
8613 // -mcmodel=large, alleviating the need for the trampoline which may clobber
8614 // IP.
8615
8616 switch (TM.getCodeModel()) {
8617 case CodeModel::Small:
8618 case CodeModel::Medium:
8619 case CodeModel::Default:
8620 case CodeModel::Kernel:
8621 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
8622 .addImm((unsigned)ARMCC::AL).addReg(0)
8623 .addExternalSymbol("__chkstk")
8624 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8625 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8626 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8627 break;
8628 case CodeModel::Large:
8629 case CodeModel::JITDefault: {
8630 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
8631 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
8632
8633 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
8634 .addExternalSymbol("__chkstk");
8635 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
8636 .addImm((unsigned)ARMCC::AL).addReg(0)
8637 .addReg(Reg, RegState::Kill)
8638 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8639 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8640 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8641 break;
8642 }
8643 }
8644
8645 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
8646 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +00008647 .addReg(ARM::SP, RegState::Kill)
8648 .addReg(ARM::R4, RegState::Kill)
8649 .setMIFlags(MachineInstr::FrameSetup)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008650
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008651 MI.eraseFromParent();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008652 return MBB;
8653}
8654
8655MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008656ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008657 MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008658 DebugLoc DL = MI.getDebugLoc();
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008659 MachineFunction *MF = MBB->getParent();
8660 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8661
8662 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
Saleem Abdulrasool750a90d2016-03-25 19:48:06 +00008663 MF->insert(++MBB->getIterator(), ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008664 ContBB->splice(ContBB->begin(), MBB,
8665 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008666 ContBB->transferSuccessorsAndUpdatePHIs(MBB);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008667 MBB->addSuccessor(ContBB);
Saleem Abdulrasoole1aa7822016-10-31 22:12:37 +00008668
8669 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8670 BuildMI(TrapBB, DL, TII->get(ARM::t__brkdiv0));
8671 MF->push_back(TrapBB);
8672 MBB->addSuccessor(TrapBB);
8673
8674 AddDefaultPred(BuildMI(*MBB, MI, DL, TII->get(ARM::tCMPi8))
8675 .addReg(MI.getOperand(0).getReg())
8676 .addImm(0));
8677 BuildMI(*MBB, MI, DL, TII->get(ARM::t2Bcc))
8678 .addMBB(TrapBB)
8679 .addImm(ARMCC::EQ)
8680 .addReg(ARM::CPSR);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008681
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008682 MI.eraseFromParent();
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008683 return ContBB;
8684}
8685
8686MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008687ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008688 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008689 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008690 DebugLoc dl = MI.getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008691 bool isThumb2 = Subtarget->isThumb2();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008692 switch (MI.getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008693 default: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008694 MI.dump();
Evan Chengb972e562009-08-07 00:34:42 +00008695 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008696 }
James Molloyb3326df2016-07-15 08:03:56 +00008697
8698 // Thumb1 post-indexed loads are really just single-register LDMs.
8699 case ARM::tLDR_postidx: {
8700 BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD))
James Molloy8f16dff2016-07-15 08:12:44 +00008701 .addOperand(MI.getOperand(1)) // Rn_wb
8702 .addOperand(MI.getOperand(2)) // Rn
8703 .addOperand(MI.getOperand(3)) // PredImm
8704 .addOperand(MI.getOperand(4)) // PredReg
8705 .addOperand(MI.getOperand(0)); // Rt
8706 MI.eraseFromParent();
James Molloyb3326df2016-07-15 08:03:56 +00008707 return BB;
8708 }
8709
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008710 // The Thumb2 pre-indexed stores have the same MI operands, they just
8711 // define them differently in the .td files from the isel patterns, so
8712 // they need pseudos.
8713 case ARM::t2STR_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008714 MI.setDesc(TII->get(ARM::t2STR_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008715 return BB;
8716 case ARM::t2STRB_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008717 MI.setDesc(TII->get(ARM::t2STRB_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008718 return BB;
8719 case ARM::t2STRH_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008720 MI.setDesc(TII->get(ARM::t2STRH_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008721 return BB;
8722
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008723 case ARM::STRi_preidx:
8724 case ARM::STRBi_preidx: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008725 unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM
8726 : ARM::STRB_PRE_IMM;
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008727 // Decode the offset.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008728 unsigned Offset = MI.getOperand(4).getImm();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008729 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8730 Offset = ARM_AM::getAM2Offset(Offset);
8731 if (isSub)
8732 Offset = -Offset;
8733
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008734 MachineMemOperand *MMO = *MI.memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008735 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008736 .addOperand(MI.getOperand(0)) // Rn_wb
8737 .addOperand(MI.getOperand(1)) // Rt
8738 .addOperand(MI.getOperand(2)) // Rn
8739 .addImm(Offset) // offset (skip GPR==zero_reg)
8740 .addOperand(MI.getOperand(5)) // pred
8741 .addOperand(MI.getOperand(6))
8742 .addMemOperand(MMO);
8743 MI.eraseFromParent();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008744 return BB;
8745 }
8746 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008747 case ARM::STRBr_preidx:
8748 case ARM::STRH_preidx: {
8749 unsigned NewOpc;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008750 switch (MI.getOpcode()) {
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008751 default: llvm_unreachable("unexpected opcode!");
8752 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8753 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8754 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8755 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008756 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008757 for (unsigned i = 0; i < MI.getNumOperands(); ++i)
8758 MIB.addOperand(MI.getOperand(i));
8759 MI.eraseFromParent();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008760 return BB;
8761 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008762
Evan Chengbb2af352009-08-12 05:17:19 +00008763 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008764 // To "insert" a SELECT_CC instruction, we actually have to insert the
8765 // diamond control-flow pattern. The incoming instruction knows the
8766 // destination vreg to set, the condition code register to branch on, the
8767 // true/false values to select between, and a branch opcode to use.
8768 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008769 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008770
8771 // thisMBB:
8772 // ...
8773 // TrueVal = ...
8774 // cmpTY ccX, r1, r2
8775 // bCC copy1MBB
8776 // fallthrough --> copy0MBB
8777 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008778 MachineFunction *F = BB->getParent();
8779 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8780 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008781 F->insert(It, copy0MBB);
8782 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008783
8784 // Transfer the remainder of BB and its successor edges to sinkMBB.
8785 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008786 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008787 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8788
Dan Gohmanf4f04102010-07-06 15:49:48 +00008789 BB->addSuccessor(copy0MBB);
8790 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008791
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008792 BuildMI(BB, dl, TII->get(ARM::tBcc))
8793 .addMBB(sinkMBB)
8794 .addImm(MI.getOperand(3).getImm())
8795 .addReg(MI.getOperand(4).getReg());
Dan Gohman34396292010-07-06 20:24:04 +00008796
Evan Cheng10043e22007-01-19 07:51:42 +00008797 // copy0MBB:
8798 // %FalseValue = ...
8799 // # fallthrough to sinkMBB
8800 BB = copy0MBB;
8801
8802 // Update machine-CFG edges
8803 BB->addSuccessor(sinkMBB);
8804
8805 // sinkMBB:
8806 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8807 // ...
8808 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008809 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg())
8810 .addReg(MI.getOperand(1).getReg())
8811 .addMBB(copy0MBB)
8812 .addReg(MI.getOperand(2).getReg())
8813 .addMBB(thisMBB);
Evan Cheng10043e22007-01-19 07:51:42 +00008814
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008815 MI.eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008816 return BB;
8817 }
Evan Chengb972e562009-08-07 00:34:42 +00008818
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008819 case ARM::BCCi64:
8820 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008821 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008822 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008823
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008824 // Compare both parts that make up the double comparison separately for
8825 // equality.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008826 bool RHSisZero = MI.getOpcode() == ARM::BCCZi64;
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008827
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008828 unsigned LHS1 = MI.getOperand(1).getReg();
8829 unsigned LHS2 = MI.getOperand(2).getReg();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008830 if (RHSisZero) {
8831 AddDefaultPred(BuildMI(BB, dl,
8832 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8833 .addReg(LHS1).addImm(0));
8834 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8835 .addReg(LHS2).addImm(0)
8836 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8837 } else {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008838 unsigned RHS1 = MI.getOperand(3).getReg();
8839 unsigned RHS2 = MI.getOperand(4).getReg();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008840 AddDefaultPred(BuildMI(BB, dl,
8841 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8842 .addReg(LHS1).addReg(RHS1));
8843 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8844 .addReg(LHS2).addReg(RHS2)
8845 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8846 }
8847
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008848 MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008849 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008850 if (MI.getOperand(0).getImm() == ARMCC::NE)
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008851 std::swap(destMBB, exitMBB);
8852
8853 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8854 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008855 if (isThumb2)
8856 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8857 else
8858 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008859
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008860 MI.eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008861 return BB;
8862 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008863
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008864 case ARM::Int_eh_sjlj_setjmp:
8865 case ARM::Int_eh_sjlj_setjmp_nofp:
8866 case ARM::tInt_eh_sjlj_setjmp:
8867 case ARM::t2Int_eh_sjlj_setjmp:
8868 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008869 return BB;
8870
8871 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008872 EmitSjLjDispatchBlock(MI, BB);
8873 return BB;
8874
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008875 case ARM::ABS:
8876 case ARM::t2ABS: {
8877 // To insert an ABS instruction, we have to insert the
8878 // diamond control-flow pattern. The incoming instruction knows the
8879 // source vreg to test against 0, the destination vreg to set,
8880 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008881 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008882 // It transforms
8883 // V1 = ABS V0
8884 // into
8885 // V2 = MOVS V0
8886 // BCC (branch to SinkBB if V0 >= 0)
8887 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008888 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008889 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008890 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008891 MachineFunction *Fn = BB->getParent();
8892 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8893 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8894 Fn->insert(BBI, RSBBB);
8895 Fn->insert(BBI, SinkBB);
8896
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008897 unsigned int ABSSrcReg = MI.getOperand(1).getReg();
8898 unsigned int ABSDstReg = MI.getOperand(0).getReg();
8899 bool ABSSrcKIll = MI.getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008900 bool isThumb2 = Subtarget->isThumb2();
8901 MachineRegisterInfo &MRI = Fn->getRegInfo();
8902 // In Thumb mode S must not be specified if source register is the SP or
8903 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008904 unsigned NewRsbDstReg =
8905 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008906
8907 // Transfer the remainder of BB and its successor edges to sinkMBB.
8908 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008909 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008910 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8911
8912 BB->addSuccessor(RSBBB);
8913 BB->addSuccessor(SinkBB);
8914
8915 // fall through to SinkMBB
8916 RSBBB->addSuccessor(SinkBB);
8917
Manman Rene0763c72012-06-15 21:32:12 +00008918 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008919 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008920 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8921 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008922
8923 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008924 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008925 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8926 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8927
8928 // insert rsbri in RSBBB
8929 // Note: BCC and rsbri will be converted into predicated rsbmi
8930 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008931 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008932 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008933 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008934 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8935
Andrew Trick3f07c422011-10-18 18:40:53 +00008936 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008937 // reuse ABSDstReg to not change uses of ABS instruction
8938 BuildMI(*SinkBB, SinkBB->begin(), dl,
8939 TII->get(ARM::PHI), ABSDstReg)
8940 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008941 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008942
8943 // remove ABS instruction
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008944 MI.eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008945
8946 // return last added BB
8947 return SinkBB;
8948 }
Manman Rene8735522012-06-01 19:33:18 +00008949 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008950 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008951 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008952 case ARM::WIN__CHKSTK:
8953 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008954 case ARM::WIN__DBZCHK:
8955 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008956 }
8957}
8958
Scott Douglass953f9082015-10-05 14:49:54 +00008959/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8960/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8961/// instead of as a custom inserter because we need the use list from the SDNode.
8962static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008963 MachineInstr &MI, const SDNode *Node) {
Scott Douglass953f9082015-10-05 14:49:54 +00008964 bool isThumb1 = Subtarget->isThumb1Only();
8965
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008966 DebugLoc DL = MI.getDebugLoc();
8967 MachineFunction *MF = MI.getParent()->getParent();
Scott Douglass953f9082015-10-05 14:49:54 +00008968 MachineRegisterInfo &MRI = MF->getRegInfo();
8969 MachineInstrBuilder MIB(*MF, MI);
8970
8971 // If the new dst/src is unused mark it as dead.
8972 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008973 MI.getOperand(0).setIsDead(true);
Scott Douglass953f9082015-10-05 14:49:54 +00008974 }
8975 if (!Node->hasAnyUseOfValue(1)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008976 MI.getOperand(1).setIsDead(true);
Scott Douglass953f9082015-10-05 14:49:54 +00008977 }
8978
8979 // The MEMCPY both defines and kills the scratch registers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008980 for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) {
Scott Douglass953f9082015-10-05 14:49:54 +00008981 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8982 : &ARM::GPRRegClass);
8983 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8984 }
8985}
8986
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008987void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Evan Chenge6fba772011-08-30 19:09:48 +00008988 SDNode *Node) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008989 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00008990 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8991 return;
8992 }
8993
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008994 const MCInstrDesc *MCID = &MI.getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008995 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8996 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8997 // operand is still set to noreg. If needed, set the optional operand's
8998 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008999 //
Andrew Trick88b24502011-10-18 19:18:52 +00009000 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00009001
Andrew Trick924123a2011-09-21 02:20:46 +00009002 // Rename pseudo opcodes.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009003 unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode());
Andrew Trick924123a2011-09-21 02:20:46 +00009004 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00009005 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00009006 MCID = &TII->get(NewOpc);
9007
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009008 assert(MCID->getNumOperands() == MI.getDesc().getNumOperands() + 1 &&
Andrew Trick88b24502011-10-18 19:18:52 +00009009 "converted opcode should be the same except for cc_out");
9010
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009011 MI.setDesc(*MCID);
Andrew Trick88b24502011-10-18 19:18:52 +00009012
9013 // Add the optional cc_out operand
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009014 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00009015 }
Andrew Trick88b24502011-10-18 19:18:52 +00009016 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00009017
9018 // Any ARM instruction that sets the 's' bit should specify an optional
9019 // "cc_out" operand in the last operand position.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009020 if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00009021 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00009022 return;
9023 }
Andrew Trick924123a2011-09-21 02:20:46 +00009024 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
9025 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00009026 bool definesCPSR = false;
9027 bool deadCPSR = false;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009028 for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e;
9029 ++i) {
9030 const MachineOperand &MO = MI.getOperand(i);
Andrew Trick8586e622011-09-20 03:17:40 +00009031 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
9032 definesCPSR = true;
9033 if (MO.isDead())
9034 deadCPSR = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009035 MI.RemoveOperand(i);
Andrew Trick8586e622011-09-20 03:17:40 +00009036 break;
Evan Chenge6fba772011-08-30 19:09:48 +00009037 }
9038 }
Andrew Trick8586e622011-09-20 03:17:40 +00009039 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00009040 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00009041 return;
9042 }
9043 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00009044 if (deadCPSR) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009045 assert(!MI.getOperand(ccOutIdx).getReg() &&
Andrew Trick924123a2011-09-21 02:20:46 +00009046 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00009047 return;
Andrew Trick924123a2011-09-21 02:20:46 +00009048 }
Andrew Trick8586e622011-09-20 03:17:40 +00009049
Andrew Trick924123a2011-09-21 02:20:46 +00009050 // If this instruction was defined with an optional CPSR def and its dag node
9051 // had a live implicit CPSR def, then activate the optional CPSR def.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00009052 MachineOperand &MO = MI.getOperand(ccOutIdx);
Andrew Trick8586e622011-09-20 03:17:40 +00009053 MO.setReg(ARM::CPSR);
9054 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00009055}
9056
Evan Cheng10043e22007-01-19 07:51:42 +00009057//===----------------------------------------------------------------------===//
9058// ARM Optimization Hooks
9059//===----------------------------------------------------------------------===//
9060
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009061// Helper function that checks if N is a null or all ones constant.
9062static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00009063 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009064}
9065
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009066// Return true if N is conditionally 0 or all ones.
9067// Detects these expressions where cc is an i1 value:
9068//
9069// (select cc 0, y) [AllOnes=0]
9070// (select cc y, 0) [AllOnes=0]
9071// (zext cc) [AllOnes=0]
9072// (sext cc) [AllOnes=0/1]
9073// (select cc -1, y) [AllOnes=1]
9074// (select cc y, -1) [AllOnes=1]
9075//
9076// Invert is set when N is the null/all ones constant when CC is false.
9077// OtherOp is set to the alternative value of N.
9078static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
9079 SDValue &CC, bool &Invert,
9080 SDValue &OtherOp,
9081 SelectionDAG &DAG) {
9082 switch (N->getOpcode()) {
9083 default: return false;
9084 case ISD::SELECT: {
9085 CC = N->getOperand(0);
9086 SDValue N1 = N->getOperand(1);
9087 SDValue N2 = N->getOperand(2);
9088 if (isZeroOrAllOnes(N1, AllOnes)) {
9089 Invert = false;
9090 OtherOp = N2;
9091 return true;
9092 }
9093 if (isZeroOrAllOnes(N2, AllOnes)) {
9094 Invert = true;
9095 OtherOp = N1;
9096 return true;
9097 }
9098 return false;
9099 }
9100 case ISD::ZERO_EXTEND:
9101 // (zext cc) can never be the all ones value.
9102 if (AllOnes)
9103 return false;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00009104 LLVM_FALLTHROUGH;
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009105 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009106 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009107 EVT VT = N->getValueType(0);
9108 CC = N->getOperand(0);
9109 if (CC.getValueType() != MVT::i1)
9110 return false;
9111 Invert = !AllOnes;
9112 if (AllOnes)
9113 // When looking for an AllOnes constant, N is an sext, and the 'other'
9114 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009115 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009116 else if (N->getOpcode() == ISD::ZERO_EXTEND)
9117 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009118 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009119 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009120 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
9121 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009122 return true;
9123 }
9124 }
9125}
9126
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009127// Combine a constant select operand into its use:
9128//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009129// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
9130// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
9131// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
9132// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
9133// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009134//
9135// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009136// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009137//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009138// Also recognize sext/zext from i1:
9139//
9140// (add (zext cc), x) -> (select cc (add x, 1), x)
9141// (add (sext cc), x) -> (select cc (add x, -1), x)
9142//
9143// These transformations eventually create predicated instructions.
9144//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009145// @param N The node to transform.
9146// @param Slct The N operand that is a select.
9147// @param OtherOp The other N operand (x above).
9148// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009149// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009150// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00009151static
9152SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009153 TargetLowering::DAGCombinerInfo &DCI,
9154 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00009155 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009156 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009157 SDValue NonConstantVal;
9158 SDValue CCOp;
9159 bool SwapSelectOps;
9160 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
9161 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009162 return SDValue();
9163
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009164 // Slct is now know to be the desired identity constant when CC is true.
9165 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009166 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009167 OtherOp, NonConstantVal);
9168 // Unless SwapSelectOps says CC should be false.
9169 if (SwapSelectOps)
9170 std::swap(TrueVal, FalseVal);
9171
Andrew Trickef9de2a2013-05-25 02:42:55 +00009172 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009173 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00009174}
9175
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009176// Attempt combineSelectAndUse on each operand of a commutative operator N.
9177static
9178SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
9179 TargetLowering::DAGCombinerInfo &DCI) {
9180 SDValue N0 = N->getOperand(0);
9181 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009182 if (N0.getNode()->hasOneUse())
9183 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009184 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009185 if (N1.getNode()->hasOneUse())
9186 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009187 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009188 return SDValue();
9189}
9190
Eric Christopher1b8b94192011-06-29 21:10:36 +00009191// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00009192// (only after legalization).
9193static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
9194 TargetLowering::DAGCombinerInfo &DCI,
9195 const ARMSubtarget *Subtarget) {
9196
9197 // Only perform optimization if after legalize, and if NEON is available. We
9198 // also expected both operands to be BUILD_VECTORs.
9199 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
9200 || N0.getOpcode() != ISD::BUILD_VECTOR
9201 || N1.getOpcode() != ISD::BUILD_VECTOR)
9202 return SDValue();
9203
9204 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
9205 EVT VT = N->getValueType(0);
9206 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
9207 return SDValue();
9208
9209 // Check that the vector operands are of the right form.
9210 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
9211 // operands, where N is the size of the formed vector.
9212 // Each EXTRACT_VECTOR should have the same input vector and odd or even
9213 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00009214
9215 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00009216 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00009217 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00009218 SDValue Vec = N0->getOperand(0)->getOperand(0);
9219 SDNode *V = Vec.getNode();
9220 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00009221
Eric Christopher1b8b94192011-06-29 21:10:36 +00009222 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009223 // check to see if each of their operands are an EXTRACT_VECTOR with
9224 // the same vector and appropriate index.
9225 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
9226 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
9227 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00009228
Tanya Lattnere9e67052011-06-14 23:48:48 +00009229 SDValue ExtVec0 = N0->getOperand(i);
9230 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00009231
Tanya Lattnere9e67052011-06-14 23:48:48 +00009232 // First operand is the vector, verify its the same.
9233 if (V != ExtVec0->getOperand(0).getNode() ||
9234 V != ExtVec1->getOperand(0).getNode())
9235 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00009236
Tanya Lattnere9e67052011-06-14 23:48:48 +00009237 // Second is the constant, verify its correct.
9238 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
9239 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00009240
Tanya Lattnere9e67052011-06-14 23:48:48 +00009241 // For the constant, we want to see all the even or all the odd.
9242 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
9243 || C1->getZExtValue() != nextIndex+1)
9244 return SDValue();
9245
9246 // Increment index.
9247 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00009248 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00009249 return SDValue();
9250 }
9251
9252 // Create VPADDL node.
9253 SelectionDAG &DAG = DCI.DAG;
9254 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00009255
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009256 SDLoc dl(N);
9257
Tanya Lattnere9e67052011-06-14 23:48:48 +00009258 // Build operand list.
9259 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009260 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00009261 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00009262
9263 // Input is the vector.
9264 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00009265
Tanya Lattnere9e67052011-06-14 23:48:48 +00009266 // Get widened type and narrowed type.
9267 MVT widenType;
9268 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00009269
Silviu Barangaa3106e62014-04-03 10:44:27 +00009270 EVT inputLaneType = Vec.getValueType().getVectorElementType();
9271 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00009272 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
9273 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
9274 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
9275 default:
Craig Toppere55c5562012-02-07 02:50:20 +00009276 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00009277 }
9278
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009279 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00009280 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009281 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009282}
9283
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009284static SDValue findMUL_LOHI(SDValue V) {
9285 if (V->getOpcode() == ISD::UMUL_LOHI ||
9286 V->getOpcode() == ISD::SMUL_LOHI)
9287 return V;
9288 return SDValue();
9289}
9290
9291static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
9292 TargetLowering::DAGCombinerInfo &DCI,
9293 const ARMSubtarget *Subtarget) {
9294
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009295 // Look for multiply add opportunities.
9296 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
9297 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
9298 // a glue link from the first add to the second add.
9299 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
9300 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00009301 // UMUL_LOHI
9302 // / :lo \ :hi
9303 // / \ [no multiline comment]
9304 // loAdd -> ADDE |
9305 // \ :glue /
9306 // \ /
9307 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009308 //
9309 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
9310 SDValue AddcOp0 = AddcNode->getOperand(0);
9311 SDValue AddcOp1 = AddcNode->getOperand(1);
9312
9313 // Check if the two operands are from the same mul_lohi node.
9314 if (AddcOp0.getNode() == AddcOp1.getNode())
9315 return SDValue();
9316
9317 assert(AddcNode->getNumValues() == 2 &&
9318 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00009319 "Expect ADDC with two result values. First: i32");
9320
9321 // Check that we have a glued ADDC node.
9322 if (AddcNode->getValueType(1) != MVT::Glue)
9323 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009324
9325 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
9326 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
9327 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
9328 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
9329 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
9330 return SDValue();
9331
9332 // Look for the glued ADDE.
9333 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00009334 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009335 return SDValue();
9336
9337 // Make sure it is really an ADDE.
9338 if (AddeNode->getOpcode() != ISD::ADDE)
9339 return SDValue();
9340
9341 assert(AddeNode->getNumOperands() == 3 &&
9342 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
9343 "ADDE node has the wrong inputs");
9344
9345 // Check for the triangle shape.
9346 SDValue AddeOp0 = AddeNode->getOperand(0);
9347 SDValue AddeOp1 = AddeNode->getOperand(1);
9348
9349 // Make sure that the ADDE operands are not coming from the same node.
9350 if (AddeOp0.getNode() == AddeOp1.getNode())
9351 return SDValue();
9352
9353 // Find the MUL_LOHI node walking up ADDE's operands.
9354 bool IsLeftOperandMUL = false;
9355 SDValue MULOp = findMUL_LOHI(AddeOp0);
9356 if (MULOp == SDValue())
9357 MULOp = findMUL_LOHI(AddeOp1);
9358 else
9359 IsLeftOperandMUL = true;
9360 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00009361 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009362
9363 // Figure out the right opcode.
9364 unsigned Opc = MULOp->getOpcode();
9365 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
9366
9367 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00009368 SDValue* HiAdd = nullptr;
9369 SDValue* LoMul = nullptr;
9370 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009371
Jyoti Allurf1d70502015-01-23 09:10:03 +00009372 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
9373 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
9374 return SDValue();
9375
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009376 if (IsLeftOperandMUL)
9377 HiAdd = &AddeOp1;
9378 else
9379 HiAdd = &AddeOp0;
9380
9381
Jyoti Allurf1d70502015-01-23 09:10:03 +00009382 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
9383 // whose low result is fed to the ADDC we are checking.
9384
9385 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009386 LoMul = &AddcOp0;
9387 LowAdd = &AddcOp1;
9388 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00009389 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009390 LoMul = &AddcOp1;
9391 LowAdd = &AddcOp0;
9392 }
9393
Craig Topper062a2ba2014-04-25 05:30:21 +00009394 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009395 return SDValue();
9396
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009397 // Create the merged node.
9398 SelectionDAG &DAG = DCI.DAG;
9399
9400 // Build operand list.
9401 SmallVector<SDValue, 8> Ops;
9402 Ops.push_back(LoMul->getOperand(0));
9403 Ops.push_back(LoMul->getOperand(1));
9404 Ops.push_back(*LowAdd);
9405 Ops.push_back(*HiAdd);
9406
Andrew Trickef9de2a2013-05-25 02:42:55 +00009407 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00009408 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009409
9410 // Replace the ADDs' nodes uses by the MLA node's values.
9411 SDValue HiMLALResult(MLALNode.getNode(), 1);
9412 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
9413
9414 SDValue LoMLALResult(MLALNode.getNode(), 0);
9415 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
9416
9417 // Return original node to notify the driver to stop replacing.
9418 SDValue resNode(AddcNode, 0);
9419 return resNode;
9420}
9421
Sam Parkerd616cf02016-06-20 16:47:09 +00009422static SDValue AddCombineTo64bitUMAAL(SDNode *AddcNode,
9423 TargetLowering::DAGCombinerInfo &DCI,
9424 const ARMSubtarget *Subtarget) {
9425 // UMAAL is similar to UMLAL except that it adds two unsigned values.
9426 // While trying to combine for the other MLAL nodes, first search for the
9427 // chance to use UMAAL. Check if Addc uses another addc node which can first
9428 // be combined into a UMLAL. The other pattern is AddcNode being combined
9429 // into an UMLAL and then using another addc is handled in ISelDAGToDAG.
9430
Sam Parkere7d95052016-10-27 09:47:10 +00009431 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP() ||
Sam Parkerd5ca0a62016-07-25 10:11:00 +00009432 (Subtarget->isThumb() && !Subtarget->hasThumb2()))
Sam Parkerd616cf02016-06-20 16:47:09 +00009433 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
9434
9435 SDNode *PrevAddc = nullptr;
9436 if (AddcNode->getOperand(0).getOpcode() == ISD::ADDC)
9437 PrevAddc = AddcNode->getOperand(0).getNode();
9438 else if (AddcNode->getOperand(1).getOpcode() == ISD::ADDC)
9439 PrevAddc = AddcNode->getOperand(1).getNode();
9440
9441 // If there's no addc chains, just return a search for any MLAL.
9442 if (PrevAddc == nullptr)
9443 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
9444
9445 // Try to convert the addc operand to an MLAL and if that fails try to
9446 // combine AddcNode.
9447 SDValue MLAL = AddCombineTo64bitMLAL(PrevAddc, DCI, Subtarget);
9448 if (MLAL != SDValue(PrevAddc, 0))
9449 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
9450
9451 // Find the converted UMAAL or quit if it doesn't exist.
9452 SDNode *UmlalNode = nullptr;
9453 SDValue AddHi;
9454 if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) {
9455 UmlalNode = AddcNode->getOperand(0).getNode();
9456 AddHi = AddcNode->getOperand(1);
9457 } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) {
9458 UmlalNode = AddcNode->getOperand(1).getNode();
9459 AddHi = AddcNode->getOperand(0);
9460 } else {
9461 return SDValue();
9462 }
9463
9464 // The ADDC should be glued to an ADDE node, which uses the same UMLAL as
9465 // the ADDC as well as Zero.
9466 auto *Zero = dyn_cast<ConstantSDNode>(UmlalNode->getOperand(3));
9467
9468 if (!Zero || Zero->getZExtValue() != 0)
9469 return SDValue();
9470
9471 // Check that we have a glued ADDC node.
9472 if (AddcNode->getValueType(1) != MVT::Glue)
9473 return SDValue();
9474
9475 // Look for the glued ADDE.
9476 SDNode* AddeNode = AddcNode->getGluedUser();
9477 if (!AddeNode)
9478 return SDValue();
9479
9480 if ((AddeNode->getOperand(0).getNode() == Zero &&
9481 AddeNode->getOperand(1).getNode() == UmlalNode) ||
9482 (AddeNode->getOperand(0).getNode() == UmlalNode &&
9483 AddeNode->getOperand(1).getNode() == Zero)) {
9484
9485 SelectionDAG &DAG = DCI.DAG;
9486 SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1),
9487 UmlalNode->getOperand(2), AddHi };
9488 SDValue UMAAL = DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode),
9489 DAG.getVTList(MVT::i32, MVT::i32), Ops);
9490
9491 // Replace the ADDs' nodes uses by the UMAAL node's values.
9492 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1));
9493 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0));
9494
9495 // Return original node to notify the driver to stop replacing.
9496 return SDValue(AddcNode, 0);
9497 }
9498 return SDValue();
9499}
9500
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009501/// PerformADDCCombine - Target-specific dag combine transform from
Sam Parkerd616cf02016-06-20 16:47:09 +00009502/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL or
9503/// ISD::ADDC, ISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009504static SDValue PerformADDCCombine(SDNode *N,
9505 TargetLowering::DAGCombinerInfo &DCI,
9506 const ARMSubtarget *Subtarget) {
9507
Sam Parkerd616cf02016-06-20 16:47:09 +00009508 if (Subtarget->isThumb1Only()) return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009509
Sam Parkerd616cf02016-06-20 16:47:09 +00009510 // Only perform the checks after legalize when the pattern is available.
9511 if (DCI.isBeforeLegalize()) return SDValue();
9512
9513 return AddCombineTo64bitUMAAL(N, DCI, Subtarget);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009514}
9515
Bob Wilson728eb292010-07-29 20:34:14 +00009516/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
9517/// operands N0 and N1. This is a helper for PerformADDCombine that is
9518/// called with the default operands, and if that fails, with commuted
9519/// operands.
9520static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009521 TargetLowering::DAGCombinerInfo &DCI,
9522 const ARMSubtarget *Subtarget){
9523
9524 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009525 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00009526 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00009527
Chris Lattner4147f082009-03-12 06:52:53 +00009528 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009529 if (N0.getNode()->hasOneUse())
9530 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
9531 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00009532 return SDValue();
9533}
9534
Bob Wilson728eb292010-07-29 20:34:14 +00009535/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
9536///
9537static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009538 TargetLowering::DAGCombinerInfo &DCI,
9539 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00009540 SDValue N0 = N->getOperand(0);
9541 SDValue N1 = N->getOperand(1);
9542
9543 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009544 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00009545 return Result;
9546
9547 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00009548 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00009549}
9550
Chris Lattner4147f082009-03-12 06:52:53 +00009551/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00009552///
Chris Lattner4147f082009-03-12 06:52:53 +00009553static SDValue PerformSUBCombine(SDNode *N,
9554 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00009555 SDValue N0 = N->getOperand(0);
9556 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00009557
Chris Lattner4147f082009-03-12 06:52:53 +00009558 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009559 if (N1.getNode()->hasOneUse())
9560 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
9561 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00009562
Chris Lattner4147f082009-03-12 06:52:53 +00009563 return SDValue();
9564}
9565
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009566/// PerformVMULCombine
9567/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
9568/// special multiplier accumulator forwarding.
9569/// vmul d3, d0, d2
9570/// vmla d3, d1, d2
9571/// is faster than
9572/// vadd d3, d0, d1
9573/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00009574// However, for (A + B) * (A + B),
9575// vadd d2, d0, d1
9576// vmul d3, d0, d2
9577// vmla d3, d1, d2
9578// is slower than
9579// vadd d2, d0, d1
9580// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009581static SDValue PerformVMULCombine(SDNode *N,
9582 TargetLowering::DAGCombinerInfo &DCI,
9583 const ARMSubtarget *Subtarget) {
9584 if (!Subtarget->hasVMLxForwarding())
9585 return SDValue();
9586
9587 SelectionDAG &DAG = DCI.DAG;
9588 SDValue N0 = N->getOperand(0);
9589 SDValue N1 = N->getOperand(1);
9590 unsigned Opcode = N0.getOpcode();
9591 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
9592 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00009593 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009594 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
9595 Opcode != ISD::FADD && Opcode != ISD::FSUB)
9596 return SDValue();
9597 std::swap(N0, N1);
9598 }
9599
Weiming Zhao2052f482013-09-25 23:12:06 +00009600 if (N0 == N1)
9601 return SDValue();
9602
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009603 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009604 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009605 SDValue N00 = N0->getOperand(0);
9606 SDValue N01 = N0->getOperand(1);
9607 return DAG.getNode(Opcode, DL, VT,
9608 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
9609 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
9610}
9611
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009612static SDValue PerformMULCombine(SDNode *N,
9613 TargetLowering::DAGCombinerInfo &DCI,
9614 const ARMSubtarget *Subtarget) {
9615 SelectionDAG &DAG = DCI.DAG;
9616
9617 if (Subtarget->isThumb1Only())
9618 return SDValue();
9619
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009620 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9621 return SDValue();
9622
9623 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009624 if (VT.is64BitVector() || VT.is128BitVector())
9625 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009626 if (VT != MVT::i32)
9627 return SDValue();
9628
9629 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9630 if (!C)
9631 return SDValue();
9632
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009633 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009634 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009635
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009636 ShiftAmt = ShiftAmt & (32 - 1);
9637 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009638 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009639
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009640 SDValue Res;
9641 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009642
9643 if (MulAmt >= 0) {
9644 if (isPowerOf2_32(MulAmt - 1)) {
9645 // (mul x, 2^N + 1) => (add (shl x, N), x)
9646 Res = DAG.getNode(ISD::ADD, DL, VT,
9647 V,
9648 DAG.getNode(ISD::SHL, DL, VT,
9649 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009650 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009651 MVT::i32)));
9652 } else if (isPowerOf2_32(MulAmt + 1)) {
9653 // (mul x, 2^N - 1) => (sub (shl x, N), x)
9654 Res = DAG.getNode(ISD::SUB, DL, VT,
9655 DAG.getNode(ISD::SHL, DL, VT,
9656 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009657 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009658 MVT::i32)),
9659 V);
9660 } else
9661 return SDValue();
9662 } else {
9663 uint64_t MulAmtAbs = -MulAmt;
9664 if (isPowerOf2_32(MulAmtAbs + 1)) {
9665 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
9666 Res = DAG.getNode(ISD::SUB, DL, VT,
9667 V,
9668 DAG.getNode(ISD::SHL, DL, VT,
9669 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009670 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009671 MVT::i32)));
9672 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
9673 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
9674 Res = DAG.getNode(ISD::ADD, DL, VT,
9675 V,
9676 DAG.getNode(ISD::SHL, DL, VT,
9677 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009678 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009679 MVT::i32)));
9680 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009681 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009682
9683 } else
9684 return SDValue();
9685 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009686
9687 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009688 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009689 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009690
9691 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009692 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009693 return SDValue();
9694}
9695
Owen Anderson30c48922010-11-05 19:27:46 +00009696static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00009697 TargetLowering::DAGCombinerInfo &DCI,
9698 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00009699
Owen Anderson30c48922010-11-05 19:27:46 +00009700 // Attempt to use immediate-form VBIC
9701 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009702 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00009703 EVT VT = N->getValueType(0);
9704 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009705
Tanya Lattner266792a2011-04-07 15:24:20 +00009706 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9707 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009708
Owen Anderson30c48922010-11-05 19:27:46 +00009709 APInt SplatBits, SplatUndef;
9710 unsigned SplatBitSize;
9711 bool HasAnyUndefs;
9712 if (BVN &&
9713 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9714 if (SplatBitSize <= 64) {
9715 EVT VbicVT;
9716 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
9717 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009718 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009719 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00009720 if (Val.getNode()) {
9721 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009722 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00009723 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009724 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00009725 }
9726 }
9727 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009728
Evan Chenge87681c2012-02-23 01:19:06 +00009729 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009730 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009731 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009732 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009733 }
9734
Owen Anderson30c48922010-11-05 19:27:46 +00009735 return SDValue();
9736}
9737
Jim Grosbach11013ed2010-07-16 23:05:05 +00009738/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
9739static SDValue PerformORCombine(SDNode *N,
9740 TargetLowering::DAGCombinerInfo &DCI,
9741 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009742 // Attempt to use immediate-form VORR
9743 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009744 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009745 EVT VT = N->getValueType(0);
9746 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009747
Tanya Lattner266792a2011-04-07 15:24:20 +00009748 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9749 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009750
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009751 APInt SplatBits, SplatUndef;
9752 unsigned SplatBitSize;
9753 bool HasAnyUndefs;
9754 if (BVN && Subtarget->hasNEON() &&
9755 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9756 if (SplatBitSize <= 64) {
9757 EVT VorrVT;
9758 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9759 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009760 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009761 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009762 if (Val.getNode()) {
9763 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009764 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009765 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009766 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009767 }
9768 }
9769 }
9770
Evan Chenge87681c2012-02-23 01:19:06 +00009771 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009772 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009773 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009774 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009775 }
9776
Nadav Rotem3a94c542012-08-13 18:52:44 +00009777 // The code below optimizes (or (and X, Y), Z).
9778 // The AND operand needs to have a single user to make these optimizations
9779 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009780 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009781 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009782 return SDValue();
9783 SDValue N1 = N->getOperand(1);
9784
9785 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9786 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9787 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9788 APInt SplatUndef;
9789 unsigned SplatBitSize;
9790 bool HasAnyUndefs;
9791
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009792 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009793 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009794 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9795 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009796 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009797 HasAnyUndefs) && !HasAnyUndefs) {
9798 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9799 HasAnyUndefs) && !HasAnyUndefs) {
9800 // Ensure that the bit width of the constants are the same and that
9801 // the splat arguments are logical inverses as per the pattern we
9802 // are trying to simplify.
9803 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9804 SplatBits0 == ~SplatBits1) {
9805 // Canonicalize the vector type to make instruction selection
9806 // simpler.
9807 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9808 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9809 N0->getOperand(1),
9810 N0->getOperand(0),
9811 N1->getOperand(0));
9812 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9813 }
9814 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009815 }
9816 }
9817
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009818 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9819 // reasonable.
9820
Jim Grosbach11013ed2010-07-16 23:05:05 +00009821 // BFI is only available on V6T2+
9822 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9823 return SDValue();
9824
Andrew Trickef9de2a2013-05-25 02:42:55 +00009825 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009826 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009827 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009828 //
9829 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009830 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009831 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009832 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009833 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009834 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009835
Jim Grosbach11013ed2010-07-16 23:05:05 +00009836 if (VT != MVT::i32)
9837 return SDValue();
9838
Evan Cheng2e51bb42010-12-13 20:32:54 +00009839 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009840
Jim Grosbach11013ed2010-07-16 23:05:05 +00009841 // The value and the mask need to be constants so we can verify this is
9842 // actually a bitfield set. If the mask is 0xffff, we can do better
9843 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009844 SDValue MaskOp = N0.getOperand(1);
9845 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9846 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009847 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009848 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009849 if (Mask == 0xffff)
9850 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009851 SDValue Res;
9852 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009853 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9854 if (N1C) {
9855 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009856 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009857 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009858
Evan Cheng34345752010-12-11 04:11:38 +00009859 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009860 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009861
Evan Cheng2e51bb42010-12-13 20:32:54 +00009862 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009863 DAG.getConstant(Val, DL, MVT::i32),
9864 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009865
9866 // Do not add new nodes to DAG combiner worklist.
9867 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009868 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009869 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009870 } else if (N1.getOpcode() == ISD::AND) {
9871 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009872 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9873 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009874 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009875 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009876
Eric Christopherd5530962011-03-26 01:21:03 +00009877 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9878 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009879 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009880 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009881 // The pack halfword instruction works better for masks that fit it,
9882 // so use that when it's available.
9883 if (Subtarget->hasT2ExtractPack() &&
9884 (Mask == 0xffff || Mask == 0xffff0000))
9885 return SDValue();
9886 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009887 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009888 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009889 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009890 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009891 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009892 // Do not add new nodes to DAG combiner worklist.
9893 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009894 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009895 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009896 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009897 // The pack halfword instruction works better for masks that fit it,
9898 // so use that when it's available.
9899 if (Subtarget->hasT2ExtractPack() &&
9900 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9901 return SDValue();
9902 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009903 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009904 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009905 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009906 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009907 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009908 // Do not add new nodes to DAG combiner worklist.
9909 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009910 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009911 }
9912 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009913
Evan Cheng2e51bb42010-12-13 20:32:54 +00009914 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9915 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9916 ARM::isBitFieldInvertedMask(~Mask)) {
9917 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9918 // where lsb(mask) == #shamt and masked bits of B are known zero.
9919 SDValue ShAmt = N00.getOperand(1);
9920 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009921 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009922 if (ShAmtC != LSB)
9923 return SDValue();
9924
9925 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009926 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009927
9928 // Do not add new nodes to DAG combiner worklist.
9929 DCI.CombineTo(N, Res, false);
9930 }
9931
Jim Grosbach11013ed2010-07-16 23:05:05 +00009932 return SDValue();
9933}
9934
Evan Chenge87681c2012-02-23 01:19:06 +00009935static SDValue PerformXORCombine(SDNode *N,
9936 TargetLowering::DAGCombinerInfo &DCI,
9937 const ARMSubtarget *Subtarget) {
9938 EVT VT = N->getValueType(0);
9939 SelectionDAG &DAG = DCI.DAG;
9940
9941 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9942 return SDValue();
9943
9944 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009945 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009946 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009947 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009948 }
9949
9950 return SDValue();
9951}
9952
James Molloyce12c922015-11-11 15:40:40 +00009953// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9954// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9955// their position in "to" (Rd).
9956static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9957 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009958
James Molloyce12c922015-11-11 15:40:40 +00009959 SDValue From = N->getOperand(1);
9960 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9961 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9962
9963 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9964 // #C in the base of the SHR.
9965 if (From->getOpcode() == ISD::SRL &&
9966 isa<ConstantSDNode>(From->getOperand(1))) {
9967 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9968 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9969 FromMask <<= Shift.getLimitedValue(31);
9970 From = From->getOperand(0);
9971 }
9972
9973 return From;
9974}
9975
9976// If A and B contain one contiguous set of bits, does A | B == A . B?
9977//
9978// Neither A nor B must be zero.
9979static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9980 unsigned LastActiveBitInA = A.countTrailingZeros();
9981 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9982 return LastActiveBitInA - 1 == FirstActiveBitInB;
9983}
9984
9985static SDValue FindBFIToCombineWith(SDNode *N) {
9986 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9987 // if one exists.
9988 APInt ToMask, FromMask;
9989 SDValue From = ParseBFI(N, ToMask, FromMask);
9990 SDValue To = N->getOperand(0);
9991
9992 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9993 // aren't compatible, but not if they set the same bit in their destination as
9994 // we do (or that of any BFI we're going to combine with).
9995 SDValue V = To;
9996 APInt CombinedToMask = ToMask;
9997 while (V.getOpcode() == ARMISD::BFI) {
9998 APInt NewToMask, NewFromMask;
9999 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
10000 if (NewFrom != From) {
10001 // This BFI has a different base. Keep going.
10002 CombinedToMask |= NewToMask;
10003 V = V.getOperand(0);
10004 continue;
10005 }
10006
10007 // Do the written bits conflict with any we've seen so far?
10008 if ((NewToMask & CombinedToMask).getBoolValue())
10009 // Conflicting bits - bail out because going further is unsafe.
10010 return SDValue();
10011
10012 // Are the new bits contiguous when combined with the old bits?
10013 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
10014 BitsProperlyConcatenate(FromMask, NewFromMask))
10015 return V;
10016 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
10017 BitsProperlyConcatenate(NewFromMask, FromMask))
10018 return V;
Chad Rosier353d7192015-12-21 18:08:05 +000010019
James Molloyce12c922015-11-11 15:40:40 +000010020 // We've seen a write to some bits, so track it.
10021 CombinedToMask |= NewToMask;
10022 // Keep going...
10023 V = V.getOperand(0);
10024 }
10025
10026 return SDValue();
10027}
10028
Evan Chengc1778132010-12-14 03:22:07 +000010029static SDValue PerformBFICombine(SDNode *N,
10030 TargetLowering::DAGCombinerInfo &DCI) {
10031 SDValue N1 = N->getOperand(1);
10032 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +000010033 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
10034 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +000010035 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
10036 if (!N11C)
10037 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +000010038 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000010039 unsigned LSB = countTrailingZeros(~InvMask);
10040 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +000010041 assert(Width <
10042 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +000010043 "undefined behavior");
10044 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +000010045 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +000010046 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +000010047 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +000010048 N->getOperand(0), N1.getOperand(0),
10049 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +000010050 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
10051 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
10052 // Keep track of any consecutive bits set that all come from the same base
10053 // value. We can combine these together into a single BFI.
10054 SDValue CombineBFI = FindBFIToCombineWith(N);
10055 if (CombineBFI == SDValue())
10056 return SDValue();
10057
10058 // We've found a BFI.
10059 APInt ToMask1, FromMask1;
10060 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
10061
10062 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +000010063 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
10064 assert(From1 == From2);
10065 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +000010066
James Molloyce12c922015-11-11 15:40:40 +000010067 // First, unlink CombineBFI.
10068 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
10069 // Then create a new BFI, combining the two together.
10070 APInt NewFromMask = FromMask1 | FromMask2;
10071 APInt NewToMask = ToMask1 | ToMask2;
10072
10073 EVT VT = N->getValueType(0);
10074 SDLoc dl(N);
10075
10076 if (NewFromMask[0] == 0)
10077 From1 = DCI.DAG.getNode(
10078 ISD::SRL, dl, VT, From1,
10079 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
10080 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
10081 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +000010082 }
10083 return SDValue();
10084}
10085
Bob Wilson22806742010-09-22 22:09:21 +000010086/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
10087/// ARMISD::VMOVRRD.
10088static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +000010089 TargetLowering::DAGCombinerInfo &DCI,
10090 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +000010091 // vmovrrd(vmovdrr x, y) -> x,y
10092 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010093 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +000010094 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010095
10096 // vmovrrd(load f64) -> (load i32), (load i32)
10097 SDNode *InNode = InDouble.getNode();
10098 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
10099 InNode->getValueType(0) == MVT::f64 &&
10100 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
10101 !cast<LoadSDNode>(InNode)->isVolatile()) {
10102 // TODO: Should this be done for non-FrameIndex operands?
10103 LoadSDNode *LD = cast<LoadSDNode>(InNode);
10104
10105 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010106 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010107 SDValue BasePtr = LD->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +000010108 SDValue NewLD1 =
10109 DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(),
10110 LD->getAlignment(), LD->getMemOperand()->getFlags());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010111
10112 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010113 DAG.getConstant(4, DL, MVT::i32));
Justin Lebar9c375812016-07-15 18:27:10 +000010114 SDValue NewLD2 = DAG.getLoad(
10115 MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, LD->getPointerInfo(),
10116 std::min(4U, LD->getAlignment() / 2), LD->getMemOperand()->getFlags());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010117
10118 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +000010119 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +000010120 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010121 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010122 return Result;
10123 }
10124
Bob Wilson22806742010-09-22 22:09:21 +000010125 return SDValue();
10126}
10127
10128/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
10129/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
10130static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
10131 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
10132 SDValue Op0 = N->getOperand(0);
10133 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +000010134 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +000010135 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +000010136 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +000010137 Op1 = Op1.getOperand(0);
10138 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
10139 Op0.getNode() == Op1.getNode() &&
10140 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +000010141 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +000010142 N->getValueType(0), Op0.getOperand(0));
10143 return SDValue();
10144}
10145
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010146/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
10147/// are normal, non-volatile loads. If so, it is profitable to bitcast an
10148/// i64 vector to have f64 elements, since the value can then be loaded
10149/// directly into a VFP register.
10150static bool hasNormalLoadOperand(SDNode *N) {
10151 unsigned NumElts = N->getValueType(0).getVectorNumElements();
10152 for (unsigned i = 0; i < NumElts; ++i) {
10153 SDNode *Elt = N->getOperand(i).getNode();
10154 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
10155 return true;
10156 }
10157 return false;
10158}
10159
Bob Wilsoncb6db982010-09-17 22:59:05 +000010160/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
10161/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010162static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +000010163 TargetLowering::DAGCombinerInfo &DCI,
10164 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +000010165 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
10166 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
10167 // into a pair of GPRs, which is fine when the value is used as a scalar,
10168 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010169 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +000010170 if (N->getNumOperands() == 2)
10171 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010172 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +000010173
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010174 // Load i64 elements as f64 values so that type legalization does not split
10175 // them up into i32 values.
10176 EVT VT = N->getValueType(0);
10177 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
10178 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010179 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010180 SmallVector<SDValue, 8> Ops;
10181 unsigned NumElts = VT.getVectorNumElements();
10182 for (unsigned i = 0; i < NumElts; ++i) {
10183 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
10184 Ops.push_back(V);
10185 // Make the DAGCombiner fold the bitcast.
10186 DCI.AddToWorklist(V.getNode());
10187 }
10188 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +000010189 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010190 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
10191}
10192
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010193/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
10194static SDValue
10195PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
10196 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
10197 // At that time, we may have inserted bitcasts from integer to float.
10198 // If these bitcasts have survived DAGCombine, change the lowering of this
10199 // BUILD_VECTOR in something more vector friendly, i.e., that does not
10200 // force to use floating point types.
10201
10202 // Make sure we can change the type of the vector.
10203 // This is possible iff:
10204 // 1. The vector is only used in a bitcast to a integer type. I.e.,
10205 // 1.1. Vector is used only once.
10206 // 1.2. Use is a bit convert to an integer type.
10207 // 2. The size of its operands are 32-bits (64-bits are not legal).
10208 EVT VT = N->getValueType(0);
10209 EVT EltVT = VT.getVectorElementType();
10210
10211 // Check 1.1. and 2.
10212 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
10213 return SDValue();
10214
10215 // By construction, the input type must be float.
10216 assert(EltVT == MVT::f32 && "Unexpected type!");
10217
10218 // Check 1.2.
10219 SDNode *Use = *N->use_begin();
10220 if (Use->getOpcode() != ISD::BITCAST ||
10221 Use->getValueType(0).isFloatingPoint())
10222 return SDValue();
10223
10224 // Check profitability.
10225 // Model is, if more than half of the relevant operands are bitcast from
10226 // i32, turn the build_vector into a sequence of insert_vector_elt.
10227 // Relevant operands are everything that is not statically
10228 // (i.e., at compile time) bitcasted.
10229 unsigned NumOfBitCastedElts = 0;
10230 unsigned NumElts = VT.getVectorNumElements();
10231 unsigned NumOfRelevantElts = NumElts;
10232 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
10233 SDValue Elt = N->getOperand(Idx);
10234 if (Elt->getOpcode() == ISD::BITCAST) {
10235 // Assume only bit cast to i32 will go away.
10236 if (Elt->getOperand(0).getValueType() == MVT::i32)
10237 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +000010238 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010239 // Constants are statically casted, thus do not count them as
10240 // relevant operands.
10241 --NumOfRelevantElts;
10242 }
10243
10244 // Check if more than half of the elements require a non-free bitcast.
10245 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
10246 return SDValue();
10247
10248 SelectionDAG &DAG = DCI.DAG;
10249 // Create the new vector type.
10250 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
10251 // Check if the type is legal.
10252 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10253 if (!TLI.isTypeLegal(VecVT))
10254 return SDValue();
10255
10256 // Combine:
10257 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
10258 // => BITCAST INSERT_VECTOR_ELT
10259 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
10260 // (BITCAST EN), N.
10261 SDValue Vec = DAG.getUNDEF(VecVT);
10262 SDLoc dl(N);
10263 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
10264 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +000010265 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010266 continue;
10267 if (V.getOpcode() == ISD::BITCAST &&
10268 V->getOperand(0).getValueType() == MVT::i32)
10269 // Fold obvious case.
10270 V = V.getOperand(0);
10271 else {
Jim Grosbach1a597112014-04-03 23:43:18 +000010272 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010273 // Make the DAGCombiner fold the bitcasts.
10274 DCI.AddToWorklist(V.getNode());
10275 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010276 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010277 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
10278 }
10279 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
10280 // Make the DAGCombiner fold the bitcasts.
10281 DCI.AddToWorklist(Vec.getNode());
10282 return Vec;
10283}
10284
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010285/// PerformInsertEltCombine - Target-specific dag combine xforms for
10286/// ISD::INSERT_VECTOR_ELT.
10287static SDValue PerformInsertEltCombine(SDNode *N,
10288 TargetLowering::DAGCombinerInfo &DCI) {
10289 // Bitcast an i64 load inserted into a vector to f64.
10290 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10291 EVT VT = N->getValueType(0);
10292 SDNode *Elt = N->getOperand(1).getNode();
10293 if (VT.getVectorElementType() != MVT::i64 ||
10294 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
10295 return SDValue();
10296
10297 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010298 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010299 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10300 VT.getVectorNumElements());
10301 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
10302 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
10303 // Make the DAGCombiner fold the bitcasts.
10304 DCI.AddToWorklist(Vec.getNode());
10305 DCI.AddToWorklist(V.getNode());
10306 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
10307 Vec, V, N->getOperand(2));
10308 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +000010309}
10310
Bob Wilsonc7334a12010-10-27 20:38:28 +000010311/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
10312/// ISD::VECTOR_SHUFFLE.
10313static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
10314 // The LLVM shufflevector instruction does not require the shuffle mask
10315 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
10316 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
10317 // operands do not match the mask length, they are extended by concatenating
10318 // them with undef vectors. That is probably the right thing for other
10319 // targets, but for NEON it is better to concatenate two double-register
10320 // size vector operands into a single quad-register size vector. Do that
10321 // transformation here:
10322 // shuffle(concat(v1, undef), concat(v2, undef)) ->
10323 // shuffle(concat(v1, v2), undef)
10324 SDValue Op0 = N->getOperand(0);
10325 SDValue Op1 = N->getOperand(1);
10326 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
10327 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
10328 Op0.getNumOperands() != 2 ||
10329 Op1.getNumOperands() != 2)
10330 return SDValue();
10331 SDValue Concat0Op1 = Op0.getOperand(1);
10332 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +000010333 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +000010334 return SDValue();
10335 // Skip the transformation if any of the types are illegal.
10336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10337 EVT VT = N->getValueType(0);
10338 if (!TLI.isTypeLegal(VT) ||
10339 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
10340 !TLI.isTypeLegal(Concat1Op1.getValueType()))
10341 return SDValue();
10342
Andrew Trickef9de2a2013-05-25 02:42:55 +000010343 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +000010344 Op0.getOperand(0), Op1.getOperand(0));
10345 // Translate the shuffle mask.
10346 SmallVector<int, 16> NewMask;
10347 unsigned NumElts = VT.getVectorNumElements();
10348 unsigned HalfElts = NumElts/2;
10349 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10350 for (unsigned n = 0; n < NumElts; ++n) {
10351 int MaskElt = SVN->getMaskElt(n);
10352 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +000010353 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +000010354 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +000010355 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +000010356 NewElt = HalfElts + MaskElt - NumElts;
10357 NewMask.push_back(NewElt);
10358 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010359 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Craig Topper2bd8b4b2016-07-01 06:54:47 +000010360 DAG.getUNDEF(VT), NewMask);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010361}
10362
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010363/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
10364/// NEON load/store intrinsics, and generic vector load/stores, to merge
10365/// base address updates.
10366/// For generic load/stores, the memory type is assumed to be a vector.
10367/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +000010368static SDValue CombineBaseUpdate(SDNode *N,
10369 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +000010370 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010371 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
10372 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010373 const bool isStore = N->getOpcode() == ISD::STORE;
10374 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +000010375 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010376 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010377 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +000010378
10379 // Search for a use of the address operand that is an increment.
10380 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
10381 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
10382 SDNode *User = *UI;
10383 if (User->getOpcode() != ISD::ADD ||
10384 UI.getUse().getResNo() != Addr.getResNo())
10385 continue;
10386
10387 // Check that the add is independent of the load/store. Otherwise, folding
10388 // it would create a cycle.
10389 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
10390 continue;
10391
10392 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010393 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +000010394 bool isLaneOp = false;
10395 unsigned NewOpc = 0;
10396 unsigned NumVecs = 0;
10397 if (isIntrinsic) {
10398 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
10399 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +000010400 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +000010401 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
10402 NumVecs = 1; break;
10403 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
10404 NumVecs = 2; break;
10405 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
10406 NumVecs = 3; break;
10407 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
10408 NumVecs = 4; break;
10409 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
10410 NumVecs = 2; isLaneOp = true; break;
10411 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
10412 NumVecs = 3; isLaneOp = true; break;
10413 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
10414 NumVecs = 4; isLaneOp = true; break;
10415 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010416 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010417 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010418 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010419 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010420 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010421 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010422 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010423 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010424 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010425 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010426 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010427 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010428 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010429 }
10430 } else {
10431 isLaneOp = true;
10432 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +000010433 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +000010434 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
10435 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
10436 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010437 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
10438 NumVecs = 1; isLaneOp = false; break;
10439 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
10440 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010441 }
10442 }
10443
10444 // Find the size of memory referenced by the load/store.
10445 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010446 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +000010447 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010448 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +000010449 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010450 } else {
10451 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
10452 VecTy = N->getOperand(1).getValueType();
10453 }
10454
Bob Wilson06fce872011-02-07 17:43:21 +000010455 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
10456 if (isLaneOp)
10457 NumBytes /= VecTy.getVectorNumElements();
10458
10459 // If the increment is a constant, it must match the memory ref size.
10460 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
10461 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
10462 uint64_t IncVal = CInc->getZExtValue();
10463 if (IncVal != NumBytes)
10464 continue;
10465 } else if (NumBytes >= 3 * 16) {
10466 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
10467 // separate instructions that make it harder to use a non-constant update.
10468 continue;
10469 }
10470
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010471 // OK, we found an ADD we can fold into the base update.
10472 // Now, create a _UPD node, taking care of not breaking alignment.
10473
10474 EVT AlignedVecTy = VecTy;
10475 unsigned Alignment = MemN->getAlignment();
10476
10477 // If this is a less-than-standard-aligned load/store, change the type to
10478 // match the standard alignment.
10479 // The alignment is overlooked when selecting _UPD variants; and it's
10480 // easier to introduce bitcasts here than fix that.
10481 // There are 3 ways to get to this base-update combine:
10482 // - intrinsics: they are assumed to be properly aligned (to the standard
10483 // alignment of the memory type), so we don't need to do anything.
10484 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
10485 // intrinsics, so, likewise, there's nothing to do.
10486 // - generic load/store instructions: the alignment is specified as an
10487 // explicit operand, rather than implicitly as the standard alignment
10488 // of the memory type (like the intrisics). We need to change the
10489 // memory type to match the explicit alignment. That way, we don't
10490 // generate non-standard-aligned ARMISD::VLDx nodes.
10491 if (isa<LSBaseSDNode>(N)) {
10492 if (Alignment == 0)
10493 Alignment = 1;
10494 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
10495 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
10496 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
10497 assert(!isLaneOp && "Unexpected generic load/store lane.");
10498 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
10499 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
10500 }
10501 // Don't set an explicit alignment on regular load/stores that we want
10502 // to transform to VLD/VST 1_UPD nodes.
10503 // This matches the behavior of regular load/stores, which only get an
10504 // explicit alignment if the MMO alignment is larger than the standard
10505 // alignment of the memory type.
10506 // Intrinsics, however, always get an explicit alignment, set to the
10507 // alignment of the MMO.
10508 Alignment = 1;
10509 }
10510
Bob Wilson06fce872011-02-07 17:43:21 +000010511 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010512 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +000010513 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010514 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +000010515 unsigned n;
10516 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010517 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +000010518 Tys[n++] = MVT::i32;
10519 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +000010520 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010521
10522 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +000010523 SmallVector<SDValue, 8> Ops;
10524 Ops.push_back(N->getOperand(0)); // incoming chain
10525 Ops.push_back(N->getOperand(AddrOpIdx));
10526 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010527
10528 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
10529 // Try to match the intrinsic's signature
10530 Ops.push_back(StN->getValue());
10531 } else {
10532 // Loads (and of course intrinsics) match the intrinsics' signature,
10533 // so just add all but the alignment operand.
10534 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
10535 Ops.push_back(N->getOperand(i));
10536 }
10537
10538 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010539 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010540
10541 // If this is a non-standard-aligned STORE, the penultimate operand is the
10542 // stored value. Bitcast it to the aligned type.
10543 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
10544 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010545 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010546 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010547
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010548 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010549 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010550 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +000010551
10552 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +000010553 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010554 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +000010555 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010556
10557 // If this is an non-standard-aligned LOAD, the first result is the loaded
10558 // value. Bitcast it to the expected result type.
10559 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
10560 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010561 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010562 }
10563
Bob Wilson06fce872011-02-07 17:43:21 +000010564 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
10565 DCI.CombineTo(N, NewResults);
10566 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
10567
10568 break;
Owen Anderson77aa2662011-04-05 21:48:57 +000010569 }
Bob Wilson06fce872011-02-07 17:43:21 +000010570 return SDValue();
10571}
10572
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010573static SDValue PerformVLDCombine(SDNode *N,
10574 TargetLowering::DAGCombinerInfo &DCI) {
10575 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10576 return SDValue();
10577
10578 return CombineBaseUpdate(N, DCI);
10579}
10580
Bob Wilson2d790df2010-11-28 06:51:26 +000010581/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
10582/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
10583/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
10584/// return true.
10585static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
10586 SelectionDAG &DAG = DCI.DAG;
10587 EVT VT = N->getValueType(0);
10588 // vldN-dup instructions only support 64-bit vectors for N > 1.
10589 if (!VT.is64BitVector())
10590 return false;
10591
10592 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
10593 SDNode *VLD = N->getOperand(0).getNode();
10594 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
10595 return false;
10596 unsigned NumVecs = 0;
10597 unsigned NewOpc = 0;
10598 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
10599 if (IntNo == Intrinsic::arm_neon_vld2lane) {
10600 NumVecs = 2;
10601 NewOpc = ARMISD::VLD2DUP;
10602 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
10603 NumVecs = 3;
10604 NewOpc = ARMISD::VLD3DUP;
10605 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
10606 NumVecs = 4;
10607 NewOpc = ARMISD::VLD4DUP;
10608 } else {
10609 return false;
10610 }
10611
10612 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
10613 // numbers match the load.
10614 unsigned VLDLaneNo =
10615 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
10616 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
10617 UI != UE; ++UI) {
10618 // Ignore uses of the chain result.
10619 if (UI.getUse().getResNo() == NumVecs)
10620 continue;
10621 SDNode *User = *UI;
10622 if (User->getOpcode() != ARMISD::VDUPLANE ||
10623 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
10624 return false;
10625 }
10626
10627 // Create the vldN-dup node.
10628 EVT Tys[5];
10629 unsigned n;
10630 for (n = 0; n < NumVecs; ++n)
10631 Tys[n] = VT;
10632 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +000010633 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +000010634 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
10635 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010636 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +000010637 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +000010638 VLDMemInt->getMemOperand());
10639
10640 // Update the uses.
10641 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
10642 UI != UE; ++UI) {
10643 unsigned ResNo = UI.getUse().getResNo();
10644 // Ignore uses of the chain result.
10645 if (ResNo == NumVecs)
10646 continue;
10647 SDNode *User = *UI;
10648 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
10649 }
10650
10651 // Now the vldN-lane intrinsic is dead except for its chain result.
10652 // Update uses of the chain.
10653 std::vector<SDValue> VLDDupResults;
10654 for (unsigned n = 0; n < NumVecs; ++n)
10655 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
10656 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
10657 DCI.CombineTo(VLD, VLDDupResults);
10658
10659 return true;
10660}
10661
Bob Wilson103a0dc2010-07-14 01:22:12 +000010662/// PerformVDUPLANECombine - Target-specific dag combine xforms for
10663/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +000010664static SDValue PerformVDUPLANECombine(SDNode *N,
10665 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +000010666 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010667
Bob Wilson2d790df2010-11-28 06:51:26 +000010668 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
10669 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
10670 if (CombineVLDDUP(N, DCI))
10671 return SDValue(N, 0);
10672
10673 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
10674 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +000010675 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010676 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +000010677 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010678 return SDValue();
10679
10680 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
Sanjay Patel284582b2016-09-14 16:54:10 +000010681 unsigned EltSize = Op.getScalarValueSizeInBits();
Bob Wilson103a0dc2010-07-14 01:22:12 +000010682 // The canonical VMOV for a zero vector uses a 32-bit element size.
10683 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10684 unsigned EltBits;
10685 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
10686 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +000010687 EVT VT = N->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010688 if (EltSize > VT.getScalarSizeInBits())
Bob Wilson103a0dc2010-07-14 01:22:12 +000010689 return SDValue();
10690
Andrew Trickef9de2a2013-05-25 02:42:55 +000010691 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010692}
10693
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010694static SDValue PerformLOADCombine(SDNode *N,
10695 TargetLowering::DAGCombinerInfo &DCI) {
10696 EVT VT = N->getValueType(0);
10697
10698 // If this is a legal vector load, try to combine it into a VLD1_UPD.
10699 if (ISD::isNormalLoad(N) && VT.isVector() &&
10700 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10701 return CombineBaseUpdate(N, DCI);
10702
10703 return SDValue();
10704}
10705
Ahmed Bougacha23167462014-12-09 21:26:53 +000010706/// PerformSTORECombine - Target-specific dag combine xforms for
10707/// ISD::STORE.
10708static SDValue PerformSTORECombine(SDNode *N,
10709 TargetLowering::DAGCombinerInfo &DCI) {
10710 StoreSDNode *St = cast<StoreSDNode>(N);
10711 if (St->isVolatile())
10712 return SDValue();
10713
10714 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
10715 // pack all of the elements in one place. Next, store to memory in fewer
10716 // chunks.
10717 SDValue StVal = St->getValue();
10718 EVT VT = StVal.getValueType();
10719 if (St->isTruncatingStore() && VT.isVector()) {
10720 SelectionDAG &DAG = DCI.DAG;
10721 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10722 EVT StVT = St->getMemoryVT();
10723 unsigned NumElems = VT.getVectorNumElements();
10724 assert(StVT != VT && "Cannot truncate to the same type");
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010725 unsigned FromEltSz = VT.getScalarSizeInBits();
10726 unsigned ToEltSz = StVT.getScalarSizeInBits();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010727
10728 // From, To sizes and ElemCount must be pow of two
10729 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
10730
10731 // We are going to use the original vector elt for storing.
10732 // Accumulated smaller vector elements must be a multiple of the store size.
10733 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
10734
10735 unsigned SizeRatio = FromEltSz / ToEltSz;
10736 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
10737
10738 // Create a type on which we perform the shuffle.
10739 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
10740 NumElems*SizeRatio);
10741 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
10742
10743 SDLoc DL(St);
10744 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
10745 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
10746 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +000010747 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
10748 ? (i + 1) * SizeRatio - 1
10749 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +000010750
10751 // Can't shuffle using an illegal type.
10752 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10753
10754 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10755 DAG.getUNDEF(WideVec.getValueType()),
Craig Topper2bd8b4b2016-07-01 06:54:47 +000010756 ShuffleVec);
Ahmed Bougacha23167462014-12-09 21:26:53 +000010757 // At this point all of the data is stored at the bottom of the
10758 // register. We now need to save it to mem.
10759
10760 // Find the largest store unit
10761 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010762 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010763 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10764 StoreType = Tp;
10765 }
10766 // Didn't find a legal store type.
10767 if (!TLI.isTypeLegal(StoreType))
10768 return SDValue();
10769
10770 // Bitcast the original vector into a vector of store-size units
10771 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10772 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10773 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10774 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10775 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010776 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10777 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010778 SDValue BasePtr = St->getBasePtr();
10779
10780 // Perform one or more big stores into memory.
10781 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10782 for (unsigned I = 0; I < E; I++) {
10783 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10784 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010785 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010786 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
Justin Lebar9c375812016-07-15 18:27:10 +000010787 St->getPointerInfo(), St->getAlignment(),
10788 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010789 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10790 Increment);
10791 Chains.push_back(Ch);
10792 }
10793 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10794 }
10795
10796 if (!ISD::isNormalStore(St))
10797 return SDValue();
10798
10799 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10800 // ARM stores of arguments in the same cache line.
10801 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10802 StVal.getNode()->hasOneUse()) {
10803 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010804 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010805 SDLoc DL(St);
10806 SDValue BasePtr = St->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +000010807 SDValue NewST1 = DAG.getStore(
10808 St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0),
10809 BasePtr, St->getPointerInfo(), St->getAlignment(),
10810 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010811
10812 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010813 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010814 return DAG.getStore(NewST1.getValue(0), DL,
10815 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Justin Lebar9c375812016-07-15 18:27:10 +000010816 OffsetPtr, St->getPointerInfo(),
10817 std::min(4U, St->getAlignment() / 2),
10818 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010819 }
10820
10821 if (StVal.getValueType() == MVT::i64 &&
10822 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10823
10824 // Bitcast an i64 store extracted from a vector to f64.
10825 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10826 SelectionDAG &DAG = DCI.DAG;
10827 SDLoc dl(StVal);
10828 SDValue IntVec = StVal.getOperand(0);
10829 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10830 IntVec.getValueType().getVectorNumElements());
10831 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10832 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10833 Vec, StVal.getOperand(1));
10834 dl = SDLoc(N);
10835 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10836 // Make the DAGCombiner fold the bitcasts.
10837 DCI.AddToWorklist(Vec.getNode());
10838 DCI.AddToWorklist(ExtElt.getNode());
10839 DCI.AddToWorklist(V.getNode());
10840 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +000010841 St->getPointerInfo(), St->getAlignment(),
10842 St->getMemOperand()->getFlags(), St->getAAInfo());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010843 }
10844
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010845 // If this is a legal vector store, try to combine it into a VST1_UPD.
10846 if (ISD::isNormalStore(N) && VT.isVector() &&
10847 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10848 return CombineBaseUpdate(N, DCI);
10849
Ahmed Bougacha23167462014-12-09 21:26:53 +000010850 return SDValue();
10851}
10852
Chad Rosierfa8d8932011-06-24 19:23:04 +000010853/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10854/// can replace combinations of VMUL and VCVT (floating-point to integer)
10855/// when the VMUL has a constant operand that is a power of 2.
10856///
10857/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10858/// vmul.f32 d16, d17, d16
10859/// vcvt.s32.f32 d16, d16
10860/// becomes:
10861/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010862static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010863 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010864 if (!Subtarget->hasNEON())
10865 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010866
Chad Rosiera087fd22015-10-06 20:23:42 +000010867 SDValue Op = N->getOperand(0);
Tim Northover498c56c2016-03-17 20:10:28 +000010868 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
10869 Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010870 return SDValue();
10871
Chad Rosierfa8d8932011-06-24 19:23:04 +000010872 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010873 if (!isa<BuildVectorSDNode>(ConstVec))
10874 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010875
Tim Northover7cbc2152013-06-28 15:29:25 +000010876 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010877 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010878 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010879 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010880 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010881 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010882 // These instructions only exist converting from f32 to i32. We can handle
10883 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010884 // be lossy. We also can't handle more then 4 lanes, since these intructions
10885 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010886 return SDValue();
10887 }
10888
Chad Rosier169865f2015-10-07 17:28:58 +000010889 BitVector UndefElements;
10890 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10891 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10892 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010893 return SDValue();
10894
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010895 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010896 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010897 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10898 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010899 SDValue FixConv = DAG.getNode(
10900 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10901 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010902 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010903
Chad Rosier9df4aff2015-10-06 20:45:45 +000010904 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010905 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010906
10907 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010908}
10909
10910/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10911/// can replace combinations of VCVT (integer to floating-point) and VDIV
10912/// when the VDIV has a constant operand that is a power of 2.
10913///
10914/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10915/// vcvt.f32.s32 d16, d16
10916/// vdiv.f32 d16, d17, d16
10917/// becomes:
10918/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010919static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010920 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010921 if (!Subtarget->hasNEON())
10922 return SDValue();
10923
Chad Rosierfa8d8932011-06-24 19:23:04 +000010924 SDValue Op = N->getOperand(0);
10925 unsigned OpOpcode = Op.getNode()->getOpcode();
Tim Northover498c56c2016-03-17 20:10:28 +000010926 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010927 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10928 return SDValue();
10929
Chad Rosierfa8d8932011-06-24 19:23:04 +000010930 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010931 if (!isa<BuildVectorSDNode>(ConstVec))
10932 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010933
Tim Northover7cbc2152013-06-28 15:29:25 +000010934 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010935 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010936 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010937 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010938 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10939 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010940 // These instructions only exist converting from i32 to f32. We can handle
10941 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010942 // be lossy. We also can't handle more then 4 lanes, since these intructions
10943 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010944 return SDValue();
10945 }
10946
Chad Rosier169865f2015-10-07 17:28:58 +000010947 BitVector UndefElements;
10948 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10949 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10950 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010951 return SDValue();
10952
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010953 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010954 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010955 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010956 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010957 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010958 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010959 ConvInput);
10960
Eric Christopher1b8b94192011-06-29 21:10:36 +000010961 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010962 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010963 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010964 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010965 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010966 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010967}
10968
10969/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010970/// operand of a vector shift operation, where all the elements of the
10971/// build_vector must have the same constant integer value.
10972static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10973 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010974 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010975 Op = Op.getOperand(0);
10976 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10977 APInt SplatBits, SplatUndef;
10978 unsigned SplatBitSize;
10979 bool HasAnyUndefs;
10980 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10981 HasAnyUndefs, ElementBits) ||
10982 SplatBitSize > ElementBits)
10983 return false;
10984 Cnt = SplatBits.getSExtValue();
10985 return true;
10986}
10987
10988/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10989/// operand of a vector shift left operation. That value must be in the range:
10990/// 0 <= Value < ElementBits for a left shift; or
10991/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010992static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010993 assert(VT.isVector() && "vector shift count is not a vector type");
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010994 int64_t ElementBits = VT.getScalarSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010995 if (! getVShiftImm(Op, ElementBits, Cnt))
10996 return false;
10997 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10998}
10999
11000/// isVShiftRImm - Check if this is a valid build_vector for the immediate
11001/// operand of a vector shift right operation. For a shift opcode, the value
11002/// is positive, but for an intrinsic the value count must be negative. The
11003/// absolute value must be in the range:
11004/// 1 <= |Value| <= ElementBits for a right shift; or
11005/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011006static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000011007 int64_t &Cnt) {
11008 assert(VT.isVector() && "vector shift count is not a vector type");
Sanjay Patel1ed771f2016-09-14 16:37:15 +000011009 int64_t ElementBits = VT.getScalarSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000011010 if (! getVShiftImm(Op, ElementBits, Cnt))
11011 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000011012 if (!isIntrinsic)
11013 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
11014 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000011015 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000011016 return true;
11017 }
11018 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000011019}
11020
11021/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
11022static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
11023 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
11024 switch (IntNo) {
11025 default:
11026 // Don't do anything for most intrinsics.
11027 break;
11028
11029 // Vector shifts: check for immediate versions and lower them.
11030 // Note: This is done during DAG combining instead of DAG legalizing because
11031 // the build_vectors for 64-bit vector element shift counts are generally
11032 // not legal, and it is hard to see their values after they get legalized to
11033 // loads from a constant pool.
11034 case Intrinsic::arm_neon_vshifts:
11035 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000011036 case Intrinsic::arm_neon_vrshifts:
11037 case Intrinsic::arm_neon_vrshiftu:
11038 case Intrinsic::arm_neon_vrshiftn:
11039 case Intrinsic::arm_neon_vqshifts:
11040 case Intrinsic::arm_neon_vqshiftu:
11041 case Intrinsic::arm_neon_vqshiftsu:
11042 case Intrinsic::arm_neon_vqshiftns:
11043 case Intrinsic::arm_neon_vqshiftnu:
11044 case Intrinsic::arm_neon_vqshiftnsu:
11045 case Intrinsic::arm_neon_vqrshiftns:
11046 case Intrinsic::arm_neon_vqrshiftnu:
11047 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011048 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000011049 int64_t Cnt;
11050 unsigned VShiftOpc = 0;
11051
11052 switch (IntNo) {
11053 case Intrinsic::arm_neon_vshifts:
11054 case Intrinsic::arm_neon_vshiftu:
11055 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
11056 VShiftOpc = ARMISD::VSHL;
11057 break;
11058 }
11059 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
11060 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
11061 ARMISD::VSHRs : ARMISD::VSHRu);
11062 break;
11063 }
11064 return SDValue();
11065
Bob Wilson2e076c42009-06-22 23:27:02 +000011066 case Intrinsic::arm_neon_vrshifts:
11067 case Intrinsic::arm_neon_vrshiftu:
11068 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
11069 break;
11070 return SDValue();
11071
11072 case Intrinsic::arm_neon_vqshifts:
11073 case Intrinsic::arm_neon_vqshiftu:
11074 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
11075 break;
11076 return SDValue();
11077
11078 case Intrinsic::arm_neon_vqshiftsu:
11079 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
11080 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000011081 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000011082
Bob Wilson2e076c42009-06-22 23:27:02 +000011083 case Intrinsic::arm_neon_vrshiftn:
11084 case Intrinsic::arm_neon_vqshiftns:
11085 case Intrinsic::arm_neon_vqshiftnu:
11086 case Intrinsic::arm_neon_vqshiftnsu:
11087 case Intrinsic::arm_neon_vqrshiftns:
11088 case Intrinsic::arm_neon_vqrshiftnu:
11089 case Intrinsic::arm_neon_vqrshiftnsu:
11090 // Narrowing shifts require an immediate right shift.
11091 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
11092 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000011093 llvm_unreachable("invalid shift count for narrowing vector shift "
11094 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000011095
11096 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000011097 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000011098 }
11099
11100 switch (IntNo) {
11101 case Intrinsic::arm_neon_vshifts:
11102 case Intrinsic::arm_neon_vshiftu:
11103 // Opcode already set above.
11104 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000011105 case Intrinsic::arm_neon_vrshifts:
11106 VShiftOpc = ARMISD::VRSHRs; break;
11107 case Intrinsic::arm_neon_vrshiftu:
11108 VShiftOpc = ARMISD::VRSHRu; break;
11109 case Intrinsic::arm_neon_vrshiftn:
11110 VShiftOpc = ARMISD::VRSHRN; break;
11111 case Intrinsic::arm_neon_vqshifts:
11112 VShiftOpc = ARMISD::VQSHLs; break;
11113 case Intrinsic::arm_neon_vqshiftu:
11114 VShiftOpc = ARMISD::VQSHLu; break;
11115 case Intrinsic::arm_neon_vqshiftsu:
11116 VShiftOpc = ARMISD::VQSHLsu; break;
11117 case Intrinsic::arm_neon_vqshiftns:
11118 VShiftOpc = ARMISD::VQSHRNs; break;
11119 case Intrinsic::arm_neon_vqshiftnu:
11120 VShiftOpc = ARMISD::VQSHRNu; break;
11121 case Intrinsic::arm_neon_vqshiftnsu:
11122 VShiftOpc = ARMISD::VQSHRNsu; break;
11123 case Intrinsic::arm_neon_vqrshiftns:
11124 VShiftOpc = ARMISD::VQRSHRNs; break;
11125 case Intrinsic::arm_neon_vqrshiftnu:
11126 VShiftOpc = ARMISD::VQRSHRNu; break;
11127 case Intrinsic::arm_neon_vqrshiftnsu:
11128 VShiftOpc = ARMISD::VQRSHRNsu; break;
11129 }
11130
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011131 SDLoc dl(N);
11132 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
11133 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000011134 }
11135
11136 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011137 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000011138 int64_t Cnt;
11139 unsigned VShiftOpc = 0;
11140
11141 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
11142 VShiftOpc = ARMISD::VSLI;
11143 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
11144 VShiftOpc = ARMISD::VSRI;
11145 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011146 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000011147 }
11148
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011149 SDLoc dl(N);
11150 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000011151 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011152 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000011153 }
11154
11155 case Intrinsic::arm_neon_vqrshifts:
11156 case Intrinsic::arm_neon_vqrshiftu:
11157 // No immediate versions of these to check for.
11158 break;
11159 }
11160
11161 return SDValue();
11162}
11163
11164/// PerformShiftCombine - Checks for immediate versions of vector shifts and
11165/// lowers them. As with the vector shift intrinsics, this is done during DAG
11166/// combining instead of DAG legalizing because the build_vectors for 64-bit
11167/// vector element shift counts are generally not legal, and it is hard to see
11168/// their values after they get legalized to loads from a constant pool.
11169static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
11170 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011171 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000011172 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
11173 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
11174 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
11175 SDValue N1 = N->getOperand(1);
11176 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
11177 SDValue N0 = N->getOperand(0);
11178 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
11179 DAG.MaskedValueIsZero(N0.getOperand(0),
11180 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000011181 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000011182 }
11183 }
Bob Wilson2e076c42009-06-22 23:27:02 +000011184
11185 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000011186 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11187 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000011188 return SDValue();
11189
11190 assert(ST->hasNEON() && "unexpected vector shift");
11191 int64_t Cnt;
11192
11193 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011194 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000011195
11196 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011197 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
11198 SDLoc dl(N);
11199 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
11200 DAG.getConstant(Cnt, dl, MVT::i32));
11201 }
Bob Wilson2e076c42009-06-22 23:27:02 +000011202 break;
11203
11204 case ISD::SRA:
11205 case ISD::SRL:
11206 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
11207 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
11208 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011209 SDLoc dl(N);
11210 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
11211 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000011212 }
11213 }
11214 return SDValue();
11215}
11216
11217/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
11218/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
11219static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
11220 const ARMSubtarget *ST) {
11221 SDValue N0 = N->getOperand(0);
11222
11223 // Check for sign- and zero-extensions of vector extract operations of 8-
11224 // and 16-bit vector elements. NEON supports these directly. They are
11225 // handled during DAG combining because type legalization will promote them
11226 // to 32-bit types and it is messy to recognize the operations after that.
11227 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
11228 SDValue Vec = N0.getOperand(0);
11229 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000011230 EVT VT = N->getValueType(0);
11231 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000011232 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11233
Owen Anderson9f944592009-08-11 20:47:22 +000011234 if (VT == MVT::i32 &&
11235 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000011236 TLI.isTypeLegal(Vec.getValueType()) &&
11237 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000011238
11239 unsigned Opc = 0;
11240 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011241 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000011242 case ISD::SIGN_EXTEND:
11243 Opc = ARMISD::VGETLANEs;
11244 break;
11245 case ISD::ZERO_EXTEND:
11246 case ISD::ANY_EXTEND:
11247 Opc = ARMISD::VGETLANEu;
11248 break;
11249 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000011250 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000011251 }
11252 }
11253
11254 return SDValue();
11255}
11256
James Molloy9d55f192015-11-10 14:22:05 +000011257static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
11258 APInt &KnownOne) {
11259 if (Op.getOpcode() == ARMISD::BFI) {
11260 // Conservatively, we can recurse down the first operand
11261 // and just mask out all affected bits.
11262 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
11263
11264 // The operand to BFI is already a mask suitable for removing the bits it
11265 // sets.
11266 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
Benjamin Kramer46e38f32016-06-08 10:01:20 +000011267 const APInt &Mask = CI->getAPIntValue();
James Molloy9d55f192015-11-10 14:22:05 +000011268 KnownZero &= Mask;
11269 KnownOne &= Mask;
11270 return;
11271 }
11272 if (Op.getOpcode() == ARMISD::CMOV) {
11273 APInt KZ2(KnownZero.getBitWidth(), 0);
11274 APInt KO2(KnownOne.getBitWidth(), 0);
11275 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
11276 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
11277
11278 KnownZero &= KZ2;
11279 KnownOne &= KO2;
11280 return;
11281 }
11282 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
11283}
11284
11285SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
11286 // If we have a CMOV, OR and AND combination such as:
11287 // if (x & CN)
11288 // y |= CM;
11289 //
11290 // And:
11291 // * CN is a single bit;
11292 // * All bits covered by CM are known zero in y
11293 //
11294 // Then we can convert this into a sequence of BFI instructions. This will
11295 // always be a win if CM is a single bit, will always be no worse than the
11296 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
11297 // three bits (due to the extra IT instruction).
11298
11299 SDValue Op0 = CMOV->getOperand(0);
11300 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000011301 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
11302 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000011303 SDValue CmpZ = CMOV->getOperand(4);
11304
James Molloy20180912015-11-16 10:49:25 +000011305 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000011306 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000011307 return SDValue();
11308
James Molloy9d55f192015-11-10 14:22:05 +000011309 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
11310 SDValue And = CmpZ->getOperand(0);
11311 if (And->getOpcode() != ISD::AND)
11312 return SDValue();
11313 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
11314 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
11315 return SDValue();
11316 SDValue X = And->getOperand(0);
11317
James Molloy8e99e972015-11-12 13:49:17 +000011318 if (CC == ARMCC::EQ) {
11319 // We're performing an "equal to zero" compare. Swap the operands so we
11320 // canonicalize on a "not equal to zero" compare.
11321 std::swap(Op0, Op1);
11322 } else {
11323 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
11324 }
Junmo Park1108ab02016-02-19 01:46:04 +000011325
James Molloy9d55f192015-11-10 14:22:05 +000011326 if (Op1->getOpcode() != ISD::OR)
11327 return SDValue();
11328
11329 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
11330 if (!OrC)
11331 return SDValue();
11332 SDValue Y = Op1->getOperand(0);
11333
11334 if (Op0 != Y)
11335 return SDValue();
11336
11337 // Now, is it profitable to continue?
11338 APInt OrCI = OrC->getAPIntValue();
11339 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
11340 if (OrCI.countPopulation() > Heuristic)
11341 return SDValue();
11342
11343 // Lastly, can we determine that the bits defined by OrCI
11344 // are zero in Y?
11345 APInt KnownZero, KnownOne;
11346 computeKnownBits(DAG, Y, KnownZero, KnownOne);
11347 if ((OrCI & KnownZero) != OrCI)
11348 return SDValue();
11349
11350 // OK, we can do the combine.
11351 SDValue V = Y;
11352 SDLoc dl(X);
11353 EVT VT = X.getValueType();
11354 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000011355
James Molloy9d55f192015-11-10 14:22:05 +000011356 if (BitInX != 0) {
11357 // We must shift X first.
11358 X = DAG.getNode(ISD::SRL, dl, VT, X,
11359 DAG.getConstant(BitInX, dl, VT));
11360 }
11361
11362 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
11363 BitInY < NumActiveBits; ++BitInY) {
11364 if (OrCI[BitInY] == 0)
11365 continue;
11366 APInt Mask(VT.getSizeInBits(), 0);
11367 Mask.setBit(BitInY);
11368 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
11369 // Confusingly, the operand is an *inverted* mask.
11370 DAG.getConstant(~Mask, dl, VT));
11371 }
11372
11373 return V;
11374}
11375
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011376/// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND.
11377SDValue
11378ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const {
11379 SDValue Cmp = N->getOperand(4);
11380 if (Cmp.getOpcode() != ARMISD::CMPZ)
11381 // Only looking at NE cases.
11382 return SDValue();
11383
11384 EVT VT = N->getValueType(0);
11385 SDLoc dl(N);
11386 SDValue LHS = Cmp.getOperand(0);
11387 SDValue RHS = Cmp.getOperand(1);
11388 SDValue Chain = N->getOperand(0);
11389 SDValue BB = N->getOperand(1);
11390 SDValue ARMcc = N->getOperand(2);
11391 ARMCC::CondCodes CC =
11392 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
11393
11394 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0))
11395 // -> (brcond Chain BB CC CPSR Cmp)
11396 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() &&
11397 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV &&
11398 LHS->getOperand(0)->hasOneUse()) {
11399 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0));
11400 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1));
11401 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
11402 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
11403 if ((LHS00C && LHS00C->getZExtValue() == 0) &&
11404 (LHS01C && LHS01C->getZExtValue() == 1) &&
11405 (LHS1C && LHS1C->getZExtValue() == 1) &&
11406 (RHSC && RHSC->getZExtValue() == 0)) {
11407 return DAG.getNode(
11408 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2),
11409 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4));
11410 }
11411 }
11412
11413 return SDValue();
11414}
11415
Evan Chengf863e3f2011-07-13 00:42:17 +000011416/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
11417SDValue
11418ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
11419 SDValue Cmp = N->getOperand(4);
11420 if (Cmp.getOpcode() != ARMISD::CMPZ)
11421 // Only looking at EQ and NE cases.
11422 return SDValue();
11423
11424 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000011425 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000011426 SDValue LHS = Cmp.getOperand(0);
11427 SDValue RHS = Cmp.getOperand(1);
11428 SDValue FalseVal = N->getOperand(0);
11429 SDValue TrueVal = N->getOperand(1);
11430 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000011431 ARMCC::CondCodes CC =
11432 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000011433
James Molloy9d55f192015-11-10 14:22:05 +000011434 // BFI is only available on V6T2+.
11435 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
11436 SDValue R = PerformCMOVToBFICombine(N, DAG);
11437 if (R)
11438 return R;
11439 }
11440
Evan Chengf863e3f2011-07-13 00:42:17 +000011441 // Simplify
11442 // mov r1, r0
11443 // cmp r1, x
11444 // mov r0, y
11445 // moveq r0, x
11446 // to
11447 // cmp r0, x
11448 // movne r0, y
11449 //
11450 // mov r1, r0
11451 // cmp r1, x
11452 // mov r0, x
11453 // movne r0, y
11454 // to
11455 // cmp r0, x
11456 // movne r0, y
11457 /// FIXME: Turn this into a target neutral optimization?
11458 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000011459 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000011460 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
11461 N->getOperand(3), Cmp);
11462 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
11463 SDValue ARMcc;
11464 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
11465 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
11466 N->getOperand(3), NewCmp);
11467 }
11468
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011469 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0))
11470 // -> (cmov F T CC CPSR Cmp)
11471 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) {
11472 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0));
11473 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
11474 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
11475 if ((LHS0C && LHS0C->getZExtValue() == 0) &&
11476 (LHS1C && LHS1C->getZExtValue() == 1) &&
11477 (RHSC && RHSC->getZExtValue() == 0)) {
11478 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
11479 LHS->getOperand(2), LHS->getOperand(3),
11480 LHS->getOperand(4));
11481 }
11482 }
11483
Evan Chengf863e3f2011-07-13 00:42:17 +000011484 if (Res.getNode()) {
11485 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000011486 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000011487 // Capture demanded bits information that would be otherwise lost.
11488 if (KnownZero == 0xfffffffe)
11489 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11490 DAG.getValueType(MVT::i1));
11491 else if (KnownZero == 0xffffff00)
11492 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11493 DAG.getValueType(MVT::i8));
11494 else if (KnownZero == 0xffff0000)
11495 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11496 DAG.getValueType(MVT::i16));
11497 }
11498
11499 return Res;
11500}
11501
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011502SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000011503 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011504 switch (N->getOpcode()) {
11505 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000011506 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000011507 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011508 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000011509 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011510 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000011511 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
11512 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000011513 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000011514 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000011515 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000011516 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000011517 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000011518 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000011519 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000011520 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000011521 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000011522 case ISD::FP_TO_UINT:
11523 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
11524 case ISD::FDIV:
11525 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011526 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000011527 case ISD::SHL:
11528 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011529 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000011530 case ISD::SIGN_EXTEND:
11531 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011532 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000011533 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011534 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011535 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011536 case ARMISD::VLD2DUP:
11537 case ARMISD::VLD3DUP:
11538 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011539 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000011540 case ARMISD::BUILD_VECTOR:
11541 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011542 case ISD::INTRINSIC_VOID:
11543 case ISD::INTRINSIC_W_CHAIN:
11544 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
11545 case Intrinsic::arm_neon_vld1:
11546 case Intrinsic::arm_neon_vld2:
11547 case Intrinsic::arm_neon_vld3:
11548 case Intrinsic::arm_neon_vld4:
11549 case Intrinsic::arm_neon_vld2lane:
11550 case Intrinsic::arm_neon_vld3lane:
11551 case Intrinsic::arm_neon_vld4lane:
11552 case Intrinsic::arm_neon_vst1:
11553 case Intrinsic::arm_neon_vst2:
11554 case Intrinsic::arm_neon_vst3:
11555 case Intrinsic::arm_neon_vst4:
11556 case Intrinsic::arm_neon_vst2lane:
11557 case Intrinsic::arm_neon_vst3lane:
11558 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011559 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011560 default: break;
11561 }
11562 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011563 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011564 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011565}
11566
Evan Chengd42641c2011-02-02 01:06:55 +000011567bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
11568 EVT VT) const {
11569 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
11570}
11571
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011572bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11573 unsigned,
11574 unsigned,
11575 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011576 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000011577 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000011578
11579 switch (VT.getSimpleVT().SimpleTy) {
11580 default:
11581 return false;
11582 case MVT::i8:
11583 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000011584 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011585 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000011586 if (AllowsUnaligned) {
11587 if (Fast)
11588 *Fast = Subtarget->hasV7Ops();
11589 return true;
11590 }
11591 return false;
11592 }
Evan Chengeec6bc62012-08-15 17:44:53 +000011593 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000011594 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011595 // For any little-endian targets with neon, we can support unaligned ld/st
11596 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000011597 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000011598 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011599 if (Fast)
11600 *Fast = true;
11601 return true;
11602 }
11603 return false;
11604 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000011605 }
11606}
11607
Lang Hames9929c422011-11-02 22:52:45 +000011608static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
11609 unsigned AlignCheck) {
11610 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
11611 (DstAlign == 0 || DstAlign % AlignCheck == 0));
11612}
11613
11614EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
11615 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011616 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000011617 bool MemcpyStrSrc,
11618 MachineFunction &MF) const {
11619 const Function *F = MF.getFunction();
11620
11621 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000011622 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
11623 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011624 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000011625 if (Size >= 16 &&
11626 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011627 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011628 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000011629 } else if (Size >= 8 &&
11630 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011631 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
11632 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011633 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000011634 }
11635 }
11636
Lang Hamesb85fcd02011-11-08 18:56:23 +000011637 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000011638 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000011639 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000011640 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000011641 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000011642
Lang Hames9929c422011-11-02 22:52:45 +000011643 // Let the target-independent logic figure it out.
11644 return MVT::Other;
11645}
11646
Evan Cheng9ec512d2012-12-06 19:13:27 +000011647bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11648 if (Val.getOpcode() != ISD::LOAD)
11649 return false;
11650
11651 EVT VT1 = Val.getValueType();
11652 if (!VT1.isSimple() || !VT1.isInteger() ||
11653 !VT2.isSimple() || !VT2.isInteger())
11654 return false;
11655
11656 switch (VT1.getSimpleVT().SimpleTy) {
11657 default: break;
11658 case MVT::i1:
11659 case MVT::i8:
11660 case MVT::i16:
11661 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
11662 return true;
11663 }
11664
11665 return false;
11666}
11667
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000011668bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
11669 EVT VT = ExtVal.getValueType();
11670
11671 if (!isTypeLegal(VT))
11672 return false;
11673
11674 // Don't create a loadext if we can fold the extension into a wide/long
11675 // instruction.
11676 // If there's more than one user instruction, the loadext is desirable no
11677 // matter what. There can be two uses by the same instruction.
11678 if (ExtVal->use_empty() ||
11679 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
11680 return true;
11681
11682 SDNode *U = *ExtVal->use_begin();
11683 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
11684 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
11685 return false;
11686
11687 return true;
11688}
11689
Tim Northovercc2e9032013-08-06 13:58:03 +000011690bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
11691 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11692 return false;
11693
11694 if (!isTypeLegal(EVT::getEVT(Ty1)))
11695 return false;
11696
11697 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
11698
11699 // Assuming the caller doesn't have a zeroext or signext return parameter,
11700 // truncation all the way down to i1 is valid.
11701 return true;
11702}
11703
Javed Absar85874a92016-10-13 14:57:43 +000011704int ARMTargetLowering::getScalingFactorCost(const DataLayout &DL,
11705 const AddrMode &AM, Type *Ty,
11706 unsigned AS) const {
11707 if (isLegalAddressingMode(DL, AM, Ty, AS)) {
11708 if (Subtarget->hasFPAO())
11709 return AM.Scale < 0 ? 1 : 0; // positive offsets execute faster
11710 return 0;
11711 }
11712 return -1;
11713}
11714
Tim Northovercc2e9032013-08-06 13:58:03 +000011715
Evan Chengdc49a8d2009-08-14 20:09:37 +000011716static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
11717 if (V < 0)
11718 return false;
11719
11720 unsigned Scale = 1;
11721 switch (VT.getSimpleVT().SimpleTy) {
11722 default: return false;
11723 case MVT::i1:
11724 case MVT::i8:
11725 // Scale == 1;
11726 break;
11727 case MVT::i16:
11728 // Scale == 2;
11729 Scale = 2;
11730 break;
11731 case MVT::i32:
11732 // Scale == 4;
11733 Scale = 4;
11734 break;
11735 }
11736
11737 if ((V & (Scale - 1)) != 0)
11738 return false;
11739 V /= Scale;
11740 return V == (V & ((1LL << 5) - 1));
11741}
11742
11743static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
11744 const ARMSubtarget *Subtarget) {
11745 bool isNeg = false;
11746 if (V < 0) {
11747 isNeg = true;
11748 V = - V;
11749 }
11750
11751 switch (VT.getSimpleVT().SimpleTy) {
11752 default: return false;
11753 case MVT::i1:
11754 case MVT::i8:
11755 case MVT::i16:
11756 case MVT::i32:
11757 // + imm12 or - imm8
11758 if (isNeg)
11759 return V == (V & ((1LL << 8) - 1));
11760 return V == (V & ((1LL << 12) - 1));
11761 case MVT::f32:
11762 case MVT::f64:
11763 // Same as ARM mode. FIXME: NEON?
11764 if (!Subtarget->hasVFP2())
11765 return false;
11766 if ((V & 3) != 0)
11767 return false;
11768 V >>= 2;
11769 return V == (V & ((1LL << 8) - 1));
11770 }
11771}
11772
Evan Cheng2150b922007-03-12 23:30:29 +000011773/// isLegalAddressImmediate - Return true if the integer value can be used
11774/// as the offset of the target addressing mode for load / store of the
11775/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011776static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011777 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000011778 if (V == 0)
11779 return true;
11780
Evan Chengce5dfb62009-03-09 19:15:00 +000011781 if (!VT.isSimple())
11782 return false;
11783
Evan Chengdc49a8d2009-08-14 20:09:37 +000011784 if (Subtarget->isThumb1Only())
11785 return isLegalT1AddressImmediate(V, VT);
11786 else if (Subtarget->isThumb2())
11787 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000011788
Evan Chengdc49a8d2009-08-14 20:09:37 +000011789 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000011790 if (V < 0)
11791 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000011792 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000011793 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011794 case MVT::i1:
11795 case MVT::i8:
11796 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000011797 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011798 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011799 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000011800 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011801 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011802 case MVT::f32:
11803 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011804 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000011805 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000011806 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000011807 return false;
11808 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011809 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000011810 }
Evan Cheng10043e22007-01-19 07:51:42 +000011811}
11812
Evan Chengdc49a8d2009-08-14 20:09:37 +000011813bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
11814 EVT VT) const {
11815 int Scale = AM.Scale;
11816 if (Scale < 0)
11817 return false;
11818
11819 switch (VT.getSimpleVT().SimpleTy) {
11820 default: return false;
11821 case MVT::i1:
11822 case MVT::i8:
11823 case MVT::i16:
11824 case MVT::i32:
11825 if (Scale == 1)
11826 return true;
11827 // r + r << imm
11828 Scale = Scale & ~1;
11829 return Scale == 2 || Scale == 4 || Scale == 8;
11830 case MVT::i64:
11831 // r + r
11832 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11833 return true;
11834 return false;
11835 case MVT::isVoid:
11836 // Note, we allow "void" uses (basically, uses that aren't loads or
11837 // stores), because arm allows folding a scale into many arithmetic
11838 // operations. This should be made more precise and revisited later.
11839
11840 // Allow r << imm, but the imm has to be a multiple of two.
11841 if (Scale & 1) return false;
11842 return isPowerOf2_32(Scale);
11843 }
11844}
11845
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011846/// isLegalAddressingMode - Return true if the addressing mode represented
11847/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011848bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11849 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011850 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011851 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011852 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011853 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011854
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011855 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011856 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011857 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011858
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011859 switch (AM.Scale) {
11860 case 0: // no scale reg, must be "r+i" or "r", or "i".
11861 break;
11862 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011863 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011864 return false;
Justin Bognercd1d5aa2016-08-17 20:30:52 +000011865 LLVM_FALLTHROUGH;
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011866 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011867 // ARM doesn't support any R+R*scale+imm addr modes.
11868 if (AM.BaseOffs)
11869 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011870
Bob Wilson866c1742009-04-08 17:55:28 +000011871 if (!VT.isSimple())
11872 return false;
11873
Evan Chengdc49a8d2009-08-14 20:09:37 +000011874 if (Subtarget->isThumb2())
11875 return isLegalT2ScaledAddressingMode(AM, VT);
11876
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011877 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011878 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011879 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011880 case MVT::i1:
11881 case MVT::i8:
11882 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011883 if (Scale < 0) Scale = -Scale;
11884 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011885 return true;
11886 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011887 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011888 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011889 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011890 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011891 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011892 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011893 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011894
Owen Anderson9f944592009-08-11 20:47:22 +000011895 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011896 // Note, we allow "void" uses (basically, uses that aren't loads or
11897 // stores), because arm allows folding a scale into many arithmetic
11898 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011899
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011900 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011901 if (Scale & 1) return false;
11902 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011903 }
Evan Cheng2150b922007-03-12 23:30:29 +000011904 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011905 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011906}
11907
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011908/// isLegalICmpImmediate - Return true if the specified immediate is legal
11909/// icmp immediate, that is the target has icmp instructions which can compare
11910/// a register against the immediate without having to materialize the
11911/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011912bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011913 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011914 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011915 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011916 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011917 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011918 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011919 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011920}
11921
Andrew Tricka22cdb72012-07-18 18:34:27 +000011922/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11923/// *or sub* immediate, that is the target has add or sub instructions which can
11924/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011925/// immediate into a register.
11926bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011927 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011928 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011929 if (!Subtarget->isThumb())
11930 return ARM_AM::getSOImmVal(AbsImm) != -1;
11931 if (Subtarget->isThumb2())
11932 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11933 // Thumb1 only has 8-bit unsigned immediate.
11934 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011935}
11936
Owen Anderson53aa7a92009-08-10 22:56:29 +000011937static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011938 bool isSEXTLoad, SDValue &Base,
11939 SDValue &Offset, bool &isInc,
11940 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011941 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11942 return false;
11943
Owen Anderson9f944592009-08-11 20:47:22 +000011944 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011945 // AddressingMode 3
11946 Base = Ptr->getOperand(0);
11947 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011948 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011949 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011950 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011951 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011952 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011953 return true;
11954 }
11955 }
11956 isInc = (Ptr->getOpcode() == ISD::ADD);
11957 Offset = Ptr->getOperand(1);
11958 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011959 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011960 // AddressingMode 2
11961 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011962 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011963 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011964 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011965 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011966 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011967 Base = Ptr->getOperand(0);
11968 return true;
11969 }
11970 }
11971
11972 if (Ptr->getOpcode() == ISD::ADD) {
11973 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011974 ARM_AM::ShiftOpc ShOpcVal=
11975 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011976 if (ShOpcVal != ARM_AM::no_shift) {
11977 Base = Ptr->getOperand(1);
11978 Offset = Ptr->getOperand(0);
11979 } else {
11980 Base = Ptr->getOperand(0);
11981 Offset = Ptr->getOperand(1);
11982 }
11983 return true;
11984 }
11985
11986 isInc = (Ptr->getOpcode() == ISD::ADD);
11987 Base = Ptr->getOperand(0);
11988 Offset = Ptr->getOperand(1);
11989 return true;
11990 }
11991
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011992 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011993 return false;
11994}
11995
Owen Anderson53aa7a92009-08-10 22:56:29 +000011996static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011997 bool isSEXTLoad, SDValue &Base,
11998 SDValue &Offset, bool &isInc,
11999 SelectionDAG &DAG) {
12000 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
12001 return false;
12002
12003 Base = Ptr->getOperand(0);
12004 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
12005 int RHSC = (int)RHS->getZExtValue();
12006 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
12007 assert(Ptr->getOpcode() == ISD::ADD);
12008 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012009 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000012010 return true;
12011 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
12012 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012013 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000012014 return true;
12015 }
12016 }
12017
12018 return false;
12019}
12020
Evan Cheng10043e22007-01-19 07:51:42 +000012021/// getPreIndexedAddressParts - returns true by value, base pointer and
12022/// offset pointer and addressing mode by reference if the node's address
12023/// can be legally represented as pre-indexed load / store address.
12024bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000012025ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
12026 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000012027 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000012028 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000012029 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000012030 return false;
12031
Owen Anderson53aa7a92009-08-10 22:56:29 +000012032 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000012033 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000012034 bool isSEXTLoad = false;
12035 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
12036 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000012037 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000012038 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
12039 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
12040 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000012041 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000012042 } else
12043 return false;
12044
12045 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000012046 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000012047 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000012048 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
12049 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000012050 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000012051 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000012052 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000012053 if (!isLegal)
12054 return false;
12055
12056 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
12057 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000012058}
12059
12060/// getPostIndexedAddressParts - returns true by value, base pointer and
12061/// offset pointer and addressing mode by reference if this node can be
12062/// combined with a load / store to form a post-indexed load / store.
12063bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000012064 SDValue &Base,
12065 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000012066 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000012067 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000012068 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000012069 SDValue Ptr;
James Molloyb3326df2016-07-15 08:03:56 +000012070 bool isSEXTLoad = false, isNonExt;
Evan Cheng10043e22007-01-19 07:51:42 +000012071 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000012072 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000012073 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000012074 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
James Molloyb3326df2016-07-15 08:03:56 +000012075 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD;
Evan Cheng10043e22007-01-19 07:51:42 +000012076 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000012077 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000012078 Ptr = ST->getBasePtr();
James Molloyb3326df2016-07-15 08:03:56 +000012079 isNonExt = !ST->isTruncatingStore();
Evan Cheng10043e22007-01-19 07:51:42 +000012080 } else
12081 return false;
12082
James Molloyb3326df2016-07-15 08:03:56 +000012083 if (Subtarget->isThumb1Only()) {
12084 // Thumb-1 can do a limited post-inc load or store as an updating LDM. It
12085 // must be non-extending/truncating, i32, with an offset of 4.
12086 assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!");
12087 if (Op->getOpcode() != ISD::ADD || !isNonExt)
12088 return false;
12089 auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1));
12090 if (!RHS || RHS->getZExtValue() != 4)
12091 return false;
12092
12093 Offset = Op->getOperand(1);
12094 Base = Op->getOperand(0);
12095 AM = ISD::POST_INC;
12096 return true;
12097 }
12098
Evan Cheng10043e22007-01-19 07:51:42 +000012099 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000012100 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000012101 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000012102 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000012103 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000012104 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000012105 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
12106 isInc, DAG);
12107 if (!isLegal)
12108 return false;
12109
Evan Chengf19384d2010-05-18 21:31:17 +000012110 if (Ptr != Base) {
12111 // Swap base ptr and offset to catch more post-index load / store when
12112 // it's legal. In Thumb2 mode, offset must be an immediate.
12113 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
12114 !Subtarget->isThumb2())
12115 std::swap(Base, Offset);
12116
12117 // Post-indexed load / store update the base pointer.
12118 if (Ptr != Base)
12119 return false;
12120 }
12121
Evan Cheng84c6cda2009-07-02 07:28:31 +000012122 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
12123 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000012124}
12125
Jay Foada0653a32014-05-14 21:14:37 +000012126void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
12127 APInt &KnownZero,
12128 APInt &KnownOne,
12129 const SelectionDAG &DAG,
12130 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000012131 unsigned BitWidth = KnownOne.getBitWidth();
12132 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000012133 switch (Op.getOpcode()) {
12134 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000012135 case ARMISD::ADDC:
12136 case ARMISD::ADDE:
12137 case ARMISD::SUBC:
12138 case ARMISD::SUBE:
12139 // These nodes' second result is a boolean
12140 if (Op.getResNo() == 0)
12141 break;
12142 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
12143 break;
Evan Cheng10043e22007-01-19 07:51:42 +000012144 case ARMISD::CMOV: {
12145 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000012146 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000012147 if (KnownZero == 0 && KnownOne == 0) return;
12148
Dan Gohmanf990faf2008-02-13 00:35:47 +000012149 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000012150 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000012151 KnownZero &= KnownZeroRHS;
12152 KnownOne &= KnownOneRHS;
12153 return;
12154 }
Tim Northover01b4aa92014-04-03 15:10:35 +000012155 case ISD::INTRINSIC_W_CHAIN: {
12156 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
12157 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
12158 switch (IntID) {
12159 default: return;
12160 case Intrinsic::arm_ldaex:
12161 case Intrinsic::arm_ldrex: {
12162 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
Sanjay Patelbd6fca12016-09-14 15:21:00 +000012163 unsigned MemBits = VT.getScalarSizeInBits();
Tim Northover01b4aa92014-04-03 15:10:35 +000012164 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
12165 return;
12166 }
12167 }
12168 }
Evan Cheng10043e22007-01-19 07:51:42 +000012169 }
12170}
12171
12172//===----------------------------------------------------------------------===//
12173// ARM Inline Assembly Support
12174//===----------------------------------------------------------------------===//
12175
Evan Cheng078b0b02011-01-08 01:24:27 +000012176bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
12177 // Looking for "rev" which is V6+.
12178 if (!Subtarget->hasV6Ops())
12179 return false;
12180
12181 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
12182 std::string AsmStr = IA->getAsmString();
12183 SmallVector<StringRef, 4> AsmPieces;
12184 SplitString(AsmStr, AsmPieces, ";\n");
12185
12186 switch (AsmPieces.size()) {
12187 default: return false;
12188 case 1:
12189 AsmStr = AsmPieces[0];
12190 AsmPieces.clear();
12191 SplitString(AsmStr, AsmPieces, " \t,");
12192
12193 // rev $0, $1
12194 if (AsmPieces.size() == 3 &&
12195 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
12196 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000012197 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000012198 if (Ty && Ty->getBitWidth() == 32)
12199 return IntrinsicLowering::LowerToByteSwap(CI);
12200 }
12201 break;
12202 }
12203
12204 return false;
12205}
12206
Silviu Baranga82d04262016-04-25 14:29:18 +000012207const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const {
12208 // At this point, we have to lower this constraint to something else, so we
12209 // lower it to an "r" or "w". However, by doing this we will force the result
12210 // to be in register, while the X constraint is much more permissive.
12211 //
12212 // Although we are correct (we are free to emit anything, without
12213 // constraints), we might break use cases that would expect us to be more
12214 // efficient and emit something else.
12215 if (!Subtarget->hasVFP2())
12216 return "r";
12217 if (ConstraintVT.isFloatingPoint())
12218 return "w";
12219 if (ConstraintVT.isVector() && Subtarget->hasNEON() &&
12220 (ConstraintVT.getSizeInBits() == 64 ||
12221 ConstraintVT.getSizeInBits() == 128))
12222 return "w";
12223
12224 return "r";
12225}
12226
Evan Cheng10043e22007-01-19 07:51:42 +000012227/// getConstraintType - Given a constraint letter, return the type of
12228/// constraint it is for this target.
12229ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000012230ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000012231 if (Constraint.size() == 1) {
12232 switch (Constraint[0]) {
12233 default: break;
12234 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000012235 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000012236 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000012237 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000012238 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000012239 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000012240 // An address with a single base register. Due to the way we
12241 // currently handle addresses it is the same as an 'r' memory constraint.
12242 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000012243 }
Eric Christophere256cd02011-06-21 22:10:57 +000012244 } else if (Constraint.size() == 2) {
12245 switch (Constraint[0]) {
12246 default: break;
12247 // All 'U+' constraints are addresses.
12248 case 'U': return C_Memory;
12249 }
Evan Cheng10043e22007-01-19 07:51:42 +000012250 }
Chris Lattnerd6855142007-03-25 02:14:49 +000012251 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000012252}
12253
John Thompsone8360b72010-10-29 17:29:13 +000012254/// Examine constraint type and operand type and determine a weight value.
12255/// This object must already have been set up with the operand type
12256/// and the current alternative constraint selected.
12257TargetLowering::ConstraintWeight
12258ARMTargetLowering::getSingleConstraintMatchWeight(
12259 AsmOperandInfo &info, const char *constraint) const {
12260 ConstraintWeight weight = CW_Invalid;
12261 Value *CallOperandVal = info.CallOperandVal;
12262 // If we don't have a value, we can't do a match,
12263 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000012264 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000012265 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000012266 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000012267 // Look at the constraint type.
12268 switch (*constraint) {
12269 default:
12270 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12271 break;
12272 case 'l':
12273 if (type->isIntegerTy()) {
12274 if (Subtarget->isThumb())
12275 weight = CW_SpecificReg;
12276 else
12277 weight = CW_Register;
12278 }
12279 break;
12280 case 'w':
12281 if (type->isFloatingPointTy())
12282 weight = CW_Register;
12283 break;
12284 }
12285 return weight;
12286}
12287
Eric Christophercf2007c2011-06-30 23:50:52 +000012288typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000012289RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
12290 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000012291 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000012292 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000012293 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000012294 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000012295 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000012296 return RCPair(0U, &ARM::tGPRRegClass);
12297 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000012298 case 'h': // High regs or no regs.
12299 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000012300 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000012301 break;
Chris Lattner6223e832007-04-02 17:24:08 +000012302 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000012303 if (Subtarget->isThumb1Only())
12304 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000012305 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000012306 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000012307 if (VT == MVT::Other)
12308 break;
Owen Anderson9f944592009-08-11 20:47:22 +000012309 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000012310 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000012311 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000012312 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000012313 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000012314 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000012315 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000012316 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000012317 if (VT == MVT::Other)
12318 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000012319 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000012320 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000012321 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000012322 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000012323 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000012324 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000012325 break;
Eric Christopherc011d312011-07-01 00:30:46 +000012326 case 't':
12327 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000012328 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000012329 break;
Evan Cheng10043e22007-01-19 07:51:42 +000012330 }
12331 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000012332 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000012333 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000012334
Eric Christopher11e4df72015-02-26 22:38:43 +000012335 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000012336}
12337
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012338/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12339/// vector. If it is invalid, don't add anything to Ops.
12340void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000012341 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012342 std::vector<SDValue>&Ops,
12343 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000012344 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012345
Eric Christopherde9399b2011-06-02 23:16:42 +000012346 // Currently only support length 1 constraints.
12347 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000012348
Eric Christopherde9399b2011-06-02 23:16:42 +000012349 char ConstraintLetter = Constraint[0];
12350 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012351 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000012352 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012353 case 'I': case 'J': case 'K': case 'L':
12354 case 'M': case 'N': case 'O':
12355 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
12356 if (!C)
12357 return;
12358
12359 int64_t CVal64 = C->getSExtValue();
12360 int CVal = (int) CVal64;
12361 // None of these constraints allow values larger than 32 bits. Check
12362 // that the value fits in an int.
12363 if (CVal != CVal64)
12364 return;
12365
Eric Christopherde9399b2011-06-02 23:16:42 +000012366 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000012367 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000012368 // Constant suitable for movw, must be between 0 and
12369 // 65535.
12370 if (Subtarget->hasV6T2Ops())
12371 if (CVal >= 0 && CVal <= 65535)
12372 break;
12373 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012374 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000012375 if (Subtarget->isThumb1Only()) {
12376 // This must be a constant between 0 and 255, for ADD
12377 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012378 if (CVal >= 0 && CVal <= 255)
12379 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000012380 } else if (Subtarget->isThumb2()) {
12381 // A constant that can be used as an immediate value in a
12382 // data-processing instruction.
12383 if (ARM_AM::getT2SOImmVal(CVal) != -1)
12384 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012385 } else {
12386 // A constant that can be used as an immediate value in a
12387 // data-processing instruction.
12388 if (ARM_AM::getSOImmVal(CVal) != -1)
12389 break;
12390 }
12391 return;
12392
12393 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000012394 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012395 // This must be a constant between -255 and -1, for negated ADD
12396 // immediates. This can be used in GCC with an "n" modifier that
12397 // prints the negated value, for use with SUB instructions. It is
12398 // not useful otherwise but is implemented for compatibility.
12399 if (CVal >= -255 && CVal <= -1)
12400 break;
12401 } else {
12402 // This must be a constant between -4095 and 4095. It is not clear
12403 // what this constraint is intended for. Implemented for
12404 // compatibility with GCC.
12405 if (CVal >= -4095 && CVal <= 4095)
12406 break;
12407 }
12408 return;
12409
12410 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000012411 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012412 // A 32-bit value where only one byte has a nonzero value. Exclude
12413 // zero to match GCC. This constraint is used by GCC internally for
12414 // constants that can be loaded with a move/shift combination.
12415 // It is not useful otherwise but is implemented for compatibility.
12416 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
12417 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000012418 } else if (Subtarget->isThumb2()) {
12419 // A constant whose bitwise inverse can be used as an immediate
12420 // value in a data-processing instruction. This can be used in GCC
12421 // with a "B" modifier that prints the inverted value, for use with
12422 // BIC and MVN instructions. It is not useful otherwise but is
12423 // implemented for compatibility.
12424 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
12425 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012426 } else {
12427 // A constant whose bitwise inverse can be used as an immediate
12428 // value in a data-processing instruction. This can be used in GCC
12429 // with a "B" modifier that prints the inverted value, for use with
12430 // BIC and MVN instructions. It is not useful otherwise but is
12431 // implemented for compatibility.
12432 if (ARM_AM::getSOImmVal(~CVal) != -1)
12433 break;
12434 }
12435 return;
12436
12437 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000012438 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012439 // This must be a constant between -7 and 7,
12440 // for 3-operand ADD/SUB immediate instructions.
12441 if (CVal >= -7 && CVal < 7)
12442 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000012443 } else if (Subtarget->isThumb2()) {
12444 // A constant whose negation can be used as an immediate value in a
12445 // data-processing instruction. This can be used in GCC with an "n"
12446 // modifier that prints the negated value, for use with SUB
12447 // instructions. It is not useful otherwise but is implemented for
12448 // compatibility.
12449 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
12450 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012451 } else {
12452 // A constant whose negation can be used as an immediate value in a
12453 // data-processing instruction. This can be used in GCC with an "n"
12454 // modifier that prints the negated value, for use with SUB
12455 // instructions. It is not useful otherwise but is implemented for
12456 // compatibility.
12457 if (ARM_AM::getSOImmVal(-CVal) != -1)
12458 break;
12459 }
12460 return;
12461
12462 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000012463 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012464 // This must be a multiple of 4 between 0 and 1020, for
12465 // ADD sp + immediate.
12466 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
12467 break;
12468 } else {
12469 // A power of two or a constant between 0 and 32. This is used in
12470 // GCC for the shift amount on shifted register operands, but it is
12471 // useful in general for any shift amounts.
12472 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
12473 break;
12474 }
12475 return;
12476
12477 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000012478 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012479 // This must be a constant between 0 and 31, for shift amounts.
12480 if (CVal >= 0 && CVal <= 31)
12481 break;
12482 }
12483 return;
12484
12485 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000012486 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012487 // This must be a multiple of 4 between -508 and 508, for
12488 // ADD/SUB sp = sp + immediate.
12489 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
12490 break;
12491 }
12492 return;
12493 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012494 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012495 break;
12496 }
12497
12498 if (Result.getNode()) {
12499 Ops.push_back(Result);
12500 return;
12501 }
Dale Johannesence97d552010-06-25 21:55:36 +000012502 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012503}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000012504
Scott Douglassd2974a62015-08-24 09:17:11 +000012505static RTLIB::Libcall getDivRemLibcall(
12506 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000012507 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
12508 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000012509 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000012510 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
12511 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000012512 RTLIB::Libcall LC;
12513 switch (SVT) {
12514 default: llvm_unreachable("Unexpected request for libcall!");
12515 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
12516 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
12517 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
12518 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
12519 }
12520 return LC;
12521}
12522
12523static TargetLowering::ArgListTy getDivRemArgList(
Martin Storsjo04864f42016-10-07 13:28:53 +000012524 const SDNode *N, LLVMContext *Context, const ARMSubtarget *Subtarget) {
Scott Douglassbdef6042015-08-24 09:17:18 +000012525 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
12526 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000012527 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000012528 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
12529 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000012530 TargetLowering::ArgListTy Args;
12531 TargetLowering::ArgListEntry Entry;
12532 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
12533 EVT ArgVT = N->getOperand(i).getValueType();
12534 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
12535 Entry.Node = N->getOperand(i);
12536 Entry.Ty = ArgTy;
12537 Entry.isSExt = isSigned;
12538 Entry.isZExt = !isSigned;
12539 Args.push_back(Entry);
12540 }
Martin Storsjo04864f42016-10-07 13:28:53 +000012541 if (Subtarget->isTargetWindows() && Args.size() >= 2)
12542 std::swap(Args[0], Args[1]);
Scott Douglassd2974a62015-08-24 09:17:11 +000012543 return Args;
12544}
12545
Renato Golin87610692013-07-16 09:32:17 +000012546SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000012547 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
Martin Storsjo04864f42016-10-07 13:28:53 +000012548 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
12549 Subtarget->isTargetWindows()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000012550 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000012551 unsigned Opcode = Op->getOpcode();
12552 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000012553 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000012554 bool isSigned = (Opcode == ISD::SDIVREM);
12555 EVT VT = Op->getValueType(0);
12556 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
Sjoerd Meijer4dbe73c2016-10-03 10:12:32 +000012557 SDLoc dl(Op);
12558
12559 // If the target has hardware divide, use divide + multiply + subtract:
12560 // div = a / b
12561 // rem = a - b * div
12562 // return {div, rem}
12563 // This should be lowered into UDIV/SDIV + MLS later on.
12564 if (Subtarget->hasDivide() && Op->getValueType(0).isSimple() &&
12565 Op->getSimpleValueType(0) == MVT::i32) {
12566 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV;
12567 const SDValue Dividend = Op->getOperand(0);
12568 const SDValue Divisor = Op->getOperand(1);
12569 SDValue Div = DAG.getNode(DivOpcode, dl, VT, Dividend, Divisor);
12570 SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Div, Divisor);
12571 SDValue Rem = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul);
12572
12573 SDValue Values[2] = {Div, Rem};
12574 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VT, VT), Values);
12575 }
Renato Golin87610692013-07-16 09:32:17 +000012576
Scott Douglassd2974a62015-08-24 09:17:11 +000012577 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
12578 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000012579 SDValue InChain = DAG.getEntryNode();
12580
Scott Douglassd2974a62015-08-24 09:17:11 +000012581 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
Martin Storsjo04864f42016-10-07 13:28:53 +000012582 DAG.getContext(),
12583 Subtarget);
Renato Golin87610692013-07-16 09:32:17 +000012584
12585 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000012586 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000012587
Reid Kleckner343c3952014-11-20 23:51:47 +000012588 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000012589
Martin Storsjo04864f42016-10-07 13:28:53 +000012590 if (Subtarget->isTargetWindows())
12591 InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain);
12592
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012593 TargetLowering::CallLoweringInfo CLI(DAG);
12594 CLI.setDebugLoc(dl).setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +000012595 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012596 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000012597
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012598 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000012599 return CallInfo.first;
12600}
12601
Scott Douglassbdef6042015-08-24 09:17:18 +000012602// Lowers REM using divmod helpers
12603// see RTABI section 4.2/4.3
12604SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
12605 // Build return types (div and rem)
12606 std::vector<Type*> RetTyParams;
12607 Type *RetTyElement;
12608
12609 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
12610 default: llvm_unreachable("Unexpected request for libcall!");
12611 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
12612 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
12613 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
12614 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
12615 }
12616
12617 RetTyParams.push_back(RetTyElement);
12618 RetTyParams.push_back(RetTyElement);
12619 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
12620 Type *RetTy = StructType::get(*DAG.getContext(), ret);
12621
12622 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
12623 SimpleTy);
12624 SDValue InChain = DAG.getEntryNode();
Martin Storsjo04864f42016-10-07 13:28:53 +000012625 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext(),
12626 Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +000012627 bool isSigned = N->getOpcode() == ISD::SREM;
12628 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
12629 getPointerTy(DAG.getDataLayout()));
12630
Martin Storsjo04864f42016-10-07 13:28:53 +000012631 if (Subtarget->isTargetWindows())
12632 InChain = WinDBZCheckDenominator(DAG, N, InChain);
12633
Scott Douglassbdef6042015-08-24 09:17:18 +000012634 // Lower call
12635 CallLoweringInfo CLI(DAG);
12636 CLI.setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +000012637 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args))
Scott Douglassbdef6042015-08-24 09:17:18 +000012638 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
12639 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
12640
12641 // Return second (rem) result operand (first contains div)
12642 SDNode *ResNode = CallResult.first.getNode();
12643 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
12644 return ResNode->getOperand(1);
12645}
12646
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012647SDValue
12648ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
12649 assert(Subtarget->isTargetWindows() && "unsupported target platform");
12650 SDLoc DL(Op);
12651
12652 // Get the inputs.
12653 SDValue Chain = Op.getOperand(0);
12654 SDValue Size = Op.getOperand(1);
12655
12656 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012657 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012658
12659 SDValue Flag;
12660 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
12661 Flag = Chain.getValue(1);
12662
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000012663 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012664 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
12665
12666 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
12667 Chain = NewSP.getValue(1);
12668
12669 SDValue Ops[2] = { NewSP, Chain };
12670 return DAG.getMergeValues(Ops, DL);
12671}
12672
Oliver Stannard51b1d462014-08-21 12:50:31 +000012673SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
12674 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
12675 "Unexpected type for custom-lowering FP_EXTEND");
12676
12677 RTLIB::Libcall LC;
12678 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
12679
12680 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000012681 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
12682 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012683}
12684
12685SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
12686 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
12687 Subtarget->isFPOnlySP() &&
12688 "Unexpected type for custom-lowering FP_ROUND");
12689
12690 RTLIB::Libcall LC;
12691 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
12692
12693 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000012694 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
12695 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012696}
12697
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000012698bool
12699ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
12700 // The ARM target isn't yet aware of offsets.
12701 return false;
12702}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012703
Jim Grosbach11013ed2010-07-16 23:05:05 +000012704bool ARM::isBitFieldInvertedMask(unsigned v) {
12705 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000012706 return false;
12707
Jim Grosbach11013ed2010-07-16 23:05:05 +000012708 // there can be 1's on either or both "outsides", all the "inside"
12709 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000012710 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000012711}
12712
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012713/// isFPImmLegal - Returns true if the target can instruction select the
12714/// specified FP immediate natively. If false, the legalizer will
12715/// materialize the FP immediate as a load from a constant pool.
12716bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
12717 if (!Subtarget->hasVFP3())
12718 return false;
12719 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000012720 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012721 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000012722 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012723 return false;
12724}
Bob Wilson5549d492010-09-21 17:56:22 +000012725
Wesley Peck527da1b2010-11-23 03:31:01 +000012726/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000012727/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
12728/// specified in the intrinsic calls.
12729bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
12730 const CallInst &I,
12731 unsigned Intrinsic) const {
12732 switch (Intrinsic) {
12733 case Intrinsic::arm_neon_vld1:
12734 case Intrinsic::arm_neon_vld2:
12735 case Intrinsic::arm_neon_vld3:
12736 case Intrinsic::arm_neon_vld4:
12737 case Intrinsic::arm_neon_vld2lane:
12738 case Intrinsic::arm_neon_vld3lane:
12739 case Intrinsic::arm_neon_vld4lane: {
12740 Info.opc = ISD::INTRINSIC_W_CHAIN;
12741 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012742 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012743 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012744 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12745 Info.ptrVal = I.getArgOperand(0);
12746 Info.offset = 0;
12747 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12748 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12749 Info.vol = false; // volatile loads with NEON intrinsics not supported
12750 Info.readMem = true;
12751 Info.writeMem = false;
12752 return true;
12753 }
12754 case Intrinsic::arm_neon_vst1:
12755 case Intrinsic::arm_neon_vst2:
12756 case Intrinsic::arm_neon_vst3:
12757 case Intrinsic::arm_neon_vst4:
12758 case Intrinsic::arm_neon_vst2lane:
12759 case Intrinsic::arm_neon_vst3lane:
12760 case Intrinsic::arm_neon_vst4lane: {
12761 Info.opc = ISD::INTRINSIC_VOID;
12762 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012763 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000012764 unsigned NumElts = 0;
12765 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000012766 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000012767 if (!ArgTy->isVectorTy())
12768 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012769 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012770 }
12771 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12772 Info.ptrVal = I.getArgOperand(0);
12773 Info.offset = 0;
12774 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12775 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12776 Info.vol = false; // volatile stores with NEON intrinsics not supported
12777 Info.readMem = false;
12778 Info.writeMem = true;
12779 return true;
12780 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012781 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012782 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012783 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012784 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
12785 Info.opc = ISD::INTRINSIC_W_CHAIN;
12786 Info.memVT = MVT::getVT(PtrTy->getElementType());
12787 Info.ptrVal = I.getArgOperand(0);
12788 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012789 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012790 Info.vol = true;
12791 Info.readMem = true;
12792 Info.writeMem = false;
12793 return true;
12794 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012795 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012796 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012797 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012798 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
12799 Info.opc = ISD::INTRINSIC_W_CHAIN;
12800 Info.memVT = MVT::getVT(PtrTy->getElementType());
12801 Info.ptrVal = I.getArgOperand(1);
12802 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012803 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012804 Info.vol = true;
12805 Info.readMem = false;
12806 Info.writeMem = true;
12807 return true;
12808 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012809 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012810 case Intrinsic::arm_strexd: {
12811 Info.opc = ISD::INTRINSIC_W_CHAIN;
12812 Info.memVT = MVT::i64;
12813 Info.ptrVal = I.getArgOperand(2);
12814 Info.offset = 0;
12815 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012816 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012817 Info.readMem = false;
12818 Info.writeMem = true;
12819 return true;
12820 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012821 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012822 case Intrinsic::arm_ldrexd: {
12823 Info.opc = ISD::INTRINSIC_W_CHAIN;
12824 Info.memVT = MVT::i64;
12825 Info.ptrVal = I.getArgOperand(0);
12826 Info.offset = 0;
12827 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012828 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012829 Info.readMem = true;
12830 Info.writeMem = false;
12831 return true;
12832 }
Bob Wilson5549d492010-09-21 17:56:22 +000012833 default:
12834 break;
12835 }
12836
12837 return false;
12838}
Juergen Ributzka659ce002014-01-28 01:20:14 +000012839
12840/// \brief Returns true if it is beneficial to convert a load of a constant
12841/// to just the constant itself.
12842bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
12843 Type *Ty) const {
12844 assert(Ty->isIntegerTy());
12845
12846 unsigned Bits = Ty->getPrimitiveSizeInBits();
12847 if (Bits == 0 || Bits > 32)
12848 return false;
12849 return true;
12850}
Tim Northover037f26f22014-04-17 18:22:47 +000012851
Robin Morisset5349e8e2014-09-18 18:56:04 +000012852Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
12853 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012854 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000012855
12856 // First, if the target has no DMB, see what fallback we can use.
12857 if (!Subtarget->hasDataBarrier()) {
12858 // Some ARMv6 cpus can support data barriers with an mcr instruction.
12859 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
12860 // here.
12861 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
12862 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
12863 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
12864 Builder.getInt32(0), Builder.getInt32(7),
12865 Builder.getInt32(10), Builder.getInt32(5)};
12866 return Builder.CreateCall(MCR, args);
12867 } else {
12868 // Instead of using barriers, atomic accesses on these subtargets use
12869 // libcalls.
12870 llvm_unreachable("makeDMB on a target so old that it has no barriers");
12871 }
12872 } else {
12873 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
12874 // Only a full system barrier exists in the M-class architectures.
12875 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
12876 Constant *CDomain = Builder.getInt32(Domain);
12877 return Builder.CreateCall(DMB, CDomain);
12878 }
Robin Morisseta47cb412014-09-03 21:01:03 +000012879}
12880
12881// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000012882Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012883 AtomicOrdering Ord, bool IsStore,
12884 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012885 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012886 case AtomicOrdering::NotAtomic:
12887 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012888 llvm_unreachable("Invalid fence: unordered/non-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012889 case AtomicOrdering::Monotonic:
12890 case AtomicOrdering::Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012891 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012892 case AtomicOrdering::SequentiallyConsistent:
Robin Morisseta47cb412014-09-03 21:01:03 +000012893 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012894 return nullptr; // Nothing to do
12895 /*FALLTHROUGH*/
JF Bastien800f87a2016-04-06 21:19:33 +000012896 case AtomicOrdering::Release:
12897 case AtomicOrdering::AcquireRelease:
Diana Picusc5baa432016-06-23 07:47:35 +000012898 if (Subtarget->preferISHSTBarriers())
Robin Morissetdedef332014-09-23 20:31:14 +000012899 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012900 // FIXME: add a comment with a link to documentation justifying this.
12901 else
Robin Morissetdedef332014-09-23 20:31:14 +000012902 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012903 }
Robin Morissetdedef332014-09-23 20:31:14 +000012904 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012905}
12906
Robin Morissetdedef332014-09-23 20:31:14 +000012907Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012908 AtomicOrdering Ord, bool IsStore,
12909 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012910 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012911 case AtomicOrdering::NotAtomic:
12912 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012913 llvm_unreachable("Invalid fence: unordered/not-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012914 case AtomicOrdering::Monotonic:
12915 case AtomicOrdering::Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012916 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012917 case AtomicOrdering::Acquire:
12918 case AtomicOrdering::AcquireRelease:
12919 case AtomicOrdering::SequentiallyConsistent:
Robin Morissetdedef332014-09-23 20:31:14 +000012920 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012921 }
Robin Morissetdedef332014-09-23 20:31:14 +000012922 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012923}
12924
Robin Morisseted3d48f2014-09-03 21:29:59 +000012925// Loads and stores less than 64-bits are already atomic; ones above that
12926// are doomed anyway, so defer to the default libcall and blame the OS when
12927// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12928// anything for those.
12929bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12930 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12931 return (Size == 64) && !Subtarget->isMClass();
12932}
Tim Northover037f26f22014-04-17 18:22:47 +000012933
Robin Morisseted3d48f2014-09-03 21:29:59 +000012934// Loads and stores less than 64-bits are already atomic; ones above that
12935// are doomed anyway, so defer to the default libcall and blame the OS when
12936// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12937// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012938// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12939// guarantee, see DDI0406C ARM architecture reference manual,
12940// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012941TargetLowering::AtomicExpansionKind
12942ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012943 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012944 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012945 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012946}
12947
12948// For the real atomic operations, we have ldrex/strex up to 32 bits,
12949// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012950TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012951ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012952 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
Weiming Zhao962eaae2016-11-03 21:49:08 +000012953 bool hasAtomicRMW = !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps();
12954 return (Size <= (Subtarget->isMClass() ? 32U : 64U) && hasAtomicRMW)
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012955 ? AtomicExpansionKind::LLSC
12956 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012957}
12958
Ahmed Bougacha52468672015-09-11 17:08:28 +000012959bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12960 AtomicCmpXchgInst *AI) const {
Tim Northoverb629c772016-04-18 21:48:55 +000012961 // At -O0, fast-regalloc cannot cope with the live vregs necessary to
12962 // implement cmpxchg without spilling. If the address being exchanged is also
12963 // on the stack and close enough to the spill slot, this can lead to a
12964 // situation where the monitor always gets cleared and the atomic operation
12965 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
Weiming Zhao962eaae2016-11-03 21:49:08 +000012966 bool hasAtomicCmpXchg =
12967 !Subtarget->isThumb() || Subtarget->hasV8MBaselineOps();
12968 return getTargetMachine().getOptLevel() != 0 && hasAtomicCmpXchg;
Ahmed Bougacha52468672015-09-11 17:08:28 +000012969}
12970
James Y Knightf44fc522016-03-16 22:12:04 +000012971bool ARMTargetLowering::shouldInsertFencesForAtomic(
12972 const Instruction *I) const {
12973 return InsertFencesForAtomic;
12974}
12975
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012976// This has so far only been implemented for MachO.
12977bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012978 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012979}
12980
Quentin Colombetc32615d2014-10-31 17:52:53 +000012981bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12982 unsigned &Cost) const {
12983 // If we do not have NEON, vector types are not natively supported.
12984 if (!Subtarget->hasNEON())
12985 return false;
12986
12987 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012988 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012989 // better to leave at float as we have more freedom in the addressing mode for
12990 // those.
12991 if (VectorTy->isFPOrFPVectorTy())
12992 return false;
12993
12994 // If the index is unknown at compile time, this is very expensive to lower
12995 // and it is not possible to combine the store with the extract.
12996 if (!isa<ConstantInt>(Idx))
12997 return false;
12998
12999 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
13000 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
13001 // We can do a store + vector extract on any vector that fits perfectly in a D
13002 // or Q register.
13003 if (BitWidth == 64 || BitWidth == 128) {
13004 Cost = 0;
13005 return true;
13006 }
13007 return false;
13008}
13009
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000013010bool ARMTargetLowering::isCheapToSpeculateCttz() const {
13011 return Subtarget->hasV6T2Ops();
13012}
13013
13014bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
13015 return Subtarget->hasV6T2Ops();
13016}
13017
Tim Northover037f26f22014-04-17 18:22:47 +000013018Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
13019 AtomicOrdering Ord) const {
13020 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
13021 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
JF Bastien800f87a2016-04-06 21:19:33 +000013022 bool IsAcquire = isAcquireOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000013023
13024 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
13025 // intrinsic must return {i32, i32} and we have to recombine them into a
13026 // single i64 here.
13027 if (ValTy->getPrimitiveSizeInBits() == 64) {
13028 Intrinsic::ID Int =
13029 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
13030 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
13031
13032 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
13033 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
13034
13035 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
13036 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000013037 if (!Subtarget->isLittle())
13038 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000013039 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
13040 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
13041 return Builder.CreateOr(
13042 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
13043 }
13044
13045 Type *Tys[] = { Addr->getType() };
13046 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
13047 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
13048
13049 return Builder.CreateTruncOrBitCast(
13050 Builder.CreateCall(Ldrex, Addr),
13051 cast<PointerType>(Addr->getType())->getElementType());
13052}
13053
Ahmed Bougacha81616a72015-09-22 17:22:58 +000013054void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
13055 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000013056 if (!Subtarget->hasV7Ops())
13057 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000013058 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
13059 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
13060}
13061
Tim Northover037f26f22014-04-17 18:22:47 +000013062Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
13063 Value *Addr,
13064 AtomicOrdering Ord) const {
13065 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
JF Bastien800f87a2016-04-06 21:19:33 +000013066 bool IsRelease = isReleaseOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000013067
13068 // Since the intrinsics must have legal type, the i64 intrinsics take two
13069 // parameters: "i32, i32". We must marshal Val into the appropriate form
13070 // before the call.
13071 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
13072 Intrinsic::ID Int =
13073 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
13074 Function *Strex = Intrinsic::getDeclaration(M, Int);
13075 Type *Int32Ty = Type::getInt32Ty(M->getContext());
13076
13077 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
13078 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000013079 if (!Subtarget->isLittle())
13080 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000013081 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000013082 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000013083 }
13084
13085 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
13086 Type *Tys[] = { Addr->getType() };
13087 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
13088
David Blaikieff6409d2015-05-18 22:13:54 +000013089 return Builder.CreateCall(
13090 Strex, {Builder.CreateZExtOrBitCast(
13091 Val, Strex->getFunctionType()->getParamType(0)),
13092 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000013093}
Oliver Stannardc24f2172014-05-09 14:01:47 +000013094
Hao Liu2cd34bb2015-06-26 02:45:36 +000013095/// \brief Lower an interleaved load into a vldN intrinsic.
13096///
13097/// E.g. Lower an interleaved load (Factor = 2):
13098/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
13099/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
13100/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
13101///
13102/// Into:
13103/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
13104/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
13105/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
13106bool ARMTargetLowering::lowerInterleavedLoad(
13107 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
13108 ArrayRef<unsigned> Indices, unsigned Factor) const {
13109 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
13110 "Invalid interleave factor");
13111 assert(!Shuffles.empty() && "Empty shufflevector input");
13112 assert(Shuffles.size() == Indices.size() &&
13113 "Unmatched number of shufflevectors and indices");
13114
13115 VectorType *VecTy = Shuffles[0]->getType();
13116 Type *EltTy = VecTy->getVectorElementType();
13117
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013118 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000013119 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
13120 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000013121
Jeroen Ketemaaebca092015-10-07 14:53:29 +000013122 // Skip if we do not have NEON and skip illegal vector types and vector types
13123 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
13124 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000013125 return false;
13126
13127 // A pointer vector can not be the return type of the ldN intrinsics. Need to
13128 // load integer vectors first and then convert to pointer vectors.
13129 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013130 VecTy =
13131 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000013132
13133 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
13134 Intrinsic::arm_neon_vld3,
13135 Intrinsic::arm_neon_vld4};
13136
Hao Liu2cd34bb2015-06-26 02:45:36 +000013137 IRBuilder<> Builder(LI);
13138 SmallVector<Value *, 2> Ops;
13139
13140 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
13141 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
13142 Ops.push_back(Builder.getInt32(LI->getAlignment()));
13143
Jeroen Ketemaab99b592015-09-30 10:56:37 +000013144 Type *Tys[] = { VecTy, Int8Ptr };
13145 Function *VldnFunc =
13146 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000013147 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
13148
13149 // Replace uses of each shufflevector with the corresponding vector loaded
13150 // by ldN.
13151 for (unsigned i = 0; i < Shuffles.size(); i++) {
13152 ShuffleVectorInst *SV = Shuffles[i];
13153 unsigned Index = Indices[i];
13154
13155 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
13156
13157 // Convert the integer vector to pointer vector if the element is pointer.
13158 if (EltTy->isPointerTy())
13159 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
13160
13161 SV->replaceAllUsesWith(SubVec);
13162 }
13163
13164 return true;
13165}
13166
13167/// \brief Get a mask consisting of sequential integers starting from \p Start.
13168///
13169/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
13170static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
13171 unsigned NumElts) {
13172 SmallVector<Constant *, 16> Mask;
13173 for (unsigned i = 0; i < NumElts; i++)
13174 Mask.push_back(Builder.getInt32(Start + i));
13175
13176 return ConstantVector::get(Mask);
13177}
13178
13179/// \brief Lower an interleaved store into a vstN intrinsic.
13180///
13181/// E.g. Lower an interleaved store (Factor = 3):
13182/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
13183/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
13184/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
13185///
13186/// Into:
13187/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
13188/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
13189/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
13190/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
13191///
13192/// Note that the new shufflevectors will be removed and we'll only generate one
13193/// vst3 instruction in CodeGen.
13194bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
13195 ShuffleVectorInst *SVI,
13196 unsigned Factor) const {
13197 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
13198 "Invalid interleave factor");
13199
13200 VectorType *VecTy = SVI->getType();
13201 assert(VecTy->getVectorNumElements() % Factor == 0 &&
13202 "Invalid interleaved store");
13203
13204 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
13205 Type *EltTy = VecTy->getVectorElementType();
13206 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
13207
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013208 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000013209 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
13210 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000013211
Jeroen Ketemaaebca092015-10-07 14:53:29 +000013212 // Skip if we do not have NEON and skip illegal vector types and vector types
13213 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
13214 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
13215 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000013216 return false;
13217
13218 Value *Op0 = SVI->getOperand(0);
13219 Value *Op1 = SVI->getOperand(1);
13220 IRBuilder<> Builder(SI);
13221
13222 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
13223 // vectors to integer vectors.
13224 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013225 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000013226
13227 // Convert to the corresponding integer vector.
13228 Type *IntVecTy =
13229 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
13230 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
13231 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
13232
13233 SubVecTy = VectorType::get(IntTy, NumSubElts);
13234 }
13235
Craig Topper26260942015-10-18 05:15:34 +000013236 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
13237 Intrinsic::arm_neon_vst3,
13238 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000013239 SmallVector<Value *, 6> Ops;
13240
13241 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
13242 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
13243
Jeroen Ketemaab99b592015-09-30 10:56:37 +000013244 Type *Tys[] = { Int8Ptr, SubVecTy };
13245 Function *VstNFunc = Intrinsic::getDeclaration(
13246 SI->getModule(), StoreInts[Factor - 2], Tys);
13247
Hao Liu2cd34bb2015-06-26 02:45:36 +000013248 // Split the shufflevector operands into sub vectors for the new vstN call.
13249 for (unsigned i = 0; i < Factor; i++)
13250 Ops.push_back(Builder.CreateShuffleVector(
13251 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
13252
13253 Ops.push_back(Builder.getInt32(SI->getAlignment()));
13254 Builder.CreateCall(VstNFunc, Ops);
13255 return true;
13256}
13257
Oliver Stannardc24f2172014-05-09 14:01:47 +000013258enum HABaseType {
13259 HA_UNKNOWN = 0,
13260 HA_FLOAT,
13261 HA_DOUBLE,
13262 HA_VECT64,
13263 HA_VECT128
13264};
13265
13266static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
13267 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000013268 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000013269 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
13270 uint64_t SubMembers = 0;
13271 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
13272 return false;
13273 Members += SubMembers;
13274 }
Craig Toppere3dcce92015-08-01 22:20:21 +000013275 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000013276 uint64_t SubMembers = 0;
13277 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
13278 return false;
13279 Members += SubMembers * AT->getNumElements();
13280 } else if (Ty->isFloatTy()) {
13281 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
13282 return false;
13283 Members = 1;
13284 Base = HA_FLOAT;
13285 } else if (Ty->isDoubleTy()) {
13286 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
13287 return false;
13288 Members = 1;
13289 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000013290 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000013291 Members = 1;
13292 switch (Base) {
13293 case HA_FLOAT:
13294 case HA_DOUBLE:
13295 return false;
13296 case HA_VECT64:
13297 return VT->getBitWidth() == 64;
13298 case HA_VECT128:
13299 return VT->getBitWidth() == 128;
13300 case HA_UNKNOWN:
13301 switch (VT->getBitWidth()) {
13302 case 64:
13303 Base = HA_VECT64;
13304 return true;
13305 case 128:
13306 Base = HA_VECT128;
13307 return true;
13308 default:
13309 return false;
13310 }
13311 }
13312 }
13313
13314 return (Members > 0 && Members <= 4);
13315}
13316
Tim Northovere95c5b32015-02-24 17:22:34 +000013317/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
13318/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
13319/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000013320bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
13321 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000013322 if (getEffectiveCallingConv(CallConv, isVarArg) !=
13323 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000013324 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000013325
13326 HABaseType Base = HA_UNKNOWN;
13327 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000013328 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
13329 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
13330
13331 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
13332 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000013333}
Joseph Tremouletf748c892015-11-07 01:11:31 +000013334
13335unsigned ARMTargetLowering::getExceptionPointerRegister(
13336 const Constant *PersonalityFn) const {
13337 // Platforms which do not use SjLj EH may return values in these registers
13338 // via the personality function.
13339 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
13340}
13341
13342unsigned ARMTargetLowering::getExceptionSelectorRegister(
13343 const Constant *PersonalityFn) const {
13344 // Platforms which do not use SjLj EH may return values in these registers
13345 // via the personality function.
13346 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
13347}
Manman Ren5e9e65e2016-01-12 00:47:18 +000013348
13349void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
13350 // Update IsSplitCSR in ARMFunctionInfo.
13351 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
13352 AFI->setIsSplitCSR(true);
13353}
13354
13355void ARMTargetLowering::insertCopiesSplitCSR(
13356 MachineBasicBlock *Entry,
13357 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
13358 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
13359 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
13360 if (!IStart)
13361 return;
13362
13363 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
13364 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000013365 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000013366 for (const MCPhysReg *I = IStart; *I; ++I) {
13367 const TargetRegisterClass *RC = nullptr;
13368 if (ARM::GPRRegClass.contains(*I))
13369 RC = &ARM::GPRRegClass;
13370 else if (ARM::DPRRegClass.contains(*I))
13371 RC = &ARM::DPRRegClass;
13372 else
13373 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
13374
13375 unsigned NewVR = MRI->createVirtualRegister(RC);
13376 // Create copy from CSR to a virtual register.
13377 // FIXME: this currently does not emit CFI pseudo-instructions, it works
13378 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
13379 // nounwind. If we want to generalize this later, we may need to emit
13380 // CFI pseudo-instructions.
13381 assert(Entry->getParent()->getFunction()->hasFnAttribute(
13382 Attribute::NoUnwind) &&
13383 "Function should be nounwind in insertCopiesSplitCSR!");
13384 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000013385 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000013386 .addReg(*I);
13387
Manman Rene5f807f2016-01-15 20:24:11 +000013388 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000013389 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000013390 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
13391 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000013392 .addReg(NewVR);
13393 }
13394}