blob: a41c4fcb9cd08549aef2b3c2cf69c9fb1c60ae44 [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();
Owen Anderson9f944592009-08-11 20:47:22 +0000281 if (ElemTy != MVT::i64 && 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);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000745 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
746 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000747 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000748 // a destination type that is wider than the source, and nor does
749 // it have a FP_TO_[SU]INT instruction with a narrower destination than
750 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000751 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
752 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000753 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
754 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000755
Eli Friedmane6385e62012-11-15 22:44:27 +0000756 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000757 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000758
Evan Chengb4eae132012-12-04 22:41:50 +0000759 // NEON does not have single instruction CTPOP for vectors with element
760 // types wider than 8-bits. However, custom lowering can leverage the
761 // v8i8/v16i8 vcnt instruction.
762 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
763 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
764 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
765 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
Benjamin Kramer569efd22016-03-31 19:42:04 +0000766 setOperationAction(ISD::CTPOP, MVT::v1i64, Expand);
767 setOperationAction(ISD::CTPOP, MVT::v2i64, Expand);
Evan Chengb4eae132012-12-04 22:41:50 +0000768
Craig Topperedb4a6b2016-04-26 05:04:33 +0000769 setOperationAction(ISD::CTLZ, MVT::v1i64, Expand);
770 setOperationAction(ISD::CTLZ, MVT::v2i64, Expand);
771
Logan Chien0a43abc2015-07-13 15:37:30 +0000772 // NEON does not have single instruction CTTZ for vectors.
773 setOperationAction(ISD::CTTZ, MVT::v8i8, Custom);
774 setOperationAction(ISD::CTTZ, MVT::v4i16, Custom);
775 setOperationAction(ISD::CTTZ, MVT::v2i32, Custom);
776 setOperationAction(ISD::CTTZ, MVT::v1i64, Custom);
777
778 setOperationAction(ISD::CTTZ, MVT::v16i8, Custom);
779 setOperationAction(ISD::CTTZ, MVT::v8i16, Custom);
780 setOperationAction(ISD::CTTZ, MVT::v4i32, Custom);
781 setOperationAction(ISD::CTTZ, MVT::v2i64, Custom);
782
783 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i8, Custom);
784 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i16, Custom);
785 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i32, Custom);
786 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v1i64, Custom);
787
788 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v16i8, Custom);
789 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v8i16, Custom);
790 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v4i32, Custom);
791 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::v2i64, Custom);
792
Jim Grosbach5f215872013-02-27 21:31:12 +0000793 // NEON only has FMA instructions as of VFP4.
794 if (!Subtarget->hasVFP4()) {
795 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
796 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
797 }
798
Bob Wilson06fce872011-02-07 17:43:21 +0000799 setTargetDAGCombine(ISD::INTRINSIC_VOID);
800 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000801 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
802 setTargetDAGCombine(ISD::SHL);
803 setTargetDAGCombine(ISD::SRL);
804 setTargetDAGCombine(ISD::SRA);
805 setTargetDAGCombine(ISD::SIGN_EXTEND);
806 setTargetDAGCombine(ISD::ZERO_EXTEND);
807 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000808 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000809 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000810 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
811 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000812 setTargetDAGCombine(ISD::FP_TO_SINT);
813 setTargetDAGCombine(ISD::FP_TO_UINT);
814 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000815 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000816
James Molloy547d4c02012-02-20 09:24:05 +0000817 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000818 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
819 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000820 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000821 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
822 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
823 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000824 }
James Molloy547d4c02012-02-20 09:24:05 +0000825 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000826 }
827
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000828 // ARM and Thumb2 support UMLAL/SMLAL.
829 if (!Subtarget->isThumb1Only())
830 setTargetDAGCombine(ISD::ADDC);
831
Oliver Stannard51b1d462014-08-21 12:50:31 +0000832 if (Subtarget->isFPOnlySP()) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000833 // When targeting a floating-point unit with only single-precision
Oliver Stannard51b1d462014-08-21 12:50:31 +0000834 // operations, f64 is legal for the few double-precision instructions which
835 // are present However, no double-precision operations other than moves,
836 // loads and stores are provided by the hardware.
837 setOperationAction(ISD::FADD, MVT::f64, Expand);
838 setOperationAction(ISD::FSUB, MVT::f64, Expand);
839 setOperationAction(ISD::FMUL, MVT::f64, Expand);
840 setOperationAction(ISD::FMA, MVT::f64, Expand);
841 setOperationAction(ISD::FDIV, MVT::f64, Expand);
842 setOperationAction(ISD::FREM, MVT::f64, Expand);
843 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
844 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
845 setOperationAction(ISD::FNEG, MVT::f64, Expand);
846 setOperationAction(ISD::FABS, MVT::f64, Expand);
847 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
848 setOperationAction(ISD::FSIN, MVT::f64, Expand);
849 setOperationAction(ISD::FCOS, MVT::f64, Expand);
850 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
851 setOperationAction(ISD::FPOW, MVT::f64, Expand);
852 setOperationAction(ISD::FLOG, MVT::f64, Expand);
853 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
854 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
855 setOperationAction(ISD::FEXP, MVT::f64, Expand);
856 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
857 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
858 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
859 setOperationAction(ISD::FRINT, MVT::f64, Expand);
860 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
861 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000862 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
863 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
864 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
865 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
866 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
867 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000868 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
869 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
870 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000871
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000872 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000873
Tim Northover4e80b582014-07-18 13:01:19 +0000874 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000875 for (MVT VT : MVT::fp_valuetypes()) {
876 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
877 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
878 }
Tim Northover4e80b582014-07-18 13:01:19 +0000879
880 // ... or truncating stores
881 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
882 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
883 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000884
Duncan Sands95d46ef2008-01-23 20:39:46 +0000885 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000886 for (MVT VT : MVT::integer_valuetypes())
887 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000888
Evan Cheng10043e22007-01-19 07:51:42 +0000889 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000890 if (!Subtarget->isThumb1Only()) {
891 for (unsigned im = (unsigned)ISD::PRE_INC;
892 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000893 setIndexedLoadAction(im, MVT::i1, Legal);
894 setIndexedLoadAction(im, MVT::i8, Legal);
895 setIndexedLoadAction(im, MVT::i16, Legal);
896 setIndexedLoadAction(im, MVT::i32, Legal);
897 setIndexedStoreAction(im, MVT::i1, Legal);
898 setIndexedStoreAction(im, MVT::i8, Legal);
899 setIndexedStoreAction(im, MVT::i16, Legal);
900 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000901 }
James Molloyb3326df2016-07-15 08:03:56 +0000902 } else {
903 // Thumb-1 has limited post-inc load/store support - LDM r0!, {r1}.
904 setIndexedLoadAction(ISD::POST_INC, MVT::i32, Legal);
905 setIndexedStoreAction(ISD::POST_INC, MVT::i32, Legal);
Evan Cheng10043e22007-01-19 07:51:42 +0000906 }
907
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000908 setOperationAction(ISD::SADDO, MVT::i32, Custom);
909 setOperationAction(ISD::UADDO, MVT::i32, Custom);
910 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
911 setOperationAction(ISD::USUBO, MVT::i32, Custom);
912
Evan Cheng10043e22007-01-19 07:51:42 +0000913 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000914 setOperationAction(ISD::MUL, MVT::i64, Expand);
915 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000916 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000917 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
918 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000919 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000920 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
Artyom Skrobovcf296442015-09-24 17:31:16 +0000921 || (Subtarget->isThumb2() && !Subtarget->hasDSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000922 setOperationAction(ISD::MULHS, MVT::i32, Expand);
923
Jim Grosbach5d994042009-10-31 19:38:01 +0000924 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000925 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000926 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000927 setOperationAction(ISD::SRL, MVT::i64, Custom);
928 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000929
Evan Chenge8916542011-08-30 01:34:54 +0000930 if (!Subtarget->isThumb1Only()) {
931 // FIXME: We should do this for Thumb1 as well.
932 setOperationAction(ISD::ADDC, MVT::i32, Custom);
933 setOperationAction(ISD::ADDE, MVT::i32, Custom);
934 setOperationAction(ISD::SUBC, MVT::i32, Custom);
935 setOperationAction(ISD::SUBE, MVT::i32, Custom);
936 }
937
Weiming Zhao4b3b13d2016-01-08 18:43:41 +0000938 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops())
James Molloyb5640982015-11-13 16:05:22 +0000939 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
940
Evan Cheng10043e22007-01-19 07:51:42 +0000941 // ARM does not have ROTL.
Charlie Turner458e79b2015-10-27 10:25:20 +0000942 setOperationAction(ISD::ROTL, MVT::i32, Expand);
943 for (MVT VT : MVT::vector_valuetypes()) {
944 setOperationAction(ISD::ROTL, VT, Expand);
945 setOperationAction(ISD::ROTR, VT, Expand);
946 }
Jim Grosbach8546ec92010-01-18 19:58:49 +0000947 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000948 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000949 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000950 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000951
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +0000952 // @llvm.readcyclecounter requires the Performance Monitors extension.
953 // Default to the 0 expansion on unsupported platforms.
954 // FIXME: Technically there are older ARM CPUs that have
955 // implementation-specific ways of obtaining this information.
956 if (Subtarget->hasPerfMon())
957 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
Tim Northoverbc933082013-05-23 19:11:20 +0000958
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000959 // Only ARMv6 has BSWAP.
960 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000961 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000962
Bradley Smith519563e2016-01-15 10:25:35 +0000963 bool hasDivide = Subtarget->isThumb() ? Subtarget->hasDivide()
964 : Subtarget->hasDivideInARMMode();
965 if (!hasDivide) {
Bob Wilsone8a549c2012-09-29 21:43:49 +0000966 // These are expanded into libcalls if the cpu doesn't have HW divider.
Artyom Skrobov7fd67e22015-10-20 13:14:52 +0000967 setOperationAction(ISD::SDIV, MVT::i32, LibCall);
968 setOperationAction(ISD::UDIV, MVT::i32, LibCall);
Jim Grosbach92d999002010-05-05 20:44:35 +0000969 }
Renato Golin87610692013-07-16 09:32:17 +0000970
Saleem Abdulrasool071a0992016-03-17 14:10:49 +0000971 if (Subtarget->isTargetWindows() && !Subtarget->hasDivide()) {
972 setOperationAction(ISD::SDIV, MVT::i32, Custom);
973 setOperationAction(ISD::UDIV, MVT::i32, Custom);
974
975 setOperationAction(ISD::SDIV, MVT::i64, Custom);
976 setOperationAction(ISD::UDIV, MVT::i64, Custom);
977 }
978
Chad Rosierad7c9102014-08-23 18:29:43 +0000979 setOperationAction(ISD::SREM, MVT::i32, Expand);
980 setOperationAction(ISD::UREM, MVT::i32, Expand);
981 // Register based DivRem for AEABI (RTABI 4.2)
Renato Golin6027dd38e2016-02-03 16:10:54 +0000982 if (Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
Martin Storsjo04864f42016-10-07 13:28:53 +0000983 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
984 Subtarget->isTargetWindows()) {
Scott Douglassbdef6042015-08-24 09:17:18 +0000985 setOperationAction(ISD::SREM, MVT::i64, Custom);
986 setOperationAction(ISD::UREM, MVT::i64, Custom);
Diana Picus774d1572016-07-18 06:48:25 +0000987 HasStandaloneRem = false;
Scott Douglassbdef6042015-08-24 09:17:18 +0000988
Martin Storsjo04864f42016-10-07 13:28:53 +0000989 for (const auto &LC :
990 {RTLIB::SDIVREM_I8, RTLIB::SDIVREM_I16, RTLIB::SDIVREM_I32})
991 setLibcallName(LC, Subtarget->isTargetWindows() ? "__rt_sdiv"
992 : "__aeabi_idivmod");
993 setLibcallName(RTLIB::SDIVREM_I64, Subtarget->isTargetWindows()
994 ? "__rt_sdiv64"
995 : "__aeabi_ldivmod");
996 for (const auto &LC :
997 {RTLIB::UDIVREM_I8, RTLIB::UDIVREM_I16, RTLIB::UDIVREM_I32})
998 setLibcallName(LC, Subtarget->isTargetWindows() ? "__rt_udiv"
999 : "__aeabi_uidivmod");
1000 setLibcallName(RTLIB::UDIVREM_I64, Subtarget->isTargetWindows()
1001 ? "__rt_udiv64"
1002 : "__aeabi_uldivmod");
Chad Rosierad7c9102014-08-23 18:29:43 +00001003
1004 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
1005 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
1006 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
1007 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
1008 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
1009 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
1010 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
1011 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
1012
1013 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
1014 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
Renato Golin175c6d62016-03-04 19:19:36 +00001015 setOperationAction(ISD::SDIVREM, MVT::i64, Custom);
1016 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
Chad Rosierad7c9102014-08-23 18:29:43 +00001017 } else {
Renato Golin87610692013-07-16 09:32:17 +00001018 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1019 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1020 }
Bob Wilson7117a912009-03-20 22:42:55 +00001021
Owen Anderson9f944592009-08-11 20:47:22 +00001022 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
1023 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +00001024 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +00001025 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +00001026
Evan Cheng74d92c12011-04-08 21:37:21 +00001027 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +00001028
Evan Cheng10043e22007-01-19 07:51:42 +00001029 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001030 setOperationAction(ISD::VASTART, MVT::Other, Custom);
1031 setOperationAction(ISD::VAARG, MVT::Other, Expand);
1032 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
1033 setOperationAction(ISD::VAEND, MVT::Other, Expand);
1034 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
1035 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +00001036
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001037 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
1038 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
1039 else
1040 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
1041
Evan Cheng6e809de2010-08-11 06:22:01 +00001042 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
James Y Knighte6a46462016-04-01 19:33:19 +00001043 // the default expansion.
James Y Knightf44fc522016-03-16 22:12:04 +00001044 InsertFencesForAtomic = false;
James Y Knighte6a46462016-04-01 19:33:19 +00001045 if (Subtarget->hasAnyDataBarrier() &&
1046 (!Subtarget->isThumb() || Subtarget->hasV8MBaselineOps())) {
Tim Northoverc882eb02014-04-03 11:44:58 +00001047 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
1048 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +00001049 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverb629c772016-04-18 21:48:55 +00001050 if (!Subtarget->isThumb() || !Subtarget->isMClass())
1051 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +00001052
Amara Emersonb4ad2f32013-09-26 12:22:36 +00001053 // On v8, we have particularly efficient implementations of atomic fences
1054 // if they can be combined with nearby atomic loads and stores.
Tim Northoverb629c772016-04-18 21:48:55 +00001055 if (!Subtarget->hasV8Ops() || getTargetMachine().getOptLevel() == 0) {
Robin Morissetd18cda62014-08-15 22:17:28 +00001056 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
James Y Knightf44fc522016-03-16 22:12:04 +00001057 InsertFencesForAtomic = true;
Amara Emersonb4ad2f32013-09-26 12:22:36 +00001058 }
Jim Grosbach6860bb72010-06-18 22:35:32 +00001059 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +00001060 // If there's anything we can use as a barrier, go through custom lowering
1061 // for ATOMIC_FENCE.
1062 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
1063 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
1064
Jim Grosbach6860bb72010-06-18 22:35:32 +00001065 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +00001066 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +00001067 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001068 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001069 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001070 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001071 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001072 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001073 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001074 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001075 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001076 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +00001077 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +00001078 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
1079 // Unordered/Monotonic case.
1080 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1081 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +00001082 }
Evan Cheng10043e22007-01-19 07:51:42 +00001083
Evan Cheng21acf9f2010-11-04 05:19:35 +00001084 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +00001085
Eli Friedman8cfa7712010-06-26 04:36:50 +00001086 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
1087 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001088 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1089 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +00001090 }
Owen Anderson9f944592009-08-11 20:47:22 +00001091 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +00001092
Eric Christopher824f42f2015-05-12 01:26:05 +00001093 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001094 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +00001095 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001096 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +00001097 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +00001098 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
1099 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00001100
1101 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +00001102 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Matthias Braun3cd00c12015-07-16 22:34:16 +00001103 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
1104 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
1105 setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom);
Tim Northoverf8e47e42015-10-28 22:56:36 +00001106 if (Subtarget->useSjLjEH())
John McCall7d84ece2011-05-29 19:50:32 +00001107 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00001108
Owen Anderson9f944592009-08-11 20:47:22 +00001109 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1110 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1111 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +00001112 setOperationAction(ISD::SELECT, MVT::i32, Custom);
1113 setOperationAction(ISD::SELECT, MVT::f32, Custom);
1114 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +00001115 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1116 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1117 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +00001118
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00001119 // Thumb-1 cannot currently select ARMISD::SUBE.
1120 if (!Subtarget->isThumb1Only())
1121 setOperationAction(ISD::SETCCE, MVT::i32, Custom);
1122
Owen Anderson9f944592009-08-11 20:47:22 +00001123 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1124 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1125 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1126 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
1127 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +00001128
Dan Gohman482732a2007-10-11 23:21:31 +00001129 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +00001130 setOperationAction(ISD::FSIN, MVT::f64, Expand);
1131 setOperationAction(ISD::FSIN, MVT::f32, Expand);
1132 setOperationAction(ISD::FCOS, MVT::f32, Expand);
1133 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001134 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1135 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001136 setOperationAction(ISD::FREM, MVT::f64, Expand);
1137 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +00001138 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001139 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001140 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
1141 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +00001142 }
Owen Anderson9f944592009-08-11 20:47:22 +00001143 setOperationAction(ISD::FPOW, MVT::f64, Expand);
1144 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +00001145
Evan Chengd0007f32012-04-10 21:40:28 +00001146 if (!Subtarget->hasVFP4()) {
1147 setOperationAction(ISD::FMA, MVT::f64, Expand);
1148 setOperationAction(ISD::FMA, MVT::f32, Expand);
1149 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001150
Anton Korobeynikovd7fece32010-03-14 18:42:31 +00001151 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +00001152 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001153 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
1154 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +00001155 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
1156 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
1157 }
1158
1159 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +00001160 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +00001161 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
1162 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +00001163 }
Evan Cheng86e476b2008-04-01 01:50:16 +00001164 }
Jim Grosbach1a597112014-04-03 23:43:18 +00001165
Bob Wilsone7dde0c2013-11-03 06:14:38 +00001166 // Combine sin / cos into one node or libcall if possible.
1167 if (Subtarget->hasSinCos()) {
1168 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1169 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Tim Northover042a6c12016-01-27 19:32:29 +00001170 if (Subtarget->isTargetWatchABI()) {
Tim Northover8b403662015-10-28 22:51:16 +00001171 setLibcallCallingConv(RTLIB::SINCOS_F32, CallingConv::ARM_AAPCS_VFP);
1172 setLibcallCallingConv(RTLIB::SINCOS_F64, CallingConv::ARM_AAPCS_VFP);
1173 }
1174 if (Subtarget->isTargetIOS() || Subtarget->isTargetWatchOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +00001175 // For iOS, we don't want to the normal expansion of a libcall to
1176 // sincos. We want to issue a libcall to __sincos_stret.
1177 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1178 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1179 }
1180 }
Evan Cheng10043e22007-01-19 07:51:42 +00001181
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001182 // FP-ARMv8 implements a lot of rounding-like FP operations.
1183 if (Subtarget->hasFPARMv8()) {
1184 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
1185 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
1186 setOperationAction(ISD::FROUND, MVT::f32, Legal);
1187 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
1188 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
1189 setOperationAction(ISD::FRINT, MVT::f32, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001190 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
1191 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
James Molloyee868b22015-08-11 12:06:25 +00001192 setOperationAction(ISD::FMINNUM, MVT::v2f32, Legal);
1193 setOperationAction(ISD::FMAXNUM, MVT::v2f32, Legal);
1194 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
1195 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
1196
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +00001197 if (!Subtarget->isFPOnlySP()) {
1198 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
1199 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
1200 setOperationAction(ISD::FROUND, MVT::f64, Legal);
1201 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
1202 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
1203 setOperationAction(ISD::FRINT, MVT::f64, Legal);
James Molloyea3a6872015-08-11 12:06:22 +00001204 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
1205 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +00001206 }
1207 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001208
James Molloy974838f2015-08-17 19:37:12 +00001209 if (Subtarget->hasNEON()) {
1210 // vmin and vmax aren't available in a scalar form, so we use
1211 // a NEON instruction with an undef lane instead.
James Molloydb8ee4b2015-08-11 12:06:15 +00001212 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
1213 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
James Molloyd616c642015-08-11 12:06:28 +00001214 setOperationAction(ISD::FMINNAN, MVT::v2f32, Legal);
1215 setOperationAction(ISD::FMAXNAN, MVT::v2f32, Legal);
1216 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
1217 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
1218 }
James Molloydb8ee4b2015-08-11 12:06:15 +00001219
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00001220 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001221 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +00001222 setTargetDAGCombine(ISD::ADD);
1223 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00001224 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +00001225 setTargetDAGCombine(ISD::AND);
1226 setTargetDAGCombine(ISD::OR);
1227 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +00001228
Evan Chengf258a152012-02-23 02:58:19 +00001229 if (Subtarget->hasV6Ops())
1230 setTargetDAGCombine(ISD::SRL);
1231
Evan Cheng10043e22007-01-19 07:51:42 +00001232 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +00001233
Eric Christopher824f42f2015-05-12 01:26:05 +00001234 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001235 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +00001236 setSchedulingPreference(Sched::RegPressure);
1237 else
1238 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +00001239
Evan Cheng3ae2b792011-01-06 06:52:41 +00001240 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001241 MaxStoresPerMemset = 8;
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001242 MaxStoresPerMemsetOptSize = 4;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001243 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001244 MaxStoresPerMemcpyOptSize = 2;
Jim Grosbach341ad3e2013-02-20 21:13:59 +00001245 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
Sanjay Patel1166f2f2015-07-30 21:41:50 +00001246 MaxStoresPerMemmoveOptSize = 2;
Evan Chengb71233f2010-06-26 01:52:05 +00001247
Rafael Espindolaa76eccf2010-07-11 04:01:49 +00001248 // On ARM arguments smaller than 4 bytes are extended, so all arguments
1249 // are at least 4 bytes aligned.
1250 setMinStackArgumentAlignment(4);
1251
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001252 // Prefer likely predicted branches to selects on out-of-order cores.
Junmo Park453f4aa2016-02-23 09:56:58 +00001253 PredictableSelectIsExpensive = Subtarget->getSchedModel().isOutOfOrder();
Benjamin Kramere31f31e2012-05-05 12:49:14 +00001254
Eli Friedman2518f832011-05-06 20:34:06 +00001255 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +00001256}
1257
Eric Christopher824f42f2015-05-12 01:26:05 +00001258bool ARMTargetLowering::useSoftFloat() const {
1259 return Subtarget->useSoftFloat();
1260}
1261
Andrew Trick43f25632011-01-19 02:35:27 +00001262// FIXME: It might make sense to define the representative register class as the
1263// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
1264// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
1265// SPR's representative would be DPR_VFP2. This should work well if register
1266// pressure tracking were modified such that a register use would increment the
1267// pressure of the register class's representative and all of it's super
1268// classes' representatives transitively. We have not implemented this because
1269// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001270// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +00001271// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001272std::pair<const TargetRegisterClass *, uint8_t>
1273ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
1274 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001275 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +00001276 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +00001277 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +00001278 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +00001279 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +00001280 // Use DPR as representative register class for all floating point
1281 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
1282 // the cost is 1 for both f32 and f64.
1283 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +00001284 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +00001285 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +00001286 // When NEON is used for SP, only half of the register file is available
1287 // because operations that define both SP and DP results will be constrained
1288 // to the VFP2 class (D0-D15). We currently model this constraint prior to
1289 // coalescing by double-counting the SP regs. See the FIXME above.
1290 if (Subtarget->useNEONForSinglePrecisionFP())
1291 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001292 break;
1293 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1294 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +00001295 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001296 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +00001297 break;
1298 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001299 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001300 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001301 break;
1302 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001303 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001304 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001305 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001306 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001307 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001308}
1309
Evan Cheng10043e22007-01-19 07:51:42 +00001310const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001311 switch ((ARMISD::NodeType)Opcode) {
1312 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001313 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001314 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001315 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001316 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001317 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001318 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001319 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
Evan Cheng10043e22007-01-19 07:51:42 +00001320 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1321 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001322 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001323 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001324 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001325 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1326 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001327 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001328 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001329 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1330 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001331 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001332 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001333
Evan Cheng10043e22007-01-19 07:51:42 +00001334 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001335
Pablo Barrio7a643462016-06-23 16:53:49 +00001336 case ARMISD::SSAT: return "ARMISD::SSAT";
1337
Evan Cheng10043e22007-01-19 07:51:42 +00001338 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1339 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1340 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001341
Evan Chenge8916542011-08-30 01:34:54 +00001342 case ARMISD::ADDC: return "ARMISD::ADDC";
1343 case ARMISD::ADDE: return "ARMISD::ADDE";
1344 case ARMISD::SUBC: return "ARMISD::SUBC";
1345 case ARMISD::SUBE: return "ARMISD::SUBE";
1346
Bob Wilson22806742010-09-22 22:09:21 +00001347 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1348 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001349
Evan Chengec6d7c92009-10-28 06:55:03 +00001350 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
Matthias Braun3cd00c12015-07-16 22:34:16 +00001351 case ARMISD::EH_SJLJ_LONGJMP: return "ARMISD::EH_SJLJ_LONGJMP";
1352 case ARMISD::EH_SJLJ_SETUP_DISPATCH: return "ARMISD::EH_SJLJ_SETUP_DISPATCH";
Evan Chengec6d7c92009-10-28 06:55:03 +00001353
Dale Johannesend679ff72010-06-03 21:09:53 +00001354 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001355
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001356 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001357
Evan Chengb972e562009-08-07 00:34:42 +00001358 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1359
Bob Wilson7ed59712010-10-30 00:54:37 +00001360 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001361
Evan Cheng8740ee32010-11-03 06:34:55 +00001362 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1363
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001364 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00001365 case ARMISD::WIN__DBZCHK: return "ARMISD::WIN__DBZCHK";
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001366
Bob Wilson2e076c42009-06-22 23:27:02 +00001367 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001368 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001369 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001370 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1371 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001372 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1373 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001374 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1375 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001376 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1377 case ARMISD::VTST: return "ARMISD::VTST";
1378
1379 case ARMISD::VSHL: return "ARMISD::VSHL";
1380 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1381 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001382 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1383 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1384 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1385 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1386 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1387 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1388 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1389 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1390 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1391 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1392 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1393 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
Matthias Braund04893f2015-05-07 21:33:59 +00001394 case ARMISD::VSLI: return "ARMISD::VSLI";
1395 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001396 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1397 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001398 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001399 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001400 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001401 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001402 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001403 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001404 case ARMISD::VREV64: return "ARMISD::VREV64";
1405 case ARMISD::VREV32: return "ARMISD::VREV32";
1406 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001407 case ARMISD::VZIP: return "ARMISD::VZIP";
1408 case ARMISD::VUZP: return "ARMISD::VUZP";
1409 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001410 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1411 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001412 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1413 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Sam Parkerd616cf02016-06-20 16:47:09 +00001414 case ARMISD::UMAAL: return "ARMISD::UMAAL";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001415 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1416 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001417 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001418 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001419 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1420 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001421 case ARMISD::VBSL: return "ARMISD::VBSL";
Scott Douglass953f9082015-10-05 14:49:54 +00001422 case ARMISD::MEMCPY: return "ARMISD::MEMCPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001423 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1424 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1425 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001426 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1427 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1428 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1429 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1430 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1431 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1432 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1433 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1434 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1435 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1436 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1437 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1438 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1439 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1440 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1441 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1442 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001443 }
Matthias Braund04893f2015-05-07 21:33:59 +00001444 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001445}
1446
Mehdi Amini44ede332015-07-09 02:09:04 +00001447EVT ARMTargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1448 EVT VT) const {
1449 if (!VT.isVector())
1450 return getPointerTy(DL);
Duncan Sandsf2641e12011-09-06 19:07:46 +00001451 return VT.changeVectorElementTypeToInteger();
1452}
1453
Evan Cheng4cad68e2010-05-15 02:18:07 +00001454/// getRegClassFor - Return the register class that should be used for the
1455/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001456const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001457 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1458 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1459 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001460 if (Subtarget->hasNEON()) {
1461 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001462 return &ARM::QQPRRegClass;
1463 if (VT == MVT::v8i64)
1464 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001465 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001466 return TargetLowering::getRegClassFor(VT);
1467}
1468
John Brawn0dbcd652015-03-18 12:01:59 +00001469// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1470// source/dest is aligned and the copy size is large enough. We therefore want
1471// to align such objects passed to memory intrinsics.
1472bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1473 unsigned &PrefAlign) const {
1474 if (!isa<MemIntrinsic>(CI))
1475 return false;
1476 MinSize = 8;
1477 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1478 // cycle faster than 4-byte aligned LDM.
1479 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1480 return true;
1481}
1482
Eric Christopher84bdfd82010-07-21 22:26:11 +00001483// Create a fast isel object.
1484FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001485ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1486 const TargetLibraryInfo *libInfo) const {
1487 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001488}
1489
Evan Cheng4401f882010-05-20 23:26:43 +00001490Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001491 unsigned NumVals = N->getNumValues();
1492 if (!NumVals)
1493 return Sched::RegPressure;
1494
1495 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001496 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001497 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001498 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001499 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001500 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001501 }
Evan Chengbf914992010-05-28 23:25:23 +00001502
1503 if (!N->isMachineOpcode())
1504 return Sched::RegPressure;
1505
1506 // Load are scheduled for latency even if there instruction itinerary
1507 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001508 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001509 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001510
Evan Cheng6cc775f2011-06-28 19:10:37 +00001511 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001512 return Sched::RegPressure;
1513 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001514 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001515 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001516
Evan Cheng4401f882010-05-20 23:26:43 +00001517 return Sched::RegPressure;
1518}
1519
Evan Cheng10043e22007-01-19 07:51:42 +00001520//===----------------------------------------------------------------------===//
1521// Lowering Code
1522//===----------------------------------------------------------------------===//
1523
Evan Cheng10043e22007-01-19 07:51:42 +00001524/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1525static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1526 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001527 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001528 case ISD::SETNE: return ARMCC::NE;
1529 case ISD::SETEQ: return ARMCC::EQ;
1530 case ISD::SETGT: return ARMCC::GT;
1531 case ISD::SETGE: return ARMCC::GE;
1532 case ISD::SETLT: return ARMCC::LT;
1533 case ISD::SETLE: return ARMCC::LE;
1534 case ISD::SETUGT: return ARMCC::HI;
1535 case ISD::SETUGE: return ARMCC::HS;
1536 case ISD::SETULT: return ARMCC::LO;
1537 case ISD::SETULE: return ARMCC::LS;
1538 }
1539}
1540
Bob Wilsona2e83332009-09-09 23:14:54 +00001541/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1542static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001543 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001544 CondCode2 = ARMCC::AL;
1545 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001546 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001547 case ISD::SETEQ:
1548 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1549 case ISD::SETGT:
1550 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1551 case ISD::SETGE:
1552 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1553 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001554 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001555 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1556 case ISD::SETO: CondCode = ARMCC::VC; break;
1557 case ISD::SETUO: CondCode = ARMCC::VS; break;
1558 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1559 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1560 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1561 case ISD::SETLT:
1562 case ISD::SETULT: CondCode = ARMCC::LT; break;
1563 case ISD::SETLE:
1564 case ISD::SETULE: CondCode = ARMCC::LE; break;
1565 case ISD::SETNE:
1566 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1567 }
Evan Cheng10043e22007-01-19 07:51:42 +00001568}
1569
Bob Wilsona4c22902009-04-17 19:07:39 +00001570//===----------------------------------------------------------------------===//
1571// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001572//===----------------------------------------------------------------------===//
1573
1574#include "ARMGenCallingConv.inc"
1575
Oliver Stannardc24f2172014-05-09 14:01:47 +00001576/// getEffectiveCallingConv - Get the effective calling convention, taking into
1577/// account presence of floating point hardware and calling convention
1578/// limitations, such as support for variadic functions.
1579CallingConv::ID
1580ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1581 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001582 switch (CC) {
1583 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001584 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001585 case CallingConv::ARM_AAPCS:
1586 case CallingConv::ARM_APCS:
1587 case CallingConv::GHC:
1588 return CC;
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001589 case CallingConv::PreserveMost:
1590 return CallingConv::PreserveMost;
Oliver Stannardc24f2172014-05-09 14:01:47 +00001591 case CallingConv::ARM_AAPCS_VFP:
Manman Ren802cd6f2016-04-05 22:44:44 +00001592 case CallingConv::Swift:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001593 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1594 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001595 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001596 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001597 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001598 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1599 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001600 return CallingConv::ARM_AAPCS_VFP;
1601 else
1602 return CallingConv::ARM_AAPCS;
1603 case CallingConv::Fast:
Manman Ren16026052016-01-11 23:50:43 +00001604 case CallingConv::CXX_FAST_TLS:
Oliver Stannardc24f2172014-05-09 14:01:47 +00001605 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001606 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001607 return CallingConv::Fast;
1608 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001609 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001610 return CallingConv::ARM_AAPCS_VFP;
1611 else
1612 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001613 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001614}
1615
1616/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1617/// CallingConvention.
1618CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1619 bool Return,
1620 bool isVarArg) const {
1621 switch (getEffectiveCallingConv(CC, isVarArg)) {
1622 default:
1623 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001624 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001625 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001626 case CallingConv::ARM_AAPCS:
1627 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1628 case CallingConv::ARM_AAPCS_VFP:
1629 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1630 case CallingConv::Fast:
1631 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001632 case CallingConv::GHC:
1633 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Roman Levenstein2792b3f2016-03-10 04:35:09 +00001634 case CallingConv::PreserveMost:
1635 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001636 }
1637}
1638
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001639/// LowerCallResult - Lower the result values of a call into the
1640/// appropriate copies out of appropriate physical registers.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001641SDValue ARMTargetLowering::LowerCallResult(
1642 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg,
1643 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
1644 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool isThisReturn,
1645 SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001646
Bob Wilsona4c22902009-04-17 19:07:39 +00001647 // Assign locations to each value returned by this call.
1648 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001649 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1650 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001651 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001652 CCAssignFnForNode(CallConv, /* Return*/ true,
1653 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001654
1655 // Copy all of the result registers out of their specified physreg.
1656 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1657 CCValAssign VA = RVLocs[i];
1658
Stephen Linb8bd2322013-04-20 05:14:40 +00001659 // Pass 'this' value directly from the argument to return value, to avoid
1660 // reg unit interference
David Majnemer5d261272016-07-20 04:13:01 +00001661 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001662 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1663 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001664 InVals.push_back(ThisVal);
1665 continue;
1666 }
1667
Bob Wilson0041bd32009-04-25 00:33:20 +00001668 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001669 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001670 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001671 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001672 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001673 Chain = Lo.getValue(1);
1674 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001675 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001676 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001677 InFlag);
1678 Chain = Hi.getValue(1);
1679 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001680 if (!Subtarget->isLittle())
1681 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001682 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001683
Owen Anderson9f944592009-08-11 20:47:22 +00001684 if (VA.getLocVT() == MVT::v2f64) {
1685 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1686 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001687 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001688
1689 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001690 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001691 Chain = Lo.getValue(1);
1692 InFlag = Lo.getValue(2);
1693 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001694 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001695 Chain = Hi.getValue(1);
1696 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001697 if (!Subtarget->isLittle())
1698 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001699 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001700 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001701 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001702 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001703 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001704 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1705 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001706 Chain = Val.getValue(1);
1707 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001708 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001709
1710 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001711 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001712 case CCValAssign::Full: break;
1713 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001714 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001715 break;
1716 }
1717
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001718 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001719 }
1720
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001721 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001722}
1723
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001724/// LowerMemOpCallTo - Store the argument to the stack.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001725SDValue ARMTargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr,
1726 SDValue Arg, const SDLoc &dl,
1727 SelectionDAG &DAG,
1728 const CCValAssign &VA,
1729 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001730 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001731 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001732 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
1733 StackPtr, PtrOff);
Alex Lorenze40c8a22015-08-11 23:09:45 +00001734 return DAG.getStore(
1735 Chain, dl, Arg, PtrOff,
Justin Lebar9c375812016-07-15 18:27:10 +00001736 MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset));
Evan Cheng10043e22007-01-19 07:51:42 +00001737}
1738
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001739void ARMTargetLowering::PassF64ArgInRegs(const SDLoc &dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001740 SDValue Chain, SDValue &Arg,
1741 RegsToPassVector &RegsToPass,
1742 CCValAssign &VA, CCValAssign &NextVA,
1743 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001744 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001745 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001746
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001747 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001748 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001749 unsigned id = Subtarget->isLittle() ? 0 : 1;
1750 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001751
1752 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001753 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001754 else {
1755 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001756 if (!StackPtr.getNode())
Mehdi Amini44ede332015-07-09 02:09:04 +00001757 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP,
1758 getPointerTy(DAG.getDataLayout()));
Bob Wilson2e076c42009-06-22 23:27:02 +00001759
Christian Pirkerb5728192014-05-08 14:06:24 +00001760 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001761 dl, DAG, NextVA,
1762 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001763 }
1764}
1765
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001766/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001767/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1768/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001769SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001770ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001771 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001772 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001773 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001774 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1775 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1776 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001777 SDValue Chain = CLI.Chain;
1778 SDValue Callee = CLI.Callee;
1779 bool &isTailCall = CLI.IsTailCall;
1780 CallingConv::ID CallConv = CLI.CallConv;
1781 bool doesNotRet = CLI.DoesNotReturn;
1782 bool isVarArg = CLI.IsVarArg;
1783
Dale Johannesend679ff72010-06-03 21:09:53 +00001784 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001785 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1786 bool isThisReturn = false;
1787 bool isSibCall = false;
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001788 auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001789
Bob Wilson8decdc42011-10-07 17:17:49 +00001790 // Disable tail calls if they're not supported.
Akira Hatanakad9699bc2015-06-09 19:07:19 +00001791 if (!Subtarget->supportsTailCall() || Attr.getValueAsString() == "true")
Bob Wilson3c9ed762010-08-13 22:43:33 +00001792 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001793
Dale Johannesend679ff72010-06-03 21:09:53 +00001794 if (isTailCall) {
1795 // Check if it's really possible to do a tail call.
1796 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001797 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001798 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001799 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1800 report_fatal_error("failed to perform tail call elimination on a call "
1801 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001802 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1803 // detected sibcalls.
1804 if (isTailCall) {
1805 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001806 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001807 }
1808 }
Evan Cheng10043e22007-01-19 07:51:42 +00001809
Bob Wilsona4c22902009-04-17 19:07:39 +00001810 // Analyze operands of the call, assigning locations to each operand.
1811 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001812 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1813 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001814 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001815 CCAssignFnForNode(CallConv, /* Return*/ false,
1816 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001817
Bob Wilsona4c22902009-04-17 19:07:39 +00001818 // Get a count of how many bytes are to be pushed on the stack.
1819 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001820
Dale Johannesend679ff72010-06-03 21:09:53 +00001821 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001822 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001823 NumBytes = 0;
1824
Evan Cheng10043e22007-01-19 07:51:42 +00001825 // Adjust the stack pointer for the new arguments...
1826 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001827 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001828 Chain = DAG.getCALLSEQ_START(Chain,
1829 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001830
Mehdi Amini44ede332015-07-09 02:09:04 +00001831 SDValue StackPtr =
1832 DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy(DAG.getDataLayout()));
Evan Cheng10043e22007-01-19 07:51:42 +00001833
Bob Wilson2e076c42009-06-22 23:27:02 +00001834 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001835 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001836
Bob Wilsona4c22902009-04-17 19:07:39 +00001837 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001838 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001839 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1840 i != e;
1841 ++i, ++realArgIdx) {
1842 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001843 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001844 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001845 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001846
Bob Wilsona4c22902009-04-17 19:07:39 +00001847 // Promote the value if needed.
1848 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001849 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001850 case CCValAssign::Full: break;
1851 case CCValAssign::SExt:
1852 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1853 break;
1854 case CCValAssign::ZExt:
1855 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1856 break;
1857 case CCValAssign::AExt:
1858 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1859 break;
1860 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001861 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001862 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001863 }
1864
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001865 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001866 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001867 if (VA.getLocVT() == MVT::v2f64) {
1868 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001869 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001870 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001871 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001872
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001873 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001874 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1875
1876 VA = ArgLocs[++i]; // skip ahead to next loc
1877 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001878 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001879 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1880 } else {
1881 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001882
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001883 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1884 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001885 }
1886 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001887 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001888 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001889 }
1890 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001891 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1892 assert(VA.getLocVT() == MVT::i32 &&
1893 "unexpected calling convention register assignment");
1894 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001895 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001896 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001897 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001898 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001899 } else if (isByVal) {
1900 assert(VA.isMemLoc());
1901 unsigned offset = 0;
1902
1903 // True if this byval aggregate will be split between registers
1904 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001905 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001906 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001907
1908 if (CurByValIdx < ByValArgsCount) {
1909
1910 unsigned RegBegin, RegEnd;
1911 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1912
Mehdi Amini44ede332015-07-09 02:09:04 +00001913 EVT PtrVT =
1914 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001915 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001916 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001917 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001918 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1919 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1920 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001921 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001922 MemOpChains.push_back(Load.getValue(1));
1923 RegsToPass.push_back(std::make_pair(j, Load));
1924 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001925
1926 // If parameter size outsides register area, "offset" value
1927 // helps us to calculate stack slot for remained part properly.
1928 offset = RegEnd - RegBegin;
1929
1930 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001931 }
1932
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001933 if (Flags.getByValSize() > 4*offset) {
Mehdi Amini44ede332015-07-09 02:09:04 +00001934 auto PtrVT = getPointerTy(DAG.getDataLayout());
Manman Ren9f911162012-06-01 02:44:42 +00001935 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001936 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001937 SDValue Dst = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001938 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Mehdi Amini44ede332015-07-09 02:09:04 +00001939 SDValue Src = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001940 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001941 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001942 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1943 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001944
Manman Ren9f911162012-06-01 02:44:42 +00001945 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001946 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001947 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001948 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001949 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001950 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001951 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001952
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001953 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1954 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001955 }
Evan Cheng10043e22007-01-19 07:51:42 +00001956 }
1957
1958 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001959 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001960
1961 // Build a sequence of copy-to-reg nodes chained together with token chain
1962 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001963 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001964 // Tail call byval lowering might overwrite argument registers so in case of
1965 // tail call optimization the copies to registers are lowered later.
1966 if (!isTailCall)
1967 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1968 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1969 RegsToPass[i].second, InFlag);
1970 InFlag = Chain.getValue(1);
1971 }
Evan Cheng10043e22007-01-19 07:51:42 +00001972
Dale Johannesend679ff72010-06-03 21:09:53 +00001973 // For tail calls lower the arguments to the 'real' stack slot.
1974 if (isTailCall) {
1975 // Force all the incoming stack arguments to be loaded from the stack
1976 // before any new outgoing arguments are stored to the stack, because the
1977 // outgoing stack slots may alias the incoming argument stack slots, and
1978 // the alias isn't otherwise explicit. This is slightly more conservative
1979 // than necessary, because it means that each store effectively depends
1980 // on every argument instead of just those arguments it would clobber.
1981
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001982 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001983 InFlag = SDValue();
1984 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1985 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1986 RegsToPass[i].second, InFlag);
1987 InFlag = Chain.getValue(1);
1988 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001989 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001990 }
1991
Bill Wendling24c79f22008-09-16 21:48:12 +00001992 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1993 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1994 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001995 bool isDirect = false;
Rafael Espindola3888bdb2016-06-16 15:22:01 +00001996
1997 const TargetMachine &TM = getTargetMachine();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00001998 const Module *Mod = MF.getFunction()->getParent();
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00001999 const GlobalValue *GV = nullptr;
2000 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
2001 GV = G->getGlobal();
Rafael Espindolac1d739f2016-06-16 15:40:24 +00002002 bool isStub =
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002003 !TM.shouldAssumeDSOLocal(*Mod, GV) && Subtarget->isTargetMachO();
Rafael Espindola3888bdb2016-06-16 15:22:01 +00002004
Rafael Espindola9ba9c5b2016-06-16 15:44:06 +00002005 bool isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00002006 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00002007 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002008 auto PtrVt = getPointerTy(DAG.getDataLayout());
Jim Grosbach32bb3622010-04-14 22:28:31 +00002009
Akira Hatanaka1bc8af72015-07-07 06:54:42 +00002010 if (Subtarget->genLongCalls()) {
Saleem Abdulrasool4d950ef2016-07-05 18:30:52 +00002011 assert((!isPositionIndependent() || Subtarget->isTargetWindows()) &&
Rafael Espindola0f898332016-06-20 16:43:17 +00002012 "long-calls codegen is not position independent!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00002013 // Handle a global address or an external symbol. If it's not one of
2014 // those, the target's already in a register, so we don't need to do
2015 // anything extra.
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00002016 if (isa<GlobalAddressSDNode>(Callee)) {
Jim Grosbach32bb3622010-04-14 22:28:31 +00002017 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00002018 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002019 ARMConstantPoolValue *CPV =
2020 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
2021
Jim Grosbach32bb3622010-04-14 22:28:31 +00002022 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00002023 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00002024 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002025 Callee = DAG.getLoad(
2026 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002027 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach32bb3622010-04-14 22:28:31 +00002028 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
2029 const char *Sym = S->getSymbol();
2030
2031 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00002032 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00002033 ARMConstantPoolValue *CPV =
2034 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
2035 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00002036 // Get the address of the callee into a register
Mehdi Amini44ede332015-07-09 02:09:04 +00002037 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Jim Grosbach32bb3622010-04-14 22:28:31 +00002038 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002039 Callee = DAG.getLoad(
2040 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002041 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach32bb3622010-04-14 22:28:31 +00002042 }
Rafael Espindolac24f0ee2016-06-16 15:31:06 +00002043 } else if (isa<GlobalAddressSDNode>(Callee)) {
James Molloy2af08fa2016-07-15 07:57:35 +00002044 // If we're optimizing for minimum size and the function is called three or
2045 // more times in this block, we can improve codesize by calling indirectly
James Molloya454a112016-07-15 07:55:21 +00002046 // as BLXr has a 16-bit encoding.
2047 auto *GV = cast<GlobalAddressSDNode>(Callee)->getGlobal();
2048 auto *BB = CLI.CS->getParent();
2049 bool PreferIndirect =
2050 Subtarget->isThumb() && MF.getFunction()->optForMinSize() &&
David Majnemer0da5afe2016-08-12 04:32:29 +00002051 count_if(GV->users(), [&BB](const User *U) {
James Molloya454a112016-07-15 07:55:21 +00002052 return isa<Instruction>(U) && cast<Instruction>(U)->getParent() == BB;
2053 }) > 2;
Rafael Espindola41410cc2016-06-01 21:57:11 +00002054
James Molloya454a112016-07-15 07:55:21 +00002055 if (!PreferIndirect) {
2056 isDirect = true;
2057 bool isDef = GV->isStrongDefinitionForLinker();
2058
2059 // ARM call to a local ARM function is predicable.
2060 isLocalARMFunc = !Subtarget->isThumb() && (isDef || !ARMInterworking);
2061 // tBX takes a register source operand.
2062 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
2063 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
2064 Callee = DAG.getNode(
2065 ARMISD::WrapperPIC, dl, PtrVt,
2066 DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, ARMII::MO_NONLAZY));
Justin Lebaradbf09e2016-09-11 01:38:58 +00002067 Callee = DAG.getLoad(
2068 PtrVt, dl, DAG.getEntryNode(), Callee,
2069 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2070 /* Alignment = */ 0, MachineMemOperand::MODereferenceable |
2071 MachineMemOperand::MOInvariant);
James Molloya454a112016-07-15 07:55:21 +00002072 } else if (Subtarget->isTargetCOFF()) {
2073 assert(Subtarget->isTargetWindows() &&
2074 "Windows is the only supported COFF target");
2075 unsigned TargetFlags = GV->hasDLLImportStorageClass()
2076 ? ARMII::MO_DLLIMPORT
2077 : ARMII::MO_NO_FLAG;
2078 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, /*Offset=*/0,
2079 TargetFlags);
2080 if (GV->hasDLLImportStorageClass())
2081 Callee =
2082 DAG.getLoad(PtrVt, dl, DAG.getEntryNode(),
2083 DAG.getNode(ARMISD::Wrapper, dl, PtrVt, Callee),
Justin Lebar9c375812016-07-15 18:27:10 +00002084 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
James Molloya454a112016-07-15 07:55:21 +00002085 } else {
2086 Callee = DAG.getTargetGlobalAddress(GV, dl, PtrVt, 0, 0);
2087 }
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00002088 }
Bill Wendling24c79f22008-09-16 21:48:12 +00002089 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00002090 isDirect = true;
Evan Cheng83f35172007-01-30 20:37:08 +00002091 // tBX takes a register source operand.
2092 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00002093 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00002094 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00002095 ARMConstantPoolValue *CPV =
2096 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
2097 ARMPCLabelIndex, 4);
Mehdi Amini44ede332015-07-09 02:09:04 +00002098 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVt, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002099 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002100 Callee = DAG.getLoad(
2101 PtrVt, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002102 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002103 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00002104 Callee = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVt, Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00002105 } else {
Rafael Espindolaafade352016-06-16 16:09:53 +00002106 Callee = DAG.getTargetExternalSymbol(Sym, PtrVt, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00002107 }
Evan Cheng10043e22007-01-19 07:51:42 +00002108 }
2109
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002110 // FIXME: handle tail calls differently.
2111 unsigned CallOpc;
Evan Cheng6ab54fd2009-08-01 00:16:10 +00002112 if (Subtarget->isThumb()) {
2113 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002114 CallOpc = ARMISD::CALL_NOLINK;
2115 else
Tim Northoverb5ece522016-05-10 19:17:47 +00002116 CallOpc = ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002117 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00002118 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00002119 CallOpc = ARMISD::CALL_NOLINK;
Sjoerd Meijerd906bf12016-06-03 14:03:27 +00002120 else if (doesNotRet && isDirect && Subtarget->hasRetAddrStack() &&
Sanjay Patel924879a2015-08-04 15:49:57 +00002121 // Emit regular call when code size is the priority
2122 !MF.getFunction()->optForMinSize())
Evan Cheng65f9d192012-02-28 18:51:51 +00002123 // "mov lr, pc; b _foo" to avoid confusing the RSP
2124 CallOpc = ARMISD::CALL_NOLINK;
2125 else
2126 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002127 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00002128
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002129 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00002130 Ops.push_back(Chain);
2131 Ops.push_back(Callee);
2132
2133 // Add argument registers to the end of the list so that they are known live
2134 // into the call.
2135 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2136 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2137 RegsToPass[i].second.getValueType()));
2138
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002139 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00002140 if (!isTailCall) {
2141 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00002142 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00002143 if (isThisReturn) {
2144 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00002145 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00002146 if (!Mask) {
2147 // Set isThisReturn to false if the calling convention is not one that
2148 // allows 'returned' to be modeled in this way, so LowerCallResult does
2149 // not try to pass 'this' straight through
2150 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00002151 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00002152 }
2153 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00002154 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00002155
Matthias Braunc22630e2013-10-04 16:52:54 +00002156 assert(Mask && "Missing call preserved mask for calling convention");
2157 Ops.push_back(DAG.getRegisterMask(Mask));
2158 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00002159
Gabor Greiff304a7a2008-08-28 21:40:38 +00002160 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00002161 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00002162
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002163 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00002164 if (isTailCall) {
Matthias Braun941a7052016-07-28 18:40:00 +00002165 MF.getFrameInfo().setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00002166 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00002167 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002168
Duncan Sands739a0542008-07-02 17:40:58 +00002169 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00002170 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00002171 InFlag = Chain.getValue(1);
2172
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002173 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
2174 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002175 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00002176 InFlag = Chain.getValue(1);
2177
Bob Wilsona4c22902009-04-17 19:07:39 +00002178 // Handle result values, copying them out of physregs into vregs that we
2179 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00002180 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00002181 InVals, isThisReturn,
2182 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00002183}
2184
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002185/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002186/// on the stack. Remember the next parameter register to allocate,
2187/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002188/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00002189void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
2190 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002191 assert((State->getCallOrPrologue() == Prologue ||
2192 State->getCallOrPrologue() == Call) &&
2193 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002194
Tim Northover8cda34f2015-03-11 18:54:22 +00002195 // Byval (as with any stack) slots are always at least 4 byte aligned.
2196 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002197
Tim Northover8cda34f2015-03-11 18:54:22 +00002198 unsigned Reg = State->AllocateReg(GPRArgRegs);
2199 if (!Reg)
2200 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002201
Tim Northover8cda34f2015-03-11 18:54:22 +00002202 unsigned AlignInRegs = Align / 4;
2203 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
2204 for (unsigned i = 0; i < Waste; ++i)
2205 Reg = State->AllocateReg(GPRArgRegs);
2206
2207 if (!Reg)
2208 return;
2209
2210 unsigned Excess = 4 * (ARM::R4 - Reg);
2211
2212 // Special case when NSAA != SP and parameter size greater than size of
2213 // all remained GPR regs. In that case we can't split parameter, we must
2214 // send it to stack. We also must set NCRN to R4, so waste all
2215 // remained registers.
2216 const unsigned NSAAOffset = State->getNextStackOffset();
2217 if (NSAAOffset != 0 && Size > Excess) {
2218 while (State->AllocateReg(GPRArgRegs))
2219 ;
2220 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002221 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002222
2223 // First register for byval parameter is the first register that wasn't
2224 // allocated before this method call, so it would be "reg".
2225 // If parameter is small enough to be saved in range [reg, r4), then
2226 // the end (first after last) register would be reg + param-size-in-regs,
2227 // else parameter would be splitted between registers and stack,
2228 // end register would be r4 in this case.
2229 unsigned ByValRegBegin = Reg;
2230 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
2231 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
2232 // Note, first register is allocated in the beginning of function already,
2233 // allocate remained amount of registers we need.
2234 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
2235 State->AllocateReg(GPRArgRegs);
2236 // A byval parameter that is split between registers and memory needs its
2237 // size truncated here.
2238 // In the case where the entire structure fits in registers, we set the
2239 // size in memory to zero.
2240 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002241}
2242
Dale Johannesend679ff72010-06-03 21:09:53 +00002243/// MatchingStackOffset - Return true if the given stack call argument is
2244/// already available in the same position (relatively) of the caller's
2245/// incoming argument stack.
2246static
2247bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
Matthias Braun941a7052016-07-28 18:40:00 +00002248 MachineFrameInfo &MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00002249 const TargetInstrInfo *TII) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002250 unsigned Bytes = Arg.getValueSizeInBits() / 8;
Dale Johannesend679ff72010-06-03 21:09:53 +00002251 int FI = INT_MAX;
2252 if (Arg.getOpcode() == ISD::CopyFromReg) {
2253 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00002254 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00002255 return false;
2256 MachineInstr *Def = MRI->getVRegDef(VR);
2257 if (!Def)
2258 return false;
2259 if (!Flags.isByVal()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00002260 if (!TII->isLoadFromStackSlot(*Def, FI))
Dale Johannesend679ff72010-06-03 21:09:53 +00002261 return false;
2262 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00002263 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002264 }
2265 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2266 if (Flags.isByVal())
2267 // ByVal argument is passed in as a pointer but it's now being
2268 // dereferenced. e.g.
2269 // define @foo(%struct.X* %A) {
2270 // tail call @bar(%struct.X* byval %A)
2271 // }
2272 return false;
2273 SDValue Ptr = Ld->getBasePtr();
2274 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2275 if (!FINode)
2276 return false;
2277 FI = FINode->getIndex();
2278 } else
2279 return false;
2280
2281 assert(FI != INT_MAX);
Matthias Braun941a7052016-07-28 18:40:00 +00002282 if (!MFI.isFixedObjectIndex(FI))
Dale Johannesend679ff72010-06-03 21:09:53 +00002283 return false;
Matthias Braun941a7052016-07-28 18:40:00 +00002284 return Offset == MFI.getObjectOffset(FI) && Bytes == MFI.getObjectSize(FI);
Dale Johannesend679ff72010-06-03 21:09:53 +00002285}
2286
2287/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2288/// for tail call optimization. Targets which want to do tail call
2289/// optimization should implement this function.
2290bool
2291ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2292 CallingConv::ID CalleeCC,
2293 bool isVarArg,
2294 bool isCalleeStructRet,
2295 bool isCallerStructRet,
2296 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002297 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00002298 const SmallVectorImpl<ISD::InputArg> &Ins,
2299 SelectionDAG& DAG) const {
Matthias Braun8d414362016-03-30 22:46:04 +00002300 MachineFunction &MF = DAG.getMachineFunction();
2301 const Function *CallerF = MF.getFunction();
Dale Johannesend679ff72010-06-03 21:09:53 +00002302 CallingConv::ID CallerCC = CallerF->getCallingConv();
Manman Ren4865d892016-03-18 23:41:51 +00002303
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002304 assert(Subtarget->supportsTailCall());
2305
Dale Johannesend679ff72010-06-03 21:09:53 +00002306 // Look for obvious safe cases to perform tail call optimization that do not
2307 // require ABI changes. This is what gcc calls sibcall.
2308
Jim Grosbache3864cc2010-06-16 23:45:49 +00002309 // Do not sibcall optimize vararg calls unless the call site is not passing
2310 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002311 if (isVarArg && !Outs.empty())
2312 return false;
2313
Tim Northoverd8407452013-10-01 14:33:28 +00002314 // Exception-handling functions need a special set of instructions to indicate
2315 // a return to the hardware. Tail-calling another function would probably
2316 // break this.
2317 if (CallerF->hasFnAttribute("interrupt"))
2318 return false;
2319
Dale Johannesend679ff72010-06-03 21:09:53 +00002320 // Also avoid sibcall optimization if either caller or callee uses struct
2321 // return semantics.
2322 if (isCalleeStructRet || isCallerStructRet)
2323 return false;
2324
Oliver Stannard12993dd2014-08-18 12:42:15 +00002325 // Externally-defined functions with weak linkage should not be
2326 // tail-called on ARM when the OS does not support dynamic
2327 // pre-emption of symbols, as the AAELF spec requires normal calls
2328 // to undefined weak functions to be replaced with a NOP or jump to the
2329 // next instruction. The behaviour of branch instructions in this
2330 // situation (as used for tail calls) is implementation-defined, so we
2331 // cannot rely on the linker replacing the tail call with a return.
2332 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2333 const GlobalValue *GV = G->getGlobal();
Daniel Sandersc81f4502015-06-16 15:44:21 +00002334 const Triple &TT = getTargetMachine().getTargetTriple();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002335 if (GV->hasExternalWeakLinkage() &&
2336 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002337 return false;
2338 }
2339
Matthias Braun8d414362016-03-30 22:46:04 +00002340 // Check that the call results are passed in the same way.
2341 LLVMContext &C = *DAG.getContext();
2342 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins,
2343 CCAssignFnForNode(CalleeCC, true, isVarArg),
2344 CCAssignFnForNode(CallerCC, true, isVarArg)))
2345 return false;
Matthias Braun870c34f2016-04-04 18:56:13 +00002346 // The callee has to preserve all registers the caller needs to preserve.
Matthias Braun707e02c2016-04-13 21:43:25 +00002347 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2348 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC);
Matthias Braun870c34f2016-04-04 18:56:13 +00002349 if (CalleeCC != CallerCC) {
Matthias Braun707e02c2016-04-13 21:43:25 +00002350 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC);
2351 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved))
Matthias Braun870c34f2016-04-04 18:56:13 +00002352 return false;
2353 }
Dale Johannesend679ff72010-06-03 21:09:53 +00002354
Manman Ren7e48b252012-10-12 23:39:43 +00002355 // If Caller's vararg or byval argument has been split between registers and
2356 // stack, do not perform tail call, since part of the argument is in caller's
2357 // local frame.
Matthias Braun8d414362016-03-30 22:46:04 +00002358 const ARMFunctionInfo *AFI_Caller = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002359 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002360 return false;
2361
Dale Johannesend679ff72010-06-03 21:09:53 +00002362 // If the callee takes no arguments then go on to check the results of the
2363 // call.
2364 if (!Outs.empty()) {
2365 // Check if stack adjustment is needed. For now, do not do this if any
2366 // argument is passed on the stack.
2367 SmallVector<CCValAssign, 16> ArgLocs;
Matthias Braun8d414362016-03-30 22:46:04 +00002368 ARMCCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C, Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002369 CCInfo.AnalyzeCallOperands(Outs,
2370 CCAssignFnForNode(CalleeCC, false, isVarArg));
2371 if (CCInfo.getNextStackOffset()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002372 // Check if the arguments are already laid out in the right way as
2373 // the caller's fixed stack objects.
Matthias Braun941a7052016-07-28 18:40:00 +00002374 MachineFrameInfo &MFI = MF.getFrameInfo();
Dale Johannesend679ff72010-06-03 21:09:53 +00002375 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002376 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002377 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2378 i != e;
2379 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002380 CCValAssign &VA = ArgLocs[i];
2381 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002382 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002383 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002384 if (VA.getLocInfo() == CCValAssign::Indirect)
2385 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002386 if (VA.needsCustom()) {
2387 // f64 and vector types are split into multiple registers or
2388 // register/stack-slot combinations. The types will not match
2389 // the registers; give up on memory f64 refs until we figure
2390 // out what to do about this.
2391 if (!VA.isRegLoc())
2392 return false;
2393 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002394 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002395 if (RegVT == MVT::v2f64) {
2396 if (!ArgLocs[++i].isRegLoc())
2397 return false;
2398 if (!ArgLocs[++i].isRegLoc())
2399 return false;
2400 }
2401 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002402 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2403 MFI, MRI, TII))
2404 return false;
2405 }
2406 }
2407 }
Matthias Braun707e02c2016-04-13 21:43:25 +00002408
Matthias Braun46b0f032016-04-14 01:10:42 +00002409 const MachineRegisterInfo &MRI = MF.getRegInfo();
2410 if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals))
2411 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00002412 }
2413
2414 return true;
2415}
2416
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002417bool
2418ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2419 MachineFunction &MF, bool isVarArg,
2420 const SmallVectorImpl<ISD::OutputArg> &Outs,
2421 LLVMContext &Context) const {
2422 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002423 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002424 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2425 isVarArg));
2426}
2427
Tim Northoverd8407452013-10-01 14:33:28 +00002428static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002429 const SDLoc &DL, SelectionDAG &DAG) {
Tim Northoverd8407452013-10-01 14:33:28 +00002430 const MachineFunction &MF = DAG.getMachineFunction();
2431 const Function *F = MF.getFunction();
2432
2433 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2434
2435 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2436 // version of the "preferred return address". These offsets affect the return
2437 // instruction if this is a return from PL1 without hypervisor extensions.
2438 // IRQ/FIQ: +4 "subs pc, lr, #4"
2439 // SWI: 0 "subs pc, lr, #0"
2440 // ABORT: +4 "subs pc, lr, #4"
2441 // UNDEF: +4/+2 "subs pc, lr, #0"
2442 // UNDEF varies depending on where the exception came from ARM or Thumb
2443 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2444
2445 int64_t LROffset;
2446 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2447 IntKind == "ABORT")
2448 LROffset = 4;
2449 else if (IntKind == "SWI" || IntKind == "UNDEF")
2450 LROffset = 0;
2451 else
2452 report_fatal_error("Unsupported interrupt attribute. If present, value "
2453 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2454
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002455 RetOps.insert(RetOps.begin() + 1,
2456 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002457
Craig Topper48d114b2014-04-26 18:35:24 +00002458 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002459}
2460
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002461SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002462ARMTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
2463 bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002464 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002465 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002466 const SDLoc &dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002467
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002468 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002469 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002470
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002471 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002472 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2473 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002474
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002475 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002476 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2477 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002478
Bob Wilsona4c22902009-04-17 19:07:39 +00002479 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002480 SmallVector<SDValue, 4> RetOps;
2481 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002482 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002483
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002484 MachineFunction &MF = DAG.getMachineFunction();
2485 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2486 AFI->setReturnRegsCount(RVLocs.size());
2487
Bob Wilsona4c22902009-04-17 19:07:39 +00002488 // Copy the result values into the output registers.
2489 for (unsigned i = 0, realRVLocIdx = 0;
2490 i != RVLocs.size();
2491 ++i, ++realRVLocIdx) {
2492 CCValAssign &VA = RVLocs[i];
2493 assert(VA.isRegLoc() && "Can only return in registers!");
2494
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002495 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002496
2497 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002498 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002499 case CCValAssign::Full: break;
2500 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002501 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002502 break;
2503 }
2504
Bob Wilsona4c22902009-04-17 19:07:39 +00002505 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002506 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002507 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002508 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002509 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002510 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002511 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002512
Christian Pirkerb5728192014-05-08 14:06:24 +00002513 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2514 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2515 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002516 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002517 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002518 VA = RVLocs[++i]; // skip ahead to next loc
2519 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002520 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2521 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002522 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002523 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002524 VA = RVLocs[++i]; // skip ahead to next loc
2525
2526 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002527 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002528 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002529 }
2530 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2531 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002532 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002533 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002534 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2535 fmrrd.getValue(isLittleEndian ? 0 : 1),
2536 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002537 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002538 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002539 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002540 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2541 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002542 Flag);
2543 } else
2544 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2545
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002546 // Guarantee that all emitted copies are
2547 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002548 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002549 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002550 }
Manman Ren5e9e65e2016-01-12 00:47:18 +00002551 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
2552 const MCPhysReg *I =
2553 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction());
2554 if (I) {
2555 for (; *I; ++I) {
2556 if (ARM::GPRRegClass.contains(*I))
2557 RetOps.push_back(DAG.getRegister(*I, MVT::i32));
2558 else if (ARM::DPRRegClass.contains(*I))
2559 RetOps.push_back(DAG.getRegister(*I, MVT::getFloatingPointVT(64)));
2560 else
2561 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
2562 }
2563 }
Bob Wilsona4c22902009-04-17 19:07:39 +00002564
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002565 // Update chain and glue.
2566 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002567 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002568 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002569
Tim Northoverd8407452013-10-01 14:33:28 +00002570 // CPUs which aren't M-class use a special sequence to return from
2571 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2572 // though we use "subs pc, lr, #N").
2573 //
2574 // M-class CPUs actually use a normal return sequence with a special
2575 // (hardware-provided) value in LR, so the normal code path works.
2576 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2577 !Subtarget->isMClass()) {
2578 if (Subtarget->isThumb1Only())
2579 report_fatal_error("interrupt attribute is not supported in Thumb1");
2580 return LowerInterruptReturn(RetOps, dl, DAG);
2581 }
2582
Craig Topper48d114b2014-04-26 18:35:24 +00002583 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002584}
2585
Evan Chengf8bad082012-04-10 01:51:00 +00002586bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002587 if (N->getNumValues() != 1)
2588 return false;
2589 if (!N->hasNUsesOfValue(1, 0))
2590 return false;
2591
Evan Chengf8bad082012-04-10 01:51:00 +00002592 SDValue TCChain = Chain;
2593 SDNode *Copy = *N->use_begin();
2594 if (Copy->getOpcode() == ISD::CopyToReg) {
2595 // If the copy has a glue operand, we conservatively assume it isn't safe to
2596 // perform a tail call.
2597 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2598 return false;
2599 TCChain = Copy->getOperand(0);
2600 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2601 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002602 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002603 SmallPtrSet<SDNode*, 2> Copies;
2604 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002605 UI != UE; ++UI) {
2606 if (UI->getOpcode() != ISD::CopyToReg)
2607 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002608 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002609 }
Evan Chengf8bad082012-04-10 01:51:00 +00002610 if (Copies.size() > 2)
2611 return false;
2612
2613 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2614 UI != UE; ++UI) {
2615 SDValue UseChain = UI->getOperand(0);
2616 if (Copies.count(UseChain.getNode()))
2617 // Second CopyToReg
2618 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002619 else {
2620 // We are at the top of this chain.
2621 // If the copy has a glue operand, we conservatively assume it
2622 // isn't safe to perform a tail call.
2623 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2624 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002625 // First CopyToReg
2626 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002627 }
Evan Chengf8bad082012-04-10 01:51:00 +00002628 }
2629 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002630 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002631 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002632 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002633 Copy = *Copy->use_begin();
2634 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002635 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002636 // If the copy has a glue operand, we conservatively assume it isn't safe to
2637 // perform a tail call.
2638 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2639 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002640 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002641 } else {
2642 return false;
2643 }
2644
Evan Cheng419ea282010-12-01 22:59:46 +00002645 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002646 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2647 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002648 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2649 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002650 return false;
2651 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002652 }
2653
Evan Chengf8bad082012-04-10 01:51:00 +00002654 if (!HasRet)
2655 return false;
2656
2657 Chain = TCChain;
2658 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002659}
2660
Evan Cheng0663f232011-03-21 01:19:09 +00002661bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002662 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002663 return false;
2664
Akira Hatanakad9699bc2015-06-09 19:07:19 +00002665 auto Attr =
2666 CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2667 if (!CI->isTailCall() || Attr.getValueAsString() == "true")
Evan Cheng0663f232011-03-21 01:19:09 +00002668 return false;
2669
Artyom Skrobovad8a0632015-09-28 09:44:11 +00002670 return true;
Evan Cheng0663f232011-03-21 01:19:09 +00002671}
2672
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002673// Trying to write a 64 bit value so need to split into two 32 bit values first,
2674// and pass the lower and high parts through.
2675static SDValue LowerWRITE_REGISTER(SDValue Op, SelectionDAG &DAG) {
2676 SDLoc DL(Op);
2677 SDValue WriteValue = Op->getOperand(2);
2678
2679 // This function is only supposed to be called for i64 type argument.
2680 assert(WriteValue.getValueType() == MVT::i64
2681 && "LowerWRITE_REGISTER called for non-i64 type argument.");
2682
2683 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2684 DAG.getConstant(0, DL, MVT::i32));
2685 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, WriteValue,
2686 DAG.getConstant(1, DL, MVT::i32));
2687 SDValue Ops[] = { Op->getOperand(0), Op->getOperand(1), Lo, Hi };
2688 return DAG.getNode(ISD::WRITE_REGISTER, DL, MVT::Other, Ops);
2689}
2690
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002691// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2692// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2693// one of the above mentioned nodes. It has to be wrapped because otherwise
2694// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2695// be used to form addressing mode. These wrapped nodes will be selected
2696// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002697static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002698 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002699 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002700 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002701 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002702 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002703 if (CP->isMachineConstantPoolEntry())
2704 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2705 CP->getAlignment());
2706 else
2707 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2708 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002709 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002710}
2711
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002712unsigned ARMTargetLowering::getJumpTableEncoding() const {
2713 return MachineJumpTableInfo::EK_Inline;
2714}
2715
Dan Gohman21cea8a2010-04-17 15:26:15 +00002716SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2717 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002718 MachineFunction &MF = DAG.getMachineFunction();
2719 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2720 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002721 SDLoc DL(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00002722 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002723 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002724 SDValue CPAddr;
Oliver Stannard8331aae2016-08-08 15:28:31 +00002725 bool IsPositionIndependent = isPositionIndependent() || Subtarget->isROPI();
Rafael Espindola0f898332016-06-20 16:43:17 +00002726 if (!IsPositionIndependent) {
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002727 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2728 } else {
2729 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002730 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002731 ARMConstantPoolValue *CPV =
2732 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2733 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002734 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2735 }
2736 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
Justin Lebar9c375812016-07-15 18:27:10 +00002737 SDValue Result = DAG.getLoad(
2738 PtrVT, DL, DAG.getEntryNode(), CPAddr,
2739 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Rafael Espindola0f898332016-06-20 16:43:17 +00002740 if (!IsPositionIndependent)
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002741 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002742 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002743 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002744}
2745
Tim Northoverbd41cf82016-01-07 09:03:03 +00002746/// \brief Convert a TLS address reference into the correct sequence of loads
2747/// and calls to compute the variable's address for Darwin, and return an
2748/// SDValue containing the final node.
2749
2750/// Darwin only has one TLS scheme which must be capable of dealing with the
2751/// fully general situation, in the worst case. This means:
2752/// + "extern __thread" declaration.
2753/// + Defined in a possibly unknown dynamic library.
2754///
2755/// The general system is that each __thread variable has a [3 x i32] descriptor
2756/// which contains information used by the runtime to calculate the address. The
2757/// only part of this the compiler needs to know about is the first word, which
2758/// contains a function pointer that must be called with the address of the
2759/// entire descriptor in "r0".
2760///
2761/// Since this descriptor may be in a different unit, in general access must
2762/// proceed along the usual ARM rules. A common sequence to produce is:
2763///
2764/// movw rT1, :lower16:_var$non_lazy_ptr
2765/// movt rT1, :upper16:_var$non_lazy_ptr
2766/// ldr r0, [rT1]
2767/// ldr rT2, [r0]
2768/// blx rT2
2769/// [...address now in r0...]
2770SDValue
2771ARMTargetLowering::LowerGlobalTLSAddressDarwin(SDValue Op,
2772 SelectionDAG &DAG) const {
2773 assert(Subtarget->isTargetDarwin() && "TLS only supported on Darwin");
2774 SDLoc DL(Op);
2775
2776 // First step is to get the address of the actua global symbol. This is where
2777 // the TLS descriptor lives.
2778 SDValue DescAddr = LowerGlobalAddressDarwin(Op, DAG);
2779
2780 // The first entry in the descriptor is a function pointer that we must call
2781 // to obtain the address of the variable.
2782 SDValue Chain = DAG.getEntryNode();
Justin Lebaradbf09e2016-09-11 01:38:58 +00002783 SDValue FuncTLVGet = DAG.getLoad(
2784 MVT::i32, DL, Chain, DescAddr,
2785 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2786 /* Alignment = */ 4,
2787 MachineMemOperand::MONonTemporal | MachineMemOperand::MODereferenceable |
2788 MachineMemOperand::MOInvariant);
Tim Northoverbd41cf82016-01-07 09:03:03 +00002789 Chain = FuncTLVGet.getValue(1);
2790
2791 MachineFunction &F = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002792 MachineFrameInfo &MFI = F.getFrameInfo();
2793 MFI.setAdjustsStack(true);
Tim Northoverbd41cf82016-01-07 09:03:03 +00002794
2795 // TLS calls preserve all registers except those that absolutely must be
2796 // trashed: R0 (it takes an argument), LR (it's a call) and CPSR (let's not be
2797 // silly).
2798 auto TRI =
2799 getTargetMachine().getSubtargetImpl(*F.getFunction())->getRegisterInfo();
2800 auto ARI = static_cast<const ARMRegisterInfo *>(TRI);
2801 const uint32_t *Mask = ARI->getTLSCallPreservedMask(DAG.getMachineFunction());
2802
2803 // Finally, we can make the call. This is just a degenerate version of a
2804 // normal AArch64 call node: r0 takes the address of the descriptor, and
2805 // returns the address of the variable in this thread.
2806 Chain = DAG.getCopyToReg(Chain, DL, ARM::R0, DescAddr, SDValue());
2807 Chain =
2808 DAG.getNode(ARMISD::CALL, DL, DAG.getVTList(MVT::Other, MVT::Glue),
2809 Chain, FuncTLVGet, DAG.getRegister(ARM::R0, MVT::i32),
2810 DAG.getRegisterMask(Mask), Chain.getValue(1));
2811 return DAG.getCopyFromReg(Chain, DL, ARM::R0, MVT::i32, Chain.getValue(1));
2812}
2813
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002814SDValue
2815ARMTargetLowering::LowerGlobalTLSAddressWindows(SDValue Op,
2816 SelectionDAG &DAG) const {
2817 assert(Subtarget->isTargetWindows() && "Windows specific TLS lowering");
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002818
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002819 SDValue Chain = DAG.getEntryNode();
2820 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2821 SDLoc DL(Op);
2822
2823 // Load the current TEB (thread environment block)
2824 SDValue Ops[] = {Chain,
2825 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
2826 DAG.getConstant(15, DL, MVT::i32),
2827 DAG.getConstant(0, DL, MVT::i32),
2828 DAG.getConstant(13, DL, MVT::i32),
2829 DAG.getConstant(0, DL, MVT::i32),
2830 DAG.getConstant(2, DL, MVT::i32)};
2831 SDValue CurrentTEB = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
2832 DAG.getVTList(MVT::i32, MVT::Other), Ops);
2833
2834 SDValue TEB = CurrentTEB.getValue(0);
2835 Chain = CurrentTEB.getValue(1);
2836
2837 // Load the ThreadLocalStoragePointer from the TEB
2838 // A pointer to the TLS array is located at offset 0x2c from the TEB.
2839 SDValue TLSArray =
2840 DAG.getNode(ISD::ADD, DL, PtrVT, TEB, DAG.getIntPtrConstant(0x2c, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00002841 TLSArray = DAG.getLoad(PtrVT, DL, Chain, TLSArray, MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002842
2843 // The pointer to the thread's TLS data area is at the TLS Index scaled by 4
2844 // offset into the TLSArray.
2845
2846 // Load the TLS index from the C runtime
2847 SDValue TLSIndex =
2848 DAG.getTargetExternalSymbol("_tls_index", PtrVT, ARMII::MO_NO_FLAG);
2849 TLSIndex = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, TLSIndex);
Justin Lebar9c375812016-07-15 18:27:10 +00002850 TLSIndex = DAG.getLoad(PtrVT, DL, Chain, TLSIndex, MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002851
2852 SDValue Slot = DAG.getNode(ISD::SHL, DL, PtrVT, TLSIndex,
2853 DAG.getConstant(2, DL, MVT::i32));
2854 SDValue TLS = DAG.getLoad(PtrVT, DL, Chain,
2855 DAG.getNode(ISD::ADD, DL, PtrVT, TLSArray, Slot),
Justin Lebar9c375812016-07-15 18:27:10 +00002856 MachinePointerInfo());
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002857
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002858 // Get the offset of the start of the .tls section (section base)
2859 const auto *GA = cast<GlobalAddressSDNode>(Op);
2860 auto *CPV = ARMConstantPoolConstant::Create(GA->getGlobal(), ARMCP::SECREL);
Justin Lebar9c375812016-07-15 18:27:10 +00002861 SDValue Offset = DAG.getLoad(
2862 PtrVT, DL, Chain, DAG.getNode(ARMISD::Wrapper, DL, MVT::i32,
2863 DAG.getTargetConstantPool(CPV, PtrVT, 4)),
2864 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Saleem Abdulrasool532dcbc2016-06-07 03:15:07 +00002865
2866 return DAG.getNode(ISD::ADD, DL, PtrVT, TLS, Offset);
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002867}
2868
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002869// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002870SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002871ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002872 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002873 SDLoc dl(GA);
Mehdi Amini44ede332015-07-09 02:09:04 +00002874 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002875 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002876 MachineFunction &MF = DAG.getMachineFunction();
2877 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002878 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002879 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002880 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2881 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002882 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002883 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Justin Lebar9c375812016-07-15 18:27:10 +00002884 Argument = DAG.getLoad(
2885 PtrVT, dl, DAG.getEntryNode(), Argument,
2886 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002887 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002888
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002889 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002890 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002891
2892 // call __tls_get_addr.
2893 ArgListTy Args;
2894 ArgListEntry Entry;
2895 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002896 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002897 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002898
Dale Johannesen555a3752009-01-30 23:10:59 +00002899 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002900 TargetLowering::CallLoweringInfo CLI(DAG);
2901 CLI.setDebugLoc(dl).setChain(Chain)
2902 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00002903 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args));
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002904
Justin Holewinskiaa583972012-05-25 16:35:28 +00002905 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002906 return CallResult.first;
2907}
2908
2909// Lower ISD::GlobalTLSAddress using the "initial exec" or
2910// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002911SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002912ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002913 SelectionDAG &DAG,
2914 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002915 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002916 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002917 SDValue Offset;
2918 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002919 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002920 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002921 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002922
Hans Wennborgaea41202012-05-04 09:40:39 +00002923 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002924 MachineFunction &MF = DAG.getMachineFunction();
2925 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002926 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002927 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002928 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2929 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002930 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2931 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2932 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002933 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002934 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002935 Offset = DAG.getLoad(
2936 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002937 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002938 Chain = Offset.getValue(1);
2939
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002940 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002941 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002942
Alex Lorenze40c8a22015-08-11 23:09:45 +00002943 Offset = DAG.getLoad(
2944 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002945 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002946 } else {
2947 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002948 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002949 ARMConstantPoolValue *CPV =
2950 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002951 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002952 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002953 Offset = DAG.getLoad(
2954 PtrVT, dl, Chain, Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002955 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002956 }
2957
2958 // The address of the thread local variable is the add of the thread
2959 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002960 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002961}
2962
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002963SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002964ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Tim Northoverbd41cf82016-01-07 09:03:03 +00002965 if (Subtarget->isTargetDarwin())
2966 return LowerGlobalTLSAddressDarwin(Op, DAG);
2967
Saleem Abdulrasoolf36005a2016-02-03 18:21:59 +00002968 if (Subtarget->isTargetWindows())
2969 return LowerGlobalTLSAddressWindows(Op, DAG);
2970
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002971 // TODO: implement the "local dynamic" model
Tim Northoverbd41cf82016-01-07 09:03:03 +00002972 assert(Subtarget->isTargetELF() && "Only ELF implemented here");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002973 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002974 if (DAG.getTarget().Options.EmulatedTLS)
2975 return LowerToTLSEmulatedModel(GA, DAG);
Hans Wennborgaea41202012-05-04 09:40:39 +00002976
2977 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2978
2979 switch (model) {
2980 case TLSModel::GeneralDynamic:
2981 case TLSModel::LocalDynamic:
2982 return LowerToTLSGeneralDynamicModel(GA, DAG);
2983 case TLSModel::InitialExec:
2984 case TLSModel::LocalExec:
2985 return LowerToTLSExecModels(GA, DAG, model);
2986 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002987 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002988}
2989
James Molloy9abb2fa2016-09-26 07:26:24 +00002990/// Return true if all users of V are within function F, looking through
2991/// ConstantExprs.
2992static bool allUsersAreInFunction(const Value *V, const Function *F) {
2993 SmallVector<const User*,4> Worklist;
2994 for (auto *U : V->users())
2995 Worklist.push_back(U);
2996 while (!Worklist.empty()) {
2997 auto *U = Worklist.pop_back_val();
2998 if (isa<ConstantExpr>(U)) {
2999 for (auto *UU : U->users())
3000 Worklist.push_back(UU);
3001 continue;
3002 }
3003
3004 auto *I = dyn_cast<Instruction>(U);
3005 if (!I || I->getParent()->getParent() != F)
3006 return false;
3007 }
3008 return true;
3009}
3010
3011/// Return true if all users of V are within some (any) function, looking through
3012/// ConstantExprs. In other words, are there any global constant users?
3013static bool allUsersAreInFunctions(const Value *V) {
3014 SmallVector<const User*,4> Worklist;
3015 for (auto *U : V->users())
3016 Worklist.push_back(U);
3017 while (!Worklist.empty()) {
3018 auto *U = Worklist.pop_back_val();
3019 if (isa<ConstantExpr>(U)) {
3020 for (auto *UU : U->users())
3021 Worklist.push_back(UU);
3022 continue;
3023 }
3024
3025 if (!isa<Instruction>(U))
3026 return false;
3027 }
3028 return true;
3029}
3030
3031// Return true if T is an integer, float or an array/vector of either.
3032static bool isSimpleType(Type *T) {
3033 if (T->isIntegerTy() || T->isFloatingPointTy())
3034 return true;
3035 Type *SubT = nullptr;
3036 if (T->isArrayTy())
3037 SubT = T->getArrayElementType();
3038 else if (T->isVectorTy())
3039 SubT = T->getVectorElementType();
3040 else
3041 return false;
3042 return SubT->isIntegerTy() || SubT->isFloatingPointTy();
3043}
3044
3045static SDValue promoteToConstantPool(const GlobalValue *GV, SelectionDAG &DAG,
3046 EVT PtrVT, SDLoc dl) {
3047 // If we're creating a pool entry for a constant global with unnamed address,
3048 // and the global is small enough, we can emit it inline into the constant pool
3049 // to save ourselves an indirection.
3050 //
3051 // This is a win if the constant is only used in one function (so it doesn't
3052 // need to be duplicated) or duplicating the constant wouldn't increase code
3053 // size (implying the constant is no larger than 4 bytes).
3054 const Function *F = DAG.getMachineFunction().getFunction();
3055
3056 // We rely on this decision to inline being idemopotent and unrelated to the
3057 // use-site. We know that if we inline a variable at one use site, we'll
3058 // inline it elsewhere too (and reuse the constant pool entry). Fast-isel
3059 // doesn't know about this optimization, so bail out if it's enabled else
3060 // we could decide to inline here (and thus never emit the GV) but require
3061 // the GV from fast-isel generated code.
3062 if (!EnableConstpoolPromotion ||
3063 DAG.getMachineFunction().getTarget().Options.EnableFastISel)
3064 return SDValue();
3065
3066 auto *GVar = dyn_cast<GlobalVariable>(GV);
3067 if (!GVar || !GVar->hasInitializer() ||
3068 !GVar->isConstant() || !GVar->hasGlobalUnnamedAddr() ||
3069 !GVar->hasLocalLinkage())
3070 return SDValue();
3071
3072 // Ensure that we don't try and inline any type that contains pointers. If
3073 // we inline a value that contains relocations, we move the relocations from
3074 // .data to .text which is not ideal.
3075 auto *Init = GVar->getInitializer();
3076 if (!isSimpleType(Init->getType()))
3077 return SDValue();
3078
3079 // The constant islands pass can only really deal with alignment requests
3080 // <= 4 bytes and cannot pad constants itself. Therefore we cannot promote
3081 // any type wanting greater alignment requirements than 4 bytes. We also
3082 // can only promote constants that are multiples of 4 bytes in size or
3083 // are paddable to a multiple of 4. Currently we only try and pad constants
3084 // that are strings for simplicity.
3085 auto *CDAInit = dyn_cast<ConstantDataArray>(Init);
3086 unsigned Size = DAG.getDataLayout().getTypeAllocSize(Init->getType());
James Molloy6215fad2016-10-06 07:56:00 +00003087 unsigned Align = GVar->getAlignment();
James Molloy9abb2fa2016-09-26 07:26:24 +00003088 unsigned RequiredPadding = 4 - (Size % 4);
3089 bool PaddingPossible =
3090 RequiredPadding == 4 || (CDAInit && CDAInit->isString());
3091 if (!PaddingPossible || Align > 4 || Size > ConstpoolPromotionMaxSize)
3092 return SDValue();
3093
3094 unsigned PaddedSize = Size + ((RequiredPadding == 4) ? 0 : RequiredPadding);
3095 MachineFunction &MF = DAG.getMachineFunction();
3096 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3097
3098 // We can't bloat the constant pool too much, else the ConstantIslands pass
3099 // may fail to converge. If we haven't promoted this global yet (it may have
3100 // multiple uses), and promoting it would increase the constant pool size (Sz
3101 // > 4), ensure we have space to do so up to MaxTotal.
3102 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar) && Size > 4)
3103 if (AFI->getPromotedConstpoolIncrease() + PaddedSize - 4 >=
3104 ConstpoolPromotionMaxTotal)
3105 return SDValue();
3106
3107 // This is only valid if all users are in a single function OR it has users
3108 // in multiple functions but it no larger than a pointer. We also check if
3109 // GVar has constant (non-ConstantExpr) users. If so, it essentially has its
3110 // address taken.
3111 if (!allUsersAreInFunction(GVar, F) &&
3112 !(Size <= 4 && allUsersAreInFunctions(GVar)))
3113 return SDValue();
3114
3115 // We're going to inline this global. Pad it out if needed.
3116 if (RequiredPadding != 4) {
3117 StringRef S = CDAInit->getAsString();
3118
3119 SmallVector<uint8_t,16> V(S.size());
3120 std::copy(S.bytes_begin(), S.bytes_end(), V.begin());
3121 while (RequiredPadding--)
3122 V.push_back(0);
3123 Init = ConstantDataArray::get(*DAG.getContext(), V);
3124 }
3125
3126 auto CPVal = ARMConstantPoolConstant::Create(GVar, Init);
3127 SDValue CPAddr =
3128 DAG.getTargetConstantPool(CPVal, PtrVT, /*Align=*/4);
3129 if (!AFI->getGlobalsPromotedToConstantPool().count(GVar)) {
3130 AFI->markGlobalAsPromotedToConstantPool(GVar);
3131 AFI->setPromotedConstpoolIncrease(AFI->getPromotedConstpoolIncrease() +
3132 PaddedSize - 4);
3133 }
3134 ++NumConstpoolPromoted;
3135 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3136}
3137
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003138SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003139 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00003140 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003141 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003142 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Rafael Espindola7ad97b22016-05-31 15:31:55 +00003143 const TargetMachine &TM = getTargetMachine();
Oliver Stannard8331aae2016-08-08 15:28:31 +00003144 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
3145 GV = GA->getBaseObject();
3146 bool IsRO =
3147 (isa<GlobalVariable>(GV) && cast<GlobalVariable>(GV)->isConstant()) ||
3148 isa<Function>(GV);
James Molloy9abb2fa2016-09-26 07:26:24 +00003149
3150 if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV))
3151 if (SDValue V = promoteToConstantPool(GV, DAG, PtrVT, dl))
3152 return V;
3153
Rafael Espindola0f898332016-06-20 16:43:17 +00003154 if (isPositionIndependent()) {
Rafael Espindola3beef8d2016-06-27 23:15:57 +00003155 bool UseGOT_PREL = !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Peter Collingbourne97aae402015-10-26 18:23:16 +00003156
3157 MachineFunction &MF = DAG.getMachineFunction();
3158 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3159 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
3160 EVT PtrVT = getPointerTy(DAG.getDataLayout());
3161 SDLoc dl(Op);
3162 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
3163 ARMConstantPoolValue *CPV = ARMConstantPoolConstant::Create(
3164 GV, ARMPCLabelIndex, ARMCP::CPValue, PCAdj,
3165 UseGOT_PREL ? ARMCP::GOT_PREL : ARMCP::no_modifier,
3166 /*AddCurrentAddress=*/UseGOT_PREL);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00003167 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00003168 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003169 SDValue Result = DAG.getLoad(
3170 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003171 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003172 SDValue Chain = Result.getValue(1);
Peter Collingbourne97aae402015-10-26 18:23:16 +00003173 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
3174 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
3175 if (UseGOT_PREL)
Justin Lebar9c375812016-07-15 18:27:10 +00003176 Result =
3177 DAG.getLoad(PtrVT, dl, Chain, Result,
3178 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00003179 return Result;
Oliver Stannard8331aae2016-08-08 15:28:31 +00003180 } else if (Subtarget->isROPI() && IsRO) {
3181 // PC-relative.
3182 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT);
3183 SDValue Result = DAG.getNode(ARMISD::WrapperPIC, dl, PtrVT, G);
3184 return Result;
3185 } else if (Subtarget->isRWPI() && !IsRO) {
3186 // SB-relative.
3187 ARMConstantPoolValue *CPV =
3188 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL);
3189 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
3190 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
3191 SDValue G = DAG.getLoad(
3192 PtrVT, dl, DAG.getEntryNode(), CPAddr,
3193 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
3194 SDValue SB = DAG.getCopyFromReg(DAG.getEntryNode(), dl, ARM::R9, PtrVT);
3195 SDValue Result = DAG.getNode(ISD::ADD, dl, PtrVT, SB, G);
3196 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00003197 }
3198
3199 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00003200 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00003201 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00003202 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00003203 // FIXME: Once remat is capable of dealing with instructions with register
3204 // operands, expand this into two nodes.
3205 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
3206 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00003207 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00003208 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
3209 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003210 return DAG.getLoad(
3211 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003212 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00003213 }
3214}
3215
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003216SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003217 SelectionDAG &DAG) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00003218 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
3219 "ROPI/RWPI not currently supported for Darwin");
Mehdi Amini44ede332015-07-09 02:09:04 +00003220 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003221 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003222 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengdfce83c2011-01-17 08:03:18 +00003223
Eric Christopherc1058df2014-07-04 01:55:26 +00003224 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00003225 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00003226
Tim Northover72360d22013-12-02 10:35:41 +00003227 // FIXME: Once remat is capable of dealing with instructions with register
3228 // operands, expand this into multiple nodes
3229 unsigned Wrapper =
Rafael Espindola0f898332016-06-20 16:43:17 +00003230 isPositionIndependent() ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00003231
Tim Northover72360d22013-12-02 10:35:41 +00003232 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
3233 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00003234
Rafael Espindola5ac8f5c2016-06-28 15:38:13 +00003235 if (Subtarget->isGVIndirectSymbol(GV))
Tim Northover72360d22013-12-02 10:35:41 +00003236 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00003237 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Evan Cheng10043e22007-01-19 07:51:42 +00003238 return Result;
3239}
3240
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003241SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
3242 SelectionDAG &DAG) const {
3243 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00003244 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
3245 "Windows on ARM expects to use movw/movt");
Oliver Stannard8331aae2016-08-08 15:28:31 +00003246 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
3247 "ROPI/RWPI not currently supported for Windows");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003248
3249 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Reid Klecknerc35e7f52015-06-11 01:31:48 +00003250 const ARMII::TOF TargetFlags =
3251 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Mehdi Amini44ede332015-07-09 02:09:04 +00003252 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003253 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003254 SDLoc DL(Op);
3255
3256 ++NumMovwMovt;
3257
3258 // FIXME: Once remat is capable of dealing with instructions with register
3259 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003260 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
3261 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
Reid Klecknerc35e7f52015-06-11 01:31:48 +00003262 TargetFlags));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003263 if (GV->hasDLLImportStorageClass())
3264 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00003265 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00003266 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00003267}
3268
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003269SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00003270ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003271 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003272 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00003273 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
3274 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00003275 Op.getOperand(1), Val);
3276}
3277
3278SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003279ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003280 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003281 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003282 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003283}
3284
Matthias Braun3cd00c12015-07-16 22:34:16 +00003285SDValue ARMTargetLowering::LowerEH_SJLJ_SETUP_DISPATCH(SDValue Op,
3286 SelectionDAG &DAG) const {
3287 SDLoc dl(Op);
3288 return DAG.getNode(ARMISD::EH_SJLJ_SETUP_DISPATCH, dl, MVT::Other,
3289 Op.getOperand(0));
3290}
3291
Jim Grosbachbd9485d2010-05-22 01:06:18 +00003292SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00003293ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00003294 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003295 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003296 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003297 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003298 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00003299 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00003300 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00003301 "RBIT intrinsic must have i32 type!");
James Molloyb5640982015-11-13 16:05:22 +00003302 return DAG.getNode(ISD::BITREVERSE, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00003303 }
Marcin Koscielnicki3fdc2572016-04-19 20:51:05 +00003304 case Intrinsic::thread_pointer: {
Mehdi Amini44ede332015-07-09 02:09:04 +00003305 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilson17f88782009-08-04 00:25:01 +00003306 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
3307 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00003308 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00003309 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00003310 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00003311 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Mehdi Amini44ede332015-07-09 02:09:04 +00003312 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Jim Grosbach693e36a2009-08-11 00:09:57 +00003313 SDValue CPAddr;
Rafael Espindola0f898332016-06-20 16:43:17 +00003314 bool IsPositionIndependent = isPositionIndependent();
3315 unsigned PCAdj = IsPositionIndependent ? (Subtarget->isThumb() ? 4 : 8) : 0;
Jim Grosbach693e36a2009-08-11 00:09:57 +00003316 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00003317 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
3318 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00003319 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00003320 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Alex Lorenze40c8a22015-08-11 23:09:45 +00003321 SDValue Result = DAG.getLoad(
3322 PtrVT, dl, DAG.getEntryNode(), CPAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003323 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Jim Grosbach693e36a2009-08-11 00:09:57 +00003324
Rafael Espindola0f898332016-06-20 16:43:17 +00003325 if (IsPositionIndependent) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003326 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00003327 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
3328 }
3329 return Result;
3330 }
Evan Cheng18381b42011-03-29 23:06:19 +00003331 case Intrinsic::arm_neon_vmulls:
3332 case Intrinsic::arm_neon_vmullu: {
3333 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
3334 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003335 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00003336 Op.getOperand(1), Op.getOperand(2));
3337 }
James Molloyee868b22015-08-11 12:06:25 +00003338 case Intrinsic::arm_neon_vminnm:
3339 case Intrinsic::arm_neon_vmaxnm: {
3340 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminnm)
3341 ? ISD::FMINNUM : ISD::FMAXNUM;
3342 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3343 Op.getOperand(1), Op.getOperand(2));
3344 }
Silviu Barangaad1b19f2015-08-19 14:11:27 +00003345 case Intrinsic::arm_neon_vminu:
3346 case Intrinsic::arm_neon_vmaxu: {
3347 if (Op.getValueType().isFloatingPoint())
3348 return SDValue();
3349 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vminu)
3350 ? ISD::UMIN : ISD::UMAX;
3351 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3352 Op.getOperand(1), Op.getOperand(2));
3353 }
James Molloyd616c642015-08-11 12:06:28 +00003354 case Intrinsic::arm_neon_vmins:
3355 case Intrinsic::arm_neon_vmaxs: {
3356 // v{min,max}s is overloaded between signed integers and floats.
Silviu Barangaad1b19f2015-08-19 14:11:27 +00003357 if (!Op.getValueType().isFloatingPoint()) {
3358 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3359 ? ISD::SMIN : ISD::SMAX;
3360 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3361 Op.getOperand(1), Op.getOperand(2));
3362 }
James Molloyd616c642015-08-11 12:06:28 +00003363 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmins)
3364 ? ISD::FMINNAN : ISD::FMAXNAN;
3365 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
3366 Op.getOperand(1), Op.getOperand(2));
3367 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00003368 }
3369}
3370
Eli Friedman30a49e92011-08-03 21:06:02 +00003371static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
3372 const ARMSubtarget *Subtarget) {
3373 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003374 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00003375 if (!Subtarget->hasDataBarrier()) {
3376 // Some ARMv6 cpus can support data barriers with an mcr instruction.
3377 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
3378 // here.
3379 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00003380 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00003381 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003382 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003383 }
3384
Tim Northover36b24172013-07-03 09:20:36 +00003385 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
3386 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00003387 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00003388 if (Subtarget->isMClass()) {
3389 // Only a full system barrier exists in the M-class architectures.
3390 Domain = ARM_MB::SY;
Diana Picusc5baa432016-06-23 07:47:35 +00003391 } else if (Subtarget->preferISHSTBarriers() &&
3392 Ord == AtomicOrdering::Release) {
Tim Northover36b24172013-07-03 09:20:36 +00003393 // Swift happens to implement ISHST barriers in a way that's compatible with
3394 // Release semantics but weaker than ISH so we'd be fools not to use
3395 // it. Beware: other processors probably don't!
3396 Domain = ARM_MB::ISHST;
3397 }
3398
Joey Gouly926d3f52013-09-05 15:35:24 +00003399 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003400 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
3401 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00003402}
3403
Evan Cheng8740ee32010-11-03 06:34:55 +00003404static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
3405 const ARMSubtarget *Subtarget) {
3406 // ARM pre v5TE and Thumb1 does not have preload instructions.
3407 if (!(Subtarget->isThumb2() ||
3408 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
3409 // Just preserve the chain.
3410 return Op.getOperand(0);
3411
Andrew Trickef9de2a2013-05-25 02:42:55 +00003412 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00003413 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
3414 if (!isRead &&
3415 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
3416 // ARMv7 with MP extension has PLDW.
3417 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00003418
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003419 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3420 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00003421 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00003422 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00003423 isData = ~isData & 1;
3424 }
Evan Cheng8740ee32010-11-03 06:34:55 +00003425
3426 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003427 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
3428 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00003429}
3430
Dan Gohman31ae5862010-04-17 14:41:14 +00003431static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
3432 MachineFunction &MF = DAG.getMachineFunction();
3433 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
3434
Evan Cheng10043e22007-01-19 07:51:42 +00003435 // vastart just stores the address of the VarArgsFrameIndex slot into the
3436 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003437 SDLoc dl(Op);
Mehdi Amini44ede332015-07-09 02:09:04 +00003438 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
Dan Gohman31ae5862010-04-17 14:41:14 +00003439 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00003440 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00003441 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
Justin Lebar9c375812016-07-15 18:27:10 +00003442 MachinePointerInfo(SV));
Evan Cheng10043e22007-01-19 07:51:42 +00003443}
3444
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003445SDValue ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA,
3446 CCValAssign &NextVA,
3447 SDValue &Root,
3448 SelectionDAG &DAG,
3449 const SDLoc &dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00003450 MachineFunction &MF = DAG.getMachineFunction();
3451 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3452
Craig Topper760b1342012-02-22 05:59:10 +00003453 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00003454 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00003455 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003456 else
Craig Topperc7242e02012-04-20 07:30:17 +00003457 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003458
3459 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003460 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003461 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003462
3463 SDValue ArgValue2;
3464 if (NextVA.isMemLoc()) {
Matthias Braun941a7052016-07-28 18:40:00 +00003465 MachineFrameInfo &MFI = MF.getFrameInfo();
3466 int FI = MFI.CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00003467
3468 // Create load node to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003469 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Alex Lorenze40c8a22015-08-11 23:09:45 +00003470 ArgValue2 = DAG.getLoad(
3471 MVT::i32, dl, Root, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00003472 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI));
Bob Wilson2e076c42009-06-22 23:27:02 +00003473 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00003474 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00003475 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00003476 }
Christian Pirkerb5728192014-05-08 14:06:24 +00003477 if (!Subtarget->isLittle())
3478 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003479 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00003480}
3481
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003482// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00003483// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003484// byval). Either way, we allocate stack slots adjacent to the data
3485// provided by our caller, and store the unallocated registers there.
3486// If this is a variadic function, the va_list pointer will begin with
3487// these values; otherwise, this reassembles a (byval) structure that
3488// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003489// Return: The frame index registers were stored into.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003490int ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
3491 const SDLoc &dl, SDValue &Chain,
3492 const Value *OrigArg,
3493 unsigned InRegsParamRecordIdx,
3494 int ArgOffset, unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003495 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00003496 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003497 // Setup first unallocated register as first byval register;
3498 // eat all remained registers
3499 // (these two actions are performed by HandleByVal method).
3500 // Then, here, we initialize stack frame with
3501 // "store-reg" instructions.
3502 // Case #2. Var-args function, that doesn't contain byval parameters.
3503 // The same: eat all remained unallocated registers,
3504 // initialize stack frame.
3505
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003506 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003507 MachineFrameInfo &MFI = MF.getFrameInfo();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003508 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003509 unsigned RBegin, REnd;
3510 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
3511 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003512 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00003513 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00003514 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00003515 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003516 }
3517
Tim Northover8cda34f2015-03-11 18:54:22 +00003518 if (REnd != RBegin)
3519 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003520
Mehdi Amini44ede332015-07-09 02:09:04 +00003521 auto PtrVT = getPointerTy(DAG.getDataLayout());
Matthias Braun941a7052016-07-28 18:40:00 +00003522 int FrameIndex = MFI.CreateFixedObject(ArgSize, ArgOffset, false);
Mehdi Amini44ede332015-07-09 02:09:04 +00003523 SDValue FIN = DAG.getFrameIndex(FrameIndex, PtrVT);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003524
Tim Northover8cda34f2015-03-11 18:54:22 +00003525 SmallVector<SDValue, 4> MemOps;
3526 const TargetRegisterClass *RC =
3527 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003528
Tim Northover8cda34f2015-03-11 18:54:22 +00003529 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
3530 unsigned VReg = MF.addLiveIn(Reg, RC);
3531 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Justin Lebar9c375812016-07-15 18:27:10 +00003532 SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN,
3533 MachinePointerInfo(OrigArg, 4 * i));
Tim Northover8cda34f2015-03-11 18:54:22 +00003534 MemOps.push_back(Store);
Mehdi Amini44ede332015-07-09 02:09:04 +00003535 FIN = DAG.getNode(ISD::ADD, dl, PtrVT, FIN, DAG.getConstant(4, dl, PtrVT));
Oliver Stannardd55e1152014-03-05 15:25:27 +00003536 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003537
3538 if (!MemOps.empty())
3539 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
3540 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003541}
3542
3543// Setup stack frame, the va_list pointer will start from.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003544void ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
3545 const SDLoc &dl, SDValue &Chain,
3546 unsigned ArgOffset,
3547 unsigned TotalArgRegsSaveSize,
3548 bool ForceMutable) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003549 MachineFunction &MF = DAG.getMachineFunction();
3550 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3551
3552 // Try to store any remaining integer argument regs
Nick Lewycky99800752016-06-28 01:45:05 +00003553 // to their spots on the stack so that they may be loaded by dereferencing
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003554 // the result of va_next.
3555 // If there is no regs to be stored, just point address after last
3556 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00003557 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3558 CCInfo.getInRegsParamsCount(),
3559 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003560 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003561}
3562
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003563SDValue ARMTargetLowering::LowerFormalArguments(
3564 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
3565 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl,
3566 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003567 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00003568 MachineFrameInfo &MFI = MF.getFrameInfo();
Bob Wilsona4c22902009-04-17 19:07:39 +00003569
Bob Wilsona4c22902009-04-17 19:07:39 +00003570 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3571
3572 // Assign locations to all of the incoming arguments.
3573 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003574 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3575 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003576 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003577 CCAssignFnForNode(CallConv, /* Return*/ false,
3578 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003579
Bob Wilsona4c22902009-04-17 19:07:39 +00003580 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003581 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003582 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3583 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003584
3585 // Initially ArgRegsSaveSize is zero.
3586 // Then we increase this value each time we meet byval parameter.
3587 // We also increase this value in case of varargs function.
3588 AFI->setArgRegsSaveSize(0);
3589
Oliver Stannardd55e1152014-03-05 15:25:27 +00003590 // Calculate the amount of stack space that we need to allocate to store
3591 // byval and variadic arguments that are passed in registers.
3592 // We need to know this before we allocate the first byval or variadic
3593 // argument, as they will be allocated a stack slot below the CFA (Canonical
3594 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00003595 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003596 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003597 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
3598 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00003599
Tim Northover8cda34f2015-03-11 18:54:22 +00003600 CCValAssign &VA = ArgLocs[i];
3601 unsigned Index = VA.getValNo();
3602 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
3603 if (!Flags.isByVal())
3604 continue;
3605
3606 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
3607 unsigned RBegin, REnd;
3608 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
3609 ArgRegBegin = std::min(ArgRegBegin, RBegin);
3610
3611 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003612 }
3613 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00003614
3615 int lastInsIndex = -1;
Matthias Braun941a7052016-07-28 18:40:00 +00003616 if (isVarArg && MFI.hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00003617 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
3618 if (RegIdx != array_lengthof(GPRArgRegs))
3619 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00003620 }
Tim Northover8cda34f2015-03-11 18:54:22 +00003621
3622 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
3623 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Mehdi Amini44ede332015-07-09 02:09:04 +00003624 auto PtrVT = getPointerTy(DAG.getDataLayout());
Oliver Stannardd55e1152014-03-05 15:25:27 +00003625
Bob Wilsona4c22902009-04-17 19:07:39 +00003626 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3627 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003628 if (Ins[VA.getValNo()].isOrigArg()) {
3629 std::advance(CurOrigArg,
3630 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3631 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3632 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003633 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003634 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003635 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003636
Bob Wilsona4c22902009-04-17 19:07:39 +00003637 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003638 // f64 and vector types are split up into multiple registers or
3639 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003640 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003641 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003642 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003643 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003644 SDValue ArgValue2;
3645 if (VA.isMemLoc()) {
Matthias Braun941a7052016-07-28 18:40:00 +00003646 int FI = MFI.CreateFixedObject(8, VA.getLocMemOffset(), true);
Mehdi Amini44ede332015-07-09 02:09:04 +00003647 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003648 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
3649 MachinePointerInfo::getFixedStack(
3650 DAG.getMachineFunction(), FI));
Bob Wilson699bdf72010-04-13 22:03:22 +00003651 } else {
3652 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3653 Chain, DAG, dl);
3654 }
Owen Anderson9f944592009-08-11 20:47:22 +00003655 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3656 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003657 ArgValue, ArgValue1,
3658 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003659 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003660 ArgValue, ArgValue2,
3661 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003662 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003663 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003664
Bob Wilson2e076c42009-06-22 23:27:02 +00003665 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003666 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003667
Owen Anderson9f944592009-08-11 20:47:22 +00003668 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003669 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003670 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003671 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003672 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003673 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003674 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003675 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3676 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003677 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003678 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003679
3680 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003681 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003682 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003683 }
3684
3685 // If this is an 8 or 16-bit value, it is really passed promoted
3686 // to 32 bits. Insert an assert[sz]ext to capture this, then
3687 // truncate to the right size.
3688 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003689 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003690 case CCValAssign::Full: break;
3691 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003692 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003693 break;
3694 case CCValAssign::SExt:
3695 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3696 DAG.getValueType(VA.getValVT()));
3697 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3698 break;
3699 case CCValAssign::ZExt:
3700 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3701 DAG.getValueType(VA.getValVT()));
3702 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3703 break;
3704 }
3705
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003706 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003707
3708 } else { // VA.isRegLoc()
3709
3710 // sanity check
3711 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003712 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003713
Andrew Trick05938a52015-02-16 18:10:47 +00003714 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003715
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003716 // Some Ins[] entries become multiple ArgLoc[] entries.
3717 // Process them only once.
3718 if (index != lastInsIndex)
3719 {
3720 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003721 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003722 // This can be changed with more analysis.
3723 // In case of tail call optimization mark all arguments mutable.
3724 // Since they could be overwritten by lowering of arguments in case of
3725 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003726 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003727 assert(Ins[index].isOrigArg() &&
3728 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003729 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003730
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00003731 int FrameIndex = StoreByValRegs(
3732 CCInfo, DAG, dl, Chain, &*CurOrigArg, CurByValIndex,
3733 VA.getLocMemOffset(), Flags.getByValSize());
Mehdi Amini44ede332015-07-09 02:09:04 +00003734 InVals.push_back(DAG.getFrameIndex(FrameIndex, PtrVT));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003735 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003736 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003737 unsigned FIOffset = VA.getLocMemOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00003738 int FI = MFI.CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
3739 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003740
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003741 // Create load nodes to retrieve arguments from the stack.
Mehdi Amini44ede332015-07-09 02:09:04 +00003742 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
Justin Lebar9c375812016-07-15 18:27:10 +00003743 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3744 MachinePointerInfo::getFixedStack(
3745 DAG.getMachineFunction(), FI)));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003746 }
3747 lastInsIndex = index;
3748 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003749 }
3750 }
3751
3752 // varargs
Matthias Braun941a7052016-07-28 18:40:00 +00003753 if (isVarArg && MFI.hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003754 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003755 CCInfo.getNextStackOffset(),
3756 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003757
Oliver Stannardb14c6252014-04-02 16:10:33 +00003758 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3759
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003760 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003761}
3762
3763/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003764static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003765 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003766 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003767 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003768 // Maybe this has already been legalized into the constant pool?
3769 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003770 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003771 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003772 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003773 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003774 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003775 } else if (Op->getOpcode() == ISD::BITCAST &&
3776 Op->getValueType(0) == MVT::f64) {
3777 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3778 // created by LowerConstantFP().
3779 SDValue BitcastOp = Op->getOperand(0);
Artyom Skrobov314ee042015-11-25 19:41:11 +00003780 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM &&
3781 isNullConstant(BitcastOp->getOperand(0)))
3782 return true;
Evan Cheng10043e22007-01-19 07:51:42 +00003783 }
3784 return false;
3785}
3786
Evan Cheng10043e22007-01-19 07:51:42 +00003787/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3788/// the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003789SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
3790 SDValue &ARMcc, SelectionDAG &DAG,
3791 const SDLoc &dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003792 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003793 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003794 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003795 // Constant does not fit, try adjusting it by one?
3796 switch (CC) {
3797 default: break;
3798 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003799 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003800 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003801 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003802 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003803 }
3804 break;
3805 case ISD::SETULT:
3806 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003807 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003808 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003809 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003810 }
3811 break;
3812 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003813 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003814 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003815 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003816 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003817 }
3818 break;
3819 case ISD::SETULE:
3820 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003821 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003822 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003823 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003824 }
3825 break;
3826 }
3827 }
3828 }
3829
3830 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003831 ARMISD::NodeType CompareType;
3832 switch (CondCode) {
3833 default:
3834 CompareType = ARMISD::CMP;
3835 break;
3836 case ARMCC::EQ:
3837 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003838 // Uses only Z Flag
3839 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003840 break;
3841 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003842 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003843 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003844}
3845
3846/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003847SDValue ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS,
3848 SelectionDAG &DAG, const SDLoc &dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003849 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003850 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003851 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003852 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003853 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003854 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3855 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003856}
3857
Bob Wilson45acbd02011-03-08 01:17:20 +00003858/// duplicateCmp - Glue values can have only one use, so this function
3859/// duplicates a comparison node.
3860SDValue
3861ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3862 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003863 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003864 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3865 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3866
3867 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3868 Cmp = Cmp.getOperand(0);
3869 Opc = Cmp.getOpcode();
3870 if (Opc == ARMISD::CMPFP)
3871 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3872 else {
3873 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3874 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3875 }
3876 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3877}
3878
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003879std::pair<SDValue, SDValue>
3880ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3881 SDValue &ARMcc) const {
3882 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3883
3884 SDValue Value, OverflowCmp;
3885 SDValue LHS = Op.getOperand(0);
3886 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003887 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003888
3889 // FIXME: We are currently always generating CMPs because we don't support
3890 // generating CMN through the backend. This is not as good as the natural
3891 // CMP case because it causes a register dependency and cannot be folded
3892 // later.
3893
3894 switch (Op.getOpcode()) {
3895 default:
3896 llvm_unreachable("Unknown overflow instruction!");
3897 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003898 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3899 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3900 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003901 break;
3902 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003903 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3904 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3905 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003906 break;
3907 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003908 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3909 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3910 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003911 break;
3912 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003913 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3914 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3915 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003916 break;
3917 } // switch (...)
3918
3919 return std::make_pair(Value, OverflowCmp);
3920}
3921
3922
3923SDValue
3924ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3925 // Let legalize expand this if it isn't a legal type yet.
3926 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3927 return SDValue();
3928
3929 SDValue Value, OverflowCmp;
3930 SDValue ARMcc;
3931 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3932 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003933 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003934 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003935 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3936 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003937 EVT VT = Op.getValueType();
3938
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003939 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003940 ARMcc, CCR, OverflowCmp);
3941
3942 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003943 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003944}
3945
3946
Bill Wendling6a981312010-08-11 08:43:16 +00003947SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3948 SDValue Cond = Op.getOperand(0);
3949 SDValue SelectTrue = Op.getOperand(1);
3950 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003951 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003952 unsigned Opc = Cond.getOpcode();
3953
3954 if (Cond.getResNo() == 1 &&
3955 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3956 Opc == ISD::USUBO)) {
3957 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3958 return SDValue();
3959
3960 SDValue Value, OverflowCmp;
3961 SDValue ARMcc;
3962 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3963 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3964 EVT VT = Op.getValueType();
3965
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003966 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003967 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003968 }
Bill Wendling6a981312010-08-11 08:43:16 +00003969
3970 // Convert:
3971 //
3972 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3973 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3974 //
3975 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3976 const ConstantSDNode *CMOVTrue =
3977 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3978 const ConstantSDNode *CMOVFalse =
3979 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3980
3981 if (CMOVTrue && CMOVFalse) {
3982 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3983 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3984
3985 SDValue True;
3986 SDValue False;
3987 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3988 True = SelectTrue;
3989 False = SelectFalse;
3990 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3991 True = SelectFalse;
3992 False = SelectTrue;
3993 }
3994
3995 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003996 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003997 SDValue ARMcc = Cond.getOperand(2);
3998 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003999 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00004000 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004001 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00004002 }
4003 }
4004 }
4005
Dan Gohmand4a77c42012-02-24 00:09:36 +00004006 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
4007 // undefined bits before doing a full-word comparison with zero.
4008 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004009 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00004010
Bill Wendling6a981312010-08-11 08:43:16 +00004011 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004012 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00004013 SelectTrue, SelectFalse, ISD::SETNE);
4014}
4015
Joey Gouly881eab52013-08-22 15:29:11 +00004016static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
4017 bool &swpCmpOps, bool &swpVselOps) {
4018 // Start by selecting the GE condition code for opcodes that return true for
4019 // 'equality'
4020 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
4021 CC == ISD::SETULE)
4022 CondCode = ARMCC::GE;
4023
4024 // and GT for opcodes that return false for 'equality'.
4025 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
4026 CC == ISD::SETULT)
4027 CondCode = ARMCC::GT;
4028
4029 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
4030 // to swap the compare operands.
4031 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
4032 CC == ISD::SETULT)
4033 swpCmpOps = true;
4034
4035 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
4036 // If we have an unordered opcode, we need to swap the operands to the VSEL
4037 // instruction (effectively negating the condition).
4038 //
4039 // This also has the effect of swapping which one of 'less' or 'greater'
4040 // returns true, so we also swap the compare operands. It also switches
4041 // whether we return true for 'equality', so we compensate by picking the
4042 // opposite condition code to our original choice.
4043 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
4044 CC == ISD::SETUGT) {
4045 swpCmpOps = !swpCmpOps;
4046 swpVselOps = !swpVselOps;
4047 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
4048 }
4049
4050 // 'ordered' is 'anything but unordered', so use the VS condition code and
4051 // swap the VSEL operands.
4052 if (CC == ISD::SETO) {
4053 CondCode = ARMCC::VS;
4054 swpVselOps = true;
4055 }
4056
4057 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
4058 // code and swap the VSEL operands.
4059 if (CC == ISD::SETUNE) {
4060 CondCode = ARMCC::EQ;
4061 swpVselOps = true;
4062 }
4063}
4064
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004065SDValue ARMTargetLowering::getCMOV(const SDLoc &dl, EVT VT, SDValue FalseVal,
Oliver Stannard51b1d462014-08-21 12:50:31 +00004066 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
4067 SDValue Cmp, SelectionDAG &DAG) const {
4068 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
4069 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
4070 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
4071 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
4072 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
4073
4074 SDValue TrueLow = TrueVal.getValue(0);
4075 SDValue TrueHigh = TrueVal.getValue(1);
4076 SDValue FalseLow = FalseVal.getValue(0);
4077 SDValue FalseHigh = FalseVal.getValue(1);
4078
4079 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
4080 ARMcc, CCR, Cmp);
4081 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
4082 ARMcc, CCR, duplicateCmp(Cmp, DAG));
4083
4084 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
4085 } else {
4086 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
4087 Cmp);
4088 }
4089}
4090
Benjamin Kramer4d098922016-07-10 11:28:51 +00004091static bool isGTorGE(ISD::CondCode CC) {
4092 return CC == ISD::SETGT || CC == ISD::SETGE;
4093}
Pablo Barrio7a643462016-06-23 16:53:49 +00004094
Benjamin Kramer4d098922016-07-10 11:28:51 +00004095static bool isLTorLE(ISD::CondCode CC) {
4096 return CC == ISD::SETLT || CC == ISD::SETLE;
4097}
Pablo Barrio7a643462016-06-23 16:53:49 +00004098
4099// See if a conditional (LHS CC RHS ? TrueVal : FalseVal) is lower-saturating.
4100// All of these conditions (and their <= and >= counterparts) will do:
4101// x < k ? k : x
4102// x > k ? x : k
4103// k < x ? x : k
4104// k > x ? k : x
Benjamin Kramer4d098922016-07-10 11:28:51 +00004105static bool isLowerSaturate(const SDValue LHS, const SDValue RHS,
4106 const SDValue TrueVal, const SDValue FalseVal,
4107 const ISD::CondCode CC, const SDValue K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00004108 return (isGTorGE(CC) &&
4109 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal))) ||
4110 (isLTorLE(CC) &&
4111 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal)));
4112}
4113
4114// Similar to isLowerSaturate(), but checks for upper-saturating conditions.
Benjamin Kramer4d098922016-07-10 11:28:51 +00004115static bool isUpperSaturate(const SDValue LHS, const SDValue RHS,
4116 const SDValue TrueVal, const SDValue FalseVal,
4117 const ISD::CondCode CC, const SDValue K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00004118 return (isGTorGE(CC) &&
4119 ((K == RHS && K == TrueVal) || (K == LHS && K == FalseVal))) ||
4120 (isLTorLE(CC) &&
4121 ((K == LHS && K == TrueVal) || (K == RHS && K == FalseVal)));
4122}
4123
4124// Check if two chained conditionals could be converted into SSAT.
4125//
4126// SSAT can replace a set of two conditional selectors that bound a number to an
4127// interval of type [k, ~k] when k + 1 is a power of 2. Here are some examples:
4128//
4129// x < -k ? -k : (x > k ? k : x)
4130// x < -k ? -k : (x < k ? x : k)
4131// x > -k ? (x > k ? k : x) : -k
4132// x < k ? (x < -k ? -k : x) : k
4133// etc.
4134//
4135// It returns true if the conversion can be done, false otherwise.
4136// Additionally, the variable is returned in parameter V and the constant in K.
Benjamin Kramer4d098922016-07-10 11:28:51 +00004137static bool isSaturatingConditional(const SDValue &Op, SDValue &V,
4138 uint64_t &K) {
Pablo Barrio7a643462016-06-23 16:53:49 +00004139
4140 SDValue LHS1 = Op.getOperand(0);
4141 SDValue RHS1 = Op.getOperand(1);
4142 SDValue TrueVal1 = Op.getOperand(2);
4143 SDValue FalseVal1 = Op.getOperand(3);
4144 ISD::CondCode CC1 = cast<CondCodeSDNode>(Op.getOperand(4))->get();
4145
4146 const SDValue Op2 = isa<ConstantSDNode>(TrueVal1) ? FalseVal1 : TrueVal1;
4147 if (Op2.getOpcode() != ISD::SELECT_CC)
4148 return false;
4149
4150 SDValue LHS2 = Op2.getOperand(0);
4151 SDValue RHS2 = Op2.getOperand(1);
4152 SDValue TrueVal2 = Op2.getOperand(2);
4153 SDValue FalseVal2 = Op2.getOperand(3);
4154 ISD::CondCode CC2 = cast<CondCodeSDNode>(Op2.getOperand(4))->get();
4155
4156 // Find out which are the constants and which are the variables
4157 // in each conditional
4158 SDValue *K1 = isa<ConstantSDNode>(LHS1) ? &LHS1 : isa<ConstantSDNode>(RHS1)
4159 ? &RHS1
4160 : NULL;
4161 SDValue *K2 = isa<ConstantSDNode>(LHS2) ? &LHS2 : isa<ConstantSDNode>(RHS2)
4162 ? &RHS2
4163 : NULL;
4164 SDValue K2Tmp = isa<ConstantSDNode>(TrueVal2) ? TrueVal2 : FalseVal2;
4165 SDValue V1Tmp = (K1 && *K1 == LHS1) ? RHS1 : LHS1;
4166 SDValue V2Tmp = (K2 && *K2 == LHS2) ? RHS2 : LHS2;
4167 SDValue V2 = (K2Tmp == TrueVal2) ? FalseVal2 : TrueVal2;
4168
4169 // We must detect cases where the original operations worked with 16- or
4170 // 8-bit values. In such case, V2Tmp != V2 because the comparison operations
4171 // must work with sign-extended values but the select operations return
4172 // the original non-extended value.
4173 SDValue V2TmpReg = V2Tmp;
4174 if (V2Tmp->getOpcode() == ISD::SIGN_EXTEND_INREG)
4175 V2TmpReg = V2Tmp->getOperand(0);
4176
4177 // Check that the registers and the constants have the correct values
4178 // in both conditionals
4179 if (!K1 || !K2 || *K1 == Op2 || *K2 != K2Tmp || V1Tmp != V2Tmp ||
4180 V2TmpReg != V2)
4181 return false;
4182
4183 // Figure out which conditional is saturating the lower/upper bound.
4184 const SDValue *LowerCheckOp =
4185 isLowerSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
4186 ? &Op
4187 : isLowerSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) ? &Op2
4188 : NULL;
4189 const SDValue *UpperCheckOp =
4190 isUpperSaturate(LHS1, RHS1, TrueVal1, FalseVal1, CC1, *K1)
4191 ? &Op
4192 : isUpperSaturate(LHS2, RHS2, TrueVal2, FalseVal2, CC2, *K2) ? &Op2
4193 : NULL;
4194
4195 if (!UpperCheckOp || !LowerCheckOp || LowerCheckOp == UpperCheckOp)
4196 return false;
4197
4198 // Check that the constant in the lower-bound check is
4199 // the opposite of the constant in the upper-bound check
4200 // in 1's complement.
4201 int64_t Val1 = cast<ConstantSDNode>(*K1)->getSExtValue();
4202 int64_t Val2 = cast<ConstantSDNode>(*K2)->getSExtValue();
4203 int64_t PosVal = std::max(Val1, Val2);
4204
4205 if (((Val1 > Val2 && UpperCheckOp == &Op) ||
4206 (Val1 < Val2 && UpperCheckOp == &Op2)) &&
4207 Val1 == ~Val2 && isPowerOf2_64(PosVal + 1)) {
4208
4209 V = V2;
4210 K = (uint64_t)PosVal; // At this point, PosVal is guaranteed to be positive
4211 return true;
4212 }
4213
4214 return false;
4215}
4216
Dan Gohman21cea8a2010-04-17 15:26:15 +00004217SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Pablo Barrio7a643462016-06-23 16:53:49 +00004218
Owen Anderson53aa7a92009-08-10 22:56:29 +00004219 EVT VT = Op.getValueType();
Pablo Barrio7a643462016-06-23 16:53:49 +00004220 SDLoc dl(Op);
4221
4222 // Try to convert two saturating conditional selects into a single SSAT
4223 SDValue SatValue;
4224 uint64_t SatConstant;
Bernard Ogden849f7372016-08-02 10:04:03 +00004225 if (((!Subtarget->isThumb() && Subtarget->hasV6Ops()) || Subtarget->isThumb2()) &&
Renato Golin32b165f2016-07-25 22:25:25 +00004226 isSaturatingConditional(Op, SatValue, SatConstant))
Pablo Barrio7a643462016-06-23 16:53:49 +00004227 return DAG.getNode(ARMISD::SSAT, dl, VT, SatValue,
4228 DAG.getConstant(countTrailingOnes(SatConstant), dl, VT));
4229
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004230 SDValue LHS = Op.getOperand(0);
4231 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00004232 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004233 SDValue TrueVal = Op.getOperand(2);
4234 SDValue FalseVal = Op.getOperand(3);
Evan Cheng10043e22007-01-19 07:51:42 +00004235
Oliver Stannard51b1d462014-08-21 12:50:31 +00004236 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4237 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4238 dl);
4239
4240 // If softenSetCCOperands only returned one value, we should compare it to
4241 // zero.
4242 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004243 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00004244 CC = ISD::SETNE;
4245 }
4246 }
4247
Owen Anderson9f944592009-08-11 20:47:22 +00004248 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00004249 // Try to generate VSEL on ARMv8.
4250 // The VSEL instruction can't use all the usual ARM condition
4251 // codes: it only has two bits to select the condition code, so it's
4252 // constrained to use only GE, GT, VS and EQ.
4253 //
4254 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
4255 // swap the operands of the previous compare instruction (effectively
4256 // inverting the compare condition, swapping 'less' and 'greater') and
4257 // sometimes need to swap the operands to the VSEL (which inverts the
4258 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00004259 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
4260 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00004261 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
4262 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
4263 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00004264 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00004265 std::swap(TrueVal, FalseVal);
4266 }
4267 }
4268
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004269 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00004270 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004271 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004272 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00004273 }
4274
4275 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00004276 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00004277
Scott Douglass7ad77922015-04-08 17:18:28 +00004278 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00004279 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
4280 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00004281 bool swpCmpOps = false;
4282 bool swpVselOps = false;
4283 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
4284
4285 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
4286 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
4287 if (swpCmpOps)
4288 std::swap(LHS, RHS);
4289 if (swpVselOps)
4290 std::swap(TrueVal, FalseVal);
4291 }
4292 }
4293
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004294 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004295 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004296 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004297 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00004298 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004299 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00004300 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004301 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004302 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00004303 }
4304 return Result;
4305}
4306
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004307/// canChangeToInt - Given the fp compare operand, return true if it is suitable
4308/// to morph to an integer compare sequence.
4309static bool canChangeToInt(SDValue Op, bool &SeenZero,
4310 const ARMSubtarget *Subtarget) {
4311 SDNode *N = Op.getNode();
4312 if (!N->hasOneUse())
4313 // Otherwise it requires moving the value from fp to integer registers.
4314 return false;
4315 if (!N->getNumValues())
4316 return false;
4317 EVT VT = Op.getValueType();
4318 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
4319 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
4320 // vmrs are very slow, e.g. cortex-a8.
4321 return false;
4322
4323 if (isFloatingPointZero(Op)) {
4324 SeenZero = true;
4325 return true;
4326 }
4327 return ISD::isNormalLoad(N);
4328}
4329
4330static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
4331 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004332 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004333
4334 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Justin Lebar9c375812016-07-15 18:27:10 +00004335 return DAG.getLoad(MVT::i32, SDLoc(Op), Ld->getChain(), Ld->getBasePtr(),
4336 Ld->getPointerInfo(), Ld->getAlignment(),
4337 Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004338
4339 llvm_unreachable("Unknown VFP cmp argument!");
4340}
4341
4342static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
4343 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004344 SDLoc dl(Op);
4345
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004346 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004347 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
4348 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004349 return;
4350 }
4351
4352 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
4353 SDValue Ptr = Ld->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +00004354 RetVal1 =
4355 DAG.getLoad(MVT::i32, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(),
4356 Ld->getAlignment(), Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004357
4358 EVT PtrType = Ptr.getValueType();
4359 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004360 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
4361 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
Justin Lebar9c375812016-07-15 18:27:10 +00004362 RetVal2 = DAG.getLoad(MVT::i32, dl, Ld->getChain(), NewPtr,
4363 Ld->getPointerInfo().getWithOffset(4), NewAlign,
4364 Ld->getMemOperand()->getFlags());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004365 return;
4366 }
4367
4368 llvm_unreachable("Unknown VFP cmp argument!");
4369}
4370
4371/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
4372/// f32 and even f64 comparisons to integer ones.
4373SDValue
4374ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
4375 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00004376 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004377 SDValue LHS = Op.getOperand(2);
4378 SDValue RHS = Op.getOperand(3);
4379 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004380 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004381
Evan Chengd12af5d2012-03-01 23:27:13 +00004382 bool LHSSeenZero = false;
4383 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
4384 bool RHSSeenZero = false;
4385 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
4386 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00004387 // If unsafe fp math optimization is enabled and there are no other uses of
4388 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004389 // to an integer comparison.
4390 if (CC == ISD::SETOEQ)
4391 CC = ISD::SETEQ;
4392 else if (CC == ISD::SETUNE)
4393 CC = ISD::SETNE;
4394
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004395 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004396 SDValue ARMcc;
4397 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00004398 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4399 bitcastf32Toi32(LHS, DAG), Mask);
4400 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
4401 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004402 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
4403 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4404 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
4405 Chain, Dest, ARMcc, CCR, Cmp);
4406 }
4407
4408 SDValue LHS1, LHS2;
4409 SDValue RHS1, RHS2;
4410 expandf64Toi32(LHS, DAG, LHS1, LHS2);
4411 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00004412 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
4413 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004414 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004415 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004416 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004417 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00004418 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004419 }
4420
4421 return SDValue();
4422}
4423
4424SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
4425 SDValue Chain = Op.getOperand(0);
4426 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
4427 SDValue LHS = Op.getOperand(2);
4428 SDValue RHS = Op.getOperand(3);
4429 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004430 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004431
Oliver Stannard51b1d462014-08-21 12:50:31 +00004432 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
4433 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
4434 dl);
4435
4436 // If softenSetCCOperands only returned one value, we should compare it to
4437 // zero.
4438 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004439 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00004440 CC = ISD::SETNE;
4441 }
4442 }
4443
Owen Anderson9f944592009-08-11 20:47:22 +00004444 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004445 SDValue ARMcc;
4446 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004447 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00004448 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004449 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00004450 }
4451
Owen Anderson9f944592009-08-11 20:47:22 +00004452 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004453
Nick Lewycky50f02cb2011-12-02 22:16:29 +00004454 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004455 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
4456 CC == ISD::SETNE || CC == ISD::SETUNE)) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004457 if (SDValue Result = OptimizeVFPBrcond(Op, DAG))
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004458 return Result;
4459 }
4460
Evan Cheng10043e22007-01-19 07:51:42 +00004461 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00004462 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00004463
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004464 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004465 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00004466 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00004467 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004468 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00004469 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00004470 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004471 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004472 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00004473 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00004474 }
4475 return Res;
4476}
4477
Dan Gohman21cea8a2010-04-17 15:26:15 +00004478SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004479 SDValue Chain = Op.getOperand(0);
4480 SDValue Table = Op.getOperand(1);
4481 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004482 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00004483
Mehdi Amini44ede332015-07-09 02:09:04 +00004484 EVT PTy = getPointerTy(DAG.getDataLayout());
Evan Cheng10043e22007-01-19 07:51:42 +00004485 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004486 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00004487 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004488 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00004489 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004490 if (Subtarget->isThumb2()) {
4491 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
4492 // which does another jump to the destination. This also makes it easier
4493 // to translate it to TBB / TBH later.
4494 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00004495 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00004496 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004497 }
Oliver Stannard8331aae2016-08-08 15:28:31 +00004498 if (isPositionIndependent() || Subtarget->isROPI()) {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004499 Addr =
4500 DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00004501 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
Evan Chengf3a1fce2009-07-25 00:33:29 +00004502 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00004503 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00004504 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004505 } else {
Alex Lorenze40c8a22015-08-11 23:09:45 +00004506 Addr =
4507 DAG.getLoad(PTy, dl, Chain, Addr,
Justin Lebar9c375812016-07-15 18:27:10 +00004508 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()));
Evan Chengf3a1fce2009-07-25 00:33:29 +00004509 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00004510 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00004511 }
Evan Cheng10043e22007-01-19 07:51:42 +00004512}
4513
Eli Friedman2d4055b2011-11-09 23:36:02 +00004514static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00004515 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004516 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004517
James Molloy547d4c02012-02-20 09:24:05 +00004518 if (Op.getValueType().getVectorElementType() == MVT::i32) {
4519 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
4520 return Op;
4521 return DAG.UnrollVectorOp(Op.getNode());
4522 }
4523
4524 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
4525 "Invalid type for custom lowering!");
4526 if (VT != MVT::v4i16)
4527 return DAG.UnrollVectorOp(Op.getNode());
4528
4529 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
4530 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00004531}
4532
Oliver Stannard51b1d462014-08-21 12:50:31 +00004533SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00004534 EVT VT = Op.getValueType();
4535 if (VT.isVector())
4536 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004537 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
4538 RTLIB::Libcall LC;
4539 if (Op.getOpcode() == ISD::FP_TO_SINT)
4540 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
4541 Op.getValueType());
4542 else
4543 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
4544 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004545 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004546 /*isSigned*/ false, SDLoc(Op)).first;
4547 }
4548
James Molloyfa041152015-03-23 16:15:16 +00004549 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004550}
4551
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004552static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4553 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004554 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004555
Eli Friedman2d4055b2011-11-09 23:36:02 +00004556 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
4557 if (VT.getVectorElementType() == MVT::f32)
4558 return Op;
4559 return DAG.UnrollVectorOp(Op.getNode());
4560 }
4561
Duncan Sandsa41634e2011-08-12 14:54:45 +00004562 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
4563 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004564 if (VT != MVT::v4f32)
4565 return DAG.UnrollVectorOp(Op.getNode());
4566
4567 unsigned CastOpc;
4568 unsigned Opc;
4569 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00004570 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004571 case ISD::SINT_TO_FP:
4572 CastOpc = ISD::SIGN_EXTEND;
4573 Opc = ISD::SINT_TO_FP;
4574 break;
4575 case ISD::UINT_TO_FP:
4576 CastOpc = ISD::ZERO_EXTEND;
4577 Opc = ISD::UINT_TO_FP;
4578 break;
4579 }
4580
4581 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
4582 return DAG.getNode(Opc, dl, VT, Op);
4583}
4584
Oliver Stannard51b1d462014-08-21 12:50:31 +00004585SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00004586 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00004587 if (VT.isVector())
4588 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00004589 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
4590 RTLIB::Libcall LC;
4591 if (Op.getOpcode() == ISD::SINT_TO_FP)
4592 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
4593 Op.getValueType());
4594 else
4595 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
4596 Op.getValueType());
Craig Topper8fe40e02015-10-22 17:05:00 +00004597 return makeLibCall(DAG, LC, Op.getValueType(), Op.getOperand(0),
Oliver Stannard51b1d462014-08-21 12:50:31 +00004598 /*isSigned*/ false, SDLoc(Op)).first;
4599 }
4600
James Molloyfa041152015-03-23 16:15:16 +00004601 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00004602}
4603
Evan Cheng25f93642010-07-08 02:08:50 +00004604SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004605 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004606 SDValue Tmp0 = Op.getOperand(0);
4607 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004608 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004609 EVT VT = Op.getValueType();
4610 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004611 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4612 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4613 bool UseNEON = !InGPR && Subtarget->hasNEON();
4614
4615 if (UseNEON) {
4616 // Use VBSL to copy the sign bit.
4617 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4618 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004619 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004620 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4621 if (VT == MVT::f64)
4622 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4623 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004624 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004625 else /*if (VT == MVT::f32)*/
4626 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4627 if (SrcVT == MVT::f32) {
4628 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4629 if (VT == MVT::f64)
4630 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4631 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004632 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004633 } else if (VT == MVT::f32)
4634 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4635 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004636 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004637 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4638 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4639
4640 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004641 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004642 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4643 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4644 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004645
Evan Chengd6b641e2011-02-23 02:24:55 +00004646 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4647 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4648 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004649 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004650 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4651 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004652 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004653 } else {
4654 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4655 }
4656
4657 return Res;
4658 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004659
4660 // Bitcast operand 1 to i32.
4661 if (SrcVT == MVT::f64)
4662 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004663 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004664 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4665
Evan Chengd6b641e2011-02-23 02:24:55 +00004666 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004667 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4668 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004669 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4670 if (VT == MVT::f32) {
4671 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4672 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4673 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4674 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004675 }
4676
Evan Chengd6b641e2011-02-23 02:24:55 +00004677 // f64: Or the high part with signbit and then combine two parts.
4678 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004679 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004680 SDValue Lo = Tmp0.getValue(0);
4681 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4682 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4683 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004684}
4685
Evan Cheng168ced92010-05-22 01:47:14 +00004686SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4687 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004688 MachineFrameInfo &MFI = MF.getFrameInfo();
4689 MFI.setReturnAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004690
Bill Wendling908bf812014-01-06 00:43:20 +00004691 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004692 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004693
Evan Cheng168ced92010-05-22 01:47:14 +00004694 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004695 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004696 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4697 if (Depth) {
4698 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004699 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004700 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4701 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Justin Lebar9c375812016-07-15 18:27:10 +00004702 MachinePointerInfo());
Evan Cheng168ced92010-05-22 01:47:14 +00004703 }
4704
4705 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004706 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004707 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4708}
4709
Dan Gohman21cea8a2010-04-17 15:26:15 +00004710SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004711 const ARMBaseRegisterInfo &ARI =
4712 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4713 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00004714 MachineFrameInfo &MFI = MF.getFrameInfo();
4715 MFI.setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004716
Owen Anderson53aa7a92009-08-10 22:56:29 +00004717 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004718 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004719 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004720 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004721 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4722 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004723 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00004724 MachinePointerInfo());
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004725 return FrameAddr;
4726}
4727
Renato Golinc7aea402014-05-06 16:51:25 +00004728// FIXME? Maybe this could be a TableGen attribute on some registers and
4729// this table could be generated automatically from RegInfo.
Pat Gavlina717f252015-07-09 17:40:29 +00004730unsigned ARMTargetLowering::getRegisterByName(const char* RegName, EVT VT,
4731 SelectionDAG &DAG) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004732 unsigned Reg = StringSwitch<unsigned>(RegName)
4733 .Case("sp", ARM::SP)
4734 .Default(0);
4735 if (Reg)
4736 return Reg;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00004737 report_fatal_error(Twine("Invalid register name \""
4738 + StringRef(RegName) + "\"."));
4739}
4740
4741// Result is 64 bit value so split into two 32 bit values and return as a
4742// pair of values.
4743static void ExpandREAD_REGISTER(SDNode *N, SmallVectorImpl<SDValue> &Results,
4744 SelectionDAG &DAG) {
4745 SDLoc DL(N);
4746
4747 // This function is only supposed to be called for i64 type destination.
4748 assert(N->getValueType(0) == MVT::i64
4749 && "ExpandREAD_REGISTER called for non-i64 type result.");
4750
4751 SDValue Read = DAG.getNode(ISD::READ_REGISTER, DL,
4752 DAG.getVTList(MVT::i32, MVT::i32, MVT::Other),
4753 N->getOperand(0),
4754 N->getOperand(1));
4755
4756 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Read.getValue(0),
4757 Read.getValue(1)));
4758 Results.push_back(Read.getOperand(0));
Renato Golinc7aea402014-05-06 16:51:25 +00004759}
4760
Quentin Colombet901f0362015-12-04 01:53:14 +00004761/// \p BC is a bitcast that is about to be turned into a VMOVDRR.
4762/// When \p DstVT, the destination type of \p BC, is on the vector
4763/// register bank and the source of bitcast, \p Op, operates on the same bank,
4764/// it might be possible to combine them, such that everything stays on the
4765/// vector register bank.
4766/// \p return The node that would replace \p BT, if the combine
4767/// is possible.
4768static SDValue CombineVMOVDRRCandidateWithVecOp(const SDNode *BC,
4769 SelectionDAG &DAG) {
4770 SDValue Op = BC->getOperand(0);
4771 EVT DstVT = BC->getValueType(0);
4772
4773 // The only vector instruction that can produce a scalar (remember,
4774 // since the bitcast was about to be turned into VMOVDRR, the source
4775 // type is i64) from a vector is EXTRACT_VECTOR_ELT.
4776 // Moreover, we can do this combine only if there is one use.
4777 // Finally, if the destination type is not a vector, there is not
4778 // much point on forcing everything on the vector bank.
4779 if (!DstVT.isVector() || Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
4780 !Op.hasOneUse())
4781 return SDValue();
4782
4783 // If the index is not constant, we will introduce an additional
4784 // multiply that will stick.
4785 // Give up in that case.
4786 ConstantSDNode *Index = dyn_cast<ConstantSDNode>(Op.getOperand(1));
4787 if (!Index)
4788 return SDValue();
4789 unsigned DstNumElt = DstVT.getVectorNumElements();
4790
4791 // Compute the new index.
4792 const APInt &APIntIndex = Index->getAPIntValue();
4793 APInt NewIndex(APIntIndex.getBitWidth(), DstNumElt);
4794 NewIndex *= APIntIndex;
4795 // Check if the new constant index fits into i32.
4796 if (NewIndex.getBitWidth() > 32)
4797 return SDValue();
4798
4799 // vMTy bitcast(i64 extractelt vNi64 src, i32 index) ->
4800 // vMTy extractsubvector vNxMTy (bitcast vNi64 src), i32 index*M)
4801 SDLoc dl(Op);
4802 SDValue ExtractSrc = Op.getOperand(0);
4803 EVT VecVT = EVT::getVectorVT(
4804 *DAG.getContext(), DstVT.getScalarType(),
4805 ExtractSrc.getValueType().getVectorNumElements() * DstNumElt);
4806 SDValue BitCast = DAG.getNode(ISD::BITCAST, dl, VecVT, ExtractSrc);
4807 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DstVT, BitCast,
4808 DAG.getConstant(NewIndex.getZExtValue(), dl, MVT::i32));
4809}
4810
Wesley Peck527da1b2010-11-23 03:31:01 +00004811/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004812/// expand a bit convert where either the source or destination type is i64 to
4813/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4814/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4815/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004816static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004817 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004818 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004819 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004820
Bob Wilson59b70ea2010-04-17 05:30:19 +00004821 // This function is only supposed to be called for i64 types, either as the
4822 // source or destination of the bit convert.
4823 EVT SrcVT = Op.getValueType();
4824 EVT DstVT = N->getValueType(0);
4825 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004826 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004827
Bob Wilson59b70ea2010-04-17 05:30:19 +00004828 // Turn i64->f64 into VMOVDRR.
4829 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Quentin Colombet901f0362015-12-04 01:53:14 +00004830 // Do not force values to GPRs (this is what VMOVDRR does for the inputs)
4831 // if we can combine the bitcast with its source.
4832 if (SDValue Val = CombineVMOVDRRCandidateWithVecOp(N, DAG))
4833 return Val;
4834
Owen Anderson9f944592009-08-11 20:47:22 +00004835 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004836 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004837 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004838 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004839 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004840 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004841 }
Bob Wilson7117a912009-03-20 22:42:55 +00004842
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004843 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004844 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004845 SDValue Cvt;
Mehdi Aminiffc14022015-07-08 01:00:38 +00004846 if (DAG.getDataLayout().isBigEndian() && SrcVT.isVector() &&
Christian Pirker6692e7c2014-05-14 16:59:44 +00004847 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004848 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4849 DAG.getVTList(MVT::i32, MVT::i32),
4850 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4851 else
4852 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4853 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004854 // Merge the pieces into a single i64 value.
4855 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4856 }
Bob Wilson7117a912009-03-20 22:42:55 +00004857
Bob Wilson59b70ea2010-04-17 05:30:19 +00004858 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004859}
4860
Bob Wilson2e076c42009-06-22 23:27:02 +00004861/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004862/// Zero vectors are used to represent vector negation and in those cases
4863/// will be implemented with the NEON VNEG instruction. However, VNEG does
4864/// not support i64 elements, so sometimes the zero vectors will need to be
4865/// explicitly constructed. Regardless, use a canonical VMOV to create the
4866/// zero vector.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004867static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004868 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004869 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004870 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004871 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4872 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004873 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004874}
4875
Jim Grosbach624fcb22009-10-31 21:00:56 +00004876/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4877/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004878SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4879 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004880 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4881 EVT VT = Op.getValueType();
4882 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004883 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004884 SDValue ShOpLo = Op.getOperand(0);
4885 SDValue ShOpHi = Op.getOperand(1);
4886 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004887 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004888 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004889
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004890 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4891
Jim Grosbach624fcb22009-10-31 21:00:56 +00004892 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004893 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004894 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4895 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004896 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004897 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4898 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004899 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004900
4901 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004902 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4903 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004904 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004905 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004906 CCR, Cmp);
4907
4908 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004909 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004910}
4911
Jim Grosbach5d994042009-10-31 19:38:01 +00004912/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4913/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004914SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4915 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004916 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4917 EVT VT = Op.getValueType();
4918 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004919 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004920 SDValue ShOpLo = Op.getOperand(0);
4921 SDValue ShOpHi = Op.getOperand(1);
4922 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004923 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004924
4925 assert(Op.getOpcode() == ISD::SHL_PARTS);
4926 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004927 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004928 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4929 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004930 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004931 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4932 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4933
4934 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4935 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004936 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4937 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004938 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004939 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004940 CCR, Cmp);
4941
4942 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004943 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004944}
4945
Jim Grosbach535d3b42010-09-08 03:54:02 +00004946SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004947 SelectionDAG &DAG) const {
4948 // The rounding mode is in bits 23:22 of the FPSCR.
4949 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4950 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4951 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004952 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004953 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004954 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004955 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004956 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004957 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004958 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004959 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004960 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004961 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004962}
4963
Jim Grosbach8546ec92010-01-18 19:58:49 +00004964static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4965 const ARMSubtarget *ST) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004966 SDLoc dl(N);
Logan Chien0a43abc2015-07-13 15:37:30 +00004967 EVT VT = N->getValueType(0);
4968 if (VT.isVector()) {
4969 assert(ST->hasNEON());
4970
4971 // Compute the least significant set bit: LSB = X & -X
4972 SDValue X = N->getOperand(0);
4973 SDValue NX = DAG.getNode(ISD::SUB, dl, VT, getZeroVector(VT, DAG, dl), X);
4974 SDValue LSB = DAG.getNode(ISD::AND, dl, VT, X, NX);
4975
4976 EVT ElemTy = VT.getVectorElementType();
4977
4978 if (ElemTy == MVT::i8) {
4979 // Compute with: cttz(x) = ctpop(lsb - 1)
4980 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4981 DAG.getTargetConstant(1, dl, ElemTy));
4982 SDValue Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
4983 return DAG.getNode(ISD::CTPOP, dl, VT, Bits);
4984 }
4985
4986 if ((ElemTy == MVT::i16 || ElemTy == MVT::i32) &&
4987 (N->getOpcode() == ISD::CTTZ_ZERO_UNDEF)) {
4988 // Compute with: cttz(x) = (width - 1) - ctlz(lsb), if x != 0
4989 unsigned NumBits = ElemTy.getSizeInBits();
4990 SDValue WidthMinus1 =
4991 DAG.getNode(ARMISD::VMOVIMM, dl, VT,
4992 DAG.getTargetConstant(NumBits - 1, dl, ElemTy));
4993 SDValue CTLZ = DAG.getNode(ISD::CTLZ, dl, VT, LSB);
4994 return DAG.getNode(ISD::SUB, dl, VT, WidthMinus1, CTLZ);
4995 }
4996
4997 // Compute with: cttz(x) = ctpop(lsb - 1)
4998
4999 // Since we can only compute the number of bits in a byte with vcnt.8, we
5000 // have to gather the result with pairwise addition (vpaddl) for i16, i32,
5001 // and i64.
5002
5003 // Compute LSB - 1.
5004 SDValue Bits;
5005 if (ElemTy == MVT::i64) {
5006 // Load constant 0xffff'ffff'ffff'ffff to register.
5007 SDValue FF = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5008 DAG.getTargetConstant(0x1eff, dl, MVT::i32));
5009 Bits = DAG.getNode(ISD::ADD, dl, VT, LSB, FF);
5010 } else {
5011 SDValue One = DAG.getNode(ARMISD::VMOVIMM, dl, VT,
5012 DAG.getTargetConstant(1, dl, ElemTy));
5013 Bits = DAG.getNode(ISD::SUB, dl, VT, LSB, One);
5014 }
5015
5016 // Count #bits with vcnt.8.
5017 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
5018 SDValue BitsVT8 = DAG.getNode(ISD::BITCAST, dl, VT8Bit, Bits);
5019 SDValue Cnt8 = DAG.getNode(ISD::CTPOP, dl, VT8Bit, BitsVT8);
5020
5021 // Gather the #bits with vpaddl (pairwise add.)
5022 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
5023 SDValue Cnt16 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT16Bit,
5024 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
5025 Cnt8);
5026 if (ElemTy == MVT::i16)
5027 return Cnt16;
5028
5029 EVT VT32Bit = VT.is64BitVector() ? MVT::v2i32 : MVT::v4i32;
5030 SDValue Cnt32 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT32Bit,
5031 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
5032 Cnt16);
5033 if (ElemTy == MVT::i32)
5034 return Cnt32;
5035
5036 assert(ElemTy == MVT::i64);
5037 SDValue Cnt64 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
5038 DAG.getTargetConstant(Intrinsic::arm_neon_vpaddlu, dl, MVT::i32),
5039 Cnt32);
5040 return Cnt64;
5041 }
Jim Grosbach8546ec92010-01-18 19:58:49 +00005042
5043 if (!ST->hasV6T2Ops())
5044 return SDValue();
5045
James Molloyb5640982015-11-13 16:05:22 +00005046 SDValue rbit = DAG.getNode(ISD::BITREVERSE, dl, VT, N->getOperand(0));
Jim Grosbach8546ec92010-01-18 19:58:49 +00005047 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
5048}
5049
Evan Chengb4eae132012-12-04 22:41:50 +00005050/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
5051/// for each 16-bit element from operand, repeated. The basic idea is to
5052/// leverage vcnt to get the 8-bit counts, gather and add the results.
5053///
5054/// Trace for v4i16:
5055/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
5056/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
5057/// 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 +00005058/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00005059/// [b0 b1 b2 b3 b4 b5 b6 b7]
5060/// +[b1 b0 b3 b2 b5 b4 b7 b6]
5061/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
5062/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
5063static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
5064 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005065 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00005066
5067 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
5068 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
5069 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
5070 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
5071 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
5072 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
5073}
5074
5075/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
5076/// bit-count for each 16-bit element from the operand. We need slightly
5077/// different sequencing for v4i16 and v8i16 to stay within NEON's available
5078/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00005079///
Evan Chengb4eae132012-12-04 22:41:50 +00005080/// Trace for v4i16:
5081/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
5082/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
5083/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
5084/// v4i16:Extracted = [k0 k1 k2 k3 ]
5085static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
5086 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005087 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00005088
5089 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
5090 if (VT.is64BitVector()) {
5091 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
5092 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005093 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005094 } else {
5095 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005096 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005097 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
5098 }
5099}
5100
5101/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
5102/// bit-count for each 32-bit element from the operand. The idea here is
5103/// to split the vector into 16-bit elements, leverage the 16-bit count
5104/// routine, and then combine the results.
5105///
5106/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
5107/// input = [v0 v1 ] (vi: 32-bit elements)
5108/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
5109/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00005110/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00005111/// [k0 k1 k2 k3 ]
5112/// N1 =+[k1 k0 k3 k2 ]
5113/// [k0 k2 k1 k3 ]
5114/// N2 =+[k1 k3 k0 k2 ]
5115/// [k0 k2 k1 k3 ]
5116/// Extended =+[k1 k3 k0 k2 ]
5117/// [k0 k2 ]
5118/// Extracted=+[k1 k3 ]
5119///
5120static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
5121 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005122 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00005123
5124 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
5125
5126 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
5127 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
5128 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
5129 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
5130 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
5131
5132 if (VT.is64BitVector()) {
5133 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
5134 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005135 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005136 } else {
5137 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005138 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00005139 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
5140 }
5141}
5142
5143static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
5144 const ARMSubtarget *ST) {
5145 EVT VT = N->getValueType(0);
5146
5147 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00005148 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
5149 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00005150 "Unexpected type for custom ctpop lowering");
5151
5152 if (VT.getVectorElementType() == MVT::i32)
5153 return lowerCTPOP32BitElements(N, DAG);
5154 else
5155 return lowerCTPOP16BitElements(N, DAG);
5156}
5157
Bob Wilson2e076c42009-06-22 23:27:02 +00005158static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
5159 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005160 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005161 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00005162
Bob Wilson7d471332010-11-18 21:16:28 +00005163 if (!VT.isVector())
5164 return SDValue();
5165
Bob Wilson2e076c42009-06-22 23:27:02 +00005166 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00005167 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00005168
Bob Wilson7d471332010-11-18 21:16:28 +00005169 // Left shifts translate directly to the vshiftu intrinsic.
5170 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00005171 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005172 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
5173 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00005174 N->getOperand(0), N->getOperand(1));
5175
5176 assert((N->getOpcode() == ISD::SRA ||
5177 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
5178
5179 // NEON uses the same intrinsics for both left and right shifts. For
5180 // right shifts, the shift amounts are negative, so negate the vector of
5181 // shift amounts.
5182 EVT ShiftVT = N->getOperand(1).getValueType();
5183 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
5184 getZeroVector(ShiftVT, DAG, dl),
5185 N->getOperand(1));
5186 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
5187 Intrinsic::arm_neon_vshifts :
5188 Intrinsic::arm_neon_vshiftu);
5189 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005190 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00005191 N->getOperand(0), NegatedCount);
5192}
5193
5194static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
5195 const ARMSubtarget *ST) {
5196 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005197 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00005198
Eli Friedman682d8c12009-08-22 03:13:10 +00005199 // We can get here for a node like i32 = ISD::SHL i32, i64
5200 if (VT != MVT::i64)
5201 return SDValue();
5202
5203 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00005204 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00005205
Chris Lattnerf81d5882007-11-24 07:07:01 +00005206 // We only lower SRA, SRL of 1 here, all others use generic lowering.
Artyom Skrobov314ee042015-11-25 19:41:11 +00005207 if (!isOneConstant(N->getOperand(1)))
Duncan Sands6ed40142008-12-01 11:39:25 +00005208 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00005209
Chris Lattnerf81d5882007-11-24 07:07:01 +00005210 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00005211 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00005212
Chris Lattnerf81d5882007-11-24 07:07:01 +00005213 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00005214 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005215 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00005216 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005217 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00005218
Chris Lattnerf81d5882007-11-24 07:07:01 +00005219 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
5220 // captures the result into a carry flag.
5221 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00005222 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00005223
Chris Lattnerf81d5882007-11-24 07:07:01 +00005224 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00005225 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00005226
Chris Lattnerf81d5882007-11-24 07:07:01 +00005227 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00005228 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00005229}
5230
Bob Wilson2e076c42009-06-22 23:27:02 +00005231static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5232 SDValue TmpOp0, TmpOp1;
5233 bool Invert = false;
5234 bool Swap = false;
5235 unsigned Opc = 0;
5236
5237 SDValue Op0 = Op.getOperand(0);
5238 SDValue Op1 = Op.getOperand(1);
5239 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00005240 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005241 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005242 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005243 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00005244
James Molloybf170092015-08-20 16:33:44 +00005245 if (CmpVT.getVectorElementType() == MVT::i64)
5246 // 64-bit comparisons are not legal. We've marked SETCC as non-Custom,
5247 // but it's possible that our operands are 64-bit but our result is 32-bit.
5248 // Bail in this case.
5249 return SDValue();
5250
Oliver Stannard51b1d462014-08-21 12:50:31 +00005251 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005252 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00005253 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00005254 case ISD::SETUNE:
Justin Bognerb03fd122016-08-17 05:10:15 +00005255 case ISD::SETNE: Invert = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005256 case ISD::SETOEQ:
5257 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
5258 case ISD::SETOLT:
Justin Bognerb03fd122016-08-17 05:10:15 +00005259 case ISD::SETLT: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005260 case ISD::SETOGT:
5261 case ISD::SETGT: Opc = ARMISD::VCGT; break;
5262 case ISD::SETOLE:
Justin Bognerb03fd122016-08-17 05:10:15 +00005263 case ISD::SETLE: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005264 case ISD::SETOGE:
5265 case ISD::SETGE: Opc = ARMISD::VCGE; break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005266 case ISD::SETUGE: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005267 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005268 case ISD::SETUGT: Swap = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005269 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005270 case ISD::SETUEQ: Invert = true; LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005271 case ISD::SETONE:
5272 // Expand this to (OLT | OGT).
5273 TmpOp0 = Op0;
5274 TmpOp1 = Op1;
5275 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00005276 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
5277 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00005278 break;
Justin Bognerb03fd122016-08-17 05:10:15 +00005279 case ISD::SETUO:
5280 Invert = true;
5281 LLVM_FALLTHROUGH;
Bob Wilson2e076c42009-06-22 23:27:02 +00005282 case ISD::SETO:
5283 // Expand this to (OLT | OGE).
5284 TmpOp0 = Op0;
5285 TmpOp1 = Op1;
5286 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00005287 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
5288 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00005289 break;
5290 }
5291 } else {
5292 // Integer comparisons.
5293 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00005294 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00005295 case ISD::SETNE: Invert = true;
5296 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
5297 case ISD::SETLT: Swap = true;
5298 case ISD::SETGT: Opc = ARMISD::VCGT; break;
5299 case ISD::SETLE: Swap = true;
5300 case ISD::SETGE: Opc = ARMISD::VCGE; break;
5301 case ISD::SETULT: Swap = true;
5302 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
5303 case ISD::SETULE: Swap = true;
5304 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
5305 }
5306
Nick Lewyckya21d3da2009-07-08 03:04:38 +00005307 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00005308 if (Opc == ARMISD::VCEQ) {
5309
5310 SDValue AndOp;
5311 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
5312 AndOp = Op0;
5313 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
5314 AndOp = Op1;
5315
5316 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00005317 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00005318 AndOp = AndOp.getOperand(0);
5319
5320 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
5321 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00005322 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
5323 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00005324 Invert = !Invert;
5325 }
5326 }
5327 }
5328
5329 if (Swap)
5330 std::swap(Op0, Op1);
5331
Owen Andersonc7baee32010-11-08 23:21:22 +00005332 // If one of the operands is a constant vector zero, attempt to fold the
5333 // comparison to a specialized compare-against-zero form.
5334 SDValue SingleOp;
5335 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
5336 SingleOp = Op0;
5337 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
5338 if (Opc == ARMISD::VCGE)
5339 Opc = ARMISD::VCLEZ;
5340 else if (Opc == ARMISD::VCGT)
5341 Opc = ARMISD::VCLTZ;
5342 SingleOp = Op1;
5343 }
Wesley Peck527da1b2010-11-23 03:31:01 +00005344
Owen Andersonc7baee32010-11-08 23:21:22 +00005345 SDValue Result;
5346 if (SingleOp.getNode()) {
5347 switch (Opc) {
5348 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00005349 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005350 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00005351 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005352 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00005353 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005354 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00005355 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005356 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00005357 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00005358 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00005359 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00005360 }
5361 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00005362 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00005363 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005364
Tim Northover45aa89c2015-02-08 00:50:47 +00005365 Result = DAG.getSExtOrTrunc(Result, dl, VT);
5366
Bob Wilson2e076c42009-06-22 23:27:02 +00005367 if (Invert)
5368 Result = DAG.getNOT(dl, Result, VT);
5369
5370 return Result;
5371}
5372
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00005373static SDValue LowerSETCCE(SDValue Op, SelectionDAG &DAG) {
5374 SDValue LHS = Op.getOperand(0);
5375 SDValue RHS = Op.getOperand(1);
5376 SDValue Carry = Op.getOperand(2);
5377 SDValue Cond = Op.getOperand(3);
5378 SDLoc DL(Op);
5379
5380 assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only.");
5381
5382 assert(Carry.getOpcode() != ISD::CARRY_FALSE);
5383 SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
5384 SDValue Cmp = DAG.getNode(ARMISD::SUBE, DL, VTs, LHS, RHS, Carry);
5385
5386 SDValue FVal = DAG.getConstant(0, DL, MVT::i32);
5387 SDValue TVal = DAG.getConstant(1, DL, MVT::i32);
5388 SDValue ARMcc = DAG.getConstant(
5389 IntCCToARMCC(cast<CondCodeSDNode>(Cond)->get()), DL, MVT::i32);
5390 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
5391 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), DL, ARM::CPSR,
5392 Cmp.getValue(1), SDValue());
5393 return DAG.getNode(ARMISD::CMOV, DL, Op.getValueType(), FVal, TVal, ARMcc,
5394 CCR, Chain.getValue(1));
5395}
5396
Bob Wilson5b2b5042010-06-14 22:19:57 +00005397/// isNEONModifiedImm - Check if the specified splat value corresponds to a
5398/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00005399/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00005400static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
5401 unsigned SplatBitSize, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005402 const SDLoc &dl, EVT &VT, bool is128Bits,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005403 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005404 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00005405
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005406 // SplatBitSize is set to the smallest size that splats the vector, so a
5407 // zero vector will always have SplatBitSize == 8. However, NEON modified
5408 // immediate instructions others than VMOV do not support the 8-bit encoding
5409 // of a zero vector, and the default encoding of zero is supposed to be the
5410 // 32-bit version.
5411 if (SplatBits == 0)
5412 SplatBitSize = 32;
5413
Bob Wilson2e076c42009-06-22 23:27:02 +00005414 switch (SplatBitSize) {
5415 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00005416 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00005417 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005418 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00005419 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005420 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00005421 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00005422 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005423 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00005424
5425 case 16:
5426 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00005427 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005428 if ((SplatBits & ~0xff) == 0) {
5429 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005430 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005431 Imm = SplatBits;
5432 break;
5433 }
5434 if ((SplatBits & ~0xff00) == 0) {
5435 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005436 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00005437 Imm = SplatBits >> 8;
5438 break;
5439 }
5440 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005441
5442 case 32:
5443 // NEON's 32-bit VMOV supports splat values where:
5444 // * only one byte is nonzero, or
5445 // * the least significant byte is 0xff and the second byte is nonzero, or
5446 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00005447 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00005448 if ((SplatBits & ~0xff) == 0) {
5449 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005450 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005451 Imm = SplatBits;
5452 break;
5453 }
5454 if ((SplatBits & ~0xff00) == 0) {
5455 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005456 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00005457 Imm = SplatBits >> 8;
5458 break;
5459 }
5460 if ((SplatBits & ~0xff0000) == 0) {
5461 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005462 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00005463 Imm = SplatBits >> 16;
5464 break;
5465 }
5466 if ((SplatBits & ~0xff000000) == 0) {
5467 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005468 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00005469 Imm = SplatBits >> 24;
5470 break;
5471 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005472
Owen Andersona4076922010-11-05 21:57:54 +00005473 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
5474 if (type == OtherModImm) return SDValue();
5475
Bob Wilson2e076c42009-06-22 23:27:02 +00005476 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005477 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
5478 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005479 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00005480 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005481 break;
5482 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005483
5484 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00005485 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
5486 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005487 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00005488 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00005489 break;
5490 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005491
5492 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
5493 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
5494 // VMOV.I32. A (very) minor optimization would be to replicate the value
5495 // and fall through here to test for a valid 64-bit splat. But, then the
5496 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00005497 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005498
5499 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00005500 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00005501 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005502 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00005503 uint64_t BitMask = 0xff;
5504 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00005505 unsigned ImmMask = 1;
5506 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00005507 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00005508 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005509 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00005510 Imm |= ImmMask;
5511 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00005512 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00005513 }
Bob Wilson2e076c42009-06-22 23:27:02 +00005514 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00005515 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00005516 }
Christian Pirker6f81e752014-06-23 18:05:53 +00005517
Mehdi Aminiffc14022015-07-08 01:00:38 +00005518 if (DAG.getDataLayout().isBigEndian())
Christian Pirker6f81e752014-06-23 18:05:53 +00005519 // swap higher and lower 32 bit word
5520 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
5521
Bob Wilson6eae5202010-06-11 21:34:50 +00005522 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00005523 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00005524 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00005525 break;
5526 }
5527
Bob Wilson6eae5202010-06-11 21:34:50 +00005528 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00005529 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00005530 }
5531
Bob Wilsona3f19012010-07-13 21:16:48 +00005532 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005533 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00005534}
5535
Lang Hames591cdaf2012-03-29 21:56:11 +00005536SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
5537 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005538 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00005539 return SDValue();
5540
Tim Northoverf79c3a52013-08-20 08:57:11 +00005541 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00005542 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005543
Oliver Stannard51b1d462014-08-21 12:50:31 +00005544 // Use the default (constant pool) lowering for double constants when we have
5545 // an SP-only FPU
5546 if (IsDouble && Subtarget->isFPOnlySP())
5547 return SDValue();
5548
Lang Hames591cdaf2012-03-29 21:56:11 +00005549 // Try splatting with a VMOV.f32...
Benjamin Kramer46e38f32016-06-08 10:01:20 +00005550 const APFloat &FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00005551 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
5552
Lang Hames591cdaf2012-03-29 21:56:11 +00005553 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00005554 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
5555 // We have code in place to select a valid ConstantFP already, no need to
5556 // do any mangling.
5557 return Op;
5558 }
5559
5560 // It's a float and we are trying to use NEON operations where
5561 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005562 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005563 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00005564 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
5565 NewVal);
5566 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005567 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005568 }
5569
Tim Northoverf79c3a52013-08-20 08:57:11 +00005570 // The rest of our options are NEON only, make sure that's allowed before
5571 // proceeding..
5572 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
5573 return SDValue();
5574
Lang Hames591cdaf2012-03-29 21:56:11 +00005575 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00005576 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
5577
5578 // It wouldn't really be worth bothering for doubles except for one very
5579 // important value, which does happen to match: 0.0. So make sure we don't do
5580 // anything stupid.
5581 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
5582 return SDValue();
5583
5584 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005585 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
5586 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005587 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005588 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005589 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
5590 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005591 if (IsDouble)
5592 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5593
5594 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005595 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5596 VecConstant);
5597 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005598 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005599 }
5600
5601 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005602 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00005603 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00005604 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005605 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00005606 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00005607
5608 if (IsDouble)
5609 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
5610
5611 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00005612 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
5613 VecConstant);
5614 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005615 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00005616 }
5617
5618 return SDValue();
5619}
5620
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005621// check if an VEXT instruction can handle the shuffle mask when the
5622// vector sources of the shuffle are the same.
5623static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
5624 unsigned NumElts = VT.getVectorNumElements();
5625
5626 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5627 if (M[0] < 0)
5628 return false;
5629
5630 Imm = M[0];
5631
5632 // If this is a VEXT shuffle, the immediate value is the index of the first
5633 // element. The other shuffle indices must be the successive elements after
5634 // the first one.
5635 unsigned ExpectedElt = Imm;
5636 for (unsigned i = 1; i < NumElts; ++i) {
5637 // Increment the expected index. If it wraps around, just follow it
5638 // back to index zero and keep going.
5639 ++ExpectedElt;
5640 if (ExpectedElt == NumElts)
5641 ExpectedElt = 0;
5642
5643 if (M[i] < 0) continue; // ignore UNDEF indices
5644 if (ExpectedElt != static_cast<unsigned>(M[i]))
5645 return false;
5646 }
5647
5648 return true;
5649}
5650
Lang Hames591cdaf2012-03-29 21:56:11 +00005651
Benjamin Kramer339ced42012-01-15 13:16:05 +00005652static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005653 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005654 unsigned NumElts = VT.getVectorNumElements();
5655 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00005656
5657 // Assume that the first shuffle index is not UNDEF. Fail if it is.
5658 if (M[0] < 0)
5659 return false;
5660
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005661 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00005662
5663 // If this is a VEXT shuffle, the immediate value is the index of the first
5664 // element. The other shuffle indices must be the successive elements after
5665 // the first one.
5666 unsigned ExpectedElt = Imm;
5667 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00005668 // Increment the expected index. If it wraps around, it may still be
5669 // a VEXT but the source vectors must be swapped.
5670 ExpectedElt += 1;
5671 if (ExpectedElt == NumElts * 2) {
5672 ExpectedElt = 0;
5673 ReverseVEXT = true;
5674 }
5675
Bob Wilson411dfad2010-08-17 05:54:34 +00005676 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005677 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00005678 return false;
5679 }
5680
5681 // Adjust the index value if the source operands will be swapped.
5682 if (ReverseVEXT)
5683 Imm -= NumElts;
5684
Bob Wilson32cd8552009-08-19 17:03:43 +00005685 return true;
5686}
5687
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005688/// isVREVMask - Check if a vector shuffle corresponds to a VREV
5689/// instruction with the specified blocksize. (The order of the elements
5690/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00005691static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005692 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
5693 "Only possible block sizes for VREV are: 16, 32, 64");
5694
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005695 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005696 if (EltSz == 64)
5697 return false;
5698
5699 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005700 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00005701 // If the first shuffle index is UNDEF, be optimistic.
5702 if (M[0] < 0)
5703 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005704
5705 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
5706 return false;
5707
5708 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005709 if (M[i] < 0) continue; // ignore UNDEF indices
5710 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00005711 return false;
5712 }
5713
5714 return true;
5715}
5716
Benjamin Kramer339ced42012-01-15 13:16:05 +00005717static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00005718 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
5719 // range, then 0 is placed into the resulting vector. So pretty much any mask
5720 // of 8 elements can work here.
5721 return VT == MVT::v8i8 && M.size() == 8;
5722}
5723
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005724// Checks whether the shuffle mask represents a vector transpose (VTRN) by
5725// checking that pairs of elements in the shuffle mask represent the same index
5726// in each vector, incrementing the expected index by 2 at each step.
5727// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 2, 6]
5728// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,c,g}
5729// v2={e,f,g,h}
5730// WhichResult gives the offset for each element in the mask based on which
5731// of the two results it belongs to.
5732//
5733// The transpose can be represented either as:
5734// result1 = shufflevector v1, v2, result1_shuffle_mask
5735// result2 = shufflevector v1, v2, result2_shuffle_mask
5736// where v1/v2 and the shuffle masks have the same number of elements
5737// (here WhichResult (see below) indicates which result is being checked)
5738//
5739// or as:
5740// results = shufflevector v1, v2, shuffle_mask
5741// where both results are returned in one vector and the shuffle mask has twice
5742// as many elements as v1/v2 (here WhichResult will always be 0 if true) here we
5743// want to check the low half and high half of the shuffle mask as if it were
5744// the other case
Benjamin Kramer339ced42012-01-15 13:16:05 +00005745static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005746 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005747 if (EltSz == 64)
5748 return false;
5749
Bob Wilsona7062312009-08-21 20:54:19 +00005750 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005751 if (M.size() != NumElts && M.size() != NumElts*2)
5752 return false;
5753
James Molloy8c995a92015-09-10 08:42:28 +00005754 // If the mask is twice as long as the input vector then we need to check the
5755 // upper and lower parts of the mask with a matching value for WhichResult
5756 // FIXME: A mask with only even values will be rejected in case the first
5757 // element is undefined, e.g. [-1, 4, 2, 6] will be rejected, because only
5758 // M[0] is used to determine WhichResult
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005759 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005760 if (M.size() == NumElts * 2)
5761 WhichResult = i / NumElts;
5762 else
5763 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005764 for (unsigned j = 0; j < NumElts; j += 2) {
5765 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5766 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + NumElts + WhichResult))
5767 return false;
5768 }
Bob Wilsona7062312009-08-21 20:54:19 +00005769 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005770
5771 if (M.size() == NumElts*2)
5772 WhichResult = 0;
5773
Bob Wilsona7062312009-08-21 20:54:19 +00005774 return true;
5775}
5776
Bob Wilson0bbd3072009-12-03 06:40:55 +00005777/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
5778/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5779/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005780static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005781 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson0bbd3072009-12-03 06:40:55 +00005782 if (EltSz == 64)
5783 return false;
5784
5785 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005786 if (M.size() != NumElts && M.size() != NumElts*2)
5787 return false;
5788
5789 for (unsigned i = 0; i < M.size(); i += NumElts) {
James Molloy8c995a92015-09-10 08:42:28 +00005790 if (M.size() == NumElts * 2)
5791 WhichResult = i / NumElts;
5792 else
5793 WhichResult = M[i] == 0 ? 0 : 1;
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005794 for (unsigned j = 0; j < NumElts; j += 2) {
5795 if ((M[i+j] >= 0 && (unsigned) M[i+j] != j + WhichResult) ||
5796 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != j + WhichResult))
5797 return false;
5798 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005799 }
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005800
5801 if (M.size() == NumElts*2)
5802 WhichResult = 0;
5803
Bob Wilson0bbd3072009-12-03 06:40:55 +00005804 return true;
5805}
5806
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005807// Checks whether the shuffle mask represents a vector unzip (VUZP) by checking
5808// that the mask elements are either all even and in steps of size 2 or all odd
5809// and in steps of size 2.
5810// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 2, 4, 6]
5811// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,c,e,g}
5812// v2={e,f,g,h}
5813// Requires similar checks to that of isVTRNMask with
5814// respect the how results are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005815static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005816 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005817 if (EltSz == 64)
5818 return false;
5819
Bob Wilsona7062312009-08-21 20:54:19 +00005820 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005821 if (M.size() != NumElts && M.size() != NumElts*2)
5822 return false;
5823
5824 for (unsigned i = 0; i < M.size(); i += NumElts) {
5825 WhichResult = M[i] == 0 ? 0 : 1;
5826 for (unsigned j = 0; j < NumElts; ++j) {
5827 if (M[i+j] >= 0 && (unsigned) M[i+j] != 2 * j + WhichResult)
5828 return false;
5829 }
Bob Wilsona7062312009-08-21 20:54:19 +00005830 }
5831
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005832 if (M.size() == NumElts*2)
5833 WhichResult = 0;
5834
Bob Wilsona7062312009-08-21 20:54:19 +00005835 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005836 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005837 return false;
5838
5839 return true;
5840}
5841
Bob Wilson0bbd3072009-12-03 06:40:55 +00005842/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
5843/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5844/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005845static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005846 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson0bbd3072009-12-03 06:40:55 +00005847 if (EltSz == 64)
5848 return false;
5849
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005850 unsigned NumElts = VT.getVectorNumElements();
5851 if (M.size() != NumElts && M.size() != NumElts*2)
5852 return false;
5853
5854 unsigned Half = NumElts / 2;
5855 for (unsigned i = 0; i < M.size(); i += NumElts) {
5856 WhichResult = M[i] == 0 ? 0 : 1;
5857 for (unsigned j = 0; j < NumElts; j += Half) {
5858 unsigned Idx = WhichResult;
5859 for (unsigned k = 0; k < Half; ++k) {
5860 int MIdx = M[i + j + k];
5861 if (MIdx >= 0 && (unsigned) MIdx != Idx)
5862 return false;
5863 Idx += 2;
5864 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005865 }
5866 }
5867
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005868 if (M.size() == NumElts*2)
5869 WhichResult = 0;
5870
Bob Wilson0bbd3072009-12-03 06:40:55 +00005871 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5872 if (VT.is64BitVector() && EltSz == 32)
5873 return false;
5874
5875 return true;
5876}
5877
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005878// Checks whether the shuffle mask represents a vector zip (VZIP) by checking
5879// that pairs of elements of the shufflemask represent the same index in each
5880// vector incrementing sequentially through the vectors.
5881// e.g. For v1,v2 of type v4i32 a valid shuffle mask is: [0, 4, 1, 5]
5882// v1={a,b,c,d} => x=shufflevector v1, v2 shufflemask => x={a,e,b,f}
5883// v2={e,f,g,h}
5884// Requires similar checks to that of isVTRNMask with respect the how results
5885// are returned.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005886static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005887 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00005888 if (EltSz == 64)
5889 return false;
5890
Bob Wilsona7062312009-08-21 20:54:19 +00005891 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005892 if (M.size() != NumElts && M.size() != NumElts*2)
5893 return false;
5894
5895 for (unsigned i = 0; i < M.size(); i += NumElts) {
5896 WhichResult = M[i] == 0 ? 0 : 1;
5897 unsigned Idx = WhichResult * NumElts / 2;
5898 for (unsigned j = 0; j < NumElts; j += 2) {
5899 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5900 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx + NumElts))
5901 return false;
5902 Idx += 1;
5903 }
Bob Wilsona7062312009-08-21 20:54:19 +00005904 }
5905
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005906 if (M.size() == NumElts*2)
5907 WhichResult = 0;
5908
Bob Wilsona7062312009-08-21 20:54:19 +00005909 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005910 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005911 return false;
5912
5913 return true;
5914}
5915
Bob Wilson0bbd3072009-12-03 06:40:55 +00005916/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5917/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5918/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005919static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Sanjay Patel1ed771f2016-09-14 16:37:15 +00005920 unsigned EltSz = VT.getScalarSizeInBits();
Bob Wilson0bbd3072009-12-03 06:40:55 +00005921 if (EltSz == 64)
5922 return false;
5923
5924 unsigned NumElts = VT.getVectorNumElements();
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005925 if (M.size() != NumElts && M.size() != NumElts*2)
5926 return false;
5927
5928 for (unsigned i = 0; i < M.size(); i += NumElts) {
5929 WhichResult = M[i] == 0 ? 0 : 1;
5930 unsigned Idx = WhichResult * NumElts / 2;
5931 for (unsigned j = 0; j < NumElts; j += 2) {
5932 if ((M[i+j] >= 0 && (unsigned) M[i+j] != Idx) ||
5933 (M[i+j+1] >= 0 && (unsigned) M[i+j+1] != Idx))
5934 return false;
5935 Idx += 1;
5936 }
Bob Wilson0bbd3072009-12-03 06:40:55 +00005937 }
5938
Luke Cheeseman4d45ff22015-07-24 09:57:05 +00005939 if (M.size() == NumElts*2)
5940 WhichResult = 0;
5941
Bob Wilson0bbd3072009-12-03 06:40:55 +00005942 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5943 if (VT.is64BitVector() && EltSz == 32)
5944 return false;
5945
5946 return true;
5947}
5948
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00005949/// Check if \p ShuffleMask is a NEON two-result shuffle (VZIP, VUZP, VTRN),
5950/// and return the corresponding ARMISD opcode if it is, or 0 if it isn't.
5951static unsigned isNEONTwoResultShuffleMask(ArrayRef<int> ShuffleMask, EVT VT,
5952 unsigned &WhichResult,
5953 bool &isV_UNDEF) {
5954 isV_UNDEF = false;
5955 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5956 return ARMISD::VTRN;
5957 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5958 return ARMISD::VUZP;
5959 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5960 return ARMISD::VZIP;
5961
5962 isV_UNDEF = true;
5963 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5964 return ARMISD::VTRN;
5965 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5966 return ARMISD::VUZP;
5967 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5968 return ARMISD::VZIP;
5969
5970 return 0;
5971}
5972
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005973/// \return true if this is a reverse operation on an vector.
5974static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5975 unsigned NumElts = VT.getVectorNumElements();
5976 // Make sure the mask has the right size.
5977 if (NumElts != M.size())
5978 return false;
5979
5980 // Look for <15, ..., 3, -1, 1, 0>.
5981 for (unsigned i = 0; i != NumElts; ++i)
5982 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5983 return false;
5984
5985 return true;
5986}
5987
Dale Johannesen2bff5052010-07-29 20:10:08 +00005988// If N is an integer constant that can be moved into a register in one
5989// instruction, return an SDValue of such a constant (will become a MOV
5990// instruction). Otherwise return null.
5991static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005992 const ARMSubtarget *ST, const SDLoc &dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005993 uint64_t Val;
5994 if (!isa<ConstantSDNode>(N))
5995 return SDValue();
5996 Val = cast<ConstantSDNode>(N)->getZExtValue();
5997
5998 if (ST->isThumb1Only()) {
5999 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006000 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006001 } else {
6002 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006003 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006004 }
6005 return SDValue();
6006}
6007
Bob Wilson2e076c42009-06-22 23:27:02 +00006008// If this is a case we can't handle, return null and let the default
6009// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00006010SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
6011 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00006012 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006013 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006014 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00006015
6016 APInt SplatBits, SplatUndef;
6017 unsigned SplatBitSize;
6018 bool HasAnyUndefs;
6019 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00006020 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00006021 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00006022 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00006023 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00006024 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006025 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00006026 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00006027 if (Val.getNode()) {
6028 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00006029 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00006030 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00006031
6032 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00006033 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00006034 Val = isNEONModifiedImm(NegatedImm,
6035 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006036 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00006037 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00006038 if (Val.getNode()) {
6039 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00006040 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00006041 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00006042
6043 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00006044 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00006045 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00006046 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006047 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00006048 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
6049 }
6050 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00006051 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00006052 }
6053
Bob Wilson91fdf682010-05-22 00:23:12 +00006054 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00006055 //
6056 // As an optimisation, even if more than one value is used it may be more
6057 // profitable to splat with one value then change some lanes.
6058 //
6059 // Heuristically we decide to do this if the vector has a "dominant" value,
6060 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00006061 unsigned NumElts = VT.getVectorNumElements();
6062 bool isOnlyLowElement = true;
6063 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00006064 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00006065 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00006066
6067 // Map of the number of times a particular SDValue appears in the
6068 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00006069 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00006070 SDValue Value;
6071 for (unsigned i = 0; i < NumElts; ++i) {
6072 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006073 if (V.isUndef())
Bob Wilson91fdf682010-05-22 00:23:12 +00006074 continue;
6075 if (i > 0)
6076 isOnlyLowElement = false;
6077 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
6078 isConstant = false;
6079
James Molloy49bdbce2012-09-06 09:55:02 +00006080 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00006081 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00006082
James Molloy49bdbce2012-09-06 09:55:02 +00006083 // Is this value dominant? (takes up more than half of the lanes)
6084 if (++Count > (NumElts / 2)) {
6085 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00006086 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00006087 }
Bob Wilson91fdf682010-05-22 00:23:12 +00006088 }
James Molloy49bdbce2012-09-06 09:55:02 +00006089 if (ValueCounts.size() != 1)
6090 usesOnlyOneValue = false;
6091 if (!Value.getNode() && ValueCounts.size() > 0)
6092 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00006093
James Molloy49bdbce2012-09-06 09:55:02 +00006094 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00006095 return DAG.getUNDEF(VT);
6096
Quentin Colombet0f2fe742013-07-23 22:34:47 +00006097 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
6098 // Keep going if we are hitting this case.
6099 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00006100 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
6101
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006102 unsigned EltSize = VT.getScalarSizeInBits();
Dale Johannesen2bff5052010-07-29 20:10:08 +00006103
Dale Johannesen710a2d92010-10-19 20:00:17 +00006104 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
6105 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00006106 if (hasDominantValue && EltSize <= 32) {
6107 if (!isConstant) {
6108 SDValue N;
6109
6110 // If we are VDUPing a value that comes directly from a vector, that will
6111 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00006112 // just use VDUPLANE. We can only do this if the lane being extracted
6113 // is at a constant index, as the VDUP from lane instructions only have
6114 // constant-index forms.
Artyom Skrobov314ee042015-11-25 19:41:11 +00006115 ConstantSDNode *constIndex;
Jim Grosbacha3c5c762013-03-02 20:16:24 +00006116 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Artyom Skrobov314ee042015-11-25 19:41:11 +00006117 (constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1)))) {
Silviu Barangab1409702012-10-15 09:41:32 +00006118 // We need to create a new undef vector to use for the VDUPLANE if the
6119 // size of the vector from which we get the value is different than the
6120 // size of the vector that we need to create. We will insert the element
6121 // such that the register coalescer will remove unnecessary copies.
6122 if (VT != Value->getOperand(0).getValueType()) {
Silviu Barangab1409702012-10-15 09:41:32 +00006123 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
6124 VT.getVectorNumElements();
6125 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
6126 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006127 Value, DAG.getConstant(index, dl, MVT::i32)),
6128 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00006129 } else
Silviu Barangab1409702012-10-15 09:41:32 +00006130 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00006131 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00006132 } else
James Molloy49bdbce2012-09-06 09:55:02 +00006133 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
6134
6135 if (!usesOnlyOneValue) {
6136 // The dominant value was splatted as 'N', but we now have to insert
6137 // all differing elements.
6138 for (unsigned I = 0; I < NumElts; ++I) {
6139 if (Op.getOperand(I) == Value)
6140 continue;
6141 SmallVector<SDValue, 3> Ops;
6142 Ops.push_back(N);
6143 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006144 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00006145 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00006146 }
6147 }
6148 return N;
6149 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00006150 if (VT.getVectorElementType().isFloatingPoint()) {
6151 SmallVector<SDValue, 8> Ops;
6152 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00006153 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00006154 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00006155 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006156 SDValue Val = DAG.getBuildVector(VecVT, dl, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00006157 Val = LowerBUILD_VECTOR(Val, DAG, ST);
6158 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00006159 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006160 }
James Molloy49bdbce2012-09-06 09:55:02 +00006161 if (usesOnlyOneValue) {
6162 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
6163 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00006164 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00006165 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00006166 }
6167
6168 // If all elements are constants and the case above didn't get hit, fall back
6169 // to the default expansion, which will generate a load from the constant
6170 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00006171 if (isConstant)
6172 return SDValue();
6173
Bob Wilson6f2b8962011-01-07 21:37:30 +00006174 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
6175 if (NumElts >= 4) {
6176 SDValue shuffle = ReconstructShuffle(Op, DAG);
6177 if (shuffle != SDValue())
6178 return shuffle;
6179 }
6180
Bob Wilson91fdf682010-05-22 00:23:12 +00006181 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00006182 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
6183 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00006184 if (EltSize >= 32) {
6185 // Do the expansion with floating-point types, since that is what the VFP
6186 // registers are defined to use, and since i64 is not legal.
6187 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6188 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006189 SmallVector<SDValue, 8> Ops;
6190 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00006191 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00006192 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006193 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006194 }
6195
Jim Grosbach24e102a2013-07-08 18:18:52 +00006196 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
6197 // know the default expansion would otherwise fall back on something even
6198 // worse. For a vector with one or two non-undef values, that's
6199 // scalar_to_vector for the elements followed by a shuffle (provided the
6200 // shuffle is valid for the target) and materialization element by element
6201 // on the stack followed by a load for everything else.
6202 if (!isConstant && !usesOnlyOneValue) {
6203 SDValue Vec = DAG.getUNDEF(VT);
6204 for (unsigned i = 0 ; i < NumElts; ++i) {
6205 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006206 if (V.isUndef())
Jim Grosbach24e102a2013-07-08 18:18:52 +00006207 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006208 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00006209 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
6210 }
6211 return Vec;
6212 }
6213
Bob Wilson2e076c42009-06-22 23:27:02 +00006214 return SDValue();
6215}
6216
Bob Wilson6f2b8962011-01-07 21:37:30 +00006217// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00006218// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00006219SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
6220 SelectionDAG &DAG) const {
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006221 assert(Op.getOpcode() == ISD::BUILD_VECTOR && "Unknown opcode!");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006222 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006223 EVT VT = Op.getValueType();
6224 unsigned NumElts = VT.getVectorNumElements();
6225
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006226 struct ShuffleSourceInfo {
6227 SDValue Vec;
6228 unsigned MinElt;
6229 unsigned MaxElt;
Andrew Trick5eb0a302011-01-19 02:26:13 +00006230
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006231 // We may insert some combination of BITCASTs and VEXT nodes to force Vec to
6232 // be compatible with the shuffle we intend to construct. As a result
6233 // ShuffleVec will be some sliding window into the original Vec.
6234 SDValue ShuffleVec;
6235
6236 // Code should guarantee that element i in Vec starts at element "WindowBase
6237 // + i * WindowScale in ShuffleVec".
6238 int WindowBase;
6239 int WindowScale;
6240
6241 bool operator ==(SDValue OtherVec) { return Vec == OtherVec; }
6242 ShuffleSourceInfo(SDValue Vec)
6243 : Vec(Vec), MinElt(UINT_MAX), MaxElt(0), ShuffleVec(Vec), WindowBase(0),
6244 WindowScale(1) {}
6245 };
6246
6247 // First gather all vectors used as an immediate source for this BUILD_VECTOR
6248 // node.
6249 SmallVector<ShuffleSourceInfo, 2> Sources;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006250 for (unsigned i = 0; i < NumElts; ++i) {
6251 SDValue V = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006252 if (V.isUndef())
Bob Wilson6f2b8962011-01-07 21:37:30 +00006253 continue;
6254 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
6255 // A shuffle can only come from building a vector from various
6256 // elements of other vectors.
6257 return SDValue();
Ahmed Bougacha699a9dd2015-09-01 21:56:00 +00006258 } else if (!isa<ConstantSDNode>(V.getOperand(1))) {
6259 // Furthermore, shuffles require a constant mask, whereas extractelts
6260 // accept variable indices.
6261 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00006262 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006263
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006264 // Add this element source to the list if it's not already there.
Bob Wilson6f2b8962011-01-07 21:37:30 +00006265 SDValue SourceVec = V.getOperand(0);
David Majnemer0d955d02016-08-11 22:21:41 +00006266 auto Source = find(Sources, SourceVec);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006267 if (Source == Sources.end())
6268 Source = Sources.insert(Sources.end(), ShuffleSourceInfo(SourceVec));
Andrew Trick5eb0a302011-01-19 02:26:13 +00006269
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006270 // Update the minimum and maximum lane number seen.
6271 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
6272 Source->MinElt = std::min(Source->MinElt, EltNo);
6273 Source->MaxElt = std::max(Source->MaxElt, EltNo);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006274 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006275
Bob Wilson6f2b8962011-01-07 21:37:30 +00006276 // Currently only do something sane when at most two source vectors
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006277 // are involved.
6278 if (Sources.size() > 2)
Bob Wilson6f2b8962011-01-07 21:37:30 +00006279 return SDValue();
6280
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006281 // Find out the smallest element size among result and two sources, and use
6282 // it as element size to build the shuffle_vector.
6283 EVT SmallestEltTy = VT.getVectorElementType();
6284 for (auto &Source : Sources) {
6285 EVT SrcEltTy = Source.Vec.getValueType().getVectorElementType();
6286 if (SrcEltTy.bitsLT(SmallestEltTy))
6287 SmallestEltTy = SrcEltTy;
6288 }
6289 unsigned ResMultiplier =
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006290 VT.getScalarSizeInBits() / SmallestEltTy.getSizeInBits();
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006291 NumElts = VT.getSizeInBits() / SmallestEltTy.getSizeInBits();
6292 EVT ShuffleVT = EVT::getVectorVT(*DAG.getContext(), SmallestEltTy, NumElts);
Andrew Trick5eb0a302011-01-19 02:26:13 +00006293
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006294 // If the source vector is too wide or too narrow, we may nevertheless be able
6295 // to construct a compatible shuffle either by concatenating it with UNDEF or
6296 // extracting a suitable range of elements.
6297 for (auto &Src : Sources) {
6298 EVT SrcVT = Src.ShuffleVec.getValueType();
6299
6300 if (SrcVT.getSizeInBits() == VT.getSizeInBits())
Bob Wilson6f2b8962011-01-07 21:37:30 +00006301 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006302
6303 // This stage of the search produces a source with the same element type as
6304 // the original, but with a total width matching the BUILD_VECTOR output.
6305 EVT EltVT = SrcVT.getVectorElementType();
6306 unsigned NumSrcElts = VT.getSizeInBits() / EltVT.getSizeInBits();
6307 EVT DestVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumSrcElts);
6308
6309 if (SrcVT.getSizeInBits() < VT.getSizeInBits()) {
6310 if (2 * SrcVT.getSizeInBits() != VT.getSizeInBits())
6311 return SDValue();
6312 // We can pad out the smaller vector for free, so if it's part of a
6313 // shuffle...
6314 Src.ShuffleVec =
6315 DAG.getNode(ISD::CONCAT_VECTORS, dl, DestVT, Src.ShuffleVec,
6316 DAG.getUNDEF(Src.ShuffleVec.getValueType()));
6317 continue;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006318 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006319
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006320 if (SrcVT.getSizeInBits() != 2 * VT.getSizeInBits())
6321 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006322
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006323 if (Src.MaxElt - Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00006324 // Span too large for a VEXT to cope
6325 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006326 }
6327
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006328 if (Src.MinElt >= NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00006329 // The extraction can just take the second half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006330 Src.ShuffleVec =
6331 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6332 DAG.getConstant(NumSrcElts, dl, MVT::i32));
6333 Src.WindowBase = -NumSrcElts;
6334 } else if (Src.MaxElt < NumSrcElts) {
Bob Wilson6f2b8962011-01-07 21:37:30 +00006335 // The extraction can just take the first half
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006336 Src.ShuffleVec =
6337 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6338 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00006339 } else {
6340 // An actual VEXT is needed
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006341 SDValue VEXTSrc1 =
6342 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6343 DAG.getConstant(0, dl, MVT::i32));
6344 SDValue VEXTSrc2 =
6345 DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, DestVT, Src.ShuffleVec,
6346 DAG.getConstant(NumSrcElts, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006347
6348 Src.ShuffleVec = DAG.getNode(ARMISD::VEXT, dl, DestVT, VEXTSrc1,
6349 VEXTSrc2,
Jeroen Ketema41681a52015-09-21 20:28:04 +00006350 DAG.getConstant(Src.MinElt, dl, MVT::i32));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006351 Src.WindowBase = -Src.MinElt;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006352 }
6353 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006354
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006355 // Another possible incompatibility occurs from the vector element types. We
6356 // can fix this by bitcasting the source vectors to the same type we intend
6357 // for the shuffle.
6358 for (auto &Src : Sources) {
6359 EVT SrcEltTy = Src.ShuffleVec.getValueType().getVectorElementType();
6360 if (SrcEltTy == SmallestEltTy)
Bob Wilson6f2b8962011-01-07 21:37:30 +00006361 continue;
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006362 assert(ShuffleVT.getVectorElementType() == SmallestEltTy);
6363 Src.ShuffleVec = DAG.getNode(ISD::BITCAST, dl, ShuffleVT, Src.ShuffleVec);
6364 Src.WindowScale = SrcEltTy.getSizeInBits() / SmallestEltTy.getSizeInBits();
6365 Src.WindowBase *= Src.WindowScale;
6366 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006367
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006368 // Final sanity check before we try to actually produce a shuffle.
Silviu Barangaa07090f2015-08-07 12:05:46 +00006369 DEBUG(
6370 for (auto Src : Sources)
6371 assert(Src.ShuffleVec.getValueType() == ShuffleVT);
6372 );
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006373
6374 // The stars all align, our next step is to produce the mask for the shuffle.
6375 SmallVector<int, 8> Mask(ShuffleVT.getVectorNumElements(), -1);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006376 int BitsPerShuffleLane = ShuffleVT.getScalarSizeInBits();
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006377 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
6378 SDValue Entry = Op.getOperand(i);
Sanjay Patel57195842016-03-14 17:28:46 +00006379 if (Entry.isUndef())
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006380 continue;
6381
David Majnemer0d955d02016-08-11 22:21:41 +00006382 auto Src = find(Sources, Entry.getOperand(0));
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006383 int EltNo = cast<ConstantSDNode>(Entry.getOperand(1))->getSExtValue();
6384
6385 // EXTRACT_VECTOR_ELT performs an implicit any_ext; BUILD_VECTOR an implicit
6386 // trunc. So only std::min(SrcBits, DestBits) actually get defined in this
6387 // segment.
6388 EVT OrigEltTy = Entry.getOperand(0).getValueType().getVectorElementType();
6389 int BitsDefined = std::min(OrigEltTy.getSizeInBits(),
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006390 VT.getScalarSizeInBits());
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006391 int LanesDefined = BitsDefined / BitsPerShuffleLane;
6392
6393 // This source is expected to fill ResMultiplier lanes of the final shuffle,
6394 // starting at the appropriate offset.
6395 int *LaneMask = &Mask[i * ResMultiplier];
6396
6397 int ExtractBase = EltNo * Src->WindowScale + Src->WindowBase;
6398 ExtractBase += NumElts * (Src - Sources.begin());
6399 for (int j = 0; j < LanesDefined; ++j)
6400 LaneMask[j] = ExtractBase + j;
Bob Wilson6f2b8962011-01-07 21:37:30 +00006401 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00006402
Bob Wilson6f2b8962011-01-07 21:37:30 +00006403 // Final check before we try to produce nonsense...
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006404 if (!isShuffleMaskLegal(Mask, ShuffleVT))
6405 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00006406
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006407 // We can't handle more than two sources. This should have already
6408 // been checked before this point.
6409 assert(Sources.size() <= 2 && "Too many sources!");
6410
6411 SDValue ShuffleOps[] = { DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT) };
6412 for (unsigned i = 0; i < Sources.size(); ++i)
6413 ShuffleOps[i] = Sources[i].ShuffleVec;
6414
6415 SDValue Shuffle = DAG.getVectorShuffle(ShuffleVT, dl, ShuffleOps[0],
Craig Topper2bd8b4b2016-07-01 06:54:47 +00006416 ShuffleOps[1], Mask);
Silviu Baranga3e8e51c2015-08-07 11:40:46 +00006417 return DAG.getNode(ISD::BITCAST, dl, VT, Shuffle);
Bob Wilson6f2b8962011-01-07 21:37:30 +00006418}
6419
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006420/// isShuffleMaskLegal - Targets can use this to indicate that they only
6421/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6422/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6423/// are assumed to be legal.
6424bool
6425ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6426 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006427 if (VT.getVectorNumElements() == 4 &&
6428 (VT.is128BitVector() || VT.is64BitVector())) {
6429 unsigned PFIndexes[4];
6430 for (unsigned i = 0; i != 4; ++i) {
6431 if (M[i] < 0)
6432 PFIndexes[i] = 8;
6433 else
6434 PFIndexes[i] = M[i];
6435 }
6436
6437 // Compute the index in the perfect shuffle table.
6438 unsigned PFTableIndex =
6439 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
6440 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6441 unsigned Cost = (PFEntry >> 30);
6442
6443 if (Cost <= 4)
6444 return true;
6445 }
6446
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006447 bool ReverseVEXT, isV_UNDEF;
Bob Wilsona7062312009-08-21 20:54:19 +00006448 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006449
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006450 unsigned EltSize = VT.getScalarSizeInBits();
Bob Wilson846bd792010-06-07 23:53:38 +00006451 return (EltSize >= 32 ||
6452 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006453 isVREVMask(M, VT, 64) ||
6454 isVREVMask(M, VT, 32) ||
6455 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00006456 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00006457 isVTBLMask(M, VT) ||
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006458 isNEONTwoResultShuffleMask(M, VT, WhichResult, isV_UNDEF) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006459 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006460}
6461
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006462/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
6463/// the specified operations to build the shuffle.
6464static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
6465 SDValue RHS, SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00006466 const SDLoc &dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006467 unsigned OpNum = (PFEntry >> 26) & 0x0F;
6468 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
6469 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
6470
6471 enum {
6472 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
6473 OP_VREV,
6474 OP_VDUP0,
6475 OP_VDUP1,
6476 OP_VDUP2,
6477 OP_VDUP3,
6478 OP_VEXT1,
6479 OP_VEXT2,
6480 OP_VEXT3,
6481 OP_VUZPL, // VUZP, left result
6482 OP_VUZPR, // VUZP, right result
6483 OP_VZIPL, // VZIP, left result
6484 OP_VZIPR, // VZIP, right result
6485 OP_VTRNL, // VTRN, left result
6486 OP_VTRNR // VTRN, right result
6487 };
6488
6489 if (OpNum == OP_COPY) {
6490 if (LHSID == (1*9+2)*9+3) return LHS;
6491 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
6492 return RHS;
6493 }
6494
6495 SDValue OpLHS, OpRHS;
6496 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
6497 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
6498 EVT VT = OpLHS.getValueType();
6499
6500 switch (OpNum) {
6501 default: llvm_unreachable("Unknown shuffle opcode!");
6502 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00006503 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00006504 if (VT.getVectorElementType() == MVT::i32 ||
6505 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00006506 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
6507 // vrev <4 x i16> -> VREV32
6508 if (VT.getVectorElementType() == MVT::i16)
6509 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
6510 // vrev <4 x i8> -> VREV16
6511 assert(VT.getVectorElementType() == MVT::i8);
6512 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006513 case OP_VDUP0:
6514 case OP_VDUP1:
6515 case OP_VDUP2:
6516 case OP_VDUP3:
6517 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006518 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006519 case OP_VEXT1:
6520 case OP_VEXT2:
6521 case OP_VEXT3:
6522 return DAG.getNode(ARMISD::VEXT, dl, VT,
6523 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006524 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006525 case OP_VUZPL:
6526 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006527 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006528 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
6529 case OP_VZIPL:
6530 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006531 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006532 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
6533 case OP_VTRNL:
6534 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00006535 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
6536 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006537 }
6538}
6539
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006540static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00006541 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006542 SelectionDAG &DAG) {
6543 // Check to see if we can use the VTBL instruction.
6544 SDValue V1 = Op.getOperand(0);
6545 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006546 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006547
6548 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00006549 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006550 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006551 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006552
Sanjay Patel57195842016-03-14 17:28:46 +00006553 if (V2.getNode()->isUndef())
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006554 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006555 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00006556
Owen Anderson77aa2662011-04-05 21:48:57 +00006557 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006558 DAG.getBuildVector(MVT::v8i8, DL, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006559}
6560
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006561static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
6562 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006563 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006564 SDValue OpLHS = Op.getOperand(0);
6565 EVT VT = OpLHS.getValueType();
6566
6567 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
6568 "Expect an v8i16/v16i8 type");
6569 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
6570 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
6571 // extract the first 8 bytes into the top double word and the last 8 bytes
6572 // into the bottom double word. The v8i16 case is similar.
6573 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
6574 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006575 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006576}
6577
Bob Wilson2e076c42009-06-22 23:27:02 +00006578static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006579 SDValue V1 = Op.getOperand(0);
6580 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006581 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00006582 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006583 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00006584
Bob Wilsonc6800b52009-08-13 02:13:04 +00006585 // Convert shuffles that are directly supported on NEON to target-specific
6586 // DAG nodes, instead of keeping them as shuffles and matching them again
6587 // during code selection. This is more efficient and avoids the possibility
6588 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00006589 // FIXME: floating-point vectors should be canonicalized to integer vectors
6590 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00006591 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00006592
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006593 unsigned EltSize = VT.getScalarSizeInBits();
Bob Wilson846bd792010-06-07 23:53:38 +00006594 if (EltSize <= 32) {
Craig Topperbc56e3b2016-06-30 04:38:51 +00006595 if (SVN->isSplat()) {
Bob Wilson846bd792010-06-07 23:53:38 +00006596 int Lane = SVN->getSplatIndex();
6597 // If this is undef splat, generate it via "just" vdup, if possible.
6598 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00006599
Dan Gohman198b7ff2011-11-03 21:49:52 +00006600 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00006601 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
6602 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6603 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00006604 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
6605 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
6606 // reaches it).
6607 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
6608 !isa<ConstantSDNode>(V1.getOperand(0))) {
6609 bool IsScalarToVector = true;
6610 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
Sanjay Patel75068522016-03-14 18:09:43 +00006611 if (!V1.getOperand(i).isUndef()) {
Dan Gohman198b7ff2011-11-03 21:49:52 +00006612 IsScalarToVector = false;
6613 break;
6614 }
6615 if (IsScalarToVector)
6616 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
6617 }
Bob Wilson846bd792010-06-07 23:53:38 +00006618 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006619 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00006620 }
Bob Wilson846bd792010-06-07 23:53:38 +00006621
6622 bool ReverseVEXT;
6623 unsigned Imm;
6624 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
6625 if (ReverseVEXT)
6626 std::swap(V1, V2);
6627 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006628 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00006629 }
6630
6631 if (isVREVMask(ShuffleMask, VT, 64))
6632 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
6633 if (isVREVMask(ShuffleMask, VT, 32))
6634 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
6635 if (isVREVMask(ShuffleMask, VT, 16))
6636 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
6637
Sanjay Patel57195842016-03-14 17:28:46 +00006638 if (V2->isUndef() && isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006639 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006640 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00006641 }
6642
Bob Wilson846bd792010-06-07 23:53:38 +00006643 // Check for Neon shuffles that modify both input vectors in place.
6644 // If both results are used, i.e., if there are two shuffles with the same
6645 // source operands and with masks corresponding to both results of one of
6646 // these operations, DAG memoization will ensure that a single node is
6647 // used for both shuffles.
6648 unsigned WhichResult;
Ahmed Bougacha2ffa91f2015-06-19 02:25:01 +00006649 bool isV_UNDEF;
6650 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6651 ShuffleMask, VT, WhichResult, isV_UNDEF)) {
6652 if (isV_UNDEF)
6653 V2 = V1;
6654 return DAG.getNode(ShuffleOpc, dl, DAG.getVTList(VT, VT), V1, V2)
6655 .getValue(WhichResult);
6656 }
Bob Wilson846bd792010-06-07 23:53:38 +00006657
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006658 // Also check for these shuffles through CONCAT_VECTORS: we canonicalize
6659 // shuffles that produce a result larger than their operands with:
6660 // shuffle(concat(v1, undef), concat(v2, undef))
6661 // ->
6662 // shuffle(concat(v1, v2), undef)
6663 // because we can access quad vectors (see PerformVECTOR_SHUFFLECombine).
6664 //
6665 // This is useful in the general case, but there are special cases where
6666 // native shuffles produce larger results: the two-result ops.
6667 //
6668 // Look through the concat when lowering them:
6669 // shuffle(concat(v1, v2), undef)
6670 // ->
6671 // concat(VZIP(v1, v2):0, :1)
6672 //
Sanjay Patel57195842016-03-14 17:28:46 +00006673 if (V1->getOpcode() == ISD::CONCAT_VECTORS && V2->isUndef()) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006674 SDValue SubV1 = V1->getOperand(0);
6675 SDValue SubV2 = V1->getOperand(1);
6676 EVT SubVT = SubV1.getValueType();
6677
6678 // We expect these to have been canonicalized to -1.
David Majnemer0a16c222016-08-11 21:15:00 +00006679 assert(all_of(ShuffleMask, [&](int i) {
Ahmed Bougacha9a909422015-06-19 02:32:35 +00006680 return i < (int)VT.getVectorNumElements();
6681 }) && "Unexpected shuffle index into UNDEF operand!");
6682
6683 if (unsigned ShuffleOpc = isNEONTwoResultShuffleMask(
6684 ShuffleMask, SubVT, WhichResult, isV_UNDEF)) {
6685 if (isV_UNDEF)
6686 SubV2 = SubV1;
6687 assert((WhichResult == 0) &&
6688 "In-place shuffle of concat can only have one result!");
6689 SDValue Res = DAG.getNode(ShuffleOpc, dl, DAG.getVTList(SubVT, SubVT),
6690 SubV1, SubV2);
6691 return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Res.getValue(0),
6692 Res.getValue(1));
6693 }
6694 }
Bob Wilsoncce31f62009-08-14 05:08:32 +00006695 }
Bob Wilson32cd8552009-08-19 17:03:43 +00006696
Bob Wilsona7062312009-08-21 20:54:19 +00006697 // If the shuffle is not directly supported and it has 4 elements, use
6698 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00006699 unsigned NumElts = VT.getVectorNumElements();
6700 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006701 unsigned PFIndexes[4];
6702 for (unsigned i = 0; i != 4; ++i) {
6703 if (ShuffleMask[i] < 0)
6704 PFIndexes[i] = 8;
6705 else
6706 PFIndexes[i] = ShuffleMask[i];
6707 }
6708
6709 // Compute the index in the perfect shuffle table.
6710 unsigned PFTableIndex =
6711 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00006712 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
6713 unsigned Cost = (PFEntry >> 30);
6714
6715 if (Cost <= 4)
6716 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
6717 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00006718
Bob Wilsond8a9a042010-06-04 00:04:02 +00006719 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00006720 if (EltSize >= 32) {
6721 // Do the expansion with floating-point types, since that is what the VFP
6722 // registers are defined to use, and since i64 is not legal.
6723 EVT EltVT = EVT::getFloatingPointVT(EltSize);
6724 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00006725 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
6726 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00006727 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00006728 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00006729 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00006730 Ops.push_back(DAG.getUNDEF(EltVT));
6731 else
6732 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
6733 ShuffleMask[i] < (int)NumElts ? V1 : V2,
6734 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006735 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00006736 }
Craig Topper48d114b2014-04-26 18:35:24 +00006737 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00006738 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00006739 }
6740
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00006741 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
6742 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
6743
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00006744 if (VT == MVT::v8i8)
6745 if (SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG))
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006746 return NewOp;
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006747
Bob Wilson6f34e272009-08-14 05:16:33 +00006748 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00006749}
6750
Eli Friedmana5e244c2011-10-24 23:08:52 +00006751static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
6752 // INSERT_VECTOR_ELT is legal only for immediate indexes.
6753 SDValue Lane = Op.getOperand(2);
6754 if (!isa<ConstantSDNode>(Lane))
6755 return SDValue();
6756
6757 return Op;
6758}
6759
Bob Wilson2e076c42009-06-22 23:27:02 +00006760static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00006761 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00006762 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00006763 if (!isa<ConstantSDNode>(Lane))
6764 return SDValue();
6765
6766 SDValue Vec = Op.getOperand(0);
Sanjay Patel284582b2016-09-14 16:54:10 +00006767 if (Op.getValueType() == MVT::i32 && Vec.getScalarValueSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006768 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00006769 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
6770 }
6771
6772 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00006773}
6774
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006775static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6776 // The only time a CONCAT_VECTORS operation can have legal types is when
6777 // two 64-bit vectors are concatenated to a 128-bit vector.
6778 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
6779 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00006780 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00006781 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006782 SDValue Op0 = Op.getOperand(0);
6783 SDValue Op1 = Op.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +00006784 if (!Op0.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006785 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006786 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006787 DAG.getIntPtrConstant(0, dl));
Sanjay Patel75068522016-03-14 18:09:43 +00006788 if (!Op1.isUndef())
Owen Anderson9f944592009-08-11 20:47:22 +00006789 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00006790 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006791 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00006792 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00006793}
6794
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006795/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
6796/// element has been zero/sign-extended, depending on the isSigned parameter,
6797/// from an integer type half its size.
6798static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
6799 bool isSigned) {
6800 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
6801 EVT VT = N->getValueType(0);
6802 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
6803 SDNode *BVN = N->getOperand(0).getNode();
6804 if (BVN->getValueType(0) != MVT::v4i32 ||
6805 BVN->getOpcode() != ISD::BUILD_VECTOR)
6806 return false;
Mehdi Aminiffc14022015-07-08 01:00:38 +00006807 unsigned LoElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006808 unsigned HiElt = 1 - LoElt;
6809 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
6810 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
6811 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
6812 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
6813 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
6814 return false;
6815 if (isSigned) {
6816 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
6817 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
6818 return true;
6819 } else {
6820 if (Hi0->isNullValue() && Hi1->isNullValue())
6821 return true;
6822 }
6823 return false;
6824 }
6825
6826 if (N->getOpcode() != ISD::BUILD_VECTOR)
6827 return false;
6828
6829 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
6830 SDNode *Elt = N->getOperand(i).getNode();
6831 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006832 unsigned EltSize = VT.getScalarSizeInBits();
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006833 unsigned HalfSize = EltSize / 2;
6834 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006835 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006836 return false;
6837 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00006838 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006839 return false;
6840 }
6841 continue;
6842 }
6843 return false;
6844 }
6845
6846 return true;
6847}
6848
6849/// isSignExtended - Check if a node is a vector value that is sign-extended
6850/// or a constant BUILD_VECTOR with sign-extended elements.
6851static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
6852 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
6853 return true;
6854 if (isExtendedBUILD_VECTOR(N, DAG, true))
6855 return true;
6856 return false;
6857}
6858
6859/// isZeroExtended - Check if a node is a vector value that is zero-extended
6860/// or a constant BUILD_VECTOR with zero-extended elements.
6861static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
6862 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
6863 return true;
6864 if (isExtendedBUILD_VECTOR(N, DAG, false))
6865 return true;
6866 return false;
6867}
6868
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006869static EVT getExtensionTo64Bits(const EVT &OrigVT) {
6870 if (OrigVT.getSizeInBits() >= 64)
6871 return OrigVT;
6872
6873 assert(OrigVT.isSimple() && "Expecting a simple value type");
6874
6875 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
6876 switch (OrigSimpleTy) {
6877 default: llvm_unreachable("Unexpected Vector Type");
6878 case MVT::v2i8:
6879 case MVT::v2i16:
6880 return MVT::v2i32;
6881 case MVT::v4i8:
6882 return MVT::v4i16;
6883 }
6884}
6885
Sebastian Popa204f722012-11-30 19:08:04 +00006886/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
6887/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
6888/// We insert the required extension here to get the vector to fill a D register.
6889static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
6890 const EVT &OrigTy,
6891 const EVT &ExtTy,
6892 unsigned ExtOpcode) {
6893 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
6894 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
6895 // 64-bits we need to insert a new extension so that it will be 64-bits.
6896 assert(ExtTy.is128BitVector() && "Unexpected extension size");
6897 if (OrigTy.getSizeInBits() >= 64)
6898 return N;
6899
6900 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006901 EVT NewVT = getExtensionTo64Bits(OrigTy);
6902
Andrew Trickef9de2a2013-05-25 02:42:55 +00006903 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00006904}
6905
6906/// SkipLoadExtensionForVMULL - return a load of the original vector size that
6907/// does not do any sign/zero extension. If the original vector is less
6908/// than 64 bits, an appropriate extension will be added after the load to
6909/// reach a total size of 64 bits. We have to add the extension separately
6910/// because ARM does not have a sign/zero extending load for vectors.
6911static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006912 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
6913
6914 // The load already has the right type.
6915 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006916 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Justin Lebar9c375812016-07-15 18:27:10 +00006917 LD->getBasePtr(), LD->getPointerInfo(),
6918 LD->getAlignment(), LD->getMemOperand()->getFlags());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006919
6920 // We need to create a zextload/sextload. We cannot just create a load
6921 // followed by a zext/zext node because LowerMUL is also run during normal
6922 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006923 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00006924 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Justin Lebar9c375812016-07-15 18:27:10 +00006925 LD->getMemoryVT(), LD->getAlignment(),
6926 LD->getMemOperand()->getFlags());
Sebastian Popa204f722012-11-30 19:08:04 +00006927}
6928
6929/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
6930/// extending load, or BUILD_VECTOR with extended elements, return the
6931/// unextended value. The unextended vector should be 64 bits so that it can
6932/// be used as an operand to a VMULL instruction. If the original vector size
6933/// before extension is less than 64 bits we add a an extension to resize
6934/// the vector to 64 bits.
6935static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00006936 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00006937 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
6938 N->getOperand(0)->getValueType(0),
6939 N->getValueType(0),
6940 N->getOpcode());
6941
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006942 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00006943 return SkipLoadExtensionForVMULL(LD, DAG);
6944
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006945 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
6946 // have been legalized as a BITCAST from v4i32.
6947 if (N->getOpcode() == ISD::BITCAST) {
6948 SDNode *BVN = N->getOperand(0).getNode();
6949 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
6950 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
Mehdi Aminiffc14022015-07-08 01:00:38 +00006951 unsigned LowElt = DAG.getDataLayout().isBigEndian() ? 1 : 0;
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006952 return DAG.getBuildVector(
6953 MVT::v2i32, SDLoc(N),
6954 {BVN->getOperand(LowElt), BVN->getOperand(LowElt + 2)});
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006955 }
6956 // Construct a new BUILD_VECTOR with elements truncated to half the size.
6957 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
6958 EVT VT = N->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00006959 unsigned EltSize = VT.getScalarSizeInBits() / 2;
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006960 unsigned NumElts = VT.getVectorNumElements();
6961 MVT TruncVT = MVT::getIntegerVT(EltSize);
6962 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006963 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006964 for (unsigned i = 0; i != NumElts; ++i) {
6965 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
6966 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00006967 // Element types smaller than 32 bits are not legal, so use i32 elements.
6968 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006969 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00006970 }
Ahmed Bougacha128f8732016-04-26 21:15:30 +00006971 return DAG.getBuildVector(MVT::getVectorVT(TruncVT, NumElts), dl, Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006972}
6973
Evan Chenge2086e72011-03-29 01:56:09 +00006974static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
6975 unsigned Opcode = N->getOpcode();
6976 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6977 SDNode *N0 = N->getOperand(0).getNode();
6978 SDNode *N1 = N->getOperand(1).getNode();
6979 return N0->hasOneUse() && N1->hasOneUse() &&
6980 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
6981 }
6982 return false;
6983}
6984
6985static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6986 unsigned Opcode = N->getOpcode();
6987 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6988 SDNode *N0 = N->getOperand(0).getNode();
6989 SDNode *N1 = N->getOperand(1).getNode();
6990 return N0->hasOneUse() && N1->hasOneUse() &&
6991 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6992 }
6993 return false;
6994}
6995
Bob Wilson38ab35a2010-09-01 23:50:19 +00006996static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6997 // Multiplications are only custom-lowered for 128-bit vectors so that
6998 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6999 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00007000 assert(VT.is128BitVector() && VT.isInteger() &&
7001 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00007002 SDNode *N0 = Op.getOperand(0).getNode();
7003 SDNode *N1 = Op.getOperand(1).getNode();
7004 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00007005 bool isMLA = false;
7006 bool isN0SExt = isSignExtended(N0, DAG);
7007 bool isN1SExt = isSignExtended(N1, DAG);
7008 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00007009 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00007010 else {
7011 bool isN0ZExt = isZeroExtended(N0, DAG);
7012 bool isN1ZExt = isZeroExtended(N1, DAG);
7013 if (isN0ZExt && isN1ZExt)
7014 NewOpc = ARMISD::VMULLu;
7015 else if (isN1SExt || isN1ZExt) {
7016 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
7017 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
7018 if (isN1SExt && isAddSubSExt(N0, DAG)) {
7019 NewOpc = ARMISD::VMULLs;
7020 isMLA = true;
7021 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
7022 NewOpc = ARMISD::VMULLu;
7023 isMLA = true;
7024 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
7025 std::swap(N0, N1);
7026 NewOpc = ARMISD::VMULLu;
7027 isMLA = true;
7028 }
7029 }
7030
7031 if (!NewOpc) {
7032 if (VT == MVT::v2i64)
7033 // Fall through to expand this. It is not legal.
7034 return SDValue();
7035 else
7036 // Other vector multiplications are legal.
7037 return Op;
7038 }
7039 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00007040
7041 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007042 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00007043 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00007044 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00007045 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00007046 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00007047 assert(Op0.getValueType().is64BitVector() &&
7048 Op1.getValueType().is64BitVector() &&
7049 "unexpected types for extended operands to VMULL");
7050 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
7051 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00007052
Evan Chenge2086e72011-03-29 01:56:09 +00007053 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
7054 // isel lowering to take advantage of no-stall back to back vmul + vmla.
7055 // vmull q0, d4, d6
7056 // vmlal q0, d5, d6
7057 // is faster than
7058 // vaddl q0, d4, d5
7059 // vmovl q1, d6
7060 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00007061 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
7062 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00007063 EVT Op1VT = Op1.getValueType();
7064 return DAG.getNode(N0->getOpcode(), DL, VT,
7065 DAG.getNode(NewOpc, DL, VT,
7066 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
7067 DAG.getNode(NewOpc, DL, VT,
7068 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00007069}
7070
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007071static SDValue LowerSDIV_v4i8(SDValue X, SDValue Y, const SDLoc &dl,
7072 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00007073 // TODO: Should this propagate fast-math-flags?
7074
Nate Begemanfa62d502011-02-11 20:53:29 +00007075 // Convert to float
7076 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
7077 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
7078 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
7079 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
7080 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
7081 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
7082 // Get reciprocal estimate.
7083 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00007084 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007085 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7086 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00007087 // Because char has a smaller range than uchar, we can actually get away
7088 // without any newton steps. This requires that we use a weird bias
7089 // of 0xb000, however (again, this has been exhaustively tested).
7090 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
7091 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
7092 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007093 Y = DAG.getConstant(0xb000, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00007094 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
7095 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
7096 // Convert back to short.
7097 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
7098 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
7099 return X;
7100}
7101
Benjamin Kramerbdc49562016-06-12 15:39:02 +00007102static SDValue LowerSDIV_v4i16(SDValue N0, SDValue N1, const SDLoc &dl,
7103 SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00007104 // TODO: Should this propagate fast-math-flags?
7105
Nate Begemanfa62d502011-02-11 20:53:29 +00007106 SDValue N2;
7107 // Convert to float.
7108 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
7109 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
7110 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
7111 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
7112 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
7113 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007114
Nate Begemanfa62d502011-02-11 20:53:29 +00007115 // Use reciprocal estimate and one refinement step.
7116 // float4 recip = vrecpeq_f32(yf);
7117 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00007118 N2 = 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 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007121 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007122 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00007123 N1, N2);
7124 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7125 // Because short has a smaller range than ushort, we can actually get away
7126 // with only a single newton step. This requires that we use a weird bias
7127 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007128 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00007129 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
7130 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007131 N1 = DAG.getConstant(0x89, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00007132 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
7133 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
7134 // Convert back to integer and return.
7135 // return vmovn_s32(vcvt_s32_f32(result));
7136 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
7137 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
7138 return N0;
7139}
7140
7141static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
7142 EVT VT = Op.getValueType();
7143 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
7144 "unexpected type for custom-lowering ISD::SDIV");
7145
Andrew Trickef9de2a2013-05-25 02:42:55 +00007146 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00007147 SDValue N0 = Op.getOperand(0);
7148 SDValue N1 = Op.getOperand(1);
7149 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00007150
Nate Begemanfa62d502011-02-11 20:53:29 +00007151 if (VT == MVT::v8i8) {
7152 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
7153 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007154
Nate Begemanfa62d502011-02-11 20:53:29 +00007155 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007156 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007157 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007158 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007159 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007160 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007161 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007162 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007163
7164 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
7165 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
7166
7167 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
7168 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00007169
Nate Begemanfa62d502011-02-11 20:53:29 +00007170 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
7171 return N0;
7172 }
7173 return LowerSDIV_v4i16(N0, N1, dl, DAG);
7174}
7175
7176static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
Sanjay Patela2607012015-09-16 16:31:21 +00007177 // TODO: Should this propagate fast-math-flags?
Nate Begemanfa62d502011-02-11 20:53:29 +00007178 EVT VT = Op.getValueType();
7179 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
7180 "unexpected type for custom-lowering ISD::UDIV");
7181
Andrew Trickef9de2a2013-05-25 02:42:55 +00007182 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00007183 SDValue N0 = Op.getOperand(0);
7184 SDValue N1 = Op.getOperand(1);
7185 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00007186
Nate Begemanfa62d502011-02-11 20:53:29 +00007187 if (VT == MVT::v8i8) {
7188 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
7189 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007190
Nate Begemanfa62d502011-02-11 20:53:29 +00007191 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007192 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007193 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007194 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007195 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007196 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00007197 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007198 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00007199
Nate Begemanfa62d502011-02-11 20:53:29 +00007200 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
7201 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00007202
Nate Begemanfa62d502011-02-11 20:53:29 +00007203 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
7204 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00007205
7206 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007207 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
7208 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00007209 N0);
7210 return N0;
7211 }
Owen Anderson77aa2662011-04-05 21:48:57 +00007212
Nate Begemanfa62d502011-02-11 20:53:29 +00007213 // v4i16 sdiv ... Convert to float.
7214 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
7215 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
7216 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
7217 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
7218 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007219 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00007220
7221 // Use reciprocal estimate and two refinement steps.
7222 // float4 recip = vrecpeq_f32(yf);
7223 // recip *= vrecpsq_f32(yf, recip);
7224 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00007225 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007226 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
7227 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00007228 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007229 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007230 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00007231 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00007232 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007233 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007234 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00007235 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
7236 // Simply multiplying by the reciprocal estimate can leave us a few ulps
7237 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
7238 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00007239 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00007240 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
7241 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Ahmed Bougacha93cff7f2016-02-15 18:07:29 +00007242 N1 = DAG.getConstant(2, dl, MVT::v4i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00007243 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
7244 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
7245 // Convert back to integer and return.
7246 // return vmovn_u32(vcvt_s32_f32(result));
7247 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
7248 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
7249 return N0;
7250}
7251
Evan Chenge8916542011-08-30 01:34:54 +00007252static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
7253 EVT VT = Op.getNode()->getValueType(0);
7254 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
7255
7256 unsigned Opc;
7257 bool ExtraOp = false;
7258 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00007259 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00007260 case ISD::ADDC: Opc = ARMISD::ADDC; break;
7261 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
7262 case ISD::SUBC: Opc = ARMISD::SUBC; break;
7263 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
7264 }
7265
7266 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007267 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00007268 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007269 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00007270 Op.getOperand(1), Op.getOperand(2));
7271}
7272
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007273SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
7274 assert(Subtarget->isTargetDarwin());
7275
7276 // For iOS, we want to call an alternative entry point: __sincos_stret,
7277 // return values are passed via sret.
7278 SDLoc dl(Op);
7279 SDValue Arg = Op.getOperand(0);
7280 EVT ArgVT = Arg.getValueType();
7281 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
Mehdi Amini44ede332015-07-09 02:09:04 +00007282 auto PtrVT = getPointerTy(DAG.getDataLayout());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007283
Matthias Braun941a7052016-07-28 18:40:00 +00007284 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Tim Northover8b403662015-10-28 22:51:16 +00007285 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007286
7287 // Pair of floats / doubles used to pass the result.
Tim Northover8b403662015-10-28 22:51:16 +00007288 Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Mehdi Amini44ede332015-07-09 02:09:04 +00007289 auto &DL = DAG.getDataLayout();
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007290
7291 ArgListTy Args;
Tim Northover8b403662015-10-28 22:51:16 +00007292 bool ShouldUseSRet = Subtarget->isAPCS_ABI();
7293 SDValue SRet;
7294 if (ShouldUseSRet) {
7295 // Create stack object for sret.
7296 const uint64_t ByteSize = DL.getTypeAllocSize(RetTy);
7297 const unsigned StackAlign = DL.getPrefTypeAlignment(RetTy);
Matthias Braun941a7052016-07-28 18:40:00 +00007298 int FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
Tim Northover8b403662015-10-28 22:51:16 +00007299 SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy(DL));
7300
7301 ArgListEntry Entry;
7302 Entry.Node = SRet;
7303 Entry.Ty = RetTy->getPointerTo();
7304 Entry.isSExt = false;
7305 Entry.isZExt = false;
7306 Entry.isSRet = true;
7307 Args.push_back(Entry);
7308 RetTy = Type::getVoidTy(*DAG.getContext());
7309 }
7310
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007311 ArgListEntry Entry;
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007312 Entry.Node = Arg;
7313 Entry.Ty = ArgTy;
7314 Entry.isSExt = false;
7315 Entry.isZExt = false;
7316 Args.push_back(Entry);
7317
Saleem Abdulrasool4966f582015-09-20 03:19:09 +00007318 const char *LibcallName =
7319 (ArgVT == MVT::f64) ? "__sincos_stret" : "__sincosf_stret";
Tim Northover8b403662015-10-28 22:51:16 +00007320 RTLIB::Libcall LC =
7321 (ArgVT == MVT::f64) ? RTLIB::SINCOS_F64 : RTLIB::SINCOS_F32;
7322 CallingConv::ID CC = getLibcallCallingConv(LC);
Mehdi Amini44ede332015-07-09 02:09:04 +00007323 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy(DL));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007324
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00007325 TargetLowering::CallLoweringInfo CLI(DAG);
Tim Northover8b403662015-10-28 22:51:16 +00007326 CLI.setDebugLoc(dl)
7327 .setChain(DAG.getEntryNode())
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00007328 .setCallee(CC, RetTy, Callee, std::move(Args))
Tim Northover8b403662015-10-28 22:51:16 +00007329 .setDiscardResult(ShouldUseSRet);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007330 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
7331
Tim Northover8b403662015-10-28 22:51:16 +00007332 if (!ShouldUseSRet)
7333 return CallResult.first;
7334
Justin Lebar9c375812016-07-15 18:27:10 +00007335 SDValue LoadSin =
7336 DAG.getLoad(ArgVT, dl, CallResult.second, SRet, MachinePointerInfo());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007337
7338 // Address of cos field.
Mehdi Amini44ede332015-07-09 02:09:04 +00007339 SDValue Add = DAG.getNode(ISD::ADD, dl, PtrVT, SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007340 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Justin Lebar9c375812016-07-15 18:27:10 +00007341 SDValue LoadCos =
7342 DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add, MachinePointerInfo());
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007343
7344 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
7345 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
7346 LoadSin.getValue(0), LoadCos.getValue(0));
7347}
7348
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007349SDValue ARMTargetLowering::LowerWindowsDIVLibCall(SDValue Op, SelectionDAG &DAG,
Martell Maloned1229242015-11-26 15:34:03 +00007350 bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007351 SDValue &Chain) const {
7352 EVT VT = Op.getValueType();
7353 assert((VT == MVT::i32 || VT == MVT::i64) &&
7354 "unexpected type for custom lowering DIV");
7355 SDLoc dl(Op);
7356
7357 const auto &DL = DAG.getDataLayout();
7358 const auto &TLI = DAG.getTargetLoweringInfo();
7359
7360 const char *Name = nullptr;
Martell Maloned1229242015-11-26 15:34:03 +00007361 if (Signed)
7362 Name = (VT == MVT::i32) ? "__rt_sdiv" : "__rt_sdiv64";
7363 else
7364 Name = (VT == MVT::i32) ? "__rt_udiv" : "__rt_udiv64";
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007365
7366 SDValue ES = DAG.getExternalSymbol(Name, TLI.getPointerTy(DL));
7367
7368 ARMTargetLowering::ArgListTy Args;
7369
7370 for (auto AI : {1, 0}) {
7371 ArgListEntry Arg;
7372 Arg.Node = Op.getOperand(AI);
7373 Arg.Ty = Arg.Node.getValueType().getTypeForEVT(*DAG.getContext());
7374 Args.push_back(Arg);
7375 }
7376
7377 CallLoweringInfo CLI(DAG);
7378 CLI.setDebugLoc(dl)
7379 .setChain(Chain)
7380 .setCallee(CallingConv::ARM_AAPCS_VFP, VT.getTypeForEVT(*DAG.getContext()),
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +00007381 ES, std::move(Args));
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007382
7383 return LowerCallTo(CLI).first;
7384}
7385
Martell Maloned1229242015-11-26 15:34:03 +00007386SDValue ARMTargetLowering::LowerDIV_Windows(SDValue Op, SelectionDAG &DAG,
7387 bool Signed) const {
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00007388 assert(Op.getValueType() == MVT::i32 &&
7389 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007390 SDLoc dl(Op);
7391
7392 SDValue DBZCHK = DAG.getNode(ARMISD::WIN__DBZCHK, dl, MVT::Other,
7393 DAG.getEntryNode(), Op.getOperand(1));
7394
Martell Maloned1229242015-11-26 15:34:03 +00007395 return LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007396}
7397
Martin Storsjo04864f42016-10-07 13:28:53 +00007398static SDValue WinDBZCheckDenominator(SelectionDAG &DAG, SDNode *N, SDValue InChain) {
7399 SDLoc DL(N);
7400 SDValue Op = N->getOperand(1);
7401 if (N->getValueType(0) == MVT::i32)
7402 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain, Op);
7403 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
7404 DAG.getConstant(0, DL, MVT::i32));
7405 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Op,
7406 DAG.getConstant(1, DL, MVT::i32));
7407 return DAG.getNode(ARMISD::WIN__DBZCHK, DL, MVT::Other, InChain,
7408 DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi));
7409}
7410
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007411void ARMTargetLowering::ExpandDIV_Windows(
Martell Maloned1229242015-11-26 15:34:03 +00007412 SDValue Op, SelectionDAG &DAG, bool Signed,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007413 SmallVectorImpl<SDValue> &Results) const {
7414 const auto &DL = DAG.getDataLayout();
7415 const auto &TLI = DAG.getTargetLoweringInfo();
7416
Saleem Abdulrasool8e99f502015-09-25 05:41:02 +00007417 assert(Op.getValueType() == MVT::i64 &&
7418 "unexpected type for custom lowering DIV");
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007419 SDLoc dl(Op);
7420
Martin Storsjo04864f42016-10-07 13:28:53 +00007421 SDValue DBZCHK = WinDBZCheckDenominator(DAG, Op.getNode(), DAG.getEntryNode());
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007422
Martell Maloned1229242015-11-26 15:34:03 +00007423 SDValue Result = LowerWindowsDIVLibCall(Op, DAG, Signed, DBZCHK);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007424
7425 SDValue Lower = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Result);
7426 SDValue Upper = DAG.getNode(ISD::SRL, dl, MVT::i64, Result,
7427 DAG.getConstant(32, dl, TLI.getPointerTy(DL)));
7428 Upper = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Upper);
7429
7430 Results.push_back(Lower);
7431 Results.push_back(Upper);
7432}
7433
Eli Friedman10f9ce22011-09-15 22:26:18 +00007434static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
JF Bastien800f87a2016-04-06 21:19:33 +00007435 if (isStrongerThanMonotonic(cast<AtomicSDNode>(Op)->getOrdering()))
7436 // Acquire/Release load/store is not legal for targets without a dmb or
7437 // equivalent available.
7438 return SDValue();
Eli Friedmanba912e02011-09-15 22:18:49 +00007439
JF Bastien800f87a2016-04-06 21:19:33 +00007440 // Monotonic load/store is legal for all targets.
7441 return Op;
Eli Friedmanba912e02011-09-15 22:18:49 +00007442}
7443
Tim Northoverbc933082013-05-23 19:11:20 +00007444static void ReplaceREADCYCLECOUNTER(SDNode *N,
7445 SmallVectorImpl<SDValue> &Results,
7446 SelectionDAG &DAG,
7447 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007448 SDLoc DL(N);
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00007449 // Under Power Management extensions, the cycle-count is:
7450 // mrc p15, #0, <Rt>, c9, c13, #0
7451 SDValue Ops[] = { N->getOperand(0), // Chain
7452 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
7453 DAG.getConstant(15, DL, MVT::i32),
7454 DAG.getConstant(0, DL, MVT::i32),
7455 DAG.getConstant(9, DL, MVT::i32),
7456 DAG.getConstant(13, DL, MVT::i32),
7457 DAG.getConstant(0, DL, MVT::i32)
7458 };
Tim Northoverbc933082013-05-23 19:11:20 +00007459
Ahmed Bougachaf9c19da2015-08-28 01:49:59 +00007460 SDValue Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
7461 DAG.getVTList(MVT::i32, MVT::Other), Ops);
7462 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Cycles32,
7463 DAG.getConstant(0, DL, MVT::i32)));
7464 Results.push_back(Cycles32.getValue(1));
Tim Northoverbc933082013-05-23 19:11:20 +00007465}
7466
Tim Northover1ee27c72016-04-19 22:25:02 +00007467static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V) {
7468 SDLoc dl(V.getNode());
7469 SDValue VLo = DAG.getAnyExtOrTrunc(V, dl, MVT::i32);
7470 SDValue VHi = DAG.getAnyExtOrTrunc(
7471 DAG.getNode(ISD::SRL, dl, MVT::i64, V, DAG.getConstant(32, dl, MVT::i32)),
7472 dl, MVT::i32);
Tim Northoverb629c772016-04-18 21:48:55 +00007473 SDValue RegClass =
7474 DAG.getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
7475 SDValue SubReg0 = DAG.getTargetConstant(ARM::gsub_0, dl, MVT::i32);
7476 SDValue SubReg1 = DAG.getTargetConstant(ARM::gsub_1, dl, MVT::i32);
Tim Northover1ee27c72016-04-19 22:25:02 +00007477 const SDValue Ops[] = { RegClass, VLo, SubReg0, VHi, SubReg1 };
Tim Northoverb629c772016-04-18 21:48:55 +00007478 return SDValue(
7479 DAG.getMachineNode(TargetOpcode::REG_SEQUENCE, dl, MVT::Untyped, Ops), 0);
7480}
7481
7482static void ReplaceCMP_SWAP_64Results(SDNode *N,
7483 SmallVectorImpl<SDValue> & Results,
7484 SelectionDAG &DAG) {
7485 assert(N->getValueType(0) == MVT::i64 &&
7486 "AtomicCmpSwap on types less than 64 should be legal");
7487 SDValue Ops[] = {N->getOperand(1),
Tim Northover1ee27c72016-04-19 22:25:02 +00007488 createGPRPairNode(DAG, N->getOperand(2)),
7489 createGPRPairNode(DAG, N->getOperand(3)),
Tim Northoverb629c772016-04-18 21:48:55 +00007490 N->getOperand(0)};
7491 SDNode *CmpSwap = DAG.getMachineNode(
7492 ARM::CMP_SWAP_64, SDLoc(N),
7493 DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other), Ops);
7494
7495 MachineFunction &MF = DAG.getMachineFunction();
7496 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
7497 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
7498 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
7499
7500 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_0, SDLoc(N), MVT::i32,
7501 SDValue(CmpSwap, 0)));
7502 Results.push_back(DAG.getTargetExtractSubreg(ARM::gsub_1, SDLoc(N), MVT::i32,
7503 SDValue(CmpSwap, 0)));
7504 Results.push_back(SDValue(CmpSwap, 2));
7505}
7506
Dan Gohman21cea8a2010-04-17 15:26:15 +00007507SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007508 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00007509 default: llvm_unreachable("Don't know how to custom lower this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007510 case ISD::WRITE_REGISTER: return LowerWRITE_REGISTER(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007511 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00007512 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00007513 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00007514 switch (Subtarget->getTargetTriple().getObjectFormat()) {
7515 default: llvm_unreachable("unknown object format");
7516 case Triple::COFF:
7517 return LowerGlobalAddressWindows(Op, DAG);
7518 case Triple::ELF:
7519 return LowerGlobalAddressELF(Op, DAG);
7520 case Triple::MachO:
7521 return LowerGlobalAddressDarwin(Op, DAG);
7522 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00007523 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00007524 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007525 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
7526 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007527 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00007528 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00007529 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00007530 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00007531 case ISD::SINT_TO_FP:
7532 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
7533 case ISD::FP_TO_SINT:
7534 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00007535 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00007536 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00007537 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00007538 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00007539 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Matthias Braun3cd00c12015-07-16 22:34:16 +00007540 case ISD::EH_SJLJ_SETUP_DISPATCH: return LowerEH_SJLJ_SETUP_DISPATCH(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00007541 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
7542 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00007543 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007544 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00007545 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00007546 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +00007547 case ISD::SREM: return LowerREM(Op.getNode(), DAG);
7548 case ISD::UREM: return LowerREM(Op.getNode(), DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00007549 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00007550 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00007551 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Logan Chien0a43abc2015-07-13 15:37:30 +00007552 case ISD::CTTZ:
7553 case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00007554 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00007555 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +00007556 case ISD::SETCCE: return LowerSETCCE(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00007557 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00007558 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00007559 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00007560 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00007561 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00007562 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00007563 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00007564 case ISD::MUL: return LowerMUL(Op, DAG);
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00007565 case ISD::SDIV:
7566 if (Subtarget->isTargetWindows())
7567 return LowerDIV_Windows(Op, DAG, /* Signed */ true);
7568 return LowerSDIV(Op, DAG);
7569 case ISD::UDIV:
7570 if (Subtarget->isTargetWindows())
7571 return LowerDIV_Windows(Op, DAG, /* Signed */ false);
7572 return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00007573 case ISD::ADDC:
7574 case ISD::ADDE:
7575 case ISD::SUBC:
7576 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00007577 case ISD::SADDO:
7578 case ISD::UADDO:
7579 case ISD::SSUBO:
7580 case ISD::USUBO:
7581 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00007582 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00007583 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00007584 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00007585 case ISD::SDIVREM:
7586 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007587 case ISD::DYNAMIC_STACKALLOC:
7588 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
7589 return LowerDYNAMIC_STACKALLOC(Op, DAG);
7590 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00007591 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
7592 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007593 case ARMISD::WIN__DBZCHK: return SDValue();
Evan Cheng10043e22007-01-19 07:51:42 +00007594 }
Evan Cheng10043e22007-01-19 07:51:42 +00007595}
7596
Duncan Sands6ed40142008-12-01 11:39:25 +00007597/// ReplaceNodeResults - Replace the results of node with an illegal result
7598/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00007599void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007600 SmallVectorImpl<SDValue> &Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00007601 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00007602 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007603 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00007604 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00007605 llvm_unreachable("Don't know how to custom expand this!");
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00007606 case ISD::READ_REGISTER:
7607 ExpandREAD_REGISTER(N, Results, DAG);
7608 break;
Wesley Peck527da1b2010-11-23 03:31:01 +00007609 case ISD::BITCAST:
7610 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007611 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00007612 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00007613 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00007614 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00007615 break;
Scott Douglassbdef6042015-08-24 09:17:18 +00007616 case ISD::SREM:
7617 case ISD::UREM:
7618 Res = LowerREM(N, DAG);
7619 break;
Renato Golin175c6d62016-03-04 19:19:36 +00007620 case ISD::SDIVREM:
7621 case ISD::UDIVREM:
7622 Res = LowerDivRem(SDValue(N, 0), DAG);
7623 assert(Res.getNumOperands() == 2 && "DivRem needs two values");
7624 Results.push_back(Res.getValue(0));
7625 Results.push_back(Res.getValue(1));
7626 return;
Tim Northoverbc933082013-05-23 19:11:20 +00007627 case ISD::READCYCLECOUNTER:
7628 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
7629 return;
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007630 case ISD::UDIV:
Martell Maloned1229242015-11-26 15:34:03 +00007631 case ISD::SDIV:
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00007632 assert(Subtarget->isTargetWindows() && "can only expand DIV on Windows");
Martell Maloned1229242015-11-26 15:34:03 +00007633 return ExpandDIV_Windows(SDValue(N, 0), DAG, N->getOpcode() == ISD::SDIV,
7634 Results);
Tim Northoverb629c772016-04-18 21:48:55 +00007635 case ISD::ATOMIC_CMP_SWAP:
7636 ReplaceCMP_SWAP_64Results(N, Results, DAG);
7637 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00007638 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00007639 if (Res.getNode())
7640 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00007641}
Chris Lattnerf81d5882007-11-24 07:07:01 +00007642
Evan Cheng10043e22007-01-19 07:51:42 +00007643//===----------------------------------------------------------------------===//
7644// ARM Scheduler Hooks
7645//===----------------------------------------------------------------------===//
7646
Bill Wendling030b58e2011-10-06 22:18:16 +00007647/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
7648/// registers the function context.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007649void ARMTargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI,
7650 MachineBasicBlock *MBB,
7651 MachineBasicBlock *DispatchBB,
7652 int FI) const {
Oliver Stannard8331aae2016-08-08 15:28:31 +00007653 assert(!Subtarget->isROPI() && !Subtarget->isRWPI() &&
7654 "ROPI/RWPI not currently supported with SjLj");
Eric Christopher1889fdc2015-01-29 00:19:39 +00007655 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007656 DebugLoc dl = MI.getDebugLoc();
Bill Wendling374ee192011-10-03 21:25:38 +00007657 MachineFunction *MF = MBB->getParent();
7658 MachineRegisterInfo *MRI = &MF->getRegInfo();
7659 MachineConstantPool *MCP = MF->getConstantPool();
7660 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
7661 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00007662
Bill Wendling374ee192011-10-03 21:25:38 +00007663 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007664 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00007665
Bill Wendling374ee192011-10-03 21:25:38 +00007666 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00007667 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00007668 ARMConstantPoolValue *CPV =
7669 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
7670 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
7671
Craig Topper61e88f42014-11-21 05:58:21 +00007672 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
7673 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00007674
Bill Wendling030b58e2011-10-06 22:18:16 +00007675 // Grab constant pool and fixed stack memory operands.
7676 MachineMemOperand *CPMMO =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007677 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(*MF),
7678 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007679
7680 MachineMemOperand *FIMMOSt =
Alex Lorenze40c8a22015-08-11 23:09:45 +00007681 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(*MF, FI),
7682 MachineMemOperand::MOStore, 4, 4);
Bill Wendling030b58e2011-10-06 22:18:16 +00007683
7684 // Load the address of the dispatch MBB into the jump buffer.
7685 if (isThumb2) {
7686 // Incoming value: jbuf
7687 // ldr.n r5, LCPI1_1
7688 // orr r5, r5, #1
7689 // add r5, pc
7690 // str r5, [$jbuf, #+4] ; &jbuf[1]
7691 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7692 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
7693 .addConstantPoolIndex(CPI)
7694 .addMemOperand(CPMMO));
7695 // Set the low bit because of thumb mode.
7696 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7697 AddDefaultCC(
7698 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
7699 .addReg(NewVReg1, RegState::Kill)
7700 .addImm(0x01)));
7701 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7702 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
7703 .addReg(NewVReg2, RegState::Kill)
7704 .addImm(PCLabelId);
7705 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
7706 .addReg(NewVReg3, RegState::Kill)
7707 .addFrameIndex(FI)
7708 .addImm(36) // &jbuf[1] :: pc
7709 .addMemOperand(FIMMOSt));
7710 } else if (isThumb) {
7711 // Incoming value: jbuf
7712 // ldr.n r1, LCPI1_4
7713 // add r1, pc
7714 // mov r2, #1
7715 // orrs r1, r2
7716 // add r2, $jbuf, #+4 ; &jbuf[1]
7717 // str r1, [r2]
7718 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7719 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
7720 .addConstantPoolIndex(CPI)
7721 .addMemOperand(CPMMO));
7722 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7723 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
7724 .addReg(NewVReg1, RegState::Kill)
7725 .addImm(PCLabelId);
7726 // Set the low bit because of thumb mode.
7727 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7728 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
7729 .addReg(ARM::CPSR, RegState::Define)
7730 .addImm(1));
7731 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7732 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
7733 .addReg(ARM::CPSR, RegState::Define)
7734 .addReg(NewVReg2, RegState::Kill)
7735 .addReg(NewVReg3, RegState::Kill));
7736 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00007737 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
7738 .addFrameIndex(FI)
7739 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00007740 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
7741 .addReg(NewVReg4, RegState::Kill)
7742 .addReg(NewVReg5, RegState::Kill)
7743 .addImm(0)
7744 .addMemOperand(FIMMOSt));
7745 } else {
7746 // Incoming value: jbuf
7747 // ldr r1, LCPI1_1
7748 // add r1, pc, r1
7749 // str r1, [$jbuf, #+4] ; &jbuf[1]
7750 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7751 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
7752 .addConstantPoolIndex(CPI)
7753 .addImm(0)
7754 .addMemOperand(CPMMO));
7755 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7756 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
7757 .addReg(NewVReg1, RegState::Kill)
7758 .addImm(PCLabelId));
7759 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
7760 .addReg(NewVReg2, RegState::Kill)
7761 .addFrameIndex(FI)
7762 .addImm(36) // &jbuf[1] :: pc
7763 .addMemOperand(FIMMOSt));
7764 }
7765}
7766
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007767void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI,
Matthias Brauneec4efc2015-04-28 00:37:05 +00007768 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007769 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00007770 DebugLoc dl = MI.getDebugLoc();
Bill Wendling030b58e2011-10-06 22:18:16 +00007771 MachineFunction *MF = MBB->getParent();
7772 MachineRegisterInfo *MRI = &MF->getRegInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00007773 MachineFrameInfo &MFI = MF->getFrameInfo();
7774 int FI = MFI.getFunctionContextIndex();
Bill Wendling030b58e2011-10-06 22:18:16 +00007775
Craig Topper61e88f42014-11-21 05:58:21 +00007776 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
7777 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00007778
Bill Wendling362c1b02011-10-06 21:29:56 +00007779 // Get a mapping of the call site numbers to all of the landing pads they're
7780 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00007781 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
7782 unsigned MaxCSNum = 0;
7783 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00007784 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
7785 ++BB) {
Reid Kleckner0e288232015-08-27 23:27:47 +00007786 if (!BB->isEHPad()) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007787
7788 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
7789 // pad.
7790 for (MachineBasicBlock::iterator
7791 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
7792 if (!II->isEHLabel()) continue;
7793
7794 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007795 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00007796
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007797 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
7798 for (SmallVectorImpl<unsigned>::iterator
7799 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
7800 CSI != CSE; ++CSI) {
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00007801 CallSiteNumToLPad[*CSI].push_back(&*BB);
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007802 MaxCSNum = std::max(MaxCSNum, *CSI);
7803 }
Bill Wendling202803e2011-10-05 00:02:33 +00007804 break;
7805 }
7806 }
7807
7808 // Get an ordered list of the machine basic blocks for the jump table.
7809 std::vector<MachineBasicBlock*> LPadList;
Matthias Braunb30f2f512016-01-30 01:24:31 +00007810 SmallPtrSet<MachineBasicBlock*, 32> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00007811 LPadList.reserve(CallSiteNumToLPad.size());
7812 for (unsigned I = 1; I <= MaxCSNum; ++I) {
7813 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
7814 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00007815 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00007816 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00007817 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
7818 }
Bill Wendling202803e2011-10-05 00:02:33 +00007819 }
7820
Bill Wendlingf793e7e2011-10-05 23:28:57 +00007821 assert(!LPadList.empty() &&
7822 "No landing pad destinations for the dispatch jump table!");
7823
Bill Wendling362c1b02011-10-06 21:29:56 +00007824 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00007825 MachineJumpTableInfo *JTI =
7826 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
7827 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Bill Wendling202803e2011-10-05 00:02:33 +00007828
Bill Wendling362c1b02011-10-06 21:29:56 +00007829 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00007830
7831 // Shove the dispatch's address into the return slot in the function context.
7832 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
Reid Kleckner0e288232015-08-27 23:27:47 +00007833 DispatchBB->setIsEHPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00007834
Bill Wendling324be982011-10-05 00:39:32 +00007835 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007836 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00007837 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007838 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00007839 else
7840 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
7841
Eli Bendersky2e2ce492013-01-30 16:30:19 +00007842 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00007843 DispatchBB->addSuccessor(TrapBB);
7844
7845 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
7846 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00007847
Bill Wendling510fbcd2011-10-17 21:32:56 +00007848 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00007849 MF->insert(MF->end(), DispatchBB);
7850 MF->insert(MF->end(), DispContBB);
7851 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00007852
Bill Wendling030b58e2011-10-06 22:18:16 +00007853 // Insert code into the entry block that creates and registers the function
7854 // context.
7855 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
7856
Alex Lorenze40c8a22015-08-11 23:09:45 +00007857 MachineMemOperand *FIMMOLd = MF->getMachineMemOperand(
7858 MachinePointerInfo::getFixedStack(*MF, FI),
7859 MachineMemOperand::MOLoad | MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00007860
Chad Rosier1ec8e402012-11-06 23:05:24 +00007861 MachineInstrBuilder MIB;
7862 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
7863
7864 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
7865 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
7866
7867 // Add a register mask with no preserved registers. This results in all
Oliver Stannard50a74392016-10-11 10:06:59 +00007868 // registers being marked as clobbered. This can't work if the dispatch block
7869 // is in a Thumb1 function and is linked with ARM code which uses the FP
7870 // registers, as there is no way to preserve the FP registers in Thumb1 mode.
7871 MIB.addRegMask(RI.getSjLjDispatchPreservedMask(*MF));
Bob Wilsonf6d17282011-11-16 07:11:57 +00007872
Rafael Espindola0f898332016-06-20 16:43:17 +00007873 bool IsPositionIndependent = isPositionIndependent();
Bill Wendling85833f72011-10-18 22:49:07 +00007874 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00007875 if (Subtarget->isThumb2()) {
7876 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7877 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
7878 .addFrameIndex(FI)
7879 .addImm(4)
7880 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007881
Bill Wendling85833f72011-10-18 22:49:07 +00007882 if (NumLPads < 256) {
7883 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
7884 .addReg(NewVReg1)
7885 .addImm(LPadList.size()));
7886 } else {
7887 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7888 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00007889 .addImm(NumLPads & 0xFFFF));
7890
7891 unsigned VReg2 = VReg1;
7892 if ((NumLPads & 0xFFFF0000) != 0) {
7893 VReg2 = MRI->createVirtualRegister(TRC);
7894 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
7895 .addReg(VReg1)
7896 .addImm(NumLPads >> 16));
7897 }
7898
Bill Wendling85833f72011-10-18 22:49:07 +00007899 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
7900 .addReg(NewVReg1)
7901 .addReg(VReg2));
7902 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007903
Bill Wendling5626c662011-10-06 22:53:00 +00007904 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
7905 .addMBB(TrapBB)
7906 .addImm(ARMCC::HI)
7907 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00007908
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007909 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
7910 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007911 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00007912
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007913 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00007914 AddDefaultCC(
7915 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007916 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
7917 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00007918 .addReg(NewVReg1)
7919 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
7920
7921 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00007922 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00007923 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00007924 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007925 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00007926 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
7927 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
7928 .addFrameIndex(FI)
7929 .addImm(1)
7930 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00007931
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007932 if (NumLPads < 256) {
7933 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
7934 .addReg(NewVReg1)
7935 .addImm(NumLPads));
7936 } else {
7937 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00007938 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7939 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
7940
7941 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007942 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00007943 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00007944 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00007945 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00007946
7947 unsigned VReg1 = MRI->createVirtualRegister(TRC);
7948 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
7949 .addReg(VReg1, RegState::Define)
7950 .addConstantPoolIndex(Idx));
7951 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
7952 .addReg(NewVReg1)
7953 .addReg(VReg1));
7954 }
7955
Bill Wendlingb3d46782011-10-06 23:37:36 +00007956 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
7957 .addMBB(TrapBB)
7958 .addImm(ARMCC::HI)
7959 .addReg(ARM::CPSR);
7960
7961 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
7962 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
7963 .addReg(ARM::CPSR, RegState::Define)
7964 .addReg(NewVReg1)
7965 .addImm(2));
7966
7967 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00007968 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00007969 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00007970
7971 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
7972 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
7973 .addReg(ARM::CPSR, RegState::Define)
7974 .addReg(NewVReg2, RegState::Kill)
7975 .addReg(NewVReg3));
7976
Alex Lorenze40c8a22015-08-11 23:09:45 +00007977 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
7978 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendlingb3d46782011-10-06 23:37:36 +00007979
7980 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
7981 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
7982 .addReg(NewVReg4, RegState::Kill)
7983 .addImm(0)
7984 .addMemOperand(JTMMOLd));
7985
Chad Rosier96603432013-03-01 18:30:38 +00007986 unsigned NewVReg6 = NewVReg5;
Rafael Espindola0f898332016-06-20 16:43:17 +00007987 if (IsPositionIndependent) {
Chad Rosier96603432013-03-01 18:30:38 +00007988 NewVReg6 = MRI->createVirtualRegister(TRC);
7989 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
7990 .addReg(ARM::CPSR, RegState::Define)
7991 .addReg(NewVReg5, RegState::Kill)
7992 .addReg(NewVReg3));
7993 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00007994
7995 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
7996 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00007997 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00007998 } else {
7999 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
8000 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
8001 .addFrameIndex(FI)
8002 .addImm(4)
8003 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00008004
Bill Wendling4969dcd2011-10-18 22:52:20 +00008005 if (NumLPads < 256) {
8006 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
8007 .addReg(NewVReg1)
8008 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00008009 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00008010 unsigned VReg1 = MRI->createVirtualRegister(TRC);
8011 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00008012 .addImm(NumLPads & 0xFFFF));
8013
8014 unsigned VReg2 = VReg1;
8015 if ((NumLPads & 0xFFFF0000) != 0) {
8016 VReg2 = MRI->createVirtualRegister(TRC);
8017 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
8018 .addReg(VReg1)
8019 .addImm(NumLPads >> 16));
8020 }
8021
Bill Wendling4969dcd2011-10-18 22:52:20 +00008022 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
8023 .addReg(NewVReg1)
8024 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00008025 } else {
8026 MachineConstantPool *ConstantPool = MF->getConstantPool();
8027 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
8028 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
8029
8030 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008031 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00008032 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008033 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00008034 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8035
8036 unsigned VReg1 = MRI->createVirtualRegister(TRC);
8037 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
8038 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00008039 .addConstantPoolIndex(Idx)
8040 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00008041 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
8042 .addReg(NewVReg1)
8043 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00008044 }
8045
Bill Wendling5626c662011-10-06 22:53:00 +00008046 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
8047 .addMBB(TrapBB)
8048 .addImm(ARMCC::HI)
8049 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00008050
Bill Wendling973c8172011-10-18 22:11:18 +00008051 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00008052 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00008053 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00008054 .addReg(NewVReg1)
8055 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00008056 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
8057 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00008058 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00008059
Alex Lorenze40c8a22015-08-11 23:09:45 +00008060 MachineMemOperand *JTMMOLd = MF->getMachineMemOperand(
8061 MachinePointerInfo::getJumpTable(*MF), MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00008062 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00008063 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00008064 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
8065 .addReg(NewVReg3, RegState::Kill)
8066 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00008067 .addImm(0)
8068 .addMemOperand(JTMMOLd));
8069
Rafael Espindola0f898332016-06-20 16:43:17 +00008070 if (IsPositionIndependent) {
Chad Rosier96603432013-03-01 18:30:38 +00008071 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
8072 .addReg(NewVReg5, RegState::Kill)
8073 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00008074 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00008075 } else {
8076 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
8077 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00008078 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00008079 }
Bill Wendling5626c662011-10-06 22:53:00 +00008080 }
Bill Wendling202803e2011-10-05 00:02:33 +00008081
Bill Wendling324be982011-10-05 00:39:32 +00008082 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00008083 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00008084 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00008085 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
8086 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00008087 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00008088 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00008089 }
8090
Bill Wendling26d27802011-10-17 05:25:09 +00008091 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00008092 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00008093 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00008094 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008095
8096 // Remove the landing pad successor from the invoke block and replace it
8097 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00008098 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
8099 BB->succ_end());
8100 while (!Successors.empty()) {
8101 MachineBasicBlock *SMBB = Successors.pop_back_val();
Reid Kleckner0e288232015-08-27 23:27:47 +00008102 if (SMBB->isEHPad()) {
Bill Wendling883ec972011-10-07 23:18:02 +00008103 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00008104 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00008105 }
8106 }
8107
Cong Houd97c1002015-12-01 05:29:22 +00008108 BB->addSuccessor(DispatchBB, BranchProbability::getZero());
Cong Houc1069892015-12-13 09:26:17 +00008109 BB->normalizeSuccProbs();
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008110
8111 // Find the invoke call and mark all of the callee-saved registers as
8112 // 'implicit defined' so that they're spilled. This prevents code from
8113 // moving instructions to before the EH block, where they will never be
8114 // executed.
8115 for (MachineBasicBlock::reverse_iterator
8116 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00008117 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008118
8119 DenseMap<unsigned, bool> DefRegs;
8120 for (MachineInstr::mop_iterator
8121 OI = II->operands_begin(), OE = II->operands_end();
8122 OI != OE; ++OI) {
8123 if (!OI->isReg()) continue;
8124 DefRegs[OI->getReg()] = true;
8125 }
8126
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00008127 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008128
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00008129 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00008130 unsigned Reg = SavedRegs[i];
8131 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00008132 !ARM::tGPRRegClass.contains(Reg) &&
8133 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00008134 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00008135 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00008136 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00008137 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00008138 continue;
8139 if (!DefRegs[Reg])
8140 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00008141 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00008142
8143 break;
8144 }
Bill Wendling883ec972011-10-07 23:18:02 +00008145 }
Bill Wendling324be982011-10-05 00:39:32 +00008146
Bill Wendling617075f2011-10-18 18:30:49 +00008147 // Mark all former landing pads as non-landing pads. The dispatch is the only
8148 // landing pad now.
8149 for (SmallVectorImpl<MachineBasicBlock*>::iterator
8150 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
Reid Kleckner0e288232015-08-27 23:27:47 +00008151 (*I)->setIsEHPad(false);
Bill Wendling617075f2011-10-18 18:30:49 +00008152
Bill Wendling324be982011-10-05 00:39:32 +00008153 // The instruction is gone now.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008154 MI.eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00008155}
8156
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008157static
8158MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
8159 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
8160 E = MBB->succ_end(); I != E; ++I)
8161 if (*I != Succ)
8162 return *I;
8163 llvm_unreachable("Expecting a BB with two successors!");
8164}
8165
Manman Renb504f492013-10-29 22:27:32 +00008166/// Return the load opcode for a given load size. If load size >= 8,
8167/// neon opcode will be returned.
8168static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
8169 if (LdSize >= 8)
8170 return LdSize == 16 ? ARM::VLD1q32wb_fixed
8171 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
8172 if (IsThumb1)
8173 return LdSize == 4 ? ARM::tLDRi
8174 : LdSize == 2 ? ARM::tLDRHi
8175 : LdSize == 1 ? ARM::tLDRBi : 0;
8176 if (IsThumb2)
8177 return LdSize == 4 ? ARM::t2LDR_POST
8178 : LdSize == 2 ? ARM::t2LDRH_POST
8179 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
8180 return LdSize == 4 ? ARM::LDR_POST_IMM
8181 : LdSize == 2 ? ARM::LDRH_POST
8182 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
8183}
8184
8185/// Return the store opcode for a given store size. If store size >= 8,
8186/// neon opcode will be returned.
8187static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
8188 if (StSize >= 8)
8189 return StSize == 16 ? ARM::VST1q32wb_fixed
8190 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
8191 if (IsThumb1)
8192 return StSize == 4 ? ARM::tSTRi
8193 : StSize == 2 ? ARM::tSTRHi
8194 : StSize == 1 ? ARM::tSTRBi : 0;
8195 if (IsThumb2)
8196 return StSize == 4 ? ARM::t2STR_POST
8197 : StSize == 2 ? ARM::t2STRH_POST
8198 : StSize == 1 ? ARM::t2STRB_POST : 0;
8199 return StSize == 4 ? ARM::STR_POST_IMM
8200 : StSize == 2 ? ARM::STRH_POST
8201 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
8202}
8203
8204/// Emit a post-increment load operation with given size. The instructions
8205/// will be added to BB at Pos.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008206static void emitPostLd(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00008207 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00008208 unsigned LdSize, unsigned Data, unsigned AddrIn,
8209 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
8210 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
8211 assert(LdOpc != 0 && "Should have a load opcode");
8212 if (LdSize >= 8) {
8213 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8214 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
8215 .addImm(0));
8216 } else if (IsThumb1) {
8217 // load + update AddrIn
8218 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8219 .addReg(AddrIn).addImm(0));
8220 MachineInstrBuilder MIB =
8221 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
8222 MIB = AddDefaultT1CC(MIB);
8223 MIB.addReg(AddrIn).addImm(LdSize);
8224 AddDefaultPred(MIB);
8225 } else if (IsThumb2) {
8226 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8227 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
8228 .addImm(LdSize));
8229 } else { // arm
8230 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
8231 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
8232 .addReg(0).addImm(LdSize));
8233 }
8234}
8235
8236/// Emit a post-increment store operation with given size. The instructions
8237/// will be added to BB at Pos.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008238static void emitPostSt(MachineBasicBlock *BB, MachineBasicBlock::iterator Pos,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00008239 const TargetInstrInfo *TII, const DebugLoc &dl,
Manman Renb504f492013-10-29 22:27:32 +00008240 unsigned StSize, unsigned Data, unsigned AddrIn,
8241 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
8242 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
8243 assert(StOpc != 0 && "Should have a store opcode");
8244 if (StSize >= 8) {
8245 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8246 .addReg(AddrIn).addImm(0).addReg(Data));
8247 } else if (IsThumb1) {
8248 // store + update AddrIn
8249 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
8250 .addReg(AddrIn).addImm(0));
8251 MachineInstrBuilder MIB =
8252 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
8253 MIB = AddDefaultT1CC(MIB);
8254 MIB.addReg(AddrIn).addImm(StSize);
8255 AddDefaultPred(MIB);
8256 } else if (IsThumb2) {
8257 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8258 .addReg(Data).addReg(AddrIn).addImm(StSize));
8259 } else { // arm
8260 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
8261 .addReg(Data).addReg(AddrIn).addReg(0)
8262 .addImm(StSize));
8263 }
8264}
8265
David Peixottoc32e24a2013-10-17 19:49:22 +00008266MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008267ARMTargetLowering::EmitStructByval(MachineInstr &MI,
David Peixottoc32e24a2013-10-17 19:49:22 +00008268 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00008269 // This pseudo instruction has 3 operands: dst, src, size
8270 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
8271 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00008272 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00008273 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008274 MachineFunction::iterator It = ++BB->getIterator();
Manman Rene8735522012-06-01 19:33:18 +00008275
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008276 unsigned dest = MI.getOperand(0).getReg();
8277 unsigned src = MI.getOperand(1).getReg();
8278 unsigned SizeVal = MI.getOperand(2).getImm();
8279 unsigned Align = MI.getOperand(3).getImm();
8280 DebugLoc dl = MI.getDebugLoc();
Manman Rene8735522012-06-01 19:33:18 +00008281
Manman Rene8735522012-06-01 19:33:18 +00008282 MachineFunction *MF = BB->getParent();
8283 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00008284 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00008285 const TargetRegisterClass *TRC = nullptr;
8286 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00008287
8288 bool IsThumb1 = Subtarget->isThumb1Only();
8289 bool IsThumb2 = Subtarget->isThumb2();
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008290 bool IsThumb = Subtarget->isThumb();
Manman Rene8735522012-06-01 19:33:18 +00008291
8292 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00008293 UnitSize = 1;
8294 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00008295 UnitSize = 2;
8296 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00008297 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00008298 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00008299 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00008300 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00008301 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00008302 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00008303 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00008304 }
8305 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00008306 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00008307 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00008308 }
Manman Ren6e1fd462012-06-18 22:23:48 +00008309
David Peixottob0653e532013-10-24 16:39:36 +00008310 // Select the correct opcode and register class for unit size load/store
8311 bool IsNeon = UnitSize >= 8;
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008312 TRC = IsThumb ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00008313 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00008314 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
8315 : UnitSize == 8 ? &ARM::DPRRegClass
8316 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00008317
Manman Rene8735522012-06-01 19:33:18 +00008318 unsigned BytesLeft = SizeVal % UnitSize;
8319 unsigned LoopSize = SizeVal - BytesLeft;
8320
8321 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
8322 // Use LDR and STR to copy.
8323 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
8324 // [destOut] = STR_POST(scratch, destIn, UnitSize)
8325 unsigned srcIn = src;
8326 unsigned destIn = dest;
8327 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00008328 unsigned srcOut = MRI.createVirtualRegister(TRC);
8329 unsigned destOut = MRI.createVirtualRegister(TRC);
8330 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00008331 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
8332 IsThumb1, IsThumb2);
8333 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
8334 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008335 srcIn = srcOut;
8336 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008337 }
8338
8339 // Handle the leftover bytes with LDRB and STRB.
8340 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
8341 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00008342 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00008343 unsigned srcOut = MRI.createVirtualRegister(TRC);
8344 unsigned destOut = MRI.createVirtualRegister(TRC);
8345 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00008346 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
8347 IsThumb1, IsThumb2);
8348 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
8349 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008350 srcIn = srcOut;
8351 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008352 }
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008353 MI.eraseFromParent(); // The instruction is gone now.
Manman Rene8735522012-06-01 19:33:18 +00008354 return BB;
8355 }
8356
8357 // Expand the pseudo op to a loop.
8358 // thisMBB:
8359 // ...
8360 // movw varEnd, # --> with thumb2
8361 // movt varEnd, #
8362 // ldrcp varEnd, idx --> without thumb2
8363 // fallthrough --> loopMBB
8364 // loopMBB:
8365 // PHI varPhi, varEnd, varLoop
8366 // PHI srcPhi, src, srcLoop
8367 // PHI destPhi, dst, destLoop
8368 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
8369 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
8370 // subs varLoop, varPhi, #UnitSize
8371 // bne loopMBB
8372 // fallthrough --> exitMBB
8373 // exitMBB:
8374 // epilogue to handle left-over bytes
8375 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
8376 // [destOut] = STRB_POST(scratch, destLoop, 1)
8377 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
8378 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
8379 MF->insert(It, loopMBB);
8380 MF->insert(It, exitMBB);
8381
8382 // Transfer the remainder of BB and its successor edges to exitMBB.
8383 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008384 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00008385 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
8386
8387 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00008388 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00008389 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00008390 unsigned Vtmp = varEnd;
8391 if ((LoopSize & 0xFFFF0000) != 0)
8392 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00008393 AddDefaultPred(BuildMI(BB, dl,
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008394 TII->get(IsThumb ? ARM::t2MOVi16 : ARM::MOVi16),
Derek Schuffb0513892015-03-26 22:11:00 +00008395 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00008396
8397 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00008398 AddDefaultPred(BuildMI(BB, dl,
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008399 TII->get(IsThumb ? ARM::t2MOVTi16 : ARM::MOVTi16),
Derek Schuffb0513892015-03-26 22:11:00 +00008400 varEnd)
8401 .addReg(Vtmp)
8402 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00008403 } else {
8404 MachineConstantPool *ConstantPool = MF->getConstantPool();
8405 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
8406 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
8407
8408 // MachineConstantPool wants an explicit alignment.
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008409 unsigned Align = MF->getDataLayout().getPrefTypeAlignment(Int32Ty);
David Peixottob0653e532013-10-24 16:39:36 +00008410 if (Align == 0)
Mehdi Aminia749f2a2015-07-09 02:09:52 +00008411 Align = MF->getDataLayout().getTypeAllocSize(C->getType());
David Peixottob0653e532013-10-24 16:39:36 +00008412 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
8413
Prakhar Bahugunad1233e82016-07-29 09:16:46 +00008414 if (IsThumb)
David Peixottob0653e532013-10-24 16:39:36 +00008415 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
8416 varEnd, RegState::Define).addConstantPoolIndex(Idx));
8417 else
8418 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
8419 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
8420 }
Manman Rene8735522012-06-01 19:33:18 +00008421 BB->addSuccessor(loopMBB);
8422
8423 // Generate the loop body:
8424 // varPhi = PHI(varLoop, varEnd)
8425 // srcPhi = PHI(srcLoop, src)
8426 // destPhi = PHI(destLoop, dst)
8427 MachineBasicBlock *entryBB = BB;
8428 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00008429 unsigned varLoop = MRI.createVirtualRegister(TRC);
8430 unsigned varPhi = MRI.createVirtualRegister(TRC);
8431 unsigned srcLoop = MRI.createVirtualRegister(TRC);
8432 unsigned srcPhi = MRI.createVirtualRegister(TRC);
8433 unsigned destLoop = MRI.createVirtualRegister(TRC);
8434 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00008435
8436 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
8437 .addReg(varLoop).addMBB(loopMBB)
8438 .addReg(varEnd).addMBB(entryBB);
8439 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
8440 .addReg(srcLoop).addMBB(loopMBB)
8441 .addReg(src).addMBB(entryBB);
8442 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
8443 .addReg(destLoop).addMBB(loopMBB)
8444 .addReg(dest).addMBB(entryBB);
8445
8446 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
8447 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00008448 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00008449 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
8450 IsThumb1, IsThumb2);
8451 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
8452 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00008453
8454 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00008455 if (IsThumb1) {
8456 MachineInstrBuilder MIB =
8457 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
8458 MIB = AddDefaultT1CC(MIB);
8459 MIB.addReg(varPhi).addImm(UnitSize);
8460 AddDefaultPred(MIB);
8461 } else {
8462 MachineInstrBuilder MIB =
8463 BuildMI(*BB, BB->end(), dl,
8464 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
8465 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
8466 MIB->getOperand(5).setReg(ARM::CPSR);
8467 MIB->getOperand(5).setIsDef(true);
8468 }
8469 BuildMI(*BB, BB->end(), dl,
8470 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
8471 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00008472
8473 // loopMBB can loop back to loopMBB or fall through to exitMBB.
8474 BB->addSuccessor(loopMBB);
8475 BB->addSuccessor(exitMBB);
8476
8477 // Add epilogue to handle BytesLeft.
8478 BB = exitMBB;
Duncan P. N. Exon Smith29c52492016-07-08 20:21:17 +00008479 auto StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00008480
8481 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
8482 // [destOut] = STRB_POST(scratch, destLoop, 1)
8483 unsigned srcIn = srcLoop;
8484 unsigned destIn = destLoop;
8485 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00008486 unsigned srcOut = MRI.createVirtualRegister(TRC);
8487 unsigned destOut = MRI.createVirtualRegister(TRC);
8488 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00008489 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
8490 IsThumb1, IsThumb2);
8491 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
8492 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00008493 srcIn = srcOut;
8494 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00008495 }
8496
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008497 MI.eraseFromParent(); // The instruction is gone now.
Manman Rene8735522012-06-01 19:33:18 +00008498 return BB;
8499}
8500
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00008501MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008502ARMTargetLowering::EmitLowered__chkstk(MachineInstr &MI,
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008503 MachineBasicBlock *MBB) const {
8504 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00008505 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008506 DebugLoc DL = MI.getDebugLoc();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008507
8508 assert(Subtarget->isTargetWindows() &&
8509 "__chkstk is only supported on Windows");
8510 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
8511
8512 // __chkstk takes the number of words to allocate on the stack in R4, and
8513 // returns the stack adjustment in number of bytes in R4. This will not
8514 // clober any other registers (other than the obvious lr).
8515 //
8516 // Although, technically, IP should be considered a register which may be
8517 // clobbered, the call itself will not touch it. Windows on ARM is a pure
8518 // thumb-2 environment, so there is no interworking required. As a result, we
8519 // do not expect a veneer to be emitted by the linker, clobbering IP.
8520 //
Alp Toker1d099d92014-06-19 19:41:26 +00008521 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008522 // required, again, ensuring that IP is not clobbered.
8523 //
8524 // Finally, although some linkers may theoretically provide a trampoline for
8525 // out of range calls (which is quite common due to a 32M range limitation of
8526 // branches for Thumb), we can generate the long-call version via
8527 // -mcmodel=large, alleviating the need for the trampoline which may clobber
8528 // IP.
8529
8530 switch (TM.getCodeModel()) {
8531 case CodeModel::Small:
8532 case CodeModel::Medium:
8533 case CodeModel::Default:
8534 case CodeModel::Kernel:
8535 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
8536 .addImm((unsigned)ARMCC::AL).addReg(0)
8537 .addExternalSymbol("__chkstk")
8538 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8539 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8540 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8541 break;
8542 case CodeModel::Large:
8543 case CodeModel::JITDefault: {
8544 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
8545 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
8546
8547 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
8548 .addExternalSymbol("__chkstk");
8549 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
8550 .addImm((unsigned)ARMCC::AL).addReg(0)
8551 .addReg(Reg, RegState::Kill)
8552 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
8553 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
8554 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
8555 break;
8556 }
8557 }
8558
8559 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
8560 ARM::SP)
Saleem Abdulrasool96115182016-04-24 20:12:48 +00008561 .addReg(ARM::SP, RegState::Kill)
8562 .addReg(ARM::R4, RegState::Kill)
8563 .setMIFlags(MachineInstr::FrameSetup)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008564
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008565 MI.eraseFromParent();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008566 return MBB;
8567}
8568
8569MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008570ARMTargetLowering::EmitLowered__dbzchk(MachineInstr &MI,
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008571 MachineBasicBlock *MBB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008572 DebugLoc DL = MI.getDebugLoc();
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008573 MachineFunction *MF = MBB->getParent();
8574 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
8575
8576 MachineBasicBlock *ContBB = MF->CreateMachineBasicBlock();
Saleem Abdulrasool750a90d2016-03-25 19:48:06 +00008577 MF->insert(++MBB->getIterator(), ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008578 ContBB->splice(ContBB->begin(), MBB,
8579 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008580 ContBB->transferSuccessorsAndUpdatePHIs(MBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008581
8582 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
8583 MF->push_back(TrapBB);
8584 BuildMI(TrapBB, DL, TII->get(ARM::t2UDF)).addImm(249);
8585 MBB->addSuccessor(TrapBB);
8586
8587 BuildMI(*MBB, MI, DL, TII->get(ARM::tCBZ))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008588 .addReg(MI.getOperand(0).getReg())
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008589 .addMBB(TrapBB);
Saleem Abdulrasool0dab98d2016-03-25 00:34:11 +00008590 AddDefaultPred(BuildMI(*MBB, MI, DL, TII->get(ARM::t2B)).addMBB(ContBB));
Saleem Abdulrasool071a0992016-03-17 14:10:49 +00008591 MBB->addSuccessor(ContBB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008592
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008593 MI.eraseFromParent();
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008594 return ContBB;
8595}
8596
8597MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008598ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI,
Dan Gohman25c16532010-05-01 00:01:06 +00008599 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008600 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008601 DebugLoc dl = MI.getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00008602 bool isThumb2 = Subtarget->isThumb2();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008603 switch (MI.getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00008604 default: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008605 MI.dump();
Evan Chengb972e562009-08-07 00:34:42 +00008606 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00008607 }
James Molloyb3326df2016-07-15 08:03:56 +00008608
8609 // Thumb1 post-indexed loads are really just single-register LDMs.
8610 case ARM::tLDR_postidx: {
8611 BuildMI(*BB, MI, dl, TII->get(ARM::tLDMIA_UPD))
James Molloy8f16dff2016-07-15 08:12:44 +00008612 .addOperand(MI.getOperand(1)) // Rn_wb
8613 .addOperand(MI.getOperand(2)) // Rn
8614 .addOperand(MI.getOperand(3)) // PredImm
8615 .addOperand(MI.getOperand(4)) // PredReg
8616 .addOperand(MI.getOperand(0)); // Rt
8617 MI.eraseFromParent();
James Molloyb3326df2016-07-15 08:03:56 +00008618 return BB;
8619 }
8620
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008621 // The Thumb2 pre-indexed stores have the same MI operands, they just
8622 // define them differently in the .td files from the isel patterns, so
8623 // they need pseudos.
8624 case ARM::t2STR_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008625 MI.setDesc(TII->get(ARM::t2STR_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008626 return BB;
8627 case ARM::t2STRB_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008628 MI.setDesc(TII->get(ARM::t2STRB_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008629 return BB;
8630 case ARM::t2STRH_preidx:
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008631 MI.setDesc(TII->get(ARM::t2STRH_PRE));
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00008632 return BB;
8633
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008634 case ARM::STRi_preidx:
8635 case ARM::STRBi_preidx: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008636 unsigned NewOpc = MI.getOpcode() == ARM::STRi_preidx ? ARM::STR_PRE_IMM
8637 : ARM::STRB_PRE_IMM;
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008638 // Decode the offset.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008639 unsigned Offset = MI.getOperand(4).getImm();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008640 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
8641 Offset = ARM_AM::getAM2Offset(Offset);
8642 if (isSub)
8643 Offset = -Offset;
8644
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008645 MachineMemOperand *MMO = *MI.memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00008646 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008647 .addOperand(MI.getOperand(0)) // Rn_wb
8648 .addOperand(MI.getOperand(1)) // Rt
8649 .addOperand(MI.getOperand(2)) // Rn
8650 .addImm(Offset) // offset (skip GPR==zero_reg)
8651 .addOperand(MI.getOperand(5)) // pred
8652 .addOperand(MI.getOperand(6))
8653 .addMemOperand(MMO);
8654 MI.eraseFromParent();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008655 return BB;
8656 }
8657 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008658 case ARM::STRBr_preidx:
8659 case ARM::STRH_preidx: {
8660 unsigned NewOpc;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008661 switch (MI.getOpcode()) {
Jim Grosbachd886f8c2011-08-11 21:17:22 +00008662 default: llvm_unreachable("unexpected opcode!");
8663 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
8664 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
8665 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
8666 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008667 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008668 for (unsigned i = 0; i < MI.getNumOperands(); ++i)
8669 MIB.addOperand(MI.getOperand(i));
8670 MI.eraseFromParent();
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00008671 return BB;
8672 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00008673
Evan Chengbb2af352009-08-12 05:17:19 +00008674 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00008675 // To "insert" a SELECT_CC instruction, we actually have to insert the
8676 // diamond control-flow pattern. The incoming instruction knows the
8677 // destination vreg to set, the condition code register to branch on, the
8678 // true/false values to select between, and a branch opcode to use.
8679 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008680 MachineFunction::iterator It = ++BB->getIterator();
Evan Cheng10043e22007-01-19 07:51:42 +00008681
8682 // thisMBB:
8683 // ...
8684 // TrueVal = ...
8685 // cmpTY ccX, r1, r2
8686 // bCC copy1MBB
8687 // fallthrough --> copy0MBB
8688 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00008689 MachineFunction *F = BB->getParent();
8690 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8691 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00008692 F->insert(It, copy0MBB);
8693 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00008694
8695 // Transfer the remainder of BB and its successor edges to sinkMBB.
8696 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008697 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00008698 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
8699
Dan Gohmanf4f04102010-07-06 15:49:48 +00008700 BB->addSuccessor(copy0MBB);
8701 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00008702
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008703 BuildMI(BB, dl, TII->get(ARM::tBcc))
8704 .addMBB(sinkMBB)
8705 .addImm(MI.getOperand(3).getImm())
8706 .addReg(MI.getOperand(4).getReg());
Dan Gohman34396292010-07-06 20:24:04 +00008707
Evan Cheng10043e22007-01-19 07:51:42 +00008708 // copy0MBB:
8709 // %FalseValue = ...
8710 // # fallthrough to sinkMBB
8711 BB = copy0MBB;
8712
8713 // Update machine-CFG edges
8714 BB->addSuccessor(sinkMBB);
8715
8716 // sinkMBB:
8717 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8718 // ...
8719 BB = sinkMBB;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008720 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), MI.getOperand(0).getReg())
8721 .addReg(MI.getOperand(1).getReg())
8722 .addMBB(copy0MBB)
8723 .addReg(MI.getOperand(2).getReg())
8724 .addMBB(thisMBB);
Evan Cheng10043e22007-01-19 07:51:42 +00008725
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008726 MI.eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00008727 return BB;
8728 }
Evan Chengb972e562009-08-07 00:34:42 +00008729
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008730 case ARM::BCCi64:
8731 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00008732 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008733 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00008734
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008735 // Compare both parts that make up the double comparison separately for
8736 // equality.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008737 bool RHSisZero = MI.getOpcode() == ARM::BCCZi64;
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008738
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008739 unsigned LHS1 = MI.getOperand(1).getReg();
8740 unsigned LHS2 = MI.getOperand(2).getReg();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008741 if (RHSisZero) {
8742 AddDefaultPred(BuildMI(BB, dl,
8743 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8744 .addReg(LHS1).addImm(0));
8745 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8746 .addReg(LHS2).addImm(0)
8747 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8748 } else {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008749 unsigned RHS1 = MI.getOperand(3).getReg();
8750 unsigned RHS2 = MI.getOperand(4).getReg();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008751 AddDefaultPred(BuildMI(BB, dl,
8752 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8753 .addReg(LHS1).addReg(RHS1));
8754 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
8755 .addReg(LHS2).addReg(RHS2)
8756 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
8757 }
8758
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008759 MachineBasicBlock *destMBB = MI.getOperand(RHSisZero ? 3 : 5).getMBB();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008760 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008761 if (MI.getOperand(0).getImm() == ARMCC::NE)
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008762 std::swap(destMBB, exitMBB);
8763
8764 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
8765 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00008766 if (isThumb2)
8767 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
8768 else
8769 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008770
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008771 MI.eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00008772 return BB;
8773 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008774
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008775 case ARM::Int_eh_sjlj_setjmp:
8776 case ARM::Int_eh_sjlj_setjmp_nofp:
8777 case ARM::tInt_eh_sjlj_setjmp:
8778 case ARM::t2Int_eh_sjlj_setjmp:
8779 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Matthias Braun3cd00c12015-07-16 22:34:16 +00008780 return BB;
8781
8782 case ARM::Int_eh_sjlj_setup_dispatch:
Bill Wendlingf7f223f2011-10-17 20:37:20 +00008783 EmitSjLjDispatchBlock(MI, BB);
8784 return BB;
8785
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008786 case ARM::ABS:
8787 case ARM::t2ABS: {
8788 // To insert an ABS instruction, we have to insert the
8789 // diamond control-flow pattern. The incoming instruction knows the
8790 // source vreg to test against 0, the destination vreg to set,
8791 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00008792 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008793 // It transforms
8794 // V1 = ABS V0
8795 // into
8796 // V2 = MOVS V0
8797 // BCC (branch to SinkBB if V0 >= 0)
8798 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00008799 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008800 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Duncan P. N. Exon Smith9f9559e2015-10-19 23:25:57 +00008801 MachineFunction::iterator BBI = ++BB->getIterator();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008802 MachineFunction *Fn = BB->getParent();
8803 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8804 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
8805 Fn->insert(BBI, RSBBB);
8806 Fn->insert(BBI, SinkBB);
8807
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008808 unsigned int ABSSrcReg = MI.getOperand(1).getReg();
8809 unsigned int ABSDstReg = MI.getOperand(0).getReg();
8810 bool ABSSrcKIll = MI.getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008811 bool isThumb2 = Subtarget->isThumb2();
8812 MachineRegisterInfo &MRI = Fn->getRegInfo();
8813 // In Thumb mode S must not be specified if source register is the SP or
8814 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00008815 unsigned NewRsbDstReg =
8816 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008817
8818 // Transfer the remainder of BB and its successor edges to sinkMBB.
8819 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00008820 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008821 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
8822
8823 BB->addSuccessor(RSBBB);
8824 BB->addSuccessor(SinkBB);
8825
8826 // fall through to SinkMBB
8827 RSBBB->addSuccessor(SinkBB);
8828
Manman Rene0763c72012-06-15 21:32:12 +00008829 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00008830 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00008831 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
8832 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008833
8834 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00008835 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008836 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
8837 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
8838
8839 // insert rsbri in RSBBB
8840 // Note: BCC and rsbri will be converted into predicated rsbmi
8841 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00008842 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008843 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00008844 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008845 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
8846
Andrew Trick3f07c422011-10-18 18:40:53 +00008847 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008848 // reuse ABSDstReg to not change uses of ABS instruction
8849 BuildMI(*SinkBB, SinkBB->begin(), dl,
8850 TII->get(ARM::PHI), ABSDstReg)
8851 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00008852 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008853
8854 // remove ABS instruction
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008855 MI.eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00008856
8857 // return last added BB
8858 return SinkBB;
8859 }
Manman Rene8735522012-06-01 19:33:18 +00008860 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00008861 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00008862 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00008863 case ARM::WIN__CHKSTK:
8864 return EmitLowered__chkstk(MI, BB);
Saleem Abdulrasoolfe83b502015-09-25 05:15:46 +00008865 case ARM::WIN__DBZCHK:
8866 return EmitLowered__dbzchk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00008867 }
8868}
8869
Scott Douglass953f9082015-10-05 14:49:54 +00008870/// \brief Attaches vregs to MEMCPY that it will use as scratch registers
8871/// when it is expanded into LDM/STM. This is done as a post-isel lowering
8872/// instead of as a custom inserter because we need the use list from the SDNode.
8873static void attachMEMCPYScratchRegs(const ARMSubtarget *Subtarget,
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008874 MachineInstr &MI, const SDNode *Node) {
Scott Douglass953f9082015-10-05 14:49:54 +00008875 bool isThumb1 = Subtarget->isThumb1Only();
8876
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008877 DebugLoc DL = MI.getDebugLoc();
8878 MachineFunction *MF = MI.getParent()->getParent();
Scott Douglass953f9082015-10-05 14:49:54 +00008879 MachineRegisterInfo &MRI = MF->getRegInfo();
8880 MachineInstrBuilder MIB(*MF, MI);
8881
8882 // If the new dst/src is unused mark it as dead.
8883 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008884 MI.getOperand(0).setIsDead(true);
Scott Douglass953f9082015-10-05 14:49:54 +00008885 }
8886 if (!Node->hasAnyUseOfValue(1)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008887 MI.getOperand(1).setIsDead(true);
Scott Douglass953f9082015-10-05 14:49:54 +00008888 }
8889
8890 // The MEMCPY both defines and kills the scratch registers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008891 for (unsigned I = 0; I != MI.getOperand(4).getImm(); ++I) {
Scott Douglass953f9082015-10-05 14:49:54 +00008892 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
8893 : &ARM::GPRRegClass);
8894 MIB.addReg(TmpReg, RegState::Define|RegState::Dead);
8895 }
8896}
8897
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008898void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Evan Chenge6fba772011-08-30 19:09:48 +00008899 SDNode *Node) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008900 if (MI.getOpcode() == ARM::MEMCPY) {
Scott Douglass953f9082015-10-05 14:49:54 +00008901 attachMEMCPYScratchRegs(Subtarget, MI, Node);
8902 return;
8903 }
8904
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008905 const MCInstrDesc *MCID = &MI.getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00008906 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
8907 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
8908 // operand is still set to noreg. If needed, set the optional operand's
8909 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00008910 //
Andrew Trick88b24502011-10-18 19:18:52 +00008911 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00008912
Andrew Trick924123a2011-09-21 02:20:46 +00008913 // Rename pseudo opcodes.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008914 unsigned NewOpc = convertAddSubFlagsOpcode(MI.getOpcode());
Andrew Trick924123a2011-09-21 02:20:46 +00008915 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00008916 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00008917 MCID = &TII->get(NewOpc);
8918
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008919 assert(MCID->getNumOperands() == MI.getDesc().getNumOperands() + 1 &&
Andrew Trick88b24502011-10-18 19:18:52 +00008920 "converted opcode should be the same except for cc_out");
8921
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008922 MI.setDesc(*MCID);
Andrew Trick88b24502011-10-18 19:18:52 +00008923
8924 // Add the optional cc_out operand
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008925 MI.addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00008926 }
Andrew Trick88b24502011-10-18 19:18:52 +00008927 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00008928
8929 // Any ARM instruction that sets the 's' bit should specify an optional
8930 // "cc_out" operand in the last operand position.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008931 if (!MI.hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00008932 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008933 return;
8934 }
Andrew Trick924123a2011-09-21 02:20:46 +00008935 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
8936 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00008937 bool definesCPSR = false;
8938 bool deadCPSR = false;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008939 for (unsigned i = MCID->getNumOperands(), e = MI.getNumOperands(); i != e;
8940 ++i) {
8941 const MachineOperand &MO = MI.getOperand(i);
Andrew Trick8586e622011-09-20 03:17:40 +00008942 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
8943 definesCPSR = true;
8944 if (MO.isDead())
8945 deadCPSR = true;
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008946 MI.RemoveOperand(i);
Andrew Trick8586e622011-09-20 03:17:40 +00008947 break;
Evan Chenge6fba772011-08-30 19:09:48 +00008948 }
8949 }
Andrew Trick8586e622011-09-20 03:17:40 +00008950 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00008951 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00008952 return;
8953 }
8954 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00008955 if (deadCPSR) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008956 assert(!MI.getOperand(ccOutIdx).getReg() &&
Andrew Trick924123a2011-09-21 02:20:46 +00008957 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00008958 return;
Andrew Trick924123a2011-09-21 02:20:46 +00008959 }
Andrew Trick8586e622011-09-20 03:17:40 +00008960
Andrew Trick924123a2011-09-21 02:20:46 +00008961 // If this instruction was defined with an optional CPSR def and its dag node
8962 // had a live implicit CPSR def, then activate the optional CPSR def.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00008963 MachineOperand &MO = MI.getOperand(ccOutIdx);
Andrew Trick8586e622011-09-20 03:17:40 +00008964 MO.setReg(ARM::CPSR);
8965 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00008966}
8967
Evan Cheng10043e22007-01-19 07:51:42 +00008968//===----------------------------------------------------------------------===//
8969// ARM Optimization Hooks
8970//===----------------------------------------------------------------------===//
8971
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008972// Helper function that checks if N is a null or all ones constant.
8973static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
Artyom Skrobov314ee042015-11-25 19:41:11 +00008974 return AllOnes ? isAllOnesConstant(N) : isNullConstant(N);
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00008975}
8976
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008977// Return true if N is conditionally 0 or all ones.
8978// Detects these expressions where cc is an i1 value:
8979//
8980// (select cc 0, y) [AllOnes=0]
8981// (select cc y, 0) [AllOnes=0]
8982// (zext cc) [AllOnes=0]
8983// (sext cc) [AllOnes=0/1]
8984// (select cc -1, y) [AllOnes=1]
8985// (select cc y, -1) [AllOnes=1]
8986//
8987// Invert is set when N is the null/all ones constant when CC is false.
8988// OtherOp is set to the alternative value of N.
8989static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
8990 SDValue &CC, bool &Invert,
8991 SDValue &OtherOp,
8992 SelectionDAG &DAG) {
8993 switch (N->getOpcode()) {
8994 default: return false;
8995 case ISD::SELECT: {
8996 CC = N->getOperand(0);
8997 SDValue N1 = N->getOperand(1);
8998 SDValue N2 = N->getOperand(2);
8999 if (isZeroOrAllOnes(N1, AllOnes)) {
9000 Invert = false;
9001 OtherOp = N2;
9002 return true;
9003 }
9004 if (isZeroOrAllOnes(N2, AllOnes)) {
9005 Invert = true;
9006 OtherOp = N1;
9007 return true;
9008 }
9009 return false;
9010 }
9011 case ISD::ZERO_EXTEND:
9012 // (zext cc) can never be the all ones value.
9013 if (AllOnes)
9014 return false;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00009015 LLVM_FALLTHROUGH;
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009016 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009017 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009018 EVT VT = N->getValueType(0);
9019 CC = N->getOperand(0);
9020 if (CC.getValueType() != MVT::i1)
9021 return false;
9022 Invert = !AllOnes;
9023 if (AllOnes)
9024 // When looking for an AllOnes constant, N is an sext, and the 'other'
9025 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009026 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009027 else if (N->getOpcode() == ISD::ZERO_EXTEND)
9028 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009029 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009030 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009031 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
9032 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009033 return true;
9034 }
9035 }
9036}
9037
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009038// Combine a constant select operand into its use:
9039//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009040// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
9041// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
9042// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
9043// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
9044// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009045//
9046// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009047// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009048//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009049// Also recognize sext/zext from i1:
9050//
9051// (add (zext cc), x) -> (select cc (add x, 1), x)
9052// (add (sext cc), x) -> (select cc (add x, -1), x)
9053//
9054// These transformations eventually create predicated instructions.
9055//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009056// @param N The node to transform.
9057// @param Slct The N operand that is a select.
9058// @param OtherOp The other N operand (x above).
9059// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009060// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009061// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00009062static
9063SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009064 TargetLowering::DAGCombinerInfo &DCI,
9065 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00009066 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009067 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009068 SDValue NonConstantVal;
9069 SDValue CCOp;
9070 bool SwapSelectOps;
9071 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
9072 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00009073 return SDValue();
9074
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009075 // Slct is now know to be the desired identity constant when CC is true.
9076 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009077 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009078 OtherOp, NonConstantVal);
9079 // Unless SwapSelectOps says CC should be false.
9080 if (SwapSelectOps)
9081 std::swap(TrueVal, FalseVal);
9082
Andrew Trickef9de2a2013-05-25 02:42:55 +00009083 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00009084 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00009085}
9086
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009087// Attempt combineSelectAndUse on each operand of a commutative operator N.
9088static
9089SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
9090 TargetLowering::DAGCombinerInfo &DCI) {
9091 SDValue N0 = N->getOperand(0);
9092 SDValue N1 = N->getOperand(1);
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009093 if (N0.getNode()->hasOneUse())
9094 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009095 return Result;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009096 if (N1.getNode()->hasOneUse())
9097 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009098 return Result;
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009099 return SDValue();
9100}
9101
Eric Christopher1b8b94192011-06-29 21:10:36 +00009102// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00009103// (only after legalization).
9104static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
9105 TargetLowering::DAGCombinerInfo &DCI,
9106 const ARMSubtarget *Subtarget) {
9107
9108 // Only perform optimization if after legalize, and if NEON is available. We
9109 // also expected both operands to be BUILD_VECTORs.
9110 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
9111 || N0.getOpcode() != ISD::BUILD_VECTOR
9112 || N1.getOpcode() != ISD::BUILD_VECTOR)
9113 return SDValue();
9114
9115 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
9116 EVT VT = N->getValueType(0);
9117 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
9118 return SDValue();
9119
9120 // Check that the vector operands are of the right form.
9121 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
9122 // operands, where N is the size of the formed vector.
9123 // Each EXTRACT_VECTOR should have the same input vector and odd or even
9124 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00009125
9126 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00009127 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00009128 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00009129 SDValue Vec = N0->getOperand(0)->getOperand(0);
9130 SDNode *V = Vec.getNode();
9131 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00009132
Eric Christopher1b8b94192011-06-29 21:10:36 +00009133 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009134 // check to see if each of their operands are an EXTRACT_VECTOR with
9135 // the same vector and appropriate index.
9136 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
9137 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
9138 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00009139
Tanya Lattnere9e67052011-06-14 23:48:48 +00009140 SDValue ExtVec0 = N0->getOperand(i);
9141 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00009142
Tanya Lattnere9e67052011-06-14 23:48:48 +00009143 // First operand is the vector, verify its the same.
9144 if (V != ExtVec0->getOperand(0).getNode() ||
9145 V != ExtVec1->getOperand(0).getNode())
9146 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00009147
Tanya Lattnere9e67052011-06-14 23:48:48 +00009148 // Second is the constant, verify its correct.
9149 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
9150 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00009151
Tanya Lattnere9e67052011-06-14 23:48:48 +00009152 // For the constant, we want to see all the even or all the odd.
9153 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
9154 || C1->getZExtValue() != nextIndex+1)
9155 return SDValue();
9156
9157 // Increment index.
9158 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00009159 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00009160 return SDValue();
9161 }
9162
9163 // Create VPADDL node.
9164 SelectionDAG &DAG = DCI.DAG;
9165 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00009166
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009167 SDLoc dl(N);
9168
Tanya Lattnere9e67052011-06-14 23:48:48 +00009169 // Build operand list.
9170 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009171 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Mehdi Amini44ede332015-07-09 02:09:04 +00009172 TLI.getPointerTy(DAG.getDataLayout())));
Tanya Lattnere9e67052011-06-14 23:48:48 +00009173
9174 // Input is the vector.
9175 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00009176
Tanya Lattnere9e67052011-06-14 23:48:48 +00009177 // Get widened type and narrowed type.
9178 MVT widenType;
9179 unsigned numElem = VT.getVectorNumElements();
Junmo Park1108ab02016-02-19 01:46:04 +00009180
Silviu Barangaa3106e62014-04-03 10:44:27 +00009181 EVT inputLaneType = Vec.getValueType().getVectorElementType();
9182 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00009183 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
9184 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
9185 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
9186 default:
Craig Toppere55c5562012-02-07 02:50:20 +00009187 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00009188 }
9189
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009190 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00009191 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009192 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009193}
9194
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009195static SDValue findMUL_LOHI(SDValue V) {
9196 if (V->getOpcode() == ISD::UMUL_LOHI ||
9197 V->getOpcode() == ISD::SMUL_LOHI)
9198 return V;
9199 return SDValue();
9200}
9201
9202static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
9203 TargetLowering::DAGCombinerInfo &DCI,
9204 const ARMSubtarget *Subtarget) {
9205
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009206 // Look for multiply add opportunities.
9207 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
9208 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
9209 // a glue link from the first add to the second add.
9210 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
9211 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00009212 // UMUL_LOHI
9213 // / :lo \ :hi
9214 // / \ [no multiline comment]
9215 // loAdd -> ADDE |
9216 // \ :glue /
9217 // \ /
9218 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009219 //
9220 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
9221 SDValue AddcOp0 = AddcNode->getOperand(0);
9222 SDValue AddcOp1 = AddcNode->getOperand(1);
9223
9224 // Check if the two operands are from the same mul_lohi node.
9225 if (AddcOp0.getNode() == AddcOp1.getNode())
9226 return SDValue();
9227
9228 assert(AddcNode->getNumValues() == 2 &&
9229 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00009230 "Expect ADDC with two result values. First: i32");
9231
9232 // Check that we have a glued ADDC node.
9233 if (AddcNode->getValueType(1) != MVT::Glue)
9234 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009235
9236 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
9237 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
9238 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
9239 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
9240 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
9241 return SDValue();
9242
9243 // Look for the glued ADDE.
9244 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00009245 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009246 return SDValue();
9247
9248 // Make sure it is really an ADDE.
9249 if (AddeNode->getOpcode() != ISD::ADDE)
9250 return SDValue();
9251
9252 assert(AddeNode->getNumOperands() == 3 &&
9253 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
9254 "ADDE node has the wrong inputs");
9255
9256 // Check for the triangle shape.
9257 SDValue AddeOp0 = AddeNode->getOperand(0);
9258 SDValue AddeOp1 = AddeNode->getOperand(1);
9259
9260 // Make sure that the ADDE operands are not coming from the same node.
9261 if (AddeOp0.getNode() == AddeOp1.getNode())
9262 return SDValue();
9263
9264 // Find the MUL_LOHI node walking up ADDE's operands.
9265 bool IsLeftOperandMUL = false;
9266 SDValue MULOp = findMUL_LOHI(AddeOp0);
9267 if (MULOp == SDValue())
9268 MULOp = findMUL_LOHI(AddeOp1);
9269 else
9270 IsLeftOperandMUL = true;
9271 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00009272 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009273
9274 // Figure out the right opcode.
9275 unsigned Opc = MULOp->getOpcode();
9276 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
9277
9278 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00009279 SDValue* HiAdd = nullptr;
9280 SDValue* LoMul = nullptr;
9281 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009282
Jyoti Allurf1d70502015-01-23 09:10:03 +00009283 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
9284 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
9285 return SDValue();
9286
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009287 if (IsLeftOperandMUL)
9288 HiAdd = &AddeOp1;
9289 else
9290 HiAdd = &AddeOp0;
9291
9292
Jyoti Allurf1d70502015-01-23 09:10:03 +00009293 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
9294 // whose low result is fed to the ADDC we are checking.
9295
9296 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009297 LoMul = &AddcOp0;
9298 LowAdd = &AddcOp1;
9299 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00009300 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009301 LoMul = &AddcOp1;
9302 LowAdd = &AddcOp0;
9303 }
9304
Craig Topper062a2ba2014-04-25 05:30:21 +00009305 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009306 return SDValue();
9307
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009308 // Create the merged node.
9309 SelectionDAG &DAG = DCI.DAG;
9310
9311 // Build operand list.
9312 SmallVector<SDValue, 8> Ops;
9313 Ops.push_back(LoMul->getOperand(0));
9314 Ops.push_back(LoMul->getOperand(1));
9315 Ops.push_back(*LowAdd);
9316 Ops.push_back(*HiAdd);
9317
Andrew Trickef9de2a2013-05-25 02:42:55 +00009318 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00009319 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009320
9321 // Replace the ADDs' nodes uses by the MLA node's values.
9322 SDValue HiMLALResult(MLALNode.getNode(), 1);
9323 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
9324
9325 SDValue LoMLALResult(MLALNode.getNode(), 0);
9326 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
9327
9328 // Return original node to notify the driver to stop replacing.
9329 SDValue resNode(AddcNode, 0);
9330 return resNode;
9331}
9332
Sam Parkerd616cf02016-06-20 16:47:09 +00009333static SDValue AddCombineTo64bitUMAAL(SDNode *AddcNode,
9334 TargetLowering::DAGCombinerInfo &DCI,
9335 const ARMSubtarget *Subtarget) {
9336 // UMAAL is similar to UMLAL except that it adds two unsigned values.
9337 // While trying to combine for the other MLAL nodes, first search for the
9338 // chance to use UMAAL. Check if Addc uses another addc node which can first
9339 // be combined into a UMLAL. The other pattern is AddcNode being combined
9340 // into an UMLAL and then using another addc is handled in ISelDAGToDAG.
9341
Sam Parkerd5ca0a62016-07-25 10:11:00 +00009342 if (!Subtarget->hasV6Ops() ||
9343 (Subtarget->isThumb() && !Subtarget->hasThumb2()))
Sam Parkerd616cf02016-06-20 16:47:09 +00009344 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
9345
9346 SDNode *PrevAddc = nullptr;
9347 if (AddcNode->getOperand(0).getOpcode() == ISD::ADDC)
9348 PrevAddc = AddcNode->getOperand(0).getNode();
9349 else if (AddcNode->getOperand(1).getOpcode() == ISD::ADDC)
9350 PrevAddc = AddcNode->getOperand(1).getNode();
9351
9352 // If there's no addc chains, just return a search for any MLAL.
9353 if (PrevAddc == nullptr)
9354 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
9355
9356 // Try to convert the addc operand to an MLAL and if that fails try to
9357 // combine AddcNode.
9358 SDValue MLAL = AddCombineTo64bitMLAL(PrevAddc, DCI, Subtarget);
9359 if (MLAL != SDValue(PrevAddc, 0))
9360 return AddCombineTo64bitMLAL(AddcNode, DCI, Subtarget);
9361
9362 // Find the converted UMAAL or quit if it doesn't exist.
9363 SDNode *UmlalNode = nullptr;
9364 SDValue AddHi;
9365 if (AddcNode->getOperand(0).getOpcode() == ARMISD::UMLAL) {
9366 UmlalNode = AddcNode->getOperand(0).getNode();
9367 AddHi = AddcNode->getOperand(1);
9368 } else if (AddcNode->getOperand(1).getOpcode() == ARMISD::UMLAL) {
9369 UmlalNode = AddcNode->getOperand(1).getNode();
9370 AddHi = AddcNode->getOperand(0);
9371 } else {
9372 return SDValue();
9373 }
9374
9375 // The ADDC should be glued to an ADDE node, which uses the same UMLAL as
9376 // the ADDC as well as Zero.
9377 auto *Zero = dyn_cast<ConstantSDNode>(UmlalNode->getOperand(3));
9378
9379 if (!Zero || Zero->getZExtValue() != 0)
9380 return SDValue();
9381
9382 // Check that we have a glued ADDC node.
9383 if (AddcNode->getValueType(1) != MVT::Glue)
9384 return SDValue();
9385
9386 // Look for the glued ADDE.
9387 SDNode* AddeNode = AddcNode->getGluedUser();
9388 if (!AddeNode)
9389 return SDValue();
9390
9391 if ((AddeNode->getOperand(0).getNode() == Zero &&
9392 AddeNode->getOperand(1).getNode() == UmlalNode) ||
9393 (AddeNode->getOperand(0).getNode() == UmlalNode &&
9394 AddeNode->getOperand(1).getNode() == Zero)) {
9395
9396 SelectionDAG &DAG = DCI.DAG;
9397 SDValue Ops[] = { UmlalNode->getOperand(0), UmlalNode->getOperand(1),
9398 UmlalNode->getOperand(2), AddHi };
9399 SDValue UMAAL = DAG.getNode(ARMISD::UMAAL, SDLoc(AddcNode),
9400 DAG.getVTList(MVT::i32, MVT::i32), Ops);
9401
9402 // Replace the ADDs' nodes uses by the UMAAL node's values.
9403 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), SDValue(UMAAL.getNode(), 1));
9404 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), SDValue(UMAAL.getNode(), 0));
9405
9406 // Return original node to notify the driver to stop replacing.
9407 return SDValue(AddcNode, 0);
9408 }
9409 return SDValue();
9410}
9411
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009412/// PerformADDCCombine - Target-specific dag combine transform from
Sam Parkerd616cf02016-06-20 16:47:09 +00009413/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL or
9414/// ISD::ADDC, ISD::ADDE and ARMISD::UMLAL to ARMISD::UMAAL
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009415static SDValue PerformADDCCombine(SDNode *N,
9416 TargetLowering::DAGCombinerInfo &DCI,
9417 const ARMSubtarget *Subtarget) {
9418
Sam Parkerd616cf02016-06-20 16:47:09 +00009419 if (Subtarget->isThumb1Only()) return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009420
Sam Parkerd616cf02016-06-20 16:47:09 +00009421 // Only perform the checks after legalize when the pattern is available.
9422 if (DCI.isBeforeLegalize()) return SDValue();
9423
9424 return AddCombineTo64bitUMAAL(N, DCI, Subtarget);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009425}
9426
Bob Wilson728eb292010-07-29 20:34:14 +00009427/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
9428/// operands N0 and N1. This is a helper for PerformADDCombine that is
9429/// called with the default operands, and if that fails, with commuted
9430/// operands.
9431static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009432 TargetLowering::DAGCombinerInfo &DCI,
9433 const ARMSubtarget *Subtarget){
9434
9435 // Attempt to create vpaddl for this add.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009436 if (SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget))
Tanya Lattnere9e67052011-06-14 23:48:48 +00009437 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00009438
Chris Lattner4147f082009-03-12 06:52:53 +00009439 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009440 if (N0.getNode()->hasOneUse())
9441 if (SDValue Result = combineSelectAndUse(N, N0, N1, DCI))
9442 return Result;
Chris Lattner4147f082009-03-12 06:52:53 +00009443 return SDValue();
9444}
9445
Bob Wilson728eb292010-07-29 20:34:14 +00009446/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
9447///
9448static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00009449 TargetLowering::DAGCombinerInfo &DCI,
9450 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00009451 SDValue N0 = N->getOperand(0);
9452 SDValue N1 = N->getOperand(1);
9453
9454 // First try with the default operand order.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009455 if (SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget))
Bob Wilson728eb292010-07-29 20:34:14 +00009456 return Result;
9457
9458 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00009459 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00009460}
9461
Chris Lattner4147f082009-03-12 06:52:53 +00009462/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00009463///
Chris Lattner4147f082009-03-12 06:52:53 +00009464static SDValue PerformSUBCombine(SDNode *N,
9465 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00009466 SDValue N0 = N->getOperand(0);
9467 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00009468
Chris Lattner4147f082009-03-12 06:52:53 +00009469 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009470 if (N1.getNode()->hasOneUse())
9471 if (SDValue Result = combineSelectAndUse(N, N1, N0, DCI))
9472 return Result;
Bob Wilson7117a912009-03-20 22:42:55 +00009473
Chris Lattner4147f082009-03-12 06:52:53 +00009474 return SDValue();
9475}
9476
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009477/// PerformVMULCombine
9478/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
9479/// special multiplier accumulator forwarding.
9480/// vmul d3, d0, d2
9481/// vmla d3, d1, d2
9482/// is faster than
9483/// vadd d3, d0, d1
9484/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00009485// However, for (A + B) * (A + B),
9486// vadd d2, d0, d1
9487// vmul d3, d0, d2
9488// vmla d3, d1, d2
9489// is slower than
9490// vadd d2, d0, d1
9491// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009492static SDValue PerformVMULCombine(SDNode *N,
9493 TargetLowering::DAGCombinerInfo &DCI,
9494 const ARMSubtarget *Subtarget) {
9495 if (!Subtarget->hasVMLxForwarding())
9496 return SDValue();
9497
9498 SelectionDAG &DAG = DCI.DAG;
9499 SDValue N0 = N->getOperand(0);
9500 SDValue N1 = N->getOperand(1);
9501 unsigned Opcode = N0.getOpcode();
9502 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
9503 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00009504 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009505 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
9506 Opcode != ISD::FADD && Opcode != ISD::FSUB)
9507 return SDValue();
9508 std::swap(N0, N1);
9509 }
9510
Weiming Zhao2052f482013-09-25 23:12:06 +00009511 if (N0 == N1)
9512 return SDValue();
9513
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009514 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009515 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009516 SDValue N00 = N0->getOperand(0);
9517 SDValue N01 = N0->getOperand(1);
9518 return DAG.getNode(Opcode, DL, VT,
9519 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
9520 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
9521}
9522
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009523static SDValue PerformMULCombine(SDNode *N,
9524 TargetLowering::DAGCombinerInfo &DCI,
9525 const ARMSubtarget *Subtarget) {
9526 SelectionDAG &DAG = DCI.DAG;
9527
9528 if (Subtarget->isThumb1Only())
9529 return SDValue();
9530
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009531 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9532 return SDValue();
9533
9534 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00009535 if (VT.is64BitVector() || VT.is128BitVector())
9536 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009537 if (VT != MVT::i32)
9538 return SDValue();
9539
9540 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9541 if (!C)
9542 return SDValue();
9543
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009544 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009545 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009546
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009547 ShiftAmt = ShiftAmt & (32 - 1);
9548 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009549 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009550
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009551 SDValue Res;
9552 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009553
9554 if (MulAmt >= 0) {
9555 if (isPowerOf2_32(MulAmt - 1)) {
9556 // (mul x, 2^N + 1) => (add (shl x, N), x)
9557 Res = DAG.getNode(ISD::ADD, DL, VT,
9558 V,
9559 DAG.getNode(ISD::SHL, DL, VT,
9560 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009561 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009562 MVT::i32)));
9563 } else if (isPowerOf2_32(MulAmt + 1)) {
9564 // (mul x, 2^N - 1) => (sub (shl x, N), x)
9565 Res = DAG.getNode(ISD::SUB, DL, VT,
9566 DAG.getNode(ISD::SHL, DL, VT,
9567 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009568 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009569 MVT::i32)),
9570 V);
9571 } else
9572 return SDValue();
9573 } else {
9574 uint64_t MulAmtAbs = -MulAmt;
9575 if (isPowerOf2_32(MulAmtAbs + 1)) {
9576 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
9577 Res = DAG.getNode(ISD::SUB, DL, VT,
9578 V,
9579 DAG.getNode(ISD::SHL, DL, VT,
9580 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009581 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009582 MVT::i32)));
9583 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
9584 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
9585 Res = DAG.getNode(ISD::ADD, DL, VT,
9586 V,
9587 DAG.getNode(ISD::SHL, DL, VT,
9588 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009589 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009590 MVT::i32)));
9591 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009592 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009593
9594 } else
9595 return SDValue();
9596 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009597
9598 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00009599 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009600 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009601
9602 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00009603 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009604 return SDValue();
9605}
9606
Owen Anderson30c48922010-11-05 19:27:46 +00009607static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00009608 TargetLowering::DAGCombinerInfo &DCI,
9609 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00009610
Owen Anderson30c48922010-11-05 19:27:46 +00009611 // Attempt to use immediate-form VBIC
9612 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009613 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00009614 EVT VT = N->getValueType(0);
9615 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009616
Tanya Lattner266792a2011-04-07 15:24:20 +00009617 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9618 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009619
Owen Anderson30c48922010-11-05 19:27:46 +00009620 APInt SplatBits, SplatUndef;
9621 unsigned SplatBitSize;
9622 bool HasAnyUndefs;
9623 if (BVN &&
9624 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9625 if (SplatBitSize <= 64) {
9626 EVT VbicVT;
9627 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
9628 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009629 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009630 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00009631 if (Val.getNode()) {
9632 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009633 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00009634 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009635 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00009636 }
9637 }
9638 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009639
Evan Chenge87681c2012-02-23 01:19:06 +00009640 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009641 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009642 if (SDValue Result = combineSelectAndUseCommutative(N, true, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009643 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009644 }
9645
Owen Anderson30c48922010-11-05 19:27:46 +00009646 return SDValue();
9647}
9648
Jim Grosbach11013ed2010-07-16 23:05:05 +00009649/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
9650static SDValue PerformORCombine(SDNode *N,
9651 TargetLowering::DAGCombinerInfo &DCI,
9652 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009653 // Attempt to use immediate-form VORR
9654 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00009655 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009656 EVT VT = N->getValueType(0);
9657 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00009658
Tanya Lattner266792a2011-04-07 15:24:20 +00009659 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9660 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00009661
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009662 APInt SplatBits, SplatUndef;
9663 unsigned SplatBitSize;
9664 bool HasAnyUndefs;
9665 if (BVN && Subtarget->hasNEON() &&
9666 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
9667 if (SplatBitSize <= 64) {
9668 EVT VorrVT;
9669 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
9670 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009671 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00009672 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009673 if (Val.getNode()) {
9674 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00009675 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009676 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00009677 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00009678 }
9679 }
9680 }
9681
Evan Chenge87681c2012-02-23 01:19:06 +00009682 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009683 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009684 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009685 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009686 }
9687
Nadav Rotem3a94c542012-08-13 18:52:44 +00009688 // The code below optimizes (or (and X, Y), Z).
9689 // The AND operand needs to have a single user to make these optimizations
9690 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009691 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00009692 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009693 return SDValue();
9694 SDValue N1 = N->getOperand(1);
9695
9696 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
9697 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
9698 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
9699 APInt SplatUndef;
9700 unsigned SplatBitSize;
9701 bool HasAnyUndefs;
9702
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009703 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009704 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009705 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
9706 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009707 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00009708 HasAnyUndefs) && !HasAnyUndefs) {
9709 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
9710 HasAnyUndefs) && !HasAnyUndefs) {
9711 // Ensure that the bit width of the constants are the same and that
9712 // the splat arguments are logical inverses as per the pattern we
9713 // are trying to simplify.
9714 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
9715 SplatBits0 == ~SplatBits1) {
9716 // Canonicalize the vector type to make instruction selection
9717 // simpler.
9718 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
9719 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
9720 N0->getOperand(1),
9721 N0->getOperand(0),
9722 N1->getOperand(0));
9723 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
9724 }
9725 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00009726 }
9727 }
9728
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009729 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
9730 // reasonable.
9731
Jim Grosbach11013ed2010-07-16 23:05:05 +00009732 // BFI is only available on V6T2+
9733 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
9734 return SDValue();
9735
Andrew Trickef9de2a2013-05-25 02:42:55 +00009736 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009737 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009738 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009739 //
9740 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009741 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009742 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00009743 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00009744 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009745 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009746
Jim Grosbach11013ed2010-07-16 23:05:05 +00009747 if (VT != MVT::i32)
9748 return SDValue();
9749
Evan Cheng2e51bb42010-12-13 20:32:54 +00009750 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009751
Jim Grosbach11013ed2010-07-16 23:05:05 +00009752 // The value and the mask need to be constants so we can verify this is
9753 // actually a bitfield set. If the mask is 0xffff, we can do better
9754 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00009755 SDValue MaskOp = N0.getOperand(1);
9756 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
9757 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00009758 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009759 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009760 if (Mask == 0xffff)
9761 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009762 SDValue Res;
9763 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009764 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9765 if (N1C) {
9766 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00009767 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009768 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00009769
Evan Cheng34345752010-12-11 04:11:38 +00009770 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009771 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009772
Evan Cheng2e51bb42010-12-13 20:32:54 +00009773 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009774 DAG.getConstant(Val, DL, MVT::i32),
9775 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00009776
9777 // Do not add new nodes to DAG combiner worklist.
9778 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009779 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00009780 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009781 } else if (N1.getOpcode() == ISD::AND) {
9782 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00009783 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9784 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009785 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00009786 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009787
Eric Christopherd5530962011-03-26 01:21:03 +00009788 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
9789 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009790 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009791 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009792 // The pack halfword instruction works better for masks that fit it,
9793 // so use that when it's available.
9794 if (Subtarget->hasT2ExtractPack() &&
9795 (Mask == 0xffff || Mask == 0xffff0000))
9796 return SDValue();
9797 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009798 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009799 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009800 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009801 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009802 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009803 // Do not add new nodes to DAG combiner worklist.
9804 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009805 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009806 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00009807 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009808 // The pack halfword instruction works better for masks that fit it,
9809 // so use that when it's available.
9810 if (Subtarget->hasT2ExtractPack() &&
9811 (Mask2 == 0xffff || Mask2 == 0xffff0000))
9812 return SDValue();
9813 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009814 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009815 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009816 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009817 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009818 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009819 // Do not add new nodes to DAG combiner worklist.
9820 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009821 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00009822 }
9823 }
Wesley Peck527da1b2010-11-23 03:31:01 +00009824
Evan Cheng2e51bb42010-12-13 20:32:54 +00009825 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
9826 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
9827 ARM::isBitFieldInvertedMask(~Mask)) {
9828 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
9829 // where lsb(mask) == #shamt and masked bits of B are known zero.
9830 SDValue ShAmt = N00.getOperand(1);
9831 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009832 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00009833 if (ShAmtC != LSB)
9834 return SDValue();
9835
9836 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009837 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00009838
9839 // Do not add new nodes to DAG combiner worklist.
9840 DCI.CombineTo(N, Res, false);
9841 }
9842
Jim Grosbach11013ed2010-07-16 23:05:05 +00009843 return SDValue();
9844}
9845
Evan Chenge87681c2012-02-23 01:19:06 +00009846static SDValue PerformXORCombine(SDNode *N,
9847 TargetLowering::DAGCombinerInfo &DCI,
9848 const ARMSubtarget *Subtarget) {
9849 EVT VT = N->getValueType(0);
9850 SelectionDAG &DAG = DCI.DAG;
9851
9852 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
9853 return SDValue();
9854
9855 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009856 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00009857 if (SDValue Result = combineSelectAndUseCommutative(N, false, DCI))
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00009858 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00009859 }
9860
9861 return SDValue();
9862}
9863
James Molloyce12c922015-11-11 15:40:40 +00009864// ParseBFI - given a BFI instruction in N, extract the "from" value (Rn) and return it,
9865// and fill in FromMask and ToMask with (consecutive) bits in "from" to be extracted and
9866// their position in "to" (Rd).
9867static SDValue ParseBFI(SDNode *N, APInt &ToMask, APInt &FromMask) {
9868 assert(N->getOpcode() == ARMISD::BFI);
Chad Rosier353d7192015-12-21 18:08:05 +00009869
James Molloyce12c922015-11-11 15:40:40 +00009870 SDValue From = N->getOperand(1);
9871 ToMask = ~cast<ConstantSDNode>(N->getOperand(2))->getAPIntValue();
9872 FromMask = APInt::getLowBitsSet(ToMask.getBitWidth(), ToMask.countPopulation());
9873
9874 // If the Base came from a SHR #C, we can deduce that it is really testing bit
9875 // #C in the base of the SHR.
9876 if (From->getOpcode() == ISD::SRL &&
9877 isa<ConstantSDNode>(From->getOperand(1))) {
9878 APInt Shift = cast<ConstantSDNode>(From->getOperand(1))->getAPIntValue();
9879 assert(Shift.getLimitedValue() < 32 && "Shift too large!");
9880 FromMask <<= Shift.getLimitedValue(31);
9881 From = From->getOperand(0);
9882 }
9883
9884 return From;
9885}
9886
9887// If A and B contain one contiguous set of bits, does A | B == A . B?
9888//
9889// Neither A nor B must be zero.
9890static bool BitsProperlyConcatenate(const APInt &A, const APInt &B) {
9891 unsigned LastActiveBitInA = A.countTrailingZeros();
9892 unsigned FirstActiveBitInB = B.getBitWidth() - B.countLeadingZeros() - 1;
9893 return LastActiveBitInA - 1 == FirstActiveBitInB;
9894}
9895
9896static SDValue FindBFIToCombineWith(SDNode *N) {
9897 // We have a BFI in N. Follow a possible chain of BFIs and find a BFI it can combine with,
9898 // if one exists.
9899 APInt ToMask, FromMask;
9900 SDValue From = ParseBFI(N, ToMask, FromMask);
9901 SDValue To = N->getOperand(0);
9902
9903 // Now check for a compatible BFI to merge with. We can pass through BFIs that
9904 // aren't compatible, but not if they set the same bit in their destination as
9905 // we do (or that of any BFI we're going to combine with).
9906 SDValue V = To;
9907 APInt CombinedToMask = ToMask;
9908 while (V.getOpcode() == ARMISD::BFI) {
9909 APInt NewToMask, NewFromMask;
9910 SDValue NewFrom = ParseBFI(V.getNode(), NewToMask, NewFromMask);
9911 if (NewFrom != From) {
9912 // This BFI has a different base. Keep going.
9913 CombinedToMask |= NewToMask;
9914 V = V.getOperand(0);
9915 continue;
9916 }
9917
9918 // Do the written bits conflict with any we've seen so far?
9919 if ((NewToMask & CombinedToMask).getBoolValue())
9920 // Conflicting bits - bail out because going further is unsafe.
9921 return SDValue();
9922
9923 // Are the new bits contiguous when combined with the old bits?
9924 if (BitsProperlyConcatenate(ToMask, NewToMask) &&
9925 BitsProperlyConcatenate(FromMask, NewFromMask))
9926 return V;
9927 if (BitsProperlyConcatenate(NewToMask, ToMask) &&
9928 BitsProperlyConcatenate(NewFromMask, FromMask))
9929 return V;
Chad Rosier353d7192015-12-21 18:08:05 +00009930
James Molloyce12c922015-11-11 15:40:40 +00009931 // We've seen a write to some bits, so track it.
9932 CombinedToMask |= NewToMask;
9933 // Keep going...
9934 V = V.getOperand(0);
9935 }
9936
9937 return SDValue();
9938}
9939
Evan Chengc1778132010-12-14 03:22:07 +00009940static SDValue PerformBFICombine(SDNode *N,
9941 TargetLowering::DAGCombinerInfo &DCI) {
9942 SDValue N1 = N->getOperand(1);
9943 if (N1.getOpcode() == ISD::AND) {
James Molloyce12c922015-11-11 15:40:40 +00009944 // (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
9945 // the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00009946 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
9947 if (!N11C)
9948 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009949 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00009950 unsigned LSB = countTrailingZeros(~InvMask);
9951 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00009952 assert(Width <
9953 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00009954 "undefined behavior");
9955 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00009956 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00009957 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00009958 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00009959 N->getOperand(0), N1.getOperand(0),
9960 N->getOperand(2));
James Molloyce12c922015-11-11 15:40:40 +00009961 } else if (N->getOperand(0).getOpcode() == ARMISD::BFI) {
9962 // We have a BFI of a BFI. Walk up the BFI chain to see how long it goes.
9963 // Keep track of any consecutive bits set that all come from the same base
9964 // value. We can combine these together into a single BFI.
9965 SDValue CombineBFI = FindBFIToCombineWith(N);
9966 if (CombineBFI == SDValue())
9967 return SDValue();
9968
9969 // We've found a BFI.
9970 APInt ToMask1, FromMask1;
9971 SDValue From1 = ParseBFI(N, ToMask1, FromMask1);
9972
9973 APInt ToMask2, FromMask2;
Diego Novillo0767ae52015-11-11 16:39:22 +00009974 SDValue From2 = ParseBFI(CombineBFI.getNode(), ToMask2, FromMask2);
9975 assert(From1 == From2);
9976 (void)From2;
Chad Rosier353d7192015-12-21 18:08:05 +00009977
James Molloyce12c922015-11-11 15:40:40 +00009978 // First, unlink CombineBFI.
9979 DCI.DAG.ReplaceAllUsesWith(CombineBFI, CombineBFI.getOperand(0));
9980 // Then create a new BFI, combining the two together.
9981 APInt NewFromMask = FromMask1 | FromMask2;
9982 APInt NewToMask = ToMask1 | ToMask2;
9983
9984 EVT VT = N->getValueType(0);
9985 SDLoc dl(N);
9986
9987 if (NewFromMask[0] == 0)
9988 From1 = DCI.DAG.getNode(
9989 ISD::SRL, dl, VT, From1,
9990 DCI.DAG.getConstant(NewFromMask.countTrailingZeros(), dl, VT));
9991 return DCI.DAG.getNode(ARMISD::BFI, dl, VT, N->getOperand(0), From1,
9992 DCI.DAG.getConstant(~NewToMask, dl, VT));
Evan Chengc1778132010-12-14 03:22:07 +00009993 }
9994 return SDValue();
9995}
9996
Bob Wilson22806742010-09-22 22:09:21 +00009997/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
9998/// ARMISD::VMOVRRD.
9999static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +000010000 TargetLowering::DAGCombinerInfo &DCI,
10001 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +000010002 // vmovrrd(vmovdrr x, y) -> x,y
10003 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +000010004 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +000010005 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010006
10007 // vmovrrd(load f64) -> (load i32), (load i32)
10008 SDNode *InNode = InDouble.getNode();
10009 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
10010 InNode->getValueType(0) == MVT::f64 &&
10011 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
10012 !cast<LoadSDNode>(InNode)->isVolatile()) {
10013 // TODO: Should this be done for non-FrameIndex operands?
10014 LoadSDNode *LD = cast<LoadSDNode>(InNode);
10015
10016 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010017 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010018 SDValue BasePtr = LD->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +000010019 SDValue NewLD1 =
10020 DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr, LD->getPointerInfo(),
10021 LD->getAlignment(), LD->getMemOperand()->getFlags());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010022
10023 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010024 DAG.getConstant(4, DL, MVT::i32));
Justin Lebar9c375812016-07-15 18:27:10 +000010025 SDValue NewLD2 = DAG.getLoad(
10026 MVT::i32, DL, NewLD1.getValue(1), OffsetPtr, LD->getPointerInfo(),
10027 std::min(4U, LD->getAlignment() / 2), LD->getMemOperand()->getFlags());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010028
10029 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Mehdi Aminiffc14022015-07-08 01:00:38 +000010030 if (DCI.DAG.getDataLayout().isBigEndian())
Christian Pirker762b2c62014-06-01 09:30:52 +000010031 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010032 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +000010033 return Result;
10034 }
10035
Bob Wilson22806742010-09-22 22:09:21 +000010036 return SDValue();
10037}
10038
10039/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
10040/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
10041static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
10042 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
10043 SDValue Op0 = N->getOperand(0);
10044 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +000010045 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +000010046 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +000010047 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +000010048 Op1 = Op1.getOperand(0);
10049 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
10050 Op0.getNode() == Op1.getNode() &&
10051 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +000010052 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +000010053 N->getValueType(0), Op0.getOperand(0));
10054 return SDValue();
10055}
10056
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010057/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
10058/// are normal, non-volatile loads. If so, it is profitable to bitcast an
10059/// i64 vector to have f64 elements, since the value can then be loaded
10060/// directly into a VFP register.
10061static bool hasNormalLoadOperand(SDNode *N) {
10062 unsigned NumElts = N->getValueType(0).getVectorNumElements();
10063 for (unsigned i = 0; i < NumElts; ++i) {
10064 SDNode *Elt = N->getOperand(i).getNode();
10065 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
10066 return true;
10067 }
10068 return false;
10069}
10070
Bob Wilsoncb6db982010-09-17 22:59:05 +000010071/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
10072/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010073static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +000010074 TargetLowering::DAGCombinerInfo &DCI,
10075 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +000010076 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
10077 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
10078 // into a pair of GPRs, which is fine when the value is used as a scalar,
10079 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010080 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +000010081 if (N->getNumOperands() == 2)
10082 if (SDValue RV = PerformVMOVDRRCombine(N, DAG))
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010083 return RV;
Bob Wilsoncb6db982010-09-17 22:59:05 +000010084
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010085 // Load i64 elements as f64 values so that type legalization does not split
10086 // them up into i32 values.
10087 EVT VT = N->getValueType(0);
10088 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
10089 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010090 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010091 SmallVector<SDValue, 8> Ops;
10092 unsigned NumElts = VT.getVectorNumElements();
10093 for (unsigned i = 0; i < NumElts; ++i) {
10094 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
10095 Ops.push_back(V);
10096 // Make the DAGCombiner fold the bitcast.
10097 DCI.AddToWorklist(V.getNode());
10098 }
10099 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Ahmed Bougacha128f8732016-04-26 21:15:30 +000010100 SDValue BV = DAG.getBuildVector(FloatVT, dl, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010101 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
10102}
10103
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010104/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
10105static SDValue
10106PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
10107 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
10108 // At that time, we may have inserted bitcasts from integer to float.
10109 // If these bitcasts have survived DAGCombine, change the lowering of this
10110 // BUILD_VECTOR in something more vector friendly, i.e., that does not
10111 // force to use floating point types.
10112
10113 // Make sure we can change the type of the vector.
10114 // This is possible iff:
10115 // 1. The vector is only used in a bitcast to a integer type. I.e.,
10116 // 1.1. Vector is used only once.
10117 // 1.2. Use is a bit convert to an integer type.
10118 // 2. The size of its operands are 32-bits (64-bits are not legal).
10119 EVT VT = N->getValueType(0);
10120 EVT EltVT = VT.getVectorElementType();
10121
10122 // Check 1.1. and 2.
10123 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
10124 return SDValue();
10125
10126 // By construction, the input type must be float.
10127 assert(EltVT == MVT::f32 && "Unexpected type!");
10128
10129 // Check 1.2.
10130 SDNode *Use = *N->use_begin();
10131 if (Use->getOpcode() != ISD::BITCAST ||
10132 Use->getValueType(0).isFloatingPoint())
10133 return SDValue();
10134
10135 // Check profitability.
10136 // Model is, if more than half of the relevant operands are bitcast from
10137 // i32, turn the build_vector into a sequence of insert_vector_elt.
10138 // Relevant operands are everything that is not statically
10139 // (i.e., at compile time) bitcasted.
10140 unsigned NumOfBitCastedElts = 0;
10141 unsigned NumElts = VT.getVectorNumElements();
10142 unsigned NumOfRelevantElts = NumElts;
10143 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
10144 SDValue Elt = N->getOperand(Idx);
10145 if (Elt->getOpcode() == ISD::BITCAST) {
10146 // Assume only bit cast to i32 will go away.
10147 if (Elt->getOperand(0).getValueType() == MVT::i32)
10148 ++NumOfBitCastedElts;
Sanjay Patel57195842016-03-14 17:28:46 +000010149 } else if (Elt.isUndef() || isa<ConstantSDNode>(Elt))
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010150 // Constants are statically casted, thus do not count them as
10151 // relevant operands.
10152 --NumOfRelevantElts;
10153 }
10154
10155 // Check if more than half of the elements require a non-free bitcast.
10156 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
10157 return SDValue();
10158
10159 SelectionDAG &DAG = DCI.DAG;
10160 // Create the new vector type.
10161 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
10162 // Check if the type is legal.
10163 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10164 if (!TLI.isTypeLegal(VecVT))
10165 return SDValue();
10166
10167 // Combine:
10168 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
10169 // => BITCAST INSERT_VECTOR_ELT
10170 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
10171 // (BITCAST EN), N.
10172 SDValue Vec = DAG.getUNDEF(VecVT);
10173 SDLoc dl(N);
10174 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
10175 SDValue V = N->getOperand(Idx);
Sanjay Patel57195842016-03-14 17:28:46 +000010176 if (V.isUndef())
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010177 continue;
10178 if (V.getOpcode() == ISD::BITCAST &&
10179 V->getOperand(0).getValueType() == MVT::i32)
10180 // Fold obvious case.
10181 V = V.getOperand(0);
10182 else {
Jim Grosbach1a597112014-04-03 23:43:18 +000010183 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010184 // Make the DAGCombiner fold the bitcasts.
10185 DCI.AddToWorklist(V.getNode());
10186 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010187 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000010188 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
10189 }
10190 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
10191 // Make the DAGCombiner fold the bitcasts.
10192 DCI.AddToWorklist(Vec.getNode());
10193 return Vec;
10194}
10195
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010196/// PerformInsertEltCombine - Target-specific dag combine xforms for
10197/// ISD::INSERT_VECTOR_ELT.
10198static SDValue PerformInsertEltCombine(SDNode *N,
10199 TargetLowering::DAGCombinerInfo &DCI) {
10200 // Bitcast an i64 load inserted into a vector to f64.
10201 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10202 EVT VT = N->getValueType(0);
10203 SDNode *Elt = N->getOperand(1).getNode();
10204 if (VT.getVectorElementType() != MVT::i64 ||
10205 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
10206 return SDValue();
10207
10208 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +000010209 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000010210 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10211 VT.getVectorNumElements());
10212 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
10213 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
10214 // Make the DAGCombiner fold the bitcasts.
10215 DCI.AddToWorklist(Vec.getNode());
10216 DCI.AddToWorklist(V.getNode());
10217 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
10218 Vec, V, N->getOperand(2));
10219 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +000010220}
10221
Bob Wilsonc7334a12010-10-27 20:38:28 +000010222/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
10223/// ISD::VECTOR_SHUFFLE.
10224static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
10225 // The LLVM shufflevector instruction does not require the shuffle mask
10226 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
10227 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
10228 // operands do not match the mask length, they are extended by concatenating
10229 // them with undef vectors. That is probably the right thing for other
10230 // targets, but for NEON it is better to concatenate two double-register
10231 // size vector operands into a single quad-register size vector. Do that
10232 // transformation here:
10233 // shuffle(concat(v1, undef), concat(v2, undef)) ->
10234 // shuffle(concat(v1, v2), undef)
10235 SDValue Op0 = N->getOperand(0);
10236 SDValue Op1 = N->getOperand(1);
10237 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
10238 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
10239 Op0.getNumOperands() != 2 ||
10240 Op1.getNumOperands() != 2)
10241 return SDValue();
10242 SDValue Concat0Op1 = Op0.getOperand(1);
10243 SDValue Concat1Op1 = Op1.getOperand(1);
Sanjay Patel75068522016-03-14 18:09:43 +000010244 if (!Concat0Op1.isUndef() || !Concat1Op1.isUndef())
Bob Wilsonc7334a12010-10-27 20:38:28 +000010245 return SDValue();
10246 // Skip the transformation if any of the types are illegal.
10247 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10248 EVT VT = N->getValueType(0);
10249 if (!TLI.isTypeLegal(VT) ||
10250 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
10251 !TLI.isTypeLegal(Concat1Op1.getValueType()))
10252 return SDValue();
10253
Andrew Trickef9de2a2013-05-25 02:42:55 +000010254 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +000010255 Op0.getOperand(0), Op1.getOperand(0));
10256 // Translate the shuffle mask.
10257 SmallVector<int, 16> NewMask;
10258 unsigned NumElts = VT.getVectorNumElements();
10259 unsigned HalfElts = NumElts/2;
10260 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10261 for (unsigned n = 0; n < NumElts; ++n) {
10262 int MaskElt = SVN->getMaskElt(n);
10263 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +000010264 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +000010265 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +000010266 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +000010267 NewElt = HalfElts + MaskElt - NumElts;
10268 NewMask.push_back(NewElt);
10269 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010270 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Craig Topper2bd8b4b2016-07-01 06:54:47 +000010271 DAG.getUNDEF(VT), NewMask);
Bob Wilsonc7334a12010-10-27 20:38:28 +000010272}
10273
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010274/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
10275/// NEON load/store intrinsics, and generic vector load/stores, to merge
10276/// base address updates.
10277/// For generic load/stores, the memory type is assumed to be a vector.
10278/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +000010279static SDValue CombineBaseUpdate(SDNode *N,
10280 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +000010281 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010282 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
10283 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010284 const bool isStore = N->getOpcode() == ISD::STORE;
10285 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +000010286 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010287 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010288 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +000010289
10290 // Search for a use of the address operand that is an increment.
10291 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
10292 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
10293 SDNode *User = *UI;
10294 if (User->getOpcode() != ISD::ADD ||
10295 UI.getUse().getResNo() != Addr.getResNo())
10296 continue;
10297
10298 // Check that the add is independent of the load/store. Otherwise, folding
10299 // it would create a cycle.
10300 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
10301 continue;
10302
10303 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010304 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +000010305 bool isLaneOp = false;
10306 unsigned NewOpc = 0;
10307 unsigned NumVecs = 0;
10308 if (isIntrinsic) {
10309 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
10310 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +000010311 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +000010312 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
10313 NumVecs = 1; break;
10314 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
10315 NumVecs = 2; break;
10316 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
10317 NumVecs = 3; break;
10318 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
10319 NumVecs = 4; break;
10320 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
10321 NumVecs = 2; isLaneOp = true; break;
10322 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
10323 NumVecs = 3; isLaneOp = true; break;
10324 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
10325 NumVecs = 4; isLaneOp = true; break;
10326 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010327 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010328 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010329 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010330 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010331 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010332 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010333 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010334 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010335 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010336 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010337 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010338 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010339 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010340 }
10341 } else {
10342 isLaneOp = true;
10343 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +000010344 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +000010345 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
10346 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
10347 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010348 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
10349 NumVecs = 1; isLaneOp = false; break;
10350 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
10351 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +000010352 }
10353 }
10354
10355 // Find the size of memory referenced by the load/store.
10356 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010357 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +000010358 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010359 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +000010360 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010361 } else {
10362 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
10363 VecTy = N->getOperand(1).getValueType();
10364 }
10365
Bob Wilson06fce872011-02-07 17:43:21 +000010366 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
10367 if (isLaneOp)
10368 NumBytes /= VecTy.getVectorNumElements();
10369
10370 // If the increment is a constant, it must match the memory ref size.
10371 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
10372 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
10373 uint64_t IncVal = CInc->getZExtValue();
10374 if (IncVal != NumBytes)
10375 continue;
10376 } else if (NumBytes >= 3 * 16) {
10377 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
10378 // separate instructions that make it harder to use a non-constant update.
10379 continue;
10380 }
10381
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010382 // OK, we found an ADD we can fold into the base update.
10383 // Now, create a _UPD node, taking care of not breaking alignment.
10384
10385 EVT AlignedVecTy = VecTy;
10386 unsigned Alignment = MemN->getAlignment();
10387
10388 // If this is a less-than-standard-aligned load/store, change the type to
10389 // match the standard alignment.
10390 // The alignment is overlooked when selecting _UPD variants; and it's
10391 // easier to introduce bitcasts here than fix that.
10392 // There are 3 ways to get to this base-update combine:
10393 // - intrinsics: they are assumed to be properly aligned (to the standard
10394 // alignment of the memory type), so we don't need to do anything.
10395 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
10396 // intrinsics, so, likewise, there's nothing to do.
10397 // - generic load/store instructions: the alignment is specified as an
10398 // explicit operand, rather than implicitly as the standard alignment
10399 // of the memory type (like the intrisics). We need to change the
10400 // memory type to match the explicit alignment. That way, we don't
10401 // generate non-standard-aligned ARMISD::VLDx nodes.
10402 if (isa<LSBaseSDNode>(N)) {
10403 if (Alignment == 0)
10404 Alignment = 1;
10405 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
10406 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
10407 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
10408 assert(!isLaneOp && "Unexpected generic load/store lane.");
10409 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
10410 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
10411 }
10412 // Don't set an explicit alignment on regular load/stores that we want
10413 // to transform to VLD/VST 1_UPD nodes.
10414 // This matches the behavior of regular load/stores, which only get an
10415 // explicit alignment if the MMO alignment is larger than the standard
10416 // alignment of the memory type.
10417 // Intrinsics, however, always get an explicit alignment, set to the
10418 // alignment of the MMO.
10419 Alignment = 1;
10420 }
10421
Bob Wilson06fce872011-02-07 17:43:21 +000010422 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010423 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +000010424 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010425 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +000010426 unsigned n;
10427 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010428 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +000010429 Tys[n++] = MVT::i32;
10430 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +000010431 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010432
10433 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +000010434 SmallVector<SDValue, 8> Ops;
10435 Ops.push_back(N->getOperand(0)); // incoming chain
10436 Ops.push_back(N->getOperand(AddrOpIdx));
10437 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010438
10439 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
10440 // Try to match the intrinsic's signature
10441 Ops.push_back(StN->getValue());
10442 } else {
10443 // Loads (and of course intrinsics) match the intrinsics' signature,
10444 // so just add all but the alignment operand.
10445 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
10446 Ops.push_back(N->getOperand(i));
10447 }
10448
10449 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010450 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010451
10452 // If this is a non-standard-aligned STORE, the penultimate operand is the
10453 // stored value. Bitcast it to the aligned type.
10454 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
10455 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010456 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010457 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010458
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010459 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010460 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010461 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +000010462
10463 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +000010464 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +000010465 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +000010466 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010467
10468 // If this is an non-standard-aligned LOAD, the first result is the loaded
10469 // value. Bitcast it to the expected result type.
10470 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
10471 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010472 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010473 }
10474
Bob Wilson06fce872011-02-07 17:43:21 +000010475 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
10476 DCI.CombineTo(N, NewResults);
10477 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
10478
10479 break;
Owen Anderson77aa2662011-04-05 21:48:57 +000010480 }
Bob Wilson06fce872011-02-07 17:43:21 +000010481 return SDValue();
10482}
10483
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010484static SDValue PerformVLDCombine(SDNode *N,
10485 TargetLowering::DAGCombinerInfo &DCI) {
10486 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10487 return SDValue();
10488
10489 return CombineBaseUpdate(N, DCI);
10490}
10491
Bob Wilson2d790df2010-11-28 06:51:26 +000010492/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
10493/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
10494/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
10495/// return true.
10496static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
10497 SelectionDAG &DAG = DCI.DAG;
10498 EVT VT = N->getValueType(0);
10499 // vldN-dup instructions only support 64-bit vectors for N > 1.
10500 if (!VT.is64BitVector())
10501 return false;
10502
10503 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
10504 SDNode *VLD = N->getOperand(0).getNode();
10505 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
10506 return false;
10507 unsigned NumVecs = 0;
10508 unsigned NewOpc = 0;
10509 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
10510 if (IntNo == Intrinsic::arm_neon_vld2lane) {
10511 NumVecs = 2;
10512 NewOpc = ARMISD::VLD2DUP;
10513 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
10514 NumVecs = 3;
10515 NewOpc = ARMISD::VLD3DUP;
10516 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
10517 NumVecs = 4;
10518 NewOpc = ARMISD::VLD4DUP;
10519 } else {
10520 return false;
10521 }
10522
10523 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
10524 // numbers match the load.
10525 unsigned VLDLaneNo =
10526 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
10527 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
10528 UI != UE; ++UI) {
10529 // Ignore uses of the chain result.
10530 if (UI.getUse().getResNo() == NumVecs)
10531 continue;
10532 SDNode *User = *UI;
10533 if (User->getOpcode() != ARMISD::VDUPLANE ||
10534 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
10535 return false;
10536 }
10537
10538 // Create the vldN-dup node.
10539 EVT Tys[5];
10540 unsigned n;
10541 for (n = 0; n < NumVecs; ++n)
10542 Tys[n] = VT;
10543 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +000010544 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +000010545 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
10546 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010547 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +000010548 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +000010549 VLDMemInt->getMemOperand());
10550
10551 // Update the uses.
10552 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
10553 UI != UE; ++UI) {
10554 unsigned ResNo = UI.getUse().getResNo();
10555 // Ignore uses of the chain result.
10556 if (ResNo == NumVecs)
10557 continue;
10558 SDNode *User = *UI;
10559 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
10560 }
10561
10562 // Now the vldN-lane intrinsic is dead except for its chain result.
10563 // Update uses of the chain.
10564 std::vector<SDValue> VLDDupResults;
10565 for (unsigned n = 0; n < NumVecs; ++n)
10566 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
10567 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
10568 DCI.CombineTo(VLD, VLDDupResults);
10569
10570 return true;
10571}
10572
Bob Wilson103a0dc2010-07-14 01:22:12 +000010573/// PerformVDUPLANECombine - Target-specific dag combine xforms for
10574/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +000010575static SDValue PerformVDUPLANECombine(SDNode *N,
10576 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +000010577 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010578
Bob Wilson2d790df2010-11-28 06:51:26 +000010579 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
10580 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
10581 if (CombineVLDDUP(N, DCI))
10582 return SDValue(N, 0);
10583
10584 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
10585 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +000010586 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010587 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +000010588 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +000010589 return SDValue();
10590
10591 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
Sanjay Patel284582b2016-09-14 16:54:10 +000010592 unsigned EltSize = Op.getScalarValueSizeInBits();
Bob Wilson103a0dc2010-07-14 01:22:12 +000010593 // The canonical VMOV for a zero vector uses a 32-bit element size.
10594 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
10595 unsigned EltBits;
10596 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
10597 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +000010598 EVT VT = N->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010599 if (EltSize > VT.getScalarSizeInBits())
Bob Wilson103a0dc2010-07-14 01:22:12 +000010600 return SDValue();
10601
Andrew Trickef9de2a2013-05-25 02:42:55 +000010602 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +000010603}
10604
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010605static SDValue PerformLOADCombine(SDNode *N,
10606 TargetLowering::DAGCombinerInfo &DCI) {
10607 EVT VT = N->getValueType(0);
10608
10609 // If this is a legal vector load, try to combine it into a VLD1_UPD.
10610 if (ISD::isNormalLoad(N) && VT.isVector() &&
10611 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10612 return CombineBaseUpdate(N, DCI);
10613
10614 return SDValue();
10615}
10616
Ahmed Bougacha23167462014-12-09 21:26:53 +000010617/// PerformSTORECombine - Target-specific dag combine xforms for
10618/// ISD::STORE.
10619static SDValue PerformSTORECombine(SDNode *N,
10620 TargetLowering::DAGCombinerInfo &DCI) {
10621 StoreSDNode *St = cast<StoreSDNode>(N);
10622 if (St->isVolatile())
10623 return SDValue();
10624
10625 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
10626 // pack all of the elements in one place. Next, store to memory in fewer
10627 // chunks.
10628 SDValue StVal = St->getValue();
10629 EVT VT = StVal.getValueType();
10630 if (St->isTruncatingStore() && VT.isVector()) {
10631 SelectionDAG &DAG = DCI.DAG;
10632 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10633 EVT StVT = St->getMemoryVT();
10634 unsigned NumElems = VT.getVectorNumElements();
10635 assert(StVT != VT && "Cannot truncate to the same type");
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010636 unsigned FromEltSz = VT.getScalarSizeInBits();
10637 unsigned ToEltSz = StVT.getScalarSizeInBits();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010638
10639 // From, To sizes and ElemCount must be pow of two
10640 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
10641
10642 // We are going to use the original vector elt for storing.
10643 // Accumulated smaller vector elements must be a multiple of the store size.
10644 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
10645
10646 unsigned SizeRatio = FromEltSz / ToEltSz;
10647 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
10648
10649 // Create a type on which we perform the shuffle.
10650 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
10651 NumElems*SizeRatio);
10652 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
10653
10654 SDLoc DL(St);
10655 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
10656 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
10657 for (unsigned i = 0; i < NumElems; ++i)
Mehdi Aminiffc14022015-07-08 01:00:38 +000010658 ShuffleVec[i] = DAG.getDataLayout().isBigEndian()
10659 ? (i + 1) * SizeRatio - 1
10660 : i * SizeRatio;
Ahmed Bougacha23167462014-12-09 21:26:53 +000010661
10662 // Can't shuffle using an illegal type.
10663 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
10664
10665 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
10666 DAG.getUNDEF(WideVec.getValueType()),
Craig Topper2bd8b4b2016-07-01 06:54:47 +000010667 ShuffleVec);
Ahmed Bougacha23167462014-12-09 21:26:53 +000010668 // At this point all of the data is stored at the bottom of the
10669 // register. We now need to save it to mem.
10670
10671 // Find the largest store unit
10672 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +000010673 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +000010674 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
10675 StoreType = Tp;
10676 }
10677 // Didn't find a legal store type.
10678 if (!TLI.isTypeLegal(StoreType))
10679 return SDValue();
10680
10681 // Bitcast the original vector into a vector of store-size units
10682 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
10683 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
10684 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
10685 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
10686 SmallVector<SDValue, 8> Chains;
Mehdi Amini44ede332015-07-09 02:09:04 +000010687 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, DL,
10688 TLI.getPointerTy(DAG.getDataLayout()));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010689 SDValue BasePtr = St->getBasePtr();
10690
10691 // Perform one or more big stores into memory.
10692 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
10693 for (unsigned I = 0; I < E; I++) {
10694 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
10695 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010696 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010697 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
Justin Lebar9c375812016-07-15 18:27:10 +000010698 St->getPointerInfo(), St->getAlignment(),
10699 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010700 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
10701 Increment);
10702 Chains.push_back(Ch);
10703 }
10704 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
10705 }
10706
10707 if (!ISD::isNormalStore(St))
10708 return SDValue();
10709
10710 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
10711 // ARM stores of arguments in the same cache line.
10712 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
10713 StVal.getNode()->hasOneUse()) {
10714 SelectionDAG &DAG = DCI.DAG;
Mehdi Aminiffc14022015-07-08 01:00:38 +000010715 bool isBigEndian = DAG.getDataLayout().isBigEndian();
Ahmed Bougacha23167462014-12-09 21:26:53 +000010716 SDLoc DL(St);
10717 SDValue BasePtr = St->getBasePtr();
Justin Lebar9c375812016-07-15 18:27:10 +000010718 SDValue NewST1 = DAG.getStore(
10719 St->getChain(), DL, StVal.getNode()->getOperand(isBigEndian ? 1 : 0),
10720 BasePtr, St->getPointerInfo(), St->getAlignment(),
10721 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010722
10723 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010724 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +000010725 return DAG.getStore(NewST1.getValue(0), DL,
10726 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Justin Lebar9c375812016-07-15 18:27:10 +000010727 OffsetPtr, St->getPointerInfo(),
10728 std::min(4U, St->getAlignment() / 2),
10729 St->getMemOperand()->getFlags());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010730 }
10731
10732 if (StVal.getValueType() == MVT::i64 &&
10733 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
10734
10735 // Bitcast an i64 store extracted from a vector to f64.
10736 // Otherwise, the i64 value will be legalized to a pair of i32 values.
10737 SelectionDAG &DAG = DCI.DAG;
10738 SDLoc dl(StVal);
10739 SDValue IntVec = StVal.getOperand(0);
10740 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
10741 IntVec.getValueType().getVectorNumElements());
10742 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
10743 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
10744 Vec, StVal.getOperand(1));
10745 dl = SDLoc(N);
10746 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
10747 // Make the DAGCombiner fold the bitcasts.
10748 DCI.AddToWorklist(Vec.getNode());
10749 DCI.AddToWorklist(ExtElt.getNode());
10750 DCI.AddToWorklist(V.getNode());
10751 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
Justin Lebar9c375812016-07-15 18:27:10 +000010752 St->getPointerInfo(), St->getAlignment(),
10753 St->getMemOperand()->getFlags(), St->getAAInfo());
Ahmed Bougacha23167462014-12-09 21:26:53 +000010754 }
10755
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000010756 // If this is a legal vector store, try to combine it into a VST1_UPD.
10757 if (ISD::isNormalStore(N) && VT.isVector() &&
10758 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
10759 return CombineBaseUpdate(N, DCI);
10760
Ahmed Bougacha23167462014-12-09 21:26:53 +000010761 return SDValue();
10762}
10763
Chad Rosierfa8d8932011-06-24 19:23:04 +000010764/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
10765/// can replace combinations of VMUL and VCVT (floating-point to integer)
10766/// when the VMUL has a constant operand that is a power of 2.
10767///
10768/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10769/// vmul.f32 d16, d17, d16
10770/// vcvt.s32.f32 d16, d16
10771/// becomes:
10772/// vcvt.s32.f32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010773static SDValue PerformVCVTCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010774 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010775 if (!Subtarget->hasNEON())
10776 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010777
Chad Rosiera087fd22015-10-06 20:23:42 +000010778 SDValue Op = N->getOperand(0);
Tim Northover498c56c2016-03-17 20:10:28 +000010779 if (!Op.getValueType().isVector() || !Op.getValueType().isSimple() ||
10780 Op.getOpcode() != ISD::FMUL)
Chad Rosierfa8d8932011-06-24 19:23:04 +000010781 return SDValue();
10782
Chad Rosierfa8d8932011-06-24 19:23:04 +000010783 SDValue ConstVec = Op->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010784 if (!isa<BuildVectorSDNode>(ConstVec))
10785 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010786
Tim Northover7cbc2152013-06-28 15:29:25 +000010787 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010788 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010789 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010790 uint32_t IntBits = IntTy.getSizeInBits();
Bradley Smithececb7f2014-12-16 10:59:27 +000010791 unsigned NumLanes = Op.getValueType().getVectorNumElements();
Chad Rosier9df4aff2015-10-06 20:45:45 +000010792 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010793 // These instructions only exist converting from f32 to i32. We can handle
10794 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +000010795 // be lossy. We also can't handle more then 4 lanes, since these intructions
10796 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010797 return SDValue();
10798 }
10799
Chad Rosier169865f2015-10-07 17:28:58 +000010800 BitVector UndefElements;
10801 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10802 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10803 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010804 return SDValue();
10805
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010806 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010807 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010808 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
10809 Intrinsic::arm_neon_vcvtfp2fxu;
Chad Rosier9df4aff2015-10-06 20:45:45 +000010810 SDValue FixConv = DAG.getNode(
10811 ISD::INTRINSIC_WO_CHAIN, dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
10812 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32), Op->getOperand(0),
Chad Rosier169865f2015-10-07 17:28:58 +000010813 DAG.getConstant(C, dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +000010814
Chad Rosier9df4aff2015-10-06 20:45:45 +000010815 if (IntBits < FloatBits)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010816 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +000010817
10818 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +000010819}
10820
10821/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
10822/// can replace combinations of VCVT (integer to floating-point) and VDIV
10823/// when the VDIV has a constant operand that is a power of 2.
10824///
10825/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
10826/// vcvt.f32.s32 d16, d16
10827/// vdiv.f32 d16, d17, d16
10828/// becomes:
10829/// vcvt.f32.s32 d16, d16, #3
Chad Rosiera087fd22015-10-06 20:23:42 +000010830static SDValue PerformVDIVCombine(SDNode *N, SelectionDAG &DAG,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010831 const ARMSubtarget *Subtarget) {
Chad Rosiera087fd22015-10-06 20:23:42 +000010832 if (!Subtarget->hasNEON())
10833 return SDValue();
10834
Chad Rosierfa8d8932011-06-24 19:23:04 +000010835 SDValue Op = N->getOperand(0);
10836 unsigned OpOpcode = Op.getNode()->getOpcode();
Tim Northover498c56c2016-03-17 20:10:28 +000010837 if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple() ||
Chad Rosierfa8d8932011-06-24 19:23:04 +000010838 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
10839 return SDValue();
10840
Chad Rosierfa8d8932011-06-24 19:23:04 +000010841 SDValue ConstVec = N->getOperand(1);
Chad Rosieraed910b2015-10-06 20:51:26 +000010842 if (!isa<BuildVectorSDNode>(ConstVec))
10843 return SDValue();
Chad Rosierfa8d8932011-06-24 19:23:04 +000010844
Tim Northover7cbc2152013-06-28 15:29:25 +000010845 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010846 uint32_t FloatBits = FloatTy.getSizeInBits();
Tim Northover7cbc2152013-06-28 15:29:25 +000010847 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
Chad Rosierdca46b42015-10-06 20:58:42 +000010848 uint32_t IntBits = IntTy.getSizeInBits();
Chad Rosier17436bf2015-10-07 16:15:40 +000010849 unsigned NumLanes = Op.getValueType().getVectorNumElements();
10850 if (FloatBits != 32 || IntBits > 32 || NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +000010851 // These instructions only exist converting from i32 to f32. We can handle
10852 // smaller integers by generating an extra extend, but larger ones would
Chad Rosier17436bf2015-10-07 16:15:40 +000010853 // be lossy. We also can't handle more then 4 lanes, since these intructions
10854 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +000010855 return SDValue();
10856 }
10857
Chad Rosier169865f2015-10-07 17:28:58 +000010858 BitVector UndefElements;
10859 BuildVectorSDNode *BV = cast<BuildVectorSDNode>(ConstVec);
10860 int32_t C = BV->getConstantFPSplatPow2ToLog2Int(&UndefElements, 33);
10861 if (C == -1 || C == 0 || C > 32)
Chad Rosierdb71abf2015-10-07 13:40:44 +000010862 return SDValue();
10863
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010864 SDLoc dl(N);
Chad Rosier169865f2015-10-07 17:28:58 +000010865 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
Tim Northover7cbc2152013-06-28 15:29:25 +000010866 SDValue ConvInput = Op.getOperand(0);
Chad Rosierdca46b42015-10-06 20:58:42 +000010867 if (IntBits < FloatBits)
Tim Northover7cbc2152013-06-28 15:29:25 +000010868 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010869 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +000010870 ConvInput);
10871
Eric Christopher1b8b94192011-06-29 21:10:36 +000010872 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +000010873 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010874 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +000010875 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010876 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
Chad Rosier169865f2015-10-07 17:28:58 +000010877 ConvInput, DAG.getConstant(C, dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +000010878}
10879
10880/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +000010881/// operand of a vector shift operation, where all the elements of the
10882/// build_vector must have the same constant integer value.
10883static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
10884 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +000010885 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +000010886 Op = Op.getOperand(0);
10887 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
10888 APInt SplatBits, SplatUndef;
10889 unsigned SplatBitSize;
10890 bool HasAnyUndefs;
10891 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
10892 HasAnyUndefs, ElementBits) ||
10893 SplatBitSize > ElementBits)
10894 return false;
10895 Cnt = SplatBits.getSExtValue();
10896 return true;
10897}
10898
10899/// isVShiftLImm - Check if this is a valid build_vector for the immediate
10900/// operand of a vector shift left operation. That value must be in the range:
10901/// 0 <= Value < ElementBits for a left shift; or
10902/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010903static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010904 assert(VT.isVector() && "vector shift count is not a vector type");
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010905 int64_t ElementBits = VT.getScalarSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010906 if (! getVShiftImm(Op, ElementBits, Cnt))
10907 return false;
10908 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
10909}
10910
10911/// isVShiftRImm - Check if this is a valid build_vector for the immediate
10912/// operand of a vector shift right operation. For a shift opcode, the value
10913/// is positive, but for an intrinsic the value count must be negative. The
10914/// absolute value must be in the range:
10915/// 1 <= |Value| <= ElementBits for a right shift; or
10916/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010917static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +000010918 int64_t &Cnt) {
10919 assert(VT.isVector() && "vector shift count is not a vector type");
Sanjay Patel1ed771f2016-09-14 16:37:15 +000010920 int64_t ElementBits = VT.getScalarSizeInBits();
Bob Wilson2e076c42009-06-22 23:27:02 +000010921 if (! getVShiftImm(Op, ElementBits, Cnt))
10922 return false;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010923 if (!isIntrinsic)
10924 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
10925 if (Cnt >= -(isNarrow ? ElementBits/2 : ElementBits) && Cnt <= -1) {
Bob Wilson2e076c42009-06-22 23:27:02 +000010926 Cnt = -Cnt;
Luke Cheesemanb5c627a2015-07-24 09:31:48 +000010927 return true;
10928 }
10929 return false;
Bob Wilson2e076c42009-06-22 23:27:02 +000010930}
10931
10932/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
10933static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
10934 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
10935 switch (IntNo) {
10936 default:
10937 // Don't do anything for most intrinsics.
10938 break;
10939
10940 // Vector shifts: check for immediate versions and lower them.
10941 // Note: This is done during DAG combining instead of DAG legalizing because
10942 // the build_vectors for 64-bit vector element shift counts are generally
10943 // not legal, and it is hard to see their values after they get legalized to
10944 // loads from a constant pool.
10945 case Intrinsic::arm_neon_vshifts:
10946 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +000010947 case Intrinsic::arm_neon_vrshifts:
10948 case Intrinsic::arm_neon_vrshiftu:
10949 case Intrinsic::arm_neon_vrshiftn:
10950 case Intrinsic::arm_neon_vqshifts:
10951 case Intrinsic::arm_neon_vqshiftu:
10952 case Intrinsic::arm_neon_vqshiftsu:
10953 case Intrinsic::arm_neon_vqshiftns:
10954 case Intrinsic::arm_neon_vqshiftnu:
10955 case Intrinsic::arm_neon_vqshiftnsu:
10956 case Intrinsic::arm_neon_vqrshiftns:
10957 case Intrinsic::arm_neon_vqrshiftnu:
10958 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010959 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000010960 int64_t Cnt;
10961 unsigned VShiftOpc = 0;
10962
10963 switch (IntNo) {
10964 case Intrinsic::arm_neon_vshifts:
10965 case Intrinsic::arm_neon_vshiftu:
10966 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
10967 VShiftOpc = ARMISD::VSHL;
10968 break;
10969 }
10970 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
10971 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
10972 ARMISD::VSHRs : ARMISD::VSHRu);
10973 break;
10974 }
10975 return SDValue();
10976
Bob Wilson2e076c42009-06-22 23:27:02 +000010977 case Intrinsic::arm_neon_vrshifts:
10978 case Intrinsic::arm_neon_vrshiftu:
10979 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
10980 break;
10981 return SDValue();
10982
10983 case Intrinsic::arm_neon_vqshifts:
10984 case Intrinsic::arm_neon_vqshiftu:
10985 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10986 break;
10987 return SDValue();
10988
10989 case Intrinsic::arm_neon_vqshiftsu:
10990 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
10991 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +000010992 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000010993
Bob Wilson2e076c42009-06-22 23:27:02 +000010994 case Intrinsic::arm_neon_vrshiftn:
10995 case Intrinsic::arm_neon_vqshiftns:
10996 case Intrinsic::arm_neon_vqshiftnu:
10997 case Intrinsic::arm_neon_vqshiftnsu:
10998 case Intrinsic::arm_neon_vqrshiftns:
10999 case Intrinsic::arm_neon_vqrshiftnu:
11000 case Intrinsic::arm_neon_vqrshiftnsu:
11001 // Narrowing shifts require an immediate right shift.
11002 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
11003 break;
Jim Grosbach84511e12010-06-02 21:53:11 +000011004 llvm_unreachable("invalid shift count for narrowing vector shift "
11005 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000011006
11007 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000011008 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +000011009 }
11010
11011 switch (IntNo) {
11012 case Intrinsic::arm_neon_vshifts:
11013 case Intrinsic::arm_neon_vshiftu:
11014 // Opcode already set above.
11015 break;
Bob Wilson2e076c42009-06-22 23:27:02 +000011016 case Intrinsic::arm_neon_vrshifts:
11017 VShiftOpc = ARMISD::VRSHRs; break;
11018 case Intrinsic::arm_neon_vrshiftu:
11019 VShiftOpc = ARMISD::VRSHRu; break;
11020 case Intrinsic::arm_neon_vrshiftn:
11021 VShiftOpc = ARMISD::VRSHRN; break;
11022 case Intrinsic::arm_neon_vqshifts:
11023 VShiftOpc = ARMISD::VQSHLs; break;
11024 case Intrinsic::arm_neon_vqshiftu:
11025 VShiftOpc = ARMISD::VQSHLu; break;
11026 case Intrinsic::arm_neon_vqshiftsu:
11027 VShiftOpc = ARMISD::VQSHLsu; break;
11028 case Intrinsic::arm_neon_vqshiftns:
11029 VShiftOpc = ARMISD::VQSHRNs; break;
11030 case Intrinsic::arm_neon_vqshiftnu:
11031 VShiftOpc = ARMISD::VQSHRNu; break;
11032 case Intrinsic::arm_neon_vqshiftnsu:
11033 VShiftOpc = ARMISD::VQSHRNsu; break;
11034 case Intrinsic::arm_neon_vqrshiftns:
11035 VShiftOpc = ARMISD::VQRSHRNs; break;
11036 case Intrinsic::arm_neon_vqrshiftnu:
11037 VShiftOpc = ARMISD::VQRSHRNu; break;
11038 case Intrinsic::arm_neon_vqrshiftnsu:
11039 VShiftOpc = ARMISD::VQRSHRNsu; break;
11040 }
11041
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011042 SDLoc dl(N);
11043 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
11044 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000011045 }
11046
11047 case Intrinsic::arm_neon_vshiftins: {
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 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
11053 VShiftOpc = ARMISD::VSLI;
11054 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
11055 VShiftOpc = ARMISD::VSRI;
11056 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011057 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +000011058 }
11059
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011060 SDLoc dl(N);
11061 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +000011062 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011063 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000011064 }
11065
11066 case Intrinsic::arm_neon_vqrshifts:
11067 case Intrinsic::arm_neon_vqrshiftu:
11068 // No immediate versions of these to check for.
11069 break;
11070 }
11071
11072 return SDValue();
11073}
11074
11075/// PerformShiftCombine - Checks for immediate versions of vector shifts and
11076/// lowers them. As with the vector shift intrinsics, this is done during DAG
11077/// combining instead of DAG legalizing because the build_vectors for 64-bit
11078/// vector element shift counts are generally not legal, and it is hard to see
11079/// their values after they get legalized to loads from a constant pool.
11080static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
11081 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011082 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +000011083 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
11084 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
11085 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
11086 SDValue N1 = N->getOperand(1);
11087 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
11088 SDValue N0 = N->getOperand(0);
11089 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
11090 DAG.MaskedValueIsZero(N0.getOperand(0),
11091 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +000011092 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +000011093 }
11094 }
Bob Wilson2e076c42009-06-22 23:27:02 +000011095
11096 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +000011097 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11098 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +000011099 return SDValue();
11100
11101 assert(ST->hasNEON() && "unexpected vector shift");
11102 int64_t Cnt;
11103
11104 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011105 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000011106
11107 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011108 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
11109 SDLoc dl(N);
11110 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
11111 DAG.getConstant(Cnt, dl, MVT::i32));
11112 }
Bob Wilson2e076c42009-06-22 23:27:02 +000011113 break;
11114
11115 case ISD::SRA:
11116 case ISD::SRL:
11117 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
11118 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
11119 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011120 SDLoc dl(N);
11121 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
11122 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +000011123 }
11124 }
11125 return SDValue();
11126}
11127
11128/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
11129/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
11130static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
11131 const ARMSubtarget *ST) {
11132 SDValue N0 = N->getOperand(0);
11133
11134 // Check for sign- and zero-extensions of vector extract operations of 8-
11135 // and 16-bit vector elements. NEON supports these directly. They are
11136 // handled during DAG combining because type legalization will promote them
11137 // to 32-bit types and it is messy to recognize the operations after that.
11138 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
11139 SDValue Vec = N0.getOperand(0);
11140 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +000011141 EVT VT = N->getValueType(0);
11142 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +000011143 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
11144
Owen Anderson9f944592009-08-11 20:47:22 +000011145 if (VT == MVT::i32 &&
11146 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +000011147 TLI.isTypeLegal(Vec.getValueType()) &&
11148 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +000011149
11150 unsigned Opc = 0;
11151 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +000011152 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +000011153 case ISD::SIGN_EXTEND:
11154 Opc = ARMISD::VGETLANEs;
11155 break;
11156 case ISD::ZERO_EXTEND:
11157 case ISD::ANY_EXTEND:
11158 Opc = ARMISD::VGETLANEu;
11159 break;
11160 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000011161 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +000011162 }
11163 }
11164
11165 return SDValue();
11166}
11167
James Molloy9d55f192015-11-10 14:22:05 +000011168static void computeKnownBits(SelectionDAG &DAG, SDValue Op, APInt &KnownZero,
11169 APInt &KnownOne) {
11170 if (Op.getOpcode() == ARMISD::BFI) {
11171 // Conservatively, we can recurse down the first operand
11172 // and just mask out all affected bits.
11173 computeKnownBits(DAG, Op.getOperand(0), KnownZero, KnownOne);
11174
11175 // The operand to BFI is already a mask suitable for removing the bits it
11176 // sets.
11177 ConstantSDNode *CI = cast<ConstantSDNode>(Op.getOperand(2));
Benjamin Kramer46e38f32016-06-08 10:01:20 +000011178 const APInt &Mask = CI->getAPIntValue();
James Molloy9d55f192015-11-10 14:22:05 +000011179 KnownZero &= Mask;
11180 KnownOne &= Mask;
11181 return;
11182 }
11183 if (Op.getOpcode() == ARMISD::CMOV) {
11184 APInt KZ2(KnownZero.getBitWidth(), 0);
11185 APInt KO2(KnownOne.getBitWidth(), 0);
11186 computeKnownBits(DAG, Op.getOperand(1), KnownZero, KnownOne);
11187 computeKnownBits(DAG, Op.getOperand(2), KZ2, KO2);
11188
11189 KnownZero &= KZ2;
11190 KnownOne &= KO2;
11191 return;
11192 }
11193 return DAG.computeKnownBits(Op, KnownZero, KnownOne);
11194}
11195
11196SDValue ARMTargetLowering::PerformCMOVToBFICombine(SDNode *CMOV, SelectionDAG &DAG) const {
11197 // If we have a CMOV, OR and AND combination such as:
11198 // if (x & CN)
11199 // y |= CM;
11200 //
11201 // And:
11202 // * CN is a single bit;
11203 // * All bits covered by CM are known zero in y
11204 //
11205 // Then we can convert this into a sequence of BFI instructions. This will
11206 // always be a win if CM is a single bit, will always be no worse than the
11207 // TST&OR sequence if CM is two bits, and for thumb will be no worse if CM is
11208 // three bits (due to the extra IT instruction).
11209
11210 SDValue Op0 = CMOV->getOperand(0);
11211 SDValue Op1 = CMOV->getOperand(1);
James Molloy8e99e972015-11-12 13:49:17 +000011212 auto CCNode = cast<ConstantSDNode>(CMOV->getOperand(2));
11213 auto CC = CCNode->getAPIntValue().getLimitedValue();
James Molloy9d55f192015-11-10 14:22:05 +000011214 SDValue CmpZ = CMOV->getOperand(4);
11215
James Molloy20180912015-11-16 10:49:25 +000011216 // The compare must be against zero.
Artyom Skrobov314ee042015-11-25 19:41:11 +000011217 if (!isNullConstant(CmpZ->getOperand(1)))
James Molloy20180912015-11-16 10:49:25 +000011218 return SDValue();
11219
James Molloy9d55f192015-11-10 14:22:05 +000011220 assert(CmpZ->getOpcode() == ARMISD::CMPZ);
11221 SDValue And = CmpZ->getOperand(0);
11222 if (And->getOpcode() != ISD::AND)
11223 return SDValue();
11224 ConstantSDNode *AndC = dyn_cast<ConstantSDNode>(And->getOperand(1));
11225 if (!AndC || !AndC->getAPIntValue().isPowerOf2())
11226 return SDValue();
11227 SDValue X = And->getOperand(0);
11228
James Molloy8e99e972015-11-12 13:49:17 +000011229 if (CC == ARMCC::EQ) {
11230 // We're performing an "equal to zero" compare. Swap the operands so we
11231 // canonicalize on a "not equal to zero" compare.
11232 std::swap(Op0, Op1);
11233 } else {
11234 assert(CC == ARMCC::NE && "How can a CMPZ node not be EQ or NE?");
11235 }
Junmo Park1108ab02016-02-19 01:46:04 +000011236
James Molloy9d55f192015-11-10 14:22:05 +000011237 if (Op1->getOpcode() != ISD::OR)
11238 return SDValue();
11239
11240 ConstantSDNode *OrC = dyn_cast<ConstantSDNode>(Op1->getOperand(1));
11241 if (!OrC)
11242 return SDValue();
11243 SDValue Y = Op1->getOperand(0);
11244
11245 if (Op0 != Y)
11246 return SDValue();
11247
11248 // Now, is it profitable to continue?
11249 APInt OrCI = OrC->getAPIntValue();
11250 unsigned Heuristic = Subtarget->isThumb() ? 3 : 2;
11251 if (OrCI.countPopulation() > Heuristic)
11252 return SDValue();
11253
11254 // Lastly, can we determine that the bits defined by OrCI
11255 // are zero in Y?
11256 APInt KnownZero, KnownOne;
11257 computeKnownBits(DAG, Y, KnownZero, KnownOne);
11258 if ((OrCI & KnownZero) != OrCI)
11259 return SDValue();
11260
11261 // OK, we can do the combine.
11262 SDValue V = Y;
11263 SDLoc dl(X);
11264 EVT VT = X.getValueType();
11265 unsigned BitInX = AndC->getAPIntValue().logBase2();
Junmo Park1108ab02016-02-19 01:46:04 +000011266
James Molloy9d55f192015-11-10 14:22:05 +000011267 if (BitInX != 0) {
11268 // We must shift X first.
11269 X = DAG.getNode(ISD::SRL, dl, VT, X,
11270 DAG.getConstant(BitInX, dl, VT));
11271 }
11272
11273 for (unsigned BitInY = 0, NumActiveBits = OrCI.getActiveBits();
11274 BitInY < NumActiveBits; ++BitInY) {
11275 if (OrCI[BitInY] == 0)
11276 continue;
11277 APInt Mask(VT.getSizeInBits(), 0);
11278 Mask.setBit(BitInY);
11279 V = DAG.getNode(ARMISD::BFI, dl, VT, V, X,
11280 // Confusingly, the operand is an *inverted* mask.
11281 DAG.getConstant(~Mask, dl, VT));
11282 }
11283
11284 return V;
11285}
11286
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011287/// PerformBRCONDCombine - Target-specific DAG combining for ARMISD::BRCOND.
11288SDValue
11289ARMTargetLowering::PerformBRCONDCombine(SDNode *N, SelectionDAG &DAG) const {
11290 SDValue Cmp = N->getOperand(4);
11291 if (Cmp.getOpcode() != ARMISD::CMPZ)
11292 // Only looking at NE cases.
11293 return SDValue();
11294
11295 EVT VT = N->getValueType(0);
11296 SDLoc dl(N);
11297 SDValue LHS = Cmp.getOperand(0);
11298 SDValue RHS = Cmp.getOperand(1);
11299 SDValue Chain = N->getOperand(0);
11300 SDValue BB = N->getOperand(1);
11301 SDValue ARMcc = N->getOperand(2);
11302 ARMCC::CondCodes CC =
11303 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
11304
11305 // (brcond Chain BB ne CPSR (cmpz (and (cmov 0 1 CC CPSR Cmp) 1) 0))
11306 // -> (brcond Chain BB CC CPSR Cmp)
11307 if (CC == ARMCC::NE && LHS.getOpcode() == ISD::AND && LHS->hasOneUse() &&
11308 LHS->getOperand(0)->getOpcode() == ARMISD::CMOV &&
11309 LHS->getOperand(0)->hasOneUse()) {
11310 auto *LHS00C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(0));
11311 auto *LHS01C = dyn_cast<ConstantSDNode>(LHS->getOperand(0)->getOperand(1));
11312 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
11313 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
11314 if ((LHS00C && LHS00C->getZExtValue() == 0) &&
11315 (LHS01C && LHS01C->getZExtValue() == 1) &&
11316 (LHS1C && LHS1C->getZExtValue() == 1) &&
11317 (RHSC && RHSC->getZExtValue() == 0)) {
11318 return DAG.getNode(
11319 ARMISD::BRCOND, dl, VT, Chain, BB, LHS->getOperand(0)->getOperand(2),
11320 LHS->getOperand(0)->getOperand(3), LHS->getOperand(0)->getOperand(4));
11321 }
11322 }
11323
11324 return SDValue();
11325}
11326
Evan Chengf863e3f2011-07-13 00:42:17 +000011327/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
11328SDValue
11329ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
11330 SDValue Cmp = N->getOperand(4);
11331 if (Cmp.getOpcode() != ARMISD::CMPZ)
11332 // Only looking at EQ and NE cases.
11333 return SDValue();
11334
11335 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000011336 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +000011337 SDValue LHS = Cmp.getOperand(0);
11338 SDValue RHS = Cmp.getOperand(1);
11339 SDValue FalseVal = N->getOperand(0);
11340 SDValue TrueVal = N->getOperand(1);
11341 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +000011342 ARMCC::CondCodes CC =
11343 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +000011344
James Molloy9d55f192015-11-10 14:22:05 +000011345 // BFI is only available on V6T2+.
11346 if (!Subtarget->isThumb1Only() && Subtarget->hasV6T2Ops()) {
11347 SDValue R = PerformCMOVToBFICombine(N, DAG);
11348 if (R)
11349 return R;
11350 }
11351
Evan Chengf863e3f2011-07-13 00:42:17 +000011352 // Simplify
11353 // mov r1, r0
11354 // cmp r1, x
11355 // mov r0, y
11356 // moveq r0, x
11357 // to
11358 // cmp r0, x
11359 // movne r0, y
11360 //
11361 // mov r1, r0
11362 // cmp r1, x
11363 // mov r0, x
11364 // movne r0, y
11365 // to
11366 // cmp r0, x
11367 // movne r0, y
11368 /// FIXME: Turn this into a target neutral optimization?
11369 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +000011370 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +000011371 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
11372 N->getOperand(3), Cmp);
11373 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
11374 SDValue ARMcc;
11375 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
11376 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
11377 N->getOperand(3), NewCmp);
11378 }
11379
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011380 // (cmov F T ne CPSR (cmpz (cmov 0 1 CC CPSR Cmp) 0))
11381 // -> (cmov F T CC CPSR Cmp)
11382 if (CC == ARMCC::NE && LHS.getOpcode() == ARMISD::CMOV && LHS->hasOneUse()) {
11383 auto *LHS0C = dyn_cast<ConstantSDNode>(LHS->getOperand(0));
11384 auto *LHS1C = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
11385 auto *RHSC = dyn_cast<ConstantSDNode>(RHS);
11386 if ((LHS0C && LHS0C->getZExtValue() == 0) &&
11387 (LHS1C && LHS1C->getZExtValue() == 1) &&
11388 (RHSC && RHSC->getZExtValue() == 0)) {
11389 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
11390 LHS->getOperand(2), LHS->getOperand(3),
11391 LHS->getOperand(4));
11392 }
11393 }
11394
Evan Chengf863e3f2011-07-13 00:42:17 +000011395 if (Res.getNode()) {
11396 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +000011397 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +000011398 // Capture demanded bits information that would be otherwise lost.
11399 if (KnownZero == 0xfffffffe)
11400 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11401 DAG.getValueType(MVT::i1));
11402 else if (KnownZero == 0xffffff00)
11403 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11404 DAG.getValueType(MVT::i8));
11405 else if (KnownZero == 0xffff0000)
11406 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
11407 DAG.getValueType(MVT::i16));
11408 }
11409
11410 return Res;
11411}
11412
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011413SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +000011414 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011415 switch (N->getOpcode()) {
11416 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +000011417 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +000011418 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011419 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +000011420 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011421 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +000011422 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
11423 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +000011424 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000011425 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +000011426 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000011427 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +000011428 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +000011429 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +000011430 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +000011431 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +000011432 case ISD::FP_TO_SINT:
Chad Rosiera087fd22015-10-06 20:23:42 +000011433 case ISD::FP_TO_UINT:
11434 return PerformVCVTCombine(N, DCI.DAG, Subtarget);
11435 case ISD::FDIV:
11436 return PerformVDIVCombine(N, DCI.DAG, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011437 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +000011438 case ISD::SHL:
11439 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011440 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +000011441 case ISD::SIGN_EXTEND:
11442 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +000011443 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +000011444 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Peter Collingbourne86b9fbe2016-03-21 18:00:02 +000011445 case ARMISD::BRCOND: return PerformBRCONDCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011446 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011447 case ARMISD::VLD2DUP:
11448 case ARMISD::VLD3DUP:
11449 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011450 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +000011451 case ARMISD::BUILD_VECTOR:
11452 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011453 case ISD::INTRINSIC_VOID:
11454 case ISD::INTRINSIC_W_CHAIN:
11455 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
11456 case Intrinsic::arm_neon_vld1:
11457 case Intrinsic::arm_neon_vld2:
11458 case Intrinsic::arm_neon_vld3:
11459 case Intrinsic::arm_neon_vld4:
11460 case Intrinsic::arm_neon_vld2lane:
11461 case Intrinsic::arm_neon_vld3lane:
11462 case Intrinsic::arm_neon_vld4lane:
11463 case Intrinsic::arm_neon_vst1:
11464 case Intrinsic::arm_neon_vst2:
11465 case Intrinsic::arm_neon_vst3:
11466 case Intrinsic::arm_neon_vst4:
11467 case Intrinsic::arm_neon_vst2lane:
11468 case Intrinsic::arm_neon_vst3lane:
11469 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +000011470 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +000011471 default: break;
11472 }
11473 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011474 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011475 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000011476}
11477
Evan Chengd42641c2011-02-02 01:06:55 +000011478bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
11479 EVT VT) const {
11480 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
11481}
11482
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011483bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
11484 unsigned,
11485 unsigned,
11486 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011487 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000011488 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000011489
11490 switch (VT.getSimpleVT().SimpleTy) {
11491 default:
11492 return false;
11493 case MVT::i8:
11494 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000011495 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011496 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000011497 if (AllowsUnaligned) {
11498 if (Fast)
11499 *Fast = Subtarget->hasV7Ops();
11500 return true;
11501 }
11502 return false;
11503 }
Evan Chengeec6bc62012-08-15 17:44:53 +000011504 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000011505 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000011506 // For any little-endian targets with neon, we can support unaligned ld/st
11507 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000011508 // A big-endian target may also explicitly support unaligned accesses
Mehdi Aminiffc14022015-07-08 01:00:38 +000011509 if (Subtarget->hasNEON() && (AllowsUnaligned || Subtarget->isLittle())) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011510 if (Fast)
11511 *Fast = true;
11512 return true;
11513 }
11514 return false;
11515 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000011516 }
11517}
11518
Lang Hames9929c422011-11-02 22:52:45 +000011519static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
11520 unsigned AlignCheck) {
11521 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
11522 (DstAlign == 0 || DstAlign % AlignCheck == 0));
11523}
11524
11525EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
11526 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000011527 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000011528 bool MemcpyStrSrc,
11529 MachineFunction &MF) const {
11530 const Function *F = MF.getFunction();
11531
11532 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000011533 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
11534 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011535 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000011536 if (Size >= 16 &&
11537 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011538 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011539 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000011540 } else if (Size >= 8 &&
11541 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000011542 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
11543 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000011544 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000011545 }
11546 }
11547
Lang Hamesb85fcd02011-11-08 18:56:23 +000011548 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000011549 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000011550 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000011551 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000011552 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000011553
Lang Hames9929c422011-11-02 22:52:45 +000011554 // Let the target-independent logic figure it out.
11555 return MVT::Other;
11556}
11557
Evan Cheng9ec512d2012-12-06 19:13:27 +000011558bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
11559 if (Val.getOpcode() != ISD::LOAD)
11560 return false;
11561
11562 EVT VT1 = Val.getValueType();
11563 if (!VT1.isSimple() || !VT1.isInteger() ||
11564 !VT2.isSimple() || !VT2.isInteger())
11565 return false;
11566
11567 switch (VT1.getSimpleVT().SimpleTy) {
11568 default: break;
11569 case MVT::i1:
11570 case MVT::i8:
11571 case MVT::i16:
11572 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
11573 return true;
11574 }
11575
11576 return false;
11577}
11578
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000011579bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
11580 EVT VT = ExtVal.getValueType();
11581
11582 if (!isTypeLegal(VT))
11583 return false;
11584
11585 // Don't create a loadext if we can fold the extension into a wide/long
11586 // instruction.
11587 // If there's more than one user instruction, the loadext is desirable no
11588 // matter what. There can be two uses by the same instruction.
11589 if (ExtVal->use_empty() ||
11590 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
11591 return true;
11592
11593 SDNode *U = *ExtVal->use_begin();
11594 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
11595 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
11596 return false;
11597
11598 return true;
11599}
11600
Tim Northovercc2e9032013-08-06 13:58:03 +000011601bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
11602 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
11603 return false;
11604
11605 if (!isTypeLegal(EVT::getEVT(Ty1)))
11606 return false;
11607
11608 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
11609
11610 // Assuming the caller doesn't have a zeroext or signext return parameter,
11611 // truncation all the way down to i1 is valid.
11612 return true;
11613}
11614
Javed Absar85874a92016-10-13 14:57:43 +000011615int ARMTargetLowering::getScalingFactorCost(const DataLayout &DL,
11616 const AddrMode &AM, Type *Ty,
11617 unsigned AS) const {
11618 if (isLegalAddressingMode(DL, AM, Ty, AS)) {
11619 if (Subtarget->hasFPAO())
11620 return AM.Scale < 0 ? 1 : 0; // positive offsets execute faster
11621 return 0;
11622 }
11623 return -1;
11624}
11625
Tim Northovercc2e9032013-08-06 13:58:03 +000011626
Evan Chengdc49a8d2009-08-14 20:09:37 +000011627static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
11628 if (V < 0)
11629 return false;
11630
11631 unsigned Scale = 1;
11632 switch (VT.getSimpleVT().SimpleTy) {
11633 default: return false;
11634 case MVT::i1:
11635 case MVT::i8:
11636 // Scale == 1;
11637 break;
11638 case MVT::i16:
11639 // Scale == 2;
11640 Scale = 2;
11641 break;
11642 case MVT::i32:
11643 // Scale == 4;
11644 Scale = 4;
11645 break;
11646 }
11647
11648 if ((V & (Scale - 1)) != 0)
11649 return false;
11650 V /= Scale;
11651 return V == (V & ((1LL << 5) - 1));
11652}
11653
11654static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
11655 const ARMSubtarget *Subtarget) {
11656 bool isNeg = false;
11657 if (V < 0) {
11658 isNeg = true;
11659 V = - V;
11660 }
11661
11662 switch (VT.getSimpleVT().SimpleTy) {
11663 default: return false;
11664 case MVT::i1:
11665 case MVT::i8:
11666 case MVT::i16:
11667 case MVT::i32:
11668 // + imm12 or - imm8
11669 if (isNeg)
11670 return V == (V & ((1LL << 8) - 1));
11671 return V == (V & ((1LL << 12) - 1));
11672 case MVT::f32:
11673 case MVT::f64:
11674 // Same as ARM mode. FIXME: NEON?
11675 if (!Subtarget->hasVFP2())
11676 return false;
11677 if ((V & 3) != 0)
11678 return false;
11679 V >>= 2;
11680 return V == (V & ((1LL << 8) - 1));
11681 }
11682}
11683
Evan Cheng2150b922007-03-12 23:30:29 +000011684/// isLegalAddressImmediate - Return true if the integer value can be used
11685/// as the offset of the target addressing mode for load / store of the
11686/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011687static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011688 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000011689 if (V == 0)
11690 return true;
11691
Evan Chengce5dfb62009-03-09 19:15:00 +000011692 if (!VT.isSimple())
11693 return false;
11694
Evan Chengdc49a8d2009-08-14 20:09:37 +000011695 if (Subtarget->isThumb1Only())
11696 return isLegalT1AddressImmediate(V, VT);
11697 else if (Subtarget->isThumb2())
11698 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000011699
Evan Chengdc49a8d2009-08-14 20:09:37 +000011700 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000011701 if (V < 0)
11702 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000011703 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000011704 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011705 case MVT::i1:
11706 case MVT::i8:
11707 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000011708 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011709 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011710 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000011711 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011712 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000011713 case MVT::f32:
11714 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011715 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000011716 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000011717 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000011718 return false;
11719 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000011720 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000011721 }
Evan Cheng10043e22007-01-19 07:51:42 +000011722}
11723
Evan Chengdc49a8d2009-08-14 20:09:37 +000011724bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
11725 EVT VT) const {
11726 int Scale = AM.Scale;
11727 if (Scale < 0)
11728 return false;
11729
11730 switch (VT.getSimpleVT().SimpleTy) {
11731 default: return false;
11732 case MVT::i1:
11733 case MVT::i8:
11734 case MVT::i16:
11735 case MVT::i32:
11736 if (Scale == 1)
11737 return true;
11738 // r + r << imm
11739 Scale = Scale & ~1;
11740 return Scale == 2 || Scale == 4 || Scale == 8;
11741 case MVT::i64:
11742 // r + r
11743 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
11744 return true;
11745 return false;
11746 case MVT::isVoid:
11747 // Note, we allow "void" uses (basically, uses that aren't loads or
11748 // stores), because arm allows folding a scale into many arithmetic
11749 // operations. This should be made more precise and revisited later.
11750
11751 // Allow r << imm, but the imm has to be a multiple of two.
11752 if (Scale & 1) return false;
11753 return isPowerOf2_32(Scale);
11754 }
11755}
11756
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011757/// isLegalAddressingMode - Return true if the addressing mode represented
11758/// by AM is legal for this target, for a load/store of the specified type.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011759bool ARMTargetLowering::isLegalAddressingMode(const DataLayout &DL,
11760 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000011761 unsigned AS) const {
Mehdi Amini0cdec1e2015-07-09 02:09:40 +000011762 EVT VT = getValueType(DL, Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000011763 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000011764 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011765
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011766 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000011767 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011768 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011769
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011770 switch (AM.Scale) {
11771 case 0: // no scale reg, must be "r+i" or "r", or "i".
11772 break;
11773 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011774 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011775 return false;
Justin Bognercd1d5aa2016-08-17 20:30:52 +000011776 LLVM_FALLTHROUGH;
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011777 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000011778 // ARM doesn't support any R+R*scale+imm addr modes.
11779 if (AM.BaseOffs)
11780 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011781
Bob Wilson866c1742009-04-08 17:55:28 +000011782 if (!VT.isSimple())
11783 return false;
11784
Evan Chengdc49a8d2009-08-14 20:09:37 +000011785 if (Subtarget->isThumb2())
11786 return isLegalT2ScaledAddressingMode(AM, VT);
11787
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011788 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000011789 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011790 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000011791 case MVT::i1:
11792 case MVT::i8:
11793 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011794 if (Scale < 0) Scale = -Scale;
11795 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011796 return true;
11797 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000011798 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000011799 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000011800 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011801 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000011802 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011803 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000011804 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000011805
Owen Anderson9f944592009-08-11 20:47:22 +000011806 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011807 // Note, we allow "void" uses (basically, uses that aren't loads or
11808 // stores), because arm allows folding a scale into many arithmetic
11809 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000011810
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011811 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000011812 if (Scale & 1) return false;
11813 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011814 }
Evan Cheng2150b922007-03-12 23:30:29 +000011815 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000011816 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000011817}
11818
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011819/// isLegalICmpImmediate - Return true if the specified immediate is legal
11820/// icmp immediate, that is the target has icmp instructions which can compare
11821/// a register against the immediate without having to materialize the
11822/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000011823bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011824 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011825 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011826 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011827 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011828 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000011829 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000011830 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000011831}
11832
Andrew Tricka22cdb72012-07-18 18:34:27 +000011833/// isLegalAddImmediate - Return true if the specified immediate is a legal add
11834/// *or sub* immediate, that is the target has add or sub instructions which can
11835/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000011836/// immediate into a register.
11837bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000011838 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000011839 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000011840 if (!Subtarget->isThumb())
11841 return ARM_AM::getSOImmVal(AbsImm) != -1;
11842 if (Subtarget->isThumb2())
11843 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
11844 // Thumb1 only has 8-bit unsigned immediate.
11845 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000011846}
11847
Owen Anderson53aa7a92009-08-10 22:56:29 +000011848static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011849 bool isSEXTLoad, SDValue &Base,
11850 SDValue &Offset, bool &isInc,
11851 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000011852 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11853 return false;
11854
Owen Anderson9f944592009-08-11 20:47:22 +000011855 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000011856 // AddressingMode 3
11857 Base = Ptr->getOperand(0);
11858 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011859 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011860 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011861 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011862 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011863 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011864 return true;
11865 }
11866 }
11867 isInc = (Ptr->getOpcode() == ISD::ADD);
11868 Offset = Ptr->getOperand(1);
11869 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000011870 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000011871 // AddressingMode 2
11872 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000011873 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000011874 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011875 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000011876 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011877 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000011878 Base = Ptr->getOperand(0);
11879 return true;
11880 }
11881 }
11882
11883 if (Ptr->getOpcode() == ISD::ADD) {
11884 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000011885 ARM_AM::ShiftOpc ShOpcVal=
11886 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000011887 if (ShOpcVal != ARM_AM::no_shift) {
11888 Base = Ptr->getOperand(1);
11889 Offset = Ptr->getOperand(0);
11890 } else {
11891 Base = Ptr->getOperand(0);
11892 Offset = Ptr->getOperand(1);
11893 }
11894 return true;
11895 }
11896
11897 isInc = (Ptr->getOpcode() == ISD::ADD);
11898 Base = Ptr->getOperand(0);
11899 Offset = Ptr->getOperand(1);
11900 return true;
11901 }
11902
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000011903 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000011904 return false;
11905}
11906
Owen Anderson53aa7a92009-08-10 22:56:29 +000011907static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000011908 bool isSEXTLoad, SDValue &Base,
11909 SDValue &Offset, bool &isInc,
11910 SelectionDAG &DAG) {
11911 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
11912 return false;
11913
11914 Base = Ptr->getOperand(0);
11915 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
11916 int RHSC = (int)RHS->getZExtValue();
11917 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
11918 assert(Ptr->getOpcode() == ISD::ADD);
11919 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011920 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011921 return true;
11922 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
11923 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000011924 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000011925 return true;
11926 }
11927 }
11928
11929 return false;
11930}
11931
Evan Cheng10043e22007-01-19 07:51:42 +000011932/// getPreIndexedAddressParts - returns true by value, base pointer and
11933/// offset pointer and addressing mode by reference if the node's address
11934/// can be legally represented as pre-indexed load / store address.
11935bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011936ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
11937 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011938 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011939 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000011940 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000011941 return false;
11942
Owen Anderson53aa7a92009-08-10 22:56:29 +000011943 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011944 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000011945 bool isSEXTLoad = false;
11946 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
11947 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011948 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011949 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
11950 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
11951 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011952 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000011953 } else
11954 return false;
11955
11956 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000011957 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000011958 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000011959 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
11960 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000011961 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000011962 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000011963 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000011964 if (!isLegal)
11965 return false;
11966
11967 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
11968 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000011969}
11970
11971/// getPostIndexedAddressParts - returns true by value, base pointer and
11972/// offset pointer and addressing mode by reference if this node can be
11973/// combined with a load / store to form a post-indexed load / store.
11974bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011975 SDValue &Base,
11976 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000011977 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000011978 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011979 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011980 SDValue Ptr;
James Molloyb3326df2016-07-15 08:03:56 +000011981 bool isSEXTLoad = false, isNonExt;
Evan Cheng10043e22007-01-19 07:51:42 +000011982 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011983 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011984 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000011985 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
James Molloyb3326df2016-07-15 08:03:56 +000011986 isNonExt = LD->getExtensionType() == ISD::NON_EXTLOAD;
Evan Cheng10043e22007-01-19 07:51:42 +000011987 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000011988 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000011989 Ptr = ST->getBasePtr();
James Molloyb3326df2016-07-15 08:03:56 +000011990 isNonExt = !ST->isTruncatingStore();
Evan Cheng10043e22007-01-19 07:51:42 +000011991 } else
11992 return false;
11993
James Molloyb3326df2016-07-15 08:03:56 +000011994 if (Subtarget->isThumb1Only()) {
11995 // Thumb-1 can do a limited post-inc load or store as an updating LDM. It
11996 // must be non-extending/truncating, i32, with an offset of 4.
11997 assert(Op->getValueType(0) == MVT::i32 && "Non-i32 post-inc op?!");
11998 if (Op->getOpcode() != ISD::ADD || !isNonExt)
11999 return false;
12000 auto *RHS = dyn_cast<ConstantSDNode>(Op->getOperand(1));
12001 if (!RHS || RHS->getZExtValue() != 4)
12002 return false;
12003
12004 Offset = Op->getOperand(1);
12005 Base = Op->getOperand(0);
12006 AM = ISD::POST_INC;
12007 return true;
12008 }
12009
Evan Cheng10043e22007-01-19 07:51:42 +000012010 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000012011 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000012012 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000012013 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000012014 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000012015 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000012016 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
12017 isInc, DAG);
12018 if (!isLegal)
12019 return false;
12020
Evan Chengf19384d2010-05-18 21:31:17 +000012021 if (Ptr != Base) {
12022 // Swap base ptr and offset to catch more post-index load / store when
12023 // it's legal. In Thumb2 mode, offset must be an immediate.
12024 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
12025 !Subtarget->isThumb2())
12026 std::swap(Base, Offset);
12027
12028 // Post-indexed load / store update the base pointer.
12029 if (Ptr != Base)
12030 return false;
12031 }
12032
Evan Cheng84c6cda2009-07-02 07:28:31 +000012033 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
12034 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000012035}
12036
Jay Foada0653a32014-05-14 21:14:37 +000012037void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
12038 APInt &KnownZero,
12039 APInt &KnownOne,
12040 const SelectionDAG &DAG,
12041 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000012042 unsigned BitWidth = KnownOne.getBitWidth();
12043 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000012044 switch (Op.getOpcode()) {
12045 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000012046 case ARMISD::ADDC:
12047 case ARMISD::ADDE:
12048 case ARMISD::SUBC:
12049 case ARMISD::SUBE:
12050 // These nodes' second result is a boolean
12051 if (Op.getResNo() == 0)
12052 break;
12053 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
12054 break;
Evan Cheng10043e22007-01-19 07:51:42 +000012055 case ARMISD::CMOV: {
12056 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000012057 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000012058 if (KnownZero == 0 && KnownOne == 0) return;
12059
Dan Gohmanf990faf2008-02-13 00:35:47 +000012060 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000012061 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000012062 KnownZero &= KnownZeroRHS;
12063 KnownOne &= KnownOneRHS;
12064 return;
12065 }
Tim Northover01b4aa92014-04-03 15:10:35 +000012066 case ISD::INTRINSIC_W_CHAIN: {
12067 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
12068 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
12069 switch (IntID) {
12070 default: return;
12071 case Intrinsic::arm_ldaex:
12072 case Intrinsic::arm_ldrex: {
12073 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
Sanjay Patelbd6fca12016-09-14 15:21:00 +000012074 unsigned MemBits = VT.getScalarSizeInBits();
Tim Northover01b4aa92014-04-03 15:10:35 +000012075 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
12076 return;
12077 }
12078 }
12079 }
Evan Cheng10043e22007-01-19 07:51:42 +000012080 }
12081}
12082
12083//===----------------------------------------------------------------------===//
12084// ARM Inline Assembly Support
12085//===----------------------------------------------------------------------===//
12086
Evan Cheng078b0b02011-01-08 01:24:27 +000012087bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
12088 // Looking for "rev" which is V6+.
12089 if (!Subtarget->hasV6Ops())
12090 return false;
12091
12092 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
12093 std::string AsmStr = IA->getAsmString();
12094 SmallVector<StringRef, 4> AsmPieces;
12095 SplitString(AsmStr, AsmPieces, ";\n");
12096
12097 switch (AsmPieces.size()) {
12098 default: return false;
12099 case 1:
12100 AsmStr = AsmPieces[0];
12101 AsmPieces.clear();
12102 SplitString(AsmStr, AsmPieces, " \t,");
12103
12104 // rev $0, $1
12105 if (AsmPieces.size() == 3 &&
12106 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
12107 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000012108 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000012109 if (Ty && Ty->getBitWidth() == 32)
12110 return IntrinsicLowering::LowerToByteSwap(CI);
12111 }
12112 break;
12113 }
12114
12115 return false;
12116}
12117
Silviu Baranga82d04262016-04-25 14:29:18 +000012118const char *ARMTargetLowering::LowerXConstraint(EVT ConstraintVT) const {
12119 // At this point, we have to lower this constraint to something else, so we
12120 // lower it to an "r" or "w". However, by doing this we will force the result
12121 // to be in register, while the X constraint is much more permissive.
12122 //
12123 // Although we are correct (we are free to emit anything, without
12124 // constraints), we might break use cases that would expect us to be more
12125 // efficient and emit something else.
12126 if (!Subtarget->hasVFP2())
12127 return "r";
12128 if (ConstraintVT.isFloatingPoint())
12129 return "w";
12130 if (ConstraintVT.isVector() && Subtarget->hasNEON() &&
12131 (ConstraintVT.getSizeInBits() == 64 ||
12132 ConstraintVT.getSizeInBits() == 128))
12133 return "w";
12134
12135 return "r";
12136}
12137
Evan Cheng10043e22007-01-19 07:51:42 +000012138/// getConstraintType - Given a constraint letter, return the type of
12139/// constraint it is for this target.
12140ARMTargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000012141ARMTargetLowering::getConstraintType(StringRef Constraint) const {
Chris Lattnerd6855142007-03-25 02:14:49 +000012142 if (Constraint.size() == 1) {
12143 switch (Constraint[0]) {
12144 default: break;
12145 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000012146 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000012147 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000012148 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000012149 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000012150 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000012151 // An address with a single base register. Due to the way we
12152 // currently handle addresses it is the same as an 'r' memory constraint.
12153 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000012154 }
Eric Christophere256cd02011-06-21 22:10:57 +000012155 } else if (Constraint.size() == 2) {
12156 switch (Constraint[0]) {
12157 default: break;
12158 // All 'U+' constraints are addresses.
12159 case 'U': return C_Memory;
12160 }
Evan Cheng10043e22007-01-19 07:51:42 +000012161 }
Chris Lattnerd6855142007-03-25 02:14:49 +000012162 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000012163}
12164
John Thompsone8360b72010-10-29 17:29:13 +000012165/// Examine constraint type and operand type and determine a weight value.
12166/// This object must already have been set up with the operand type
12167/// and the current alternative constraint selected.
12168TargetLowering::ConstraintWeight
12169ARMTargetLowering::getSingleConstraintMatchWeight(
12170 AsmOperandInfo &info, const char *constraint) const {
12171 ConstraintWeight weight = CW_Invalid;
12172 Value *CallOperandVal = info.CallOperandVal;
12173 // If we don't have a value, we can't do a match,
12174 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000012175 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000012176 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000012177 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000012178 // Look at the constraint type.
12179 switch (*constraint) {
12180 default:
12181 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
12182 break;
12183 case 'l':
12184 if (type->isIntegerTy()) {
12185 if (Subtarget->isThumb())
12186 weight = CW_SpecificReg;
12187 else
12188 weight = CW_Register;
12189 }
12190 break;
12191 case 'w':
12192 if (type->isFloatingPointTy())
12193 weight = CW_Register;
12194 break;
12195 }
12196 return weight;
12197}
12198
Eric Christophercf2007c2011-06-30 23:50:52 +000012199typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
Benjamin Kramer9bfb6272015-07-05 19:29:18 +000012200RCPair ARMTargetLowering::getRegForInlineAsmConstraint(
12201 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000012202 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000012203 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000012204 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000012205 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000012206 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000012207 return RCPair(0U, &ARM::tGPRRegClass);
12208 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000012209 case 'h': // High regs or no regs.
12210 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000012211 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000012212 break;
Chris Lattner6223e832007-04-02 17:24:08 +000012213 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000012214 if (Subtarget->isThumb1Only())
12215 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000012216 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000012217 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000012218 if (VT == MVT::Other)
12219 break;
Owen Anderson9f944592009-08-11 20:47:22 +000012220 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000012221 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000012222 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000012223 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000012224 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000012225 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000012226 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000012227 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000012228 if (VT == MVT::Other)
12229 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000012230 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000012231 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000012232 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000012233 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000012234 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000012235 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000012236 break;
Eric Christopherc011d312011-07-01 00:30:46 +000012237 case 't':
12238 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000012239 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000012240 break;
Evan Cheng10043e22007-01-19 07:51:42 +000012241 }
12242 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000012243 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000012244 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000012245
Eric Christopher11e4df72015-02-26 22:38:43 +000012246 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000012247}
12248
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012249/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12250/// vector. If it is invalid, don't add anything to Ops.
12251void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000012252 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012253 std::vector<SDValue>&Ops,
12254 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000012255 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012256
Eric Christopherde9399b2011-06-02 23:16:42 +000012257 // Currently only support length 1 constraints.
12258 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000012259
Eric Christopherde9399b2011-06-02 23:16:42 +000012260 char ConstraintLetter = Constraint[0];
12261 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012262 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000012263 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012264 case 'I': case 'J': case 'K': case 'L':
12265 case 'M': case 'N': case 'O':
12266 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
12267 if (!C)
12268 return;
12269
12270 int64_t CVal64 = C->getSExtValue();
12271 int CVal = (int) CVal64;
12272 // None of these constraints allow values larger than 32 bits. Check
12273 // that the value fits in an int.
12274 if (CVal != CVal64)
12275 return;
12276
Eric Christopherde9399b2011-06-02 23:16:42 +000012277 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000012278 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000012279 // Constant suitable for movw, must be between 0 and
12280 // 65535.
12281 if (Subtarget->hasV6T2Ops())
12282 if (CVal >= 0 && CVal <= 65535)
12283 break;
12284 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012285 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000012286 if (Subtarget->isThumb1Only()) {
12287 // This must be a constant between 0 and 255, for ADD
12288 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012289 if (CVal >= 0 && CVal <= 255)
12290 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000012291 } else if (Subtarget->isThumb2()) {
12292 // A constant that can be used as an immediate value in a
12293 // data-processing instruction.
12294 if (ARM_AM::getT2SOImmVal(CVal) != -1)
12295 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012296 } else {
12297 // A constant that can be used as an immediate value in a
12298 // data-processing instruction.
12299 if (ARM_AM::getSOImmVal(CVal) != -1)
12300 break;
12301 }
12302 return;
12303
12304 case 'J':
Eric Christopherb7932302016-01-08 00:34:44 +000012305 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012306 // This must be a constant between -255 and -1, for negated ADD
12307 // immediates. This can be used in GCC with an "n" modifier that
12308 // prints the negated value, for use with SUB instructions. It is
12309 // not useful otherwise but is implemented for compatibility.
12310 if (CVal >= -255 && CVal <= -1)
12311 break;
12312 } else {
12313 // This must be a constant between -4095 and 4095. It is not clear
12314 // what this constraint is intended for. Implemented for
12315 // compatibility with GCC.
12316 if (CVal >= -4095 && CVal <= 4095)
12317 break;
12318 }
12319 return;
12320
12321 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000012322 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012323 // A 32-bit value where only one byte has a nonzero value. Exclude
12324 // zero to match GCC. This constraint is used by GCC internally for
12325 // constants that can be loaded with a move/shift combination.
12326 // It is not useful otherwise but is implemented for compatibility.
12327 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
12328 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000012329 } else if (Subtarget->isThumb2()) {
12330 // A constant whose bitwise inverse can be used as an immediate
12331 // value in a data-processing instruction. This can be used in GCC
12332 // with a "B" modifier that prints the inverted value, for use with
12333 // BIC and MVN instructions. It is not useful otherwise but is
12334 // implemented for compatibility.
12335 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
12336 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012337 } else {
12338 // A constant whose bitwise inverse can be used as an immediate
12339 // value in a data-processing instruction. This can be used in GCC
12340 // with a "B" modifier that prints the inverted value, for use with
12341 // BIC and MVN instructions. It is not useful otherwise but is
12342 // implemented for compatibility.
12343 if (ARM_AM::getSOImmVal(~CVal) != -1)
12344 break;
12345 }
12346 return;
12347
12348 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000012349 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012350 // This must be a constant between -7 and 7,
12351 // for 3-operand ADD/SUB immediate instructions.
12352 if (CVal >= -7 && CVal < 7)
12353 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000012354 } else if (Subtarget->isThumb2()) {
12355 // A constant whose negation can be used as an immediate value in a
12356 // data-processing instruction. This can be used in GCC with an "n"
12357 // modifier that prints the negated value, for use with SUB
12358 // instructions. It is not useful otherwise but is implemented for
12359 // compatibility.
12360 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
12361 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012362 } else {
12363 // A constant whose negation can be used as an immediate value in a
12364 // data-processing instruction. This can be used in GCC with an "n"
12365 // modifier that prints the negated value, for use with SUB
12366 // instructions. It is not useful otherwise but is implemented for
12367 // compatibility.
12368 if (ARM_AM::getSOImmVal(-CVal) != -1)
12369 break;
12370 }
12371 return;
12372
12373 case 'M':
Eric Christopherb7932302016-01-08 00:34:44 +000012374 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012375 // This must be a multiple of 4 between 0 and 1020, for
12376 // ADD sp + immediate.
12377 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
12378 break;
12379 } else {
12380 // A power of two or a constant between 0 and 32. This is used in
12381 // GCC for the shift amount on shifted register operands, but it is
12382 // useful in general for any shift amounts.
12383 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
12384 break;
12385 }
12386 return;
12387
12388 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000012389 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012390 // This must be a constant between 0 and 31, for shift amounts.
12391 if (CVal >= 0 && CVal <= 31)
12392 break;
12393 }
12394 return;
12395
12396 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000012397 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012398 // This must be a multiple of 4 between -508 and 508, for
12399 // ADD/SUB sp = sp + immediate.
12400 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
12401 break;
12402 }
12403 return;
12404 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012405 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012406 break;
12407 }
12408
12409 if (Result.getNode()) {
12410 Ops.push_back(Result);
12411 return;
12412 }
Dale Johannesence97d552010-06-25 21:55:36 +000012413 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000012414}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000012415
Scott Douglassd2974a62015-08-24 09:17:11 +000012416static RTLIB::Libcall getDivRemLibcall(
12417 const SDNode *N, MVT::SimpleValueType SVT) {
Scott Douglassbdef6042015-08-24 09:17:18 +000012418 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
12419 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000012420 "Unhandled Opcode in getDivRemLibcall");
Scott Douglassbdef6042015-08-24 09:17:18 +000012421 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
12422 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000012423 RTLIB::Libcall LC;
12424 switch (SVT) {
12425 default: llvm_unreachable("Unexpected request for libcall!");
12426 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
12427 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
12428 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
12429 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
12430 }
12431 return LC;
12432}
12433
12434static TargetLowering::ArgListTy getDivRemArgList(
Martin Storsjo04864f42016-10-07 13:28:53 +000012435 const SDNode *N, LLVMContext *Context, const ARMSubtarget *Subtarget) {
Scott Douglassbdef6042015-08-24 09:17:18 +000012436 assert((N->getOpcode() == ISD::SDIVREM || N->getOpcode() == ISD::UDIVREM ||
12437 N->getOpcode() == ISD::SREM || N->getOpcode() == ISD::UREM) &&
Scott Douglassd2974a62015-08-24 09:17:11 +000012438 "Unhandled Opcode in getDivRemArgList");
Scott Douglassbdef6042015-08-24 09:17:18 +000012439 bool isSigned = N->getOpcode() == ISD::SDIVREM ||
12440 N->getOpcode() == ISD::SREM;
Scott Douglassd2974a62015-08-24 09:17:11 +000012441 TargetLowering::ArgListTy Args;
12442 TargetLowering::ArgListEntry Entry;
12443 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
12444 EVT ArgVT = N->getOperand(i).getValueType();
12445 Type *ArgTy = ArgVT.getTypeForEVT(*Context);
12446 Entry.Node = N->getOperand(i);
12447 Entry.Ty = ArgTy;
12448 Entry.isSExt = isSigned;
12449 Entry.isZExt = !isSigned;
12450 Args.push_back(Entry);
12451 }
Martin Storsjo04864f42016-10-07 13:28:53 +000012452 if (Subtarget->isTargetWindows() && Args.size() >= 2)
12453 std::swap(Args[0], Args[1]);
Scott Douglassd2974a62015-08-24 09:17:11 +000012454 return Args;
12455}
12456
Renato Golin87610692013-07-16 09:32:17 +000012457SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
Renato Golin6027dd38e2016-02-03 16:10:54 +000012458 assert((Subtarget->isTargetAEABI() || Subtarget->isTargetAndroid() ||
Martin Storsjo04864f42016-10-07 13:28:53 +000012459 Subtarget->isTargetGNUAEABI() || Subtarget->isTargetMuslAEABI() ||
12460 Subtarget->isTargetWindows()) &&
Sumanth Gundapaneni532a1362015-07-31 00:45:12 +000012461 "Register-based DivRem lowering only");
Renato Golin87610692013-07-16 09:32:17 +000012462 unsigned Opcode = Op->getOpcode();
12463 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000012464 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000012465 bool isSigned = (Opcode == ISD::SDIVREM);
12466 EVT VT = Op->getValueType(0);
12467 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
Sjoerd Meijer4dbe73c2016-10-03 10:12:32 +000012468 SDLoc dl(Op);
12469
12470 // If the target has hardware divide, use divide + multiply + subtract:
12471 // div = a / b
12472 // rem = a - b * div
12473 // return {div, rem}
12474 // This should be lowered into UDIV/SDIV + MLS later on.
12475 if (Subtarget->hasDivide() && Op->getValueType(0).isSimple() &&
12476 Op->getSimpleValueType(0) == MVT::i32) {
12477 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV;
12478 const SDValue Dividend = Op->getOperand(0);
12479 const SDValue Divisor = Op->getOperand(1);
12480 SDValue Div = DAG.getNode(DivOpcode, dl, VT, Dividend, Divisor);
12481 SDValue Mul = DAG.getNode(ISD::MUL, dl, VT, Div, Divisor);
12482 SDValue Rem = DAG.getNode(ISD::SUB, dl, VT, Dividend, Mul);
12483
12484 SDValue Values[2] = {Div, Rem};
12485 return DAG.getNode(ISD::MERGE_VALUES, dl, DAG.getVTList(VT, VT), Values);
12486 }
Renato Golin87610692013-07-16 09:32:17 +000012487
Scott Douglassd2974a62015-08-24 09:17:11 +000012488 RTLIB::Libcall LC = getDivRemLibcall(Op.getNode(),
12489 VT.getSimpleVT().SimpleTy);
Renato Golin87610692013-07-16 09:32:17 +000012490 SDValue InChain = DAG.getEntryNode();
12491
Scott Douglassd2974a62015-08-24 09:17:11 +000012492 TargetLowering::ArgListTy Args = getDivRemArgList(Op.getNode(),
Martin Storsjo04864f42016-10-07 13:28:53 +000012493 DAG.getContext(),
12494 Subtarget);
Renato Golin87610692013-07-16 09:32:17 +000012495
12496 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
Mehdi Amini44ede332015-07-09 02:09:04 +000012497 getPointerTy(DAG.getDataLayout()));
Renato Golin87610692013-07-16 09:32:17 +000012498
Reid Kleckner343c3952014-11-20 23:51:47 +000012499 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000012500
Martin Storsjo04864f42016-10-07 13:28:53 +000012501 if (Subtarget->isTargetWindows())
12502 InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain);
12503
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012504 TargetLowering::CallLoweringInfo CLI(DAG);
12505 CLI.setDebugLoc(dl).setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +000012506 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args))
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012507 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000012508
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000012509 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000012510 return CallInfo.first;
12511}
12512
Scott Douglassbdef6042015-08-24 09:17:18 +000012513// Lowers REM using divmod helpers
12514// see RTABI section 4.2/4.3
12515SDValue ARMTargetLowering::LowerREM(SDNode *N, SelectionDAG &DAG) const {
12516 // Build return types (div and rem)
12517 std::vector<Type*> RetTyParams;
12518 Type *RetTyElement;
12519
12520 switch (N->getValueType(0).getSimpleVT().SimpleTy) {
12521 default: llvm_unreachable("Unexpected request for libcall!");
12522 case MVT::i8: RetTyElement = Type::getInt8Ty(*DAG.getContext()); break;
12523 case MVT::i16: RetTyElement = Type::getInt16Ty(*DAG.getContext()); break;
12524 case MVT::i32: RetTyElement = Type::getInt32Ty(*DAG.getContext()); break;
12525 case MVT::i64: RetTyElement = Type::getInt64Ty(*DAG.getContext()); break;
12526 }
12527
12528 RetTyParams.push_back(RetTyElement);
12529 RetTyParams.push_back(RetTyElement);
12530 ArrayRef<Type*> ret = ArrayRef<Type*>(RetTyParams);
12531 Type *RetTy = StructType::get(*DAG.getContext(), ret);
12532
12533 RTLIB::Libcall LC = getDivRemLibcall(N, N->getValueType(0).getSimpleVT().
12534 SimpleTy);
12535 SDValue InChain = DAG.getEntryNode();
Martin Storsjo04864f42016-10-07 13:28:53 +000012536 TargetLowering::ArgListTy Args = getDivRemArgList(N, DAG.getContext(),
12537 Subtarget);
Scott Douglassbdef6042015-08-24 09:17:18 +000012538 bool isSigned = N->getOpcode() == ISD::SREM;
12539 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
12540 getPointerTy(DAG.getDataLayout()));
12541
Martin Storsjo04864f42016-10-07 13:28:53 +000012542 if (Subtarget->isTargetWindows())
12543 InChain = WinDBZCheckDenominator(DAG, N, InChain);
12544
Scott Douglassbdef6042015-08-24 09:17:18 +000012545 // Lower call
12546 CallLoweringInfo CLI(DAG);
12547 CLI.setChain(InChain)
Krzysztof Parzyszeke116d5002016-06-22 12:54:25 +000012548 .setCallee(CallingConv::ARM_AAPCS, RetTy, Callee, std::move(Args))
Scott Douglassbdef6042015-08-24 09:17:18 +000012549 .setSExtResult(isSigned).setZExtResult(!isSigned).setDebugLoc(SDLoc(N));
12550 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
12551
12552 // Return second (rem) result operand (first contains div)
12553 SDNode *ResNode = CallResult.first.getNode();
12554 assert(ResNode->getNumOperands() == 2 && "divmod should return two operands");
12555 return ResNode->getOperand(1);
12556}
12557
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012558SDValue
12559ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
12560 assert(Subtarget->isTargetWindows() && "unsupported target platform");
12561 SDLoc DL(Op);
12562
12563 // Get the inputs.
12564 SDValue Chain = Op.getOperand(0);
12565 SDValue Size = Op.getOperand(1);
12566
12567 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000012568 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012569
12570 SDValue Flag;
12571 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
12572 Flag = Chain.getValue(1);
12573
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000012574 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000012575 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
12576
12577 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
12578 Chain = NewSP.getValue(1);
12579
12580 SDValue Ops[2] = { NewSP, Chain };
12581 return DAG.getMergeValues(Ops, DL);
12582}
12583
Oliver Stannard51b1d462014-08-21 12:50:31 +000012584SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
12585 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
12586 "Unexpected type for custom-lowering FP_EXTEND");
12587
12588 RTLIB::Libcall LC;
12589 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
12590
12591 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000012592 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
12593 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012594}
12595
12596SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
12597 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
12598 Subtarget->isFPOnlySP() &&
12599 "Unexpected type for custom-lowering FP_ROUND");
12600
12601 RTLIB::Libcall LC;
12602 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
12603
12604 SDValue SrcVal = Op.getOperand(0);
Craig Topper8fe40e02015-10-22 17:05:00 +000012605 return makeLibCall(DAG, LC, Op.getValueType(), SrcVal, /*isSigned*/ false,
12606 SDLoc(Op)).first;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012607}
12608
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000012609bool
12610ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
12611 // The ARM target isn't yet aware of offsets.
12612 return false;
12613}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012614
Jim Grosbach11013ed2010-07-16 23:05:05 +000012615bool ARM::isBitFieldInvertedMask(unsigned v) {
12616 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000012617 return false;
12618
Jim Grosbach11013ed2010-07-16 23:05:05 +000012619 // there can be 1's on either or both "outsides", all the "inside"
12620 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000012621 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000012622}
12623
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012624/// isFPImmLegal - Returns true if the target can instruction select the
12625/// specified FP immediate natively. If false, the legalizer will
12626/// materialize the FP immediate as a load from a constant pool.
12627bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
12628 if (!Subtarget->hasVFP3())
12629 return false;
12630 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000012631 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000012632 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000012633 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000012634 return false;
12635}
Bob Wilson5549d492010-09-21 17:56:22 +000012636
Wesley Peck527da1b2010-11-23 03:31:01 +000012637/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000012638/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
12639/// specified in the intrinsic calls.
12640bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
12641 const CallInst &I,
12642 unsigned Intrinsic) const {
12643 switch (Intrinsic) {
12644 case Intrinsic::arm_neon_vld1:
12645 case Intrinsic::arm_neon_vld2:
12646 case Intrinsic::arm_neon_vld3:
12647 case Intrinsic::arm_neon_vld4:
12648 case Intrinsic::arm_neon_vld2lane:
12649 case Intrinsic::arm_neon_vld3lane:
12650 case Intrinsic::arm_neon_vld4lane: {
12651 Info.opc = ISD::INTRINSIC_W_CHAIN;
12652 // Conservatively set memVT to the entire set of vectors loaded.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012653 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012654 uint64_t NumElts = DL.getTypeSizeInBits(I.getType()) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012655 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12656 Info.ptrVal = I.getArgOperand(0);
12657 Info.offset = 0;
12658 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12659 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12660 Info.vol = false; // volatile loads with NEON intrinsics not supported
12661 Info.readMem = true;
12662 Info.writeMem = false;
12663 return true;
12664 }
12665 case Intrinsic::arm_neon_vst1:
12666 case Intrinsic::arm_neon_vst2:
12667 case Intrinsic::arm_neon_vst3:
12668 case Intrinsic::arm_neon_vst4:
12669 case Intrinsic::arm_neon_vst2lane:
12670 case Intrinsic::arm_neon_vst3lane:
12671 case Intrinsic::arm_neon_vst4lane: {
12672 Info.opc = ISD::INTRINSIC_VOID;
12673 // Conservatively set memVT to the entire set of vectors stored.
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012674 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Bob Wilson5549d492010-09-21 17:56:22 +000012675 unsigned NumElts = 0;
12676 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000012677 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000012678 if (!ArgTy->isVectorTy())
12679 break;
Ahmed Bougacha97564c32015-12-09 01:19:50 +000012680 NumElts += DL.getTypeSizeInBits(ArgTy) / 64;
Bob Wilson5549d492010-09-21 17:56:22 +000012681 }
12682 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
12683 Info.ptrVal = I.getArgOperand(0);
12684 Info.offset = 0;
12685 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
12686 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
12687 Info.vol = false; // volatile stores with NEON intrinsics not supported
12688 Info.readMem = false;
12689 Info.writeMem = true;
12690 return true;
12691 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012692 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012693 case Intrinsic::arm_ldrex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012694 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012695 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
12696 Info.opc = ISD::INTRINSIC_W_CHAIN;
12697 Info.memVT = MVT::getVT(PtrTy->getElementType());
12698 Info.ptrVal = I.getArgOperand(0);
12699 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012700 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012701 Info.vol = true;
12702 Info.readMem = true;
12703 Info.writeMem = false;
12704 return true;
12705 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012706 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000012707 case Intrinsic::arm_strex: {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012708 auto &DL = I.getCalledFunction()->getParent()->getDataLayout();
Tim Northovera7ecd242013-07-16 09:46:55 +000012709 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
12710 Info.opc = ISD::INTRINSIC_W_CHAIN;
12711 Info.memVT = MVT::getVT(PtrTy->getElementType());
12712 Info.ptrVal = I.getArgOperand(1);
12713 Info.offset = 0;
Mehdi Aminia749f2a2015-07-09 02:09:52 +000012714 Info.align = DL.getABITypeAlignment(PtrTy->getElementType());
Tim Northovera7ecd242013-07-16 09:46:55 +000012715 Info.vol = true;
12716 Info.readMem = false;
12717 Info.writeMem = true;
12718 return true;
12719 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012720 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012721 case Intrinsic::arm_strexd: {
12722 Info.opc = ISD::INTRINSIC_W_CHAIN;
12723 Info.memVT = MVT::i64;
12724 Info.ptrVal = I.getArgOperand(2);
12725 Info.offset = 0;
12726 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012727 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012728 Info.readMem = false;
12729 Info.writeMem = true;
12730 return true;
12731 }
Tim Northover1ff5f292014-03-26 14:39:31 +000012732 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012733 case Intrinsic::arm_ldrexd: {
12734 Info.opc = ISD::INTRINSIC_W_CHAIN;
12735 Info.memVT = MVT::i64;
12736 Info.ptrVal = I.getArgOperand(0);
12737 Info.offset = 0;
12738 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000012739 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000012740 Info.readMem = true;
12741 Info.writeMem = false;
12742 return true;
12743 }
Bob Wilson5549d492010-09-21 17:56:22 +000012744 default:
12745 break;
12746 }
12747
12748 return false;
12749}
Juergen Ributzka659ce002014-01-28 01:20:14 +000012750
12751/// \brief Returns true if it is beneficial to convert a load of a constant
12752/// to just the constant itself.
12753bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
12754 Type *Ty) const {
12755 assert(Ty->isIntegerTy());
12756
12757 unsigned Bits = Ty->getPrimitiveSizeInBits();
12758 if (Bits == 0 || Bits > 32)
12759 return false;
12760 return true;
12761}
Tim Northover037f26f22014-04-17 18:22:47 +000012762
Robin Morisset5349e8e2014-09-18 18:56:04 +000012763Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
12764 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012765 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000012766
12767 // First, if the target has no DMB, see what fallback we can use.
12768 if (!Subtarget->hasDataBarrier()) {
12769 // Some ARMv6 cpus can support data barriers with an mcr instruction.
12770 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
12771 // here.
12772 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
12773 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
12774 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
12775 Builder.getInt32(0), Builder.getInt32(7),
12776 Builder.getInt32(10), Builder.getInt32(5)};
12777 return Builder.CreateCall(MCR, args);
12778 } else {
12779 // Instead of using barriers, atomic accesses on these subtargets use
12780 // libcalls.
12781 llvm_unreachable("makeDMB on a target so old that it has no barriers");
12782 }
12783 } else {
12784 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
12785 // Only a full system barrier exists in the M-class architectures.
12786 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
12787 Constant *CDomain = Builder.getInt32(Domain);
12788 return Builder.CreateCall(DMB, CDomain);
12789 }
Robin Morisseta47cb412014-09-03 21:01:03 +000012790}
12791
12792// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000012793Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012794 AtomicOrdering Ord, bool IsStore,
12795 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012796 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012797 case AtomicOrdering::NotAtomic:
12798 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012799 llvm_unreachable("Invalid fence: unordered/non-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012800 case AtomicOrdering::Monotonic:
12801 case AtomicOrdering::Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000012802 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012803 case AtomicOrdering::SequentiallyConsistent:
Robin Morisseta47cb412014-09-03 21:01:03 +000012804 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000012805 return nullptr; // Nothing to do
12806 /*FALLTHROUGH*/
JF Bastien800f87a2016-04-06 21:19:33 +000012807 case AtomicOrdering::Release:
12808 case AtomicOrdering::AcquireRelease:
Diana Picusc5baa432016-06-23 07:47:35 +000012809 if (Subtarget->preferISHSTBarriers())
Robin Morissetdedef332014-09-23 20:31:14 +000012810 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000012811 // FIXME: add a comment with a link to documentation justifying this.
12812 else
Robin Morissetdedef332014-09-23 20:31:14 +000012813 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012814 }
Robin Morissetdedef332014-09-23 20:31:14 +000012815 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012816}
12817
Robin Morissetdedef332014-09-23 20:31:14 +000012818Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000012819 AtomicOrdering Ord, bool IsStore,
12820 bool IsLoad) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000012821 switch (Ord) {
JF Bastien800f87a2016-04-06 21:19:33 +000012822 case AtomicOrdering::NotAtomic:
12823 case AtomicOrdering::Unordered:
Robin Morisseta47cb412014-09-03 21:01:03 +000012824 llvm_unreachable("Invalid fence: unordered/not-atomic");
JF Bastien800f87a2016-04-06 21:19:33 +000012825 case AtomicOrdering::Monotonic:
12826 case AtomicOrdering::Release:
Robin Morissetdedef332014-09-23 20:31:14 +000012827 return nullptr; // Nothing to do
JF Bastien800f87a2016-04-06 21:19:33 +000012828 case AtomicOrdering::Acquire:
12829 case AtomicOrdering::AcquireRelease:
12830 case AtomicOrdering::SequentiallyConsistent:
Robin Morissetdedef332014-09-23 20:31:14 +000012831 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000012832 }
Robin Morissetdedef332014-09-23 20:31:14 +000012833 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000012834}
12835
Robin Morisseted3d48f2014-09-03 21:29:59 +000012836// Loads and stores less than 64-bits are already atomic; ones above that
12837// are doomed anyway, so defer to the default libcall and blame the OS when
12838// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12839// anything for those.
12840bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
12841 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
12842 return (Size == 64) && !Subtarget->isMClass();
12843}
Tim Northover037f26f22014-04-17 18:22:47 +000012844
Robin Morisseted3d48f2014-09-03 21:29:59 +000012845// Loads and stores less than 64-bits are already atomic; ones above that
12846// are doomed anyway, so defer to the default libcall and blame the OS when
12847// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
12848// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000012849// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
12850// guarantee, see DDI0406C ARM architecture reference manual,
12851// sections A8.8.72-74 LDRD)
Ahmed Bougacha52468672015-09-11 17:08:28 +000012852TargetLowering::AtomicExpansionKind
12853ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012854 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
Tim Northoverf520eff2015-12-02 18:12:57 +000012855 return ((Size == 64) && !Subtarget->isMClass()) ? AtomicExpansionKind::LLOnly
Ahmed Bougacha52468672015-09-11 17:08:28 +000012856 : AtomicExpansionKind::None;
Robin Morisseted3d48f2014-09-03 21:29:59 +000012857}
12858
12859// For the real atomic operations, we have ldrex/strex up to 32 bits,
12860// and up to 64 bits on the non-M profiles
Ahmed Bougacha52468672015-09-11 17:08:28 +000012861TargetLowering::AtomicExpansionKind
JF Bastienf14889e2015-03-04 15:47:57 +000012862ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000012863 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000012864 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
Ahmed Bougacha9d677132015-09-11 17:08:17 +000012865 ? AtomicExpansionKind::LLSC
12866 : AtomicExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000012867}
12868
Ahmed Bougacha52468672015-09-11 17:08:28 +000012869bool ARMTargetLowering::shouldExpandAtomicCmpXchgInIR(
12870 AtomicCmpXchgInst *AI) const {
Tim Northoverb629c772016-04-18 21:48:55 +000012871 // At -O0, fast-regalloc cannot cope with the live vregs necessary to
12872 // implement cmpxchg without spilling. If the address being exchanged is also
12873 // on the stack and close enough to the spill slot, this can lead to a
12874 // situation where the monitor always gets cleared and the atomic operation
12875 // can never succeed. So at -O0 we need a late-expanded pseudo-inst instead.
12876 return getTargetMachine().getOptLevel() != 0;
Ahmed Bougacha52468672015-09-11 17:08:28 +000012877}
12878
James Y Knightf44fc522016-03-16 22:12:04 +000012879bool ARMTargetLowering::shouldInsertFencesForAtomic(
12880 const Instruction *I) const {
12881 return InsertFencesForAtomic;
12882}
12883
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012884// This has so far only been implemented for MachO.
12885bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000012886 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000012887}
12888
Quentin Colombetc32615d2014-10-31 17:52:53 +000012889bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
12890 unsigned &Cost) const {
12891 // If we do not have NEON, vector types are not natively supported.
12892 if (!Subtarget->hasNEON())
12893 return false;
12894
12895 // Floating point values and vector values map to the same register file.
Benjamin Kramerdf005cb2015-08-08 18:27:36 +000012896 // Therefore, although we could do a store extract of a vector type, this is
Quentin Colombetc32615d2014-10-31 17:52:53 +000012897 // better to leave at float as we have more freedom in the addressing mode for
12898 // those.
12899 if (VectorTy->isFPOrFPVectorTy())
12900 return false;
12901
12902 // If the index is unknown at compile time, this is very expensive to lower
12903 // and it is not possible to combine the store with the extract.
12904 if (!isa<ConstantInt>(Idx))
12905 return false;
12906
12907 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
12908 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
12909 // We can do a store + vector extract on any vector that fits perfectly in a D
12910 // or Q register.
12911 if (BitWidth == 64 || BitWidth == 128) {
12912 Cost = 0;
12913 return true;
12914 }
12915 return false;
12916}
12917
Sanjay Patelaf1b48b2015-11-10 19:24:31 +000012918bool ARMTargetLowering::isCheapToSpeculateCttz() const {
12919 return Subtarget->hasV6T2Ops();
12920}
12921
12922bool ARMTargetLowering::isCheapToSpeculateCtlz() const {
12923 return Subtarget->hasV6T2Ops();
12924}
12925
Tim Northover037f26f22014-04-17 18:22:47 +000012926Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
12927 AtomicOrdering Ord) const {
12928 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12929 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
JF Bastien800f87a2016-04-06 21:19:33 +000012930 bool IsAcquire = isAcquireOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012931
12932 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
12933 // intrinsic must return {i32, i32} and we have to recombine them into a
12934 // single i64 here.
12935 if (ValTy->getPrimitiveSizeInBits() == 64) {
12936 Intrinsic::ID Int =
12937 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
12938 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
12939
12940 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
12941 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
12942
12943 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
12944 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012945 if (!Subtarget->isLittle())
12946 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012947 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
12948 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
12949 return Builder.CreateOr(
12950 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
12951 }
12952
12953 Type *Tys[] = { Addr->getType() };
12954 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
12955 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
12956
12957 return Builder.CreateTruncOrBitCast(
12958 Builder.CreateCall(Ldrex, Addr),
12959 cast<PointerType>(Addr->getType())->getElementType());
12960}
12961
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012962void ARMTargetLowering::emitAtomicCmpXchgNoStoreLLBalance(
12963 IRBuilder<> &Builder) const {
Ahmed Bougachae81610f2015-09-26 00:14:02 +000012964 if (!Subtarget->hasV7Ops())
12965 return;
Ahmed Bougacha81616a72015-09-22 17:22:58 +000012966 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
12967 Builder.CreateCall(llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_clrex));
12968}
12969
Tim Northover037f26f22014-04-17 18:22:47 +000012970Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
12971 Value *Addr,
12972 AtomicOrdering Ord) const {
12973 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
JF Bastien800f87a2016-04-06 21:19:33 +000012974 bool IsRelease = isReleaseOrStronger(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000012975
12976 // Since the intrinsics must have legal type, the i64 intrinsics take two
12977 // parameters: "i32, i32". We must marshal Val into the appropriate form
12978 // before the call.
12979 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
12980 Intrinsic::ID Int =
12981 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
12982 Function *Strex = Intrinsic::getDeclaration(M, Int);
12983 Type *Int32Ty = Type::getInt32Ty(M->getContext());
12984
12985 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
12986 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000012987 if (!Subtarget->isLittle())
12988 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000012989 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000012990 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000012991 }
12992
12993 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
12994 Type *Tys[] = { Addr->getType() };
12995 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
12996
David Blaikieff6409d2015-05-18 22:13:54 +000012997 return Builder.CreateCall(
12998 Strex, {Builder.CreateZExtOrBitCast(
12999 Val, Strex->getFunctionType()->getParamType(0)),
13000 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000013001}
Oliver Stannardc24f2172014-05-09 14:01:47 +000013002
Hao Liu2cd34bb2015-06-26 02:45:36 +000013003/// \brief Lower an interleaved load into a vldN intrinsic.
13004///
13005/// E.g. Lower an interleaved load (Factor = 2):
13006/// %wide.vec = load <8 x i32>, <8 x i32>* %ptr, align 4
13007/// %v0 = shuffle %wide.vec, undef, <0, 2, 4, 6> ; Extract even elements
13008/// %v1 = shuffle %wide.vec, undef, <1, 3, 5, 7> ; Extract odd elements
13009///
13010/// Into:
13011/// %vld2 = { <4 x i32>, <4 x i32> } call llvm.arm.neon.vld2(%ptr, 4)
13012/// %vec0 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 0
13013/// %vec1 = extractelement { <4 x i32>, <4 x i32> } %vld2, i32 1
13014bool ARMTargetLowering::lowerInterleavedLoad(
13015 LoadInst *LI, ArrayRef<ShuffleVectorInst *> Shuffles,
13016 ArrayRef<unsigned> Indices, unsigned Factor) const {
13017 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
13018 "Invalid interleave factor");
13019 assert(!Shuffles.empty() && "Empty shufflevector input");
13020 assert(Shuffles.size() == Indices.size() &&
13021 "Unmatched number of shufflevectors and indices");
13022
13023 VectorType *VecTy = Shuffles[0]->getType();
13024 Type *EltTy = VecTy->getVectorElementType();
13025
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013026 const DataLayout &DL = LI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000013027 unsigned VecSize = DL.getTypeSizeInBits(VecTy);
13028 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000013029
Jeroen Ketemaaebca092015-10-07 14:53:29 +000013030 // Skip if we do not have NEON and skip illegal vector types and vector types
13031 // with i64/f64 elements (vldN doesn't support i64/f64 elements).
13032 if (!Subtarget->hasNEON() || (VecSize != 64 && VecSize != 128) || EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000013033 return false;
13034
13035 // A pointer vector can not be the return type of the ldN intrinsics. Need to
13036 // load integer vectors first and then convert to pointer vectors.
13037 if (EltTy->isPointerTy())
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013038 VecTy =
13039 VectorType::get(DL.getIntPtrType(EltTy), VecTy->getVectorNumElements());
Hao Liu2cd34bb2015-06-26 02:45:36 +000013040
13041 static const Intrinsic::ID LoadInts[3] = {Intrinsic::arm_neon_vld2,
13042 Intrinsic::arm_neon_vld3,
13043 Intrinsic::arm_neon_vld4};
13044
Hao Liu2cd34bb2015-06-26 02:45:36 +000013045 IRBuilder<> Builder(LI);
13046 SmallVector<Value *, 2> Ops;
13047
13048 Type *Int8Ptr = Builder.getInt8PtrTy(LI->getPointerAddressSpace());
13049 Ops.push_back(Builder.CreateBitCast(LI->getPointerOperand(), Int8Ptr));
13050 Ops.push_back(Builder.getInt32(LI->getAlignment()));
13051
Jeroen Ketemaab99b592015-09-30 10:56:37 +000013052 Type *Tys[] = { VecTy, Int8Ptr };
13053 Function *VldnFunc =
13054 Intrinsic::getDeclaration(LI->getModule(), LoadInts[Factor - 2], Tys);
Hao Liu2cd34bb2015-06-26 02:45:36 +000013055 CallInst *VldN = Builder.CreateCall(VldnFunc, Ops, "vldN");
13056
13057 // Replace uses of each shufflevector with the corresponding vector loaded
13058 // by ldN.
13059 for (unsigned i = 0; i < Shuffles.size(); i++) {
13060 ShuffleVectorInst *SV = Shuffles[i];
13061 unsigned Index = Indices[i];
13062
13063 Value *SubVec = Builder.CreateExtractValue(VldN, Index);
13064
13065 // Convert the integer vector to pointer vector if the element is pointer.
13066 if (EltTy->isPointerTy())
13067 SubVec = Builder.CreateIntToPtr(SubVec, SV->getType());
13068
13069 SV->replaceAllUsesWith(SubVec);
13070 }
13071
13072 return true;
13073}
13074
13075/// \brief Get a mask consisting of sequential integers starting from \p Start.
13076///
13077/// I.e. <Start, Start + 1, ..., Start + NumElts - 1>
13078static Constant *getSequentialMask(IRBuilder<> &Builder, unsigned Start,
13079 unsigned NumElts) {
13080 SmallVector<Constant *, 16> Mask;
13081 for (unsigned i = 0; i < NumElts; i++)
13082 Mask.push_back(Builder.getInt32(Start + i));
13083
13084 return ConstantVector::get(Mask);
13085}
13086
13087/// \brief Lower an interleaved store into a vstN intrinsic.
13088///
13089/// E.g. Lower an interleaved store (Factor = 3):
13090/// %i.vec = shuffle <8 x i32> %v0, <8 x i32> %v1,
13091/// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>
13092/// store <12 x i32> %i.vec, <12 x i32>* %ptr, align 4
13093///
13094/// Into:
13095/// %sub.v0 = shuffle <8 x i32> %v0, <8 x i32> v1, <0, 1, 2, 3>
13096/// %sub.v1 = shuffle <8 x i32> %v0, <8 x i32> v1, <4, 5, 6, 7>
13097/// %sub.v2 = shuffle <8 x i32> %v0, <8 x i32> v1, <8, 9, 10, 11>
13098/// call void llvm.arm.neon.vst3(%ptr, %sub.v0, %sub.v1, %sub.v2, 4)
13099///
13100/// Note that the new shufflevectors will be removed and we'll only generate one
13101/// vst3 instruction in CodeGen.
13102bool ARMTargetLowering::lowerInterleavedStore(StoreInst *SI,
13103 ShuffleVectorInst *SVI,
13104 unsigned Factor) const {
13105 assert(Factor >= 2 && Factor <= getMaxSupportedInterleaveFactor() &&
13106 "Invalid interleave factor");
13107
13108 VectorType *VecTy = SVI->getType();
13109 assert(VecTy->getVectorNumElements() % Factor == 0 &&
13110 "Invalid interleaved store");
13111
13112 unsigned NumSubElts = VecTy->getVectorNumElements() / Factor;
13113 Type *EltTy = VecTy->getVectorElementType();
13114 VectorType *SubVecTy = VectorType::get(EltTy, NumSubElts);
13115
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013116 const DataLayout &DL = SI->getModule()->getDataLayout();
Ahmed Bougacha97564c32015-12-09 01:19:50 +000013117 unsigned SubVecSize = DL.getTypeSizeInBits(SubVecTy);
13118 bool EltIs64Bits = DL.getTypeSizeInBits(EltTy) == 64;
Hao Liu2cd34bb2015-06-26 02:45:36 +000013119
Jeroen Ketemaaebca092015-10-07 14:53:29 +000013120 // Skip if we do not have NEON and skip illegal vector types and vector types
13121 // with i64/f64 elements (vstN doesn't support i64/f64 elements).
13122 if (!Subtarget->hasNEON() || (SubVecSize != 64 && SubVecSize != 128) ||
13123 EltIs64Bits)
Hao Liu2cd34bb2015-06-26 02:45:36 +000013124 return false;
13125
13126 Value *Op0 = SVI->getOperand(0);
13127 Value *Op1 = SVI->getOperand(1);
13128 IRBuilder<> Builder(SI);
13129
13130 // StN intrinsics don't support pointer vectors as arguments. Convert pointer
13131 // vectors to integer vectors.
13132 if (EltTy->isPointerTy()) {
Mehdi Aminia749f2a2015-07-09 02:09:52 +000013133 Type *IntTy = DL.getIntPtrType(EltTy);
Hao Liu2cd34bb2015-06-26 02:45:36 +000013134
13135 // Convert to the corresponding integer vector.
13136 Type *IntVecTy =
13137 VectorType::get(IntTy, Op0->getType()->getVectorNumElements());
13138 Op0 = Builder.CreatePtrToInt(Op0, IntVecTy);
13139 Op1 = Builder.CreatePtrToInt(Op1, IntVecTy);
13140
13141 SubVecTy = VectorType::get(IntTy, NumSubElts);
13142 }
13143
Craig Topper26260942015-10-18 05:15:34 +000013144 static const Intrinsic::ID StoreInts[3] = {Intrinsic::arm_neon_vst2,
13145 Intrinsic::arm_neon_vst3,
13146 Intrinsic::arm_neon_vst4};
Hao Liu2cd34bb2015-06-26 02:45:36 +000013147 SmallVector<Value *, 6> Ops;
13148
13149 Type *Int8Ptr = Builder.getInt8PtrTy(SI->getPointerAddressSpace());
13150 Ops.push_back(Builder.CreateBitCast(SI->getPointerOperand(), Int8Ptr));
13151
Jeroen Ketemaab99b592015-09-30 10:56:37 +000013152 Type *Tys[] = { Int8Ptr, SubVecTy };
13153 Function *VstNFunc = Intrinsic::getDeclaration(
13154 SI->getModule(), StoreInts[Factor - 2], Tys);
13155
Hao Liu2cd34bb2015-06-26 02:45:36 +000013156 // Split the shufflevector operands into sub vectors for the new vstN call.
13157 for (unsigned i = 0; i < Factor; i++)
13158 Ops.push_back(Builder.CreateShuffleVector(
13159 Op0, Op1, getSequentialMask(Builder, NumSubElts * i, NumSubElts)));
13160
13161 Ops.push_back(Builder.getInt32(SI->getAlignment()));
13162 Builder.CreateCall(VstNFunc, Ops);
13163 return true;
13164}
13165
Oliver Stannardc24f2172014-05-09 14:01:47 +000013166enum HABaseType {
13167 HA_UNKNOWN = 0,
13168 HA_FLOAT,
13169 HA_DOUBLE,
13170 HA_VECT64,
13171 HA_VECT128
13172};
13173
13174static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
13175 uint64_t &Members) {
Craig Toppere3dcce92015-08-01 22:20:21 +000013176 if (auto *ST = dyn_cast<StructType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000013177 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
13178 uint64_t SubMembers = 0;
13179 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
13180 return false;
13181 Members += SubMembers;
13182 }
Craig Toppere3dcce92015-08-01 22:20:21 +000013183 } else if (auto *AT = dyn_cast<ArrayType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000013184 uint64_t SubMembers = 0;
13185 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
13186 return false;
13187 Members += SubMembers * AT->getNumElements();
13188 } else if (Ty->isFloatTy()) {
13189 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
13190 return false;
13191 Members = 1;
13192 Base = HA_FLOAT;
13193 } else if (Ty->isDoubleTy()) {
13194 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
13195 return false;
13196 Members = 1;
13197 Base = HA_DOUBLE;
Craig Toppere3dcce92015-08-01 22:20:21 +000013198 } else if (auto *VT = dyn_cast<VectorType>(Ty)) {
Oliver Stannardc24f2172014-05-09 14:01:47 +000013199 Members = 1;
13200 switch (Base) {
13201 case HA_FLOAT:
13202 case HA_DOUBLE:
13203 return false;
13204 case HA_VECT64:
13205 return VT->getBitWidth() == 64;
13206 case HA_VECT128:
13207 return VT->getBitWidth() == 128;
13208 case HA_UNKNOWN:
13209 switch (VT->getBitWidth()) {
13210 case 64:
13211 Base = HA_VECT64;
13212 return true;
13213 case 128:
13214 Base = HA_VECT128;
13215 return true;
13216 default:
13217 return false;
13218 }
13219 }
13220 }
13221
13222 return (Members > 0 && Members <= 4);
13223}
13224
Tim Northovere95c5b32015-02-24 17:22:34 +000013225/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
13226/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
13227/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000013228bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
13229 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000013230 if (getEffectiveCallingConv(CallConv, isVarArg) !=
13231 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000013232 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000013233
13234 HABaseType Base = HA_UNKNOWN;
13235 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000013236 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
13237 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
13238
13239 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
13240 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000013241}
Joseph Tremouletf748c892015-11-07 01:11:31 +000013242
13243unsigned ARMTargetLowering::getExceptionPointerRegister(
13244 const Constant *PersonalityFn) const {
13245 // Platforms which do not use SjLj EH may return values in these registers
13246 // via the personality function.
13247 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R0;
13248}
13249
13250unsigned ARMTargetLowering::getExceptionSelectorRegister(
13251 const Constant *PersonalityFn) const {
13252 // Platforms which do not use SjLj EH may return values in these registers
13253 // via the personality function.
13254 return Subtarget->useSjLjEH() ? ARM::NoRegister : ARM::R1;
13255}
Manman Ren5e9e65e2016-01-12 00:47:18 +000013256
13257void ARMTargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const {
13258 // Update IsSplitCSR in ARMFunctionInfo.
13259 ARMFunctionInfo *AFI = Entry->getParent()->getInfo<ARMFunctionInfo>();
13260 AFI->setIsSplitCSR(true);
13261}
13262
13263void ARMTargetLowering::insertCopiesSplitCSR(
13264 MachineBasicBlock *Entry,
13265 const SmallVectorImpl<MachineBasicBlock *> &Exits) const {
13266 const ARMBaseRegisterInfo *TRI = Subtarget->getRegisterInfo();
13267 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent());
13268 if (!IStart)
13269 return;
13270
13271 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
13272 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo();
Manman Rene5f807f2016-01-15 20:24:11 +000013273 MachineBasicBlock::iterator MBBI = Entry->begin();
Manman Ren5e9e65e2016-01-12 00:47:18 +000013274 for (const MCPhysReg *I = IStart; *I; ++I) {
13275 const TargetRegisterClass *RC = nullptr;
13276 if (ARM::GPRRegClass.contains(*I))
13277 RC = &ARM::GPRRegClass;
13278 else if (ARM::DPRRegClass.contains(*I))
13279 RC = &ARM::DPRRegClass;
13280 else
13281 llvm_unreachable("Unexpected register class in CSRsViaCopy!");
13282
13283 unsigned NewVR = MRI->createVirtualRegister(RC);
13284 // Create copy from CSR to a virtual register.
13285 // FIXME: this currently does not emit CFI pseudo-instructions, it works
13286 // fine for CXX_FAST_TLS since the C++-style TLS access functions should be
13287 // nounwind. If we want to generalize this later, we may need to emit
13288 // CFI pseudo-instructions.
13289 assert(Entry->getParent()->getFunction()->hasFnAttribute(
13290 Attribute::NoUnwind) &&
13291 "Function should be nounwind in insertCopiesSplitCSR!");
13292 Entry->addLiveIn(*I);
Manman Rene5f807f2016-01-15 20:24:11 +000013293 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR)
Manman Ren5e9e65e2016-01-12 00:47:18 +000013294 .addReg(*I);
13295
Manman Rene5f807f2016-01-15 20:24:11 +000013296 // Insert the copy-back instructions right before the terminator.
Manman Ren5e9e65e2016-01-12 00:47:18 +000013297 for (auto *Exit : Exits)
Manman Rene5f807f2016-01-15 20:24:11 +000013298 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(),
13299 TII->get(TargetOpcode::COPY), *I)
Manman Ren5e9e65e2016-01-12 00:47:18 +000013300 .addReg(NewVR);
13301 }
13302}